summaryrefslogtreecommitdiff
path: root/statsd/src/anomaly/AlarmMonitor.cpp
blob: b632d040eb43a36ede0f1dc5394342ddce82acee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define DEBUG false
#include "Log.h"

#include "anomaly/AlarmMonitor.h"
#include "guardrail/StatsdStats.h"

namespace android {
namespace os {
namespace statsd {

AlarmMonitor::AlarmMonitor(
        uint32_t minDiffToUpdateRegisteredAlarmTimeSec,
        const std::function<void(const shared_ptr<IStatsCompanionService>&, int64_t)>& updateAlarm,
        const std::function<void(const shared_ptr<IStatsCompanionService>&)>& cancelAlarm)
    : mRegisteredAlarmTimeSec(0),
      mMinUpdateTimeSec(minDiffToUpdateRegisteredAlarmTimeSec),
      mUpdateAlarm(updateAlarm),
      mCancelAlarm(cancelAlarm) {}

AlarmMonitor::~AlarmMonitor() {}

void AlarmMonitor::setStatsCompanionService(
        shared_ptr<IStatsCompanionService> statsCompanionService) {
    std::lock_guard<std::mutex> lock(mLock);
    shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
    mStatsCompanionService = statsCompanionService;
    if (statsCompanionService == nullptr) {
        VLOG("Erasing link to statsCompanionService");
        return;
    }
    VLOG("Creating link to statsCompanionService");
    const sp<const InternalAlarm> top = mPq.top();
    if (top != nullptr) {
        updateRegisteredAlarmTime_l(top->timestampSec);
    }
}

void AlarmMonitor::add(sp<const InternalAlarm> alarm) {
    std::lock_guard<std::mutex> lock(mLock);
    if (alarm == nullptr) {
        ALOGW("Asked to add a null alarm.");
        return;
    }
    if (alarm->timestampSec < 1) {
        // forbidden since a timestamp 0 is used to indicate no alarm registered
        ALOGW("Asked to add a 0-time alarm.");
        return;
    }
    // TODO(b/110563466): Ensure that refractory period is respected.
    VLOG("Adding alarm with time %u", alarm->timestampSec);
    mPq.push(alarm);
    if (mRegisteredAlarmTimeSec < 1 ||
        alarm->timestampSec + mMinUpdateTimeSec < mRegisteredAlarmTimeSec) {
        updateRegisteredAlarmTime_l(alarm->timestampSec);
    }
}

void AlarmMonitor::remove(sp<const InternalAlarm> alarm) {
    std::lock_guard<std::mutex> lock(mLock);
    if (alarm == nullptr) {
        ALOGW("Asked to remove a null alarm.");
        return;
    }
    VLOG("Removing alarm with time %u", alarm->timestampSec);
    bool wasPresent = mPq.remove(alarm);
    if (!wasPresent) return;
    if (mPq.empty()) {
        VLOG("Queue is empty. Cancel any alarm.");
        cancelRegisteredAlarmTime_l();
        return;
    }
    uint32_t soonestAlarmTimeSec = mPq.top()->timestampSec;
    VLOG("Soonest alarm is %u", soonestAlarmTimeSec);
    if (soonestAlarmTimeSec > mRegisteredAlarmTimeSec + mMinUpdateTimeSec) {
        updateRegisteredAlarmTime_l(soonestAlarmTimeSec);
    }
}

// More efficient than repeatedly calling remove(mPq.top()) since it batches the
// updates to the registered alarm.
unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> AlarmMonitor::popSoonerThan(
        uint32_t timestampSec) {
    VLOG("Removing alarms with time <= %u", timestampSec);
    unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> oldAlarms;
    std::lock_guard<std::mutex> lock(mLock);

    for (sp<const InternalAlarm> t = mPq.top(); t != nullptr && t->timestampSec <= timestampSec;
        t = mPq.top()) {
        oldAlarms.insert(t);
        mPq.pop();  // remove t
    }
    // Always update registered alarm time (if anything has changed).
    if (!oldAlarms.empty()) {
        if (mPq.empty()) {
            VLOG("Queue is empty. Cancel any alarm.");
            cancelRegisteredAlarmTime_l();
        } else {
            // Always update the registered alarm in this case (unlike remove()).
            updateRegisteredAlarmTime_l(mPq.top()->timestampSec);
        }
    }
    return oldAlarms;
}

void AlarmMonitor::updateRegisteredAlarmTime_l(uint32_t timestampSec) {
    VLOG("Updating reg alarm time to %u", timestampSec);
    mRegisteredAlarmTimeSec = timestampSec;
    mUpdateAlarm(mStatsCompanionService, secToMs(mRegisteredAlarmTimeSec));
}

void AlarmMonitor::cancelRegisteredAlarmTime_l() {
    VLOG("Cancelling reg alarm.");
    mRegisteredAlarmTimeSec = 0;
    mCancelAlarm(mStatsCompanionService);
}

int64_t AlarmMonitor::secToMs(uint32_t timeSec) {
    return ((int64_t)timeSec) * 1000;
}

}  // namespace statsd
}  // namespace os
}  // namespace android