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

Commit cc5adef2 authored by Bookatz's avatar Bookatz
Browse files

Statsd anomaly detection - fixes

Fixes a few items in AnomalyTracker, especially to do with what happens
when an anomaly alarm fires.

Test: unit tests still pass
Change-Id: Ia89bd617442e952e587336b890c3ca67430b5e21
parent d12e276f
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -75,10 +75,8 @@ StatsLogProcessor::~StatsLogProcessor() {
void StatsLogProcessor::onAnomalyAlarmFired(
        const uint64_t timestampNs,
        unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet) {
    for (const auto& anomaly : anomalySet) {
    for (const auto& itr : mMetricsManagers) {
            itr.second->onAnomalyAlarmFired(timestampNs, anomaly);
        }
        itr.second->onAnomalyAlarmFired(timestampNs, anomalySet);
    }
}

+2 −0
Original line number Diff line number Diff line
@@ -44,6 +44,8 @@ public:
    size_t GetMetricsSize(const ConfigKey& key) const;

    void onDumpReport(const ConfigKey& key, vector<uint8_t>* outData);

    /* Tells MetricsManager that the alarms in anomalySet have fired. Modifies anomalySet. */
    void onAnomalyAlarmFired(
            const uint64_t timestampNs,
            unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet);
+3 −4
Original line number Diff line number Diff line
@@ -585,11 +585,10 @@ Status StatsService::informAnomalyAlarmFired() {
    }

    if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded");
    // TODO: check through all counters/timers and see if an anomaly has indeed occurred.
    uint64_t currentTimeNs = time(nullptr) * NS_PER_SEC;
    uint64_t currentTimeSec = time(nullptr);
    std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
            mAnomalyMonitor->onAlarmFired(currentTimeNs);
    mProcessor->onAnomalyAlarmFired(currentTimeNs, anomalySet);
            mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
    mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet);
    return Status::ok();
}

+2 −6
Original line number Diff line number Diff line
@@ -72,7 +72,8 @@ void AnomalyMonitor::remove(sp<const AnomalyAlarm> alarm) {
        return;
    }
    if (DEBUG) ALOGD("Removing alarm with time %u", alarm->timestampSec);
    mPq.remove(alarm);
    bool wasPresent = mPq.remove(alarm);
    if (!wasPresent) return;
    if (mPq.empty()) {
        if (DEBUG) ALOGD("Queue is empty. Cancel any alarm.");
        mRegisteredAlarmTimeSec = 0;
@@ -129,11 +130,6 @@ int64_t AnomalyMonitor::secToMs(uint32_t timeSec) {
    return ((int64_t)timeSec) * 1000;
}

unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> AnomalyMonitor::onAlarmFired(
        uint64_t timestampNs) {
    return popSoonerThan(static_cast<uint32_t>(timestampNs));
}

}  // namespace statsd
}  // namespace os
}  // namespace android
+0 −12
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@

#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

@@ -97,15 +96,6 @@ public:
    unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> popSoonerThan(
            uint32_t timestampSec);

    // TODO: Function that uses popSoonerThan to get all alarms that have fired, and then
    // iterates over all DurationAnomalyTracker, looking for those alarms. When they're found,
    // have them declareAnomaly on those alarms. This means that DurationAnomalyTracker
    // must be thread-safe (since this is being called on a different thread). There is no
    // worry about missing the alarms (due to them being cancelled after this function being called)
    // because DurationAnomalyTracker guarantees that it checks for anaomlies when it cancels
    // alarms anyway.
    // void declareAnomalies(uint32_t timestampSec);

    /**
     * Returns the projected alarm timestamp that is registered with
     * StatsCompanionService. This may not be equal to the soonest alarm,
@@ -115,8 +105,6 @@ public:
        return mRegisteredAlarmTimeSec;
    }

    unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> onAlarmFired(uint64_t timestampNs);

private:
    std::mutex mLock;

Loading