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