blob: 8a9ec7456e557f3b047769a88f01623dd6a653c1 [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 Singh7b975a82020-05-11 11:05:08 -070094bool StatsPullerManager::Pull(int tagId, const ConfigKey& configKey, const int64_t eventTimeNs,
Tej Singh3be093b2020-03-04 20:08:38 -080095 vector<shared_ptr<LogEvent>>* data, bool useUids) {
96 std::lock_guard<std::mutex> _l(mLock);
Tej Singh7b975a82020-05-11 11:05:08 -070097 return PullLocked(tagId, configKey, eventTimeNs, data, useUids);
Tej Singhfa1c1372019-12-05 20:36:54 -080098}
99
Tej Singh7b975a82020-05-11 11:05:08 -0700100bool StatsPullerManager::Pull(int tagId, const vector<int32_t>& uids, const int64_t eventTimeNs,
Tej Singh3be093b2020-03-04 20:08:38 -0800101 vector<std::shared_ptr<LogEvent>>* data, bool useUids) {
102 std::lock_guard<std::mutex> _l(mLock);
Tej Singh7b975a82020-05-11 11:05:08 -0700103 return PullLocked(tagId, uids, eventTimeNs, data, useUids);
Tej Singh3be093b2020-03-04 20:08:38 -0800104}
Chenjie Yub3dda412017-10-24 13:41:59 -0700105
Tej Singh3be093b2020-03-04 20:08:38 -0800106bool StatsPullerManager::PullLocked(int tagId, const ConfigKey& configKey,
Tej Singh7b975a82020-05-11 11:05:08 -0700107 const int64_t eventTimeNs, vector<shared_ptr<LogEvent>>* data,
108 bool useUids) {
Tej Singh3be093b2020-03-04 20:08:38 -0800109 vector<int32_t> uids;
110 if (useUids) {
111 auto uidProviderIt = mPullUidProviders.find(configKey);
112 if (uidProviderIt == mPullUidProviders.end()) {
113 ALOGE("Error pulling tag %d. No pull uid provider for config key %s", tagId,
114 configKey.ToString().c_str());
Tej Singh873e91a2020-04-28 00:33:11 -0700115 StatsdStats::getInstance().notePullUidProviderNotFound(tagId);
Tej Singh3be093b2020-03-04 20:08:38 -0800116 return false;
Misha Wagner1eee2212019-01-22 11:47:11 +0000117 }
Tej Singh3be093b2020-03-04 20:08:38 -0800118 sp<PullUidProvider> pullUidProvider = uidProviderIt->second.promote();
119 if (pullUidProvider == nullptr) {
120 ALOGE("Error pulling tag %d, pull uid provider for config %s is gone.", tagId,
121 configKey.ToString().c_str());
Tej Singh873e91a2020-04-28 00:33:11 -0700122 StatsdStats::getInstance().notePullUidProviderNotFound(tagId);
Tej Singh3be093b2020-03-04 20:08:38 -0800123 return false;
124 }
125 uids = pullUidProvider->getPullAtomUids(tagId);
126 }
Tej Singh7b975a82020-05-11 11:05:08 -0700127 return PullLocked(tagId, uids, eventTimeNs, data, useUids);
Tej Singh3be093b2020-03-04 20:08:38 -0800128}
129
130bool StatsPullerManager::PullLocked(int tagId, const vector<int32_t>& uids,
Tej Singh7b975a82020-05-11 11:05:08 -0700131 const int64_t eventTimeNs, vector<shared_ptr<LogEvent>>* data,
132 bool useUids) {
Tej Singh3be093b2020-03-04 20:08:38 -0800133 VLOG("Initiating pulling %d", tagId);
134 if (useUids) {
135 for (int32_t uid : uids) {
136 PullerKey key = {.atomTag = tagId, .uid = uid};
137 auto pullerIt = kAllPullAtomInfo.find(key);
138 if (pullerIt != kAllPullAtomInfo.end()) {
Tej Singh7b975a82020-05-11 11:05:08 -0700139 bool ret = pullerIt->second->Pull(eventTimeNs, data);
Tej Singh3be093b2020-03-04 20:08:38 -0800140 VLOG("pulled %zu items", data->size());
141 if (!ret) {
142 StatsdStats::getInstance().notePullFailed(tagId);
143 }
144 return ret;
145 }
146 }
Tej Singh873e91a2020-04-28 00:33:11 -0700147 StatsdStats::getInstance().notePullerNotFound(tagId);
Tej Singh3be093b2020-03-04 20:08:38 -0800148 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
149 return false; // Return early since we don't know what to pull.
Yao Chen93fe3a32017-11-02 13:52:59 -0700150 } else {
Tej Singh3be093b2020-03-04 20:08:38 -0800151 PullerKey key = {.atomTag = tagId, .uid = -1};
152 auto pullerIt = kAllPullAtomInfo.find(key);
153 if (pullerIt != kAllPullAtomInfo.end()) {
Tej Singh7b975a82020-05-11 11:05:08 -0700154 bool ret = pullerIt->second->Pull(eventTimeNs, data);
Tej Singh3be093b2020-03-04 20:08:38 -0800155 VLOG("pulled %zu items", data->size());
156 if (!ret) {
157 StatsdStats::getInstance().notePullFailed(tagId);
158 }
159 return ret;
160 }
Tej Singh730ed292020-02-03 17:24:27 -0800161 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700162 return false; // Return early since we don't know what to pull.
163 }
164}
Chenjie Yub3dda412017-10-24 13:41:59 -0700165
Chenjie Yue2219202018-06-08 10:07:51 -0700166bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -0800167 // Pulled atoms might be registered after we parse the config, so just make sure the id is in
168 // an appropriate range.
169 return isVendorPulledAtom(tagId) || isPulledAtom(tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700170}
171
Chenjie Yue2219202018-06-08 10:07:51 -0700172void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700173 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700174 VLOG("No need to set alarms. Skipping");
175 return;
176 }
177
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800178 // TODO(b/151045771): do not hold a lock while making a binder call
179 if (mStatsCompanionService != nullptr) {
180 mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700181 } else {
182 VLOG("StatsCompanionService not available. Alarm not set.");
183 }
184 return;
185}
186
Chenjie Yue2219202018-06-08 10:07:51 -0700187void StatsPullerManager::SetStatsCompanionService(
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800188 shared_ptr<IStatsCompanionService> statsCompanionService) {
Tej Singh3be093b2020-03-04 20:08:38 -0800189 std::lock_guard<std::mutex> _l(mLock);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800190 shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700191 mStatsCompanionService = statsCompanionService;
192 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800193 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700194 }
195 if (mStatsCompanionService != nullptr) {
196 updateAlarmLocked();
197 }
198}
199
Tej Singh3be093b2020-03-04 20:08:38 -0800200void StatsPullerManager::RegisterReceiver(int tagId, const ConfigKey& configKey,
201 wp<PullDataReceiver> receiver, int64_t nextPullTimeNs,
202 int64_t intervalNs) {
203 std::lock_guard<std::mutex> _l(mLock);
204 auto& receivers = mReceivers[{.atomTag = tagId, .configKey = configKey}];
Chenjie Yub3dda412017-10-24 13:41:59 -0700205 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800206 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700207 VLOG("Receiver already registered of %d", (int)receivers.size());
208 return;
209 }
210 }
211 ReceiverInfo receiverInfo;
212 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700213
Chenjie Yu85ed8382017-12-14 16:48:54 -0800214 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700215 // In practice, we should always have larger buckets.
216 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700217 // Scheduled pulling should be at least 1 min apart.
218 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700219 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
220 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700221 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700222
223 receiverInfo.intervalNs = roundedIntervalNs;
224 receiverInfo.nextPullTimeNs = nextPullTimeNs;
225 receivers.push_back(receiverInfo);
226
Chenjie Yub3dda412017-10-24 13:41:59 -0700227 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700228 if (nextPullTimeNs < mNextPullTimeNs) {
229 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
230 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700231 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700232 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700233 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700234}
235
Tej Singh3be093b2020-03-04 20:08:38 -0800236void StatsPullerManager::UnRegisterReceiver(int tagId, const ConfigKey& configKey,
237 wp<PullDataReceiver> receiver) {
238 std::lock_guard<std::mutex> _l(mLock);
239 auto receiversIt = mReceivers.find({.atomTag = tagId, .configKey = configKey});
240 if (receiversIt == mReceivers.end()) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700241 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700242 return;
243 }
Tej Singh3be093b2020-03-04 20:08:38 -0800244 std::list<ReceiverInfo>& receivers = receiversIt->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700245 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800246 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700247 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700248 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700249 return;
250 }
251 }
252}
253
Tej Singh3be093b2020-03-04 20:08:38 -0800254void StatsPullerManager::RegisterPullUidProvider(const ConfigKey& configKey,
255 wp<PullUidProvider> provider) {
256 std::lock_guard<std::mutex> _l(mLock);
257 mPullUidProviders[configKey] = provider;
258}
259
Tej Singh3eb9ced2020-04-20 22:04:38 -0700260void StatsPullerManager::UnregisterPullUidProvider(const ConfigKey& configKey,
261 wp<PullUidProvider> provider) {
Tej Singh3be093b2020-03-04 20:08:38 -0800262 std::lock_guard<std::mutex> _l(mLock);
Tej Singh3eb9ced2020-04-20 22:04:38 -0700263 const auto& it = mPullUidProviders.find(configKey);
264 if (it != mPullUidProviders.end() && it->second == provider) {
265 mPullUidProviders.erase(it);
266 }
Tej Singh3be093b2020-03-04 20:08:38 -0800267}
268
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800269void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Tej Singh3be093b2020-03-04 20:08:38 -0800270 std::lock_guard<std::mutex> _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800271 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700272
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700273 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700274
Tej Singh3be093b2020-03-04 20:08:38 -0800275 vector<pair<const ReceiverKey*, vector<ReceiverInfo*>>> needToPull;
Chenjie Yub3dda412017-10-24 13:41:59 -0700276 for (auto& pair : mReceivers) {
Tej Singh3be093b2020-03-04 20:08:38 -0800277 vector<ReceiverInfo*> receivers;
Yao Chen93fe3a32017-11-02 13:52:59 -0700278 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700279 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800280 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700281 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700282 } else {
283 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
284 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
285 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700286 }
287 }
288 if (receivers.size() > 0) {
Tej Singh3be093b2020-03-04 20:08:38 -0800289 needToPull.push_back(make_pair(&pair.first, receivers));
Chenjie Yub3dda412017-10-24 13:41:59 -0700290 }
291 }
292 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700293 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700294 vector<shared_ptr<LogEvent>> data;
Tej Singh7b975a82020-05-11 11:05:08 -0700295 bool pullSuccess = PullLocked(pullInfo.first->atomTag, pullInfo.first->configKey,
296 elapsedTimeNs, &data);
Tej Singh873e91a2020-04-28 00:33:11 -0700297 if (!pullSuccess) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800298 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800299 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800300
301 // Convention is to mark pull atom timestamp at request time.
302 // If we pull at t0, puller starts at t1, finishes at t2, and send back
303 // at t3, we mark t0 as its timestamp, which should correspond to its
304 // triggering event, such as condition change at t0.
305 // Here the triggering event is alarm fired from AlarmManager.
306 // In ValueMetricProducer and GaugeMetricProducer we do same thing
307 // when pull on condition change, etc.
308 for (auto& event : data) {
309 event->setElapsedTimestampNs(elapsedTimeNs);
310 event->setLogdWallClockTimestampNs(wallClockNs);
311 }
312
313 for (const auto& receiverInfo : pullInfo.second) {
314 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
315 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000316 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000317 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800318 int numBucketsAhead =
319 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
320 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
321 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
322 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800323 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800324 } else {
325 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700326 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700327 }
328 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700329
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700330 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
331 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700332 mNextPullTimeNs = minNextPullTimeNs;
333 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700334}
335
Chenjie Yue2219202018-06-08 10:07:51 -0700336int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800337 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800338 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800339 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800340 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800341 return totalCleared;
342}
343
Chenjie Yue2219202018-06-08 10:07:51 -0700344int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800345 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800346 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800347 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800348 }
349 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800350}
351
Tej Singh6a5c9432019-10-11 11:07:06 -0700352void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
353 const int64_t coolDownNs, const int64_t timeoutNs,
354 const vector<int32_t>& additiveFields,
Tej Singh3be093b2020-03-04 20:08:38 -0800355 const shared_ptr<IPullAtomCallback>& callback,
356 bool useUid) {
357 std::lock_guard<std::mutex> _l(mLock);
Tej Singh6a5c9432019-10-11 11:07:06 -0700358 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
Tej Singhdd25c822020-04-01 14:59:36 -0700359
Tej Singhbaed5252020-05-01 17:30:00 -0700360 if (callback == nullptr) {
361 ALOGW("SetPullAtomCallback called with null callback for atom %d.", atomTag);
362 return;
363 }
364
Tej Singh6a5c9432019-10-11 11:07:06 -0700365 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singhabc1b8de2020-03-30 17:19:36 -0700366 int64_t actualCoolDownNs = coolDownNs < kMinCoolDownNs ? kMinCoolDownNs : coolDownNs;
367 int64_t actualTimeoutNs = timeoutNs > kMaxTimeoutNs ? kMaxTimeoutNs : timeoutNs;
Tej Singhdd25c822020-04-01 14:59:36 -0700368
369 sp<StatsCallbackPuller> puller = new StatsCallbackPuller(atomTag, callback, actualCoolDownNs,
370 actualTimeoutNs, additiveFields);
371 PullerKey key = {.atomTag = atomTag, .uid = useUid ? uid : -1};
372 AIBinder_linkToDeath(callback->asBinder().get(), mPullAtomCallbackDeathRecipient.get(),
373 new PullAtomCallbackDeathCookie(this, key, puller));
374 kAllPullAtomInfo[key] = puller;
Tej Singha0c89dd2019-01-25 16:39:18 -0800375}
376
Tej Singhdd25c822020-04-01 14:59:36 -0700377void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag,
378 bool useUids) {
Tej Singh3be093b2020-03-04 20:08:38 -0800379 std::lock_guard<std::mutex> _l(mLock);
Tej Singhdd25c822020-04-01 14:59:36 -0700380 PullerKey key = {.atomTag = atomTag, .uid = useUids ? uid : -1};
381 if (kAllPullAtomInfo.find(key) != kAllPullAtomInfo.end()) {
382 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag,
383 /*registered=*/false);
384 kAllPullAtomInfo.erase(key);
385 }
Tej Singhfa1c1372019-12-05 20:36:54 -0800386}
387
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700388} // namespace statsd
389} // namespace os
390} // namespace android