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" |
Primiano Tucci | e4d4491 | 2018-01-10 12:14:50 +0000 | [diff] [blame] | 21 | #include "external/Perfetto.h" |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 22 | #include "frameworks/base/libs/incident/proto/android/os/header.pb.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) { |
| 96 | flushPastBuckets(bucketNum); |
| 97 | |
| 98 | auto& bucket = mPastBuckets[index(bucketNum)]; |
| 99 | if (bucket == nullptr) { |
| 100 | bucket = std::make_shared<DimToValMap>(); |
| 101 | } |
| 102 | bucket->insert({key, bucketValue}); |
| 103 | addBucketToSum(bucket); |
| 104 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 105 | } |
| 106 | |
| 107 | void AnomalyTracker::addPastBucket(std::shared_ptr<DimToValMap> bucketValues, |
| 108 | const int64_t& bucketNum) { |
| 109 | VLOG("addPastBucket() called."); |
| 110 | flushPastBuckets(bucketNum); |
| 111 | // Replace the oldest bucket with the new bucket we are adding. |
| 112 | mPastBuckets[index(bucketNum)] = bucketValues; |
| 113 | addBucketToSum(bucketValues); |
| 114 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 115 | } |
| 116 | |
| 117 | void AnomalyTracker::subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket) { |
| 118 | if (bucket == nullptr) { |
| 119 | return; |
| 120 | } |
| 121 | // For each dimension present in the bucket, subtract its value from its corresponding sum. |
| 122 | for (const auto& keyValuePair : *bucket) { |
| 123 | auto itr = mSumOverPastBuckets.find(keyValuePair.first); |
| 124 | if (itr == mSumOverPastBuckets.end()) { |
| 125 | continue; |
| 126 | } |
| 127 | itr->second -= keyValuePair.second; |
| 128 | // TODO: No need to look up the object twice like this. Use a var. |
| 129 | if (itr->second == 0) { |
| 130 | mSumOverPastBuckets.erase(itr); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void AnomalyTracker::addBucketToSum(const shared_ptr<DimToValMap>& bucket) { |
| 136 | if (bucket == nullptr) { |
| 137 | return; |
| 138 | } |
| 139 | // For each dimension present in the bucket, add its value to its corresponding sum. |
| 140 | for (const auto& keyValuePair : *bucket) { |
| 141 | mSumOverPastBuckets[keyValuePair.first] += keyValuePair.second; |
| 142 | } |
| 143 | } |
| 144 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 145 | int64_t AnomalyTracker::getPastBucketValue(const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 146 | const int64_t& bucketNum) const { |
| 147 | const auto& bucket = mPastBuckets[index(bucketNum)]; |
| 148 | if (bucket == nullptr) { |
| 149 | return 0; |
| 150 | } |
| 151 | const auto& itr = bucket->find(key); |
| 152 | return itr == bucket->end() ? 0 : itr->second; |
| 153 | } |
| 154 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 155 | int64_t AnomalyTracker::getSumOverPastBuckets(const MetricDimensionKey& key) const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 156 | const auto& itr = mSumOverPastBuckets.find(key); |
| 157 | if (itr != mSumOverPastBuckets.end()) { |
| 158 | return itr->second; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 163 | bool AnomalyTracker::detectAnomaly(const int64_t& currentBucketNum, const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 164 | const int64_t& currentBucketValue) { |
| 165 | if (currentBucketNum > mMostRecentBucketNum + 1) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 166 | // TODO: This creates a needless 0 entry in mSumOverPastBuckets. Fix this. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 167 | addPastBucket(key, 0, currentBucketNum - 1); |
| 168 | } |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame^] | 169 | return mAlert.has_trigger_if_sum_gt() && |
| 170 | getSumOverPastBuckets(key) + currentBucketValue > mAlert.trigger_if_sum_gt(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 173 | void AnomalyTracker::declareAnomaly(const uint64_t& timestampNs, const MetricDimensionKey& key) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 174 | // 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] | 175 | if (isInRefractoryPeriod(timestampNs, key)) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 176 | VLOG("Skipping anomaly declaration since within refractory period"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 177 | return; |
| 178 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 179 | mRefractoryPeriodEndsSec[key] = (timestampNs / NS_PER_SEC) + mAlert.refractory_period_secs(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 180 | |
| 181 | // TODO: If we had access to the bucket_size_millis, consider calling resetStorage() |
| 182 | // if (mAlert.refractory_period_secs() > mNumOfPastBuckets * bucketSizeNs) { resetStorage(); } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 183 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 184 | if (!mSubscriptions.empty()) { |
| 185 | if (mAlert.has_id()) { |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame^] | 186 | ALOGI("An anomaly (%llu) has occurred! Informing subscribers.", mAlert.id()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 187 | informSubscribers(key); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 188 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 189 | ALOGI("An anomaly (with no id) has occurred! Not informing any subscribers."); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 190 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 191 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 192 | ALOGI("An anomaly has occurred! (But no subscriber for that alert.)"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 193 | } |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 194 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 195 | StatsdStats::getInstance().noteAnomalyDeclared(mConfigKey, mAlert.id()); |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 196 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 197 | // TODO: This should also take in the const MetricDimensionKey& key? |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 198 | android::util::stats_write(android::util::ANOMALY_DETECTED, mConfigKey.GetUid(), |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 199 | mConfigKey.GetId(), mAlert.id()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 202 | void AnomalyTracker::detectAndDeclareAnomaly(const uint64_t& timestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 203 | const int64_t& currBucketNum, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 204 | const MetricDimensionKey& key, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 205 | const int64_t& currentBucketValue) { |
| 206 | if (detectAnomaly(currBucketNum, key, currentBucketValue)) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 207 | declareAnomaly(timestampNs, key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 211 | bool AnomalyTracker::isInRefractoryPeriod(const uint64_t& timestampNs, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 212 | const MetricDimensionKey& key) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 213 | const auto& it = mRefractoryPeriodEndsSec.find(key); |
| 214 | if (it != mRefractoryPeriodEndsSec.end()) { |
| 215 | if ((timestampNs / NS_PER_SEC) <= it->second) { |
| 216 | return true; |
| 217 | } else { |
| 218 | mRefractoryPeriodEndsSec.erase(key); |
| 219 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 220 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 221 | return false; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 224 | void AnomalyTracker::informSubscribers(const MetricDimensionKey& key) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 225 | VLOG("informSubscribers called."); |
| 226 | if (mSubscriptions.empty()) { |
| 227 | ALOGE("Attempt to call with no subscribers."); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 231 | for (const Subscription& subscription : mSubscriptions) { |
| 232 | switch (subscription.subscriber_information_case()) { |
| 233 | case Subscription::SubscriberInformationCase::kIncidentdDetails: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame^] | 234 | if (!GenerateIncidentReport(subscription.incidentd_details(), mAlert, mConfigKey)) { |
| 235 | ALOGW("Failed to generate incident report."); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 236 | } |
| 237 | break; |
| 238 | case Subscription::SubscriberInformationCase::kPerfettoDetails: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame^] | 239 | if (!CollectPerfettoTraceAndUploadToDropbox(subscription.perfetto_details())) { |
| 240 | ALOGW("Failed to generate prefetto traces."); |
| 241 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 242 | break; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 243 | case Subscription::SubscriberInformationCase::kBroadcastSubscriberDetails: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame^] | 244 | SubscriberReporter::getInstance().alertBroadcastSubscriber(mConfigKey, subscription, |
| 245 | key); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 246 | break; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 247 | default: |
| 248 | break; |
| 249 | } |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 250 | } |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 253 | } // namespace statsd |
| 254 | } // namespace os |
| 255 | } // namespace android |