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