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 | |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 35 | // Sets an alarm for the given timestamp. |
| 36 | // Replaces previous alarm if one already exists. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 37 | void startAlarm(const MetricDimensionKey& dimensionKey, const int64_t& eventTime); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 38 | |
| 39 | // Stops the alarm. |
Bookatz | 3e8cd35 | 2018-03-16 10:09:59 -0700 | [diff] [blame] | 40 | // If it should have already fired, but hasn't yet (e.g. because the AlarmManager is delayed), |
| 41 | // declare the anomaly now. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 42 | void stopAlarm(const MetricDimensionKey& dimensionKey, const int64_t& timestampNs); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 43 | |
Bookatz | 3e8cd35 | 2018-03-16 10:09:59 -0700 | [diff] [blame] | 44 | // Stop all the alarms owned by this tracker. Does not declare any anomalies. |
| 45 | void cancelAllAlarms(); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 46 | |
| 47 | // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 48 | // and removes it from firedAlarms. The AlarmMonitor is not informed. |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 49 | // Note that this will generally be called from a different thread from the other functions; |
| 50 | // the caller is responsible for thread safety. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 51 | void informAlarmsFired(const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 52 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) override; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 53 | |
| 54 | protected: |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 55 | // Returns the alarm timestamp in seconds for the query dimension if it exists. Otherwise |
| 56 | // returns 0. |
| 57 | uint32_t getAlarmTimestampSec(const MetricDimensionKey& dimensionKey) const override { |
| 58 | auto it = mAlarms.find(dimensionKey); |
| 59 | return it == mAlarms.end() ? 0 : it->second->timestampSec; |
| 60 | } |
| 61 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 62 | // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they |
| 63 | // are still active. |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 64 | std::unordered_map<MetricDimensionKey, sp<const InternalAlarm>> mAlarms; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 65 | |
| 66 | // Anomaly alarm monitor. |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 67 | sp<AlarmMonitor> mAlarmMonitor; |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 68 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 69 | FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 70 | FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm); |
| 71 | FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 72 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 73 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp); |
| 74 | FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace statsd |
| 78 | } // namespace os |
| 79 | } // namespace android |