blob: d3da7dcf241b9c22a12bac9b0c7eb71bd8a72991 [file] [log] [blame]
Yangster-mace2cd6d52017-11-09 20:38:30 -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
Yi Jinafb36062018-01-31 19:14:25 -080019#include <memory> // unique_ptr
20
21#include <stdlib.h>
22
Yangster-mace2cd6d52017-11-09 20:38:30 -080023#include <gtest/gtest_prod.h>
Yi Jinafb36062018-01-31 19:14:25 -080024#include <utils/RefBase.h>
25
Yangster-mac932ecec2018-02-01 10:23:52 -080026#include "AlarmMonitor.h"
Bookatz8f2f3d82017-12-07 13:53:21 -080027#include "config/ConfigKey.h"
Yangster-mace2cd6d52017-11-09 20:38:30 -080028#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert
29#include "stats_util.h" // HashableDimensionKey and DimToValMap
30
Yangster-mace2cd6d52017-11-09 20:38:30 -080031namespace android {
32namespace os {
33namespace statsd {
34
Yangster-mace2cd6d52017-11-09 20:38:30 -080035using std::shared_ptr;
Yi Jinafb36062018-01-31 19:14:25 -080036using std::unordered_map;
Yangster-mace2cd6d52017-11-09 20:38:30 -080037
Bookatzcc5adef2017-11-21 14:36:23 -080038// Does NOT allow negative values.
Yangster-mace2cd6d52017-11-09 20:38:30 -080039class AnomalyTracker : public virtual RefBase {
40public:
Bookatz8f2f3d82017-12-07 13:53:21 -080041 AnomalyTracker(const Alert& alert, const ConfigKey& configKey);
Yangster-mace2cd6d52017-11-09 20:38:30 -080042
43 virtual ~AnomalyTracker();
44
Yangster-mac94e197c2018-01-02 16:03:03 -080045 // Add subscriptions that depend on this alert.
46 void addSubscription(const Subscription& subscription) {
47 mSubscriptions.push_back(subscription);
48 }
49
Bookatz6bf98252018-03-14 10:44:24 -070050 // Adds a bucket for the given bucketNum (index starting at 0).
51 // If a bucket for bucketNum already exists, it will be replaced.
52 // Also, advances to bucketNum (if not in the past), effectively filling any intervening
53 // buckets with 0s.
54 void addPastBucket(std::shared_ptr<DimToValMap> bucket, const int64_t& bucketNum);
55
56 // Inserts (or replaces) the bucket entry for the given bucketNum at the given key to be the
57 // given bucketValue. If the bucket does not exist, it will be created.
58 // Also, advances to bucketNum (if not in the past), effectively filling any intervening
59 // buckets with 0s.
Yangster-mac93694462018-01-22 20:49:31 -080060 void addPastBucket(const MetricDimensionKey& key, const int64_t& bucketValue,
Yangster-mace2cd6d52017-11-09 20:38:30 -080061 const int64_t& bucketNum);
62
Bookatz6bf98252018-03-14 10:44:24 -070063 // Returns true if, based on past buckets plus the new currentBucketValue (which generally
64 // represents the partially-filled current bucket), an anomaly has happened.
Bookatz3e8cd352018-03-16 10:09:59 -070065 // Also advances to currBucketNum-1.
Yangster-mac93694462018-01-22 20:49:31 -080066 bool detectAnomaly(const int64_t& currBucketNum, const MetricDimensionKey& key,
Yangster-mace2cd6d52017-11-09 20:38:30 -080067 const int64_t& currentBucketValue);
68
69 // Informs incidentd about the detected alert.
Yangster-mac93694462018-01-22 20:49:31 -080070 void declareAnomaly(const uint64_t& timestampNs, const MetricDimensionKey& key);
Yangster-mace2cd6d52017-11-09 20:38:30 -080071
Bookatz6bf98252018-03-14 10:44:24 -070072 // Detects if, based on past buckets plus the new currentBucketValue (which generally
73 // represents the partially-filled current bucket), an anomaly has happened, and if so,
74 // declares an anomaly and informs relevant subscribers.
Bookatz3e8cd352018-03-16 10:09:59 -070075 // Also advances to currBucketNum-1.
Bookatzcc5adef2017-11-21 14:36:23 -080076 void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum,
Yi Jinafb36062018-01-31 19:14:25 -080077 const MetricDimensionKey& key, const int64_t& currentBucketValue);
Yangster-mace2cd6d52017-11-09 20:38:30 -080078
Yangster-mac932ecec2018-02-01 10:23:52 -080079 // Init the AlarmMonitor which is shared across anomaly trackers.
80 virtual void setAlarmMonitor(const sp<AlarmMonitor>& alarmMonitor) {
81 return; // Base AnomalyTracker class has no need for the AlarmMonitor.
Yangster-mace2cd6d52017-11-09 20:38:30 -080082 }
83
Bookatz6bf98252018-03-14 10:44:24 -070084 // Returns the sum of all past bucket values for the given dimension key.
Yangster-mac93694462018-01-22 20:49:31 -080085 int64_t getSumOverPastBuckets(const MetricDimensionKey& key) const;
Yangster-mace2cd6d52017-11-09 20:38:30 -080086
Bookatz6bf98252018-03-14 10:44:24 -070087 // Returns the value for a past bucket, or 0 if that bucket doesn't exist.
Yangster-mac93694462018-01-22 20:49:31 -080088 int64_t getPastBucketValue(const MetricDimensionKey& key, const int64_t& bucketNum) const;
Yangster-mace2cd6d52017-11-09 20:38:30 -080089
Bookatz6bf98252018-03-14 10:44:24 -070090 // Returns the anomaly threshold set in the configuration.
Yangster-mace2cd6d52017-11-09 20:38:30 -080091 inline int64_t getAnomalyThreshold() const {
92 return mAlert.trigger_if_sum_gt();
93 }
94
Bookatz6bf98252018-03-14 10:44:24 -070095 // Returns the refractory period ending timestamp (in seconds) for the given key.
96 // Before this moment, any detected anomaly will be ignored.
Bookatz1bf94382018-01-04 11:43:20 -080097 // If there is no stored refractory period ending timestamp, returns 0.
Yangster-mac93694462018-01-22 20:49:31 -080098 uint32_t getRefractoryPeriodEndsSec(const MetricDimensionKey& key) const {
Bookatz1bf94382018-01-04 11:43:20 -080099 const auto& it = mRefractoryPeriodEndsSec.find(key);
100 return it != mRefractoryPeriodEndsSec.end() ? it->second : 0;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101 }
102
Bookatz6bf98252018-03-14 10:44:24 -0700103 // Returns the (constant) number of past buckets this anomaly tracker can store.
Bookatzcc5adef2017-11-21 14:36:23 -0800104 inline int getNumOfPastBuckets() const {
105 return mNumOfPastBuckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800106 }
107
Bookatzcc5adef2017-11-21 14:36:23 -0800108 // Declares an anomaly for each alarm in firedAlarms that belongs to this AnomalyTracker,
Yangster-mac932ecec2018-02-01 10:23:52 -0800109 // and removes it from firedAlarms. Does NOT remove the alarm from the AlarmMonitor.
110 virtual void informAlarmsFired(const uint64_t& timestampNs,
111 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) {
112 return; // The base AnomalyTracker class doesn't have alarms.
Bookatz857aaa52017-12-19 15:29:06 -0800113 }
Bookatzcc5adef2017-11-21 14:36:23 -0800114
Yangster-mace2cd6d52017-11-09 20:38:30 -0800115protected:
Yangster-mace2cd6d52017-11-09 20:38:30 -0800116 // statsd_config.proto Alert message that defines this tracker.
117 const Alert mAlert;
118
Yangster-mac94e197c2018-01-02 16:03:03 -0800119 // The subscriptions that depend on this alert.
120 std::vector<Subscription> mSubscriptions;
121
Bookatz8f2f3d82017-12-07 13:53:21 -0800122 // A reference to the Alert's config key.
123 const ConfigKey& mConfigKey;
124
Bookatzcc5adef2017-11-21 14:36:23 -0800125 // Number of past buckets. One less than the total number of buckets needed
126 // for the anomaly detection (since the current bucket is not in the past).
Bookatz2fb56532018-03-08 11:16:48 -0800127 const int mNumOfPastBuckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800128
Bookatz6bf98252018-03-14 10:44:24 -0700129 // Values for each of the past mNumOfPastBuckets buckets. Always of size mNumOfPastBuckets.
130 // mPastBuckets[i] can be null, meaning that no data is present in that bucket.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800131 std::vector<shared_ptr<DimToValMap>> mPastBuckets;
132
Bookatz6bf98252018-03-14 10:44:24 -0700133 // Cached sum over all existing buckets in mPastBuckets.
134 // Its buckets never contain entries of 0.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800135 DimToValMap mSumOverPastBuckets;
136
137 // The bucket number of the last added bucket.
138 int64_t mMostRecentBucketNum = -1;
139
Bookatz1bf94382018-01-04 11:43:20 -0800140 // Map from each dimension to the timestamp that its refractory period (if this anomaly was
Bookatz6bf98252018-03-14 10:44:24 -0700141 // declared for that dimension) ends, in seconds. From this moment and onwards, anomalies
142 // can be declared again.
Bookatz1bf94382018-01-04 11:43:20 -0800143 // Entries may be, but are not guaranteed to be, removed after the period is finished.
Yangster-mac93694462018-01-22 20:49:31 -0800144 unordered_map<MetricDimensionKey, uint32_t> mRefractoryPeriodEndsSec;
Bookatz857aaa52017-12-19 15:29:06 -0800145
Bookatz6bf98252018-03-14 10:44:24 -0700146 // Advances mMostRecentBucketNum to bucketNum, deleting any data that is now too old.
147 // Specifically, since it is now too old, removes the data for
148 // [mMostRecentBucketNum - mNumOfPastBuckets + 1, bucketNum - mNumOfPastBuckets].
149 void advanceMostRecentBucketTo(const int64_t& bucketNum);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800150
151 // Add the information in the given bucket to mSumOverPastBuckets.
152 void addBucketToSum(const shared_ptr<DimToValMap>& bucket);
153
154 // Subtract the information in the given bucket from mSumOverPastBuckets
155 // and remove any items with value 0.
156 void subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket);
157
Bookatz6bf98252018-03-14 10:44:24 -0700158 // From mSumOverPastBuckets[key], subtracts bucketValue, removing it if it is now 0.
159 void subtractValueFromSum(const MetricDimensionKey& key, const int64_t& bucketValue);
160
161 // Returns true if in the refractory period, else false.
162 // If there is a stored refractory period but it ended prior to timestampNs, it is removed.
Yangster-mac93694462018-01-22 20:49:31 -0800163 bool isInRefractoryPeriod(const uint64_t& timestampNs, const MetricDimensionKey& key);
Bookatzcc5adef2017-11-21 14:36:23 -0800164
Yangster-mace2cd6d52017-11-09 20:38:30 -0800165 // Calculates the corresponding bucket index within the circular array.
Bookatz6bf98252018-03-14 10:44:24 -0700166 // Requires bucketNum >= 0.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800167 size_t index(int64_t bucketNum) const;
168
Bookatzcc5adef2017-11-21 14:36:23 -0800169 // Resets all bucket data. For use when all the data gets stale.
Bookatz857aaa52017-12-19 15:29:06 -0800170 virtual void resetStorage();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800171
Bookatz6bf98252018-03-14 10:44:24 -0700172 // Informs the subscribers (incidentd, perfetto, broadcasts, etc) that an anomaly has occurred.
Yangster-mac93694462018-01-22 20:49:31 -0800173 void informSubscribers(const MetricDimensionKey& key);
Bookatzd1fd2422017-11-22 15:21:03 -0800174
Yangster-mace2cd6d52017-11-09 20:38:30 -0800175 FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets);
176 FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets);
177 FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection);
Bookatz1bf94382018-01-04 11:43:20 -0800178 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800179};
180
181} // namespace statsd
182} // namespace os
183} // namespace android