Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 17adac9c authored by yro's avatar yro
Browse files

Finish migrating to use ProtoOutputStream. This change will take the

outputs of each MetricProducer's and merge the results into
ConfigMetricsReport which holds ConfigKey, repeated field of
StatsLogReport, and UidMap. The data will be represented as
vector<uint8_t> which can be passed down to binder call for clients to
pick up. Also, all unnecessary dependencies to stats_log proto have been
removed.

Test: statsd, statsd_test
Change-Id: Ia69137cbc8613644a892e6be1e87b4858bd39fe3
parent 4a3d8447
Loading
Loading
Loading
Loading
+49 −16
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
#include "Log.h"

#include "StatsLogProcessor.h"
#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
#include "metrics/CountMetricProducer.h"
#include "stats_util.h"

@@ -25,6 +24,13 @@
#include <utils/Errors.h>

using namespace android;
using android::util::FIELD_TYPE_BOOL;
using android::util::FIELD_TYPE_FLOAT;
using android::util::FIELD_TYPE_INT32;
using android::util::FIELD_TYPE_INT64;
using android::util::FIELD_TYPE_MESSAGE;
using android::util::FIELD_TYPE_STRING;
using android::util::ProtoOutputStream;
using std::make_unique;
using std::unique_ptr;
using std::vector;
@@ -33,6 +39,14 @@ namespace android {
namespace os {
namespace statsd {

// for ConfigMetricsReport
const int FIELD_ID_CONFIG_KEY = 1;
const int FIELD_ID_METRICS = 2;
const int FIELD_ID_UID_MAP = 3;
// for ConfigKey
const int FIELD_ID_UID = 1;
const int FIELD_ID_NAME = 1;

StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
                                     const std::function<void(const vector<uint8_t>&)>& pushLog)
    : mUidMap(uidMap), mPushLog(pushLog) {
@@ -70,27 +84,46 @@ void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig
    }
}

ConfigMetricsReport StatsLogProcessor::onDumpReport(const ConfigKey& key) {
    ConfigMetricsReport report;

vector<uint8_t> StatsLogProcessor::onDumpReport(const ConfigKey& key) {
    auto it = mMetricsManagers.find(key);
    if (it == mMetricsManagers.end()) {
        ALOGW("Config source %s does not exist", key.ToString().c_str());
        return report;
        return vector<uint8_t>();
    }

    ProtoOutputStream proto;

    // Fill in ConfigKey.
    long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
    proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
    proto.write(FIELD_TYPE_STRING | FIELD_ID_NAME, key.GetName());
    proto.end(configKeyToken);

    // Fill in StatsLogReport's.
    for (auto& m : it->second->onDumpReport()) {
        // Add each vector of StatsLogReport into a repeated field.
        proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_METRICS, reinterpret_cast<char*>(m.get()->data()),
                    m.get()->size());
    }

    auto set_key = report.mutable_config_key();
    set_key->set_uid(key.GetUid());
    set_key->set_name(key.GetName());
    for (auto m : it->second->onDumpReport()) {
        // Transfer the vector of StatsLogReport into a field
        // TODO: perhaps we just have bytes being returned from onDumpReport and transfer bytes
        auto dest = report.add_metrics();
        *dest = m;
    // Fill in UidMap.
    auto uidMap = mUidMap->getOutput(key);
    const int uidMapSize = uidMap.ByteSize();
    char uidMapBuffer[uidMapSize];
    uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize);
    proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize);

    vector<uint8_t> buffer(proto.size());
    size_t pos = 0;
    auto iter = proto.data();
    while (iter.readBuffer() != NULL) {
        size_t toRead = iter.currentToRead();
        std::memcpy(&buffer[pos], iter.readBuffer(), toRead);
        pos += toRead;
        iter.rp()->move(toRead);
    }
    auto temp = mUidMap->getOutput(key);
    report.mutable_uid_map()->Swap(&temp);
    return report;

    return buffer;
}

void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
+1 −2
Original line number Diff line number Diff line
@@ -41,8 +41,7 @@ public:
    void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config);
    void OnConfigRemoved(const ConfigKey& key);

    // TODO: Once we have the ProtoOutputStream in c++, we can just return byte array.
    ConfigMetricsReport onDumpReport(const ConfigKey& key);
    vector<uint8_t> onDumpReport(const ConfigKey& key);

    /* Request a flush through a binder call. */
    void flush();
+0 −1
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@
#include <unordered_map>
#include "../matchers/matcher_util.h"
#include "ConditionTracker.h"
#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "stats_util.h"

+0 −1
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@

#include <vector>
#include "../matchers/matcher_util.h"
#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"

namespace android {
+0 −1
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

#pragma once

#include <frameworks/base/cmds/statsd/src/stats_log.pb.h>
#include "config/ConfigKey.h"

#include <utils/RefBase.h>
Loading