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