blob: 60e259be95580e0b9c768680cf88cdaf351d1611 [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53:50 -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 Yuc7939cb2019-02-04 17:25:45 -080017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
tsaichristined21aacf2019-10-07 14:47:38 -070019
20#include "StatsLogProcessor.h"
Yao Chenab273e22017-09-06 12:53:50 -070021
yro947fbce2017-11-15 22:50:23 -080022#include <android-base/file.h>
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080023#include <cutils/multiuser.h>
Chenjie Yuc7939cb2019-02-04 17:25:45 -080024#include <frameworks/base/cmds/statsd/src/active_config_list.pb.h>
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080025#include <frameworks/base/cmds/statsd/src/experiment_ids.pb.h>
tsaichristined21aacf2019-10-07 14:47:38 -070026
yro947fbce2017-11-15 22:50:23 -080027#include "android-base/stringprintf.h"
Chenjie Yuc7939cb2019-02-04 17:25:45 -080028#include "external/StatsPullerManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080029#include "guardrail/StatsdStats.h"
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080030#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031#include "metrics/CountMetricProducer.h"
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080032#include "StatsService.h"
tsaichristined21aacf2019-10-07 14:47:38 -070033#include "state/StateManager.h"
Chenjie Yuc7939cb2019-02-04 17:25:45 -080034#include "stats_log_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035#include "stats_util.h"
Jeffrey Huang74fc4352020-03-06 15:18:33 -080036#include "statslog_statsd.h"
yro947fbce2017-11-15 22:50:23 -080037#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070038
Yao Chenab273e22017-09-06 12:53:50 -070039using namespace android;
yro947fbce2017-11-15 22:50:23 -080040using android::base::StringPrintf;
yrob0378b02017-11-09 20:36:25 -080041using android::util::FIELD_COUNT_REPEATED;
yro17adac92017-11-08 23:16:29 -080042using android::util::FIELD_TYPE_BOOL;
43using android::util::FIELD_TYPE_FLOAT;
44using android::util::FIELD_TYPE_INT32;
45using android::util::FIELD_TYPE_INT64;
46using android::util::FIELD_TYPE_MESSAGE;
47using android::util::FIELD_TYPE_STRING;
48using android::util::ProtoOutputStream;
Yao Chen44cf27c2017-09-14 22:32:50 -070049using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070050
51namespace android {
52namespace os {
53namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070054
yro947fbce2017-11-15 22:50:23 -080055// for ConfigMetricsReportList
yro17adac92017-11-08 23:16:29 -080056const int FIELD_ID_CONFIG_KEY = 1;
yro947fbce2017-11-15 22:50:23 -080057const int FIELD_ID_REPORTS = 2;
yro17adac92017-11-08 23:16:29 -080058// for ConfigKey
59const int FIELD_ID_UID = 1;
Yangster-mac94e197c2018-01-02 16:03:03 -080060const int FIELD_ID_ID = 2;
yro947fbce2017-11-15 22:50:23 -080061// for ConfigMetricsReport
Yao Chen4c959cb2018-02-13 13:27:48 -080062// const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp
yro947fbce2017-11-15 22:50:23 -080063const int FIELD_ID_UID_MAP = 2;
Yangster-mac330af582018-02-08 15:24:38 -080064const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
65const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080066const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5;
67const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6;
Chenjie Yue36018b2018-04-16 15:18:30 -070068const int FIELD_ID_DUMP_REPORT_REASON = 8;
Yangster-mac9def8e32018-04-17 13:55:51 -070069const int FIELD_ID_STRINGS = 9;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080070
Muhammad Qureshi844694b2019-04-05 10:10:40 -070071// for ActiveConfigList
72const int FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG = 1;
Chenjie Yuc7939cb2019-02-04 17:25:45 -080073
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080074// for permissions checks
75constexpr const char* kPermissionDump = "android.permission.DUMP";
76constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS";
77
Yangster-macb142cc82018-03-30 15:22:08 -070078#define NS_PER_HOUR 3600 * NS_PER_SEC
yro947fbce2017-11-15 22:50:23 -080079
Chenjie Yuc7939cb2019-02-04 17:25:45 -080080#define STATS_ACTIVE_METRIC_DIR "/data/misc/stats-active-metric"
Jeffrey Huangb8f54032020-03-23 13:42:42 -070081#define STATS_METADATA_DIR "/data/misc/stats-metadata"
yro17adac92017-11-08 23:16:29 -080082
Tej Singh42f9e062018-11-09 10:01:00 -080083// Cool down period for writing data to disk to avoid overwriting files.
84#define WRITE_DATA_COOL_DOWN_SEC 5
85
yro31eb67b2017-10-24 13:33:21 -070086StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
Chenjie Yue2219202018-06-08 10:07:51 -070087 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -080088 const sp<AlarmMonitor>& anomalyAlarmMonitor,
89 const sp<AlarmMonitor>& periodicAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070090 const int64_t timeBaseNs,
Tej Singh6ede28b2019-01-29 17:06:54 -080091 const std::function<bool(const ConfigKey&)>& sendBroadcast,
92 const std::function<bool(
93 const int&, const vector<int64_t>&)>& activateBroadcast)
Chenjie Yu85ed8382017-12-14 16:48:54 -080094 : mUidMap(uidMap),
Chenjie Yue2219202018-06-08 10:07:51 -070095 mPullerManager(pullerManager),
Yangster-mac932ecec2018-02-01 10:23:52 -080096 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
97 mPeriodicAlarmMonitor(periodicAlarmMonitor),
Chenjie Yu85ed8382017-12-14 16:48:54 -080098 mSendBroadcast(sendBroadcast),
Tej Singh6ede28b2019-01-29 17:06:54 -080099 mSendActivationBroadcast(activateBroadcast),
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700100 mTimeBaseNs(timeBaseNs),
Yao Chen163d2602018-04-10 10:39:53 -0700101 mLargestTimestampSeen(0),
102 mLastTimestampSeen(0) {
Chenjie Yue2219202018-06-08 10:07:51 -0700103 mPullerManager->ForceClearPullerCache();
Yao Chenab273e22017-09-06 12:53:50 -0700104}
105
Yao Chenef99c4f2017-09-22 16:26:54 -0700106StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -0700107}
108
Yao Chen9a43b4f2019-04-10 10:43:20 -0700109static void flushProtoToBuffer(ProtoOutputStream& proto, vector<uint8_t>* outData) {
110 outData->clear();
111 outData->resize(proto.size());
112 size_t pos = 0;
113 sp<android::util::ProtoReader> reader = proto.data();
114 while (reader->readBuffer() != NULL) {
115 size_t toRead = reader->currentToRead();
116 std::memcpy(&((*outData)[pos]), reader->readBuffer(), toRead);
117 pos += toRead;
118 reader->move(toRead);
119 }
120}
121
Yangster-mace2cd6d52017-11-09 20:38:30 -0800122void StatsLogProcessor::onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700123 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800124 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
Yangster-macb0d06282018-01-05 15:44:07 -0800125 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef22017-11-21 14:36:23 -0800126 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800127 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
128 }
129}
130void StatsLogProcessor::onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700131 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800132 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
133
134 std::lock_guard<std::mutex> lock(mMetricsMutex);
135 for (const auto& itr : mMetricsManagers) {
136 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800137 }
138}
139
Yangster-macd40053e2018-01-09 16:29:22 -0800140void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Muhammad Qureshi83aaa662020-04-21 12:51:36 -0700141 if (std::pair<int, int> indexRange; event->hasAttributionChain(&indexRange)) {
142 vector<FieldValue>* const fieldValues = event->getMutableValues();
143 for (int i = indexRange.first; i <= indexRange.second; i++) {
144 FieldValue& fieldValue = fieldValues->at(i);
145 if (isAttributionUidField(fieldValue)) {
146 const int hostUid = mUidMap->getHostUidOrSelf(fieldValue.mValue.int_value);
147 fieldValue.mValue.setInt(hostUid);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800148 }
Yangster-macd40053e2018-01-09 16:29:22 -0800149 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700150 } else {
Ruchir Rastogiffa34f052020-04-04 15:30:11 -0700151 int uidFieldIndex = event->getUidFieldIndex();
152 if (uidFieldIndex != -1) {
153 Value& value = (*event->getMutableValues())[uidFieldIndex].mValue;
154 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
155 value.setInt(hostUid);
Yao Chenc40a19d2018-03-15 16:48:25 -0700156 }
Yao Chen312e8982017-12-05 15:29:03 -0800157 }
Yangster-macd40053e2018-01-09 16:29:22 -0800158}
159
160void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
161 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
162 bool is_create = event.GetBool(3, &err);
163 auto parent_uid = int(event.GetLong(1, &err2));
164 auto isolated_uid = int(event.GetLong(2, &err3));
165 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
166 if (is_create) {
167 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
168 } else {
Bookatz3c648862018-05-25 13:32:43 -0700169 mUidMap->removeIsolatedUid(isolated_uid);
Yangster-macd40053e2018-01-09 16:29:22 -0800170 }
171 } else {
172 ALOGE("Failed to parse uid in the isolated uid change event.");
173 }
174}
175
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800176void StatsLogProcessor::onBinaryPushStateChangedEventLocked(LogEvent* event) {
177 pid_t pid = event->GetPid();
178 uid_t uid = event->GetUid();
179 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
180 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
181 return;
182 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800183 // The Get* functions don't modify the status on success, they only write in
184 // failure statuses, so we can use one status variable for all calls then
185 // check if it is no longer NO_ERROR.
186 status_t err = NO_ERROR;
187 InstallTrainInfo trainInfo;
188 trainInfo.trainName = string(event->GetString(1 /*train name field id*/, &err));
189 trainInfo.trainVersionCode = event->GetLong(2 /*train version field id*/, &err);
190 trainInfo.requiresStaging = event->GetBool(3 /*requires staging field id*/, &err);
191 trainInfo.rollbackEnabled = event->GetBool(4 /*rollback enabled field id*/, &err);
192 trainInfo.requiresLowLatencyMonitor =
193 event->GetBool(5 /*requires low latency monitor field id*/, &err);
194 trainInfo.status = int32_t(event->GetLong(6 /*state field id*/, &err));
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800195 std::vector<uint8_t> trainExperimentIdBytes =
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800196 event->GetStorage(7 /*experiment ids field id*/, &err);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800197 bool is_rollback = event->GetBool(10 /*is rollback field id*/, &err);
198
199 if (err != NO_ERROR) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800200 ALOGE("Failed to parse fields in binary push state changed log event");
201 return;
202 }
203 ExperimentIds trainExperimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800204 if (!trainExperimentIds.ParseFromArray(trainExperimentIdBytes.data(),
205 trainExperimentIdBytes.size())) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800206 ALOGE("Failed to parse experimentids in binary push state changed.");
207 return;
208 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800209 trainInfo.experimentIds = {trainExperimentIds.experiment_id().begin(),
210 trainExperimentIds.experiment_id().end()};
211
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800212 // Update the train info on disk and get any data the logevent is missing.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800213 getAndUpdateTrainInfoOnDisk(is_rollback, &trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800214
215 std::vector<uint8_t> trainExperimentIdProto;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800216 writeExperimentIdsToProto(trainInfo.experimentIds, &trainExperimentIdProto);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800217 int32_t userId = multiuser_get_user_id(uid);
218
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800219 event->updateValue(2 /*train version field id*/, trainInfo.trainVersionCode, LONG);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800220 event->updateValue(7 /*experiment ids field id*/, trainExperimentIdProto, STORAGE);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800221 event->updateValue(8 /*user id field id*/, userId, INT);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800222
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800223 // If this event is a rollback event, then the following bits in the event
224 // are invalid and we will need to update them with the values we pulled
225 // from disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800226 if (is_rollback) {
227 int bit = trainInfo.requiresStaging ? 1 : 0;
228 event->updateValue(3 /*requires staging field id*/, bit, INT);
229 bit = trainInfo.rollbackEnabled ? 1 : 0;
230 event->updateValue(4 /*rollback enabled field id*/, bit, INT);
231 bit = trainInfo.requiresLowLatencyMonitor ? 1 : 0;
232 event->updateValue(5 /*requires low latency monitor field id*/, bit, INT);
233 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800234}
235
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800236void StatsLogProcessor::getAndUpdateTrainInfoOnDisk(bool is_rollback,
237 InstallTrainInfo* trainInfo) {
238 // If the train name is empty, we don't know which train to attribute the
239 // event to, so return early.
240 if (trainInfo->trainName.empty()) {
241 return;
242 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800243 bool readTrainInfoSuccess = false;
244 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800245 readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfo->trainName, trainInfoOnDisk);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800246
247 bool resetExperimentIds = false;
248 if (readTrainInfoSuccess) {
249 // Keep the old train version if we received an empty version.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800250 if (trainInfo->trainVersionCode == -1) {
251 trainInfo->trainVersionCode = trainInfoOnDisk.trainVersionCode;
252 } else if (trainInfo->trainVersionCode != trainInfoOnDisk.trainVersionCode) {
253 // Reset experiment ids if we receive a new non-empty train version.
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800254 resetExperimentIds = true;
255 }
256
257 // Reset if we received a different experiment id.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800258 if (!trainInfo->experimentIds.empty() &&
259 (trainInfoOnDisk.experimentIds.empty() ||
260 trainInfo->experimentIds.at(0) != trainInfoOnDisk.experimentIds[0])) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800261 resetExperimentIds = true;
262 }
263 }
264
265 // Find the right experiment IDs
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800266 if ((!resetExperimentIds || is_rollback) && readTrainInfoSuccess) {
267 trainInfo->experimentIds = trainInfoOnDisk.experimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800268 }
269
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800270 if (!trainInfo->experimentIds.empty()) {
271 int64_t firstId = trainInfo->experimentIds.at(0);
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800272 auto& ids = trainInfo->experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800273 switch (trainInfo->status) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800274 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALL_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800275 if (find(ids.begin(), ids.end(), firstId + 1) == ids.end()) {
276 ids.push_back(firstId + 1);
277 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800278 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800279 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_INITIATED:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800280 if (find(ids.begin(), ids.end(), firstId + 2) == ids.end()) {
281 ids.push_back(firstId + 2);
282 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800283 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800284 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800285 if (find(ids.begin(), ids.end(), firstId + 3) == ids.end()) {
286 ids.push_back(firstId + 3);
287 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800288 break;
289 }
290 }
291
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800292 // If this event is a rollback event, the following fields are invalid and
293 // need to be replaced by the fields stored to disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800294 if (is_rollback) {
295 trainInfo->requiresStaging = trainInfoOnDisk.requiresStaging;
296 trainInfo->rollbackEnabled = trainInfoOnDisk.rollbackEnabled;
297 trainInfo->requiresLowLatencyMonitor = trainInfoOnDisk.requiresLowLatencyMonitor;
298 }
299
300 StorageManager::writeTrainInfo(*trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800301}
302
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800303void StatsLogProcessor::onWatchdogRollbackOccurredLocked(LogEvent* event) {
304 pid_t pid = event->GetPid();
305 uid_t uid = event->GetUid();
306 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
307 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
308 return;
309 }
310 // The Get* functions don't modify the status on success, they only write in
311 // failure statuses, so we can use one status variable for all calls then
312 // check if it is no longer NO_ERROR.
313 status_t err = NO_ERROR;
314 int32_t rollbackType = int32_t(event->GetInt(1 /*rollback type field id*/, &err));
315 string packageName = string(event->GetString(2 /*package name field id*/, &err));
316
317 if (err != NO_ERROR) {
318 ALOGE("Failed to parse fields in watchdog rollback occurred log event");
319 return;
320 }
321
322 vector<int64_t> experimentIds =
323 processWatchdogRollbackOccurred(rollbackType, packageName);
324 vector<uint8_t> experimentIdProto;
325 writeExperimentIdsToProto(experimentIds, &experimentIdProto);
326
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800327 event->updateValue(6 /*experiment ids field id*/, experimentIdProto, STORAGE);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800328}
329
330vector<int64_t> StatsLogProcessor::processWatchdogRollbackOccurred(const int32_t rollbackTypeIn,
331 const string& packageNameIn) {
332 // If the package name is empty, we can't attribute it to any train, so
333 // return early.
334 if (packageNameIn.empty()) {
335 return vector<int64_t>();
336 }
337 bool readTrainInfoSuccess = false;
338 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800339 // We use the package name of the event as the train name.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800340 readTrainInfoSuccess = StorageManager::readTrainInfo(packageNameIn, trainInfoOnDisk);
341
342 if (!readTrainInfoSuccess) {
343 return vector<int64_t>();
344 }
345
346 if (trainInfoOnDisk.experimentIds.empty()) {
347 return vector<int64_t>();
348 }
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800349
350 int64_t firstId = trainInfoOnDisk.experimentIds[0];
351 auto& ids = trainInfoOnDisk.experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800352 switch (rollbackTypeIn) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800353 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_INITIATE:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800354 if (find(ids.begin(), ids.end(), firstId + 4) == ids.end()) {
355 ids.push_back(firstId + 4);
356 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800357 StorageManager::writeTrainInfo(trainInfoOnDisk);
358 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800359 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800360 if (find(ids.begin(), ids.end(), firstId + 5) == ids.end()) {
361 ids.push_back(firstId + 5);
362 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800363 StorageManager::writeTrainInfo(trainInfoOnDisk);
364 break;
365 }
366
367 return trainInfoOnDisk.experimentIds;
368}
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800369
Yangster-mac892f3d32018-05-02 14:16:48 -0700370void StatsLogProcessor::resetConfigs() {
371 std::lock_guard<std::mutex> lock(mMetricsMutex);
372 resetConfigsLocked(getElapsedRealtimeNs());
373}
374
375void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) {
376 std::vector<ConfigKey> configKeys;
377 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
378 configKeys.push_back(it->first);
379 }
380 resetConfigsLocked(timestampNs, configKeys);
381}
382
Yao Chen3ff3a492018-08-06 16:17:37 -0700383void StatsLogProcessor::OnLogEvent(LogEvent* event) {
Tej Singh480392f2019-10-23 15:53:46 -0700384 OnLogEvent(event, getElapsedRealtimeNs());
385}
386
387void StatsLogProcessor::OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs) {
Yangster-macd40053e2018-01-09 16:29:22 -0800388 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen876889c2018-05-02 11:16:16 -0700389
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700390 // Tell StatsdStats about new event
391 const int64_t eventElapsedTimeNs = event->GetElapsedTimestampNs();
392 int atomId = event->GetTagId();
393 StatsdStats::getInstance().noteAtomLogged(atomId, eventElapsedTimeNs / NS_PER_SEC);
394 if (!event->isValid()) {
395 StatsdStats::getInstance().noteAtomError(atomId);
396 return;
397 }
398
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800399 // Hard-coded logic to update train info on disk and fill in any information
400 // this log event may be missing.
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700401 if (atomId == android::os::statsd::util::BINARY_PUSH_STATE_CHANGED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800402 onBinaryPushStateChangedEventLocked(event);
403 }
404
405 // Hard-coded logic to update experiment ids on disk for certain rollback
406 // types and fill the rollback atom with experiment ids
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700407 if (atomId == android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800408 onWatchdogRollbackOccurredLocked(event);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800409 }
410
Yao Chen876889c2018-05-02 11:16:16 -0700411#ifdef VERY_VERBOSE_PRINTING
412 if (mPrintAllLogs) {
413 ALOGI("%s", event->ToString().c_str());
414 }
415#endif
Tej Singh480392f2019-10-23 15:53:46 -0700416 resetIfConfigTtlExpiredLocked(eventElapsedTimeNs);
Yangster-macb142cc82018-03-30 15:22:08 -0700417
David Chen21582962017-11-01 17:32:46 -0700418 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800419 // The field numbers need to be currently updated by hand with atoms.proto
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700420 if (atomId == android::os::statsd::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800421 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800422 }
423
tsaichristined21aacf2019-10-07 14:47:38 -0700424 StateManager::getInstance().onLogEvent(*event);
425
David Chencfc311d2018-01-23 17:55:54 -0800426 if (mMetricsManagers.empty()) {
427 return;
428 }
429
Yangster-macb142cc82018-03-30 15:22:08 -0700430 int64_t curTimeSec = getElapsedRealtimeSec();
Yangster-mac330af582018-02-08 15:24:38 -0800431 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
Chenjie Yue2219202018-06-08 10:07:51 -0700432 mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC);
Yangster-mac330af582018-02-08 15:24:38 -0800433 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800434 }
435
Yangster-macb142cc82018-03-30 15:22:08 -0700436
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700437 if (atomId != android::os::statsd::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800438 // Map the isolated uid to host uid if necessary.
439 mapIsolatedUidToHostUidIfNecessaryLocked(event);
440 }
441
Tej Singh6ede28b2019-01-29 17:06:54 -0800442 std::unordered_set<int> uidsWithActiveConfigsChanged;
443 std::unordered_map<int, std::vector<int64_t>> activeConfigsPerUid;
Yangster-macd40053e2018-01-09 16:29:22 -0800444 // pass the event to metrics managers.
445 for (auto& pair : mMetricsManagers) {
Tej Singh6ede28b2019-01-29 17:06:54 -0800446 int uid = pair.first.GetUid();
447 int64_t configId = pair.first.GetId();
448 bool isPrevActive = pair.second->isActive();
Yangster-macd40053e2018-01-09 16:29:22 -0800449 pair.second->onLogEvent(*event);
Tej Singh6ede28b2019-01-29 17:06:54 -0800450 bool isCurActive = pair.second->isActive();
451 // Map all active configs by uid.
452 if (isCurActive) {
453 auto activeConfigs = activeConfigsPerUid.find(uid);
454 if (activeConfigs != activeConfigsPerUid.end()) {
455 activeConfigs->second.push_back(configId);
456 } else {
457 vector<int64_t> newActiveConfigs;
458 newActiveConfigs.push_back(configId);
459 activeConfigsPerUid[uid] = newActiveConfigs;
460 }
461 }
462 // The activation state of this config changed.
463 if (isPrevActive != isCurActive) {
464 VLOG("Active status changed for uid %d", uid);
465 uidsWithActiveConfigsChanged.insert(uid);
466 StatsdStats::getInstance().noteActiveStatusChanged(pair.first, isCurActive);
467 }
Tej Singh480392f2019-10-23 15:53:46 -0700468 flushIfNecessaryLocked(pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700469 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800470
Tej Singh480392f2019-10-23 15:53:46 -0700471 // Don't use the event timestamp for the guardrail.
Tej Singh6ede28b2019-01-29 17:06:54 -0800472 for (int uid : uidsWithActiveConfigsChanged) {
473 // Send broadcast so that receivers can pull data.
474 auto lastBroadcastTime = mLastActivationBroadcastTimes.find(uid);
475 if (lastBroadcastTime != mLastActivationBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700476 if (elapsedRealtimeNs - lastBroadcastTime->second <
477 StatsdStats::kMinActivationBroadcastPeriodNs) {
Tej Singh16ca28f2019-06-24 11:58:23 -0700478 StatsdStats::getInstance().noteActivationBroadcastGuardrailHit(uid);
Tej Singh6ede28b2019-01-29 17:06:54 -0800479 VLOG("StatsD would've sent an activation broadcast but the rate limit stopped us.");
480 return;
481 }
482 }
483 auto activeConfigs = activeConfigsPerUid.find(uid);
484 if (activeConfigs != activeConfigsPerUid.end()) {
485 if (mSendActivationBroadcast(uid, activeConfigs->second)) {
486 VLOG("StatsD sent activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700487 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800488 }
489 } else {
490 std::vector<int64_t> emptyActiveConfigs;
491 if (mSendActivationBroadcast(uid, emptyActiveConfigs)) {
492 VLOG("StatsD sent EMPTY activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700493 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800494 }
495 }
496 }
497}
498
499void StatsLogProcessor::GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs) {
500 std::lock_guard<std::mutex> lock(mMetricsMutex);
501 GetActiveConfigsLocked(uid, outActiveConfigs);
502}
503
504void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs) {
505 outActiveConfigs.clear();
506 for (auto& pair : mMetricsManagers) {
507 if (pair.first.GetUid() == uid && pair.second->isActive()) {
508 outActiveConfigs.push_back(pair.first.GetId());
509 }
510 }
Yao Chenab273e22017-09-06 12:53:50 -0700511}
512
Yangster-macc04feba2018-04-02 14:37:33 -0700513void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
514 const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800515 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000516 WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED, NO_TIME_CONSTRAINTS);
Yangster-macb142cc82018-03-30 15:22:08 -0700517 OnConfigUpdatedLocked(timestampNs, key, config);
518}
519
520void StatsLogProcessor::OnConfigUpdatedLocked(
521 const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) {
Tej Singh484524a2018-02-01 15:10:05 -0800522 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800523 sp<MetricsManager> newMetricsManager =
Chenjie Yue2219202018-06-08 10:07:51 -0700524 new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager,
525 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Yao Chencaf339d2017-10-06 16:01:10 -0700526 if (newMetricsManager->isConfigValid()) {
Tej Singh3be093b2020-03-04 20:08:38 -0800527 newMetricsManager->init();
David Chend6896892017-10-25 11:49:03 -0700528 mUidMap->OnConfigUpdated(key);
Yangster-macb142cc82018-03-30 15:22:08 -0700529 newMetricsManager->refreshTtl(timestampNs);
Yao Chend10f7b12017-12-18 12:53:50 -0800530 mMetricsManagers[key] = newMetricsManager;
Yao Chenb3561512017-11-21 18:07:17 -0800531 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700532 } else {
533 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800534 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700535 }
yro00698da2017-09-15 10:06:40 -0700536}
Bookatz906a35c2017-09-20 15:26:44 -0700537
Yangster7c334a12017-11-22 14:24:24 -0800538size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800539 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700540 auto it = mMetricsManagers.find(key);
541 if (it == mMetricsManagers.end()) {
542 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800543 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700544 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800545 return it->second->byteSize();
546}
547
Yao Chena80e5c02018-09-04 13:55:29 -0700548void StatsLogProcessor::dumpStates(int out, bool verbose) {
Yao Chen884c8c12018-01-26 10:36:25 -0800549 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chena80e5c02018-09-04 13:55:29 -0700550 FILE* fout = fdopen(out, "w");
551 if (fout == NULL) {
552 return;
Yao Chen884c8c12018-01-26 10:36:25 -0800553 }
Yao Chena80e5c02018-09-04 13:55:29 -0700554 fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
555 for (auto metricsManager : mMetricsManagers) {
556 metricsManager.second->dumpStates(fout, verbose);
557 }
558
559 fclose(fout);
Yao Chen884c8c12018-01-26 10:36:25 -0800560}
561
yro4beccbe2018-03-15 19:42:05 -0700562/*
Bookatz9cc7b662018-11-06 10:39:21 -0800563 * onDumpReport dumps serialized ConfigMetricsReportList into proto.
yro4beccbe2018-03-15 19:42:05 -0700564 */
Yangster-macb142cc82018-03-30 15:22:08 -0700565void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700566 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700567 const bool erase_data,
Chenjie Yue36018b2018-04-16 15:18:30 -0700568 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000569 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700570 ProtoOutputStream* proto) {
Yangster-macb0d06282018-01-05 15:44:07 -0800571 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac20877162017-12-22 17:19:39 -0800572
yro947fbce2017-11-15 22:50:23 -0800573 // Start of ConfigKey.
Bookatzff71cad2018-09-20 17:17:49 -0700574 uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
575 proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
576 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
577 proto->end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800578 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800579
Yao Chen9a43b4f2019-04-10 10:43:20 -0700580 bool keepFile = false;
581 auto it = mMetricsManagers.find(key);
582 if (it != mMetricsManagers.end() && it->second->shouldPersistLocalHistory()) {
583 keepFile = true;
584 }
585
yro947fbce2017-11-15 22:50:23 -0800586 // Then, check stats-data directory to see there's any file containing
587 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700588 StorageManager::appendConfigMetricsReport(
589 key, proto, erase_data && !keepFile /* should remove file after appending it */,
590 dumpReportReason == ADB_DUMP /*if caller is adb*/);
yro947fbce2017-11-15 22:50:23 -0800591
Yangster-mace68f3a52018-04-04 00:01:43 -0700592 if (it != mMetricsManagers.end()) {
593 // This allows another broadcast to be sent within the rate-limit period if we get close to
594 // filling the buffer again soon.
595 mLastBroadcastTimes.erase(key);
596
Yao Chen9a43b4f2019-04-10 10:43:20 -0700597 vector<uint8_t> buffer;
598 onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket,
599 erase_data, dumpReportReason, dumpLatency,
600 false /* is this data going to be saved on disk */, &buffer);
601 proto->write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS,
602 reinterpret_cast<char*>(buffer.data()), buffer.size());
Yangster-mace68f3a52018-04-04 00:01:43 -0700603 } else {
604 ALOGW("Config source %s does not exist", key.ToString().c_str());
605 }
Bookatzff71cad2018-09-20 17:17:49 -0700606}
607
608/*
609 * onDumpReport dumps serialized ConfigMetricsReportList into outData.
610 */
611void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
612 const bool include_current_partial_bucket,
613 const bool erase_data,
614 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000615 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700616 vector<uint8_t>* outData) {
617 ProtoOutputStream proto;
618 onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000619 dumpReportReason, dumpLatency, &proto);
Yangster-mace68f3a52018-04-04 00:01:43 -0700620
David Chen1d7b0cd2017-11-15 14:20:04 -0800621 if (outData != nullptr) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700622 flushProtoToBuffer(proto, outData);
623 VLOG("output data size %zu", outData->size());
yro17adac92017-11-08 23:16:29 -0800624 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800625
Yangster-mace68f3a52018-04-04 00:01:43 -0700626 StatsdStats::getInstance().noteMetricsReportSent(key, proto.size());
Yao Chen729093d2017-10-16 10:33:26 -0700627}
628
yro4beccbe2018-03-15 19:42:05 -0700629/*
630 * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData.
631 */
Yao Chen9a43b4f2019-04-10 10:43:20 -0700632void StatsLogProcessor::onConfigMetricsReportLocked(
633 const ConfigKey& key, const int64_t dumpTimeStampNs,
634 const bool include_current_partial_bucket, const bool erase_data,
635 const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
636 const bool dataSavedOnDisk, vector<uint8_t>* buffer) {
yro4beccbe2018-03-15 19:42:05 -0700637 // We already checked whether key exists in mMetricsManagers in
638 // WriteDataToDisk.
639 auto it = mMetricsManagers.find(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700640 if (it == mMetricsManagers.end()) {
641 return;
642 }
yro4beccbe2018-03-15 19:42:05 -0700643 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
644 int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
645
Yangster-mac9def8e32018-04-17 13:55:51 -0700646 std::set<string> str_set;
647
Yao Chen9a43b4f2019-04-10 10:43:20 -0700648 ProtoOutputStream tempProto;
yro4beccbe2018-03-15 19:42:05 -0700649 // First, fill in ConfigMetricsReport using current data on memory, which
650 // starts from filling in StatsLogReport's.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700651 it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
652 dumpLatency, &str_set, &tempProto);
yro4beccbe2018-03-15 19:42:05 -0700653
David Chen9e6dbbd2018-05-07 17:52:29 -0700654 // Fill in UidMap if there is at least one metric to report.
655 // This skips the uid map if it's an empty config.
656 if (it->second->getNumMetrics() > 0) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700657 uint64_t uidMapToken = tempProto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP);
dwchen730403e2018-10-29 11:41:56 -0700658 mUidMap->appendUidMap(
659 dumpTimeStampNs, key, it->second->hashStringInReport() ? &str_set : nullptr,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700660 it->second->versionStringsInReport(), it->second->installerInReport(), &tempProto);
661 tempProto.end(uidMapToken);
David Chen9e6dbbd2018-05-07 17:52:29 -0700662 }
yro4beccbe2018-03-15 19:42:05 -0700663
664 // Fill in the timestamps.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700665 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
666 (long long)lastReportTimeNs);
667 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
668 (long long)dumpTimeStampNs);
669 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
670 (long long)lastReportWallClockNs);
671 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
672 (long long)getWallClockNs());
Chenjie Yue36018b2018-04-16 15:18:30 -0700673 // Dump report reason
Yao Chen9a43b4f2019-04-10 10:43:20 -0700674 tempProto.write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason);
Yangster-mac9def8e32018-04-17 13:55:51 -0700675
David Chen56ae0d92018-05-11 16:00:22 -0700676 for (const auto& str : str_set) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700677 tempProto.write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str);
678 }
679
680 flushProtoToBuffer(tempProto, buffer);
681
682 // save buffer to disk if needed
683 if (erase_data && !dataSavedOnDisk && it->second->shouldPersistLocalHistory()) {
684 VLOG("save history to disk");
685 string file_name = StorageManager::getDataHistoryFileName((long)getWallClockSec(),
686 key.GetUid(), key.GetId());
687 StorageManager::writeFile(file_name.c_str(), buffer->data(), buffer->size());
Yangster-mac9def8e32018-04-17 13:55:51 -0700688 }
Yangster-macb142cc82018-03-30 15:22:08 -0700689}
yro4beccbe2018-03-15 19:42:05 -0700690
Yao Chen163d2602018-04-10 10:39:53 -0700691void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs,
692 const std::vector<ConfigKey>& configs) {
693 for (const auto& key : configs) {
Yangster-macb142cc82018-03-30 15:22:08 -0700694 StatsdConfig config;
695 if (StorageManager::readConfigFromDisk(key, &config)) {
696 OnConfigUpdatedLocked(timestampNs, key, config);
697 StatsdStats::getInstance().noteConfigReset(key);
698 } else {
699 ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str());
700 auto it = mMetricsManagers.find(key);
701 if (it != mMetricsManagers.end()) {
702 it->second->refreshTtl(timestampNs);
703 }
704 }
705 }
yro4beccbe2018-03-15 19:42:05 -0700706}
707
Yao Chen163d2602018-04-10 10:39:53 -0700708void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) {
709 std::vector<ConfigKey> configKeysTtlExpired;
710 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
711 if (it->second != nullptr && !it->second->isInTtl(timestampNs)) {
712 configKeysTtlExpired.push_back(it->first);
713 }
714 }
715 if (configKeysTtlExpired.size() > 0) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000716 WriteDataToDiskLocked(CONFIG_RESET, NO_TIME_CONSTRAINTS);
Yao Chen163d2602018-04-10 10:39:53 -0700717 resetConfigsLocked(timestampNs, configKeysTtlExpired);
718 }
719}
720
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700721void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800722 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700723 auto it = mMetricsManagers.find(key);
724 if (it != mMetricsManagers.end()) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700725 WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000726 NO_TIME_CONSTRAINTS);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700727 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700728 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700729 }
Yao Chenb3561512017-11-21 18:07:17 -0800730 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800731
David Chen1d7b0cd2017-11-15 14:20:04 -0800732 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800733
Tej Singh6ede28b2019-01-29 17:06:54 -0800734 int uid = key.GetUid();
735 bool lastConfigForUid = true;
736 for (auto it : mMetricsManagers) {
737 if (it.first.GetUid() == uid) {
738 lastConfigForUid = false;
739 break;
740 }
741 }
742 if (lastConfigForUid) {
743 mLastActivationBroadcastTimes.erase(uid);
744 }
745
Chenjie Yufa22d652018-02-05 14:37:48 -0800746 if (mMetricsManagers.empty()) {
Chenjie Yue2219202018-06-08 10:07:51 -0700747 mPullerManager->ForceClearPullerCache();
Chenjie Yufa22d652018-02-05 14:37:48 -0800748 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700749}
750
Tej Singh480392f2019-10-23 15:53:46 -0700751void StatsLogProcessor::flushIfNecessaryLocked(const ConfigKey& key,
752 MetricsManager& metricsManager) {
753 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
David Chend9269e22017-12-05 13:43:51 -0800754 auto lastCheckTime = mLastByteSizeTimes.find(key);
755 if (lastCheckTime != mLastByteSizeTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700756 if (elapsedRealtimeNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800757 return;
758 }
759 }
760
761 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
762 size_t totalBytes = metricsManager.byteSize();
Tej Singh480392f2019-10-23 15:53:46 -0700763 mLastByteSizeTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700764 bool requestDump = false;
Tej Singh480392f2019-10-23 15:53:46 -0700765 if (totalBytes > StatsdStats::kMaxMetricsBytesPerConfig) {
766 // Too late. We need to start clearing data.
767 metricsManager.dropData(elapsedRealtimeNs);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700768 StatsdStats::getInstance().noteDataDropped(key, totalBytes);
David Chen12942952017-12-04 14:28:43 -0800769 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chen48944902018-05-03 10:29:11 -0700770 } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) ||
771 (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) {
772 // Request to send a broadcast if:
773 // 1. in memory data > threshold OR
774 // 2. config has old data report on disk.
775 requestDump = true;
776 }
777
778 if (requestDump) {
David Chend9269e22017-12-05 13:43:51 -0800779 // Send broadcast so that receivers can pull data.
780 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
781 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700782 if (elapsedRealtimeNs - lastBroadcastTime->second <
783 StatsdStats::kMinBroadcastPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800784 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800785 return;
786 }
787 }
David Chen48944902018-05-03 10:29:11 -0700788 if (mSendBroadcast(key)) {
789 mOnDiskDataConfigs.erase(key);
790 VLOG("StatsD triggered data fetch for %s", key.ToString().c_str());
Tej Singh480392f2019-10-23 15:53:46 -0700791 mLastBroadcastTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700792 StatsdStats::getInstance().noteBroadcastSent(key);
793 }
yro31eb67b2017-10-24 13:33:21 -0700794 }
795}
796
Chenjie Yue36018b2018-04-16 15:18:30 -0700797void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key,
Yangster-mac892f3d32018-05-02 14:16:48 -0700798 const int64_t timestampNs,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000799 const DumpReportReason dumpReportReason,
800 const DumpLatency dumpLatency) {
yro028091c2018-05-09 16:03:27 -0700801 if (mMetricsManagers.find(key) == mMetricsManagers.end() ||
802 !mMetricsManagers.find(key)->second->shouldWriteToDisk()) {
Yangster-mac892f3d32018-05-02 14:16:48 -0700803 return;
804 }
Yao Chen9a43b4f2019-04-10 10:43:20 -0700805 vector<uint8_t> buffer;
David Chen56ae0d92018-05-11 16:00:22 -0700806 onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700807 true /* erase_data */, dumpReportReason, dumpLatency, true,
808 &buffer);
809 string file_name =
810 StorageManager::getDataFileName((long)getWallClockSec(), key.GetUid(), key.GetId());
811 StorageManager::writeFile(file_name.c_str(), buffer.data(), buffer.size());
812
David Chen48944902018-05-03 10:29:11 -0700813 // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP.
814 mOnDiskDataConfigs.insert(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700815}
816
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700817void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) {
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800818 std::lock_guard<std::mutex> lock(mMetricsMutex);
jianjin996f40d2019-03-29 13:53:48 -0700819 const int64_t timeNs = getElapsedRealtimeNs();
820 // Do not write to disk if we already have in the last few seconds.
jianjin996f40d2019-03-29 13:53:48 -0700821 if (static_cast<unsigned long long> (timeNs) <
822 mLastActiveMetricsWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
823 ALOGI("Statsd skipping writing active metrics to disk. Already wrote data in last %d seconds",
824 WRITE_DATA_COOL_DOWN_SEC);
825 return;
826 }
827 mLastActiveMetricsWriteNs = timeNs;
828
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800829 ProtoOutputStream proto;
Tej Singhf53d4452019-05-09 18:17:59 -0700830 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, DEVICE_SHUTDOWN, &proto);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800831
832 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
833 StorageManager::deleteFile(file_name.c_str());
834 android::base::unique_fd fd(
835 open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR));
836 if (fd == -1) {
837 ALOGE("Attempt to write %s but failed", file_name.c_str());
838 return;
839 }
840 proto.flush(fd.get());
841}
842
Jeffrey Huangb8f54032020-03-23 13:42:42 -0700843void StatsLogProcessor::SaveMetadataToDisk(int64_t currentWallClockTimeNs,
844 int64_t systemElapsedTimeNs) {
845 std::lock_guard<std::mutex> lock(mMetricsMutex);
846 // Do not write to disk if we already have in the last few seconds.
847 if (static_cast<unsigned long long> (systemElapsedTimeNs) <
848 mLastMetadataWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
849 ALOGI("Statsd skipping writing metadata to disk. Already wrote data in last %d seconds",
850 WRITE_DATA_COOL_DOWN_SEC);
851 return;
852 }
853 mLastMetadataWriteNs = systemElapsedTimeNs;
854
855 metadata::StatsMetadataList metadataList;
856 WriteMetadataToProtoLocked(
857 currentWallClockTimeNs, systemElapsedTimeNs, &metadataList);
858
859 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
860 StorageManager::deleteFile(file_name.c_str());
861
862 if (metadataList.stats_metadata_size() == 0) {
863 // Skip the write if we have nothing to write.
864 return;
865 }
866
867 std::string data;
868 metadataList.SerializeToString(&data);
869 StorageManager::writeFile(file_name.c_str(), data.c_str(), data.size());
870}
871
872void StatsLogProcessor::WriteMetadataToProto(int64_t currentWallClockTimeNs,
873 int64_t systemElapsedTimeNs,
874 metadata::StatsMetadataList* metadataList) {
875 std::lock_guard<std::mutex> lock(mMetricsMutex);
876 WriteMetadataToProtoLocked(currentWallClockTimeNs, systemElapsedTimeNs, metadataList);
877}
878
879void StatsLogProcessor::WriteMetadataToProtoLocked(int64_t currentWallClockTimeNs,
880 int64_t systemElapsedTimeNs,
881 metadata::StatsMetadataList* metadataList) {
882 for (const auto& pair : mMetricsManagers) {
883 const sp<MetricsManager>& metricsManager = pair.second;
884 metadata::StatsMetadata* statsMetadata = metadataList->add_stats_metadata();
885 bool metadataWritten = metricsManager->writeMetadataToProto(currentWallClockTimeNs,
886 systemElapsedTimeNs, statsMetadata);
887 if (!metadataWritten) {
888 metadataList->mutable_stats_metadata()->RemoveLast();
889 }
890 }
891}
892
Jeffrey Huang475677e2020-03-30 19:52:07 -0700893void StatsLogProcessor::LoadMetadataFromDisk(int64_t currentWallClockTimeNs,
894 int64_t systemElapsedTimeNs) {
895 std::lock_guard<std::mutex> lock(mMetricsMutex);
896 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
897 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
898 if (-1 == fd) {
899 VLOG("Attempt to read %s but failed", file_name.c_str());
900 StorageManager::deleteFile(file_name.c_str());
901 return;
902 }
903 string content;
904 if (!android::base::ReadFdToString(fd, &content)) {
905 ALOGE("Attempt to read %s but failed", file_name.c_str());
906 close(fd);
907 StorageManager::deleteFile(file_name.c_str());
908 return;
909 }
910
911 close(fd);
912
913 metadata::StatsMetadataList statsMetadataList;
914 if (!statsMetadataList.ParseFromString(content)) {
915 ALOGE("Attempt to read %s but failed; failed to metadata", file_name.c_str());
916 StorageManager::deleteFile(file_name.c_str());
917 return;
918 }
919 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
920 StorageManager::deleteFile(file_name.c_str());
921}
922
923void StatsLogProcessor::SetMetadataState(const metadata::StatsMetadataList& statsMetadataList,
924 int64_t currentWallClockTimeNs,
925 int64_t systemElapsedTimeNs) {
926 std::lock_guard<std::mutex> lock(mMetricsMutex);
927 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
928}
929
930void StatsLogProcessor::SetMetadataStateLocked(
931 const metadata::StatsMetadataList& statsMetadataList,
932 int64_t currentWallClockTimeNs,
933 int64_t systemElapsedTimeNs) {
934 for (const metadata::StatsMetadata& metadata : statsMetadataList.stats_metadata()) {
935 ConfigKey key(metadata.config_key().uid(), metadata.config_key().config_id());
936 auto it = mMetricsManagers.find(key);
937 if (it == mMetricsManagers.end()) {
938 ALOGE("No config found for configKey %s", key.ToString().c_str());
939 continue;
940 }
941 VLOG("Setting metadata %s", key.ToString().c_str());
942 it->second->loadMetadata(metadata, currentWallClockTimeNs, systemElapsedTimeNs);
943 }
944 VLOG("Successfully loaded %d metadata.", statsMetadataList.stats_metadata_size());
945}
946
Tej Singhf53d4452019-05-09 18:17:59 -0700947void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream(
948 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
949 std::lock_guard<std::mutex> lock(mMetricsMutex);
950 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, reason, proto);
951}
952
953void StatsLogProcessor::WriteActiveConfigsToProtoOutputStreamLocked(
954 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
955 for (const auto& pair : mMetricsManagers) {
956 const sp<MetricsManager>& metricsManager = pair.second;
957 uint64_t configToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
958 FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG);
959 metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, reason, proto);
960 proto->end(configToken);
961 }
962}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700963void StatsLogProcessor::LoadActiveConfigsFromDisk() {
964 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800965 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
966 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700967 if (-1 == fd) {
968 VLOG("Attempt to read %s but failed", file_name.c_str());
969 StorageManager::deleteFile(file_name.c_str());
970 return;
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800971 }
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700972 string content;
973 if (!android::base::ReadFdToString(fd, &content)) {
974 ALOGE("Attempt to read %s but failed", file_name.c_str());
975 close(fd);
976 StorageManager::deleteFile(file_name.c_str());
977 return;
978 }
979
980 close(fd);
981
982 ActiveConfigList activeConfigList;
983 if (!activeConfigList.ParseFromString(content)) {
984 ALOGE("Attempt to read %s but failed; failed to load active configs", file_name.c_str());
985 StorageManager::deleteFile(file_name.c_str());
986 return;
987 }
Tej Singhf53d4452019-05-09 18:17:59 -0700988 // Passing in mTimeBaseNs only works as long as we only load from disk is when statsd starts.
989 SetConfigsActiveStateLocked(activeConfigList, mTimeBaseNs);
990 StorageManager::deleteFile(file_name.c_str());
991}
992
993void StatsLogProcessor::SetConfigsActiveState(const ActiveConfigList& activeConfigList,
994 int64_t currentTimeNs) {
995 std::lock_guard<std::mutex> lock(mMetricsMutex);
996 SetConfigsActiveStateLocked(activeConfigList, currentTimeNs);
997}
998
999void StatsLogProcessor::SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
1000 int64_t currentTimeNs) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001001 for (int i = 0; i < activeConfigList.config_size(); i++) {
1002 const auto& config = activeConfigList.config(i);
1003 ConfigKey key(config.uid(), config.id());
1004 auto it = mMetricsManagers.find(key);
1005 if (it == mMetricsManagers.end()) {
1006 ALOGE("No config found for config %s", key.ToString().c_str());
1007 continue;
1008 }
1009 VLOG("Setting active config %s", key.ToString().c_str());
Tej Singhf53d4452019-05-09 18:17:59 -07001010 it->second->loadActiveConfig(config, currentTimeNs);
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001011 }
1012 VLOG("Successfully loaded %d active configs.", activeConfigList.config_size());
Chenjie Yuc7939cb2019-02-04 17:25:45 -08001013}
1014
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001015void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
1016 const DumpLatency dumpLatency) {
Yangster-mac892f3d32018-05-02 14:16:48 -07001017 const int64_t timeNs = getElapsedRealtimeNs();
Tej Singh42f9e062018-11-09 10:01:00 -08001018 // Do not write to disk if we already have in the last few seconds.
1019 // This is to avoid overwriting files that would have the same name if we
1020 // write twice in the same second.
1021 if (static_cast<unsigned long long> (timeNs) <
1022 mLastWriteTimeNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
1023 ALOGI("Statsd skipping writing data to disk. Already wrote data in last %d seconds",
1024 WRITE_DATA_COOL_DOWN_SEC);
1025 return;
1026 }
1027 mLastWriteTimeNs = timeNs;
Yangster-mace68f3a52018-04-04 00:01:43 -07001028 for (auto& pair : mMetricsManagers) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001029 WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason, dumpLatency);
Yangster-mace68f3a52018-04-04 00:01:43 -07001030 }
1031}
1032
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001033void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001034 const DumpLatency dumpLatency) {
Yangster-macb0d06282018-01-05 15:44:07 -08001035 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001036 WriteDataToDiskLocked(dumpReportReason, dumpLatency);
yro947fbce2017-11-15 22:50:23 -08001037}
1038
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001039void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) {
Yangster6df5fcc2018-04-12 11:04:29 -07001040 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yue2219202018-06-08 10:07:51 -07001041 mPullerManager->OnAlarmFired(timestampNs);
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001042}
1043
David Chend37bc232018-04-12 18:05:11 -07001044int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) {
1045 auto it = mMetricsManagers.find(key);
1046 if (it == mMetricsManagers.end()) {
1047 return 0;
1048 } else {
1049 return it->second->getLastReportTimeNs();
1050 }
1051}
1052
Tej Singh9ec159a2019-11-14 11:59:48 -08001053void StatsLogProcessor::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk,
1054 const int uid, const int64_t version) {
1055 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singhe678cb72020-04-14 16:23:30 -07001056 VLOG("Received app upgrade");
1057 for (const auto& it : mMetricsManagers) {
Tej Singh9ec159a2019-11-14 11:59:48 -08001058 it.second->notifyAppUpgrade(eventTimeNs, apk, uid, version);
1059 }
1060}
1061
1062void StatsLogProcessor::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
1063 const int uid) {
1064 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singhe678cb72020-04-14 16:23:30 -07001065 VLOG("Received app removed");
1066 for (const auto& it : mMetricsManagers) {
Tej Singh9ec159a2019-11-14 11:59:48 -08001067 it.second->notifyAppRemoved(eventTimeNs, apk, uid);
1068 }
1069}
1070
1071void StatsLogProcessor::onUidMapReceived(const int64_t& eventTimeNs) {
1072 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singhe678cb72020-04-14 16:23:30 -07001073 VLOG("Received uid map");
1074 for (const auto& it : mMetricsManagers) {
Tej Singh9ec159a2019-11-14 11:59:48 -08001075 it.second->onUidMapReceived(eventTimeNs);
1076 }
1077}
1078
Tej Singhe678cb72020-04-14 16:23:30 -07001079void StatsLogProcessor::onStatsdInitCompleted(const int64_t& elapsedTimeNs) {
1080 std::lock_guard<std::mutex> lock(mMetricsMutex);
1081 VLOG("Received boot completed signal");
1082 for (const auto& it : mMetricsManagers) {
1083 it.second->onStatsdInitCompleted(elapsedTimeNs);
1084 }
1085}
1086
David Chen48944902018-05-03 10:29:11 -07001087void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) {
1088 std::lock_guard<std::mutex> lock(mMetricsMutex);
1089 mOnDiskDataConfigs.insert(key);
1090}
1091
Yao Chenef99c4f2017-09-22 16:26:54 -07001092} // namespace statsd
1093} // namespace os
1094} // namespace android