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, bool nesting, int64_t currentBucketStartNs, 32 int64_t currentBucketNum, int64_t startTimeNs, int64_t bucketSizeNs, 33 bool conditionSliced, bool fullLink, 34 const std::vector<sp<DurationAnomalyTracker>>& anomalyTrackers); 35 36 OringDurationTracker(const OringDurationTracker& tracker) = default; 37 38 void noteStart(const HashableDimensionKey& key, bool condition, const int64_t eventTime, 39 const ConditionKey& conditionKey) override; 40 void noteStop(const HashableDimensionKey& key, const int64_t eventTime, 41 const bool stopAll) override; 42 void noteStopAll(const int64_t eventTime) override; 43 44 void onSlicedConditionMayChange(bool overallCondition, const int64_t timestamp) override; 45 void onConditionChanged(bool condition, const int64_t timestamp) override; 46 47 void onStateChanged(const int64_t timestamp, const int32_t atomId, 48 const FieldValue& newState) 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 int64_t getCurrentStateKeyDuration() const override; 62 63 int64_t getCurrentStateKeyFullBucketDuration() const override; 64 65 void updateCurrentStateKey(const int32_t atomId, const FieldValue& newState); 66 67 private: 68 // We don't need to keep track of individual durations. The information that's needed is: 69 // 1) which keys are started. We record the first start time. 70 // 2) which keys are paused (started but condition was false) 71 // 3) whenever a key stops, we remove it from the started set. And if the set becomes empty, 72 // it means everything has stopped, we then record the end time. 73 std::unordered_map<HashableDimensionKey, int> mStarted; 74 std::unordered_map<HashableDimensionKey, int> mPaused; 75 int64_t mLastStartTime; 76 std::unordered_map<HashableDimensionKey, ConditionKey> mConditionKeyMap; 77 78 // return true if we should not allow newKey to be tracked because we are above the threshold 79 bool hitGuardRail(const HashableDimensionKey& newKey); 80 81 FRIEND_TEST(OringDurationTrackerTest, TestDurationOverlap); 82 FRIEND_TEST(OringDurationTrackerTest, TestCrossBucketBoundary); 83 FRIEND_TEST(OringDurationTrackerTest, TestDurationConditionChange); 84 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); 85 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm); 86 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm); 87 }; 88 89 } // namespace statsd 90 } // namespace os 91 } // namespace android 92 93 #endif // ORING_DURATION_TRACKER_H 94