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 #define DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "DurationAnomalyTracker.h"
21 #include "guardrail/StatsdStats.h"
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
DurationAnomalyTracker(const Alert & alert,const ConfigKey & configKey,const sp<AlarmMonitor> & alarmMonitor)27 DurationAnomalyTracker::DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey,
28                                                const sp<AlarmMonitor>& alarmMonitor)
29         : AnomalyTracker(alert, configKey), mAlarmMonitor(alarmMonitor) {
30     VLOG("DurationAnomalyTracker() called");
31 }
32 
~DurationAnomalyTracker()33 DurationAnomalyTracker::~DurationAnomalyTracker() {
34     VLOG("~DurationAnomalyTracker() called");
35     cancelAllAlarms();
36 }
37 
startAlarm(const MetricDimensionKey & dimensionKey,const int64_t & timestampNs)38 void DurationAnomalyTracker::startAlarm(const MetricDimensionKey& dimensionKey,
39                                         const int64_t& timestampNs) {
40     // Alarms are stored in secs. Must round up, since if it fires early, it is ignored completely.
41     uint32_t timestampSec = static_cast<uint32_t>((timestampNs -1) / NS_PER_SEC) + 1; // round up
42     if (isInRefractoryPeriod(timestampNs, dimensionKey)) {
43         VLOG("Not setting anomaly alarm since it would fall in the refractory period.");
44         return;
45     }
46 
47     auto itr = mAlarms.find(dimensionKey);
48     if (itr != mAlarms.end() && mAlarmMonitor != nullptr) {
49         mAlarmMonitor->remove(itr->second);
50     }
51 
52     sp<const InternalAlarm> alarm = new InternalAlarm{timestampSec};
53     mAlarms[dimensionKey] = alarm;
54     if (mAlarmMonitor != nullptr) {
55         mAlarmMonitor->add(alarm);
56     }
57 }
58 
stopAlarm(const MetricDimensionKey & dimensionKey,const int64_t & timestampNs)59 void DurationAnomalyTracker::stopAlarm(const MetricDimensionKey& dimensionKey,
60                                        const int64_t& timestampNs) {
61     const auto itr = mAlarms.find(dimensionKey);
62     if (itr == mAlarms.end()) {
63         return;
64     }
65 
66     // If the alarm is set in the past but hasn't fired yet (due to lag), catch it now.
67     if (itr->second != nullptr && timestampNs >= (int64_t)NS_PER_SEC * itr->second->timestampSec) {
68         declareAnomaly(timestampNs, mAlert.metric_id(), dimensionKey,
69                        mAlert.trigger_if_sum_gt() + (timestampNs / NS_PER_SEC) -
70                                itr->second->timestampSec);
71     }
72     if (mAlarmMonitor != nullptr) {
73         mAlarmMonitor->remove(itr->second);
74     }
75     mAlarms.erase(dimensionKey);
76 }
77 
cancelAllAlarms()78 void DurationAnomalyTracker::cancelAllAlarms() {
79     if (mAlarmMonitor != nullptr) {
80         for (const auto& itr : mAlarms) {
81             mAlarmMonitor->remove(itr.second);
82         }
83     }
84     mAlarms.clear();
85 }
86 
informAlarmsFired(const int64_t & timestampNs,unordered_set<sp<const InternalAlarm>,SpHash<InternalAlarm>> & firedAlarms)87 void DurationAnomalyTracker::informAlarmsFired(const int64_t& timestampNs,
88         unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) {
89 
90     if (firedAlarms.empty() || mAlarms.empty()) return;
91     // Find the intersection of firedAlarms and mAlarms.
92     // The for loop is inefficient, since it loops over all keys, but that's okay since it is very
93     // seldomly called. The alternative would be having InternalAlarms store information about the
94     // DurationAnomalyTracker and key, but that's a lot of data overhead to speed up something that
95     // is rarely ever called.
96     unordered_map<MetricDimensionKey, sp<const InternalAlarm>> matchedAlarms;
97     for (const auto& kv : mAlarms) {
98         if (firedAlarms.count(kv.second) > 0) {
99             matchedAlarms.insert({kv.first, kv.second});
100         }
101     }
102 
103     // Now declare each of these alarms to have fired.
104     for (const auto& kv : matchedAlarms) {
105         declareAnomaly(
106                 timestampNs, mAlert.metric_id(), kv.first,
107                 mAlert.trigger_if_sum_gt() + (timestampNs / NS_PER_SEC) - kv.second->timestampSec);
108         mAlarms.erase(kv.first);
109         firedAlarms.erase(kv.second);  // No one else can also own it, so we're done with it.
110     }
111 }
112 
113 }  // namespace statsd
114 }  // namespace os
115 }  // namespace android
116