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