blob: 1a52eb928777e45cd912de18599a7f42aac505a1 [file] [log] [blame]
Chenjie Yu1a317ba2017-10-05 16:05:32 -07001/*
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 Yu80f91122018-01-31 20:24:50 -080017#define DEBUG false
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070019
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070020#include "StatsPullerManager.h"
21
David Chen1481fe12017-10-16 13:16:34 -070022#include <cutils/log.h>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070023#include <math.h>
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070024#include <stdint.h>
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070025
David Chen1481fe12017-10-16 13:16:34 -070026#include <algorithm>
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070027#include <iostream>
28
Chenjie Yu1a0a9412018-03-28 10:07:22 -070029#include "../StatsService.h"
Chenjie Yuaa5b2012018-03-21 13:53:15 -070030#include "../logd/LogEvent.h"
31#include "../stats_log_util.h"
32#include "../statscompanion_util.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080033#include "StatsCallbackPuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080034#include "TrainInfoPuller.h"
Jeffrey Huang74fc4352020-03-06 15:18:33 -080035#include "statslog_statsd.h"
David Chen1481fe12017-10-16 13:16:34 -070036
Yao Chen93fe3a32017-11-02 13:52:59 -070037using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070038using std::vector;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070039
40namespace android {
41namespace os {
42namespace statsd {
43
Tej Singhdd25c822020-04-01 14:59:36 -070044// Stores the puller as a wp to avoid holding a reference in case it is unregistered and
45// pullAtomCallbackDied is never called.
46struct PullAtomCallbackDeathCookie {
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070047 PullAtomCallbackDeathCookie(const wp<StatsPullerManager>& pullerManager,
48 const PullerKey& pullerKey, const wp<StatsPuller>& puller) :
49 mPullerManager(pullerManager), mPullerKey(pullerKey), mPuller(puller) {
Tej Singhdd25c822020-04-01 14:59:36 -070050 }
51
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070052 wp<StatsPullerManager> mPullerManager;
Tej Singhdd25c822020-04-01 14:59:36 -070053 PullerKey mPullerKey;
54 wp<StatsPuller> mPuller;
55};
56
57void StatsPullerManager::pullAtomCallbackDied(void* cookie) {
58 PullAtomCallbackDeathCookie* cookie_ = static_cast<PullAtomCallbackDeathCookie*>(cookie);
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070059 sp<StatsPullerManager> thiz = cookie_->mPullerManager.promote();
60 if (!thiz) {
61 return;
62 }
63
Tej Singhdd25c822020-04-01 14:59:36 -070064 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 Yu3b3adcd2018-04-18 16:25:36 -070082// Values smaller than this may require to update the alarm.
83const int64_t NO_ALARM_UPDATE = INT64_MAX;
84
Tej Singh5b4951b2020-01-24 13:23:56 -080085StatsPullerManager::StatsPullerManager()
86 : kAllPullAtomInfo({
Tej Singh5b4951b2020-01-24 13:23:56 -080087 // TrainInfo.
Tej Singhd8049352020-04-02 23:31:08 -070088 {{.atomTag = util::TRAIN_INFO, .uid = AID_STATSD}, new TrainInfoPuller()},
Tej Singh5b4951b2020-01-24 13:23:56 -080089 }),
Tej Singhdd25c822020-04-01 14:59:36 -070090 mNextPullTimeNs(NO_ALARM_UPDATE),
91 mPullAtomCallbackDeathRecipient(AIBinder_DeathRecipient_new(pullAtomCallbackDied)) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070092}
93
Tej Singh3be093b2020-03-04 20:08:38 -080094bool 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 Singhfa1c1372019-12-05 20:36:54 -080098}
99
Tej Singh3be093b2020-03-04 20:08:38 -0800100bool 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 Yub3dda412017-10-24 13:41:59 -0700105
Tej Singh3be093b2020-03-04 20:08:38 -0800106bool 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());
Tej Singh873e91a2020-04-28 00:33:11 -0700114 StatsdStats::getInstance().notePullUidProviderNotFound(tagId);
Tej Singh3be093b2020-03-04 20:08:38 -0800115 return false;
Misha Wagner1eee2212019-01-22 11:47:11 +0000116 }
Tej Singh3be093b2020-03-04 20:08:38 -0800117 sp<PullUidProvider> pullUidProvider = uidProviderIt->second.promote();
118 if (pullUidProvider == nullptr) {
119 ALOGE("Error pulling tag %d, pull uid provider for config %s is gone.", tagId,
120 configKey.ToString().c_str());
Tej Singh873e91a2020-04-28 00:33:11 -0700121 StatsdStats::getInstance().notePullUidProviderNotFound(tagId);
Tej Singh3be093b2020-03-04 20:08:38 -0800122 return false;
123 }
124 uids = pullUidProvider->getPullAtomUids(tagId);
125 }
126 return PullLocked(tagId, uids, data, useUids);
127}
128
129bool StatsPullerManager::PullLocked(int tagId, const vector<int32_t>& uids,
130 vector<shared_ptr<LogEvent>>* data, bool useUids) {
131 VLOG("Initiating pulling %d", tagId);
132 if (useUids) {
133 for (int32_t uid : uids) {
134 PullerKey key = {.atomTag = tagId, .uid = uid};
135 auto pullerIt = kAllPullAtomInfo.find(key);
136 if (pullerIt != kAllPullAtomInfo.end()) {
137 bool ret = pullerIt->second->Pull(data);
138 VLOG("pulled %zu items", data->size());
139 if (!ret) {
140 StatsdStats::getInstance().notePullFailed(tagId);
141 }
142 return ret;
143 }
144 }
Tej Singh873e91a2020-04-28 00:33:11 -0700145 StatsdStats::getInstance().notePullerNotFound(tagId);
Tej Singh3be093b2020-03-04 20:08:38 -0800146 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
147 return false; // Return early since we don't know what to pull.
Yao Chen93fe3a32017-11-02 13:52:59 -0700148 } else {
Tej Singh3be093b2020-03-04 20:08:38 -0800149 PullerKey key = {.atomTag = tagId, .uid = -1};
150 auto pullerIt = kAllPullAtomInfo.find(key);
151 if (pullerIt != kAllPullAtomInfo.end()) {
152 bool ret = pullerIt->second->Pull(data);
153 VLOG("pulled %zu items", data->size());
154 if (!ret) {
155 StatsdStats::getInstance().notePullFailed(tagId);
156 }
157 return ret;
158 }
Tej Singh730ed292020-02-03 17:24:27 -0800159 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700160 return false; // Return early since we don't know what to pull.
161 }
162}
Chenjie Yub3dda412017-10-24 13:41:59 -0700163
Chenjie Yue2219202018-06-08 10:07:51 -0700164bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -0800165 // Pulled atoms might be registered after we parse the config, so just make sure the id is in
166 // an appropriate range.
167 return isVendorPulledAtom(tagId) || isPulledAtom(tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700168}
169
Chenjie Yue2219202018-06-08 10:07:51 -0700170void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700171 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700172 VLOG("No need to set alarms. Skipping");
173 return;
174 }
175
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800176 // TODO(b/151045771): do not hold a lock while making a binder call
177 if (mStatsCompanionService != nullptr) {
178 mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700179 } else {
180 VLOG("StatsCompanionService not available. Alarm not set.");
181 }
182 return;
183}
184
Chenjie Yue2219202018-06-08 10:07:51 -0700185void StatsPullerManager::SetStatsCompanionService(
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800186 shared_ptr<IStatsCompanionService> statsCompanionService) {
Tej Singh3be093b2020-03-04 20:08:38 -0800187 std::lock_guard<std::mutex> _l(mLock);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800188 shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700189 mStatsCompanionService = statsCompanionService;
190 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800191 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700192 }
193 if (mStatsCompanionService != nullptr) {
194 updateAlarmLocked();
195 }
196}
197
Tej Singh3be093b2020-03-04 20:08:38 -0800198void StatsPullerManager::RegisterReceiver(int tagId, const ConfigKey& configKey,
199 wp<PullDataReceiver> receiver, int64_t nextPullTimeNs,
200 int64_t intervalNs) {
201 std::lock_guard<std::mutex> _l(mLock);
202 auto& receivers = mReceivers[{.atomTag = tagId, .configKey = configKey}];
Chenjie Yub3dda412017-10-24 13:41:59 -0700203 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800204 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700205 VLOG("Receiver already registered of %d", (int)receivers.size());
206 return;
207 }
208 }
209 ReceiverInfo receiverInfo;
210 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700211
Chenjie Yu85ed8382017-12-14 16:48:54 -0800212 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700213 // In practice, we should always have larger buckets.
214 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700215 // Scheduled pulling should be at least 1 min apart.
216 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700217 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
218 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700219 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700220
221 receiverInfo.intervalNs = roundedIntervalNs;
222 receiverInfo.nextPullTimeNs = nextPullTimeNs;
223 receivers.push_back(receiverInfo);
224
Chenjie Yub3dda412017-10-24 13:41:59 -0700225 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700226 if (nextPullTimeNs < mNextPullTimeNs) {
227 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
228 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700229 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700230 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700231 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700232}
233
Tej Singh3be093b2020-03-04 20:08:38 -0800234void StatsPullerManager::UnRegisterReceiver(int tagId, const ConfigKey& configKey,
235 wp<PullDataReceiver> receiver) {
236 std::lock_guard<std::mutex> _l(mLock);
237 auto receiversIt = mReceivers.find({.atomTag = tagId, .configKey = configKey});
238 if (receiversIt == mReceivers.end()) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700239 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700240 return;
241 }
Tej Singh3be093b2020-03-04 20:08:38 -0800242 std::list<ReceiverInfo>& receivers = receiversIt->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700243 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800244 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700245 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700246 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700247 return;
248 }
249 }
250}
251
Tej Singh3be093b2020-03-04 20:08:38 -0800252void StatsPullerManager::RegisterPullUidProvider(const ConfigKey& configKey,
253 wp<PullUidProvider> provider) {
254 std::lock_guard<std::mutex> _l(mLock);
255 mPullUidProviders[configKey] = provider;
256}
257
Tej Singh3eb9ced2020-04-20 22:04:38 -0700258void StatsPullerManager::UnregisterPullUidProvider(const ConfigKey& configKey,
259 wp<PullUidProvider> provider) {
Tej Singh3be093b2020-03-04 20:08:38 -0800260 std::lock_guard<std::mutex> _l(mLock);
Tej Singh3eb9ced2020-04-20 22:04:38 -0700261 const auto& it = mPullUidProviders.find(configKey);
262 if (it != mPullUidProviders.end() && it->second == provider) {
263 mPullUidProviders.erase(it);
264 }
Tej Singh3be093b2020-03-04 20:08:38 -0800265}
266
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800267void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Tej Singh3be093b2020-03-04 20:08:38 -0800268 std::lock_guard<std::mutex> _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800269 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700270
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700271 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700272
Tej Singh3be093b2020-03-04 20:08:38 -0800273 vector<pair<const ReceiverKey*, vector<ReceiverInfo*>>> needToPull;
Chenjie Yub3dda412017-10-24 13:41:59 -0700274 for (auto& pair : mReceivers) {
Tej Singh3be093b2020-03-04 20:08:38 -0800275 vector<ReceiverInfo*> receivers;
Yao Chen93fe3a32017-11-02 13:52:59 -0700276 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700277 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800278 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700279 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700280 } else {
281 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
282 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
283 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700284 }
285 }
286 if (receivers.size() > 0) {
Tej Singh3be093b2020-03-04 20:08:38 -0800287 needToPull.push_back(make_pair(&pair.first, receivers));
Chenjie Yub3dda412017-10-24 13:41:59 -0700288 }
289 }
290 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700291 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700292 vector<shared_ptr<LogEvent>> data;
Tej Singh3be093b2020-03-04 20:08:38 -0800293 bool pullSuccess = PullLocked(pullInfo.first->atomTag, pullInfo.first->configKey, &data);
Tej Singh873e91a2020-04-28 00:33:11 -0700294 if (!pullSuccess) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800295 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800296 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800297
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 Gaillard11203df2019-02-06 13:18:09 +0000313 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000314 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800315 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 Yu6736c892017-11-09 10:50:09 -0800320 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800321 } else {
322 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700323 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700324 }
325 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700326
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700327 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
328 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700329 mNextPullTimeNs = minNextPullTimeNs;
330 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700331}
332
Chenjie Yue2219202018-06-08 10:07:51 -0700333int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800334 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800335 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800336 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800337 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800338 return totalCleared;
339}
340
Chenjie Yue2219202018-06-08 10:07:51 -0700341int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800342 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800343 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800344 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800345 }
346 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800347}
348
Tej Singh6a5c9432019-10-11 11:07:06 -0700349void 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 Singh3be093b2020-03-04 20:08:38 -0800352 const shared_ptr<IPullAtomCallback>& callback,
353 bool useUid) {
354 std::lock_guard<std::mutex> _l(mLock);
Tej Singh6a5c9432019-10-11 11:07:06 -0700355 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
Tej Singhdd25c822020-04-01 14:59:36 -0700356
Tej Singhbaed5252020-05-01 17:30:00 -0700357 if (callback == nullptr) {
358 ALOGW("SetPullAtomCallback called with null callback for atom %d.", atomTag);
359 return;
360 }
361
Tej Singh6a5c9432019-10-11 11:07:06 -0700362 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singhabc1b8de2020-03-30 17:19:36 -0700363 int64_t actualCoolDownNs = coolDownNs < kMinCoolDownNs ? kMinCoolDownNs : coolDownNs;
364 int64_t actualTimeoutNs = timeoutNs > kMaxTimeoutNs ? kMaxTimeoutNs : timeoutNs;
Tej Singhdd25c822020-04-01 14:59:36 -0700365
366 sp<StatsCallbackPuller> puller = new StatsCallbackPuller(atomTag, callback, actualCoolDownNs,
367 actualTimeoutNs, additiveFields);
368 PullerKey key = {.atomTag = atomTag, .uid = useUid ? uid : -1};
369 AIBinder_linkToDeath(callback->asBinder().get(), mPullAtomCallbackDeathRecipient.get(),
370 new PullAtomCallbackDeathCookie(this, key, puller));
371 kAllPullAtomInfo[key] = puller;
Tej Singha0c89dd2019-01-25 16:39:18 -0800372}
373
Tej Singhdd25c822020-04-01 14:59:36 -0700374void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag,
375 bool useUids) {
Tej Singh3be093b2020-03-04 20:08:38 -0800376 std::lock_guard<std::mutex> _l(mLock);
Tej Singhdd25c822020-04-01 14:59:36 -0700377 PullerKey key = {.atomTag = atomTag, .uid = useUids ? uid : -1};
378 if (kAllPullAtomInfo.find(key) != kAllPullAtomInfo.end()) {
379 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag,
380 /*registered=*/false);
381 kAllPullAtomInfo.erase(key);
382 }
Tej Singhfa1c1372019-12-05 20:36:54 -0800383}
384
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700385} // namespace statsd
386} // namespace os
387} // namespace android