blob: bca858e67f13011f397113493ee43b5b6ec17e3e [file] [log] [blame]
Bookatz486d1cf2017-09-01 13:10:41 -07001/*
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-mace2cd6d52017-11-09 20:38:30 -080017#pragma once
Bookatz486d1cf2017-09-01 13:10:41 -070018
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#include "anomaly/indexed_priority_queue.h"
20
Bookatz486d1cf2017-09-01 13:10:41 -070021#include <android/os/IStatsCompanionService.h>
22#include <utils/RefBase.h>
23
24#include <queue>
Yangster-mace2cd6d52017-11-09 20:38:30 -080025#include <set>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include <unordered_set>
Bookatz486d1cf2017-09-01 13:10:41 -070027#include <vector>
28
Bookatz486d1cf2017-09-01 13:10:41 -070029using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070030
31using android::os::IStatsCompanionService;
Bookatzece5f702017-10-09 14:59:38 -070032using std::unordered_set;
Bookatz486d1cf2017-09-01 13:10:41 -070033
Bookatzb487b552017-09-18 11:26:01 -070034namespace android {
35namespace os {
Bookatz486d1cf2017-09-01 13:10:41 -070036namespace 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-mac932ecec2018-02-01 10:23:52 -080044struct InternalAlarm : public RefBase {
Chih-Hung Hsieh3227aab2018-12-20 13:42:28 -080045 explicit InternalAlarm(uint32_t timestampSec) : timestampSec(timestampSec) {
Bookatz486d1cf2017-09-01 13:10:41 -070046 }
47
48 const uint32_t timestampSec;
49
Yangster-mac932ecec2018-02-01 10:23:52 -080050 /** InternalAlarm a is smaller (higher priority) than b if its timestamp is sooner. */
Bookatz0e959092017-09-07 17:39:37 -070051 struct SmallerTimestamp {
Yangster-mac932ecec2018-02-01 10:23:52 -080052 bool operator()(sp<const InternalAlarm> a, sp<const InternalAlarm> b) const {
Bookatz0e959092017-09-07 17:39:37 -070053 return (a->timestampSec < b->timestampSec);
Bookatz486d1cf2017-09-01 13:10:41 -070054 }
55 };
56};
57
58/**
Yangster-mac932ecec2018-02-01 10:23:52 -080059 * Manages internal alarms that may get registered with the AlarmManager.
Bookatz486d1cf2017-09-01 13:10:41 -070060 */
Yangster-mac932ecec2018-02-01 10:23:52 -080061class AlarmMonitor : public RefBase {
Yao Chenef99c4f2017-09-22 16:26:54 -070062public:
Bookatz486d1cf2017-09-01 13:10:41 -070063 /**
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-mac932ecec2018-02-01 10:23:52 -080068 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();
Bookatz486d1cf2017-09-01 13:10:41 -070072
73 /**
Bookatzb487b552017-09-18 11:26:01 -070074 * 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 /**
Bookatz486d1cf2017-09-01 13:10:41 -070082 * Adds the given alarm (reference) to the queue.
83 */
Yangster-mac932ecec2018-02-01 10:23:52 -080084 void add(sp<const InternalAlarm> alarm);
Bookatz486d1cf2017-09-01 13:10:41 -070085
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-mac932ecec2018-02-01 10:23:52 -080091 void remove(sp<const InternalAlarm> alarm);
Bookatz486d1cf2017-09-01 13:10:41 -070092
93 /**
Bookatzece5f702017-10-09 14:59:38 -070094 * Returns and removes all alarms whose timestamp <= the given timestampSec.
95 * Always updates the registered alarm if return is non-empty.
96 */
Yangster-mac932ecec2018-02-01 10:23:52 -080097 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> popSoonerThan(
Joe Onorato9fc9edf2017-10-15 20:08:52 -070098 uint32_t timestampSec);
Bookatzece5f702017-10-09 14:59:38 -070099
100 /**
Bookatz486d1cf2017-09-01 13:10:41 -0700101 * 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 Chenef99c4f2017-09-22 16:26:54 -0700109private:
Bookatz486d1cf2017-09-01 13:10:41 -0700110 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-mac932ecec2018-02-01 10:23:52 -0800123 indexed_priority_queue<InternalAlarm, InternalAlarm::SmallerTimestamp> mPq;
Bookatz486d1cf2017-09-01 13:10:41 -0700124
125 /**
126 * Binder interface for communicating with StatsCompanionService.
127 */
Bookatzb487b552017-09-18 11:26:01 -0700128 sp<IStatsCompanionService> mStatsCompanionService = nullptr;
Bookatz486d1cf2017-09-01 13:10:41 -0700129
130 /**
131 * Amount by which the soonest projected alarm must differ from
Bookatzb487b552017-09-18 11:26:01 -0700132 * mRegisteredAlarmTimeSec before updateRegisteredAlarmTime_l is called.
Bookatz486d1cf2017-09-01 13:10:41 -0700133 */
134 uint32_t mMinUpdateTimeSec;
135
136 /**
137 * Updates the alarm registered with StatsCompanionService to the given time.
138 * Also correspondingly updates mRegisteredAlarmTimeSec.
139 */
Bookatzb487b552017-09-18 11:26:01 -0700140 void updateRegisteredAlarmTime_l(uint32_t timestampSec);
Bookatz486d1cf2017-09-01 13:10:41 -0700141
Bookatz1d0136d2017-12-01 11:13:32 -0800142 /**
143 * Cancels the alarm registered with StatsCompanionService.
144 * Also correspondingly sets mRegisteredAlarmTimeSec to 0.
145 */
146 void cancelRegisteredAlarmTime_l();
147
Bookatz486d1cf2017-09-01 13:10:41 -0700148 /** Converts uint32 timestamp in seconds to a Java long in msec. */
149 int64_t secToMs(uint32_t timeSec);
Yangster-mac932ecec2018-02-01 10:23:52 -0800150
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
Bookatz486d1cf2017-09-01 13:10:41 -0700157};
158
Yao Chenef99c4f2017-09-22 16:26:54 -0700159} // namespace statsd
160} // namespace os
161} // namespace android