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<AnomalyTracker>>& 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, const optional<UploadThreshold>& uploadThreshold, 52 std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>>* output) override; 53 bool flushIfNeeded( 54 int64_t timestampNs, const optional<UploadThreshold>& uploadThreshold, 55 std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>>* output) override; 56 57 int64_t predictAnomalyTimestampNs(const AnomalyTracker& 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 protected: 68 // Returns true if at least one of the mInfos is started. 69 bool hasAccumulatingDuration() override; 70 71 private: 72 // We don't need to keep track of individual durations. The information that's needed is: 73 // 1) which keys are started. We record the first start time. 74 // 2) which keys are paused (started but condition was false) 75 // 3) whenever a key stops, we remove it from the started set. And if the set becomes empty, 76 // it means everything has stopped, we then record the end time. 77 std::unordered_map<HashableDimensionKey, int> mStarted; 78 std::unordered_map<HashableDimensionKey, int> mPaused; 79 int64_t mLastStartTime; 80 std::unordered_map<HashableDimensionKey, ConditionKey> mConditionKeyMap; 81 82 // return true if we should not allow newKey to be tracked because we are above the threshold 83 bool hitGuardRail(const HashableDimensionKey& newKey); 84 85 FRIEND_TEST(OringDurationTrackerTest, TestDurationOverlap); 86 FRIEND_TEST(OringDurationTrackerTest, TestCrossBucketBoundary); 87 FRIEND_TEST(OringDurationTrackerTest, TestDurationConditionChange); 88 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); 89 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm); 90 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm); 91 FRIEND_TEST(OringDurationTrackerTest, TestUploadThreshold); 92 }; 93 94 } // namespace statsd 95 } // namespace os 96 } // namespace android 97 98 #endif // ORING_DURATION_TRACKER_H 99