blob: cfd5d14b0d3bef16e1e0f94f46fe044486c5d276 [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());
114 return false;
Misha Wagner1eee2212019-01-22 11:47:11 +0000115 }
Tej Singh3be093b2020-03-04 20:08:38 -0800116 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
127bool 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 Chen93fe3a32017-11-02 13:52:59 -0700145 } else {
Tej Singh3be093b2020-03-04 20:08:38 -0800146 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 Singh730ed292020-02-03 17:24:27 -0800156 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700157 return false; // Return early since we don't know what to pull.
158 }
159}
Chenjie Yub3dda412017-10-24 13:41:59 -0700160
Chenjie Yue2219202018-06-08 10:07:51 -0700161bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -0800162 // 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 Yub3dda412017-10-24 13:41:59 -0700165}
166
Chenjie Yue2219202018-06-08 10:07:51 -0700167void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700168 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700169 VLOG("No need to set alarms. Skipping");
170 return;
171 }
172
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800173 // TODO(b/151045771): do not hold a lock while making a binder call
174 if (mStatsCompanionService != nullptr) {
175 mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700176 } else {
177 VLOG("StatsCompanionService not available. Alarm not set.");
178 }
179 return;
180}
181
Chenjie Yue2219202018-06-08 10:07:51 -0700182void StatsPullerManager::SetStatsCompanionService(
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800183 shared_ptr<IStatsCompanionService> statsCompanionService) {
Tej Singh3be093b2020-03-04 20:08:38 -0800184 std::lock_guard<std::mutex> _l(mLock);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800185 shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700186 mStatsCompanionService = statsCompanionService;
187 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800188 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700189 }
190 if (mStatsCompanionService != nullptr) {
191 updateAlarmLocked();
192 }
193}
194
Tej Singh3be093b2020-03-04 20:08:38 -0800195void 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 Yub3dda412017-10-24 13:41:59 -0700200 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800201 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700202 VLOG("Receiver already registered of %d", (int)receivers.size());
203 return;
204 }
205 }
206 ReceiverInfo receiverInfo;
207 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700208
Chenjie Yu85ed8382017-12-14 16:48:54 -0800209 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700210 // In practice, we should always have larger buckets.
211 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700212 // 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 Yu1a0a9412018-03-28 10:07:22 -0700214 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
215 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700216 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700217
218 receiverInfo.intervalNs = roundedIntervalNs;
219 receiverInfo.nextPullTimeNs = nextPullTimeNs;
220 receivers.push_back(receiverInfo);
221
Chenjie Yub3dda412017-10-24 13:41:59 -0700222 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700223 if (nextPullTimeNs < mNextPullTimeNs) {
224 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
225 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700226 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700227 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700228 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700229}
230
Tej Singh3be093b2020-03-04 20:08:38 -0800231void 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 Yu5305e1d2017-10-31 13:49:36 -0700236 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700237 return;
238 }
Tej Singh3be093b2020-03-04 20:08:38 -0800239 std::list<ReceiverInfo>& receivers = receiversIt->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700240 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800241 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700242 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700243 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700244 return;
245 }
246 }
247}
248
Tej Singh3be093b2020-03-04 20:08:38 -0800249void StatsPullerManager::RegisterPullUidProvider(const ConfigKey& configKey,
250 wp<PullUidProvider> provider) {
251 std::lock_guard<std::mutex> _l(mLock);
252 mPullUidProviders[configKey] = provider;
253}
254
Tej Singh3eb9ced2020-04-20 22:04:38 -0700255void StatsPullerManager::UnregisterPullUidProvider(const ConfigKey& configKey,
256 wp<PullUidProvider> provider) {
Tej Singh3be093b2020-03-04 20:08:38 -0800257 std::lock_guard<std::mutex> _l(mLock);
Tej Singh3eb9ced2020-04-20 22:04:38 -0700258 const auto& it = mPullUidProviders.find(configKey);
259 if (it != mPullUidProviders.end() && it->second == provider) {
260 mPullUidProviders.erase(it);
261 }
Tej Singh3be093b2020-03-04 20:08:38 -0800262}
263
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800264void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Tej Singh3be093b2020-03-04 20:08:38 -0800265 std::lock_guard<std::mutex> _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800266 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700267
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700268 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700269
Tej Singh3be093b2020-03-04 20:08:38 -0800270 vector<pair<const ReceiverKey*, vector<ReceiverInfo*>>> needToPull;
Chenjie Yub3dda412017-10-24 13:41:59 -0700271 for (auto& pair : mReceivers) {
Tej Singh3be093b2020-03-04 20:08:38 -0800272 vector<ReceiverInfo*> receivers;
Yao Chen93fe3a32017-11-02 13:52:59 -0700273 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700274 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800275 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700276 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700277 } else {
278 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
279 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
280 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700281 }
282 }
283 if (receivers.size() > 0) {
Tej Singh3be093b2020-03-04 20:08:38 -0800284 needToPull.push_back(make_pair(&pair.first, receivers));
Chenjie Yub3dda412017-10-24 13:41:59 -0700285 }
286 }
287 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700288 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700289 vector<shared_ptr<LogEvent>> data;
Tej Singh3be093b2020-03-04 20:08:38 -0800290 bool pullSuccess = PullLocked(pullInfo.first->atomTag, pullInfo.first->configKey, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000291 if (pullSuccess) {
Tej Singh3be093b2020-03-04 20:08:38 -0800292 StatsdStats::getInstance().notePullDelay(pullInfo.first->atomTag,
293 getElapsedRealtimeNs() - elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000294 } else {
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 Singh6a5c9432019-10-11 11:07:06 -0700357 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singhabc1b8de2020-03-30 17:19:36 -0700358 int64_t actualCoolDownNs = coolDownNs < kMinCoolDownNs ? kMinCoolDownNs : coolDownNs;
359 int64_t actualTimeoutNs = timeoutNs > kMaxTimeoutNs ? kMaxTimeoutNs : timeoutNs;
Tej Singhdd25c822020-04-01 14:59:36 -0700360
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 Singha0c89dd2019-01-25 16:39:18 -0800367}
368
Tej Singhdd25c822020-04-01 14:59:36 -0700369void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag,
370 bool useUids) {
Tej Singh3be093b2020-03-04 20:08:38 -0800371 std::lock_guard<std::mutex> _l(mLock);
Tej Singhdd25c822020-04-01 14:59:36 -0700372 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 Singhfa1c1372019-12-05 20:36:54 -0800378}
379
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700380} // namespace statsd
381} // namespace os
382} // namespace android