blob: 1c38542b901776058acc92b9320f094567c8055a [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"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070035#include "statslog.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.
50 {{.atomTag = android::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 Rastogie449b0c2020-02-10 17:40:09 -080088 // TODO(b/149254662): Why are we creating a copy here? This is different
89 // from the other places where we create a copy because we don't reassign
90 // mStatsCompanionService so a destructor can't implicitly be called...
91 shared_ptr<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -070092 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -070093 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -070094 } else {
95 VLOG("StatsCompanionService not available. Alarm not set.");
96 }
97 return;
98}
99
Chenjie Yue2219202018-06-08 10:07:51 -0700100void StatsPullerManager::SetStatsCompanionService(
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800101 shared_ptr<IStatsCompanionService> statsCompanionService) {
102 // TODO(b/149254662): Why are we using AutoMutex instead of lock_guard?
103 // Additionally, do we need the temporary shared_ptr to prevent deadlocks?
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700104 AutoMutex _l(mLock);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800105 shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700106 mStatsCompanionService = statsCompanionService;
107 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800108 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700109 }
110 if (mStatsCompanionService != nullptr) {
111 updateAlarmLocked();
112 }
113}
114
Chenjie Yue2219202018-06-08 10:07:51 -0700115void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700116 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700117 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800118 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700119 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800120 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700121 VLOG("Receiver already registered of %d", (int)receivers.size());
122 return;
123 }
124 }
125 ReceiverInfo receiverInfo;
126 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700127
Chenjie Yu85ed8382017-12-14 16:48:54 -0800128 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700129 // In practice, we should always have larger buckets.
130 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700131 // Scheduled pulling should be at least 1 min apart.
132 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700133 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
134 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700135 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700136
137 receiverInfo.intervalNs = roundedIntervalNs;
138 receiverInfo.nextPullTimeNs = nextPullTimeNs;
139 receivers.push_back(receiverInfo);
140
Chenjie Yub3dda412017-10-24 13:41:59 -0700141 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700142 if (nextPullTimeNs < mNextPullTimeNs) {
143 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
144 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700145 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700146 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700147 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700148}
149
Chenjie Yue2219202018-06-08 10:07:51 -0700150void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700151 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700152 if (mReceivers.find(tagId) == mReceivers.end()) {
153 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700154 return;
155 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700156 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700157 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800158 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700159 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700160 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700161 return;
162 }
163 }
164}
165
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800166void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700167 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800168 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700169
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700170 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700171
172 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
173 vector<pair<int, vector<ReceiverInfo*>>>();
174 for (auto& pair : mReceivers) {
175 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700176 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700177 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800178 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700179 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700180 } else {
181 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
182 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
183 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700184 }
185 }
186 if (receivers.size() > 0) {
187 needToPull.push_back(make_pair(pair.first, receivers));
188 }
189 }
190 }
191
192 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700193 vector<shared_ptr<LogEvent>> data;
Tej Singhfa1c1372019-12-05 20:36:54 -0800194 bool pullSuccess = PullLocked(pullInfo.first, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000195 if (pullSuccess) {
196 StatsdStats::getInstance().notePullDelay(
197 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
198 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800199 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800200 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800201
202 // Convention is to mark pull atom timestamp at request time.
203 // If we pull at t0, puller starts at t1, finishes at t2, and send back
204 // at t3, we mark t0 as its timestamp, which should correspond to its
205 // triggering event, such as condition change at t0.
206 // Here the triggering event is alarm fired from AlarmManager.
207 // In ValueMetricProducer and GaugeMetricProducer we do same thing
208 // when pull on condition change, etc.
209 for (auto& event : data) {
210 event->setElapsedTimestampNs(elapsedTimeNs);
211 event->setLogdWallClockTimestampNs(wallClockNs);
212 }
213
214 for (const auto& receiverInfo : pullInfo.second) {
215 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
216 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000217 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000218 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800219 int numBucketsAhead =
220 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
221 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
222 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
223 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800224 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800225 } else {
226 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700227 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700228 }
229 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700230
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700231 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
232 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700233 mNextPullTimeNs = minNextPullTimeNs;
234 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700235}
236
Chenjie Yue2219202018-06-08 10:07:51 -0700237int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800238 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800239 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800240 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800241 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800242 return totalCleared;
243}
244
Chenjie Yue2219202018-06-08 10:07:51 -0700245int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800246 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800247 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800248 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800249 }
250 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800251}
252
Tej Singh6a5c9432019-10-11 11:07:06 -0700253void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
254 const int64_t coolDownNs, const int64_t timeoutNs,
255 const vector<int32_t>& additiveFields,
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800256 const shared_ptr<IPullAtomCallback>& callback) {
Tej Singh6a5c9432019-10-11 11:07:06 -0700257 AutoMutex _l(mLock);
258 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800259 // TODO(b/146439412): linkToDeath with the callback so that we can remove it
260 // and delete the puller.
Tej Singh6a5c9432019-10-11 11:07:06 -0700261 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh5b4951b2020-01-24 13:23:56 -0800262 kAllPullAtomInfo[{.atomTag = atomTag}] =
263 new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields);
Tej Singha0c89dd2019-01-25 16:39:18 -0800264}
265
Tej Singhfa1c1372019-12-05 20:36:54 -0800266void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
267 AutoMutex _l(mLock);
268 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
269 kAllPullAtomInfo.erase({.atomTag = atomTag});
270}
271
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700272} // namespace statsd
273} // namespace os
274} // namespace android