Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 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 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 19 | #include "AlarmMonitor.h" |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 20 | #include "AnomalyTracker.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
| 26 | using std::unordered_map; |
| 27 | |
| 28 | class DurationAnomalyTracker : public virtual AnomalyTracker { |
| 29 | public: |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 30 | DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey, |
| 31 | const sp<AlarmMonitor>& alarmMonitor); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 32 | |
| 33 | virtual ~DurationAnomalyTracker(); |
| 34 | |
| 35 | // Starts the alarm at the given timestamp. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 36 | void startAlarm(const MetricDimensionKey& dimensionKey, const uint64_t& eventTime); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 37 | |
| 38 | // Stops the alarm. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 39 | void stopAlarm(const MetricDimensionKey& dimensionKey); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 40 | |
| 41 | // Stop all the alarms owned by this tracker. |
| 42 | void stopAllAlarms(); |
| 43 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 44 | // Declares the anomaly when the alarm expired given the current timestamp. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 45 | void declareAnomalyIfAlarmExpired(const MetricDimensionKey& dimensionKey, |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 46 | const uint64_t& timestampNs); |
| 47 | |
| 48 | // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 49 | // and removes it from firedAlarms. |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 50 | // Note that this will generally be called from a different thread from the other functions; |
| 51 | // the caller is responsible for thread safety. |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 52 | void informAlarmsFired(const uint64_t& timestampNs, |
| 53 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) override; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 54 | |
| 55 | protected: |
| 56 | // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they |
| 57 | // are still active. |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 58 | std::unordered_map<MetricDimensionKey, sp<const InternalAlarm>> mAlarms; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 59 | |
| 60 | // Anomaly alarm monitor. |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 61 | sp<AlarmMonitor> mAlarmMonitor; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 62 | |
| 63 | // Resets all bucket data. For use when all the data gets stale. |
| 64 | void resetStorage() override; |
| 65 | |
| 66 | FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 67 | FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm); |
| 68 | FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 69 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 70 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp); |
| 71 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace statsd |
| 75 | } // namespace os |
| 76 | } // namespace android |