blob: f01a97f86cf6c39ae9c1064dfb3cffb829b7b181 [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
19#include <gtest/gtest_prod.h>
20#include "AnomalyMonitor.h"
Bookatz8f2f3d82017-12-07 13:53:21 -080021#include "config/ConfigKey.h"
Yangster-mace2cd6d52017-11-09 20:38:30 -080022#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert
23#include "stats_util.h" // HashableDimensionKey and DimToValMap
24
25#include <memory> // unique_ptr
26#include <stdlib.h>
27#include <utils/RefBase.h>
28
29namespace android {
30namespace os {
31namespace statsd {
32
33using std::unordered_map;
34using std::shared_ptr;
35
Bookatzcc5adef2017-11-21 14:36:23 -080036// Does NOT allow negative values.
Yangster-mace2cd6d52017-11-09 20:38:30 -080037class AnomalyTracker : public virtual RefBase {
38public:
Bookatz8f2f3d82017-12-07 13:53:21 -080039 AnomalyTracker(const Alert& alert, const ConfigKey& configKey);
Yangster-mace2cd6d52017-11-09 20:38:30 -080040
41 virtual ~AnomalyTracker();
42
Yangster-mac94e197c2018-01-02 16:03:03 -080043 // Add subscriptions that depend on this alert.
44 void addSubscription(const Subscription& subscription) {
45 mSubscriptions.push_back(subscription);
46 }
47
Yangster-mace2cd6d52017-11-09 20:38:30 -080048 // Adds a bucket.
49 // Bucket index starts from 0.
50 void addPastBucket(std::shared_ptr<DimToValMap> bucketValues, const int64_t& bucketNum);
Yangster-mac93694462018-01-22 20:49:31 -080051 void addPastBucket(const MetricDimensionKey& key, const int64_t& bucketValue,
Yangster-mace2cd6d52017-11-09 20:38:30 -080052 const int64_t& bucketNum);
53
54 // Returns true if detected anomaly for the existing buckets on one or more dimension keys.
Yangster-mac93694462018-01-22 20:49:31 -080055 bool detectAnomaly(const int64_t& currBucketNum, const MetricDimensionKey& key,
Yangster-mace2cd6d52017-11-09 20:38:30 -080056 const int64_t& currentBucketValue);
57
58 // Informs incidentd about the detected alert.
Yangster-mac93694462018-01-22 20:49:31 -080059 void declareAnomaly(const uint64_t& timestampNs, const MetricDimensionKey& key);
Yangster-mace2cd6d52017-11-09 20:38:30 -080060
61 // Detects the alert and informs the incidentd when applicable.
Bookatzcc5adef2017-11-21 14:36:23 -080062 void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum,
Yangster-mac93694462018-01-22 20:49:31 -080063 const MetricDimensionKey& key,
Yangster-mace2cd6d52017-11-09 20:38:30 -080064 const int64_t& currentBucketValue);
65
Bookatz857aaa52017-12-19 15:29:06 -080066 // Init the AnomalyMonitor which is shared across anomaly trackers.
67 virtual void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
68 return; // Base AnomalyTracker class has no need for the AnomalyMonitor.
Yangster-mace2cd6d52017-11-09 20:38:30 -080069 }
70
Yangster-mace2cd6d52017-11-09 20:38:30 -080071 // Helper function to return the sum value of past buckets at given dimension.
Yangster-mac93694462018-01-22 20:49:31 -080072 int64_t getSumOverPastBuckets(const MetricDimensionKey& key) const;
Yangster-mace2cd6d52017-11-09 20:38:30 -080073
74 // Helper function to return the value for a past bucket.
Yangster-mac93694462018-01-22 20:49:31 -080075 int64_t getPastBucketValue(const MetricDimensionKey& key, const int64_t& bucketNum) const;
Yangster-mace2cd6d52017-11-09 20:38:30 -080076
77 // Returns the anomaly threshold.
78 inline int64_t getAnomalyThreshold() const {
79 return mAlert.trigger_if_sum_gt();
80 }
81
Bookatz1bf94382018-01-04 11:43:20 -080082 // Returns the refractory period timestamp (in seconds) for the given key.
83 // If there is no stored refractory period ending timestamp, returns 0.
Yangster-mac93694462018-01-22 20:49:31 -080084 uint32_t getRefractoryPeriodEndsSec(const MetricDimensionKey& key) const {
Bookatz1bf94382018-01-04 11:43:20 -080085 const auto& it = mRefractoryPeriodEndsSec.find(key);
86 return it != mRefractoryPeriodEndsSec.end() ? it->second : 0;
Yangster-mace2cd6d52017-11-09 20:38:30 -080087 }
88
Bookatzcc5adef2017-11-21 14:36:23 -080089 inline int getNumOfPastBuckets() const {
90 return mNumOfPastBuckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -080091 }
92
Bookatzcc5adef2017-11-21 14:36:23 -080093 // Declares an anomaly for each alarm in firedAlarms that belongs to this AnomalyTracker,
94 // and removes it from firedAlarms. Does NOT remove the alarm from the AnomalyMonitor.
Bookatz857aaa52017-12-19 15:29:06 -080095 virtual void informAlarmsFired(const uint64_t& timestampNs,
96 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& firedAlarms) {
97 return; // The base AnomalyTracker class doesn't have alarms.
98 }
Bookatzcc5adef2017-11-21 14:36:23 -080099
Yangster-mace2cd6d52017-11-09 20:38:30 -0800100protected:
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101 // statsd_config.proto Alert message that defines this tracker.
102 const Alert mAlert;
103
Yangster-mac94e197c2018-01-02 16:03:03 -0800104 // The subscriptions that depend on this alert.
105 std::vector<Subscription> mSubscriptions;
106
Bookatz8f2f3d82017-12-07 13:53:21 -0800107 // A reference to the Alert's config key.
108 const ConfigKey& mConfigKey;
109
Bookatzcc5adef2017-11-21 14:36:23 -0800110 // Number of past buckets. One less than the total number of buckets needed
111 // for the anomaly detection (since the current bucket is not in the past).
112 int mNumOfPastBuckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800113
Yangster-mac94e197c2018-01-02 16:03:03 -0800114 // The existing bucket list.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800115 std::vector<shared_ptr<DimToValMap>> mPastBuckets;
116
117 // Sum over all existing buckets cached in mPastBuckets.
118 DimToValMap mSumOverPastBuckets;
119
120 // The bucket number of the last added bucket.
121 int64_t mMostRecentBucketNum = -1;
122
Bookatz1bf94382018-01-04 11:43:20 -0800123 // Map from each dimension to the timestamp that its refractory period (if this anomaly was
124 // declared for that dimension) ends, in seconds. Only anomalies that occur after this period
125 // ends will be declared.
126 // Entries may be, but are not guaranteed to be, removed after the period is finished.
Yangster-mac93694462018-01-22 20:49:31 -0800127 unordered_map<MetricDimensionKey, uint32_t> mRefractoryPeriodEndsSec;
Bookatz857aaa52017-12-19 15:29:06 -0800128
129 void flushPastBuckets(const int64_t& currBucketNum);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800130
131 // Add the information in the given bucket to mSumOverPastBuckets.
132 void addBucketToSum(const shared_ptr<DimToValMap>& bucket);
133
134 // Subtract the information in the given bucket from mSumOverPastBuckets
135 // and remove any items with value 0.
136 void subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket);
137
Yangster-mac93694462018-01-22 20:49:31 -0800138 bool isInRefractoryPeriod(const uint64_t& timestampNs, const MetricDimensionKey& key);
Bookatzcc5adef2017-11-21 14:36:23 -0800139
Yangster-mace2cd6d52017-11-09 20:38:30 -0800140 // Calculates the corresponding bucket index within the circular array.
141 size_t index(int64_t bucketNum) const;
142
Bookatzcc5adef2017-11-21 14:36:23 -0800143 // Resets all bucket data. For use when all the data gets stale.
Bookatz857aaa52017-12-19 15:29:06 -0800144 virtual void resetStorage();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800145
Yangster-mac94e197c2018-01-02 16:03:03 -0800146 // Informs the subscribers that an anomaly has occurred.
Yangster-mac93694462018-01-22 20:49:31 -0800147 void informSubscribers(const MetricDimensionKey& key);
Bookatzd1fd2422017-11-22 15:21:03 -0800148
Yangster-mace2cd6d52017-11-09 20:38:30 -0800149 FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets);
150 FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets);
151 FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection);
Bookatz1bf94382018-01-04 11:43:20 -0800152 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800153};
154
155} // namespace statsd
156} // namespace os
157} // namespace android