• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "AlarmMonitor.h"
20 #include "AnomalyTracker.h"
21 
22 namespace android {
23 namespace os {
24 namespace statsd {
25 
26 using std::unordered_map;
27 
28 class DurationAnomalyTracker : public virtual AnomalyTracker {
29 public:
30     DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey,
31                            const sp<AlarmMonitor>& alarmMonitor);
32 
33     virtual ~DurationAnomalyTracker();
34 
35     // Sets an alarm for the given timestamp.
36     // Replaces previous alarm if one already exists.
37     void startAlarm(const MetricDimensionKey& dimensionKey, const int64_t& eventTime);
38 
39     // Stops the alarm.
40     // If it should have already fired, but hasn't yet (e.g. because the AlarmManager is delayed),
41     // declare the anomaly now.
42     void stopAlarm(const MetricDimensionKey& dimensionKey, const int64_t& timestampNs);
43 
44     // Stop all the alarms owned by this tracker. Does not declare any anomalies.
45     void cancelAllAlarms();
46 
47     // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker
48     // and removes it from firedAlarms. The AlarmMonitor is not informed.
49     // Note that this will generally be called from a different thread from the other functions;
50     // the caller is responsible for thread safety.
51     void informAlarmsFired(const int64_t& timestampNs,
52             unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) override;
53 
54 protected:
55     // Returns the alarm timestamp in seconds for the query dimension if it exists. Otherwise
56     // returns 0.
getAlarmTimestampSec(const MetricDimensionKey & dimensionKey)57     uint32_t getAlarmTimestampSec(const MetricDimensionKey& dimensionKey) const override {
58         auto it = mAlarms.find(dimensionKey);
59         return it == mAlarms.end() ? 0 : it->second->timestampSec;
60     }
61 
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<MetricDimensionKey, sp<const InternalAlarm>> mAlarms;
65 
66     // Anomaly alarm monitor.
67     sp<AlarmMonitor> mAlarmMonitor;
68 
69     FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
70     FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
71     FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
72     FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
73     FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp);
74     FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop);
75 };
76 
77 }  // namespace statsd
78 }  // namespace os
79 }  // namespace android
80