blob: 019a9f7e5f9a49f024299ef0da60a5ea454c564b [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
26#include <statslog.h>
27#include <time.h>
28
29namespace android {
30namespace os {
31namespace statsd {
32
Yangster-macb142cc82018-03-30 15:22:08 -070033AlarmTracker::AlarmTracker(const int64_t startMillis,
34 const int64_t currentMillis,
Yangster-mac932ecec2018-02-01 10:23:52 -080035 const Alarm& alarm, const ConfigKey& configKey,
36 const sp<AlarmMonitor>& alarmMonitor)
37 : mAlarmConfig(alarm),
38 mConfigKey(configKey),
39 mAlarmMonitor(alarmMonitor) {
40 VLOG("AlarmTracker() called");
41 mAlarmSec = (startMillis + mAlarmConfig.offset_millis()) / MS_PER_SEC;
Yangster-macc04feba2018-04-02 14:37:33 -070042 // startMillis is the time statsd is created. We need to find the 1st alarm timestamp after
43 // the config is added to statsd.
44 mAlarmSec = findNextAlarmSec(currentMillis / MS_PER_SEC); // round up
Yangster-mac932ecec2018-02-01 10:23:52 -080045 mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
Yangster-macc04feba2018-04-02 14:37:33 -070046 VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
Yangster-mac684d1952018-03-24 16:47:16 -070047 if (mAlarmMonitor != nullptr) {
48 mAlarmMonitor->add(mInternalAlarm);
49 }
Yangster-mac932ecec2018-02-01 10:23:52 -080050}
51
52AlarmTracker::~AlarmTracker() {
53 VLOG("~AlarmTracker() called");
Yangster-mac684d1952018-03-24 16:47:16 -070054 if (mInternalAlarm != nullptr && mAlarmMonitor != nullptr) {
Yangster-mac932ecec2018-02-01 10:23:52 -080055 mAlarmMonitor->remove(mInternalAlarm);
56 }
57}
58
59void AlarmTracker::addSubscription(const Subscription& subscription) {
60 mSubscriptions.push_back(subscription);
61}
62
Yangster-macc04feba2018-04-02 14:37:33 -070063int64_t AlarmTracker::findNextAlarmSec(int64_t currentTimeSec) {
64 if (currentTimeSec <= mAlarmSec) {
65 return mAlarmSec;
66 }
67 int64_t periodsForward =
68 ((currentTimeSec - mAlarmSec) * MS_PER_SEC - 1) / mAlarmConfig.period_millis() + 1;
69 return mAlarmSec + periodsForward * mAlarmConfig.period_millis() / MS_PER_SEC;
Yangster-mac932ecec2018-02-01 10:23:52 -080070}
71
72void AlarmTracker::informAlarmsFired(
Yangster-macb142cc82018-03-30 15:22:08 -070073 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080074 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) {
Yangster-mac684d1952018-03-24 16:47:16 -070075 if (firedAlarms.empty() || mInternalAlarm == nullptr ||
76 firedAlarms.find(mInternalAlarm) == firedAlarms.end()) {
Yangster-mac932ecec2018-02-01 10:23:52 -080077 return;
78 }
79 if (!mSubscriptions.empty()) {
Yangster-macc04feba2018-04-02 14:37:33 -070080 VLOG("AlarmTracker triggers the subscribers.");
Yao Chen4ce07292019-02-13 13:06:36 -080081 triggerSubscribers(mAlarmConfig.id(), 0 /*metricId N/A*/, DEFAULT_METRIC_DIMENSION_KEY,
82 0 /* metricValue N/A */, mConfigKey, mSubscriptions);
Yangster-mac932ecec2018-02-01 10:23:52 -080083 }
84 firedAlarms.erase(mInternalAlarm);
Yangster-mac684d1952018-03-24 16:47:16 -070085 mAlarmSec = findNextAlarmSec((timestampNs-1) / NS_PER_SEC + 1); // round up
Yangster-mac932ecec2018-02-01 10:23:52 -080086 mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
Yangster-macc04feba2018-04-02 14:37:33 -070087 VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
Yangster-mac684d1952018-03-24 16:47:16 -070088 if (mAlarmMonitor != nullptr) {
89 mAlarmMonitor->add(mInternalAlarm);
90 }
Yangster-mac932ecec2018-02-01 10:23:52 -080091}
92
93} // namespace statsd
94} // namespace os
95} // namespace android