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 | |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
| 20 | #include "AnomalyTracker.h" |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 21 | #include "subscriber_util.h" |
Primiano Tucci | e4d4491 | 2018-01-10 12:14:50 +0000 | [diff] [blame] | 22 | #include "external/Perfetto.h" |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 23 | #include "guardrail/StatsdStats.h" |
| 24 | #include "subscriber/IncidentdReporter.h" |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 25 | #include "subscriber/SubscriberReporter.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 26 | |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 27 | #include <statslog.h> |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 28 | #include <time.h> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 34 | // TODO: Get rid of bucketNumbers, and return to the original circular array method. |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 35 | AnomalyTracker::AnomalyTracker(const Alert& alert, const ConfigKey& configKey) |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 36 | : mAlert(alert), mConfigKey(configKey), mNumOfPastBuckets(mAlert.num_buckets() - 1) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 37 | VLOG("AnomalyTracker() called"); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 38 | if (mAlert.num_buckets() <= 0) { |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 39 | ALOGE("Cannot create AnomalyTracker with %lld buckets", (long long)mAlert.num_buckets()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 40 | return; |
| 41 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 42 | if (!mAlert.has_trigger_if_sum_gt()) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 43 | ALOGE("Cannot create AnomalyTracker without threshold"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 44 | return; |
| 45 | } |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 46 | resetStorage(); // initialization |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | AnomalyTracker::~AnomalyTracker() { |
| 50 | VLOG("~AnomalyTracker() called"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 53 | void AnomalyTracker::resetStorage() { |
| 54 | VLOG("resetStorage() called."); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 55 | mPastBuckets.clear(); |
| 56 | // Excludes the current bucket. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 57 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 58 | mSumOverPastBuckets.clear(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | size_t AnomalyTracker::index(int64_t bucketNum) const { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 62 | return bucketNum % mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void AnomalyTracker::flushPastBuckets(const int64_t& latestPastBucketNum) { |
| 66 | VLOG("addPastBucket() called."); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 67 | if (latestPastBucketNum <= mMostRecentBucketNum - mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 68 | ALOGE("Cannot add a past bucket %lld units in past", (long long)latestPastBucketNum); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // The past packets are ancient. Empty out old mPastBuckets[i] values and reset |
| 73 | // mSumOverPastBuckets. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 74 | if (latestPastBucketNum - mMostRecentBucketNum >= mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 75 | mPastBuckets.clear(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 76 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 77 | mSumOverPastBuckets.clear(); |
| 78 | } else { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 79 | for (int64_t i = std::max(0LL, (long long)(mMostRecentBucketNum - mNumOfPastBuckets + 1)); |
| 80 | i <= latestPastBucketNum - mNumOfPastBuckets; i++) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 81 | const int idx = index(i); |
| 82 | subtractBucketFromSum(mPastBuckets[idx]); |
| 83 | mPastBuckets[idx] = nullptr; // release (but not clear) the old bucket. |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // It is an update operation. |
| 88 | if (latestPastBucketNum <= mMostRecentBucketNum && |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 89 | latestPastBucketNum > mMostRecentBucketNum - mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 90 | subtractBucketFromSum(mPastBuckets[index(latestPastBucketNum)]); |
| 91 | } |
| 92 | } |
| 93 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 94 | void AnomalyTracker::addPastBucket(const MetricDimensionKey& key, const int64_t& bucketValue, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 95 | const int64_t& bucketNum) { |
David Chen | c189bdcb | 2018-02-09 16:09:26 -0800 | [diff] [blame] | 96 | if (mNumOfPastBuckets == 0) { |
| 97 | return; |
| 98 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 99 | flushPastBuckets(bucketNum); |
| 100 | |
| 101 | auto& bucket = mPastBuckets[index(bucketNum)]; |
| 102 | if (bucket == nullptr) { |
| 103 | bucket = std::make_shared<DimToValMap>(); |
| 104 | } |
| 105 | bucket->insert({key, bucketValue}); |
| 106 | addBucketToSum(bucket); |
| 107 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 108 | } |
| 109 | |
| 110 | void AnomalyTracker::addPastBucket(std::shared_ptr<DimToValMap> bucketValues, |
| 111 | const int64_t& bucketNum) { |
| 112 | VLOG("addPastBucket() called."); |
David Chen | c189bdcb | 2018-02-09 16:09:26 -0800 | [diff] [blame] | 113 | if (mNumOfPastBuckets == 0) { |
| 114 | return; |
| 115 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 116 | flushPastBuckets(bucketNum); |
| 117 | // Replace the oldest bucket with the new bucket we are adding. |
| 118 | mPastBuckets[index(bucketNum)] = bucketValues; |
| 119 | addBucketToSum(bucketValues); |
| 120 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 121 | } |
| 122 | |
| 123 | void AnomalyTracker::subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket) { |
| 124 | if (bucket == nullptr) { |
| 125 | return; |
| 126 | } |
| 127 | // For each dimension present in the bucket, subtract its value from its corresponding sum. |
| 128 | for (const auto& keyValuePair : *bucket) { |
| 129 | auto itr = mSumOverPastBuckets.find(keyValuePair.first); |
| 130 | if (itr == mSumOverPastBuckets.end()) { |
| 131 | continue; |
| 132 | } |
| 133 | itr->second -= keyValuePair.second; |
| 134 | // TODO: No need to look up the object twice like this. Use a var. |
| 135 | if (itr->second == 0) { |
| 136 | mSumOverPastBuckets.erase(itr); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void AnomalyTracker::addBucketToSum(const shared_ptr<DimToValMap>& bucket) { |
| 142 | if (bucket == nullptr) { |
| 143 | return; |
| 144 | } |
| 145 | // For each dimension present in the bucket, add its value to its corresponding sum. |
| 146 | for (const auto& keyValuePair : *bucket) { |
| 147 | mSumOverPastBuckets[keyValuePair.first] += keyValuePair.second; |
| 148 | } |
| 149 | } |
| 150 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 151 | int64_t AnomalyTracker::getPastBucketValue(const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 152 | const int64_t& bucketNum) const { |
David Chen | ebe7e237 | 2018-02-20 13:22:53 -0800 | [diff] [blame] | 153 | if (mNumOfPastBuckets == 0) { |
| 154 | return 0; |
| 155 | } |
| 156 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 157 | const auto& bucket = mPastBuckets[index(bucketNum)]; |
| 158 | if (bucket == nullptr) { |
| 159 | return 0; |
| 160 | } |
| 161 | const auto& itr = bucket->find(key); |
| 162 | return itr == bucket->end() ? 0 : itr->second; |
| 163 | } |
| 164 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 165 | int64_t AnomalyTracker::getSumOverPastBuckets(const MetricDimensionKey& key) const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 166 | const auto& itr = mSumOverPastBuckets.find(key); |
| 167 | if (itr != mSumOverPastBuckets.end()) { |
| 168 | return itr->second; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 173 | bool AnomalyTracker::detectAnomaly(const int64_t& currentBucketNum, const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 174 | const int64_t& currentBucketValue) { |
| 175 | if (currentBucketNum > mMostRecentBucketNum + 1) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 176 | // TODO: This creates a needless 0 entry in mSumOverPastBuckets. Fix this. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 177 | addPastBucket(key, 0, currentBucketNum - 1); |
| 178 | } |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 179 | return mAlert.has_trigger_if_sum_gt() && |
| 180 | getSumOverPastBuckets(key) + currentBucketValue > mAlert.trigger_if_sum_gt(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 183 | void AnomalyTracker::declareAnomaly(const uint64_t& timestampNs, const MetricDimensionKey& key) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 184 | // TODO: Why receive timestamp? RefractoryPeriod should always be based on real time right now. |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 185 | if (isInRefractoryPeriod(timestampNs, key)) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 186 | VLOG("Skipping anomaly declaration since within refractory period"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 187 | return; |
| 188 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 189 | mRefractoryPeriodEndsSec[key] = (timestampNs / NS_PER_SEC) + mAlert.refractory_period_secs(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 190 | |
| 191 | // TODO: If we had access to the bucket_size_millis, consider calling resetStorage() |
| 192 | // if (mAlert.refractory_period_secs() > mNumOfPastBuckets * bucketSizeNs) { resetStorage(); } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 193 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 194 | if (!mSubscriptions.empty()) { |
| 195 | if (mAlert.has_id()) { |
Bookatz | 32f2d4f | 2018-02-21 14:30:51 -0800 | [diff] [blame] | 196 | ALOGI("An anomaly (%lld) has occurred! Informing subscribers.", mAlert.id()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 197 | informSubscribers(key); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 198 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 199 | ALOGI("An anomaly (with no id) has occurred! Not informing any subscribers."); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 200 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 201 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 202 | ALOGI("An anomaly has occurred! (But no subscriber for that alert.)"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 203 | } |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 204 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 205 | StatsdStats::getInstance().noteAnomalyDeclared(mConfigKey, mAlert.id()); |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 206 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 207 | // TODO: This should also take in the const MetricDimensionKey& key? |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 208 | android::util::stats_write(android::util::ANOMALY_DETECTED, mConfigKey.GetUid(), |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 209 | mConfigKey.GetId(), mAlert.id()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 212 | void AnomalyTracker::detectAndDeclareAnomaly(const uint64_t& timestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 213 | const int64_t& currBucketNum, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 214 | const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 215 | const int64_t& currentBucketValue) { |
| 216 | if (detectAnomaly(currBucketNum, key, currentBucketValue)) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 217 | declareAnomaly(timestampNs, key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 221 | bool AnomalyTracker::isInRefractoryPeriod(const uint64_t& timestampNs, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 222 | const MetricDimensionKey& key) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 223 | const auto& it = mRefractoryPeriodEndsSec.find(key); |
| 224 | if (it != mRefractoryPeriodEndsSec.end()) { |
| 225 | if ((timestampNs / NS_PER_SEC) <= it->second) { |
| 226 | return true; |
| 227 | } else { |
| 228 | mRefractoryPeriodEndsSec.erase(key); |
| 229 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 230 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 231 | return false; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 234 | void AnomalyTracker::informSubscribers(const MetricDimensionKey& key) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 235 | triggerSubscribers(mAlert.id(), key, mConfigKey, mSubscriptions); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 238 | } // namespace statsd |
| 239 | } // namespace os |
| 240 | } // namespace android |