1 /*
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
17 #define DEBUG false // STOPSHIP if true
18 #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 <time.h>
27
28 namespace android {
29 namespace os {
30 namespace statsd {
31
AlarmTracker(const int64_t startMillis,const int64_t currentMillis,const Alarm & alarm,const ConfigKey & configKey,const sp<AlarmMonitor> & alarmMonitor)32 AlarmTracker::AlarmTracker(const int64_t startMillis,
33 const int64_t currentMillis,
34 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;
41 // 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
44 mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
45 VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
46 if (mAlarmMonitor != nullptr) {
47 mAlarmMonitor->add(mInternalAlarm);
48 }
49 }
50
~AlarmTracker()51 AlarmTracker::~AlarmTracker() {
52 VLOG("~AlarmTracker() called");
53 if (mInternalAlarm != nullptr && mAlarmMonitor != nullptr) {
54 mAlarmMonitor->remove(mInternalAlarm);
55 }
56 }
57
addSubscription(const Subscription & subscription)58 void AlarmTracker::addSubscription(const Subscription& subscription) {
59 mSubscriptions.push_back(subscription);
60 }
61
findNextAlarmSec(int64_t currentTimeSec)62 int64_t AlarmTracker::findNextAlarmSec(int64_t currentTimeSec) {
63 if (currentTimeSec < mAlarmSec) {
64 return mAlarmSec;
65 }
66 int64_t periodsForward =
67 ((currentTimeSec - mAlarmSec) * MS_PER_SEC) / mAlarmConfig.period_millis() + 1;
68 return mAlarmSec + periodsForward * mAlarmConfig.period_millis() / MS_PER_SEC;
69 }
70
informAlarmsFired(const int64_t & timestampNs,unordered_set<sp<const InternalAlarm>,SpHash<InternalAlarm>> & firedAlarms)71 void AlarmTracker::informAlarmsFired(
72 const int64_t& timestampNs,
73 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) {
74 if (firedAlarms.empty() || mInternalAlarm == nullptr ||
75 firedAlarms.find(mInternalAlarm) == firedAlarms.end()) {
76 return;
77 }
78 if (!mSubscriptions.empty()) {
79 VLOG("AlarmTracker triggers the subscribers.");
80 triggerSubscribers(mAlarmConfig.id(), 0 /*metricId N/A*/, DEFAULT_METRIC_DIMENSION_KEY,
81 0 /* metricValue N/A */, mConfigKey, mSubscriptions);
82 }
83 firedAlarms.erase(mInternalAlarm);
84 mAlarmSec = findNextAlarmSec((timestampNs-1) / NS_PER_SEC + 1); // round up
85 mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
86 VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
87 if (mAlarmMonitor != nullptr) {
88 mAlarmMonitor->add(mInternalAlarm);
89 }
90 }
91
92 } // namespace statsd
93 } // namespace os
94 } // namespace android
95