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 | |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 21 | #include <gtest/gtest_prod.h> |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 22 | #include <utils/RefBase.h> |
| 23 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 24 | #include "AlarmMonitor.h" |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 25 | #include "config/ConfigKey.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 26 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert |
Jeffrey Huang | 475677e | 2020-03-30 19:52:07 -0700 | [diff] [blame] | 27 | #include "frameworks/base/cmds/statsd/src/statsd_metadata.pb.h" // AlertMetadata |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 28 | #include "stats_util.h" // HashableDimensionKey and DimToValMap |
| 29 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 34 | using std::shared_ptr; |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 35 | using std::unordered_map; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 36 | |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 37 | // Does NOT allow negative values. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 38 | class AnomalyTracker : public virtual RefBase { |
| 39 | public: |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 40 | AnomalyTracker(const Alert& alert, const ConfigKey& configKey); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 41 | |
| 42 | virtual ~AnomalyTracker(); |
| 43 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 44 | // Add subscriptions that depend on this alert. |
| 45 | void addSubscription(const Subscription& subscription) { |
| 46 | mSubscriptions.push_back(subscription); |
| 47 | } |
| 48 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 49 | // Adds a bucket for the given bucketNum (index starting at 0). |
| 50 | // If a bucket for bucketNum already exists, it will be replaced. |
| 51 | // Also, advances to bucketNum (if not in the past), effectively filling any intervening |
| 52 | // buckets with 0s. |
| 53 | void addPastBucket(std::shared_ptr<DimToValMap> bucket, const int64_t& bucketNum); |
| 54 | |
| 55 | // Inserts (or replaces) the bucket entry for the given bucketNum at the given key to be the |
| 56 | // given bucketValue. If the bucket does not exist, it will be created. |
| 57 | // Also, advances to bucketNum (if not in the past), effectively filling any intervening |
| 58 | // buckets with 0s. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 59 | void addPastBucket(const MetricDimensionKey& key, const int64_t& bucketValue, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 60 | const int64_t& bucketNum); |
| 61 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 62 | // Returns true if, based on past buckets plus the new currentBucketValue (which generally |
| 63 | // represents the partially-filled current bucket), an anomaly has happened. |
Bookatz | 3e8cd35 | 2018-03-16 10:09:59 -0700 | [diff] [blame] | 64 | // Also advances to currBucketNum-1. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 65 | bool detectAnomaly(const int64_t& currBucketNum, const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 66 | const int64_t& currentBucketValue); |
| 67 | |
| 68 | // Informs incidentd about the detected alert. |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 69 | void declareAnomaly(const int64_t& timestampNs, int64_t metricId, const MetricDimensionKey& key, |
| 70 | int64_t metricValue); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 71 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 72 | // 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. |
Bookatz | 3e8cd35 | 2018-03-16 10:09:59 -0700 | [diff] [blame] | 75 | // Also advances to currBucketNum-1. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 76 | void detectAndDeclareAnomaly(const int64_t& timestampNs, const int64_t& currBucketNum, |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 77 | int64_t metricId, const MetricDimensionKey& key, |
| 78 | const int64_t& currentBucketValue); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 79 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 80 | // Init the AlarmMonitor which is shared across anomaly trackers. |
| 81 | virtual void setAlarmMonitor(const sp<AlarmMonitor>& alarmMonitor) { |
| 82 | return; // Base AnomalyTracker class has no need for the AlarmMonitor. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 85 | // Returns the sum of all past bucket values for the given dimension key. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 86 | int64_t getSumOverPastBuckets(const MetricDimensionKey& key) const; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 87 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 88 | // Returns the value for a past bucket, or 0 if that bucket doesn't exist. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 89 | int64_t getPastBucketValue(const MetricDimensionKey& key, const int64_t& bucketNum) const; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 90 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 91 | // Returns the anomaly threshold set in the configuration. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 92 | inline int64_t getAnomalyThreshold() const { |
| 93 | return mAlert.trigger_if_sum_gt(); |
| 94 | } |
| 95 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 96 | // Returns the refractory period ending timestamp (in seconds) for the given key. |
| 97 | // Before this moment, any detected anomaly will be ignored. |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 98 | // If there is no stored refractory period ending timestamp, returns 0. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 99 | uint32_t getRefractoryPeriodEndsSec(const MetricDimensionKey& key) const { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 100 | const auto& it = mRefractoryPeriodEndsSec.find(key); |
| 101 | return it != mRefractoryPeriodEndsSec.end() ? it->second : 0; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 104 | // Returns the (constant) number of past buckets this anomaly tracker can store. |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 105 | inline int getNumOfPastBuckets() const { |
| 106 | return mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 109 | // Declares an anomaly for each alarm in firedAlarms that belongs to this AnomalyTracker, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 110 | // and removes it from firedAlarms. Does NOT remove the alarm from the AlarmMonitor. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 111 | virtual void informAlarmsFired(const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 112 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) { |
| 113 | return; // The base AnomalyTracker class doesn't have alarms. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 114 | } |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 115 | |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 116 | // Writes metadata of the alert (refractory_period_end_sec) to AlertMetadata. |
| 117 | // Returns true if at least one element is written to alertMetadata. |
| 118 | bool writeAlertMetadataToProto( |
| 119 | int64_t currentWallClockTimeNs, |
| 120 | int64_t systemElapsedTimeNs, metadata::AlertMetadata* alertMetadata); |
| 121 | |
Jeffrey Huang | 475677e | 2020-03-30 19:52:07 -0700 | [diff] [blame] | 122 | void loadAlertMetadata( |
| 123 | const metadata::AlertMetadata& alertMetadata, |
| 124 | int64_t currentWallClockTimeNs, |
| 125 | int64_t systemElapsedTimeNs); |
| 126 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 127 | protected: |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 128 | // For testing only. |
| 129 | // Returns the alarm timestamp in seconds for the query dimension if it exists. Otherwise |
| 130 | // returns 0. |
| 131 | virtual uint32_t getAlarmTimestampSec(const MetricDimensionKey& dimensionKey) const { |
| 132 | return 0; // The base AnomalyTracker class doesn't have alarms. |
| 133 | } |
| 134 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 135 | // statsd_config.proto Alert message that defines this tracker. |
| 136 | const Alert mAlert; |
| 137 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 138 | // The subscriptions that depend on this alert. |
| 139 | std::vector<Subscription> mSubscriptions; |
| 140 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 141 | // A reference to the Alert's config key. |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 142 | const ConfigKey mConfigKey; |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 143 | |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 144 | // Number of past buckets. One less than the total number of buckets needed |
| 145 | // for the anomaly detection (since the current bucket is not in the past). |
Bookatz | 2fb5653 | 2018-03-08 11:16:48 -0800 | [diff] [blame] | 146 | const int mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 147 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 148 | // Values for each of the past mNumOfPastBuckets buckets. Always of size mNumOfPastBuckets. |
| 149 | // mPastBuckets[i] can be null, meaning that no data is present in that bucket. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 150 | std::vector<shared_ptr<DimToValMap>> mPastBuckets; |
| 151 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 152 | // Cached sum over all existing buckets in mPastBuckets. |
| 153 | // Its buckets never contain entries of 0. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 154 | DimToValMap mSumOverPastBuckets; |
| 155 | |
| 156 | // The bucket number of the last added bucket. |
| 157 | int64_t mMostRecentBucketNum = -1; |
| 158 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 159 | // Map from each dimension to the timestamp that its refractory period (if this anomaly was |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 160 | // declared for that dimension) ends, in seconds. From this moment and onwards, anomalies |
| 161 | // can be declared again. |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 162 | // 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] | 163 | unordered_map<MetricDimensionKey, uint32_t> mRefractoryPeriodEndsSec; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 164 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 165 | // Advances mMostRecentBucketNum to bucketNum, deleting any data that is now too old. |
| 166 | // Specifically, since it is now too old, removes the data for |
| 167 | // [mMostRecentBucketNum - mNumOfPastBuckets + 1, bucketNum - mNumOfPastBuckets]. |
| 168 | void advanceMostRecentBucketTo(const int64_t& bucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 169 | |
| 170 | // Add the information in the given bucket to mSumOverPastBuckets. |
| 171 | void addBucketToSum(const shared_ptr<DimToValMap>& bucket); |
| 172 | |
| 173 | // Subtract the information in the given bucket from mSumOverPastBuckets |
| 174 | // and remove any items with value 0. |
| 175 | void subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket); |
| 176 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 177 | // From mSumOverPastBuckets[key], subtracts bucketValue, removing it if it is now 0. |
| 178 | void subtractValueFromSum(const MetricDimensionKey& key, const int64_t& bucketValue); |
| 179 | |
| 180 | // Returns true if in the refractory period, else false. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 181 | bool isInRefractoryPeriod(const int64_t& timestampNs, const MetricDimensionKey& key) const; |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 182 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 183 | // Calculates the corresponding bucket index within the circular array. |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 184 | // Requires bucketNum >= 0. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 185 | size_t index(int64_t bucketNum) const; |
| 186 | |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 187 | // Resets all bucket data. For use when all the data gets stale. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 188 | virtual void resetStorage(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 189 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 190 | // Informs the subscribers (incidentd, perfetto, broadcasts, etc) that an anomaly has occurred. |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 191 | void informSubscribers(const MetricDimensionKey& key, int64_t metricId, int64_t metricValue); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 192 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 193 | FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets); |
| 194 | FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets); |
| 195 | FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 196 | FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced); |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 197 | FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket); |
| 198 | FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets); |
| 199 | FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace statsd |
| 203 | } // namespace os |
| 204 | } // namespace android |