Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [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 | #ifndef ANOMALY_MONITOR_H |
| 18 | #define ANOMALY_MONITOR_H |
| 19 | |
Bookatz | 0e95909 | 2017-09-07 17:39:37 -0700 | [diff] [blame] | 20 | #include <indexed_priority_queue.h> |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 21 | #include <android/os/IStatsCompanionService.h> |
| 22 | #include <utils/RefBase.h> |
| 23 | |
| 24 | #include <queue> |
| 25 | #include <vector> |
| 26 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 27 | using namespace android; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame^] | 28 | |
| 29 | using android::os::IStatsCompanionService; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 30 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace os { |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 33 | namespace statsd { |
| 34 | |
| 35 | /** |
| 36 | * Represents an alarm, associated with some aggregate metric, holding a |
| 37 | * projected time at which the metric is expected to exceed its anomaly |
| 38 | * threshold. |
| 39 | * Timestamps are in seconds since epoch in a uint32, so will fail in year 2106. |
| 40 | */ |
| 41 | struct AnomalyAlarm : public RefBase { |
| 42 | AnomalyAlarm(uint32_t timestampSec) : timestampSec(timestampSec) { |
| 43 | } |
| 44 | |
| 45 | const uint32_t timestampSec; |
| 46 | |
Bookatz | 0e95909 | 2017-09-07 17:39:37 -0700 | [diff] [blame] | 47 | /** AnomalyAlarm a is smaller (higher priority) than b if its timestamp is sooner. */ |
| 48 | struct SmallerTimestamp { |
| 49 | bool operator()(sp<const AnomalyAlarm> a, sp<const AnomalyAlarm> b) const { |
| 50 | return (a->timestampSec < b->timestampSec); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 51 | } |
| 52 | }; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Manages alarms for Anomaly Detection. |
| 57 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 58 | class AnomalyMonitor : public RefBase { |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 59 | public: |
| 60 | /** |
| 61 | * @param minDiffToUpdateRegisteredAlarmTimeSec If the soonest alarm differs |
| 62 | * from the registered alarm by more than this amount, update the registered |
| 63 | * alarm. |
| 64 | */ |
| 65 | AnomalyMonitor(uint32_t minDiffToUpdateRegisteredAlarmTimeSec); |
| 66 | ~AnomalyMonitor(); |
| 67 | |
| 68 | /** |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 69 | * Tells AnomalyMonitor what IStatsCompanionService to use and, if |
| 70 | * applicable, immediately registers an existing alarm with it. |
| 71 | * If nullptr, AnomalyMonitor will continue to add/remove alarms, but won't |
| 72 | * update IStatsCompanionService (until such time as it is set non-null). |
| 73 | */ |
| 74 | void setStatsCompanionService(sp<IStatsCompanionService> statsCompanionService); |
| 75 | |
| 76 | /** |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 77 | * Adds the given alarm (reference) to the queue. |
| 78 | */ |
| 79 | void add(sp<const AnomalyAlarm> alarm); |
| 80 | |
| 81 | /** |
| 82 | * Removes the given alarm (reference) from the queue. |
| 83 | * Note that alarm comparison is reference-based; if another alarm exists |
| 84 | * with the same timestampSec, that alarm will still remain in the queue. |
| 85 | */ |
| 86 | void remove(sp<const AnomalyAlarm> alarm); |
| 87 | |
| 88 | /** |
| 89 | * Returns the projected alarm timestamp that is registered with |
| 90 | * StatsCompanionService. This may not be equal to the soonest alarm, |
| 91 | * but should be within minDiffToUpdateRegisteredAlarmTimeSec of it. |
| 92 | */ |
| 93 | uint32_t getRegisteredAlarmTimeSec() const { |
| 94 | return mRegisteredAlarmTimeSec; |
| 95 | } |
| 96 | |
| 97 | private: |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 98 | std::mutex mLock; |
| 99 | |
| 100 | /** |
| 101 | * Timestamp (seconds since epoch) of the alarm registered with |
| 102 | * StatsCompanionService. This, in general, may not be equal to the soonest |
| 103 | * alarm stored in mPq, but should be within minUpdateTimeSec of it. |
| 104 | * A value of 0 indicates that no alarm is currently registered. |
| 105 | */ |
| 106 | uint32_t mRegisteredAlarmTimeSec; |
| 107 | |
| 108 | /** |
| 109 | * Priority queue of alarms, prioritized by soonest alarm.timestampSec. |
| 110 | */ |
Bookatz | 0e95909 | 2017-09-07 17:39:37 -0700 | [diff] [blame] | 111 | indexed_priority_queue<AnomalyAlarm, AnomalyAlarm::SmallerTimestamp> mPq; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Binder interface for communicating with StatsCompanionService. |
| 115 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 116 | sp<IStatsCompanionService> mStatsCompanionService = nullptr; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Amount by which the soonest projected alarm must differ from |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 120 | * mRegisteredAlarmTimeSec before updateRegisteredAlarmTime_l is called. |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 121 | */ |
| 122 | uint32_t mMinUpdateTimeSec; |
| 123 | |
| 124 | /** |
| 125 | * Updates the alarm registered with StatsCompanionService to the given time. |
| 126 | * Also correspondingly updates mRegisteredAlarmTimeSec. |
| 127 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 128 | void updateRegisteredAlarmTime_l(uint32_t timestampSec); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 129 | |
| 130 | /** Converts uint32 timestamp in seconds to a Java long in msec. */ |
| 131 | int64_t secToMs(uint32_t timeSec); |
| 132 | }; |
| 133 | |
| 134 | } // namespace statsd |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 135 | } // namespace os |
| 136 | } // namespace android |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 137 | |
| 138 | #endif // ANOMALY_MONITOR_H |