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 | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 34 | AnomalyTracker::AnomalyTracker(const Alert& alert, const ConfigKey& configKey) |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 35 | : mAlert(alert), mConfigKey(configKey), mNumOfPastBuckets(mAlert.num_buckets() - 1) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 36 | VLOG("AnomalyTracker() called"); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 37 | if (mAlert.num_buckets() <= 0) { |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 38 | ALOGE("Cannot create AnomalyTracker with %lld buckets", (long long)mAlert.num_buckets()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 39 | return; |
| 40 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 41 | if (!mAlert.has_trigger_if_sum_gt()) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 42 | ALOGE("Cannot create AnomalyTracker without threshold"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 43 | return; |
| 44 | } |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 45 | resetStorage(); // initialization |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | AnomalyTracker::~AnomalyTracker() { |
| 49 | VLOG("~AnomalyTracker() called"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 52 | void AnomalyTracker::resetStorage() { |
| 53 | VLOG("resetStorage() called."); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 54 | mPastBuckets.clear(); |
| 55 | // Excludes the current bucket. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 56 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 57 | mSumOverPastBuckets.clear(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | size_t AnomalyTracker::index(int64_t bucketNum) const { |
Bookatz | 2fb5653 | 2018-03-08 11:16:48 -0800 | [diff] [blame] | 61 | if (bucketNum < 0) { |
Bookatz | 2fb5653 | 2018-03-08 11:16:48 -0800 | [diff] [blame] | 62 | ALOGE("index() was passed a negative bucket number (%lld)!", (long long)bucketNum); |
| 63 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 64 | return bucketNum % mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 67 | void AnomalyTracker::advanceMostRecentBucketTo(const int64_t& bucketNum) { |
| 68 | VLOG("advanceMostRecentBucketTo() called."); |
| 69 | if (bucketNum <= mMostRecentBucketNum) { |
| 70 | ALOGW("Cannot advance buckets backwards (bucketNum=%lld but mMostRecentBucketNum=%lld)", |
| 71 | (long long)bucketNum, (long long)mMostRecentBucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 72 | return; |
| 73 | } |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 74 | // If in the future (i.e. buckets are ancient), just empty out all past info. |
| 75 | if (bucketNum >= mMostRecentBucketNum + mNumOfPastBuckets) { |
Bookatz | 2fb5653 | 2018-03-08 11:16:48 -0800 | [diff] [blame] | 76 | resetStorage(); |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 77 | mMostRecentBucketNum = bucketNum; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Clear out space by emptying out old mPastBuckets[i] values and update mSumOverPastBuckets. |
| 82 | for (int64_t i = mMostRecentBucketNum + 1; i <= bucketNum; i++) { |
| 83 | const int idx = index(i); |
| 84 | subtractBucketFromSum(mPastBuckets[idx]); |
| 85 | mPastBuckets[idx] = nullptr; // release (but not clear) the old bucket. |
| 86 | } |
| 87 | mMostRecentBucketNum = bucketNum; |
| 88 | } |
| 89 | |
| 90 | void AnomalyTracker::addPastBucket(const MetricDimensionKey& key, |
| 91 | const int64_t& bucketValue, |
| 92 | const int64_t& bucketNum) { |
| 93 | VLOG("addPastBucket(bucketValue) called."); |
| 94 | if (mNumOfPastBuckets == 0 || |
| 95 | bucketNum < 0 || bucketNum <= mMostRecentBucketNum - mNumOfPastBuckets) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | const int bucketIndex = index(bucketNum); |
| 100 | if (bucketNum <= mMostRecentBucketNum && (mPastBuckets[bucketIndex] != nullptr)) { |
| 101 | // We need to insert into an already existing past bucket. |
| 102 | std::shared_ptr<DimToValMap>& bucket = mPastBuckets[bucketIndex]; |
| 103 | auto itr = bucket->find(key); |
| 104 | if (itr != bucket->end()) { |
| 105 | // Old entry already exists; update it. |
| 106 | subtractValueFromSum(key, itr->second); |
| 107 | itr->second = bucketValue; |
| 108 | } else { |
| 109 | bucket->insert({key, bucketValue}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 110 | } |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 111 | mSumOverPastBuckets[key] += bucketValue; |
| 112 | } else { |
| 113 | // Bucket does not exist yet (in future or was never made), so we must make it. |
| 114 | std::shared_ptr<DimToValMap> bucket = std::make_shared<DimToValMap>(); |
| 115 | bucket->insert({key, bucketValue}); |
| 116 | addPastBucket(bucket, bucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 120 | void AnomalyTracker::addPastBucket(std::shared_ptr<DimToValMap> bucket, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 121 | const int64_t& bucketNum) { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 122 | VLOG("addPastBucket(bucket) called."); |
| 123 | if (mNumOfPastBuckets == 0 || |
| 124 | bucketNum < 0 || bucketNum <= mMostRecentBucketNum - mNumOfPastBuckets) { |
David Chen | c189bdcb | 2018-02-09 16:09:26 -0800 | [diff] [blame] | 125 | return; |
| 126 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 127 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 128 | if (bucketNum <= mMostRecentBucketNum) { |
| 129 | // We are updating an old bucket, not adding a new one. |
| 130 | subtractBucketFromSum(mPastBuckets[index(bucketNum)]); |
| 131 | } else { |
| 132 | // Clear space for the new bucket to be at bucketNum. |
| 133 | advanceMostRecentBucketTo(bucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 134 | } |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 135 | mPastBuckets[index(bucketNum)] = bucket; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 136 | addBucketToSum(bucket); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void AnomalyTracker::subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket) { |
| 140 | if (bucket == nullptr) { |
| 141 | return; |
| 142 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 143 | for (const auto& keyValuePair : *bucket) { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 144 | subtractValueFromSum(keyValuePair.first, keyValuePair.second); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void AnomalyTracker::subtractValueFromSum(const MetricDimensionKey& key, |
| 150 | const int64_t& bucketValue) { |
| 151 | auto itr = mSumOverPastBuckets.find(key); |
| 152 | if (itr == mSumOverPastBuckets.end()) { |
| 153 | return; |
| 154 | } |
| 155 | itr->second -= bucketValue; |
| 156 | if (itr->second == 0) { |
| 157 | mSumOverPastBuckets.erase(itr); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | void AnomalyTracker::addBucketToSum(const shared_ptr<DimToValMap>& bucket) { |
| 162 | if (bucket == nullptr) { |
| 163 | return; |
| 164 | } |
| 165 | // For each dimension present in the bucket, add its value to its corresponding sum. |
| 166 | for (const auto& keyValuePair : *bucket) { |
| 167 | mSumOverPastBuckets[keyValuePair.first] += keyValuePair.second; |
| 168 | } |
| 169 | } |
| 170 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 171 | int64_t AnomalyTracker::getPastBucketValue(const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 172 | const int64_t& bucketNum) const { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 173 | if (bucketNum < 0 || bucketNum <= mMostRecentBucketNum - mNumOfPastBuckets |
| 174 | || bucketNum > mMostRecentBucketNum) { |
David Chen | ebe7e237 | 2018-02-20 13:22:53 -0800 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 178 | const auto& bucket = mPastBuckets[index(bucketNum)]; |
| 179 | if (bucket == nullptr) { |
| 180 | return 0; |
| 181 | } |
| 182 | const auto& itr = bucket->find(key); |
| 183 | return itr == bucket->end() ? 0 : itr->second; |
| 184 | } |
| 185 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 186 | int64_t AnomalyTracker::getSumOverPastBuckets(const MetricDimensionKey& key) const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 187 | const auto& itr = mSumOverPastBuckets.find(key); |
| 188 | if (itr != mSumOverPastBuckets.end()) { |
| 189 | return itr->second; |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 194 | bool AnomalyTracker::detectAnomaly(const int64_t& currentBucketNum, |
| 195 | const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 196 | const int64_t& currentBucketValue) { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 197 | |
| 198 | // currentBucketNum should be the next bucket after pastBuckets. If not, advance so that it is. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 199 | if (currentBucketNum > mMostRecentBucketNum + 1) { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 200 | advanceMostRecentBucketTo(currentBucketNum - 1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 201 | } |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 202 | return mAlert.has_trigger_if_sum_gt() && |
| 203 | getSumOverPastBuckets(key) + currentBucketValue > mAlert.trigger_if_sum_gt(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 206 | void AnomalyTracker::declareAnomaly(const uint64_t& timestampNs, const MetricDimensionKey& key) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 207 | // 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] | 208 | if (isInRefractoryPeriod(timestampNs, key)) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 209 | VLOG("Skipping anomaly declaration since within refractory period"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 212 | if (mAlert.has_refractory_period_secs()) { |
| 213 | mRefractoryPeriodEndsSec[key] = ((timestampNs + NS_PER_SEC - 1) / NS_PER_SEC) // round up |
| 214 | + mAlert.refractory_period_secs(); |
| 215 | // TODO: If we had access to the bucket_size_millis, consider calling resetStorage() |
| 216 | // if (mAlert.refractory_period_secs() > mNumOfPastBuckets * bucketSizeNs) {resetStorage();} |
| 217 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 218 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 219 | if (!mSubscriptions.empty()) { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 220 | ALOGI("An anomaly (%lld) %s has occurred! Informing subscribers.", |
| 221 | mAlert.id(), key.toString().c_str()); |
| 222 | informSubscribers(key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 223 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 224 | ALOGI("An anomaly has occurred! (But no subscriber for that alert.)"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 225 | } |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 226 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 227 | StatsdStats::getInstance().noteAnomalyDeclared(mConfigKey, mAlert.id()); |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 228 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 229 | // TODO: This should also take in the const MetricDimensionKey& key? |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 230 | android::util::stats_write(android::util::ANOMALY_DETECTED, mConfigKey.GetUid(), |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 231 | mConfigKey.GetId(), mAlert.id()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 234 | void AnomalyTracker::detectAndDeclareAnomaly(const uint64_t& timestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 235 | const int64_t& currBucketNum, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 236 | const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 237 | const int64_t& currentBucketValue) { |
| 238 | if (detectAnomaly(currBucketNum, key, currentBucketValue)) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 239 | declareAnomaly(timestampNs, key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 243 | bool AnomalyTracker::isInRefractoryPeriod(const uint64_t& timestampNs, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 244 | const MetricDimensionKey& key) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 245 | const auto& it = mRefractoryPeriodEndsSec.find(key); |
| 246 | if (it != mRefractoryPeriodEndsSec.end()) { |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame^] | 247 | if (timestampNs < it->second * NS_PER_SEC) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 248 | return true; |
| 249 | } else { |
| 250 | mRefractoryPeriodEndsSec.erase(key); |
| 251 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 252 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 253 | return false; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 256 | void AnomalyTracker::informSubscribers(const MetricDimensionKey& key) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 257 | triggerSubscribers(mAlert.id(), key, mConfigKey, mSubscriptions); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 260 | } // namespace statsd |
| 261 | } // namespace os |
| 262 | } // namespace android |