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