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); |
| 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. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 60 | void declareAnomaly(const uint64_t& timestampNs); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 61 | |
| 62 | // Detects the alert and informs the incidentd when applicable. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 63 | void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 64 | const DimToValMap& currentBucket); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 65 | void detectAndDeclareAnomaly(const uint64_t& timestampNs, const int64_t& currBucketNum, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 66 | const HashableDimensionKey& key, |
| 67 | const int64_t& currentBucketValue); |
| 68 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 69 | // 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 74 | // 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 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 85 | // Helper function to return the timestamp of the last detected anomaly. |
| 86 | inline int64_t getLastAnomalyTimestampNs() const { |
| 87 | return mLastAnomalyTimestampNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 90 | inline int getNumOfPastBuckets() const { |
| 91 | return mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 94 | // 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. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 96 | 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 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 100 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | protected: |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 102 | // statsd_config.proto Alert message that defines this tracker. |
| 103 | const Alert mAlert; |
| 104 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 105 | // The subscriptions that depend on this alert. |
| 106 | std::vector<Subscription> mSubscriptions; |
| 107 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 108 | // A reference to the Alert's config key. |
| 109 | const ConfigKey& mConfigKey; |
| 110 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 111 | // 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 114 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 115 | // The existing bucket list. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 116 | 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. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 125 | int64_t mLastAnomalyTimestampNs = -1; |
| 126 | |
| 127 | void flushPastBuckets(const int64_t& currBucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 128 | |
| 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 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 136 | bool isInRefractoryPeriod(const uint64_t& timestampNs) const; |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 137 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 138 | // Calculates the corresponding bucket index within the circular array. |
| 139 | size_t index(int64_t bucketNum) const; |
| 140 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 141 | // Resets all bucket data. For use when all the data gets stale. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 142 | virtual void resetStorage(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 143 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 144 | // Informs the subscribers that an anomaly has occurred. |
| 145 | void informSubscribers(); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 146 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 147 | FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets); |
| 148 | FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets); |
| 149 | FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection); |
| 150 | FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetection); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | } // namespace statsd |
| 154 | } // namespace os |
| 155 | } // namespace android |