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 STATSD_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         (mAlarmConfig.probability_of_informing() >= 1 ||
80          (mAlarmConfig.probability_of_informing() < 1 &&
81           ((float)rand() / (float)RAND_MAX) < mAlarmConfig.probability_of_informing()))) {
82         // Note that due to float imprecision, 0.0 and 1.0 might not truly mean never/always.
83         // The config writer was advised to use -0.1 and 1.1 for never/always.
84 
85         ALOGI("Fate decided that an alarm will trigger subscribers.");
86         triggerSubscribers(mAlarmConfig.id(), 0 /*metricId N/A*/, DEFAULT_METRIC_DIMENSION_KEY,
87                            0 /* metricValue N/A */, mConfigKey, mSubscriptions);
88     }
89     firedAlarms.erase(mInternalAlarm);
90     mAlarmSec = findNextAlarmSec((timestampNs-1) / NS_PER_SEC + 1); // round up
91     mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)};
92     VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec);
93     if (mAlarmMonitor != nullptr) {
94         mAlarmMonitor->add(mInternalAlarm);
95     }
96 }
97 
98 }  // namespace statsd
99 }  // namespace os
100 }  // namespace android
101