blob: 2d5ab867da00c7607ce220fcf95b2c2feacc4ec6 [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);
51 void addPastBucket(const HashableDimensionKey& key, const int64_t& bucketValue,
52 const int64_t& bucketNum);
53
54 // Returns true if detected anomaly for the existing buckets on one or more dimension keys.
55 bool detectAnomaly(const int64_t& currBucketNum, const DimToValMap& currentBucket);
56 bool detectAnomaly(const int64_t& currBucketNum, const HashableDimensionKey& key,
57 const int64_t& currentBucketValue);
58
59 // Informs incidentd about the detected alert.
Bookatzcc5adef2017-11-21 14:36:23 -080060 void declareAnomaly(const uint64_t& timestampNs);
Yangster-mace2cd6d52017-11-09 20:38:30 -080061
62 // Detects the alert and informs the incidentd when applicable.
Bookatzcc5adef2017-11-21 14:36:23 -080063 void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum,
Yangster-mace2cd6d52017-11-09 20:38:30 -080064 const DimToValMap& currentBucket);
Bookatzcc5adef2017-11-21 14:36:23 -080065 void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum,
Yangster-mace2cd6d52017-11-09 20:38:30 -080066 const HashableDimensionKey& key,
67 const int64_t& currentBucketValue);
68
Bookatz857aaa52017-12-19 15:29:06 -080069 // Init the AnomalyMonitor which is shared across anomaly trackers.
70 virtual void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
71 return; // Base AnomalyTracker class has no need for the AnomalyMonitor.
Yangster-mace2cd6d52017-11-09 20:38:30 -080072 }
73
Yangster-mace2cd6d52017-11-09 20:38:30 -080074 // Helper function to return the sum value of past buckets at given dimension.
75 int64_t getSumOverPastBuckets(const HashableDimensionKey& key) const;
76
77 // Helper function to return the value for a past bucket.
78 int64_t getPastBucketValue(const HashableDimensionKey& key, const int64_t& bucketNum) const;
79
80 // Returns the anomaly threshold.
81 inline int64_t getAnomalyThreshold() const {
82 return mAlert.trigger_if_sum_gt();
83 }
84
Bookatz857aaa52017-12-19 15:29:06 -080085 // Helper function to return the timestamp of the last detected anomaly.
86 inline int64_t getLastAnomalyTimestampNs() const {
87 return mLastAnomalyTimestampNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -080088 }
89
Bookatzcc5adef2017-11-21 14:36:23 -080090 inline int getNumOfPastBuckets() const {
91 return mNumOfPastBuckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -080092 }
93
Bookatzcc5adef2017-11-21 14:36:23 -080094 // Declares an anomaly for each alarm in firedAlarms that belongs to this AnomalyTracker,
95 // and removes it from firedAlarms. Does NOT remove the alarm from the AnomalyMonitor.
Bookatz857aaa52017-12-19 15:29:06 -080096 virtual void informAlarmsFired(const uint64_t& timestampNs,
97 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& firedAlarms) {
98 return; // The base AnomalyTracker class doesn't have alarms.
99 }
Bookatzcc5adef2017-11-21 14:36:23 -0800100
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101protected:
Yangster-mace2cd6d52017-11-09 20:38:30 -0800102 // statsd_config.proto Alert message that defines this tracker.
103 const Alert mAlert;
104
Yangster-mac94e197c2018-01-02 16:03:03 -0800105 // The subscriptions that depend on this alert.
106 std::vector<Subscription> mSubscriptions;
107
Bookatz8f2f3d82017-12-07 13:53:21 -0800108 // A reference to the Alert's config key.
109 const ConfigKey& mConfigKey;
110
Bookatzcc5adef2017-11-21 14:36:23 -0800111 // Number of past buckets. One less than the total number of buckets needed
112 // for the anomaly detection (since the current bucket is not in the past).
113 int mNumOfPastBuckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800114
Yangster-mac94e197c2018-01-02 16:03:03 -0800115 // The existing bucket list.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800116 std::vector<shared_ptr<DimToValMap>> mPastBuckets;
117
118 // Sum over all existing buckets cached in mPastBuckets.
119 DimToValMap mSumOverPastBuckets;
120
121 // The bucket number of the last added bucket.
122 int64_t mMostRecentBucketNum = -1;
123
124 // The timestamp when the last anomaly was declared.
Bookatz857aaa52017-12-19 15:29:06 -0800125 int64_t mLastAnomalyTimestampNs = -1;
126
127 void flushPastBuckets(const int64_t& currBucketNum);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800128
129 // Add the information in the given bucket to mSumOverPastBuckets.
130 void addBucketToSum(const shared_ptr<DimToValMap>& bucket);
131
132 // Subtract the information in the given bucket from mSumOverPastBuckets
133 // and remove any items with value 0.
134 void subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket);
135
Bookatz857aaa52017-12-19 15:29:06 -0800136 bool isInRefractoryPeriod(const uint64_t& timestampNs) const;
Bookatzcc5adef2017-11-21 14:36:23 -0800137
Yangster-mace2cd6d52017-11-09 20:38:30 -0800138 // Calculates the corresponding bucket index within the circular array.
139 size_t index(int64_t bucketNum) const;
140
Bookatzcc5adef2017-11-21 14:36:23 -0800141 // Resets all bucket data. For use when all the data gets stale.
Bookatz857aaa52017-12-19 15:29:06 -0800142 virtual void resetStorage();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800143
Yangster-mac94e197c2018-01-02 16:03:03 -0800144 // Informs the subscribers that an anomaly has occurred.
145 void informSubscribers();
Bookatzd1fd2422017-11-22 15:21:03 -0800146
Yangster-mace2cd6d52017-11-09 20:38:30 -0800147 FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets);
148 FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets);
149 FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection);
150 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetection);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800151};
152
153} // namespace statsd
154} // namespace os
155} // namespace android