blob: d30810fc800dd817edc749f4ea471804537daeb8 [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#define DEBUG true // STOPSHIP if true
18#include "Log.h"
19
20#include "DurationAnomalyTracker.h"
21#include "guardrail/StatsdStats.h"
22
23namespace android {
24namespace os {
25namespace statsd {
26
27DurationAnomalyTracker::DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey)
28 : AnomalyTracker(alert, configKey) {
29}
30
31DurationAnomalyTracker::~DurationAnomalyTracker() {
32 stopAllAlarms();
33}
34
35void DurationAnomalyTracker::resetStorage() {
36 AnomalyTracker::resetStorage();
37 if (!mAlarms.empty()) VLOG("AnomalyTracker.resetStorage() called but mAlarms is NOT empty!");
38}
39
40void DurationAnomalyTracker::declareAnomalyIfAlarmExpired(const HashableDimensionKey& dimensionKey,
41 const uint64_t& timestampNs) {
42 auto itr = mAlarms.find(dimensionKey);
43 if (itr == mAlarms.end()) {
44 return;
45 }
46
47 if (itr->second != nullptr &&
48 static_cast<uint32_t>(timestampNs / NS_PER_SEC) >= itr->second->timestampSec) {
49 declareAnomaly(timestampNs);
50 stopAlarm(dimensionKey);
51 }
52}
53
54void DurationAnomalyTracker::startAlarm(const HashableDimensionKey& dimensionKey,
55 const uint64_t& timestampNs) {
56
57 uint32_t timestampSec = static_cast<uint32_t>(timestampNs / NS_PER_SEC);
58 if (isInRefractoryPeriod(timestampNs)) {
59 VLOG("Skipping setting anomaly alarm since it'd fall in the refractory period");
60 return;
61 }
62 sp<const AnomalyAlarm> alarm = new AnomalyAlarm{timestampSec};
63 mAlarms.insert({dimensionKey, alarm});
64 if (mAnomalyMonitor != nullptr) {
65 mAnomalyMonitor->add(alarm);
66 }
67}
68
69void DurationAnomalyTracker::stopAlarm(const HashableDimensionKey& dimensionKey) {
70 auto itr = mAlarms.find(dimensionKey);
71 if (itr != mAlarms.end()) {
72 mAlarms.erase(dimensionKey);
73 if (mAnomalyMonitor != nullptr) {
74 mAnomalyMonitor->remove(itr->second);
75 }
76 }
77}
78
79void DurationAnomalyTracker::stopAllAlarms() {
80 std::set<HashableDimensionKey> keys;
81 for (auto itr = mAlarms.begin(); itr != mAlarms.end(); ++itr) {
82 keys.insert(itr->first);
83 }
84 for (auto key : keys) {
85 stopAlarm(key);
86 }
87}
88
89void DurationAnomalyTracker::informAlarmsFired(const uint64_t& timestampNs,
90 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& firedAlarms) {
91
92 if (firedAlarms.empty() || mAlarms.empty()) return;
93 // Find the intersection of firedAlarms and mAlarms.
94 // The for loop is inefficient, since it loops over all keys, but that's okay since it is very
95 // seldomly called. The alternative would be having AnomalyAlarms store information about the
96 // DurationAnomalyTracker and key, but that's a lot of data overhead to speed up something that is
97 // rarely ever called.
98 unordered_map<HashableDimensionKey, sp<const AnomalyAlarm>> matchedAlarms;
99 for (const auto& kv : mAlarms) {
100 if (firedAlarms.count(kv.second) > 0) {
101 matchedAlarms.insert({kv.first, kv.second});
102 }
103 }
104
105 // Now declare each of these alarms to have fired.
106 for (const auto& kv : matchedAlarms) {
107 declareAnomaly(timestampNs /* TODO: , kv.first */);
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