Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [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 | */ |
Yao Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 16 | #define DEBUG false |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | #include "OringDurationTracker.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 19 | #include "guardrail/StatsdStats.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace os { |
| 23 | namespace statsd { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 24 | |
| 25 | using std::pair; |
| 26 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 27 | OringDurationTracker::OringDurationTracker( |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 28 | const ConfigKey& key, const int64_t& id, const HashableDimensionKey& eventKey, |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 29 | sp<ConditionWizard> wizard, int conditionIndex, bool nesting, uint64_t currentBucketStartNs, |
| 30 | uint64_t bucketSizeNs, const vector<sp<DurationAnomalyTracker>>& anomalyTrackers) |
| 31 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 32 | : DurationTracker(key, id, eventKey, wizard, conditionIndex, nesting, currentBucketStartNs, |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 33 | bucketSizeNs, anomalyTrackers), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 34 | mStarted(), |
| 35 | mPaused() { |
| 36 | mLastStartTime = 0; |
| 37 | } |
| 38 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 39 | bool OringDurationTracker::hitGuardRail(const HashableDimensionKey& newKey) { |
| 40 | // ===========GuardRail============== |
| 41 | // 1. Report the tuple count if the tuple count > soft limit |
| 42 | if (mConditionKeyMap.find(newKey) != mConditionKeyMap.end()) { |
| 43 | return false; |
| 44 | } |
| 45 | if (mConditionKeyMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 46 | size_t newTupleCount = mConditionKeyMap.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 47 | StatsdStats::getInstance().noteMetricDimensionSize( |
| 48 | mConfigKey, hashDimensionsValue(mTrackerId, mEventKey.getDimensionsValue()), |
| 49 | newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 50 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 51 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 52 | ALOGE("OringDurTracker %lld dropping data for dimension key %s", |
| 53 | (long long)mTrackerId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 60 | void OringDurationTracker::noteStart(const HashableDimensionKey& key, bool condition, |
| 61 | const uint64_t eventTime, const ConditionKey& conditionKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 62 | if (hitGuardRail(key)) { |
| 63 | return; |
| 64 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 65 | if (condition) { |
| 66 | if (mStarted.size() == 0) { |
| 67 | mLastStartTime = eventTime; |
| 68 | VLOG("record first start...."); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 69 | startAnomalyAlarm(eventTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 70 | } |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 71 | mStarted[key]++; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 72 | } else { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 73 | mPaused[key]++; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (mConditionKeyMap.find(key) == mConditionKeyMap.end()) { |
| 77 | mConditionKeyMap[key] = conditionKey; |
| 78 | } |
| 79 | |
| 80 | VLOG("Oring: %s start, condition %d", key.c_str(), condition); |
| 81 | } |
| 82 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 83 | void OringDurationTracker::noteStop(const HashableDimensionKey& key, const uint64_t timestamp, |
| 84 | const bool stopAll) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 85 | declareAnomalyIfAlarmExpired(timestamp); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 86 | VLOG("Oring: %s stop", key.c_str()); |
| 87 | auto it = mStarted.find(key); |
| 88 | if (it != mStarted.end()) { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 89 | (it->second)--; |
| 90 | if (stopAll || !mNested || it->second <= 0) { |
| 91 | mStarted.erase(it); |
| 92 | mConditionKeyMap.erase(key); |
| 93 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 94 | if (mStarted.empty()) { |
| 95 | mDuration += (timestamp - mLastStartTime); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 96 | detectAndDeclareAnomaly(timestamp, mCurrentBucketNum, mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 97 | VLOG("record duration %lld, total %lld ", (long long)timestamp - mLastStartTime, |
| 98 | (long long)mDuration); |
| 99 | } |
| 100 | } |
| 101 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 102 | auto pausedIt = mPaused.find(key); |
| 103 | if (pausedIt != mPaused.end()) { |
| 104 | (pausedIt->second)--; |
| 105 | if (stopAll || !mNested || pausedIt->second <= 0) { |
| 106 | mPaused.erase(pausedIt); |
| 107 | mConditionKeyMap.erase(key); |
| 108 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 109 | } |
| 110 | if (mStarted.empty()) { |
| 111 | stopAnomalyAlarm(); |
| 112 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 113 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 114 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 115 | void OringDurationTracker::noteStopAll(const uint64_t timestamp) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 116 | declareAnomalyIfAlarmExpired(timestamp); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 117 | if (!mStarted.empty()) { |
| 118 | mDuration += (timestamp - mLastStartTime); |
| 119 | VLOG("Oring Stop all: record duration %lld %lld ", (long long)timestamp - mLastStartTime, |
| 120 | (long long)mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 121 | detectAndDeclareAnomaly(timestamp, mCurrentBucketNum, mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 124 | stopAnomalyAlarm(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 125 | mStarted.clear(); |
| 126 | mPaused.clear(); |
| 127 | mConditionKeyMap.clear(); |
| 128 | } |
| 129 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 130 | bool OringDurationTracker::flushIfNeeded( |
| 131 | uint64_t eventTime, unordered_map<HashableDimensionKey, vector<DurationBucket>>* output) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 132 | if (eventTime < mCurrentBucketStartTimeNs + mBucketSizeNs) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | VLOG("OringDurationTracker Flushing............."); |
| 136 | // adjust the bucket start time |
| 137 | int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 138 | DurationBucket current_info; |
| 139 | current_info.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 140 | current_info.mBucketEndNs = current_info.mBucketStartNs + mBucketSizeNs; |
| 141 | current_info.mBucketNum = mCurrentBucketNum; |
| 142 | // Process the current bucket. |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 143 | if (mStarted.size() > 0) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 144 | mDuration += (current_info.mBucketEndNs - mLastStartTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 145 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 146 | if (mDuration > 0) { |
| 147 | current_info.mDuration = mDuration; |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 148 | (*output)[mEventKey].push_back(current_info); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 149 | addPastBucketToAnomalyTrackers(current_info.mDuration, current_info.mBucketNum); |
| 150 | VLOG(" duration: %lld", (long long)current_info.mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (mStarted.size() > 0) { |
| 154 | for (int i = 1; i < numBucketsForward; i++) { |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 155 | DurationBucket info; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 156 | info.mBucketStartNs = mCurrentBucketStartTimeNs + mBucketSizeNs * i; |
| 157 | info.mBucketEndNs = info.mBucketStartNs + mBucketSizeNs; |
| 158 | info.mBucketNum = mCurrentBucketNum + i; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 159 | info.mDuration = mBucketSizeNs; |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 160 | (*output)[mEventKey].push_back(info); |
| 161 | addPastBucketToAnomalyTrackers(info.mDuration, info.mBucketNum); |
| 162 | VLOG(" add filling bucket with duration %lld", (long long)info.mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 165 | mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs; |
| 166 | mCurrentBucketNum += numBucketsForward; |
| 167 | |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 168 | mLastStartTime = mCurrentBucketStartTimeNs; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 169 | mDuration = 0; |
| 170 | |
| 171 | // if all stopped, then tell owner it's safe to remove this tracker. |
| 172 | return mStarted.empty() && mPaused.empty(); |
| 173 | } |
| 174 | |
| 175 | void OringDurationTracker::onSlicedConditionMayChange(const uint64_t timestamp) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 176 | declareAnomalyIfAlarmExpired(timestamp); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 177 | vector<pair<HashableDimensionKey, int>> startedToPaused; |
| 178 | vector<pair<HashableDimensionKey, int>> pausedToStarted; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 179 | if (!mStarted.empty()) { |
| 180 | for (auto it = mStarted.begin(); it != mStarted.end();) { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 181 | const auto& key = it->first; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 182 | if (mConditionKeyMap.find(key) == mConditionKeyMap.end()) { |
| 183 | VLOG("Key %s dont have condition key", key.c_str()); |
| 184 | ++it; |
| 185 | continue; |
| 186 | } |
| 187 | if (mWizard->query(mConditionTrackerIndex, mConditionKeyMap[key]) != |
| 188 | ConditionState::kTrue) { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 189 | startedToPaused.push_back(*it); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 190 | it = mStarted.erase(it); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 191 | VLOG("Key %s started -> paused", key.c_str()); |
| 192 | } else { |
| 193 | ++it; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (mStarted.empty()) { |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 198 | mDuration += (timestamp - mLastStartTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 199 | VLOG("Duration add %lld , to %lld ", (long long)(timestamp - mLastStartTime), |
| 200 | (long long)mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 201 | detectAndDeclareAnomaly(timestamp, mCurrentBucketNum, mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | if (!mPaused.empty()) { |
| 206 | for (auto it = mPaused.begin(); it != mPaused.end();) { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 207 | const auto& key = it->first; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 208 | if (mConditionKeyMap.find(key) == mConditionKeyMap.end()) { |
| 209 | VLOG("Key %s dont have condition key", key.c_str()); |
| 210 | ++it; |
| 211 | continue; |
| 212 | } |
| 213 | if (mWizard->query(mConditionTrackerIndex, mConditionKeyMap[key]) == |
| 214 | ConditionState::kTrue) { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 215 | pausedToStarted.push_back(*it); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 216 | it = mPaused.erase(it); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 217 | VLOG("Key %s paused -> started", key.c_str()); |
| 218 | } else { |
| 219 | ++it; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (mStarted.empty() && pausedToStarted.size() > 0) { |
| 224 | mLastStartTime = timestamp; |
| 225 | } |
| 226 | } |
| 227 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 228 | if (mStarted.empty() && !pausedToStarted.empty()) { |
| 229 | startAnomalyAlarm(timestamp); |
| 230 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 231 | mStarted.insert(pausedToStarted.begin(), pausedToStarted.end()); |
| 232 | mPaused.insert(startedToPaused.begin(), startedToPaused.end()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 233 | |
| 234 | if (mStarted.empty()) { |
| 235 | stopAnomalyAlarm(); |
| 236 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void OringDurationTracker::onConditionChanged(bool condition, const uint64_t timestamp) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 240 | declareAnomalyIfAlarmExpired(timestamp); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 241 | if (condition) { |
| 242 | if (!mPaused.empty()) { |
| 243 | VLOG("Condition true, all started"); |
| 244 | if (mStarted.empty()) { |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 245 | mLastStartTime = timestamp; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 246 | } |
| 247 | if (mStarted.empty() && !mPaused.empty()) { |
| 248 | startAnomalyAlarm(timestamp); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 249 | } |
| 250 | mStarted.insert(mPaused.begin(), mPaused.end()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 251 | mPaused.clear(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 252 | } |
| 253 | } else { |
| 254 | if (!mStarted.empty()) { |
| 255 | VLOG("Condition false, all paused"); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 256 | mDuration += (timestamp - mLastStartTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 257 | mPaused.insert(mStarted.begin(), mStarted.end()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 258 | mStarted.clear(); |
| 259 | detectAndDeclareAnomaly(timestamp, mCurrentBucketNum, mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 262 | if (mStarted.empty()) { |
| 263 | stopAnomalyAlarm(); |
| 264 | } |
| 265 | } |
| 266 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 267 | int64_t OringDurationTracker::predictAnomalyTimestampNs( |
| 268 | const DurationAnomalyTracker& anomalyTracker, const uint64_t eventTimestampNs) const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 269 | // TODO: Unit-test this and see if it can be done more efficiently (e.g. use int32). |
| 270 | // All variables below represent durations (not timestamps). |
| 271 | |
| 272 | // The time until the current bucket ends. This is how much more 'space' it can hold. |
| 273 | const int64_t currRemainingBucketSizeNs = |
| 274 | mBucketSizeNs - (eventTimestampNs - mCurrentBucketStartTimeNs); |
| 275 | // TODO: This should never be < 0. Document/guard against possible failures if it is. |
| 276 | |
| 277 | const int64_t thresholdNs = anomalyTracker.getAnomalyThreshold(); |
| 278 | |
| 279 | // As we move into the future, old buckets get overwritten (so their old data is erased). |
| 280 | |
| 281 | // Sum of past durations. Will change as we overwrite old buckets. |
| 282 | int64_t pastNs = mDuration; |
| 283 | pastNs += anomalyTracker.getSumOverPastBuckets(mEventKey); |
| 284 | |
| 285 | // How much of the threshold is still unaccounted after considering pastNs. |
| 286 | int64_t leftNs = thresholdNs - pastNs; |
| 287 | |
| 288 | // First deal with the remainder of the current bucket. |
| 289 | if (leftNs <= currRemainingBucketSizeNs) { // Predict the anomaly will occur in this bucket. |
| 290 | return eventTimestampNs + leftNs; |
| 291 | } |
| 292 | // The remainder of this bucket contributes, but we must then move to the next bucket. |
| 293 | pastNs += currRemainingBucketSizeNs; |
| 294 | |
| 295 | // Now deal with the past buckets, starting with the oldest. |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 296 | for (int futBucketIdx = 0; futBucketIdx < anomalyTracker.getNumOfPastBuckets(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 297 | futBucketIdx++) { |
| 298 | // We now overwrite the oldest bucket with the previous 'current', and start a new |
| 299 | // 'current'. |
| 300 | pastNs -= anomalyTracker.getPastBucketValue( |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 301 | mEventKey, mCurrentBucketNum - anomalyTracker.getNumOfPastBuckets() + futBucketIdx); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 302 | leftNs = thresholdNs - pastNs; |
| 303 | if (leftNs <= mBucketSizeNs) { // Predict anomaly will occur in this bucket. |
| 304 | return eventTimestampNs + currRemainingBucketSizeNs + (futBucketIdx * mBucketSizeNs) + |
| 305 | leftNs; |
| 306 | } else { // This bucket would be entirely filled, and we'll need to move to the next |
| 307 | // bucket. |
| 308 | pastNs += mBucketSizeNs; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // If we have reached this point, we even have to overwrite the the original current bucket. |
| 313 | // Thus, none of the past data will still be extant - pastNs is now 0. |
| 314 | return eventTimestampNs + thresholdNs; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | } // namespace statsd |
| 318 | } // namespace os |
| 319 | } // namespace android |