blob: 2b56810170e5e63303af464c045f54eb98f5e587 [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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Bookatz857aaa52017-12-19 15:29:06 -080018#include "Log.h"
19
20#include "DurationAnomalyTracker.h"
21#include "guardrail/StatsdStats.h"
22
23namespace android {
24namespace os {
25namespace statsd {
26
Yangster-mac932ecec2018-02-01 10:23:52 -080027DurationAnomalyTracker::DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey,
28 const sp<AlarmMonitor>& alarmMonitor)
Bookatz6a1d3af2018-03-16 15:25:49 -070029 : AnomalyTracker(alert, configKey), mAlarmMonitor(alarmMonitor) {
30 VLOG("DurationAnomalyTracker() called");
Bookatz857aaa52017-12-19 15:29:06 -080031}
32
33DurationAnomalyTracker::~DurationAnomalyTracker() {
Bookatz6a1d3af2018-03-16 15:25:49 -070034 VLOG("~DurationAnomalyTracker() called");
Bookatz3e8cd352018-03-16 10:09:59 -070035 cancelAllAlarms();
Bookatz857aaa52017-12-19 15:29:06 -080036}
37
Yangster-mac93694462018-01-22 20:49:31 -080038void DurationAnomalyTracker::startAlarm(const MetricDimensionKey& dimensionKey,
Yangster-macb142cc82018-03-30 15:22:08 -070039 const int64_t& timestampNs) {
Bookatz66fe0612018-02-07 18:51:48 -080040 // Alarms are stored in secs. Must round up, since if it fires early, it is ignored completely.
Yangster-macbe10ddf2018-03-13 15:39:51 -070041 uint32_t timestampSec = static_cast<uint32_t>((timestampNs -1) / NS_PER_SEC) + 1; // round up
Bookatz1bf94382018-01-04 11:43:20 -080042 if (isInRefractoryPeriod(timestampNs, dimensionKey)) {
Yangster-macbe10ddf2018-03-13 15:39:51 -070043 VLOG("Not setting anomaly alarm since it would fall in the refractory period.");
44 return;
Bookatz857aaa52017-12-19 15:29:06 -080045 }
Bookatz6bf98252018-03-14 10:44:24 -070046
47 auto itr = mAlarms.find(dimensionKey);
48 if (itr != mAlarms.end() && mAlarmMonitor != nullptr) {
49 mAlarmMonitor->remove(itr->second);
50 }
51
Yangster-mac932ecec2018-02-01 10:23:52 -080052 sp<const InternalAlarm> alarm = new InternalAlarm{timestampSec};
Bookatz6bf98252018-03-14 10:44:24 -070053 mAlarms[dimensionKey] = alarm;
Yangster-mac932ecec2018-02-01 10:23:52 -080054 if (mAlarmMonitor != nullptr) {
55 mAlarmMonitor->add(alarm);
Bookatz857aaa52017-12-19 15:29:06 -080056 }
57}
58
Bookatz3e8cd352018-03-16 10:09:59 -070059void DurationAnomalyTracker::stopAlarm(const MetricDimensionKey& dimensionKey,
Yangster-macb142cc82018-03-30 15:22:08 -070060 const int64_t& timestampNs) {
Bookatz3e8cd352018-03-16 10:09:59 -070061 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.
Yangster-macb142cc82018-03-30 15:22:08 -070067 if (itr->second != nullptr && timestampNs >= (int64_t)NS_PER_SEC * itr->second->timestampSec) {
Yao Chen4ce07292019-02-13 13:06:36 -080068 declareAnomaly(timestampNs, mAlert.metric_id(), dimensionKey,
69 mAlert.trigger_if_sum_gt() + (timestampNs / NS_PER_SEC) -
70 itr->second->timestampSec);
Bookatz3e8cd352018-03-16 10:09:59 -070071 }
Bookatz3e8cd352018-03-16 10:09:59 -070072 if (mAlarmMonitor != nullptr) {
73 mAlarmMonitor->remove(itr->second);
Bookatz857aaa52017-12-19 15:29:06 -080074 }
Yao Chen1aa00022018-03-21 17:04:34 -070075 mAlarms.erase(dimensionKey);
Bookatz857aaa52017-12-19 15:29:06 -080076}
77
Bookatz3e8cd352018-03-16 10:09:59 -070078void DurationAnomalyTracker::cancelAllAlarms() {
79 if (mAlarmMonitor != nullptr) {
80 for (const auto& itr : mAlarms) {
81 mAlarmMonitor->remove(itr.second);
82 }
Bookatz857aaa52017-12-19 15:29:06 -080083 }
Bookatz3e8cd352018-03-16 10:09:59 -070084 mAlarms.clear();
Bookatz857aaa52017-12-19 15:29:06 -080085}
86
Yangster-macb142cc82018-03-30 15:22:08 -070087void DurationAnomalyTracker::informAlarmsFired(const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080088 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) {
89
Bookatz857aaa52017-12-19 15:29:06 -080090 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
Yangster-mac932ecec2018-02-01 10:23:52 -080093 // seldomly called. The alternative would be having InternalAlarms store information about the
Yi Jinafb36062018-01-31 19:14:25 -080094 // DurationAnomalyTracker and key, but that's a lot of data overhead to speed up something that
95 // is rarely ever called.
Yangster-mac932ecec2018-02-01 10:23:52 -080096 unordered_map<MetricDimensionKey, sp<const InternalAlarm>> matchedAlarms;
Bookatz857aaa52017-12-19 15:29:06 -080097 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) {
Yao Chen4ce07292019-02-13 13:06:36 -0800105 declareAnomaly(
106 timestampNs, mAlert.metric_id(), kv.first,
107 mAlert.trigger_if_sum_gt() + (timestampNs / NS_PER_SEC) - kv.second->timestampSec);
Bookatz857aaa52017-12-19 15:29:06 -0800108 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