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 | |
| 17 | #ifndef DURATION_TRACKER_H |
| 18 | #define DURATION_TRACKER_H |
| 19 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 20 | #include "anomaly/DurationAnomalyTracker.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 21 | #include "condition/ConditionWizard.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 23 | #include "stats_util.h" |
| 24 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
| 29 | enum DurationState { |
| 30 | kStopped = 0, // The event is stopped. |
| 31 | kStarted = 1, // The event is on going. |
| 32 | kPaused = 2, // The event is started, but condition is false, clock is paused. When condition |
| 33 | // turns to true, kPaused will become kStarted. |
| 34 | }; |
| 35 | |
| 36 | // Hold duration information for one atom level duration in current on-going bucket. |
| 37 | struct DurationInfo { |
| 38 | DurationState state; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 39 | |
| 40 | // the number of starts seen. |
| 41 | int32_t startCount; |
| 42 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 43 | // most recent start time. |
| 44 | int64_t lastStartTime; |
| 45 | // existing duration in current bucket. |
| 46 | int64_t lastDuration; |
| 47 | // TODO: Optimize the way we track sliced condition in duration metrics. |
| 48 | // cache the HashableDimensionKeys we need to query the condition for this duration event. |
| 49 | ConditionKey conditionKeys; |
| 50 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 51 | DurationInfo() : state(kStopped), startCount(0), lastStartTime(0), lastDuration(0){}; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 54 | struct DurationBucket { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 55 | uint64_t mBucketStartNs; |
| 56 | uint64_t mBucketEndNs; |
| 57 | uint64_t mDuration; |
| 58 | uint64_t mBucketNum; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 61 | class DurationTracker { |
| 62 | public: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 63 | DurationTracker(const ConfigKey& key, const string& name, const HashableDimensionKey& eventKey, |
| 64 | sp<ConditionWizard> wizard, int conditionIndex, bool nesting, |
| 65 | uint64_t currentBucketStartNs, uint64_t bucketSizeNs, |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 66 | const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers) |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 67 | : mConfigKey(key), |
| 68 | mName(name), |
| 69 | mEventKey(eventKey), |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 70 | mWizard(wizard), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 71 | mConditionTrackerIndex(conditionIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 72 | mBucketSizeNs(bucketSizeNs), |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 73 | mNested(nesting), |
| 74 | mCurrentBucketStartTimeNs(currentBucketStartNs), |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 75 | mDuration(0), |
| 76 | mCurrentBucketNum(0), |
| 77 | mAnomalyTrackers(anomalyTrackers){}; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 78 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 79 | virtual ~DurationTracker(){}; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 80 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 81 | virtual void noteStart(const HashableDimensionKey& key, bool condition, |
| 82 | const uint64_t eventTime, const ConditionKey& conditionKey) = 0; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 83 | virtual void noteStop(const HashableDimensionKey& key, const uint64_t eventTime, |
| 84 | const bool stopAll) = 0; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 85 | virtual void noteStopAll(const uint64_t eventTime) = 0; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 86 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 87 | virtual void onSlicedConditionMayChange(const uint64_t timestamp) = 0; |
| 88 | virtual void onConditionChanged(bool condition, const uint64_t timestamp) = 0; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 89 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 90 | // Flush stale buckets if needed, and return true if the tracker has no on-going duration |
| 91 | // events, so that the owner can safely remove the tracker. |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 92 | virtual bool flushIfNeeded( |
| 93 | uint64_t timestampNs, |
| 94 | std::unordered_map<HashableDimensionKey, std::vector<DurationBucket>>* output) = 0; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 95 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 96 | // Predict the anomaly timestamp given the current status. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 97 | virtual int64_t predictAnomalyTimestampNs(const DurationAnomalyTracker& anomalyTracker, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 98 | const uint64_t currentTimestamp) const = 0; |
| 99 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 100 | protected: |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | // Starts the anomaly alarm. |
| 102 | void startAnomalyAlarm(const uint64_t eventTime) { |
| 103 | for (auto& anomalyTracker : mAnomalyTrackers) { |
| 104 | if (anomalyTracker != nullptr) { |
| 105 | anomalyTracker->startAlarm(mEventKey, |
| 106 | predictAnomalyTimestampNs(*anomalyTracker, eventTime)); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Stops the anomaly alarm. |
| 112 | void stopAnomalyAlarm() { |
| 113 | for (auto& anomalyTracker : mAnomalyTrackers) { |
| 114 | if (anomalyTracker != nullptr) { |
| 115 | anomalyTracker->stopAlarm(mEventKey); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void addPastBucketToAnomalyTrackers(const int64_t& bucketValue, const int64_t& bucketNum) { |
| 121 | for (auto& anomalyTracker : mAnomalyTrackers) { |
| 122 | if (anomalyTracker != nullptr) { |
| 123 | anomalyTracker->addPastBucket(mEventKey, bucketValue, bucketNum); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void detectAndDeclareAnomaly(const uint64_t& timestamp, const int64_t& currBucketNum, |
| 129 | const int64_t& currentBucketValue) { |
| 130 | for (auto& anomalyTracker : mAnomalyTrackers) { |
| 131 | if (anomalyTracker != nullptr) { |
| 132 | anomalyTracker->detectAndDeclareAnomaly(timestamp, currBucketNum, mEventKey, |
| 133 | currentBucketValue); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void declareAnomalyIfAlarmExpired(const uint64_t& timestamp) { |
| 139 | for (auto& anomalyTracker : mAnomalyTrackers) { |
| 140 | if (anomalyTracker != nullptr) { |
| 141 | anomalyTracker->declareAnomalyIfAlarmExpired(mEventKey, timestamp); |
| 142 | } |
| 143 | } |
| 144 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 145 | // A reference to the DurationMetricProducer's config key. |
| 146 | const ConfigKey& mConfigKey; |
| 147 | |
| 148 | const std::string mName; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 149 | |
| 150 | HashableDimensionKey mEventKey; |
| 151 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 152 | sp<ConditionWizard> mWizard; |
| 153 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 154 | const int mConditionTrackerIndex; |
| 155 | |
| 156 | const int64_t mBucketSizeNs; |
| 157 | |
| 158 | const bool mNested; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 159 | |
| 160 | uint64_t mCurrentBucketStartTimeNs; |
| 161 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 162 | int64_t mDuration; // current recorded duration result |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 163 | |
| 164 | uint64_t mCurrentBucketNum; |
| 165 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 166 | std::vector<sp<DurationAnomalyTracker>> mAnomalyTrackers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 167 | |
| 168 | FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); |
| 169 | FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetection); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } // namespace statsd |
| 173 | } // namespace os |
| 174 | } // namespace android |
| 175 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 176 | #endif // DURATION_TRACKER_H |