blob: de7093d3b1b64a98a12e3318352c130dc8636c41 [file] [log] [blame]
Bookatz857aaa52017-12-19 15:29:06 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "AnomalyMonitor.h"
20#include "AnomalyTracker.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
26using std::unordered_map;
27
28class DurationAnomalyTracker : public virtual AnomalyTracker {
29public:
30 DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey);
31
32 virtual ~DurationAnomalyTracker();
33
34 // Starts the alarm at the given timestamp.
35 void startAlarm(const HashableDimensionKey& dimensionKey, const uint64_t& eventTime);
36
37 // Stops the alarm.
38 void stopAlarm(const HashableDimensionKey& dimensionKey);
39
40 // Stop all the alarms owned by this tracker.
41 void stopAllAlarms();
42
43 // Init the AnomalyMonitor which is shared across anomaly trackers.
44 void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) override {
45 mAnomalyMonitor = anomalyMonitor;
46 }
47
48 // Declares the anomaly when the alarm expired given the current timestamp.
49 void declareAnomalyIfAlarmExpired(const HashableDimensionKey& dimensionKey,
50 const uint64_t& timestampNs);
51
52 // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker
Bookatz1bf94382018-01-04 11:43:20 -080053 // and removes it from firedAlarms.
Bookatz857aaa52017-12-19 15:29:06 -080054 // TODO: This will actually be called from a different thread, so make it thread-safe!
55 // This means that almost every function in DurationAnomalyTracker needs to be locked.
56 // But this should be done at the level of StatsLogProcessor, which needs to lock
57 // mMetricsMangers anyway.
58 void informAlarmsFired(const uint64_t& timestampNs,
59 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& firedAlarms) override;
60
61protected:
62 // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they
63 // are still active.
64 std::unordered_map<HashableDimensionKey, sp<const AnomalyAlarm>> mAlarms;
65
66 // Anomaly alarm monitor.
67 sp<AnomalyMonitor> mAnomalyMonitor;
68
69 // Resets all bucket data. For use when all the data gets stale.
70 void resetStorage() override;
71
72 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
Bookatz1bf94382018-01-04 11:43:20 -080073 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
74 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
Bookatz857aaa52017-12-19 15:29:06 -080075 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
76 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
Bookatz857aaa52017-12-19 15:29:06 -080077};
78
79} // namespace statsd
80} // namespace os
81} // namespace android