Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 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 | |
Yangster-mac | 754e29e | 2018-05-02 12:23:17 -0700 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 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 <statslog.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 33 | AlarmTracker::AlarmTracker(const int64_t startMillis, |
| 34 | const int64_t currentMillis, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 35 | 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-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 42 | // 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-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 45 | mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)}; |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 46 | VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec); |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 47 | if (mAlarmMonitor != nullptr) { |
| 48 | mAlarmMonitor->add(mInternalAlarm); |
| 49 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | AlarmTracker::~AlarmTracker() { |
| 53 | VLOG("~AlarmTracker() called"); |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 54 | if (mInternalAlarm != nullptr && mAlarmMonitor != nullptr) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 55 | mAlarmMonitor->remove(mInternalAlarm); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void AlarmTracker::addSubscription(const Subscription& subscription) { |
| 60 | mSubscriptions.push_back(subscription); |
| 61 | } |
| 62 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 63 | int64_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-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void AlarmTracker::informAlarmsFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 73 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 74 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) { |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 75 | if (firedAlarms.empty() || mInternalAlarm == nullptr || |
| 76 | firedAlarms.find(mInternalAlarm) == firedAlarms.end()) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | if (!mSubscriptions.empty()) { |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 80 | VLOG("AlarmTracker triggers the subscribers."); |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 81 | triggerSubscribers(mAlarmConfig.id(), 0 /*metricId N/A*/, DEFAULT_METRIC_DIMENSION_KEY, |
| 82 | 0 /* metricValue N/A */, mConfigKey, mSubscriptions); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 83 | } |
| 84 | firedAlarms.erase(mInternalAlarm); |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 85 | mAlarmSec = findNextAlarmSec((timestampNs-1) / NS_PER_SEC + 1); // round up |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 86 | mInternalAlarm = new InternalAlarm{static_cast<uint32_t>(mAlarmSec)}; |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 87 | VLOG("AlarmTracker sets the periodic alarm at: %lld", (long long)mAlarmSec); |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 88 | if (mAlarmMonitor != nullptr) { |
| 89 | mAlarmMonitor->add(mInternalAlarm); |
| 90 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace statsd |
| 94 | } // namespace os |
| 95 | } // namespace android |