blob: e89afa8a6497a5f115f2cedd3405b7b654ca317a [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
17#ifndef ANOMALY_MONITOR_H
18#define ANOMALY_MONITOR_H
19
Bookatz0e959092017-09-07 17:39:37 -070020#include <indexed_priority_queue.h>
Bookatz486d1cf2017-09-01 13:10:41 -070021#include <android/os/IStatsCompanionService.h>
22#include <utils/RefBase.h>
23
24#include <queue>
25#include <vector>
26
Bookatz486d1cf2017-09-01 13:10:41 -070027using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070028
29using android::os::IStatsCompanionService;
Bookatz486d1cf2017-09-01 13:10:41 -070030
Bookatzb487b552017-09-18 11:26:01 -070031namespace android {
32namespace os {
Bookatz486d1cf2017-09-01 13:10:41 -070033namespace 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 */
41struct AnomalyAlarm : public RefBase {
42 AnomalyAlarm(uint32_t timestampSec) : timestampSec(timestampSec) {
43 }
44
45 const uint32_t timestampSec;
46
Bookatz0e959092017-09-07 17:39:37 -070047 /** 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);
Bookatz486d1cf2017-09-01 13:10:41 -070051 }
52 };
53};
54
55/**
56 * Manages alarms for Anomaly Detection.
57 */
Bookatzb487b552017-09-18 11:26:01 -070058class AnomalyMonitor : public RefBase {
Bookatz486d1cf2017-09-01 13:10:41 -070059 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 /**
Bookatzb487b552017-09-18 11:26:01 -070069 * 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 /**
Bookatz486d1cf2017-09-01 13:10:41 -070077 * 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:
Bookatz486d1cf2017-09-01 13:10:41 -070098 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 */
Bookatz0e959092017-09-07 17:39:37 -0700111 indexed_priority_queue<AnomalyAlarm, AnomalyAlarm::SmallerTimestamp> mPq;
Bookatz486d1cf2017-09-01 13:10:41 -0700112
113 /**
114 * Binder interface for communicating with StatsCompanionService.
115 */
Bookatzb487b552017-09-18 11:26:01 -0700116 sp<IStatsCompanionService> mStatsCompanionService = nullptr;
Bookatz486d1cf2017-09-01 13:10:41 -0700117
118 /**
119 * Amount by which the soonest projected alarm must differ from
Bookatzb487b552017-09-18 11:26:01 -0700120 * mRegisteredAlarmTimeSec before updateRegisteredAlarmTime_l is called.
Bookatz486d1cf2017-09-01 13:10:41 -0700121 */
122 uint32_t mMinUpdateTimeSec;
123
124 /**
125 * Updates the alarm registered with StatsCompanionService to the given time.
126 * Also correspondingly updates mRegisteredAlarmTimeSec.
127 */
Bookatzb487b552017-09-18 11:26:01 -0700128 void updateRegisteredAlarmTime_l(uint32_t timestampSec);
Bookatz486d1cf2017-09-01 13:10:41 -0700129
130 /** Converts uint32 timestamp in seconds to a Java long in msec. */
131 int64_t secToMs(uint32_t timeSec);
132};
133
134} // namespace statsd
Bookatzb487b552017-09-18 11:26:01 -0700135} // namespace os
136} // namespace android
Bookatz486d1cf2017-09-01 13:10:41 -0700137
138#endif // ANOMALY_MONITOR_H