Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 1 | /* |
| 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" |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 21 | #include "config/ConfigKey.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 22 | #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 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
| 33 | using std::unordered_map; |
| 34 | using std::shared_ptr; |
| 35 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 36 | // Does NOT allow negative values. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 37 | class AnomalyTracker : public virtual RefBase { |
| 38 | public: |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 39 | AnomalyTracker(const Alert& alert, const ConfigKey& configKey); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 40 | |
| 41 | virtual ~AnomalyTracker(); |
| 42 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 43 | // Add subscriptions that depend on this alert. |
| 44 | void addSubscription(const Subscription& subscription) { |
| 45 | mSubscriptions.push_back(subscription); |
| 46 | } |
| 47 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 48 | // Adds a bucket. |
| 49 | // Bucket index starts from 0. |
| 50 | void addPastBucket(std::shared_ptr<DimToValMap> bucketValues, const int64_t& bucketNum); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 51 | void addPastBucket(const MetricDimensionKey& key, const int64_t& bucketValue, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 52 | const int64_t& bucketNum); |
| 53 | |
| 54 | // Returns true if detected anomaly for the existing buckets on one or more dimension keys. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 55 | bool detectAnomaly(const int64_t& currBucketNum, const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 56 | const int64_t& currentBucketValue); |
| 57 | |
| 58 | // Informs incidentd about the detected alert. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 59 | void declareAnomaly(const uint64_t& timestampNs, const MetricDimensionKey& key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 60 | |
| 61 | // Detects the alert and informs the incidentd when applicable. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 62 | void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 63 | const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 64 | const int64_t& currentBucketValue); |
| 65 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 66 | // 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 71 | // Helper function to return the sum value of past buckets at given dimension. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 72 | int64_t getSumOverPastBuckets(const MetricDimensionKey& key) const; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 73 | |
| 74 | // Helper function to return the value for a past bucket. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 75 | int64_t getPastBucketValue(const MetricDimensionKey& key, const int64_t& bucketNum) const; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 76 | |
| 77 | // Returns the anomaly threshold. |
| 78 | inline int64_t getAnomalyThreshold() const { |
| 79 | return mAlert.trigger_if_sum_gt(); |
| 80 | } |
| 81 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 82 | // Returns the refractory period timestamp (in seconds) for the given key. |
| 83 | // If there is no stored refractory period ending timestamp, returns 0. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 84 | uint32_t getRefractoryPeriodEndsSec(const MetricDimensionKey& key) const { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 85 | const auto& it = mRefractoryPeriodEndsSec.find(key); |
| 86 | return it != mRefractoryPeriodEndsSec.end() ? it->second : 0; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 89 | inline int getNumOfPastBuckets() const { |
| 90 | return mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 93 | // 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. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 95 | 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 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 99 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 100 | protected: |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | // statsd_config.proto Alert message that defines this tracker. |
| 102 | const Alert mAlert; |
| 103 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 104 | // The subscriptions that depend on this alert. |
| 105 | std::vector<Subscription> mSubscriptions; |
| 106 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 107 | // A reference to the Alert's config key. |
| 108 | const ConfigKey& mConfigKey; |
| 109 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 110 | // 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 113 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 114 | // The existing bucket list. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 115 | 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 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 123 | // 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-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 127 | unordered_map<MetricDimensionKey, uint32_t> mRefractoryPeriodEndsSec; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 128 | |
| 129 | void flushPastBuckets(const int64_t& currBucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 130 | |
| 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-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 138 | bool isInRefractoryPeriod(const uint64_t& timestampNs, const MetricDimensionKey& key); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 139 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 140 | // Calculates the corresponding bucket index within the circular array. |
| 141 | size_t index(int64_t bucketNum) const; |
| 142 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 143 | // Resets all bucket data. For use when all the data gets stale. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 144 | virtual void resetStorage(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 145 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 146 | // Informs the subscribers that an anomaly has occurred. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 147 | void informSubscribers(const MetricDimensionKey& key); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 148 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 149 | FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets); |
| 150 | FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets); |
| 151 | FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 152 | FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace statsd |
| 156 | } // namespace os |
| 157 | } // namespace android |