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

Commit 9bdd372e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Sync statsd to the public proto changes: 1/ use string id/name for all...

Merge "Sync statsd to the public proto changes: 1/ use string id/name for all metrics/configs/matchers. 2/ move alerts out of metric proto."
parents 4769d2fe d1815dc7
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -140,14 +140,8 @@ void DiscreteAnomalyTracker::declareAnomaly() {
    mLastAlarmAtBucketIndex = mCurrentBucketIndex;

    if (mAlert.has_incidentd_details()) {
        const Alert_IncidentdDetails& incident = mAlert.incidentd_details();
        if (incident.has_alert_name()) {
            ALOGW("An anomaly (%s) has occurred! Informing incidentd.",
                  incident.alert_name().c_str());
        } else {
        // TODO: Can construct a name based on the criteria (and/or relay the criteria).
        ALOGW("An anomaly (nameless) has occurred! Informing incidentd.");
        }
        // TODO: Send incidentd_details.name and incidentd_details.incidentd_sections to incidentd
    } else {
        ALOGW("An anomaly has occurred! (But informing incidentd not requested.)");
+16 −14
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ void ConfigManager::update_saved_configs() {
static StatsdConfig build_fake_config() {
    // HACK: Hard code a test metric for counting screen on events...
    StatsdConfig config;
    config.set_config_id(12345L);
    config.set_name("12345");

    int WAKE_LOCK_TAG_ID = 1111;  // put a fake id here to make testing easier.
    int WAKE_LOCK_UID_KEY_ID = 1;
@@ -147,33 +147,35 @@ static StatsdConfig build_fake_config() {

    // Count Screen ON events.
    CountMetric* metric = config.add_count_metric();
    metric->set_metric_id(1);
    metric->set_name("1");
    metric->set_what("SCREEN_TURNED_ON");
    metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);

    // Anomaly threshold for screen-on count.
    Alert* alert = metric->add_alerts();
    Alert* alert = config.add_alerts();
    alert->set_name("1");
    alert->set_number_of_buckets(6);
    alert->set_trigger_if_sum_gt(10);
    alert->set_refractory_period_secs(30);

    // Count process state changes, slice by uid.
    metric = config.add_count_metric();
    metric->set_metric_id(2);
    metric->set_name("2");
    metric->set_what("PROCESS_STATE_CHANGE");
    metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
    KeyMatcher* keyMatcher = metric->add_dimension();
    keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);

    // Anomaly threshold for background count.
    alert = metric->add_alerts();
    alert = config.add_alerts();
    alert->set_name("2");
    alert->set_number_of_buckets(4);
    alert->set_trigger_if_sum_gt(30);
    alert->set_refractory_period_secs(20);

    // Count process state changes, slice by uid, while SCREEN_IS_OFF
    metric = config.add_count_metric();
    metric->set_metric_id(3);
    metric->set_name("3");
    metric->set_what("PROCESS_STATE_CHANGE");
    metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
    keyMatcher = metric->add_dimension();
@@ -182,7 +184,7 @@ static StatsdConfig build_fake_config() {

    // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
    metric = config.add_count_metric();
    metric->set_metric_id(4);
    metric->set_name("4");
    metric->set_what("APP_GET_WL");
    metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
    keyMatcher = metric->add_dimension();
@@ -195,7 +197,7 @@ static StatsdConfig build_fake_config() {

    // Duration of an app holding any wl, while screen on and app in background, slice by uid
    DurationMetric* durationMetric = config.add_duration_metric();
    durationMetric->set_metric_id(5);
    durationMetric->set_name("5");
    durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
    durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
    keyMatcher = durationMetric->add_dimension();
@@ -209,7 +211,7 @@ static StatsdConfig build_fake_config() {

    // max Duration of an app holding any wl, while screen on and app in background, slice by uid
    durationMetric = config.add_duration_metric();
    durationMetric->set_metric_id(6);
    durationMetric->set_name("6");
    durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
    durationMetric->set_type(DurationMetric_AggregationType_DURATION_MAX_SPARSE);
    keyMatcher = durationMetric->add_dimension();
@@ -223,7 +225,7 @@ static StatsdConfig build_fake_config() {

    // Duration of an app holding any wl, while screen on and app in background
    durationMetric = config.add_duration_metric();
    durationMetric->set_metric_id(7);
    durationMetric->set_name("7");
    durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
    durationMetric->set_type(DurationMetric_AggregationType_DURATION_MAX_SPARSE);
    durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
@@ -235,14 +237,14 @@ static StatsdConfig build_fake_config() {

    // Duration of screen on time.
    durationMetric = config.add_duration_metric();
    durationMetric->set_metric_id(8);
    durationMetric->set_name("8");
    durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
    durationMetric->set_type(DurationMetric_AggregationType_DURATION_SUM);
    durationMetric->set_what("SCREEN_IS_ON");

    // Value metric to count KERNEL_WAKELOCK when screen turned on
    ValueMetric* valueMetric = config.add_value_metric();
    valueMetric->set_metric_id(6);
    valueMetric->set_name("6");
    valueMetric->set_what("KERNEL_WAKELOCK");
    valueMetric->set_value_field(1);
    valueMetric->set_condition("SCREEN_IS_ON");
@@ -253,12 +255,12 @@ static StatsdConfig build_fake_config() {

    // Add an EventMetric to log process state change events.
    EventMetric* eventMetric = config.add_event_metric();
    eventMetric->set_metric_id(9);
    eventMetric->set_name("9");
    eventMetric->set_what("SCREEN_TURNED_ON");

    // Add an GaugeMetric.
    GaugeMetric* gaugeMetric = config.add_gauge_metric();
    gaugeMetric->set_metric_id(10);
    gaugeMetric->set_name("10");
    gaugeMetric->set_what("DEVICE_TEMPERATURE");
    gaugeMetric->set_gauge_field(DEVICE_TEMPERATURE_KEY);
    gaugeMetric->mutable_bucket()->set_bucket_size_millis(60 * 1000L);
+11 −21
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ 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::map;
using std::string;
@@ -41,7 +42,7 @@ namespace os {
namespace statsd {

// for StatsLogReport
const int FIELD_ID_METRIC_ID = 1;
const int FIELD_ID_NAME = 1;
const int FIELD_ID_START_REPORT_NANOS = 2;
const int FIELD_ID_END_REPORT_NANOS = 3;
const int FIELD_ID_COUNT_METRICS = 5;
@@ -74,17 +75,6 @@ CountMetricProducer::CountMetricProducer(const CountMetric& metric, const int co
        mBucketSizeNs = LLONG_MAX;
    }

    mAnomalyTrackers.reserve(metric.alerts_size());
    for (int i = 0; i < metric.alerts_size(); i++) {
        const Alert& alert = metric.alerts(i);
        if (alert.trigger_if_sum_gt() > 0 && alert.number_of_buckets() > 0) {
            mAnomalyTrackers.push_back(std::make_unique<DiscreteAnomalyTracker>(alert));
        } else {
            ALOGW("Ignoring invalid count metric alert: threshold=%lld num_buckets= %d",
                  alert.trigger_if_sum_gt(), alert.number_of_buckets());
        }
    }

    // TODO: use UidMap if uid->pkg_name is required
    mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());

@@ -96,7 +86,7 @@ CountMetricProducer::CountMetricProducer(const CountMetric& metric, const int co

    startNewProtoOutputStream(mStartTimeNs);

    VLOG("metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(),
    VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
         (long long)mBucketSizeNs, (long long)mStartTimeNs);
}

@@ -106,7 +96,7 @@ CountMetricProducer::~CountMetricProducer() {

void CountMetricProducer::startNewProtoOutputStream(long long startTime) {
    mProto = std::make_unique<ProtoOutputStream>();
    mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id());
    mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
    mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
    mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
}
@@ -115,7 +105,7 @@ void CountMetricProducer::finish() {
}

void CountMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
    VLOG("Metric %lld onSlicedConditionMayChange", mMetric.metric_id());
    VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
}

std::unique_ptr<std::vector<uint8_t>> CountMetricProducer::onDumpReport() {
@@ -125,7 +115,7 @@ std::unique_ptr<std::vector<uint8_t>> CountMetricProducer::onDumpReport() {
    // If current bucket is still on-going, don't force dump current bucket.
    // In finish(), We can force dump current bucket.
    flushCounterIfNeeded(endTime);
    VLOG("metric %lld dump report now...", mMetric.metric_id());
    VLOG("metric %s dump report now...", mMetric.name().c_str());

    for (const auto& counter : mPastBuckets) {
        const HashableDimensionKey& hashableKey = counter.first;
@@ -175,7 +165,7 @@ std::unique_ptr<std::vector<uint8_t>> CountMetricProducer::onDumpReport() {
    mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS,
                  (long long)mCurrentBucketStartTimeNs);

    VLOG("metric %lld dump report now...", mMetric.metric_id());
    VLOG("metric %s dump report now...", mMetric.name().c_str());
    std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();

    startNewProtoOutputStream(endTime);
@@ -188,7 +178,7 @@ std::unique_ptr<std::vector<uint8_t>> CountMetricProducer::onDumpReport() {
}

void CountMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
    VLOG("Metric %lld onConditionChanged", mMetric.metric_id());
    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
    mCondition = conditionMet;
}

@@ -215,7 +205,7 @@ void CountMetricProducer::onMatchedLogEventInternal(
        count++;
    }

    VLOG("metric %lld %s->%d", mMetric.metric_id(), eventKey.c_str(),
    VLOG("metric %s %s->%d", mMetric.name().c_str(), eventKey.c_str(),
         (*mCurrentSlicedCounter)[eventKey]);
}

@@ -237,7 +227,7 @@ void CountMetricProducer::flushCounterIfNeeded(const uint64_t eventTimeNs) {
        info.mCount = counter.second;
        auto& bucketList = mPastBuckets[counter.first];
        bucketList.push_back(info);
        VLOG("metric %lld, dump key value: %s -> %d", mMetric.metric_id(), counter.first.c_str(),
        VLOG("metric %s, dump key value: %s -> %d", mMetric.name().c_str(), counter.first.c_str(),
             counter.second);
        mByteSize += sizeof(info);
    }
@@ -252,7 +242,7 @@ void CountMetricProducer::flushCounterIfNeeded(const uint64_t eventTimeNs) {

    mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
    mCurrentBucketNum += numBucketsForward;
    VLOG("metric %lld: new bucket start time: %lld", mMetric.metric_id(),
    VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
         (long long)mCurrentBucketStartTimeNs);
}

+7 −6
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ 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::string;
using std::unordered_map;
@@ -39,7 +40,7 @@ namespace os {
namespace statsd {

// for StatsLogReport
const int FIELD_ID_METRIC_ID = 1;
const int FIELD_ID_NAME = 1;
const int FIELD_ID_START_REPORT_NANOS = 2;
const int FIELD_ID_END_REPORT_NANOS = 3;
const int FIELD_ID_DURATION_METRICS = 6;
@@ -91,7 +92,7 @@ DurationMetricProducer::DurationMetricProducer(const DurationMetric& metric,

    startNewProtoOutputStream(mStartTimeNs);

    VLOG("metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(),
    VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
         (long long)mBucketSizeNs, (long long)mStartTimeNs);
}

@@ -101,7 +102,7 @@ DurationMetricProducer::~DurationMetricProducer() {

void DurationMetricProducer::startNewProtoOutputStream(long long startTime) {
    mProto = std::make_unique<ProtoOutputStream>();
    mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id());
    mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
    mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
    mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
}
@@ -126,7 +127,7 @@ void DurationMetricProducer::finish() {
}

void DurationMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
    VLOG("Metric %lld onSlicedConditionMayChange", mMetric.metric_id());
    VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
    // Now for each of the on-going event, check if the condition has changed for them.
    flushIfNeeded(eventTime);
    for (auto& pair : mCurrentSlicedDuration) {
@@ -135,7 +136,7 @@ void DurationMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime
}

void DurationMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
    VLOG("Metric %lld onConditionChanged", mMetric.metric_id());
    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
    mCondition = conditionMet;
    // TODO: need to populate the condition change time from the event which triggers the condition
    // change, instead of using current time.
@@ -167,7 +168,7 @@ std::unique_ptr<std::vector<uint8_t>> DurationMetricProducer::onDumpReport() {
    // If current bucket is still on-going, don't force dump current bucket.
    // In finish(), We can force dump current bucket.
    flushIfNeeded(endTime);
    VLOG("metric %lld dump report now...", mMetric.metric_id());
    VLOG("metric %s dump report now...", mMetric.name().c_str());

    for (const auto& pair : mPastBuckets) {
        const HashableDimensionKey& hashableKey = pair.first;
+6 −5
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ 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_STRING;
using android::util::FIELD_TYPE_MESSAGE;
using android::util::ProtoOutputStream;
using std::map;
@@ -40,7 +41,7 @@ namespace os {
namespace statsd {

// for StatsLogReport
const int FIELD_ID_METRIC_ID = 1;
const int FIELD_ID_NAME = 1;
const int FIELD_ID_START_REPORT_NANOS = 2;
const int FIELD_ID_END_REPORT_NANOS = 3;
const int FIELD_ID_EVENT_METRICS = 4;
@@ -62,7 +63,7 @@ EventMetricProducer::EventMetricProducer(const EventMetric& metric, const int co

    startNewProtoOutputStream(mStartTimeNs);

    VLOG("metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(),
    VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
         (long long)mBucketSizeNs, (long long)mStartTimeNs);
}

@@ -74,7 +75,7 @@ void EventMetricProducer::startNewProtoOutputStream(long long startTime) {
    mProto = std::make_unique<ProtoOutputStream>();
    // TODO: We need to auto-generate the field IDs for StatsLogReport, EventMetricData,
    // and StatsEvent.
    mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id());
    mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
    mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
    mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS);
}
@@ -91,7 +92,7 @@ std::unique_ptr<std::vector<uint8_t>> EventMetricProducer::onDumpReport() {
    mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, endTime);

    size_t bufferSize = mProto->size();
    VLOG("metric %lld dump report now... proto size: %zu ", mMetric.metric_id(), bufferSize);
    VLOG("metric %s dump report now... proto size: %zu ", mMetric.name().c_str(), bufferSize);
    std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();

    startNewProtoOutputStream(endTime);
@@ -101,7 +102,7 @@ std::unique_ptr<std::vector<uint8_t>> EventMetricProducer::onDumpReport() {
}

void EventMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
    VLOG("Metric %lld onConditionChanged", mMetric.metric_id());
    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
    mCondition = conditionMet;
}

Loading