blob: 51186df3e93d9749c1b163c8bca48a5ed1f5d300 [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
Yangster-mac932ecec2018-02-01 10:23:52 -080019#include "AlarmMonitor.h"
Bookatz857aaa52017-12-19 15:29:06 -080020#include "AnomalyTracker.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
26using std::unordered_map;
27
28class DurationAnomalyTracker : public virtual AnomalyTracker {
29public:
Yangster-mac932ecec2018-02-01 10:23:52 -080030 DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey,
31 const sp<AlarmMonitor>& alarmMonitor);
Bookatz857aaa52017-12-19 15:29:06 -080032
33 virtual ~DurationAnomalyTracker();
34
35 // Starts the alarm at the given timestamp.
Yangster-mac93694462018-01-22 20:49:31 -080036 void startAlarm(const MetricDimensionKey& dimensionKey, const uint64_t& eventTime);
Bookatz857aaa52017-12-19 15:29:06 -080037
38 // Stops the alarm.
Yangster-mac93694462018-01-22 20:49:31 -080039 void stopAlarm(const MetricDimensionKey& dimensionKey);
Bookatz857aaa52017-12-19 15:29:06 -080040
41 // Stop all the alarms owned by this tracker.
42 void stopAllAlarms();
43
Bookatz857aaa52017-12-19 15:29:06 -080044 // Declares the anomaly when the alarm expired given the current timestamp.
Yangster-mac93694462018-01-22 20:49:31 -080045 void declareAnomalyIfAlarmExpired(const MetricDimensionKey& dimensionKey,
Bookatz857aaa52017-12-19 15:29:06 -080046 const uint64_t& timestampNs);
47
48 // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker
Bookatz1bf94382018-01-04 11:43:20 -080049 // and removes it from firedAlarms.
Bookatzc6977972018-01-16 16:55:05 -080050 // Note that this will generally be called from a different thread from the other functions;
51 // the caller is responsible for thread safety.
Yangster-mac932ecec2018-02-01 10:23:52 -080052 void informAlarmsFired(const uint64_t& timestampNs,
53 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) override;
Bookatz857aaa52017-12-19 15:29:06 -080054
55protected:
56 // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they
57 // are still active.
Yangster-mac932ecec2018-02-01 10:23:52 -080058 std::unordered_map<MetricDimensionKey, sp<const InternalAlarm>> mAlarms;
Bookatz857aaa52017-12-19 15:29:06 -080059
60 // Anomaly alarm monitor.
Yangster-mac932ecec2018-02-01 10:23:52 -080061 sp<AlarmMonitor> mAlarmMonitor;
Bookatz857aaa52017-12-19 15:29:06 -080062
63 // Resets all bucket data. For use when all the data gets stale.
64 void resetStorage() override;
65
66 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
Bookatz1bf94382018-01-04 11:43:20 -080067 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
68 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
Bookatz857aaa52017-12-19 15:29:06 -080069 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
David Chen2e414b92018-02-12 17:24:40 -080070 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp);
71 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop);
Bookatz857aaa52017-12-19 15:29:06 -080072};
73
74} // namespace statsd
75} // namespace os
76} // namespace android