blob: 33e55ab850c9069a4976610f254df891ffddb2f1 [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.
Bookatzc6977972018-01-16 16:55:05 -080054 // Note that this will generally be called from a different thread from the other functions;
55 // the caller is responsible for thread safety.
Bookatz857aaa52017-12-19 15:29:06 -080056 void informAlarmsFired(const uint64_t& timestampNs,
57 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& firedAlarms) override;
58
59protected:
60 // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they
61 // are still active.
62 std::unordered_map<HashableDimensionKey, sp<const AnomalyAlarm>> mAlarms;
63
64 // Anomaly alarm monitor.
65 sp<AnomalyMonitor> mAnomalyMonitor;
66
67 // Resets all bucket data. For use when all the data gets stale.
68 void resetStorage() override;
69
70 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
Bookatz1bf94382018-01-04 11:43:20 -080071 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
72 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
Bookatz857aaa52017-12-19 15:29:06 -080073 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
74 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
Bookatz857aaa52017-12-19 15:29:06 -080075};
76
77} // namespace statsd
78} // namespace os
79} // namespace android