MaxDurationTracker.h revision 7ba8fc357e44ee3bb8e614c202852a1c46a9d4a8
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 22namespace android { 23namespace os { 24namespace 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. 29class MaxDurationTracker : public DurationTracker { 30public: 31 MaxDurationTracker(const ConfigKey& key, const int64_t& id, 32 const HashableDimensionKey& eventKey, sp<ConditionWizard> wizard, 33 int conditionIndex, bool nesting, uint64_t currentBucketStartNs, 34 uint64_t bucketSizeNs, bool conditionSliced, 35 const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers); 36 void noteStart(const HashableDimensionKey& key, bool condition, const uint64_t eventTime, 37 const ConditionKey& conditionKey) override; 38 void noteStop(const HashableDimensionKey& key, const uint64_t eventTime, 39 const bool stopAll) override; 40 void noteStopAll(const uint64_t eventTime) override; 41 42 bool flushIfNeeded( 43 uint64_t timestampNs, 44 std::unordered_map<HashableDimensionKey, std::vector<DurationBucket>>* output) override; 45 46 void onSlicedConditionMayChange(const uint64_t timestamp) override; 47 void onConditionChanged(bool condition, const uint64_t timestamp) override; 48 49 int64_t predictAnomalyTimestampNs(const DurationAnomalyTracker& anomalyTracker, 50 const uint64_t currentTimestamp) const override; 51 52private: 53 std::unordered_map<HashableDimensionKey, DurationInfo> mInfos; 54 55 void noteConditionChanged(const HashableDimensionKey& key, bool conditionMet, 56 const uint64_t timestamp); 57 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 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); 66}; 67 68} // namespace statsd 69} // namespace os 70} // namespace android 71 72#endif // MAX_DURATION_TRACKER_H 73