blob: 68c48cb10a4d869dc262642d6c345786fed58c8d [file] [log] [blame]
Yao Chen5154a372017-10-30 22:57:06 -07001/*
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 Chen5154a372017-10-30 22:57:06 -070022namespace 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:
Yangster-mac94e197c2018-01-02 16:03:03 -080031 MaxDurationTracker(const ConfigKey& key, const int64_t& id,
Yao Chenb3561512017-11-21 18:07:17 -080032 const HashableDimensionKey& eventKey, sp<ConditionWizard> wizard,
Yangster-mace2cd6d52017-11-09 20:38:30 -080033 int conditionIndex, bool nesting, uint64_t currentBucketStartNs,
34 uint64_t bucketSizeNs,
Bookatz857aaa52017-12-19 15:29:06 -080035 const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers);
Yao Chen5154a372017-10-30 22:57:06 -070036 void noteStart(const HashableDimensionKey& key, bool condition, const uint64_t eventTime,
37 const ConditionKey& conditionKey) override;
Yao Chen0ea19902017-11-15 15:44:45 -080038 void noteStop(const HashableDimensionKey& key, const uint64_t eventTime,
39 const bool stopAll) override;
Yao Chen5154a372017-10-30 22:57:06 -070040 void noteStopAll(const uint64_t eventTime) override;
Yangster7c334a12017-11-22 14:24:24 -080041
Yao Chenf60e0ba2017-11-29 15:06:41 -080042 bool flushIfNeeded(
43 uint64_t timestampNs,
44 std::unordered_map<HashableDimensionKey, std::vector<DurationBucket>>* output) override;
Yangster7c334a12017-11-22 14:24:24 -080045
Yao Chen5154a372017-10-30 22:57:06 -070046 void onSlicedConditionMayChange(const uint64_t timestamp) override;
47 void onConditionChanged(bool condition, const uint64_t timestamp) override;
48
Bookatz857aaa52017-12-19 15:29:06 -080049 int64_t predictAnomalyTimestampNs(const DurationAnomalyTracker& anomalyTracker,
Yangster-mace2cd6d52017-11-09 20:38:30 -080050 const uint64_t currentTimestamp) const override;
51
Yao Chen5154a372017-10-30 22:57:06 -070052private:
53 std::map<HashableDimensionKey, DurationInfo> mInfos;
54
55 void noteConditionChanged(const HashableDimensionKey& key, bool conditionMet,
56 const uint64_t timestamp);
Yangster-mace2cd6d52017-11-09 20:38:30 -080057
Yao Chenb3561512017-11-21 18:07:17 -080058 // 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-mace2cd6d52017-11-09 20:38:30 -080061 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 Chen5154a372017-10-30 22:57:06 -070066};
67
68} // namespace statsd
69} // namespace os
70} // namespace android
71
72#endif // MAX_DURATION_TRACKER_H