blob: a3701a77f27a14f20e28e17137cef6588f39d498 [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
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070044// Values smaller than this may require to update the alarm.
45const int64_t NO_ALARM_UPDATE = INT64_MAX;
46
Tej Singh5b4951b2020-01-24 13:23:56 -080047StatsPullerManager::StatsPullerManager()
48 : kAllPullAtomInfo({
Tej Singh5b4951b2020-01-24 13:23:56 -080049 // TrainInfo.
Tej Singh3be093b2020-03-04 20:08:38 -080050 {{.atomTag = util::TRAIN_INFO, .uid = -1}, new TrainInfoPuller()},
Tej Singh5b4951b2020-01-24 13:23:56 -080051 }),
52 mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070053}
54
Tej Singh3be093b2020-03-04 20:08:38 -080055bool 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 Singhfa1c1372019-12-05 20:36:54 -080059}
60
Tej Singh3be093b2020-03-04 20:08:38 -080061bool 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 Yub3dda412017-10-24 13:41:59 -070066
Tej Singh3be093b2020-03-04 20:08:38 -080067bool 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 Wagner1eee2212019-01-22 11:47:11 +000076 }
Tej Singh3be093b2020-03-04 20:08:38 -080077 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
88bool 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 Chen93fe3a32017-11-02 13:52:59 -0700106 } else {
Tej Singh3be093b2020-03-04 20:08:38 -0800107 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 Singh730ed292020-02-03 17:24:27 -0800117 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700118 return false; // Return early since we don't know what to pull.
119 }
120}
Chenjie Yub3dda412017-10-24 13:41:59 -0700121
Chenjie Yue2219202018-06-08 10:07:51 -0700122bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -0800123 // 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 Yub3dda412017-10-24 13:41:59 -0700126}
127
Chenjie Yue2219202018-06-08 10:07:51 -0700128void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700129 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700130 VLOG("No need to set alarms. Skipping");
131 return;
132 }
133
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800134 // TODO(b/151045771): do not hold a lock while making a binder call
135 if (mStatsCompanionService != nullptr) {
136 mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700137 } else {
138 VLOG("StatsCompanionService not available. Alarm not set.");
139 }
140 return;
141}
142
Chenjie Yue2219202018-06-08 10:07:51 -0700143void StatsPullerManager::SetStatsCompanionService(
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800144 shared_ptr<IStatsCompanionService> statsCompanionService) {
Tej Singh3be093b2020-03-04 20:08:38 -0800145 std::lock_guard<std::mutex> _l(mLock);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800146 shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700147 mStatsCompanionService = statsCompanionService;
148 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800149 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700150 }
151 if (mStatsCompanionService != nullptr) {
152 updateAlarmLocked();
153 }
154}
155
Tej Singh3be093b2020-03-04 20:08:38 -0800156void 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 Yub3dda412017-10-24 13:41:59 -0700161 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800162 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700163 VLOG("Receiver already registered of %d", (int)receivers.size());
164 return;
165 }
166 }
167 ReceiverInfo receiverInfo;
168 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700169
Chenjie Yu85ed8382017-12-14 16:48:54 -0800170 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700171 // In practice, we should always have larger buckets.
172 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700173 // 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 Yu1a0a9412018-03-28 10:07:22 -0700175 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
176 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700177 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700178
179 receiverInfo.intervalNs = roundedIntervalNs;
180 receiverInfo.nextPullTimeNs = nextPullTimeNs;
181 receivers.push_back(receiverInfo);
182
Chenjie Yub3dda412017-10-24 13:41:59 -0700183 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700184 if (nextPullTimeNs < mNextPullTimeNs) {
185 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
186 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700187 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700188 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700189 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700190}
191
Tej Singh3be093b2020-03-04 20:08:38 -0800192void 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 Yu5305e1d2017-10-31 13:49:36 -0700197 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700198 return;
199 }
Tej Singh3be093b2020-03-04 20:08:38 -0800200 std::list<ReceiverInfo>& receivers = receiversIt->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700201 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800202 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700203 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700204 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700205 return;
206 }
207 }
208}
209
Tej Singh3be093b2020-03-04 20:08:38 -0800210void StatsPullerManager::RegisterPullUidProvider(const ConfigKey& configKey,
211 wp<PullUidProvider> provider) {
212 std::lock_guard<std::mutex> _l(mLock);
213 mPullUidProviders[configKey] = provider;
214}
215
216void StatsPullerManager::UnregisterPullUidProvider(const ConfigKey& configKey) {
217 std::lock_guard<std::mutex> _l(mLock);
218 mPullUidProviders.erase(configKey);
219}
220
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800221void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Tej Singh3be093b2020-03-04 20:08:38 -0800222 std::lock_guard<std::mutex> _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800223 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700224
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700225 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700226
Tej Singh3be093b2020-03-04 20:08:38 -0800227 vector<pair<const ReceiverKey*, vector<ReceiverInfo*>>> needToPull;
Chenjie Yub3dda412017-10-24 13:41:59 -0700228 for (auto& pair : mReceivers) {
Tej Singh3be093b2020-03-04 20:08:38 -0800229 vector<ReceiverInfo*> receivers;
Yao Chen93fe3a32017-11-02 13:52:59 -0700230 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700231 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800232 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700233 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700234 } else {
235 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
236 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
237 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700238 }
239 }
240 if (receivers.size() > 0) {
Tej Singh3be093b2020-03-04 20:08:38 -0800241 needToPull.push_back(make_pair(&pair.first, receivers));
Chenjie Yub3dda412017-10-24 13:41:59 -0700242 }
243 }
244 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700245 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700246 vector<shared_ptr<LogEvent>> data;
Tej Singh3be093b2020-03-04 20:08:38 -0800247 bool pullSuccess = PullLocked(pullInfo.first->atomTag, pullInfo.first->configKey, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000248 if (pullSuccess) {
Tej Singh3be093b2020-03-04 20:08:38 -0800249 StatsdStats::getInstance().notePullDelay(pullInfo.first->atomTag,
250 getElapsedRealtimeNs() - elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000251 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800252 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800253 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800254
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 Gaillard11203df2019-02-06 13:18:09 +0000270 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000271 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800272 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 Yu6736c892017-11-09 10:50:09 -0800277 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800278 } else {
279 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700280 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700281 }
282 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700283
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700284 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
285 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700286 mNextPullTimeNs = minNextPullTimeNs;
287 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700288}
289
Chenjie Yue2219202018-06-08 10:07:51 -0700290int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800291 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800292 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800293 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800294 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800295 return totalCleared;
296}
297
Chenjie Yue2219202018-06-08 10:07:51 -0700298int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800299 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800300 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800301 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800302 }
303 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800304}
305
Tej Singh6a5c9432019-10-11 11:07:06 -0700306void 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 Singh3be093b2020-03-04 20:08:38 -0800309 const shared_ptr<IPullAtomCallback>& callback,
310 bool useUid) {
311 std::lock_guard<std::mutex> _l(mLock);
Tej Singh6a5c9432019-10-11 11:07:06 -0700312 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800313 // TODO(b/146439412): linkToDeath with the callback so that we can remove it
314 // and delete the puller.
Tej Singh6a5c9432019-10-11 11:07:06 -0700315 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singhabc1b8de2020-03-30 17:19:36 -0700316 int64_t actualCoolDownNs = coolDownNs < kMinCoolDownNs ? kMinCoolDownNs : coolDownNs;
317 int64_t actualTimeoutNs = timeoutNs > kMaxTimeoutNs ? kMaxTimeoutNs : timeoutNs;
318 kAllPullAtomInfo[{.atomTag = atomTag, .uid = useUid ? uid : -1}] = new StatsCallbackPuller(
319 atomTag, callback, actualCoolDownNs, actualTimeoutNs, additiveFields);
Tej Singha0c89dd2019-01-25 16:39:18 -0800320}
321
Tej Singhfa1c1372019-12-05 20:36:54 -0800322void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
Tej Singh3be093b2020-03-04 20:08:38 -0800323 std::lock_guard<std::mutex> _l(mLock);
Tej Singhfa1c1372019-12-05 20:36:54 -0800324 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
325 kAllPullAtomInfo.erase({.atomTag = atomTag});
326}
327
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700328} // namespace statsd
329} // namespace os
330} // namespace android