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