blob: fef213dad60e9291d25a45a490f4322cd080298b [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
Tej Singh6a5c9432019-10-11 11:07:06 -070022#include <android/os/IPullAtomCallback.h>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070023#include <android/os/IStatsCompanionService.h>
David Chen1481fe12017-10-16 13:16:34 -070024#include <cutils/log.h>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070025#include <math.h>
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070026#include <stdint.h>
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070027
David Chen1481fe12017-10-16 13:16:34 -070028#include <algorithm>
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070029#include <iostream>
30
Chenjie Yu1a0a9412018-03-28 10:07:22 -070031#include "../StatsService.h"
Chenjie Yuaa5b2012018-03-21 13:53:15 -070032#include "../logd/LogEvent.h"
33#include "../stats_log_util.h"
34#include "../statscompanion_util.h"
Yiwei Zhang7e633032019-03-01 17:25:27 -080035#include "GpuStatsPuller.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080036#include "StatsCallbackPuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080037#include "TrainInfoPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070038#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070039
Yao Chen93fe3a32017-11-02 13:52:59 -070040using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070041using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070042using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070043using std::string;
44using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080045using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070046
47namespace android {
48namespace os {
49namespace statsd {
50
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070051// Values smaller than this may require to update the alarm.
52const int64_t NO_ALARM_UPDATE = INT64_MAX;
53
Tej Singh5b4951b2020-01-24 13:23:56 -080054StatsPullerManager::StatsPullerManager()
55 : kAllPullAtomInfo({
Tej Singh5b4951b2020-01-24 13:23:56 -080056 // TrainInfo.
57 {{.atomTag = android::util::TRAIN_INFO}, new TrainInfoPuller()},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080058
Tej Singh5b4951b2020-01-24 13:23:56 -080059 // GpuStatsGlobalInfo
60 {{.atomTag = android::util::GPU_STATS_GLOBAL_INFO},
61 new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080062
Tej Singh5b4951b2020-01-24 13:23:56 -080063 // GpuStatsAppInfo
64 {{.atomTag = android::util::GPU_STATS_APP_INFO},
65 new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080066
Tej Singh5b4951b2020-01-24 13:23:56 -080067 }),
68 mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070069}
70
Chenjie Yu0bd73db2018-12-16 07:37:04 -080071bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singhfa1c1372019-12-05 20:36:54 -080072 AutoMutex _l(mLock);
73 return PullLocked(tagId, data);
74}
75
76bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -080077 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070078
Tej Singh6a5c9432019-10-11 11:07:06 -070079 if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) {
Tej Singh5b4951b2020-01-24 13:23:56 -080080 bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -080081 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +000082 if (!ret) {
83 StatsdStats::getInstance().notePullFailed(tagId);
84 }
Tej Singh484524a2018-02-01 15:10:05 -080085 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -070086 } else {
Tej Singh484524a2018-02-01 15:10:05 -080087 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -070088 return false; // Return early since we don't know what to pull.
89 }
90}
Chenjie Yub3dda412017-10-24 13:41:59 -070091
Chenjie Yue2219202018-06-08 10:07:51 -070092bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -080093 // Pulled atoms might be registered after we parse the config, so just make sure the id is in
94 // an appropriate range.
95 return isVendorPulledAtom(tagId) || isPulledAtom(tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070096}
97
Chenjie Yue2219202018-06-08 10:07:51 -070098void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070099 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700100 VLOG("No need to set alarms. Skipping");
101 return;
102 }
103
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700104 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
105 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700106 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700107 } else {
108 VLOG("StatsCompanionService not available. Alarm not set.");
109 }
110 return;
111}
112
Chenjie Yue2219202018-06-08 10:07:51 -0700113void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700114 sp<IStatsCompanionService> statsCompanionService) {
115 AutoMutex _l(mLock);
116 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
117 mStatsCompanionService = statsCompanionService;
118 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800119 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700120 }
121 if (mStatsCompanionService != nullptr) {
122 updateAlarmLocked();
123 }
124}
125
Chenjie Yue2219202018-06-08 10:07:51 -0700126void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700127 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700128 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800129 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700130 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800131 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700132 VLOG("Receiver already registered of %d", (int)receivers.size());
133 return;
134 }
135 }
136 ReceiverInfo receiverInfo;
137 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700138
Chenjie Yu85ed8382017-12-14 16:48:54 -0800139 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700140 // In practice, we should always have larger buckets.
141 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700142 // Scheduled pulling should be at least 1 min apart.
143 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700144 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
145 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700146 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700147
148 receiverInfo.intervalNs = roundedIntervalNs;
149 receiverInfo.nextPullTimeNs = nextPullTimeNs;
150 receivers.push_back(receiverInfo);
151
Chenjie Yub3dda412017-10-24 13:41:59 -0700152 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700153 if (nextPullTimeNs < mNextPullTimeNs) {
154 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
155 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700156 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700157 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700158 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700159}
160
Chenjie Yue2219202018-06-08 10:07:51 -0700161void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700162 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700163 if (mReceivers.find(tagId) == mReceivers.end()) {
164 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700165 return;
166 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700167 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700168 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800169 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700170 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700171 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700172 return;
173 }
174 }
175}
176
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800177void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700178 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800179 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700180
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700181 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700182
183 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
184 vector<pair<int, vector<ReceiverInfo*>>>();
185 for (auto& pair : mReceivers) {
186 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700187 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700188 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800189 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700190 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700191 } else {
192 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
193 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
194 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700195 }
196 }
197 if (receivers.size() > 0) {
198 needToPull.push_back(make_pair(pair.first, receivers));
199 }
200 }
201 }
202
203 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700204 vector<shared_ptr<LogEvent>> data;
Tej Singhfa1c1372019-12-05 20:36:54 -0800205 bool pullSuccess = PullLocked(pullInfo.first, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000206 if (pullSuccess) {
207 StatsdStats::getInstance().notePullDelay(
208 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
209 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800210 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800211 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800212
213 // Convention is to mark pull atom timestamp at request time.
214 // If we pull at t0, puller starts at t1, finishes at t2, and send back
215 // at t3, we mark t0 as its timestamp, which should correspond to its
216 // triggering event, such as condition change at t0.
217 // Here the triggering event is alarm fired from AlarmManager.
218 // In ValueMetricProducer and GaugeMetricProducer we do same thing
219 // when pull on condition change, etc.
220 for (auto& event : data) {
221 event->setElapsedTimestampNs(elapsedTimeNs);
222 event->setLogdWallClockTimestampNs(wallClockNs);
223 }
224
225 for (const auto& receiverInfo : pullInfo.second) {
226 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
227 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000228 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000229 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800230 int numBucketsAhead =
231 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
232 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
233 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
234 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800235 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800236 } else {
237 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700238 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700239 }
240 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700241
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700242 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
243 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700244 mNextPullTimeNs = minNextPullTimeNs;
245 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700246}
247
Chenjie Yue2219202018-06-08 10:07:51 -0700248int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800249 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800250 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800251 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800252 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800253 return totalCleared;
254}
255
Chenjie Yue2219202018-06-08 10:07:51 -0700256int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800257 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800258 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800259 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800260 }
261 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800262}
263
Tej Singh6a5c9432019-10-11 11:07:06 -0700264void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
265 const int64_t coolDownNs, const int64_t timeoutNs,
266 const vector<int32_t>& additiveFields,
267 const sp<IPullAtomCallback>& callback) {
268 AutoMutex _l(mLock);
269 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
270 // TODO: linkToDeath with the callback so that we can remove it and delete the puller.
271 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh5b4951b2020-01-24 13:23:56 -0800272 kAllPullAtomInfo[{.atomTag = atomTag}] =
273 new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields);
Tej Singha0c89dd2019-01-25 16:39:18 -0800274}
275
Tej Singhfa1c1372019-12-05 20:36:54 -0800276void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
277 AutoMutex _l(mLock);
278 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
279 kAllPullAtomInfo.erase({.atomTag = atomTag});
280}
281
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700282} // namespace statsd
283} // namespace os
284} // namespace android