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 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 22 | #include <cutils/log.h> |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 23 | #include <math.h> |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
Alec Mouri | 1dc5f1e | 2019-09-18 21:13:01 -0700 | [diff] [blame] | 25 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Alec Mouri | 1dc5f1e | 2019-09-18 21:13:01 -0700 | [diff] [blame] | 27 | #include <iostream> |
| 28 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 29 | #include "../StatsService.h" |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 30 | #include "../logd/LogEvent.h" |
| 31 | #include "../stats_log_util.h" |
| 32 | #include "../statscompanion_util.h" |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 33 | #include "StatsCallbackPuller.h" |
Chenjie Yu | 97dbb20 | 2019-02-13 16:42:04 -0800 | [diff] [blame] | 34 | #include "TrainInfoPuller.h" |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 35 | #include "statslog_statsd.h" |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 36 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 37 | using std::shared_ptr; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 38 | using std::vector; |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | namespace os { |
| 42 | namespace statsd { |
| 43 | |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 44 | // Stores the puller as a wp to avoid holding a reference in case it is unregistered and |
| 45 | // pullAtomCallbackDied is never called. |
| 46 | struct PullAtomCallbackDeathCookie { |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 47 | PullAtomCallbackDeathCookie(const wp<StatsPullerManager>& pullerManager, |
| 48 | const PullerKey& pullerKey, const wp<StatsPuller>& puller) : |
| 49 | mPullerManager(pullerManager), mPullerKey(pullerKey), mPuller(puller) { |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 52 | wp<StatsPullerManager> mPullerManager; |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 53 | PullerKey mPullerKey; |
| 54 | wp<StatsPuller> mPuller; |
| 55 | }; |
| 56 | |
| 57 | void StatsPullerManager::pullAtomCallbackDied(void* cookie) { |
| 58 | PullAtomCallbackDeathCookie* cookie_ = static_cast<PullAtomCallbackDeathCookie*>(cookie); |
Ruchir Rastogi | e20c53e | 2020-04-05 21:44:31 -0700 | [diff] [blame] | 59 | sp<StatsPullerManager> thiz = cookie_->mPullerManager.promote(); |
| 60 | if (!thiz) { |
| 61 | return; |
| 62 | } |
| 63 | |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 64 | const PullerKey& pullerKey = cookie_->mPullerKey; |
| 65 | wp<StatsPuller> puller = cookie_->mPuller; |
| 66 | |
| 67 | // Erase the mapping from the puller key to the puller if the mapping still exists. |
| 68 | // Note that we are removing the StatsPuller object, which internally holds the binder |
| 69 | // IPullAtomCallback. However, each new registration creates a new StatsPuller, so this works. |
| 70 | lock_guard<mutex> lock(thiz->mLock); |
| 71 | const auto& it = thiz->kAllPullAtomInfo.find(pullerKey); |
| 72 | if (it != thiz->kAllPullAtomInfo.end() && puller != nullptr && puller == it->second) { |
| 73 | StatsdStats::getInstance().notePullerCallbackRegistrationChanged(pullerKey.atomTag, |
| 74 | /*registered=*/false); |
| 75 | thiz->kAllPullAtomInfo.erase(pullerKey); |
| 76 | } |
| 77 | // The death recipient corresponding to this specific IPullAtomCallback can never |
| 78 | // be triggered again, so free up resources. |
| 79 | delete cookie_; |
| 80 | } |
| 81 | |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 82 | // Values smaller than this may require to update the alarm. |
| 83 | const int64_t NO_ALARM_UPDATE = INT64_MAX; |
| 84 | |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 85 | StatsPullerManager::StatsPullerManager() |
| 86 | : kAllPullAtomInfo({ |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 87 | // TrainInfo. |
Tej Singh | d804935 | 2020-04-02 23:31:08 -0700 | [diff] [blame] | 88 | {{.atomTag = util::TRAIN_INFO, .uid = AID_STATSD}, new TrainInfoPuller()}, |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 89 | }), |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 90 | mNextPullTimeNs(NO_ALARM_UPDATE), |
| 91 | mPullAtomCallbackDeathRecipient(AIBinder_DeathRecipient_new(pullAtomCallbackDied)) { |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 94 | bool StatsPullerManager::Pull(int tagId, const ConfigKey& configKey, |
| 95 | vector<shared_ptr<LogEvent>>* data, bool useUids) { |
| 96 | std::lock_guard<std::mutex> _l(mLock); |
| 97 | return PullLocked(tagId, configKey, data, useUids); |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 100 | bool StatsPullerManager::Pull(int tagId, const vector<int32_t>& uids, |
| 101 | vector<std::shared_ptr<LogEvent>>* data, bool useUids) { |
| 102 | std::lock_guard<std::mutex> _l(mLock); |
| 103 | return PullLocked(tagId, uids, data, useUids); |
| 104 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 105 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 106 | bool StatsPullerManager::PullLocked(int tagId, const ConfigKey& configKey, |
| 107 | vector<shared_ptr<LogEvent>>* data, bool useUids) { |
| 108 | vector<int32_t> uids; |
| 109 | if (useUids) { |
| 110 | auto uidProviderIt = mPullUidProviders.find(configKey); |
| 111 | if (uidProviderIt == mPullUidProviders.end()) { |
| 112 | ALOGE("Error pulling tag %d. No pull uid provider for config key %s", tagId, |
| 113 | configKey.ToString().c_str()); |
| 114 | return false; |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 115 | } |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 116 | sp<PullUidProvider> pullUidProvider = uidProviderIt->second.promote(); |
| 117 | if (pullUidProvider == nullptr) { |
| 118 | ALOGE("Error pulling tag %d, pull uid provider for config %s is gone.", tagId, |
| 119 | configKey.ToString().c_str()); |
| 120 | return false; |
| 121 | } |
| 122 | uids = pullUidProvider->getPullAtomUids(tagId); |
| 123 | } |
| 124 | return PullLocked(tagId, uids, data, useUids); |
| 125 | } |
| 126 | |
| 127 | bool StatsPullerManager::PullLocked(int tagId, const vector<int32_t>& uids, |
| 128 | vector<shared_ptr<LogEvent>>* data, bool useUids) { |
| 129 | VLOG("Initiating pulling %d", tagId); |
| 130 | if (useUids) { |
| 131 | for (int32_t uid : uids) { |
| 132 | PullerKey key = {.atomTag = tagId, .uid = uid}; |
| 133 | auto pullerIt = kAllPullAtomInfo.find(key); |
| 134 | if (pullerIt != kAllPullAtomInfo.end()) { |
| 135 | bool ret = pullerIt->second->Pull(data); |
| 136 | VLOG("pulled %zu items", data->size()); |
| 137 | if (!ret) { |
| 138 | StatsdStats::getInstance().notePullFailed(tagId); |
| 139 | } |
| 140 | return ret; |
| 141 | } |
| 142 | } |
| 143 | ALOGW("StatsPullerManager: Unknown tagId %d", tagId); |
| 144 | return false; // Return early since we don't know what to pull. |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 145 | } else { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 146 | PullerKey key = {.atomTag = tagId, .uid = -1}; |
| 147 | auto pullerIt = kAllPullAtomInfo.find(key); |
| 148 | if (pullerIt != kAllPullAtomInfo.end()) { |
| 149 | bool ret = pullerIt->second->Pull(data); |
| 150 | VLOG("pulled %zu items", data->size()); |
| 151 | if (!ret) { |
| 152 | StatsdStats::getInstance().notePullFailed(tagId); |
| 153 | } |
| 154 | return ret; |
| 155 | } |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 156 | ALOGW("StatsPullerManager: Unknown tagId %d", tagId); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 157 | return false; // Return early since we don't know what to pull. |
| 158 | } |
| 159 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 160 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 161 | bool StatsPullerManager::PullerForMatcherExists(int tagId) const { |
Tej Singh | 97db3ff | 2020-01-27 16:52:17 -0800 | [diff] [blame] | 162 | // Pulled atoms might be registered after we parse the config, so just make sure the id is in |
| 163 | // an appropriate range. |
| 164 | return isVendorPulledAtom(tagId) || isPulledAtom(tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 167 | void StatsPullerManager::updateAlarmLocked() { |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 168 | if (mNextPullTimeNs == NO_ALARM_UPDATE) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 169 | VLOG("No need to set alarms. Skipping"); |
| 170 | return; |
| 171 | } |
| 172 | |
Ruchir Rastogi | 1497e8f | 2020-03-03 16:04:42 -0800 | [diff] [blame] | 173 | // TODO(b/151045771): do not hold a lock while making a binder call |
| 174 | if (mStatsCompanionService != nullptr) { |
| 175 | mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000); |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 176 | } else { |
| 177 | VLOG("StatsCompanionService not available. Alarm not set."); |
| 178 | } |
| 179 | return; |
| 180 | } |
| 181 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 182 | void StatsPullerManager::SetStatsCompanionService( |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 183 | shared_ptr<IStatsCompanionService> statsCompanionService) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 184 | std::lock_guard<std::mutex> _l(mLock); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 185 | shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService; |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 186 | mStatsCompanionService = statsCompanionService; |
| 187 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 188 | pulledAtom.second->SetStatsCompanionService(statsCompanionService); |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 189 | } |
| 190 | if (mStatsCompanionService != nullptr) { |
| 191 | updateAlarmLocked(); |
| 192 | } |
| 193 | } |
| 194 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 195 | void StatsPullerManager::RegisterReceiver(int tagId, const ConfigKey& configKey, |
| 196 | wp<PullDataReceiver> receiver, int64_t nextPullTimeNs, |
| 197 | int64_t intervalNs) { |
| 198 | std::lock_guard<std::mutex> _l(mLock); |
| 199 | auto& receivers = mReceivers[{.atomTag = tagId, .configKey = configKey}]; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 200 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 201 | if (it->receiver == receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 202 | VLOG("Receiver already registered of %d", (int)receivers.size()); |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | ReceiverInfo receiverInfo; |
| 207 | receiverInfo.receiver = receiver; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 208 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 209 | // 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] | 210 | // In practice, we should always have larger buckets. |
| 211 | int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60; |
Chenjie Yu | 83baaa1 | 2018-03-19 10:41:35 -0700 | [diff] [blame] | 212 | // Scheduled pulling should be at least 1 min apart. |
| 213 | // 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] | 214 | if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) { |
| 215 | roundedIntervalNs = 60 * (int64_t)NS_PER_SEC; |
Chenjie Yu | 83baaa1 | 2018-03-19 10:41:35 -0700 | [diff] [blame] | 216 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 217 | |
| 218 | receiverInfo.intervalNs = roundedIntervalNs; |
| 219 | receiverInfo.nextPullTimeNs = nextPullTimeNs; |
| 220 | receivers.push_back(receiverInfo); |
| 221 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 222 | // 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] | 223 | if (nextPullTimeNs < mNextPullTimeNs) { |
| 224 | VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs); |
| 225 | mNextPullTimeNs = nextPullTimeNs; |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 226 | updateAlarmLocked(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 227 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 228 | VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 231 | void StatsPullerManager::UnRegisterReceiver(int tagId, const ConfigKey& configKey, |
| 232 | wp<PullDataReceiver> receiver) { |
| 233 | std::lock_guard<std::mutex> _l(mLock); |
| 234 | auto receiversIt = mReceivers.find({.atomTag = tagId, .configKey = configKey}); |
| 235 | if (receiversIt == mReceivers.end()) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 236 | VLOG("Unknown pull code or no receivers: %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 237 | return; |
| 238 | } |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 239 | std::list<ReceiverInfo>& receivers = receiversIt->second; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 240 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 241 | if (receiver == it->receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 242 | receivers.erase(it); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 243 | VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 249 | void StatsPullerManager::RegisterPullUidProvider(const ConfigKey& configKey, |
| 250 | wp<PullUidProvider> provider) { |
| 251 | std::lock_guard<std::mutex> _l(mLock); |
| 252 | mPullUidProviders[configKey] = provider; |
| 253 | } |
| 254 | |
Tej Singh | 3eb9ced | 2020-04-20 22:04:38 -0700 | [diff] [blame] | 255 | void StatsPullerManager::UnregisterPullUidProvider(const ConfigKey& configKey, |
| 256 | wp<PullUidProvider> provider) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 257 | std::lock_guard<std::mutex> _l(mLock); |
Tej Singh | 3eb9ced | 2020-04-20 22:04:38 -0700 | [diff] [blame] | 258 | const auto& it = mPullUidProviders.find(configKey); |
| 259 | if (it != mPullUidProviders.end() && it->second == provider) { |
| 260 | mPullUidProviders.erase(it); |
| 261 | } |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 264 | void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 265 | std::lock_guard<std::mutex> _l(mLock); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 266 | int64_t wallClockNs = getWallClockNs(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 267 | |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 268 | int64_t minNextPullTimeNs = NO_ALARM_UPDATE; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 269 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 270 | vector<pair<const ReceiverKey*, vector<ReceiverInfo*>>> needToPull; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 271 | for (auto& pair : mReceivers) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 272 | vector<ReceiverInfo*> receivers; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 273 | if (pair.second.size() != 0) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 274 | for (ReceiverInfo& receiverInfo : pair.second) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 275 | if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 276 | receivers.push_back(&receiverInfo); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 277 | } else { |
| 278 | if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) { |
| 279 | minNextPullTimeNs = receiverInfo.nextPullTimeNs; |
| 280 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | if (receivers.size() > 0) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 284 | needToPull.push_back(make_pair(&pair.first, receivers)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 288 | for (const auto& pullInfo : needToPull) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 289 | vector<shared_ptr<LogEvent>> data; |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 290 | bool pullSuccess = PullLocked(pullInfo.first->atomTag, pullInfo.first->configKey, &data); |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 291 | if (pullSuccess) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 292 | StatsdStats::getInstance().notePullDelay(pullInfo.first->atomTag, |
| 293 | getElapsedRealtimeNs() - elapsedTimeNs); |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 294 | } else { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 295 | VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 296 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 297 | |
| 298 | // Convention is to mark pull atom timestamp at request time. |
| 299 | // If we pull at t0, puller starts at t1, finishes at t2, and send back |
| 300 | // at t3, we mark t0 as its timestamp, which should correspond to its |
| 301 | // triggering event, such as condition change at t0. |
| 302 | // Here the triggering event is alarm fired from AlarmManager. |
| 303 | // In ValueMetricProducer and GaugeMetricProducer we do same thing |
| 304 | // when pull on condition change, etc. |
| 305 | for (auto& event : data) { |
| 306 | event->setElapsedTimestampNs(elapsedTimeNs); |
| 307 | event->setLogdWallClockTimestampNs(wallClockNs); |
| 308 | } |
| 309 | |
| 310 | for (const auto& receiverInfo : pullInfo.second) { |
| 311 | sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote(); |
| 312 | if (receiverPtr != nullptr) { |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 313 | receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs); |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 314 | // 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] | 315 | int numBucketsAhead = |
| 316 | (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs; |
| 317 | receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs; |
| 318 | if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) { |
| 319 | minNextPullTimeNs = receiverInfo->nextPullTimeNs; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 320 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 321 | } else { |
| 322 | VLOG("receiver already gone."); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 323 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 326 | |
Chenjie Yu | 3b3adcd | 2018-04-18 16:25:36 -0700 | [diff] [blame] | 327 | VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs, |
| 328 | (long long)minNextPullTimeNs); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 329 | mNextPullTimeNs = minNextPullTimeNs; |
| 330 | updateAlarmLocked(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 333 | int StatsPullerManager::ForceClearPullerCache() { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 334 | int totalCleared = 0; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 335 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 336 | totalCleared += pulledAtom.second->ForceClearCache(); |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 337 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 338 | return totalCleared; |
| 339 | } |
| 340 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 341 | int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 342 | int totalCleared = 0; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 343 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 344 | totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 345 | } |
| 346 | return totalCleared; |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 349 | void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag, |
| 350 | const int64_t coolDownNs, const int64_t timeoutNs, |
| 351 | const vector<int32_t>& additiveFields, |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 352 | const shared_ptr<IPullAtomCallback>& callback, |
| 353 | bool useUid) { |
| 354 | std::lock_guard<std::mutex> _l(mLock); |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 355 | VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag); |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 356 | |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 357 | StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true); |
Tej Singh | abc1b8de | 2020-03-30 17:19:36 -0700 | [diff] [blame] | 358 | int64_t actualCoolDownNs = coolDownNs < kMinCoolDownNs ? kMinCoolDownNs : coolDownNs; |
| 359 | int64_t actualTimeoutNs = timeoutNs > kMaxTimeoutNs ? kMaxTimeoutNs : timeoutNs; |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 360 | |
| 361 | sp<StatsCallbackPuller> puller = new StatsCallbackPuller(atomTag, callback, actualCoolDownNs, |
| 362 | actualTimeoutNs, additiveFields); |
| 363 | PullerKey key = {.atomTag = atomTag, .uid = useUid ? uid : -1}; |
| 364 | AIBinder_linkToDeath(callback->asBinder().get(), mPullAtomCallbackDeathRecipient.get(), |
| 365 | new PullAtomCallbackDeathCookie(this, key, puller)); |
| 366 | kAllPullAtomInfo[key] = puller; |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 369 | void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag, |
| 370 | bool useUids) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 371 | std::lock_guard<std::mutex> _l(mLock); |
Tej Singh | dd25c82 | 2020-04-01 14:59:36 -0700 | [diff] [blame] | 372 | PullerKey key = {.atomTag = atomTag, .uid = useUids ? uid : -1}; |
| 373 | if (kAllPullAtomInfo.find(key) != kAllPullAtomInfo.end()) { |
| 374 | StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, |
| 375 | /*registered=*/false); |
| 376 | kAllPullAtomInfo.erase(key); |
| 377 | } |
Tej Singh | fa1c137 | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 380 | } // namespace statsd |
| 381 | } // namespace os |
| 382 | } // namespace android |