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, const MetricDimensionKey& eventKey,
30                          const sp<ConditionWizard>& wizard, int conditionIndex, bool nesting,
31                          int64_t currentBucketStartNs, int64_t currentBucketNum,
32                          int64_t startTimeNs, int64_t bucketSizeNs, bool conditionSliced,
33                          bool fullLink, const std::vector<sp<AnomalyTracker>>& anomalyTrackers);
34 
35     OringDurationTracker(const OringDurationTracker& tracker) = default;
36 
37     void noteStart(const HashableDimensionKey& key, bool condition, int64_t eventTime,
38                    const ConditionKey& conditionKey, size_t dimensionHardLimit) override;
39     void noteStop(const HashableDimensionKey& key, int64_t eventTime, const bool stopAll) override;
40     void noteStopAll(const int64_t eventTime) override;
41 
42     void onSlicedConditionMayChange(const int64_t timestamp) override;
43     void onConditionChanged(bool condition, int64_t timestamp) override;
44 
45     void onStateChanged(const int64_t timestamp, const int32_t atomId,
46                         const FieldValue& newState) override;
47 
48     bool flushCurrentBucket(
49             int64_t eventTimeNs, const optional<UploadThreshold>& uploadThreshold,
50             const int64_t globalConditionTrueNs,
51             std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>>* output) override;
52     bool flushIfNeeded(
53             int64_t timestampNs, const optional<UploadThreshold>& uploadThreshold,
54             std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>>* output) override;
55 
56     int64_t predictAnomalyTimestampNs(const AnomalyTracker& anomalyTracker,
57                                       const int64_t currentTimestamp) const override;
58     void dumpStates(int out, bool verbose) const override;
59 
60     int64_t getCurrentStateKeyDuration() const override;
61 
62     int64_t getCurrentStateKeyFullBucketDuration() const override;
63 
64     void updateCurrentStateKey(int32_t atomId, const FieldValue& newState);
65 
66     bool hasAccumulatedDuration() const override;
67 
68 protected:
69     // Returns true if at least one of the mInfos is started.
70     bool hasStartedDuration() const override;
71 
72 private:
73     // We don't need to keep track of individual durations. The information that's needed is:
74     // 1) which keys are started. We record the first start time.
75     // 2) which keys are paused (started but condition was false)
76     // 3) whenever a key stops, we remove it from the started set. And if the set becomes empty,
77     //    it means everything has stopped, we then record the end time.
78     std::unordered_map<HashableDimensionKey, int> mStarted;
79     std::unordered_map<HashableDimensionKey, int> mPaused;
80     int64_t mLastStartTime;
81     std::unordered_map<HashableDimensionKey, ConditionKey> mConditionKeyMap;
82 
83     // return true if we should not allow newKey to be tracked because we are above the threshold
84     bool hitGuardRail(const HashableDimensionKey& newKey, size_t dimensionHardLimit) const;
85 
86     FRIEND_TEST(OringDurationTrackerTest, TestDurationOverlap);
87     FRIEND_TEST(OringDurationTrackerTest, TestCrossBucketBoundary);
88     FRIEND_TEST(OringDurationTrackerTest, TestDurationConditionChange);
89     FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
90     FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
91     FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
92     FRIEND_TEST(OringDurationTrackerTest, TestUploadThreshold);
93     FRIEND_TEST(OringDurationTrackerTest, TestClearStateKeyMapWhenBucketFull);
94     FRIEND_TEST(OringDurationTrackerTest, TestClearStateKeyMapWhenNoTrackers);
95 };
96 
97 }  // namespace statsd
98 }  // namespace os
99 }  // namespace android
100 
101 #endif  // ORING_DURATION_TRACKER_H
102