blob: 3ceff75294400a103b33d76d6af35b54dbbdf852 [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"
Tej Singha0c89dd2019-01-25 16:39:18 -080035#include "StatsCallbackPuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080036#include "TrainInfoPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070037#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070038
Yao Chen93fe3a32017-11-02 13:52:59 -070039using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070040using std::vector;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070041
42namespace android {
43namespace os {
44namespace statsd {
45
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070046// Values smaller than this may require to update the alarm.
47const int64_t NO_ALARM_UPDATE = INT64_MAX;
48
Tej Singh5b4951b2020-01-24 13:23:56 -080049StatsPullerManager::StatsPullerManager()
50 : kAllPullAtomInfo({
Tej Singh5b4951b2020-01-24 13:23:56 -080051 // TrainInfo.
52 {{.atomTag = android::util::TRAIN_INFO}, new TrainInfoPuller()},
Tej Singh5b4951b2020-01-24 13:23:56 -080053 }),
54 mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070055}
56
Chenjie Yu0bd73db2018-12-16 07:37:04 -080057bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singhfa1c1372019-12-05 20:36:54 -080058 AutoMutex _l(mLock);
59 return PullLocked(tagId, data);
60}
61
62bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -080063 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070064
Tej Singh6a5c9432019-10-11 11:07:06 -070065 if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) {
Tej Singh5b4951b2020-01-24 13:23:56 -080066 bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -080067 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +000068 if (!ret) {
69 StatsdStats::getInstance().notePullFailed(tagId);
70 }
Tej Singh484524a2018-02-01 15:10:05 -080071 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -070072 } else {
Tej Singh730ed292020-02-03 17:24:27 -080073 ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -070074 return false; // Return early since we don't know what to pull.
75 }
76}
Chenjie Yub3dda412017-10-24 13:41:59 -070077
Chenjie Yue2219202018-06-08 10:07:51 -070078bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singh97db3ff2020-01-27 16:52:17 -080079 // Pulled atoms might be registered after we parse the config, so just make sure the id is in
80 // an appropriate range.
81 return isVendorPulledAtom(tagId) || isPulledAtom(tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070082}
83
Chenjie Yue2219202018-06-08 10:07:51 -070084void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070085 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -070086 VLOG("No need to set alarms. Skipping");
87 return;
88 }
89
Chenjie Yuaa5b2012018-03-21 13:53:15 -070090 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
91 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -070092 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -070093 } else {
94 VLOG("StatsCompanionService not available. Alarm not set.");
95 }
96 return;
97}
98
Chenjie Yue2219202018-06-08 10:07:51 -070099void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700100 sp<IStatsCompanionService> statsCompanionService) {
101 AutoMutex _l(mLock);
102 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
103 mStatsCompanionService = statsCompanionService;
104 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800105 pulledAtom.second->SetStatsCompanionService(statsCompanionService);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700106 }
107 if (mStatsCompanionService != nullptr) {
108 updateAlarmLocked();
109 }
110}
111
Chenjie Yue2219202018-06-08 10:07:51 -0700112void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700113 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700114 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800115 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700116 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800117 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700118 VLOG("Receiver already registered of %d", (int)receivers.size());
119 return;
120 }
121 }
122 ReceiverInfo receiverInfo;
123 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700124
Chenjie Yu85ed8382017-12-14 16:48:54 -0800125 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700126 // In practice, we should always have larger buckets.
127 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700128 // Scheduled pulling should be at least 1 min apart.
129 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700130 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
131 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700132 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700133
134 receiverInfo.intervalNs = roundedIntervalNs;
135 receiverInfo.nextPullTimeNs = nextPullTimeNs;
136 receivers.push_back(receiverInfo);
137
Chenjie Yub3dda412017-10-24 13:41:59 -0700138 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700139 if (nextPullTimeNs < mNextPullTimeNs) {
140 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
141 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700142 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700143 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700144 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700145}
146
Chenjie Yue2219202018-06-08 10:07:51 -0700147void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700148 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700149 if (mReceivers.find(tagId) == mReceivers.end()) {
150 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700151 return;
152 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700153 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700154 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800155 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700156 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700157 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700158 return;
159 }
160 }
161}
162
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800163void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700164 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800165 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700166
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700167 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700168
169 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
170 vector<pair<int, vector<ReceiverInfo*>>>();
171 for (auto& pair : mReceivers) {
172 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700173 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700174 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800175 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700176 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700177 } else {
178 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
179 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
180 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700181 }
182 }
183 if (receivers.size() > 0) {
184 needToPull.push_back(make_pair(pair.first, receivers));
185 }
186 }
187 }
188
189 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700190 vector<shared_ptr<LogEvent>> data;
Tej Singhfa1c1372019-12-05 20:36:54 -0800191 bool pullSuccess = PullLocked(pullInfo.first, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000192 if (pullSuccess) {
193 StatsdStats::getInstance().notePullDelay(
194 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
195 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800196 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800197 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800198
199 // Convention is to mark pull atom timestamp at request time.
200 // If we pull at t0, puller starts at t1, finishes at t2, and send back
201 // at t3, we mark t0 as its timestamp, which should correspond to its
202 // triggering event, such as condition change at t0.
203 // Here the triggering event is alarm fired from AlarmManager.
204 // In ValueMetricProducer and GaugeMetricProducer we do same thing
205 // when pull on condition change, etc.
206 for (auto& event : data) {
207 event->setElapsedTimestampNs(elapsedTimeNs);
208 event->setLogdWallClockTimestampNs(wallClockNs);
209 }
210
211 for (const auto& receiverInfo : pullInfo.second) {
212 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
213 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000214 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000215 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800216 int numBucketsAhead =
217 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
218 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
219 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
220 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800221 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800222 } else {
223 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700224 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700225 }
226 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700227
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700228 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
229 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700230 mNextPullTimeNs = minNextPullTimeNs;
231 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700232}
233
Chenjie Yue2219202018-06-08 10:07:51 -0700234int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800235 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800236 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800237 totalCleared += pulledAtom.second->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800238 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800239 return totalCleared;
240}
241
Chenjie Yue2219202018-06-08 10:07:51 -0700242int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800243 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800244 for (const auto& pulledAtom : kAllPullAtomInfo) {
Tej Singh5b4951b2020-01-24 13:23:56 -0800245 totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800246 }
247 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800248}
249
Tej Singh6a5c9432019-10-11 11:07:06 -0700250void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
251 const int64_t coolDownNs, const int64_t timeoutNs,
252 const vector<int32_t>& additiveFields,
253 const sp<IPullAtomCallback>& callback) {
254 AutoMutex _l(mLock);
255 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
256 // TODO: linkToDeath with the callback so that we can remove it and delete the puller.
257 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh5b4951b2020-01-24 13:23:56 -0800258 kAllPullAtomInfo[{.atomTag = atomTag}] =
259 new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields);
Tej Singha0c89dd2019-01-25 16:39:18 -0800260}
261
Tej Singhfa1c1372019-12-05 20:36:54 -0800262void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
263 AutoMutex _l(mLock);
264 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
265 kAllPullAtomInfo.erase({.atomTag = atomTag});
266}
267
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700268} // namespace statsd
269} // namespace os
270} // namespace android