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 | */ |
| 16 | |
Yao Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 17 | #define DEBUG false |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 18 | |
| 19 | #include "Log.h" |
| 20 | #include "MaxDurationTracker.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | #include "guardrail/StatsdStats.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 27 | MaxDurationTracker::MaxDurationTracker(const ConfigKey& key, const int64_t& id, |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 28 | const HashableDimensionKey& eventKey, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 29 | sp<ConditionWizard> wizard, int conditionIndex, bool nesting, |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 30 | uint64_t currentBucketStartNs, uint64_t bucketSizeNs, |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 31 | const vector<sp<DurationAnomalyTracker>>& anomalyTrackers) |
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 | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | bool MaxDurationTracker::hitGuardRail(const HashableDimensionKey& newKey) { |
| 37 | // ===========GuardRail============== |
| 38 | if (mInfos.find(newKey) != mInfos.end()) { |
| 39 | // if the key existed, we are good! |
| 40 | return false; |
| 41 | } |
| 42 | // 1. Report the tuple count if the tuple count > soft limit |
| 43 | if (mInfos.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 44 | size_t newTupleCount = mInfos.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 45 | StatsdStats::getInstance().noteMetricDimensionSize( |
| 46 | mConfigKey, hashDimensionsValue(mTrackerId, mEventKey.getDimensionsValue()), |
| 47 | newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 48 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 49 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 50 | ALOGE("MaxDurTracker %lld dropping data for dimension key %s", |
| 51 | (long long)mTrackerId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | return false; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void MaxDurationTracker::noteStart(const HashableDimensionKey& key, bool condition, |
| 59 | const uint64_t eventTime, const ConditionKey& conditionKey) { |
| 60 | // this will construct a new DurationInfo if this key didn't exist. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 61 | if (hitGuardRail(key)) { |
| 62 | return; |
| 63 | } |
| 64 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 65 | DurationInfo& duration = mInfos[key]; |
| 66 | duration.conditionKeys = conditionKey; |
| 67 | VLOG("MaxDuration: key %s start condition %d", key.c_str(), condition); |
| 68 | |
| 69 | switch (duration.state) { |
| 70 | case kStarted: |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 71 | duration.startCount++; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 72 | break; |
| 73 | case kPaused: |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 74 | duration.startCount++; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 75 | break; |
| 76 | case kStopped: |
| 77 | if (!condition) { |
| 78 | // event started, but we need to wait for the condition to become true. |
| 79 | duration.state = DurationState::kPaused; |
| 80 | } else { |
| 81 | duration.state = DurationState::kStarted; |
| 82 | duration.lastStartTime = eventTime; |
| 83 | } |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 84 | duration.startCount = 1; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 89 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 90 | void MaxDurationTracker::noteStop(const HashableDimensionKey& key, const uint64_t eventTime, |
| 91 | bool forceStop) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 92 | declareAnomalyIfAlarmExpired(eventTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 93 | VLOG("MaxDuration: key %s stop", key.c_str()); |
| 94 | if (mInfos.find(key) == mInfos.end()) { |
| 95 | // we didn't see a start event before. do nothing. |
| 96 | return; |
| 97 | } |
| 98 | DurationInfo& duration = mInfos[key]; |
| 99 | |
| 100 | switch (duration.state) { |
| 101 | case DurationState::kStopped: |
| 102 | // already stopped, do nothing. |
| 103 | break; |
| 104 | case DurationState::kStarted: { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 105 | duration.startCount--; |
| 106 | if (forceStop || !mNested || duration.startCount <= 0) { |
| 107 | duration.state = DurationState::kStopped; |
| 108 | int64_t durationTime = eventTime - duration.lastStartTime; |
| 109 | VLOG("Max, key %s, Stop %lld %lld %lld", key.c_str(), |
| 110 | (long long)duration.lastStartTime, (long long)eventTime, |
| 111 | (long long)durationTime); |
| 112 | duration.lastDuration = duration.lastDuration + durationTime; |
| 113 | VLOG(" record duration: %lld ", (long long)duration.lastDuration); |
| 114 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | case DurationState::kPaused: { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 118 | duration.startCount--; |
| 119 | if (forceStop || !mNested || duration.startCount <= 0) { |
| 120 | duration.state = DurationState::kStopped; |
| 121 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (duration.lastDuration > mDuration) { |
| 127 | mDuration = duration.lastDuration; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 128 | detectAndDeclareAnomaly(eventTime, mCurrentBucketNum, mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 129 | VLOG("Max: new max duration: %lld", (long long)mDuration); |
| 130 | } |
| 131 | // Once an atom duration ends, we erase it. Next time, if we see another atom event with the |
| 132 | // same name, they are still considered as different atom durations. |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 133 | if (duration.state == DurationState::kStopped) { |
| 134 | mInfos.erase(key); |
| 135 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 136 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 137 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 138 | void MaxDurationTracker::noteStopAll(const uint64_t eventTime) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 139 | std::set<HashableDimensionKey> keys; |
| 140 | for (const auto& pair : mInfos) { |
| 141 | keys.insert(pair.first); |
| 142 | } |
| 143 | for (auto& key : keys) { |
| 144 | noteStop(key, eventTime, true); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 148 | bool MaxDurationTracker::flushIfNeeded( |
| 149 | uint64_t eventTime, unordered_map<HashableDimensionKey, vector<DurationBucket>>* output) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 150 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | VLOG("MaxDurationTracker flushing....."); |
| 155 | |
| 156 | // adjust the bucket start time |
| 157 | int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
| 158 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 159 | uint64_t endTime = mCurrentBucketStartTimeNs + mBucketSizeNs; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 160 | |
| 161 | DurationBucket info; |
| 162 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 163 | info.mBucketEndNs = endTime; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 164 | info.mBucketNum = mCurrentBucketNum; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 165 | |
| 166 | uint64_t oldBucketStartTimeNs = mCurrentBucketStartTimeNs; |
| 167 | mCurrentBucketStartTimeNs += (numBucketsForward)*mBucketSizeNs; |
| 168 | |
| 169 | bool hasOnGoingStartedEvent = false; // a kStarted event last across bucket boundaries. |
| 170 | bool hasPendingEvent = |
| 171 | false; // has either a kStarted or kPaused event across bucket boundaries |
| 172 | // meaning we need to carry them over to the new bucket. |
| 173 | for (auto it = mInfos.begin(); it != mInfos.end(); ++it) { |
| 174 | int64_t finalDuration = it->second.lastDuration; |
| 175 | if (it->second.state == kStarted) { |
| 176 | // the event is still on-going, duration needs to be updated. |
| 177 | // |..lastDurationTime_recorded...last_start -----|bucket_end. We need to record the |
| 178 | // duration between lastStartTime and bucketEnd. |
| 179 | int64_t durationTime = endTime - it->second.lastStartTime; |
| 180 | |
| 181 | finalDuration += durationTime; |
| 182 | VLOG(" unrecorded %lld -> %lld", (long long)(durationTime), (long long)finalDuration); |
| 183 | // if the event is still on-going, we need to fill the buckets between prev_bucket and |
| 184 | // now_bucket. |prev_bucket|...|..|...|now_bucket| |
| 185 | hasOnGoingStartedEvent = true; |
| 186 | } |
| 187 | |
| 188 | if (finalDuration > mDuration) { |
| 189 | mDuration = finalDuration; |
| 190 | } |
| 191 | |
| 192 | if (it->second.state == DurationState::kStopped) { |
| 193 | // No need to keep buckets for events that were stopped before. |
| 194 | mInfos.erase(it); |
| 195 | } else { |
| 196 | hasPendingEvent = true; |
| 197 | // for kPaused, and kStarted event, we will keep track of them, and reset the start time |
| 198 | // and duration. |
| 199 | it->second.lastStartTime = mCurrentBucketStartTimeNs; |
| 200 | it->second.lastDuration = 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (mDuration != 0) { |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 205 | info.mDuration = mDuration; |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 206 | (*output)[mEventKey].push_back(info); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 207 | addPastBucketToAnomalyTrackers(info.mDuration, info.mBucketNum); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 208 | VLOG(" final duration for last bucket: %lld", (long long)mDuration); |
| 209 | } |
| 210 | |
| 211 | mDuration = 0; |
| 212 | if (hasOnGoingStartedEvent) { |
| 213 | for (int i = 1; i < numBucketsForward; i++) { |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 214 | DurationBucket info; |
| 215 | info.mBucketStartNs = oldBucketStartTimeNs + mBucketSizeNs * i; |
| 216 | info.mBucketEndNs = endTime + mBucketSizeNs * i; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 217 | info.mBucketNum = mCurrentBucketNum + i; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 218 | info.mDuration = mBucketSizeNs; |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 219 | (*output)[mEventKey].push_back(info); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 220 | addPastBucketToAnomalyTrackers(info.mDuration, info.mBucketNum); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 221 | VLOG(" filling gap bucket with duration %lld", (long long)mBucketSizeNs); |
| 222 | } |
| 223 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 224 | |
| 225 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 226 | // If this tracker has no pending events, tell owner to remove. |
| 227 | return !hasPendingEvent; |
| 228 | } |
| 229 | |
| 230 | void MaxDurationTracker::onSlicedConditionMayChange(const uint64_t timestamp) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 231 | // Now for each of the on-going event, check if the condition has changed for them. |
| 232 | for (auto& pair : mInfos) { |
| 233 | if (pair.second.state == kStopped) { |
| 234 | continue; |
| 235 | } |
| 236 | bool conditionMet = mWizard->query(mConditionTrackerIndex, pair.second.conditionKeys) == |
| 237 | ConditionState::kTrue; |
| 238 | VLOG("key: %s, condition: %d", pair.first.c_str(), conditionMet); |
| 239 | noteConditionChanged(pair.first, conditionMet, timestamp); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void MaxDurationTracker::onConditionChanged(bool condition, const uint64_t timestamp) { |
| 244 | for (auto& pair : mInfos) { |
| 245 | noteConditionChanged(pair.first, condition, timestamp); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void MaxDurationTracker::noteConditionChanged(const HashableDimensionKey& key, bool conditionMet, |
| 250 | const uint64_t timestamp) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 251 | declareAnomalyIfAlarmExpired(timestamp); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 252 | auto it = mInfos.find(key); |
| 253 | if (it == mInfos.end()) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | switch (it->second.state) { |
| 258 | case kStarted: |
| 259 | // if condition becomes false, kStarted -> kPaused. Record the current duration. |
| 260 | if (!conditionMet) { |
| 261 | it->second.state = DurationState::kPaused; |
| 262 | it->second.lastDuration += (timestamp - it->second.lastStartTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 263 | VLOG("MaxDurationTracker Key: %s Started->Paused ", key.c_str()); |
| 264 | } |
| 265 | break; |
| 266 | case kStopped: |
| 267 | // nothing to do if it's stopped. |
| 268 | break; |
| 269 | case kPaused: |
| 270 | // if condition becomes true, kPaused -> kStarted. and the start time is the condition |
| 271 | // change time. |
| 272 | if (conditionMet) { |
| 273 | it->second.state = DurationState::kStarted; |
| 274 | it->second.lastStartTime = timestamp; |
| 275 | VLOG("MaxDurationTracker Key: %s Paused->Started", key.c_str()); |
| 276 | } |
| 277 | break; |
| 278 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 279 | if (it->second.lastDuration > mDuration) { |
| 280 | mDuration = it->second.lastDuration; |
| 281 | detectAndDeclareAnomaly(timestamp, mCurrentBucketNum, mDuration); |
| 282 | } |
| 283 | } |
| 284 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 285 | int64_t MaxDurationTracker::predictAnomalyTimestampNs(const DurationAnomalyTracker& anomalyTracker, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 286 | const uint64_t currentTimestamp) const { |
| 287 | ALOGE("Max duration producer does not support anomaly timestamp prediction!!!"); |
| 288 | return currentTimestamp; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | } // namespace statsd |
| 292 | } // namespace os |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 293 | } // namespace android |