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 | #define LOG_TAG "AnomalyMonitor" |
| 18 | #define DEBUG true |
| 19 | |
| 20 | #include <AnomalyMonitor.h> |
| 21 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 22 | #include <cutils/log.h> |
| 23 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 24 | using namespace android::os::statsd; |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 25 | |
| 26 | AnomalyMonitor::AnomalyMonitor(uint32_t minDiffToUpdateRegisteredAlarmTimeSec) |
| 27 | : mRegisteredAlarmTimeSec(0), |
| 28 | mMinUpdateTimeSec(minDiffToUpdateRegisteredAlarmTimeSec) { |
| 29 | } |
| 30 | |
| 31 | AnomalyMonitor::~AnomalyMonitor() { |
| 32 | } |
| 33 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 34 | void 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 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 49 | void AnomalyMonitor::add(sp<const AnomalyAlarm> alarm) { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 50 | std::lock_guard<std::mutex> lock(mLock); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 51 | 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 | } |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 60 | // 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) { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 65 | updateRegisteredAlarmTime_l(alarm->timestampSec); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void AnomalyMonitor::remove(sp<const AnomalyAlarm> alarm) { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 70 | std::lock_guard<std::mutex> lock(mLock); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 71 | if (alarm == nullptr) { |
| 72 | ALOGW("Asked to remove a null alarm."); |
| 73 | return; |
| 74 | } |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 75 | if (DEBUG) ALOGD("Removing alarm with time %u", alarm->timestampSec); |
Bookatz | 0e95909 | 2017-09-07 17:39:37 -0700 | [diff] [blame] | 76 | mPq.remove(alarm); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 77 | if (mPq.empty()) { |
| 78 | if (DEBUG) ALOGD("Queue is empty. Cancel any alarm."); |
| 79 | mRegisteredAlarmTimeSec = 0; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 80 | if (mStatsCompanionService != nullptr) { |
| 81 | mStatsCompanionService->cancelAnomalyAlarm(); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 82 | } |
| 83 | return; |
| 84 | } |
| 85 | uint32_t soonestAlarmTimeSec = mPq.top()->timestampSec; |
| 86 | if (DEBUG) ALOGD("Soonest alarm is %u", soonestAlarmTimeSec); |
| 87 | if (soonestAlarmTimeSec > mRegisteredAlarmTimeSec + mMinUpdateTimeSec) { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 88 | updateRegisteredAlarmTime_l(soonestAlarmTimeSec); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 92 | void AnomalyMonitor::updateRegisteredAlarmTime_l(uint32_t timestampSec) { |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 93 | if (DEBUG) ALOGD("Updating reg alarm time to %u", timestampSec); |
| 94 | mRegisteredAlarmTimeSec = timestampSec; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 95 | if (mStatsCompanionService != nullptr) { |
| 96 | mStatsCompanionService->setAnomalyAlarm(secToMs(mRegisteredAlarmTimeSec)); |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Bookatz | 486d1cf | 2017-09-01 13:10:41 -0700 | [diff] [blame] | 100 | int64_t AnomalyMonitor::secToMs(uint32_t timeSec) { |
| 101 | return ((int64_t) timeSec) * 1000; |
| 102 | } |