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 namespace android {
23 namespace os {
24 namespace statsd {
25 
26 // Tracks the "Or'd" duration -- if 2 durations are overlapping, they won't be double counted.
27 class OringDurationTracker : public DurationTracker {
28 public:
29     OringDurationTracker(const ConfigKey& key, const int64_t& id,
30                          const MetricDimensionKey& eventKey, sp<ConditionWizard> wizard,
31                          int conditionIndex, const std::vector<Matcher>& dimensionInCondition,
32                          bool nesting, int64_t currentBucketStartNs, int64_t currentBucketNum,
33                          int64_t startTimeNs, int64_t bucketSizeNs, bool conditionSliced,
34                          bool fullLink,
35                          const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers);
36 
37     OringDurationTracker(const OringDurationTracker& tracker) = default;
38 
39     unique_ptr<DurationTracker> clone(const int64_t eventTime) override;
40 
41     void noteStart(const HashableDimensionKey& key, bool condition, const int64_t eventTime,
42                    const ConditionKey& conditionKey) override;
43     void noteStop(const HashableDimensionKey& key, const int64_t eventTime,
44                   const bool stopAll) override;
45     void noteStopAll(const int64_t eventTime) override;
46 
47     void onSlicedConditionMayChange(bool overallCondition, const int64_t timestamp) override;
48     void onConditionChanged(bool condition, const int64_t timestamp) override;
49 
50     bool flushCurrentBucket(
51             const int64_t& eventTimeNs,
52             std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>>* output) override;
53     bool flushIfNeeded(
54             int64_t timestampNs,
55             std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>>* output) override;
56 
57     int64_t predictAnomalyTimestampNs(const DurationAnomalyTracker& anomalyTracker,
58                                       const int64_t currentTimestamp) const override;
59     void dumpStates(FILE* out, bool verbose) const override;
60 
61 private:
62     // We don't need to keep track of individual durations. The information that's needed is:
63     // 1) which keys are started. We record the first start time.
64     // 2) which keys are paused (started but condition was false)
65     // 3) whenever a key stops, we remove it from the started set. And if the set becomes empty,
66     //    it means everything has stopped, we then record the end time.
67     std::unordered_map<HashableDimensionKey, int> mStarted;
68     std::unordered_map<HashableDimensionKey, int> mPaused;
69     int64_t mLastStartTime;
70     std::unordered_map<HashableDimensionKey, ConditionKey> mConditionKeyMap;
71 
72     // return true if we should not allow newKey to be tracked because we are above the threshold
73     bool hitGuardRail(const HashableDimensionKey& newKey);
74 
75     FRIEND_TEST(OringDurationTrackerTest, TestDurationOverlap);
76     FRIEND_TEST(OringDurationTrackerTest, TestCrossBucketBoundary);
77     FRIEND_TEST(OringDurationTrackerTest, TestDurationConditionChange);
78     FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
79     FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
80     FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
81 };
82 
83 }  // namespace statsd
84 }  // namespace os
85 }  // namespace android
86 
87 #endif  // ORING_DURATION_TRACKER_H
88