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" |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 22 | #include "guardrail/StatsdStats.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 23 | |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 24 | #include <android/os/IIncidentManager.h> |
| 25 | #include <android/os/IncidentReportArgs.h> |
| 26 | #include <binder/IServiceManager.h> |
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) |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 36 | : mAlert(alert), |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 37 | mConfigKey(configKey), |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 38 | mNumOfPastBuckets(mAlert.num_buckets() - 1) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 39 | VLOG("AnomalyTracker() called"); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 40 | if (mAlert.num_buckets() <= 0) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 41 | ALOGE("Cannot create AnomalyTracker with %lld buckets", |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 42 | (long long)mAlert.num_buckets()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 43 | return; |
| 44 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 45 | if (!mAlert.has_trigger_if_sum_gt()) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 46 | ALOGE("Cannot create AnomalyTracker without threshold"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 47 | return; |
| 48 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 49 | resetStorage(); // initialization |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | AnomalyTracker::~AnomalyTracker() { |
| 53 | VLOG("~AnomalyTracker() called"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 56 | void AnomalyTracker::resetStorage() { |
| 57 | VLOG("resetStorage() called."); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 58 | mPastBuckets.clear(); |
| 59 | // Excludes the current bucket. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 60 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 61 | mSumOverPastBuckets.clear(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | size_t AnomalyTracker::index(int64_t bucketNum) const { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 65 | return bucketNum % mNumOfPastBuckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void AnomalyTracker::flushPastBuckets(const int64_t& latestPastBucketNum) { |
| 69 | VLOG("addPastBucket() called."); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 70 | if (latestPastBucketNum <= mMostRecentBucketNum - mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 71 | ALOGE("Cannot add a past bucket %lld units in past", (long long)latestPastBucketNum); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // The past packets are ancient. Empty out old mPastBuckets[i] values and reset |
| 76 | // mSumOverPastBuckets. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 77 | if (latestPastBucketNum - mMostRecentBucketNum >= mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 78 | mPastBuckets.clear(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 79 | mPastBuckets.resize(mNumOfPastBuckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 80 | mSumOverPastBuckets.clear(); |
| 81 | } else { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 82 | for (int64_t i = std::max(0LL, (long long)(mMostRecentBucketNum - mNumOfPastBuckets + 1)); |
| 83 | i <= latestPastBucketNum - mNumOfPastBuckets; i++) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 84 | const int idx = index(i); |
| 85 | subtractBucketFromSum(mPastBuckets[idx]); |
| 86 | mPastBuckets[idx] = nullptr; // release (but not clear) the old bucket. |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // It is an update operation. |
| 91 | if (latestPastBucketNum <= mMostRecentBucketNum && |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 92 | latestPastBucketNum > mMostRecentBucketNum - mNumOfPastBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 93 | subtractBucketFromSum(mPastBuckets[index(latestPastBucketNum)]); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void AnomalyTracker::addPastBucket(const HashableDimensionKey& key, const int64_t& bucketValue, |
| 98 | const int64_t& bucketNum) { |
| 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."); |
| 113 | flushPastBuckets(bucketNum); |
| 114 | // Replace the oldest bucket with the new bucket we are adding. |
| 115 | mPastBuckets[index(bucketNum)] = bucketValues; |
| 116 | addBucketToSum(bucketValues); |
| 117 | mMostRecentBucketNum = std::max(mMostRecentBucketNum, bucketNum); |
| 118 | } |
| 119 | |
| 120 | void AnomalyTracker::subtractBucketFromSum(const shared_ptr<DimToValMap>& bucket) { |
| 121 | if (bucket == nullptr) { |
| 122 | return; |
| 123 | } |
| 124 | // For each dimension present in the bucket, subtract its value from its corresponding sum. |
| 125 | for (const auto& keyValuePair : *bucket) { |
| 126 | auto itr = mSumOverPastBuckets.find(keyValuePair.first); |
| 127 | if (itr == mSumOverPastBuckets.end()) { |
| 128 | continue; |
| 129 | } |
| 130 | itr->second -= keyValuePair.second; |
| 131 | // TODO: No need to look up the object twice like this. Use a var. |
| 132 | if (itr->second == 0) { |
| 133 | mSumOverPastBuckets.erase(itr); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void AnomalyTracker::addBucketToSum(const shared_ptr<DimToValMap>& bucket) { |
| 139 | if (bucket == nullptr) { |
| 140 | return; |
| 141 | } |
| 142 | // For each dimension present in the bucket, add its value to its corresponding sum. |
| 143 | for (const auto& keyValuePair : *bucket) { |
| 144 | mSumOverPastBuckets[keyValuePair.first] += keyValuePair.second; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | int64_t AnomalyTracker::getPastBucketValue(const HashableDimensionKey& key, |
| 149 | const int64_t& bucketNum) const { |
| 150 | const auto& bucket = mPastBuckets[index(bucketNum)]; |
| 151 | if (bucket == nullptr) { |
| 152 | return 0; |
| 153 | } |
| 154 | const auto& itr = bucket->find(key); |
| 155 | return itr == bucket->end() ? 0 : itr->second; |
| 156 | } |
| 157 | |
| 158 | int64_t AnomalyTracker::getSumOverPastBuckets(const HashableDimensionKey& key) const { |
| 159 | const auto& itr = mSumOverPastBuckets.find(key); |
| 160 | if (itr != mSumOverPastBuckets.end()) { |
| 161 | return itr->second; |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 166 | bool AnomalyTracker::detectAnomaly(const int64_t& currentBucketNum, const HashableDimensionKey& key, |
| 167 | const int64_t& currentBucketValue) { |
| 168 | if (currentBucketNum > mMostRecentBucketNum + 1) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 169 | // TODO: This creates a needless 0 entry in mSumOverPastBuckets. Fix this. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 170 | addPastBucket(key, 0, currentBucketNum - 1); |
| 171 | } |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 172 | return mAlert.has_trigger_if_sum_gt() |
| 173 | && getSumOverPastBuckets(key) + currentBucketValue > mAlert.trigger_if_sum_gt(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 176 | void AnomalyTracker::declareAnomaly(const uint64_t& timestampNs, const HashableDimensionKey& key) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 177 | // 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] | 178 | if (isInRefractoryPeriod(timestampNs, key)) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 179 | VLOG("Skipping anomaly declaration since within refractory period"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 180 | return; |
| 181 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 182 | mRefractoryPeriodEndsSec[key] = (timestampNs / NS_PER_SEC) + mAlert.refractory_period_secs(); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 183 | |
| 184 | // TODO: If we had access to the bucket_size_millis, consider calling resetStorage() |
| 185 | // if (mAlert.refractory_period_secs() > mNumOfPastBuckets * bucketSizeNs) { resetStorage(); } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 186 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 187 | if (!mSubscriptions.empty()) { |
| 188 | if (mAlert.has_id()) { |
| 189 | ALOGI("An anomaly (%llu) has occurred! Informing subscribers.",mAlert.id()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 190 | informSubscribers(key); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 191 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 192 | ALOGI("An anomaly (with no id) has occurred! Not informing any subscribers."); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 193 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 194 | } else { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 195 | ALOGI("An anomaly has occurred! (But no subscriber for that alert.)"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 196 | } |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 197 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 198 | StatsdStats::getInstance().noteAnomalyDeclared(mConfigKey, mAlert.id()); |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 199 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 200 | // TODO: This should also take in the const HashableDimensionKey& key? |
Bookatz | 8fcd09a | 2017-12-18 13:01:10 -0800 | [diff] [blame] | 201 | android::util::stats_write(android::util::ANOMALY_DETECTED, mConfigKey.GetUid(), |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 202 | mConfigKey.GetId(), mAlert.id()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 205 | void AnomalyTracker::detectAndDeclareAnomaly(const uint64_t& timestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 206 | const int64_t& currBucketNum, |
| 207 | const HashableDimensionKey& key, |
| 208 | const int64_t& currentBucketValue) { |
| 209 | if (detectAnomaly(currBucketNum, key, currentBucketValue)) { |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 210 | declareAnomaly(timestampNs, key); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 214 | bool AnomalyTracker::isInRefractoryPeriod(const uint64_t& timestampNs, |
| 215 | const HashableDimensionKey& key) { |
| 216 | const auto& it = mRefractoryPeriodEndsSec.find(key); |
| 217 | if (it != mRefractoryPeriodEndsSec.end()) { |
| 218 | if ((timestampNs / NS_PER_SEC) <= it->second) { |
| 219 | return true; |
| 220 | } else { |
| 221 | mRefractoryPeriodEndsSec.erase(key); |
| 222 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 223 | } |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 224 | return false; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 227 | void AnomalyTracker::informSubscribers(const HashableDimensionKey& key) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 228 | VLOG("informSubscribers called."); |
| 229 | if (mSubscriptions.empty()) { |
| 230 | ALOGE("Attempt to call with no subscribers."); |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 234 | std::set<int> incidentdSections; |
| 235 | for (const Subscription& subscription : mSubscriptions) { |
| 236 | switch (subscription.subscriber_information_case()) { |
| 237 | case Subscription::SubscriberInformationCase::kIncidentdDetails: |
| 238 | for (int i = 0; i < subscription.incidentd_details().section_size(); i++) { |
| 239 | incidentdSections.insert(subscription.incidentd_details().section(i)); |
| 240 | } |
| 241 | break; |
| 242 | case Subscription::SubscriberInformationCase::kPerfettoDetails: |
Primiano Tucci | e4d4491 | 2018-01-10 12:14:50 +0000 | [diff] [blame^] | 243 | CollectPerfettoTraceAndUploadToDropbox(subscription.perfetto_details()); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 244 | break; |
| 245 | default: |
| 246 | break; |
| 247 | } |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 248 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 249 | if (!incidentdSections.empty()) { |
| 250 | sp<IIncidentManager> service = interface_cast<IIncidentManager>( |
| 251 | defaultServiceManager()->getService(android::String16("incident"))); |
| 252 | if (service != NULL) { |
| 253 | IncidentReportArgs incidentReport; |
| 254 | for (const auto section : incidentdSections) { |
| 255 | incidentReport.addSection(section); |
| 256 | } |
| 257 | int64_t alertId = mAlert.id(); |
| 258 | std::vector<uint8_t> header; |
| 259 | uint8_t* src = static_cast<uint8_t*>(static_cast<void*>(&alertId)); |
| 260 | header.insert(header.end(), src, src + sizeof(int64_t)); |
| 261 | incidentReport.addHeader(header); |
| 262 | service->reportIncident(incidentReport); |
| 263 | } else { |
| 264 | ALOGW("Couldn't get the incident service."); |
| 265 | } |
| 266 | } |
Bookatz | d1fd242 | 2017-11-22 15:21:03 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 269 | } // namespace statsd |
| 270 | } // namespace os |
| 271 | } // namespace android |