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 ORING_DURATION_TRACKER_H |
| 18 | #define ORING_DURATION_TRACKER_H |
| 19 | |
| 20 | #include "DurationTracker.h" |
| 21 | |
| 22 | #include <set> |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
| 27 | // Tracks the "Or'd" duration -- if 2 durations are overlapping, they won't be double counted. |
| 28 | class OringDurationTracker : public DurationTracker { |
| 29 | public: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 30 | OringDurationTracker(const ConfigKey& key, const string& name, |
| 31 | const HashableDimensionKey& eventKey, sp<ConditionWizard> wizard, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 32 | int conditionIndex, bool nesting, uint64_t currentBucketStartNs, |
| 33 | uint64_t bucketSizeNs, |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 34 | const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers); |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 35 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 36 | void noteStart(const HashableDimensionKey& key, bool condition, const uint64_t eventTime, |
| 37 | const ConditionKey& conditionKey) override; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 38 | void noteStop(const HashableDimensionKey& key, const uint64_t eventTime, |
| 39 | const bool stopAll) override; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 40 | void noteStopAll(const uint64_t eventTime) override; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 41 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 42 | void onSlicedConditionMayChange(const uint64_t timestamp) override; |
| 43 | void onConditionChanged(bool condition, const uint64_t timestamp) override; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 44 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 45 | bool flushIfNeeded( |
| 46 | uint64_t timestampNs, |
| 47 | std::unordered_map<HashableDimensionKey, std::vector<DurationBucket>>* output) override; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 48 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 49 | int64_t predictAnomalyTimestampNs(const DurationAnomalyTracker& anomalyTracker, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 50 | const uint64_t currentTimestamp) const override; |
| 51 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 52 | private: |
| 53 | // We don't need to keep track of individual durations. The information that's needed is: |
| 54 | // 1) which keys are started. We record the first start time. |
| 55 | // 2) which keys are paused (started but condition was false) |
| 56 | // 3) whenever a key stops, we remove it from the started set. And if the set becomes empty, |
| 57 | // it means everything has stopped, we then record the end time. |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 58 | std::map<HashableDimensionKey, int> mStarted; |
| 59 | std::map<HashableDimensionKey, int> mPaused; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 60 | int64_t mLastStartTime; |
| 61 | std::map<HashableDimensionKey, ConditionKey> mConditionKeyMap; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 62 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 63 | // return true if we should not allow newKey to be tracked because we are above the threshold |
| 64 | bool hitGuardRail(const HashableDimensionKey& newKey); |
| 65 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 66 | FRIEND_TEST(OringDurationTrackerTest, TestDurationOverlap); |
| 67 | FRIEND_TEST(OringDurationTrackerTest, TestCrossBucketBoundary); |
| 68 | FRIEND_TEST(OringDurationTrackerTest, TestDurationConditionChange); |
| 69 | FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); |
| 70 | FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetection); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace statsd |
| 74 | } // namespace os |
| 75 | } // namespace android |
| 76 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 77 | #endif // ORING_DURATION_TRACKER_H |