blob: 8b6a5a17bc0eaa4c87dda753c95d182f1c86aba3 [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.
Jeffrey Huang74fc4352020-03-06 15:18:33 -080050 {{.atomTag = util::TRAIN_INFO}, new TrainInfoPuller()},
Tej Singh5b4951b2020-01-24 13:23:56 -080051 }),
52 mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070053}
54
Chenjie Yu0bd73db2018-12-16 07:37:04 -080055bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singhfa1c1372019-12-05 20:36:54 -080056 AutoMutex _l(mLock);
57 return PullLocked(tagId, data);
58}
59
60bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -080061 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070062
Tej Singh6a5c9432019-10-11 11:07:06 -070063 if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) {
Tej Singh5b4951b2020-01-24 13:23:56 -080064 bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -080065 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +000066 if (!ret) {
67 StatsdStats::getInstance().notePullFailed(tagId);
68 }
Tej Singh484524a2018-02-01 15:10:05 -080069 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -070070 } else {
Tej Singh730ed292020-02-03 17:24:27 -080071 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -070072 return false; // Return early since we don't know what to pull.
73 }
74}
Chenjie Yub3dda412017-10-24 13:41:59 -070075
Chenjie Yue2219202018-06-08 10:07:51 -070076bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -080077 // Pulled atoms might be registered after we parse the config, so just make sure the id is in
78 // an appropriate range.
79 return isVendorPulledAtom(tagId) || isPulledAtom(tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070080}
81
Chenjie Yue2219202018-06-08 10:07:51 -070082void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070083 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -070084 VLOG("No need to set alarms. Skipping");
85 return;
86 }
87
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080088 // TODO(b/151045771): do not hold a lock while making a binder call
89 if (mStatsCompanionService != nullptr) {
90 mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -070091 } else {
92 VLOG("StatsCompanionService not available. Alarm not set.");
93 }
94 return;
95}
96
Chenjie Yue2219202018-06-08 10:07:51 -070097void StatsPullerManager::SetStatsCompanionService(
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080098 shared_ptr<IStatsCompanionService> statsCompanionService) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -070099 AutoMutex _l(mLock);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800100 shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700101 mStatsCompanionService = statsCompanionService;
102 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800103 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700104 }
105 if (mStatsCompanionService != nullptr) {
106 updateAlarmLocked();
107 }
108}
109
Chenjie Yue2219202018-06-08 10:07:51 -0700110void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700111 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700112 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800113 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700114 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800115 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700116 VLOG("Receiver already registered of %d", (int)receivers.size());
117 return;
118 }
119 }
120 ReceiverInfo receiverInfo;
121 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700122
Chenjie Yu85ed8382017-12-14 16:48:54 -0800123 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700124 // In practice, we should always have larger buckets.
125 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700126 // Scheduled pulling should be at least 1 min apart.
127 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700128 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
129 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700130 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700131
132 receiverInfo.intervalNs = roundedIntervalNs;
133 receiverInfo.nextPullTimeNs = nextPullTimeNs;
134 receivers.push_back(receiverInfo);
135
Chenjie Yub3dda412017-10-24 13:41:59 -0700136 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700137 if (nextPullTimeNs < mNextPullTimeNs) {
138 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
139 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700140 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700141 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700142 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700143}
144
Chenjie Yue2219202018-06-08 10:07:51 -0700145void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700146 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700147 if (mReceivers.find(tagId) == mReceivers.end()) {
148 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700149 return;
150 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700151 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700152 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800153 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700154 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700155 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700156 return;
157 }
158 }
159}
160
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800161void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700162 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800163 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700164
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700165 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700166
167 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
168 vector<pair<int, vector<ReceiverInfo*>>>();
169 for (auto& pair : mReceivers) {
170 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700171 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700172 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800173 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700174 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700175 } else {
176 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
177 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
178 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700179 }
180 }
181 if (receivers.size() > 0) {
182 needToPull.push_back(make_pair(pair.first, receivers));
183 }
184 }
185 }
186
187 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700188 vector<shared_ptr<LogEvent>> data;
Tej Singhfa1c1372019-12-05 20:36:54 -0800189 bool pullSuccess = PullLocked(pullInfo.first, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000190 if (pullSuccess) {
191 StatsdStats::getInstance().notePullDelay(
192 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
193 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800194 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800195 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800196
197 // Convention is to mark pull atom timestamp at request time.
198 // If we pull at t0, puller starts at t1, finishes at t2, and send back
199 // at t3, we mark t0 as its timestamp, which should correspond to its
200 // triggering event, such as condition change at t0.
201 // Here the triggering event is alarm fired from AlarmManager.
202 // In ValueMetricProducer and GaugeMetricProducer we do same thing
203 // when pull on condition change, etc.
204 for (auto& event : data) {
205 event->setElapsedTimestampNs(elapsedTimeNs);
206 event->setLogdWallClockTimestampNs(wallClockNs);
207 }
208
209 for (const auto& receiverInfo : pullInfo.second) {
210 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
211 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000212 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000213 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800214 int numBucketsAhead =
215 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
216 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
217 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
218 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800219 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800220 } else {
221 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700222 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700223 }
224 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700225
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700226 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
227 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700228 mNextPullTimeNs = minNextPullTimeNs;
229 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700230}
231
Chenjie Yue2219202018-06-08 10:07:51 -0700232int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800233 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800234 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800235 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800236 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800237 return totalCleared;
238}
239
Chenjie Yue2219202018-06-08 10:07:51 -0700240int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800241 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800242 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800243 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800244 }
245 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800246}
247
Tej Singh6a5c9432019-10-11 11:07:06 -0700248void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
249 const int64_t coolDownNs, const int64_t timeoutNs,
250 const vector<int32_t>& additiveFields,
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800251 const shared_ptr<IPullAtomCallback>& callback) {
Tej Singh6a5c9432019-10-11 11:07:06 -0700252 AutoMutex _l(mLock);
253 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800254 // TODO(b/146439412): linkToDeath with the callback so that we can remove it
255 // and delete the puller.
Tej Singh6a5c9432019-10-11 11:07:06 -0700256 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh5b4951b2020-01-24 13:23:56 -0800257 kAllPullAtomInfo[{.atomTag = atomTag}] =
258 new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields);
Tej Singha0c89dd2019-01-25 16:39:18 -0800259}
260
Tej Singhfa1c1372019-12-05 20:36:54 -0800261void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
262 AutoMutex _l(mLock);
263 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
264 kAllPullAtomInfo.erase({.atomTag = atomTag});
265}
266
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700267} // namespace statsd
268} // namespace os
269} // namespace android