blob: 8fd52499b20461ea77a5fab887b64d92e50dd334 [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#define LOG_TAG "AnomalyMonitor"
18#define DEBUG true
19
20#include <AnomalyMonitor.h>
21
Bookatz486d1cf2017-09-01 13:10:41 -070022#include <cutils/log.h>
23
Bookatzb487b552017-09-18 11:26:01 -070024using namespace android::os::statsd;
Bookatz486d1cf2017-09-01 13:10:41 -070025
26AnomalyMonitor::AnomalyMonitor(uint32_t minDiffToUpdateRegisteredAlarmTimeSec)
27 : mRegisteredAlarmTimeSec(0),
28 mMinUpdateTimeSec(minDiffToUpdateRegisteredAlarmTimeSec) {
29}
30
31AnomalyMonitor::~AnomalyMonitor() {
32}
33
Bookatzb487b552017-09-18 11:26:01 -070034void AnomalyMonitor::setStatsCompanionService(sp<IStatsCompanionService> statsCompanionService) {
35 std::lock_guard<std::mutex> lock(mLock);
36 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
37 mStatsCompanionService = statsCompanionService;
38 if (statsCompanionService == nullptr) {
39 if (DEBUG) ALOGD("Erasing link to statsCompanionService");
40 return;
41 }
42 if (DEBUG) ALOGD("Creating link to statsCompanionService");
43 const sp<const AnomalyAlarm> top = mPq.top();
44 if (top != nullptr) {
45 updateRegisteredAlarmTime_l(top->timestampSec);
46 }
47}
48
Bookatz486d1cf2017-09-01 13:10:41 -070049void AnomalyMonitor::add(sp<const AnomalyAlarm> alarm) {
Bookatzb487b552017-09-18 11:26:01 -070050 std::lock_guard<std::mutex> lock(mLock);
Bookatz486d1cf2017-09-01 13:10:41 -070051 if (alarm == nullptr) {
52 ALOGW("Asked to add a null alarm.");
53 return;
54 }
55 if (alarm->timestampSec < 1) {
56 // forbidden since a timestamp 0 is used to indicate no alarm registered
57 ALOGW("Asked to add a 0-time alarm.");
58 return;
59 }
Bookatz486d1cf2017-09-01 13:10:41 -070060 // TODO: Ensure that refractory period is respected.
61 if (DEBUG) ALOGD("Adding alarm with time %u", alarm->timestampSec);
62 mPq.push(alarm);
63 if (mRegisteredAlarmTimeSec < 1 ||
64 alarm->timestampSec + mMinUpdateTimeSec < mRegisteredAlarmTimeSec) {
Bookatzb487b552017-09-18 11:26:01 -070065 updateRegisteredAlarmTime_l(alarm->timestampSec);
Bookatz486d1cf2017-09-01 13:10:41 -070066 }
67}
68
69void AnomalyMonitor::remove(sp<const AnomalyAlarm> alarm) {
Bookatzb487b552017-09-18 11:26:01 -070070 std::lock_guard<std::mutex> lock(mLock);
Bookatz486d1cf2017-09-01 13:10:41 -070071 if (alarm == nullptr) {
72 ALOGW("Asked to remove a null alarm.");
73 return;
74 }
Bookatz486d1cf2017-09-01 13:10:41 -070075 if (DEBUG) ALOGD("Removing alarm with time %u", alarm->timestampSec);
Bookatz0e959092017-09-07 17:39:37 -070076 mPq.remove(alarm);
Bookatz486d1cf2017-09-01 13:10:41 -070077 if (mPq.empty()) {
78 if (DEBUG) ALOGD("Queue is empty. Cancel any alarm.");
79 mRegisteredAlarmTimeSec = 0;
Bookatzb487b552017-09-18 11:26:01 -070080 if (mStatsCompanionService != nullptr) {
81 mStatsCompanionService->cancelAnomalyAlarm();
Bookatz486d1cf2017-09-01 13:10:41 -070082 }
83 return;
84 }
85 uint32_t soonestAlarmTimeSec = mPq.top()->timestampSec;
86 if (DEBUG) ALOGD("Soonest alarm is %u", soonestAlarmTimeSec);
87 if (soonestAlarmTimeSec > mRegisteredAlarmTimeSec + mMinUpdateTimeSec) {
Bookatzb487b552017-09-18 11:26:01 -070088 updateRegisteredAlarmTime_l(soonestAlarmTimeSec);
Bookatz486d1cf2017-09-01 13:10:41 -070089 }
90}
91
Bookatzb487b552017-09-18 11:26:01 -070092void AnomalyMonitor::updateRegisteredAlarmTime_l(uint32_t timestampSec) {
Bookatz486d1cf2017-09-01 13:10:41 -070093 if (DEBUG) ALOGD("Updating reg alarm time to %u", timestampSec);
94 mRegisteredAlarmTimeSec = timestampSec;
Bookatzb487b552017-09-18 11:26:01 -070095 if (mStatsCompanionService != nullptr) {
96 mStatsCompanionService->setAnomalyAlarm(secToMs(mRegisteredAlarmTimeSec));
Bookatz486d1cf2017-09-01 13:10:41 -070097 }
98}
99
Bookatz486d1cf2017-09-01 13:10:41 -0700100int64_t AnomalyMonitor::secToMs(uint32_t timeSec) {
101 return ((int64_t) timeSec) * 1000;
102}