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 MAX_DURATION_TRACKER_H |
| 18 | #define MAX_DURATION_TRACKER_H |
| 19 | |
| 20 | #include "DurationTracker.h" |
| 21 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
| 26 | // Tracks a pool of atom durations, and output the max duration for each bucket. |
| 27 | // To get max duration, we need to keep track of each individual durations, and compare them when |
| 28 | // they stop or bucket expires. |
| 29 | class MaxDurationTracker : public DurationTracker { |
| 30 | public: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 31 | MaxDurationTracker(const ConfigKey& key, const string& name, |
| 32 | const HashableDimensionKey& eventKey, sp<ConditionWizard> wizard, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 33 | int conditionIndex, bool nesting, uint64_t currentBucketStartNs, |
| 34 | uint64_t bucketSizeNs, |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 35 | const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers); |
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 | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 42 | bool flushIfNeeded( |
| 43 | uint64_t timestampNs, |
| 44 | std::unordered_map<HashableDimensionKey, std::vector<DurationBucket>>* output) override; |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 45 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 46 | void onSlicedConditionMayChange(const uint64_t timestamp) override; |
| 47 | void onConditionChanged(bool condition, const uint64_t timestamp) override; |
| 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 | std::map<HashableDimensionKey, DurationInfo> mInfos; |
| 54 | |
| 55 | void noteConditionChanged(const HashableDimensionKey& key, bool conditionMet, |
| 56 | const uint64_t timestamp); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 57 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 58 | // return true if we should not allow newKey to be tracked because we are above the threshold |
| 59 | bool hitGuardRail(const HashableDimensionKey& newKey); |
| 60 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 61 | FRIEND_TEST(MaxDurationTrackerTest, TestSimpleMaxDuration); |
| 62 | FRIEND_TEST(MaxDurationTrackerTest, TestCrossBucketBoundary); |
| 63 | FRIEND_TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition); |
| 64 | FRIEND_TEST(MaxDurationTrackerTest, TestStopAll); |
| 65 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace statsd |
| 69 | } // namespace os |
| 70 | } // namespace android |
| 71 | |
| 72 | #endif // MAX_DURATION_TRACKER_H |