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

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

Merge "AnomalyDetection invalidity depends on MetricProd"

parents 90f7ad1a 450099db
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -324,6 +324,16 @@ StatsdConfig build_fake_config() {
    durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
    durationMetric->set_what("SCREEN_IS_ON");

    // Anomaly threshold for background count.
    alert = config.add_alert();
    alert->set_name("ALERT_8");
    alert->set_metric_name("METRIC_8");
    alert->set_number_of_buckets(4);
    alert->set_trigger_if_sum_gt(2000000000); // 2 seconds
    alert->set_refractory_period_secs(120);
    details = alert->mutable_incidentd_details();
    details->add_section(-1);

    // Value metric to count KERNEL_WAKELOCK when screen turned on
    ValueMetric* valueMetric = config.add_value_metric();
    valueMetric->set_name("METRIC_6");
+11 −0
Original line number Diff line number Diff line
@@ -103,6 +103,17 @@ DurationMetricProducer::~DurationMetricProducer() {
    VLOG("~DurationMetric() called");
}

sp<AnomalyTracker> DurationMetricProducer::createAnomalyTracker(const Alert &alert) {
    if (alert.trigger_if_sum_gt() > alert.number_of_buckets() * mBucketSizeNs) {
        ALOGW("invalid alert: threshold (%lld) > possible recordable value (%d x %lld)",
              alert.trigger_if_sum_gt(), alert.number_of_buckets(),
              (long long)mBucketSizeNs);
        return nullptr;
    }
    // TODO: return a DurationAnomalyTracker (which should sublclass AnomalyTracker)
    return new AnomalyTracker(alert);
}

void DurationMetricProducer::startNewProtoOutputStreamLocked(long long startTime) {
    mProto = std::make_unique<ProtoOutputStream>();
    mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
+2 −0
Original line number Diff line number Diff line
@@ -45,6 +45,8 @@ public:

    virtual ~DurationMetricProducer();

    virtual sp<AnomalyTracker> createAnomalyTracker(const Alert &alert) override;

    void finish() override;

    // TODO: Implement this later.
+4 −0
Original line number Diff line number Diff line
@@ -93,6 +93,10 @@ public:
        return byteSizeLocked();
    }

    virtual sp<AnomalyTracker> createAnomalyTracker(const Alert &alert) {
        return new AnomalyTracker(alert);
    }

    void addAnomalyTracker(sp<AnomalyTracker> tracker) {
        std::lock_guard<std::mutex> lock(mMutex);
        mAnomalyTrackers.push_back(tracker);
+5 −13
Original line number Diff line number Diff line
@@ -468,21 +468,13 @@ bool initAlerts(const StatsdConfig& config, const unordered_map<string, int>& me
            return false;
        }
        const int metricIndex = itr->second;
        if (alert.trigger_if_sum_gt() >
            (int64_t)alert.number_of_buckets() *
                    allMetricProducers[metricIndex]->getBuckeSizeInNs()) {
            ALOGW("invalid alert: threshold (%lld) > possible recordable value (%d x %lld)",
                  alert.trigger_if_sum_gt(), alert.number_of_buckets(),
                  (long long)allMetricProducers[metricIndex]->getBuckeSizeInNs());
            return false;
        }

        // TODO: Give each MetricProducer a method called createAnomalyTracker(alert), which
        //       creates either an AnomalyTracker or a DurationAnomalyTracker and returns it.
        sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert);
        allMetricProducers[metricIndex]->addAnomalyTracker(anomalyTracker);
        sp<MetricProducer> metric = allMetricProducers[metricIndex];
        sp<AnomalyTracker> anomalyTracker = metric->createAnomalyTracker(alert);
        if (anomalyTracker != nullptr) {
            metric->addAnomalyTracker(anomalyTracker);
            allAnomalyTrackers.push_back(anomalyTracker);
        }
    }
    return true;
}