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 | #define DEBUG true // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
| 20 | #include "AnomalyTracker.h" |
| 21 | |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame^] | 22 | #include <android/os/IIncidentManager.h> |
| 23 | #include <android/os/IncidentReportArgs.h> |
| 24 | #include <binder/IServiceManager.h> |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 25 | #include <time.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 31 | // TODO: Separate DurationAnomalyTracker as a separate subclass and let each MetricProducer |
| 32 | // decide and let which one it wants. |
| 33 | // TODO: Get rid of bucketNumbers, and return to the original circular array method. |
| 34 | AnomalyTracker::AnomalyTracker(const Alert& alert) |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 35 | : mAlert(alert), |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 36 | mNumOfPastBuckets(mAlert.number_of_buckets() - 1) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 37 | VLOG("AnomalyTracker() called"); |
| 38 | if (mAlert.number_of_buckets() <= 0) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 39 | ALOGE("Cannot create AnomalyTracker with %lld buckets", |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 40 | (long long)mAlert.number_of_buckets()); |
| 41 | return; |
| 42 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 43 | if (!mAlert.has_trigger_if_sum_gt()) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 44 | ALOGE("Cannot create AnomalyTracker without threshold"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 45 | return; |
| 46 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 47 | resetStorage(); // initialization |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | AnomalyTracker::~AnomalyTracker() { |
| 51 | VLOG("~AnomalyTracker() called"); |
| 52 | stopAllAlarms(); |
| 53 | } |
| 54 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 55 | void AnomalyTracker::resetStorage() { |
| 56 | VLOG("resetStorage() called."); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 57 | mPastBuckets.clear(); |
| 58 | // Excludes the current bucket. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 59 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 60 | mSumOverPastBuckets.clear(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 61 | |
| 62 | if (!mAlarms.empty()) VLOG("AnomalyTracker.resetStorage() called but mAlarms is NOT empty!"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | size_t AnomalyTracker::index(int64_t bucketNum) const { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 66 | return bucketNum % mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void AnomalyTracker::flushPastBuckets(const int64_t& latestPastBucketNum) { |
| 70 | VLOG("addPastBucket() called."); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 71 | if (latestPastBucketNum <= mMostRecentBucketNum - mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 72 | ALOGE("Cannot add a past bucket %lld units in past", (long long)latestPastBucketNum); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // The past packets are ancient. Empty out old mPastBuckets[i] values and reset |
| 77 | // mSumOverPastBuckets. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 78 | if (latestPastBucketNum - mMostRecentBucketNum >= mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 79 | mPastBuckets.clear(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 80 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 81 | mSumOverPastBuckets.clear(); |
| 82 | } else { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 83 | for (int64_t i = std::max(0LL, (long long)(mMostRecentBucketNum - mNumOfPastBuckets + 1)); |
| 84 | i <= latestPastBucketNum - mNumOfPastBuckets; i++) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 85 | const int idx = index(i); |
| 86 | subtractBucketFromSum(mPastBuckets[idx]); |
| 87 | mPastBuckets[idx] = nullptr; // release (but not clear) the old bucket. |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // It is an update operation. |
| 92 | if (latestPastBucketNum <= mMostRecentBucketNum && |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 93 | latestPastBucketNum > mMostRecentBucketNum - mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 94 | subtractBucketFromSum(mPastBuckets[index(latestPastBucketNum)]); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void AnomalyTracker::addPastBucket(const HashableDimensionKey& key, const int64_t& bucketValue, |
| 99 | const int64_t& bucketNum) { |
| 100 | flushPastBuckets(bucketNum); |
| 101 | |
| 102 | auto& bucket = mPastBuckets[index(bucketNum)]; |
| 103 | if (bucket == nullptr) { |
| 104 | bucket = std::make_shared<DimToValMap>(); |
| 105 | } |
| 106 | bucket->insert({key, bucketValue}); |
| 107 | addBucketToSum(bucket); |
| 108 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 109 | } |
| 110 | |
| 111 | void AnomalyTracker::addPastBucket(std::shared_ptr<DimToValMap> bucketValues, |
| 112 | const int64_t& bucketNum) { |
| 113 | VLOG("addPastBucket() called."); |
| 114 | flushPastBuckets(bucketNum); |
| 115 | // Replace the oldest bucket with the new bucket we are adding. |
| 116 | mPastBuckets[index(bucketNum)] = bucketValues; |
| 117 | addBucketToSum(bucketValues); |
| 118 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 119 | } |
| 120 | |
| 121 | void AnomalyTracker::subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket) { |
| 122 | if (bucket == nullptr) { |
| 123 | return; |
| 124 | } |
| 125 | // For each dimension present in the bucket, subtract its value from its corresponding sum. |
| 126 | for (const auto& keyValuePair : *bucket) { |
| 127 | auto itr = mSumOverPastBuckets.find(keyValuePair.first); |
| 128 | if (itr == mSumOverPastBuckets.end()) { |
| 129 | continue; |
| 130 | } |
| 131 | itr->second -= keyValuePair.second; |
| 132 | // TODO: No need to look up the object twice like this. Use a var. |
| 133 | if (itr->second == 0) { |
| 134 | mSumOverPastBuckets.erase(itr); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void AnomalyTracker::addBucketToSum(const shared_ptr<DimToValMap>& bucket) { |
| 140 | if (bucket == nullptr) { |
| 141 | return; |
| 142 | } |
| 143 | // For each dimension present in the bucket, add its value to its corresponding sum. |
| 144 | for (const auto& keyValuePair : *bucket) { |
| 145 | mSumOverPastBuckets[keyValuePair.first] += keyValuePair.second; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int64_t AnomalyTracker::getPastBucketValue(const HashableDimensionKey& key, |
| 150 | const int64_t& bucketNum) const { |
| 151 | const auto& bucket = mPastBuckets[index(bucketNum)]; |
| 152 | if (bucket == nullptr) { |
| 153 | return 0; |
| 154 | } |
| 155 | const auto& itr = bucket->find(key); |
| 156 | return itr == bucket->end() ? 0 : itr->second; |
| 157 | } |
| 158 | |
| 159 | int64_t AnomalyTracker::getSumOverPastBuckets(const HashableDimensionKey& key) const { |
| 160 | const auto& itr = mSumOverPastBuckets.find(key); |
| 161 | if (itr != mSumOverPastBuckets.end()) { |
| 162 | return itr->second; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | bool AnomalyTracker::detectAnomaly(const int64_t& currentBucketNum, |
| 168 | const DimToValMap& currentBucket) { |
| 169 | if (currentBucketNum > mMostRecentBucketNum + 1) { |
| 170 | addPastBucket(nullptr, currentBucketNum - 1); |
| 171 | } |
| 172 | for (auto itr = currentBucket.begin(); itr != currentBucket.end(); itr++) { |
| 173 | if (itr->second + getSumOverPastBuckets(itr->first) > mAlert.trigger_if_sum_gt()) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | // In theory, we also need to check the dimsions not in the current bucket. In single-thread |
| 178 | // mode, usually we could avoid the following loops. |
| 179 | for (auto itr = mSumOverPastBuckets.begin(); itr != mSumOverPastBuckets.end(); itr++) { |
| 180 | if (itr->second > mAlert.trigger_if_sum_gt()) { |
| 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool AnomalyTracker::detectAnomaly(const int64_t& currentBucketNum, const HashableDimensionKey& key, |
| 188 | const int64_t& currentBucketValue) { |
| 189 | if (currentBucketNum > mMostRecentBucketNum + 1) { |
| 190 | addPastBucket(key, 0, currentBucketNum - 1); |
| 191 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 192 | return mAlert.has_trigger_if_sum_gt() |
| 193 | && getSumOverPastBuckets(key) + currentBucketValue > mAlert.trigger_if_sum_gt(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 196 | void AnomalyTracker::declareAnomaly(const uint64_t& timestampNs) { |
| 197 | // TODO: This should also take in the const HashableDimensionKey& key, to pass |
| 198 | // more details to incidentd and to make mRefractoryPeriodEndsSec key-specific. |
| 199 | // TODO: Why receive timestamp? RefractoryPeriod should always be based on real time right now. |
| 200 | if (isInRefractoryPeriod(timestampNs)) { |
| 201 | VLOG("Skipping anomaly declaration since within refractory period"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | // TODO(guardrail): Consider guarding against too short refractory periods. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 205 | mLastAlarmTimestampNs = timestampNs; |
| 206 | |
| 207 | |
| 208 | // TODO: If we had access to the bucket_size_millis, consider calling resetStorage() |
| 209 | // if (mAlert.refractory_period_secs() > mNumOfPastBuckets * bucketSizeNs) { resetStorage(); } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 210 | |
| 211 | if (mAlert.has_incidentd_details()) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 212 | if (mAlert.has_name()) { |
| 213 | ALOGW("An anomaly (%s) has occurred! Informing incidentd.", |
| 214 | mAlert.name().c_str()); |
| 215 | } else { |
| 216 | // TODO: Can construct a name based on the criteria (and/or relay the criteria). |
| 217 | ALOGW("An anomaly (nameless) has occurred! Informing incidentd."); |
| 218 | } |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame^] | 219 | informIncidentd(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 220 | } else { |
| 221 | ALOGW("An anomaly has occurred! (But informing incidentd not requested.)"); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void AnomalyTracker::declareAnomalyIfAlarmExpired(const HashableDimensionKey& dimensionKey, |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 226 | const uint64_t& timestampNs) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 227 | auto itr = mAlarms.find(dimensionKey); |
| 228 | if (itr == mAlarms.end()) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | if (itr->second != nullptr && |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 233 | static_cast<uint32_t>(timestampNs / NS_PER_SEC) >= itr->second->timestampSec) { |
| 234 | declareAnomaly(timestampNs); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 235 | stopAlarm(dimensionKey); |
| 236 | } |
| 237 | } |
| 238 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 239 | void AnomalyTracker::detectAndDeclareAnomaly(const uint64_t& timestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 240 | const int64_t& currBucketNum, |
| 241 | const HashableDimensionKey& key, |
| 242 | const int64_t& currentBucketValue) { |
| 243 | if (detectAnomaly(currBucketNum, key, currentBucketValue)) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 244 | declareAnomaly(timestampNs); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 248 | void AnomalyTracker::detectAndDeclareAnomaly(const uint64_t& timestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 249 | const int64_t& currBucketNum, |
| 250 | const DimToValMap& currentBucket) { |
| 251 | if (detectAnomaly(currBucketNum, currentBucket)) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 252 | declareAnomaly(timestampNs); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | void AnomalyTracker::startAlarm(const HashableDimensionKey& dimensionKey, |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 257 | const uint64_t& timestampNs) { |
| 258 | uint32_t timestampSec = static_cast<uint32_t>(timestampNs / NS_PER_SEC); |
| 259 | if (isInRefractoryPeriod(timestampNs)) { |
| 260 | VLOG("Skipping setting anomaly alarm since it'd fall in the refractory period"); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | sp<const AnomalyAlarm> alarm = new AnomalyAlarm{timestampSec}; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 265 | mAlarms.insert({dimensionKey, alarm}); |
| 266 | if (mAnomalyMonitor != nullptr) { |
| 267 | mAnomalyMonitor->add(alarm); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void AnomalyTracker::stopAlarm(const HashableDimensionKey& dimensionKey) { |
| 272 | auto itr = mAlarms.find(dimensionKey); |
| 273 | if (itr != mAlarms.end()) { |
| 274 | mAlarms.erase(dimensionKey); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 275 | if (mAnomalyMonitor != nullptr) { |
| 276 | mAnomalyMonitor->remove(itr->second); |
| 277 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | void AnomalyTracker::stopAllAlarms() { |
| 282 | std::set<HashableDimensionKey> keys; |
| 283 | for (auto itr = mAlarms.begin(); itr != mAlarms.end(); ++itr) { |
| 284 | keys.insert(itr->first); |
| 285 | } |
| 286 | for (auto key : keys) { |
| 287 | stopAlarm(key); |
| 288 | } |
| 289 | } |
| 290 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 291 | bool AnomalyTracker::isInRefractoryPeriod(const uint64_t& timestampNs) { |
| 292 | return mLastAlarmTimestampNs >= 0 && |
| 293 | timestampNs - mLastAlarmTimestampNs <= mAlert.refractory_period_secs() * NS_PER_SEC; |
| 294 | } |
| 295 | |
| 296 | void AnomalyTracker::informAlarmsFired(const uint64_t& timestampNs, |
| 297 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& firedAlarms) { |
| 298 | |
| 299 | if (firedAlarms.empty() || mAlarms.empty()) return; |
| 300 | // Find the intersection of firedAlarms and mAlarms. |
| 301 | // The for loop is inefficient, since it loops over all keys, but that's okay since it is very |
| 302 | // seldomly called. The alternative would be having AnomalyAlarms store information about the |
| 303 | // AnomalyTracker and key, but that's a lot of data overhead to speed up something that is |
| 304 | // rarely ever called. |
| 305 | unordered_map<HashableDimensionKey, sp<const AnomalyAlarm>> matchedAlarms; |
| 306 | for (const auto& kv : mAlarms) { |
| 307 | if (firedAlarms.count(kv.second) > 0) { |
| 308 | matchedAlarms.insert({kv.first, kv.second}); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Now declare each of these alarms to have fired. |
| 313 | for (const auto& kv : matchedAlarms) { |
| 314 | declareAnomaly(timestampNs /* TODO: , kv.first */); |
| 315 | mAlarms.erase(kv.first); |
| 316 | firedAlarms.erase(kv.second); // No one else can also own it, so we're done with it. |
| 317 | } |
| 318 | } |
| 319 | |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame^] | 320 | void AnomalyTracker::informIncidentd() { |
| 321 | VLOG("informIncidentd called."); |
| 322 | if (!mAlert.has_incidentd_details()) { |
| 323 | ALOGE("Attempted to call incidentd without any incidentd_details."); |
| 324 | return; |
| 325 | } |
| 326 | sp<IIncidentManager> service = interface_cast<IIncidentManager>( |
| 327 | defaultServiceManager()->getService(android::String16("incident"))); |
| 328 | if (service == NULL) { |
| 329 | ALOGW("Couldn't get the incident service."); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | IncidentReportArgs incidentReport; |
| 334 | const Alert::IncidentdDetails& details = mAlert.incidentd_details(); |
| 335 | for (int i = 0; i < details.section_size(); i++) { |
| 336 | incidentReport.addSection(details.section(i)); |
| 337 | } |
| 338 | // TODO: Pass in mAlert.name() into the addHeader? |
| 339 | |
| 340 | service->reportIncident(incidentReport); |
| 341 | } |
| 342 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 343 | } // namespace statsd |
| 344 | } // namespace os |
| 345 | } // namespace android |