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 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 17 | #pragma once |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 18 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | #include "anomaly/indexed_priority_queue.h" |
| 20 | |
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> |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 25 | #include <set> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | #include <unordered_set> |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 29 | using namespace android; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 30 | |
| 31 | using android::os::IStatsCompanionService; |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 32 | using std::unordered_set; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 33 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace os { |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 36 | namespace statsd { |
| 37 | |
| 38 | /** |
| 39 | * Represents an alarm, associated with some aggregate metric, holding a |
| 40 | * projected time at which the metric is expected to exceed its anomaly |
| 41 | * threshold. |
| 42 | * Timestamps are in seconds since epoch in a uint32, so will fail in year 2106. |
| 43 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 44 | struct InternalAlarm : public RefBase { |
Chih-Hung Hsieh | 3227aab | 2018-12-20 13:42:28 -0800 | [diff] [blame] | 45 | explicit InternalAlarm(uint32_t timestampSec) : timestampSec(timestampSec) { |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | const uint32_t timestampSec; |
| 49 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 50 | /** InternalAlarm a is smaller (higher priority) than b if its timestamp is sooner. */ |
Bookatz | 0e95909 | 2017-09-07 17:39:37 -0700 | [diff] [blame] | 51 | struct SmallerTimestamp { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 52 | bool operator()(sp<const InternalAlarm> a, sp<const InternalAlarm> b) const { |
Bookatz | 0e95909 | 2017-09-07 17:39:37 -0700 | [diff] [blame] | 53 | return (a->timestampSec < b->timestampSec); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | }; |
| 57 | |
| 58 | /** |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 59 | * Manages internal alarms that may get registered with the AlarmManager. |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 60 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 61 | class AlarmMonitor : public RefBase { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 62 | public: |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @param minDiffToUpdateRegisteredAlarmTimeSec If the soonest alarm differs |
| 65 | * from the registered alarm by more than this amount, update the registered |
| 66 | * alarm. |
| 67 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 68 | AlarmMonitor(uint32_t minDiffToUpdateRegisteredAlarmTimeSec, |
| 69 | const std::function<void(const sp<IStatsCompanionService>&, int64_t)>& updateAlarm, |
| 70 | const std::function<void(const sp<IStatsCompanionService>&)>& cancelAlarm); |
| 71 | ~AlarmMonitor(); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 74 | * Tells AnomalyMonitor what IStatsCompanionService to use and, if |
| 75 | * applicable, immediately registers an existing alarm with it. |
| 76 | * If nullptr, AnomalyMonitor will continue to add/remove alarms, but won't |
| 77 | * update IStatsCompanionService (until such time as it is set non-null). |
| 78 | */ |
| 79 | void setStatsCompanionService(sp<IStatsCompanionService> statsCompanionService); |
| 80 | |
| 81 | /** |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 82 | * Adds the given alarm (reference) to the queue. |
| 83 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 84 | void add(sp<const InternalAlarm> alarm); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Removes the given alarm (reference) from the queue. |
| 88 | * Note that alarm comparison is reference-based; if another alarm exists |
| 89 | * with the same timestampSec, that alarm will still remain in the queue. |
| 90 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 91 | void remove(sp<const InternalAlarm> alarm); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 94 | * Returns and removes all alarms whose timestamp <= the given timestampSec. |
| 95 | * Always updates the registered alarm if return is non-empty. |
| 96 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 97 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> popSoonerThan( |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 98 | uint32_t timestampSec); |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 101 | * Returns the projected alarm timestamp that is registered with |
| 102 | * StatsCompanionService. This may not be equal to the soonest alarm, |
| 103 | * but should be within minDiffToUpdateRegisteredAlarmTimeSec of it. |
| 104 | */ |
| 105 | uint32_t getRegisteredAlarmTimeSec() const { |
| 106 | return mRegisteredAlarmTimeSec; |
| 107 | } |
| 108 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 109 | private: |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 110 | std::mutex mLock; |
| 111 | |
| 112 | /** |
| 113 | * Timestamp (seconds since epoch) of the alarm registered with |
| 114 | * StatsCompanionService. This, in general, may not be equal to the soonest |
| 115 | * alarm stored in mPq, but should be within minUpdateTimeSec of it. |
| 116 | * A value of 0 indicates that no alarm is currently registered. |
| 117 | */ |
| 118 | uint32_t mRegisteredAlarmTimeSec; |
| 119 | |
| 120 | /** |
| 121 | * Priority queue of alarms, prioritized by soonest alarm.timestampSec. |
| 122 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 123 | indexed_priority_queue<InternalAlarm, InternalAlarm::SmallerTimestamp> mPq; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Binder interface for communicating with StatsCompanionService. |
| 127 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 128 | sp<IStatsCompanionService> mStatsCompanionService = nullptr; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Amount by which the soonest projected alarm must differ from |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 132 | * mRegisteredAlarmTimeSec before updateRegisteredAlarmTime_l is called. |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 133 | */ |
| 134 | uint32_t mMinUpdateTimeSec; |
| 135 | |
| 136 | /** |
| 137 | * Updates the alarm registered with StatsCompanionService to the given time. |
| 138 | * Also correspondingly updates mRegisteredAlarmTimeSec. |
| 139 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 140 | void updateRegisteredAlarmTime_l(uint32_t timestampSec); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 141 | |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 142 | /** |
| 143 | * Cancels the alarm registered with StatsCompanionService. |
| 144 | * Also correspondingly sets mRegisteredAlarmTimeSec to 0. |
| 145 | */ |
| 146 | void cancelRegisteredAlarmTime_l(); |
| 147 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 148 | /** Converts uint32 timestamp in seconds to a Java long in msec. */ |
| 149 | int64_t secToMs(uint32_t timeSec); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 150 | |
| 151 | // Callback function to update the alarm via StatsCompanionService. |
| 152 | std::function<void(const sp<IStatsCompanionService>, int64_t)> mUpdateAlarm; |
| 153 | |
| 154 | // Callback function to cancel the alarm via StatsCompanionService. |
| 155 | std::function<void(const sp<IStatsCompanionService>)> mCancelAlarm; |
| 156 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 159 | } // namespace statsd |
| 160 | } // namespace os |
| 161 | } // namespace android |