blob: 6d9beb8f718de94d6cb3d5b8d0fb2367dc9d9704 [file] [log] [blame]
Yangster-mac932ecec2018-02-01 10:23:52 -08001/*
2 * Copyright (C) 2018 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-mac754e29e2018-05-02 12:23:17 -070017#define DEBUG false // STOPSHIP if true
Yangster-mac932ecec2018-02-01 10:23:52 -080018#include "Log.h"
19
20#include "anomaly/AlarmTracker.h"
21#include "anomaly/subscriber_util.h"
22#include "HashableDimensionKey.h"
23#include "stats_util.h"
24#include "storage/StorageManager.h"
25
Yangster-mac932ecec2018-02-01 10:23:52 -080026#include <time.h>
27
28namespace android {
29namespace os {
30namespace statsd {
31
Yangster-macb142cc82018-03-30 15:22:08 -070032AlarmTracker::AlarmTracker(const int64_t startMillis,
33 const int64_t currentMillis,
Yangster-mac932ecec2018-02-01 10:23:52 -080034 const Alarm& alarm, const ConfigKey& configKey,
35 const sp<AlarmMonitor>& alarmMonitor)
36 : mAlarmConfig(alarm),
37 mConfigKey(configKey),
38 mAlarmMonitor(alarmMonitor) {
39 VLOG("AlarmTracker() called");
40 mAlarmSec = (startMillis + mAlarmConfig.offset_millis()) / MS_PER_SEC;
Yangster-macc04feba2018-04-02 14:37:33 -070041 // startMillis is the time statsd is created. We need to find the 1st alarm timestamp after
42 // the config is added to statsd.
43 mAlarmSec = findNextAlarmSec(currentMillis / MS_PER_SEC); // round up
Yangster-mac932ecec2018-02-01 10:23:52 -080044 mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
Yangster-macc04feba2018-04-02 14:37:33 -070045 VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
Yangster-mac684d1952018-03-24 16:47:16 -070046 if (mAlarmMonitor != nullptr) {
47 mAlarmMonitor->add(mInternalAlarm);
48 }
Yangster-mac932ecec2018-02-01 10:23:52 -080049}
50
51AlarmTracker::~AlarmTracker() {
52 VLOG("~AlarmTracker() called");
Yangster-mac684d1952018-03-24 16:47:16 -070053 if (mInternalAlarm != nullptr && mAlarmMonitor != nullptr) {
Yangster-mac932ecec2018-02-01 10:23:52 -080054 mAlarmMonitor->remove(mInternalAlarm);
55 }
56}
57
58void AlarmTracker::addSubscription(const Subscription& subscription) {
59 mSubscriptions.push_back(subscription);
60}
61
Yangster-macc04feba2018-04-02 14:37:33 -070062int64_t AlarmTracker::findNextAlarmSec(int64_t currentTimeSec) {
Tej Singhf5cd94b2020-06-25 22:38:03 -070063 if (currentTimeSec < mAlarmSec) {
Yangster-macc04feba2018-04-02 14:37:33 -070064 return mAlarmSec;
65 }
66 int64_t periodsForward =
Tej Singhf5cd94b2020-06-25 22:38:03 -070067 ((currentTimeSec - mAlarmSec) * MS_PER_SEC) / mAlarmConfig.period_millis() + 1;
Yangster-macc04feba2018-04-02 14:37:33 -070068 return mAlarmSec + periodsForward * mAlarmConfig.period_millis() / MS_PER_SEC;
Yangster-mac932ecec2018-02-01 10:23:52 -080069}
70
71void AlarmTracker::informAlarmsFired(
Yangster-macb142cc82018-03-30 15:22:08 -070072 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080073 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) {
Yangster-mac684d1952018-03-24 16:47:16 -070074 if (firedAlarms.empty() || mInternalAlarm == nullptr ||
75 firedAlarms.find(mInternalAlarm) == firedAlarms.end()) {
Yangster-mac932ecec2018-02-01 10:23:52 -080076 return;
77 }
78 if (!mSubscriptions.empty()) {
Yangster-macc04feba2018-04-02 14:37:33 -070079 VLOG("AlarmTracker triggers the subscribers.");
Yao Chen4ce07292019-02-13 13:06:36 -080080 triggerSubscribers(mAlarmConfig.id(), 0 /*metricId N/A*/, DEFAULT_METRIC_DIMENSION_KEY,
81 0 /* metricValue N/A */, mConfigKey, mSubscriptions);
Yangster-mac932ecec2018-02-01 10:23:52 -080082 }
83 firedAlarms.erase(mInternalAlarm);
Yangster-mac684d1952018-03-24 16:47:16 -070084 mAlarmSec = findNextAlarmSec((timestampNs-1) / NS_PER_SEC + 1); // round up
Yangster-mac932ecec2018-02-01 10:23:52 -080085 mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
Yangster-macc04feba2018-04-02 14:37:33 -070086 VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
Yangster-mac684d1952018-03-24 16:47:16 -070087 if (mAlarmMonitor != nullptr) {
88 mAlarmMonitor->add(mInternalAlarm);
89 }
Yangster-mac932ecec2018-02-01 10:23:52 -080090}
91
92} // namespace statsd
93} // namespace os
94} // namespace android