blob: 7708e300929e499d4a9d926a4d8ea27eaa7d0844 [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"
Bookatz92da2832018-11-01 18:10:03 -070036#include "PowerStatsPuller.h"
Tej Singh40298312018-02-16 00:15:09 -080037#include "ResourceHealthManagerPuller.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080038#include "StatsCallbackPuller.h"
Tej Singhbf972d92018-01-10 20:51:13 -080039#include "SubsystemSleepStatePuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080040#include "TrainInfoPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070041#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070042
Yao Chen93fe3a32017-11-02 13:52:59 -070043using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070044using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070045using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070046using std::string;
47using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080048using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070049
50namespace android {
51namespace os {
52namespace statsd {
53
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070054// Values smaller than this may require to update the alarm.
55const int64_t NO_ALARM_UPDATE = INT64_MAX;
56
Tej Singh5b4951b2020-01-24 13:23:56 -080057StatsPullerManager::StatsPullerManager()
58 : kAllPullAtomInfo({
59 // subsystem_sleep_state
60 {{.atomTag = android::util::SUBSYSTEM_SLEEP_STATE}, new SubsystemSleepStatePuller()},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080061
Tej Singh5b4951b2020-01-24 13:23:56 -080062 // on_device_power_measurement
63 {{.atomTag = android::util::ON_DEVICE_POWER_MEASUREMENT}, new PowerStatsPuller()},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080064
Tej Singh5b4951b2020-01-24 13:23:56 -080065 // remaining_battery_capacity
66 {{.atomTag = android::util::REMAINING_BATTERY_CAPACITY},
67 new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080068
Tej Singh5b4951b2020-01-24 13:23:56 -080069 // full_battery_capacity
70 {{.atomTag = android::util::FULL_BATTERY_CAPACITY},
71 new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080072
Tej Singh5b4951b2020-01-24 13:23:56 -080073 // battery_voltage
74 {{.atomTag = android::util::BATTERY_VOLTAGE},
75 new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080076
Tej Singh5b4951b2020-01-24 13:23:56 -080077 // battery_level
78 {{.atomTag = android::util::BATTERY_LEVEL},
79 new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080080
Tej Singh5b4951b2020-01-24 13:23:56 -080081 // battery_cycle_count
82 {{.atomTag = android::util::BATTERY_CYCLE_COUNT},
83 new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080084
Tej Singh5b4951b2020-01-24 13:23:56 -080085 // TrainInfo.
86 {{.atomTag = android::util::TRAIN_INFO}, new TrainInfoPuller()},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080087
Tej Singh5b4951b2020-01-24 13:23:56 -080088 // GpuStatsGlobalInfo
89 {{.atomTag = android::util::GPU_STATS_GLOBAL_INFO},
90 new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080091
Tej Singh5b4951b2020-01-24 13:23:56 -080092 // GpuStatsAppInfo
93 {{.atomTag = android::util::GPU_STATS_APP_INFO},
94 new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080095
Tej Singh5b4951b2020-01-24 13:23:56 -080096 }),
97 mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070098}
99
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800100bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singhfa1c1372019-12-05 20:36:54 -0800101 AutoMutex _l(mLock);
102 return PullLocked(tagId, data);
103}
104
105bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800106 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700107
Tej Singh6a5c9432019-10-11 11:07:06 -0700108 if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800109 bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -0800110 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +0000111 if (!ret) {
112 StatsdStats::getInstance().notePullFailed(tagId);
113 }
Tej Singh484524a2018-02-01 15:10:05 -0800114 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700115 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800116 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700117 return false; // Return early since we don't know what to pull.
118 }
119}
Chenjie Yub3dda412017-10-24 13:41:59 -0700120
Chenjie Yue2219202018-06-08 10:07:51 -0700121bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singhde473b512019-04-10 19:25:03 -0700122 // Vendor pulled atoms might be registered after we parse the config.
Tej Singh6a5c9432019-10-11 11:07:06 -0700123 return isVendorPulledAtom(tagId) ||
124 kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700125}
126
Chenjie Yue2219202018-06-08 10:07:51 -0700127void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700128 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700129 VLOG("No need to set alarms. Skipping");
130 return;
131 }
132
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700133 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
134 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700135 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700136 } else {
137 VLOG("StatsCompanionService not available. Alarm not set.");
138 }
139 return;
140}
141
Chenjie Yue2219202018-06-08 10:07:51 -0700142void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700143 sp<IStatsCompanionService> statsCompanionService) {
144 AutoMutex _l(mLock);
145 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
146 mStatsCompanionService = statsCompanionService;
147 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800148 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700149 }
150 if (mStatsCompanionService != nullptr) {
151 updateAlarmLocked();
152 }
153}
154
Chenjie Yue2219202018-06-08 10:07:51 -0700155void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700156 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700157 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800158 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700159 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800160 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700161 VLOG("Receiver already registered of %d", (int)receivers.size());
162 return;
163 }
164 }
165 ReceiverInfo receiverInfo;
166 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700167
Chenjie Yu85ed8382017-12-14 16:48:54 -0800168 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700169 // In practice, we should always have larger buckets.
170 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700171 // Scheduled pulling should be at least 1 min apart.
172 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700173 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
174 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700175 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700176
177 receiverInfo.intervalNs = roundedIntervalNs;
178 receiverInfo.nextPullTimeNs = nextPullTimeNs;
179 receivers.push_back(receiverInfo);
180
Chenjie Yub3dda412017-10-24 13:41:59 -0700181 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700182 if (nextPullTimeNs < mNextPullTimeNs) {
183 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
184 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700185 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700186 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700187 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700188}
189
Chenjie Yue2219202018-06-08 10:07:51 -0700190void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700191 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700192 if (mReceivers.find(tagId) == mReceivers.end()) {
193 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700194 return;
195 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700196 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700197 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800198 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700199 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700200 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700201 return;
202 }
203 }
204}
205
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800206void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700207 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800208 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700209
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700210 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700211
212 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
213 vector<pair<int, vector<ReceiverInfo*>>>();
214 for (auto& pair : mReceivers) {
215 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700216 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700217 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800218 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700219 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700220 } else {
221 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
222 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
223 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700224 }
225 }
226 if (receivers.size() > 0) {
227 needToPull.push_back(make_pair(pair.first, receivers));
228 }
229 }
230 }
231
232 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700233 vector<shared_ptr<LogEvent>> data;
Tej Singhfa1c1372019-12-05 20:36:54 -0800234 bool pullSuccess = PullLocked(pullInfo.first, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000235 if (pullSuccess) {
236 StatsdStats::getInstance().notePullDelay(
237 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
238 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800239 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800240 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800241
242 // Convention is to mark pull atom timestamp at request time.
243 // If we pull at t0, puller starts at t1, finishes at t2, and send back
244 // at t3, we mark t0 as its timestamp, which should correspond to its
245 // triggering event, such as condition change at t0.
246 // Here the triggering event is alarm fired from AlarmManager.
247 // In ValueMetricProducer and GaugeMetricProducer we do same thing
248 // when pull on condition change, etc.
249 for (auto& event : data) {
250 event->setElapsedTimestampNs(elapsedTimeNs);
251 event->setLogdWallClockTimestampNs(wallClockNs);
252 }
253
254 for (const auto& receiverInfo : pullInfo.second) {
255 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
256 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000257 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000258 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800259 int numBucketsAhead =
260 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
261 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
262 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
263 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800264 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800265 } else {
266 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700267 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700268 }
269 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700270
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700271 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
272 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700273 mNextPullTimeNs = minNextPullTimeNs;
274 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700275}
276
Chenjie Yue2219202018-06-08 10:07:51 -0700277int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800278 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800279 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800280 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800281 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800282 return totalCleared;
283}
284
Chenjie Yue2219202018-06-08 10:07:51 -0700285int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800286 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800287 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800288 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800289 }
290 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800291}
292
Tej Singh6a5c9432019-10-11 11:07:06 -0700293void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
294 const int64_t coolDownNs, const int64_t timeoutNs,
295 const vector<int32_t>& additiveFields,
296 const sp<IPullAtomCallback>& callback) {
297 AutoMutex _l(mLock);
298 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
299 // TODO: linkToDeath with the callback so that we can remove it and delete the puller.
300 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh5b4951b2020-01-24 13:23:56 -0800301 kAllPullAtomInfo[{.atomTag = atomTag}] =
302 new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields);
Tej Singha0c89dd2019-01-25 16:39:18 -0800303}
304
Tej Singhfa1c1372019-12-05 20:36:54 -0800305void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
306 AutoMutex _l(mLock);
307 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
308 kAllPullAtomInfo.erase({.atomTag = atomTag});
309}
310
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700311} // namespace statsd
312} // namespace os
313} // namespace android