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 <stdlib.h> 20 21 #include <gtest/gtest_prod.h> 22 #include <utils/RefBase.h> 23 24 #include "AlarmMonitor.h" 25 #include "config/ConfigKey.h" 26 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert 27 #include "frameworks/base/cmds/statsd/src/statsd_metadata.pb.h" // AlertMetadata 28 #include "stats_util.h" // HashableDimensionKey and DimToValMap 29 30 namespace android { 31 namespace os { 32 namespace statsd { 33 34 using std::shared_ptr; 35 using std::unordered_map; 36 37 // Does NOT allow negative values. 38 class AnomalyTracker : public virtual RefBase { 39 public: 40 AnomalyTracker(const Alert& alert, const ConfigKey& configKey); 41 42 virtual ~AnomalyTracker(); 43 44 // Add subscriptions that depend on this alert. addSubscription(const Subscription & subscription)45 void addSubscription(const Subscription& subscription) { 46 mSubscriptions.push_back(subscription); 47 } 48 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. 59 void addPastBucket(const MetricDimensionKey& key, const int64_t& bucketValue, 60 const int64_t& bucketNum); 61 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. 64 // Also advances to currBucketNum-1. 65 bool detectAnomaly(const int64_t& currBucketNum, const MetricDimensionKey& key, 66 const int64_t& currentBucketValue); 67 68 // Informs incidentd about the detected alert. 69 void declareAnomaly(const int64_t& timestampNs, int64_t metricId, const MetricDimensionKey& key, 70 int64_t metricValue); 71 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. 75 // Also advances to currBucketNum-1. 76 void detectAndDeclareAnomaly(const int64_t& timestampNs, const int64_t& currBucketNum, 77 int64_t metricId, const MetricDimensionKey& key, 78 const int64_t& currentBucketValue); 79 80 // Init the AlarmMonitor which is shared across anomaly trackers. setAlarmMonitor(const sp<AlarmMonitor> & alarmMonitor)81 virtual void setAlarmMonitor(const sp<AlarmMonitor>& alarmMonitor) { 82 return; // Base AnomalyTracker class has no need for the AlarmMonitor. 83 } 84 85 // Returns the sum of all past bucket values for the given dimension key. 86 int64_t getSumOverPastBuckets(const MetricDimensionKey& key) const; 87 88 // Returns the value for a past bucket, or 0 if that bucket doesn't exist. 89 int64_t getPastBucketValue(const MetricDimensionKey& key, const int64_t& bucketNum) const; 90 91 // Returns the anomaly threshold set in the configuration. getAnomalyThreshold()92 inline int64_t getAnomalyThreshold() const { 93 return mAlert.trigger_if_sum_gt(); 94 } 95 96 // Returns the refractory period ending timestamp (in seconds) for the given key. 97 // Before this moment, any detected anomaly will be ignored. 98 // If there is no stored refractory period ending timestamp, returns 0. getRefractoryPeriodEndsSec(const MetricDimensionKey & key)99 uint32_t getRefractoryPeriodEndsSec(const MetricDimensionKey& key) const { 100 const auto& it = mRefractoryPeriodEndsSec.find(key); 101 return it != mRefractoryPeriodEndsSec.end() ? it->second : 0; 102 } 103 104 // Returns the (constant) number of past buckets this anomaly tracker can store. getNumOfPastBuckets()105 inline int getNumOfPastBuckets() const { 106 return mNumOfPastBuckets; 107 } 108 109 // Declares an anomaly for each alarm in firedAlarms that belongs to this AnomalyTracker, 110 // and removes it from firedAlarms. Does NOT remove the alarm from the AlarmMonitor. informAlarmsFired(const int64_t & timestampNs,unordered_set<sp<const InternalAlarm>,SpHash<InternalAlarm>> & firedAlarms)111 virtual void informAlarmsFired(const int64_t& timestampNs, 112 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) { 113 return; // The base AnomalyTracker class doesn't have alarms. 114 } 115 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 122 void loadAlertMetadata( 123 const metadata::AlertMetadata& alertMetadata, 124 int64_t currentWallClockTimeNs, 125 int64_t systemElapsedTimeNs); 126 127 protected: 128 // For testing only. 129 // Returns the alarm timestamp in seconds for the query dimension if it exists. Otherwise 130 // returns 0. getAlarmTimestampSec(const MetricDimensionKey & dimensionKey)131 virtual uint32_t getAlarmTimestampSec(const MetricDimensionKey& dimensionKey) const { 132 return 0; // The base AnomalyTracker class doesn't have alarms. 133 } 134 135 // statsd_config.proto Alert message that defines this tracker. 136 const Alert mAlert; 137 138 // The subscriptions that depend on this alert. 139 std::vector<Subscription> mSubscriptions; 140 141 // A reference to the Alert's config key. 142 const ConfigKey mConfigKey; 143 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). 146 const int mNumOfPastBuckets; 147 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. 150 std::vector<shared_ptr<DimToValMap>> mPastBuckets; 151 152 // Cached sum over all existing buckets in mPastBuckets. 153 // Its buckets never contain entries of 0. 154 DimToValMap mSumOverPastBuckets; 155 156 // The bucket number of the last added bucket. 157 int64_t mMostRecentBucketNum = -1; 158 159 // Map from each dimension to the timestamp that its refractory period (if this anomaly was 160 // declared for that dimension) ends, in seconds. From this moment and onwards, anomalies 161 // can be declared again. 162 // Entries may be, but are not guaranteed to be, removed after the period is finished. 163 unordered_map<MetricDimensionKey, uint32_t> mRefractoryPeriodEndsSec; 164 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); 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 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. 181 bool isInRefractoryPeriod(const int64_t& timestampNs, const MetricDimensionKey& key) const; 182 183 // Calculates the corresponding bucket index within the circular array. 184 // Requires bucketNum >= 0. 185 size_t index(int64_t bucketNum) const; 186 187 // Resets all bucket data. For use when all the data gets stale. 188 virtual void resetStorage(); 189 190 // Informs the subscribers (incidentd, perfetto, broadcasts, etc) that an anomaly has occurred. 191 void informSubscribers(const MetricDimensionKey& key, int64_t metricId, int64_t metricValue); 192 193 FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets); 194 FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets); 195 FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection); 196 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced); 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); 200 }; 201 202 } // namespace statsd 203 } // namespace os 204 } // namespace android 205