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 #pragma once 18 19 #include "AlarmMonitor.h" 20 #include "AnomalyTracker.h" 21 22 namespace android { 23 namespace os { 24 namespace statsd { 25 26 class DurationAnomalyTracker : public virtual AnomalyTracker { 27 public: 28 DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey, 29 const sp<AlarmMonitor>& alarmMonitor); 30 31 virtual ~DurationAnomalyTracker(); 32 33 // Sets an alarm for the given timestamp. 34 // Replaces previous alarm if one already exists. 35 void startAlarm(const MetricDimensionKey& dimensionKey, int64_t eventTime) override; 36 37 // Stops the alarm. 38 // If it should have already fired, but hasn't yet (e.g. because the AlarmManager is delayed), 39 // declare the anomaly now. 40 void stopAlarm(const MetricDimensionKey& dimensionKey, int64_t timestampNs) override; 41 42 // Stop all the alarms owned by this tracker. Does not declare any anomalies. 43 void cancelAllAlarms() override; 44 45 // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker 46 // and removes it from firedAlarms. The AlarmMonitor is not informed. 47 // Note that this will generally be called from a different thread from the other functions; 48 // the caller is responsible for thread safety. 49 void informAlarmsFired( 50 int64_t timestampNs, 51 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) override; 52 53 protected: 54 // Returns the alarm timestamp in seconds for the query dimension if it exists. Otherwise 55 // returns 0. getAlarmTimestampSec(const MetricDimensionKey & dimensionKey)56 uint32_t getAlarmTimestampSec(const MetricDimensionKey& dimensionKey) const override { 57 auto it = mAlarms.find(dimensionKey); 58 return it == mAlarms.end() ? 0 : it->second->timestampSec; 59 } 60 61 // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they 62 // are still active. 63 std::unordered_map<MetricDimensionKey, sp<const InternalAlarm>> mAlarms; 64 65 // Anomaly alarm monitor. 66 sp<AlarmMonitor> mAlarmMonitor; 67 68 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); 69 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm); 70 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm); 71 FRIEND_TEST(OringDurationTrackerTest, TestClearStateKeyMapWhenBucketFull); 72 FRIEND_TEST(OringDurationTrackerTest, TestClearStateKeyMapWhenNoTrackers); 73 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection); 74 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp); 75 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop); 76 }; 77 78 } // namespace statsd 79 } // namespace os 80 } // namespace android 81