Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 17 | #define DEBUG false |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 19 | |
Alec Mouri | 1dc5f1e | 2019-09-18 21:13:01 -0700 | [diff] [blame] | 20 | #include "StatsPullerManager.h" |
| 21 | |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 22 | #include <android/os/IPullAtomCallback.h> |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 23 | #include <android/os/IStatsCompanionService.h> |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 24 | #include <cutils/log.h> |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 25 | #include <math.h> |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 26 | #include <stdint.h> |
Alec Mouri | 1dc5f1e | 2019-09-18 21:13:01 -0700 | [diff] [blame] | 27 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 28 | #include <algorithm> |
Alec Mouri | 1dc5f1e | 2019-09-18 21:13:01 -0700 | [diff] [blame] | 29 | #include <iostream> |
| 30 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 31 | #include "../StatsService.h" |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 32 | #include "../logd/LogEvent.h" |
| 33 | #include "../stats_log_util.h" |
| 34 | #include "../statscompanion_util.h" |
Yiwei Zhang | 7e63303 | 2019-03-01 17:25:27 -0800 | [diff] [blame] | 35 | #include "GpuStatsPuller.h" |
Bookatz | 92da283 | 2018-11-01 18:10:03 -0700 | [diff] [blame] | 36 | #include "PowerStatsPuller.h" |
Tej Singh | 4029831 | 2018-02-16 00:15:09 -0800 | [diff] [blame] | 37 | #include "ResourceHealthManagerPuller.h" |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 38 | #include "StatsCallbackPuller.h" |
Tej Singh | bf972d9 | 2018-01-10 20:51:13 -0800 | [diff] [blame] | 39 | #include "SubsystemSleepStatePuller.h" |
Chenjie Yu | 97dbb20 | 2019-02-13 16:42:04 -0800 | [diff] [blame] | 40 | #include "TrainInfoPuller.h" |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 41 | #include "statslog.h" |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 42 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 43 | using std::make_shared; |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 44 | using std::map; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 45 | using std::shared_ptr; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 46 | using std::string; |
| 47 | using std::vector; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 48 | using std::list; |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 49 | |
| 50 | namespace android { |
| 51 | namespace os { |
| 52 | namespace statsd { |
| 53 | |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 54 | // Values smaller than this may require to update the alarm. |
| 55 | const int64_t NO_ALARM_UPDATE = INT64_MAX; |
| 56 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 57 | StatsPullerManager::StatsPullerManager() |
| 58 | : kAllPullAtomInfo({ |
| 59 | // subsystem_sleep_state |
| 60 | {{.atomTag = android::util::SUBSYSTEM_SLEEP_STATE}, new SubsystemSleepStatePuller()}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 61 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 62 | // on_device_power_measurement |
| 63 | {{.atomTag = android::util::ON_DEVICE_POWER_MEASUREMENT}, new PowerStatsPuller()}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 64 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 65 | // remaining_battery_capacity |
| 66 | {{.atomTag = android::util::REMAINING_BATTERY_CAPACITY}, |
| 67 | new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 68 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 69 | // full_battery_capacity |
| 70 | {{.atomTag = android::util::FULL_BATTERY_CAPACITY}, |
| 71 | new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 72 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 73 | // battery_voltage |
| 74 | {{.atomTag = android::util::BATTERY_VOLTAGE}, |
| 75 | new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 76 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 77 | // battery_level |
| 78 | {{.atomTag = android::util::BATTERY_LEVEL}, |
| 79 | new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 80 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 81 | // battery_cycle_count |
| 82 | {{.atomTag = android::util::BATTERY_CYCLE_COUNT}, |
| 83 | new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 84 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 85 | // TrainInfo. |
| 86 | {{.atomTag = android::util::TRAIN_INFO}, new TrainInfoPuller()}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 87 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 88 | // GpuStatsGlobalInfo |
| 89 | {{.atomTag = android::util::GPU_STATS_GLOBAL_INFO}, |
| 90 | new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 91 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 92 | // GpuStatsAppInfo |
| 93 | {{.atomTag = android::util::GPU_STATS_APP_INFO}, |
| 94 | new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)}, |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 95 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 96 | }), |
| 97 | mNextPullTimeNs(NO_ALARM_UPDATE) { |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 100 | bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) { |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 101 | AutoMutex _l(mLock); |
| 102 | return PullLocked(tagId, data); |
| 103 | } |
| 104 | |
| 105 | bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 106 | VLOG("Initiating pulling %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 107 | |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 108 | if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 109 | bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second->Pull(data); |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 110 | VLOG("pulled %d items", (int)data->size()); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 111 | if (!ret) { |
| 112 | StatsdStats::getInstance().notePullFailed(tagId); |
| 113 | } |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 114 | return ret; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 115 | } else { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 116 | VLOG("Unknown tagId %d", tagId); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 117 | return false; // Return early since we don't know what to pull. |
| 118 | } |
| 119 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 120 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 121 | bool StatsPullerManager::PullerForMatcherExists(int tagId) const { |
Tej Singh | de473b51 | 2019-04-10 19:25:03 -0700 | [diff] [blame] | 122 | // Vendor pulled atoms might be registered after we parse the config. |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 123 | return isVendorPulledAtom(tagId) || |
| 124 | kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 127 | void StatsPullerManager::updateAlarmLocked() { |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 128 | if (mNextPullTimeNs == NO_ALARM_UPDATE) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 129 | VLOG("No need to set alarms. Skipping"); |
| 130 | return; |
| 131 | } |
| 132 | |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 133 | sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService; |
| 134 | if (statsCompanionServiceCopy != nullptr) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 135 | statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000); |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 136 | } else { |
| 137 | VLOG("StatsCompanionService not available. Alarm not set."); |
| 138 | } |
| 139 | return; |
| 140 | } |
| 141 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 142 | void StatsPullerManager::SetStatsCompanionService( |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 143 | sp<IStatsCompanionService> statsCompanionService) { |
| 144 | AutoMutex _l(mLock); |
| 145 | sp<IStatsCompanionService> tmpForLock = mStatsCompanionService; |
| 146 | mStatsCompanionService = statsCompanionService; |
| 147 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 148 | pulledAtom.second->SetStatsCompanionService(statsCompanionService); |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 149 | } |
| 150 | if (mStatsCompanionService != nullptr) { |
| 151 | updateAlarmLocked(); |
| 152 | } |
| 153 | } |
| 154 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 155 | void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 156 | int64_t nextPullTimeNs, int64_t intervalNs) { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 157 | AutoMutex _l(mLock); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 158 | auto& receivers = mReceivers[tagId]; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 159 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 160 | if (it->receiver == receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 161 | VLOG("Receiver already registered of %d", (int)receivers.size()); |
| 162 | return; |
| 163 | } |
| 164 | } |
| 165 | ReceiverInfo receiverInfo; |
| 166 | receiverInfo.receiver = receiver; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 167 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 168 | // Round it to the nearest minutes. This is the limit of alarm manager. |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 169 | // In practice, we should always have larger buckets. |
| 170 | int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60; |
Chenjie Yu | 83baaa1 | 2018-03-19 10:41:35 -0700 | [diff] [blame] | 171 | // Scheduled pulling should be at least 1 min apart. |
| 172 | // This can be lower in cts tests, in which case we round it to 1 min. |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 173 | if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) { |
| 174 | roundedIntervalNs = 60 * (int64_t)NS_PER_SEC; |
Chenjie Yu | 83baaa1 | 2018-03-19 10:41:35 -0700 | [diff] [blame] | 175 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 176 | |
| 177 | receiverInfo.intervalNs = roundedIntervalNs; |
| 178 | receiverInfo.nextPullTimeNs = nextPullTimeNs; |
| 179 | receivers.push_back(receiverInfo); |
| 180 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 181 | // There is only one alarm for all pulled events. So only set it to the smallest denom. |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 182 | if (nextPullTimeNs < mNextPullTimeNs) { |
| 183 | VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs); |
| 184 | mNextPullTimeNs = nextPullTimeNs; |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 185 | updateAlarmLocked(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 186 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 187 | VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 190 | void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 191 | AutoMutex _l(mLock); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 192 | if (mReceivers.find(tagId) == mReceivers.end()) { |
| 193 | VLOG("Unknown pull code or no receivers: %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 194 | return; |
| 195 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 196 | auto& receivers = mReceivers.find(tagId)->second; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 197 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 198 | if (receiver == it->receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 199 | receivers.erase(it); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 200 | VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 206 | void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 207 | AutoMutex _l(mLock); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 208 | int64_t wallClockNs = getWallClockNs(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 209 | |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 210 | int64_t minNextPullTimeNs = NO_ALARM_UPDATE; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 211 | |
| 212 | vector<pair<int, vector<ReceiverInfo*>>> needToPull = |
| 213 | vector<pair<int, vector<ReceiverInfo*>>>(); |
| 214 | for (auto& pair : mReceivers) { |
| 215 | vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 216 | if (pair.second.size() != 0) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 217 | for (ReceiverInfo& receiverInfo : pair.second) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 218 | if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 219 | receivers.push_back(&receiverInfo); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 220 | } else { |
| 221 | if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) { |
| 222 | minNextPullTimeNs = receiverInfo.nextPullTimeNs; |
| 223 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | if (receivers.size() > 0) { |
| 227 | needToPull.push_back(make_pair(pair.first, receivers)); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | for (const auto& pullInfo : needToPull) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 233 | vector<shared_ptr<LogEvent>> data; |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 234 | bool pullSuccess = PullLocked(pullInfo.first, &data); |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 235 | if (pullSuccess) { |
| 236 | StatsdStats::getInstance().notePullDelay( |
| 237 | pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs); |
| 238 | } else { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 239 | VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 240 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 241 | |
| 242 | // Convention is to mark pull atom timestamp at request time. |
| 243 | // If we pull at t0, puller starts at t1, finishes at t2, and send back |
| 244 | // at t3, we mark t0 as its timestamp, which should correspond to its |
| 245 | // triggering event, such as condition change at t0. |
| 246 | // Here the triggering event is alarm fired from AlarmManager. |
| 247 | // In ValueMetricProducer and GaugeMetricProducer we do same thing |
| 248 | // when pull on condition change, etc. |
| 249 | for (auto& event : data) { |
| 250 | event->setElapsedTimestampNs(elapsedTimeNs); |
| 251 | event->setLogdWallClockTimestampNs(wallClockNs); |
| 252 | } |
| 253 | |
| 254 | for (const auto& receiverInfo : pullInfo.second) { |
| 255 | sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote(); |
| 256 | if (receiverPtr != nullptr) { |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 257 | receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs); |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 258 | // We may have just come out of a coma, compute next pull time. |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 259 | int numBucketsAhead = |
| 260 | (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs; |
| 261 | receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs; |
| 262 | if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) { |
| 263 | minNextPullTimeNs = receiverInfo->nextPullTimeNs; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 264 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 265 | } else { |
| 266 | VLOG("receiver already gone."); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 267 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 270 | |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 271 | VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs, |
| 272 | (long long)minNextPullTimeNs); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 273 | mNextPullTimeNs = minNextPullTimeNs; |
| 274 | updateAlarmLocked(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 277 | int StatsPullerManager::ForceClearPullerCache() { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 278 | int totalCleared = 0; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 279 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 280 | totalCleared += pulledAtom.second->ForceClearCache(); |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 281 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 282 | return totalCleared; |
| 283 | } |
| 284 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 285 | int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 286 | int totalCleared = 0; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 287 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 288 | totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 289 | } |
| 290 | return totalCleared; |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 293 | void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag, |
| 294 | const int64_t coolDownNs, const int64_t timeoutNs, |
| 295 | const vector<int32_t>& additiveFields, |
| 296 | const sp<IPullAtomCallback>& callback) { |
| 297 | AutoMutex _l(mLock); |
| 298 | VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag); |
| 299 | // TODO: linkToDeath with the callback so that we can remove it and delete the puller. |
| 300 | StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true); |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 301 | kAllPullAtomInfo[{.atomTag = atomTag}] = |
| 302 | new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields); |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 305 | void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) { |
| 306 | AutoMutex _l(mLock); |
| 307 | StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false); |
| 308 | kAllPullAtomInfo.erase({.atomTag = atomTag}); |
| 309 | } |
| 310 | |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 311 | } // namespace statsd |
| 312 | } // namespace os |
| 313 | } // namespace android |