blob: dcf04dbcc8af88115544f25e0f9db67a3d503a29 [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 ORING_DURATION_TRACKER_H
18#define ORING_DURATION_TRACKER_H
19
20#include "DurationTracker.h"
21
22#include <set>
23namespace android {
24namespace os {
25namespace statsd {
26
27// Tracks the "Or'd" duration -- if 2 durations are overlapping, they won't be double counted.
28class OringDurationTracker : public DurationTracker {
29public:
Yao Chenb3561512017-11-21 18:07:17 -080030 OringDurationTracker(const ConfigKey& key, const string& name,
31 const HashableDimensionKey& eventKey, sp<ConditionWizard> wizard,
Yangster-mace2cd6d52017-11-09 20:38:30 -080032 int conditionIndex, bool nesting, uint64_t currentBucketStartNs,
33 uint64_t bucketSizeNs,
Bookatz857aaa52017-12-19 15:29:06 -080034 const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers);
Yangster7c334a12017-11-22 14:24:24 -080035
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 Chen5154a372017-10-30 22:57:06 -070042 void onSlicedConditionMayChange(const uint64_t timestamp) override;
43 void onConditionChanged(bool condition, const uint64_t timestamp) override;
Yangster7c334a12017-11-22 14:24:24 -080044
Yao Chenf60e0ba2017-11-29 15:06:41 -080045 bool flushIfNeeded(
46 uint64_t timestampNs,
47 std::unordered_map<HashableDimensionKey, std::vector<DurationBucket>>* output) override;
Yao Chen5154a372017-10-30 22:57:06 -070048
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 // 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 Chen0ea19902017-11-15 15:44:45 -080058 std::map<HashableDimensionKey, int> mStarted;
59 std::map<HashableDimensionKey, int> mPaused;
Yao Chen5154a372017-10-30 22:57:06 -070060 int64_t mLastStartTime;
61 std::map<HashableDimensionKey, ConditionKey> mConditionKeyMap;
Yangster-mace2cd6d52017-11-09 20:38:30 -080062
Yao Chenb3561512017-11-21 18:07:17 -080063 // 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-mace2cd6d52017-11-09 20:38:30 -080066 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 Chen5154a372017-10-30 22:57:06 -070071};
72
73} // namespace statsd
74} // namespace os
75} // namespace android
76
yro2b0f8862017-11-06 14:27:31 -080077#endif // ORING_DURATION_TRACKER_H