blob: 50fe47dfed03baf92133bdf3260adafc1689fba7 [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"
Muhammad Qureshic8e22662019-11-20 17:18:03 -080028#include "atoms_info.h"
Chenjie Yuc7939cb2019-02-04 17:25:45 -080029#include "external/StatsPullerManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080030#include "guardrail/StatsdStats.h"
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080031#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070032#include "metrics/CountMetricProducer.h"
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080033#include "StatsService.h"
tsaichristined21aacf2019-10-07 14:47:38 -070034#include "state/StateManager.h"
Chenjie Yuc7939cb2019-02-04 17:25:45 -080035#include "stats_log_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036#include "stats_util.h"
Jeffrey Huang74fc4352020-03-06 15:18:33 -080037#include "statslog_statsd.h"
yro947fbce2017-11-15 22:50:23 -080038#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070039
Yao Chenab273e22017-09-06 12:53:50 -070040using namespace android;
yro947fbce2017-11-15 22:50:23 -080041using android::base::StringPrintf;
yrob0378b02017-11-09 20:36:25 -080042using android::util::FIELD_COUNT_REPEATED;
yro17adac92017-11-08 23:16:29 -080043using android::util::FIELD_TYPE_BOOL;
44using android::util::FIELD_TYPE_FLOAT;
45using android::util::FIELD_TYPE_INT32;
46using android::util::FIELD_TYPE_INT64;
47using android::util::FIELD_TYPE_MESSAGE;
48using android::util::FIELD_TYPE_STRING;
49using android::util::ProtoOutputStream;
Yao Chen44cf27c2017-09-14 22:32:50 -070050using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070051
52namespace android {
53namespace os {
54namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070055
yro947fbce2017-11-15 22:50:23 -080056// for ConfigMetricsReportList
yro17adac92017-11-08 23:16:29 -080057const int FIELD_ID_CONFIG_KEY = 1;
yro947fbce2017-11-15 22:50:23 -080058const int FIELD_ID_REPORTS = 2;
yro17adac92017-11-08 23:16:29 -080059// for ConfigKey
60const int FIELD_ID_UID = 1;
Yangster-mac94e197c2018-01-02 16:03:03 -080061const int FIELD_ID_ID = 2;
yro947fbce2017-11-15 22:50:23 -080062// for ConfigMetricsReport
Yao Chen4c959cb2018-02-13 13:27:48 -080063// const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp
yro947fbce2017-11-15 22:50:23 -080064const int FIELD_ID_UID_MAP = 2;
Yangster-mac330af582018-02-08 15:24:38 -080065const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
66const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080067const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5;
68const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6;
Chenjie Yue36018b2018-04-16 15:18:30 -070069const int FIELD_ID_DUMP_REPORT_REASON = 8;
Yangster-mac9def8e32018-04-17 13:55:51 -070070const int FIELD_ID_STRINGS = 9;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080071
Muhammad Qureshi844694b2019-04-05 10:10:40 -070072// for ActiveConfigList
73const int FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG = 1;
Chenjie Yuc7939cb2019-02-04 17:25:45 -080074
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080075// for permissions checks
76constexpr const char* kPermissionDump = "android.permission.DUMP";
77constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS";
78
Yangster-macb142cc82018-03-30 15:22:08 -070079#define NS_PER_HOUR 3600 * NS_PER_SEC
yro947fbce2017-11-15 22:50:23 -080080
Chenjie Yuc7939cb2019-02-04 17:25:45 -080081#define STATS_ACTIVE_METRIC_DIR "/data/misc/stats-active-metric"
Jeffrey Huangb8f54032020-03-23 13:42:42 -070082#define STATS_METADATA_DIR "/data/misc/stats-metadata"
yro17adac92017-11-08 23:16:29 -080083
Tej Singh42f9e062018-11-09 10:01:00 -080084// Cool down period for writing data to disk to avoid overwriting files.
85#define WRITE_DATA_COOL_DOWN_SEC 5
86
yro31eb67b2017-10-24 13:33:21 -070087StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
Chenjie Yue2219202018-06-08 10:07:51 -070088 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -080089 const sp<AlarmMonitor>& anomalyAlarmMonitor,
90 const sp<AlarmMonitor>& periodicAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070091 const int64_t timeBaseNs,
Tej Singh6ede28b2019-01-29 17:06:54 -080092 const std::function<bool(const ConfigKey&)>& sendBroadcast,
93 const std::function<bool(
94 const int&, const vector<int64_t>&)>& activateBroadcast)
Chenjie Yu85ed8382017-12-14 16:48:54 -080095 : mUidMap(uidMap),
Chenjie Yue2219202018-06-08 10:07:51 -070096 mPullerManager(pullerManager),
Yangster-mac932ecec2018-02-01 10:23:52 -080097 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
98 mPeriodicAlarmMonitor(periodicAlarmMonitor),
Chenjie Yu85ed8382017-12-14 16:48:54 -080099 mSendBroadcast(sendBroadcast),
Tej Singh6ede28b2019-01-29 17:06:54 -0800100 mSendActivationBroadcast(activateBroadcast),
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700101 mTimeBaseNs(timeBaseNs),
Yao Chen163d2602018-04-10 10:39:53 -0700102 mLargestTimestampSeen(0),
103 mLastTimestampSeen(0) {
Chenjie Yue2219202018-06-08 10:07:51 -0700104 mPullerManager->ForceClearPullerCache();
Yao Chenab273e22017-09-06 12:53:50 -0700105}
106
Yao Chenef99c4f2017-09-22 16:26:54 -0700107StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -0700108}
109
Yao Chen9a43b4f2019-04-10 10:43:20 -0700110static void flushProtoToBuffer(ProtoOutputStream& proto, vector<uint8_t>* outData) {
111 outData->clear();
112 outData->resize(proto.size());
113 size_t pos = 0;
114 sp<android::util::ProtoReader> reader = proto.data();
115 while (reader->readBuffer() != NULL) {
116 size_t toRead = reader->currentToRead();
117 std::memcpy(&((*outData)[pos]), reader->readBuffer(), toRead);
118 pos += toRead;
119 reader->move(toRead);
120 }
121}
122
Yangster-mace2cd6d52017-11-09 20:38:30 -0800123void StatsLogProcessor::onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700124 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800125 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
Yangster-macb0d06282018-01-05 15:44:07 -0800126 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef22017-11-21 14:36:23 -0800127 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800128 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
129 }
130}
131void StatsLogProcessor::onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700132 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800133 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
134
135 std::lock_guard<std::mutex> lock(mMetricsMutex);
136 for (const auto& itr : mMetricsManagers) {
137 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800138 }
139}
140
Yangster-macd40053e2018-01-09 16:29:22 -0800141void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Muhammad Qureshi83aaa662020-04-21 12:51:36 -0700142 if (std::pair<int, int> indexRange; event->hasAttributionChain(&indexRange)) {
143 vector<FieldValue>* const fieldValues = event->getMutableValues();
144 for (int i = indexRange.first; i <= indexRange.second; i++) {
145 FieldValue& fieldValue = fieldValues->at(i);
146 if (isAttributionUidField(fieldValue)) {
147 const int hostUid = mUidMap->getHostUidOrSelf(fieldValue.mValue.int_value);
148 fieldValue.mValue.setInt(hostUid);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800149 }
Yangster-macd40053e2018-01-09 16:29:22 -0800150 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700151 } else {
Ruchir Rastogiffa34f052020-04-04 15:30:11 -0700152 int uidFieldIndex = event->getUidFieldIndex();
153 if (uidFieldIndex != -1) {
154 Value& value = (*event->getMutableValues())[uidFieldIndex].mValue;
155 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
156 value.setInt(hostUid);
Yao Chenc40a19d2018-03-15 16:48:25 -0700157 }
Yao Chen312e8982017-12-05 15:29:03 -0800158 }
Yangster-macd40053e2018-01-09 16:29:22 -0800159}
160
161void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
162 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
163 bool is_create = event.GetBool(3, &err);
164 auto parent_uid = int(event.GetLong(1, &err2));
165 auto isolated_uid = int(event.GetLong(2, &err3));
166 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
167 if (is_create) {
168 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
169 } else {
Bookatz3c648862018-05-25 13:32:43 -0700170 mUidMap->removeIsolatedUid(isolated_uid);
Yangster-macd40053e2018-01-09 16:29:22 -0800171 }
172 } else {
173 ALOGE("Failed to parse uid in the isolated uid change event.");
174 }
175}
176
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800177void StatsLogProcessor::onBinaryPushStateChangedEventLocked(LogEvent* event) {
178 pid_t pid = event->GetPid();
179 uid_t uid = event->GetUid();
180 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
181 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
182 return;
183 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800184 // The Get* functions don't modify the status on success, they only write in
185 // failure statuses, so we can use one status variable for all calls then
186 // check if it is no longer NO_ERROR.
187 status_t err = NO_ERROR;
188 InstallTrainInfo trainInfo;
189 trainInfo.trainName = string(event->GetString(1 /*train name field id*/, &err));
190 trainInfo.trainVersionCode = event->GetLong(2 /*train version field id*/, &err);
191 trainInfo.requiresStaging = event->GetBool(3 /*requires staging field id*/, &err);
192 trainInfo.rollbackEnabled = event->GetBool(4 /*rollback enabled field id*/, &err);
193 trainInfo.requiresLowLatencyMonitor =
194 event->GetBool(5 /*requires low latency monitor field id*/, &err);
195 trainInfo.status = int32_t(event->GetLong(6 /*state field id*/, &err));
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800196 std::vector<uint8_t> trainExperimentIdBytes =
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800197 event->GetStorage(7 /*experiment ids field id*/, &err);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800198 bool is_rollback = event->GetBool(10 /*is rollback field id*/, &err);
199
200 if (err != NO_ERROR) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800201 ALOGE("Failed to parse fields in binary push state changed log event");
202 return;
203 }
204 ExperimentIds trainExperimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800205 if (!trainExperimentIds.ParseFromArray(trainExperimentIdBytes.data(),
206 trainExperimentIdBytes.size())) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800207 ALOGE("Failed to parse experimentids in binary push state changed.");
208 return;
209 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800210 trainInfo.experimentIds = {trainExperimentIds.experiment_id().begin(),
211 trainExperimentIds.experiment_id().end()};
212
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800213 // Update the train info on disk and get any data the logevent is missing.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800214 getAndUpdateTrainInfoOnDisk(is_rollback, &trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800215
216 std::vector<uint8_t> trainExperimentIdProto;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800217 writeExperimentIdsToProto(trainInfo.experimentIds, &trainExperimentIdProto);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800218 int32_t userId = multiuser_get_user_id(uid);
219
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800220 event->updateValue(2 /*train version field id*/, trainInfo.trainVersionCode, LONG);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800221 event->updateValue(7 /*experiment ids field id*/, trainExperimentIdProto, STORAGE);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800222 event->updateValue(8 /*user id field id*/, userId, INT);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800223
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800224 // If this event is a rollback event, then the following bits in the event
225 // are invalid and we will need to update them with the values we pulled
226 // from disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800227 if (is_rollback) {
228 int bit = trainInfo.requiresStaging ? 1 : 0;
229 event->updateValue(3 /*requires staging field id*/, bit, INT);
230 bit = trainInfo.rollbackEnabled ? 1 : 0;
231 event->updateValue(4 /*rollback enabled field id*/, bit, INT);
232 bit = trainInfo.requiresLowLatencyMonitor ? 1 : 0;
233 event->updateValue(5 /*requires low latency monitor field id*/, bit, INT);
234 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800235}
236
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800237void StatsLogProcessor::getAndUpdateTrainInfoOnDisk(bool is_rollback,
238 InstallTrainInfo* trainInfo) {
239 // If the train name is empty, we don't know which train to attribute the
240 // event to, so return early.
241 if (trainInfo->trainName.empty()) {
242 return;
243 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800244 bool readTrainInfoSuccess = false;
245 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800246 readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfo->trainName, trainInfoOnDisk);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800247
248 bool resetExperimentIds = false;
249 if (readTrainInfoSuccess) {
250 // Keep the old train version if we received an empty version.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800251 if (trainInfo->trainVersionCode == -1) {
252 trainInfo->trainVersionCode = trainInfoOnDisk.trainVersionCode;
253 } else if (trainInfo->trainVersionCode != trainInfoOnDisk.trainVersionCode) {
254 // Reset experiment ids if we receive a new non-empty train version.
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800255 resetExperimentIds = true;
256 }
257
258 // Reset if we received a different experiment id.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800259 if (!trainInfo->experimentIds.empty() &&
260 (trainInfoOnDisk.experimentIds.empty() ||
261 trainInfo->experimentIds.at(0) != trainInfoOnDisk.experimentIds[0])) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800262 resetExperimentIds = true;
263 }
264 }
265
266 // Find the right experiment IDs
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800267 if ((!resetExperimentIds || is_rollback) && readTrainInfoSuccess) {
268 trainInfo->experimentIds = trainInfoOnDisk.experimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800269 }
270
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800271 if (!trainInfo->experimentIds.empty()) {
272 int64_t firstId = trainInfo->experimentIds.at(0);
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800273 auto& ids = trainInfo->experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800274 switch (trainInfo->status) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800275 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALL_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800276 if (find(ids.begin(), ids.end(), firstId + 1) == ids.end()) {
277 ids.push_back(firstId + 1);
278 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800279 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800280 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_INITIATED:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800281 if (find(ids.begin(), ids.end(), firstId + 2) == ids.end()) {
282 ids.push_back(firstId + 2);
283 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800284 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800285 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800286 if (find(ids.begin(), ids.end(), firstId + 3) == ids.end()) {
287 ids.push_back(firstId + 3);
288 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800289 break;
290 }
291 }
292
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800293 // If this event is a rollback event, the following fields are invalid and
294 // need to be replaced by the fields stored to disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800295 if (is_rollback) {
296 trainInfo->requiresStaging = trainInfoOnDisk.requiresStaging;
297 trainInfo->rollbackEnabled = trainInfoOnDisk.rollbackEnabled;
298 trainInfo->requiresLowLatencyMonitor = trainInfoOnDisk.requiresLowLatencyMonitor;
299 }
300
301 StorageManager::writeTrainInfo(*trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800302}
303
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800304void StatsLogProcessor::onWatchdogRollbackOccurredLocked(LogEvent* event) {
305 pid_t pid = event->GetPid();
306 uid_t uid = event->GetUid();
307 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
308 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
309 return;
310 }
311 // The Get* functions don't modify the status on success, they only write in
312 // failure statuses, so we can use one status variable for all calls then
313 // check if it is no longer NO_ERROR.
314 status_t err = NO_ERROR;
315 int32_t rollbackType = int32_t(event->GetInt(1 /*rollback type field id*/, &err));
316 string packageName = string(event->GetString(2 /*package name field id*/, &err));
317
318 if (err != NO_ERROR) {
319 ALOGE("Failed to parse fields in watchdog rollback occurred log event");
320 return;
321 }
322
323 vector<int64_t> experimentIds =
324 processWatchdogRollbackOccurred(rollbackType, packageName);
325 vector<uint8_t> experimentIdProto;
326 writeExperimentIdsToProto(experimentIds, &experimentIdProto);
327
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800328 event->updateValue(6 /*experiment ids field id*/, experimentIdProto, STORAGE);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800329}
330
331vector<int64_t> StatsLogProcessor::processWatchdogRollbackOccurred(const int32_t rollbackTypeIn,
332 const string& packageNameIn) {
333 // If the package name is empty, we can't attribute it to any train, so
334 // return early.
335 if (packageNameIn.empty()) {
336 return vector<int64_t>();
337 }
338 bool readTrainInfoSuccess = false;
339 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800340 // We use the package name of the event as the train name.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800341 readTrainInfoSuccess = StorageManager::readTrainInfo(packageNameIn, trainInfoOnDisk);
342
343 if (!readTrainInfoSuccess) {
344 return vector<int64_t>();
345 }
346
347 if (trainInfoOnDisk.experimentIds.empty()) {
348 return vector<int64_t>();
349 }
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800350
351 int64_t firstId = trainInfoOnDisk.experimentIds[0];
352 auto& ids = trainInfoOnDisk.experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800353 switch (rollbackTypeIn) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800354 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_INITIATE:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800355 if (find(ids.begin(), ids.end(), firstId + 4) == ids.end()) {
356 ids.push_back(firstId + 4);
357 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800358 StorageManager::writeTrainInfo(trainInfoOnDisk);
359 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800360 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800361 if (find(ids.begin(), ids.end(), firstId + 5) == ids.end()) {
362 ids.push_back(firstId + 5);
363 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800364 StorageManager::writeTrainInfo(trainInfoOnDisk);
365 break;
366 }
367
368 return trainInfoOnDisk.experimentIds;
369}
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800370
Yangster-mac892f3d32018-05-02 14:16:48 -0700371void StatsLogProcessor::resetConfigs() {
372 std::lock_guard<std::mutex> lock(mMetricsMutex);
373 resetConfigsLocked(getElapsedRealtimeNs());
374}
375
376void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) {
377 std::vector<ConfigKey> configKeys;
378 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
379 configKeys.push_back(it->first);
380 }
381 resetConfigsLocked(timestampNs, configKeys);
382}
383
Yao Chen3ff3a492018-08-06 16:17:37 -0700384void StatsLogProcessor::OnLogEvent(LogEvent* event) {
Tej Singh480392f2019-10-23 15:53:46 -0700385 OnLogEvent(event, getElapsedRealtimeNs());
386}
387
388void StatsLogProcessor::OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs) {
Yangster-macd40053e2018-01-09 16:29:22 -0800389 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen876889c2018-05-02 11:16:16 -0700390
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800391 // Hard-coded logic to update train info on disk and fill in any information
392 // this log event may be missing.
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800393 if (event->GetTagId() == android::os::statsd::util::BINARY_PUSH_STATE_CHANGED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800394 onBinaryPushStateChangedEventLocked(event);
395 }
396
397 // Hard-coded logic to update experiment ids on disk for certain rollback
398 // types and fill the rollback atom with experiment ids
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800399 if (event->GetTagId() == android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800400 onWatchdogRollbackOccurredLocked(event);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800401 }
402
Yao Chen876889c2018-05-02 11:16:16 -0700403#ifdef VERY_VERBOSE_PRINTING
404 if (mPrintAllLogs) {
405 ALOGI("%s", event->ToString().c_str());
406 }
407#endif
Tej Singh480392f2019-10-23 15:53:46 -0700408 const int64_t eventElapsedTimeNs = event->GetElapsedTimestampNs();
Yangster-macb8382a12018-04-04 10:39:12 -0700409
Tej Singh480392f2019-10-23 15:53:46 -0700410 resetIfConfigTtlExpiredLocked(eventElapsedTimeNs);
Yangster-macb142cc82018-03-30 15:22:08 -0700411
Yangster-macd40053e2018-01-09 16:29:22 -0800412 StatsdStats::getInstance().noteAtomLogged(
Yangster-mac330af582018-02-08 15:24:38 -0800413 event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC);
Yangster-macd40053e2018-01-09 16:29:22 -0800414
David Chen21582962017-11-01 17:32:46 -0700415 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800416 // The field numbers need to be currently updated by hand with atoms.proto
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800417 if (event->GetTagId() == android::os::statsd::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800418 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800419 }
420
tsaichristined21aacf2019-10-07 14:47:38 -0700421 StateManager::getInstance().onLogEvent(*event);
422
David Chencfc311d2018-01-23 17:55:54 -0800423 if (mMetricsManagers.empty()) {
424 return;
425 }
426
Yangster-macb142cc82018-03-30 15:22:08 -0700427 int64_t curTimeSec = getElapsedRealtimeSec();
Yangster-mac330af582018-02-08 15:24:38 -0800428 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
Chenjie Yue2219202018-06-08 10:07:51 -0700429 mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC);
Yangster-mac330af582018-02-08 15:24:38 -0800430 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800431 }
432
Yangster-macb142cc82018-03-30 15:22:08 -0700433
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800434 if (event->GetTagId() != android::os::statsd::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800435 // Map the isolated uid to host uid if necessary.
436 mapIsolatedUidToHostUidIfNecessaryLocked(event);
437 }
438
Tej Singh6ede28b2019-01-29 17:06:54 -0800439 std::unordered_set<int> uidsWithActiveConfigsChanged;
440 std::unordered_map<int, std::vector<int64_t>> activeConfigsPerUid;
Yangster-macd40053e2018-01-09 16:29:22 -0800441 // pass the event to metrics managers.
442 for (auto& pair : mMetricsManagers) {
Tej Singh6ede28b2019-01-29 17:06:54 -0800443 int uid = pair.first.GetUid();
444 int64_t configId = pair.first.GetId();
445 bool isPrevActive = pair.second->isActive();
Yangster-macd40053e2018-01-09 16:29:22 -0800446 pair.second->onLogEvent(*event);
Tej Singh6ede28b2019-01-29 17:06:54 -0800447 bool isCurActive = pair.second->isActive();
448 // Map all active configs by uid.
449 if (isCurActive) {
450 auto activeConfigs = activeConfigsPerUid.find(uid);
451 if (activeConfigs != activeConfigsPerUid.end()) {
452 activeConfigs->second.push_back(configId);
453 } else {
454 vector<int64_t> newActiveConfigs;
455 newActiveConfigs.push_back(configId);
456 activeConfigsPerUid[uid] = newActiveConfigs;
457 }
458 }
459 // The activation state of this config changed.
460 if (isPrevActive != isCurActive) {
461 VLOG("Active status changed for uid %d", uid);
462 uidsWithActiveConfigsChanged.insert(uid);
463 StatsdStats::getInstance().noteActiveStatusChanged(pair.first, isCurActive);
464 }
Tej Singh480392f2019-10-23 15:53:46 -0700465 flushIfNecessaryLocked(pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700466 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800467
Tej Singh480392f2019-10-23 15:53:46 -0700468 // Don't use the event timestamp for the guardrail.
Tej Singh6ede28b2019-01-29 17:06:54 -0800469 for (int uid : uidsWithActiveConfigsChanged) {
470 // Send broadcast so that receivers can pull data.
471 auto lastBroadcastTime = mLastActivationBroadcastTimes.find(uid);
472 if (lastBroadcastTime != mLastActivationBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700473 if (elapsedRealtimeNs - lastBroadcastTime->second <
474 StatsdStats::kMinActivationBroadcastPeriodNs) {
Tej Singh16ca28f2019-06-24 11:58:23 -0700475 StatsdStats::getInstance().noteActivationBroadcastGuardrailHit(uid);
Tej Singh6ede28b2019-01-29 17:06:54 -0800476 VLOG("StatsD would've sent an activation broadcast but the rate limit stopped us.");
477 return;
478 }
479 }
480 auto activeConfigs = activeConfigsPerUid.find(uid);
481 if (activeConfigs != activeConfigsPerUid.end()) {
482 if (mSendActivationBroadcast(uid, activeConfigs->second)) {
483 VLOG("StatsD sent activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700484 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800485 }
486 } else {
487 std::vector<int64_t> emptyActiveConfigs;
488 if (mSendActivationBroadcast(uid, emptyActiveConfigs)) {
489 VLOG("StatsD sent EMPTY activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700490 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800491 }
492 }
493 }
494}
495
496void StatsLogProcessor::GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs) {
497 std::lock_guard<std::mutex> lock(mMetricsMutex);
498 GetActiveConfigsLocked(uid, outActiveConfigs);
499}
500
501void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs) {
502 outActiveConfigs.clear();
503 for (auto& pair : mMetricsManagers) {
504 if (pair.first.GetUid() == uid && pair.second->isActive()) {
505 outActiveConfigs.push_back(pair.first.GetId());
506 }
507 }
Yao Chenab273e22017-09-06 12:53:50 -0700508}
509
Yangster-macc04feba2018-04-02 14:37:33 -0700510void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
511 const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800512 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000513 WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED, NO_TIME_CONSTRAINTS);
Yangster-macb142cc82018-03-30 15:22:08 -0700514 OnConfigUpdatedLocked(timestampNs, key, config);
515}
516
517void StatsLogProcessor::OnConfigUpdatedLocked(
518 const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) {
Tej Singh484524a2018-02-01 15:10:05 -0800519 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800520 sp<MetricsManager> newMetricsManager =
Chenjie Yue2219202018-06-08 10:07:51 -0700521 new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager,
522 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Yao Chencaf339d2017-10-06 16:01:10 -0700523 if (newMetricsManager->isConfigValid()) {
Tej Singh3be093b2020-03-04 20:08:38 -0800524 newMetricsManager->init();
David Chend6896892017-10-25 11:49:03 -0700525 mUidMap->OnConfigUpdated(key);
Yangster-macb142cc82018-03-30 15:22:08 -0700526 newMetricsManager->refreshTtl(timestampNs);
Yao Chend10f7b12017-12-18 12:53:50 -0800527 mMetricsManagers[key] = newMetricsManager;
Yao Chenb3561512017-11-21 18:07:17 -0800528 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700529 } else {
530 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800531 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700532 }
yro00698da2017-09-15 10:06:40 -0700533}
Bookatz906a35c2017-09-20 15:26:44 -0700534
Yangster7c334a12017-11-22 14:24:24 -0800535size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800536 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700537 auto it = mMetricsManagers.find(key);
538 if (it == mMetricsManagers.end()) {
539 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800540 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700541 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800542 return it->second->byteSize();
543}
544
Yao Chena80e5c02018-09-04 13:55:29 -0700545void StatsLogProcessor::dumpStates(int out, bool verbose) {
Yao Chen884c8c12018-01-26 10:36:25 -0800546 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chena80e5c02018-09-04 13:55:29 -0700547 FILE* fout = fdopen(out, "w");
548 if (fout == NULL) {
549 return;
Yao Chen884c8c12018-01-26 10:36:25 -0800550 }
Yao Chena80e5c02018-09-04 13:55:29 -0700551 fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
552 for (auto metricsManager : mMetricsManagers) {
553 metricsManager.second->dumpStates(fout, verbose);
554 }
555
556 fclose(fout);
Yao Chen884c8c12018-01-26 10:36:25 -0800557}
558
yro4beccbe2018-03-15 19:42:05 -0700559/*
Bookatz9cc7b662018-11-06 10:39:21 -0800560 * onDumpReport dumps serialized ConfigMetricsReportList into proto.
yro4beccbe2018-03-15 19:42:05 -0700561 */
Yangster-macb142cc82018-03-30 15:22:08 -0700562void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700563 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700564 const bool erase_data,
Chenjie Yue36018b2018-04-16 15:18:30 -0700565 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000566 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700567 ProtoOutputStream* proto) {
Yangster-macb0d06282018-01-05 15:44:07 -0800568 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac20877162017-12-22 17:19:39 -0800569
yro947fbce2017-11-15 22:50:23 -0800570 // Start of ConfigKey.
Bookatzff71cad2018-09-20 17:17:49 -0700571 uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
572 proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
573 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
574 proto->end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800575 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800576
Yao Chen9a43b4f2019-04-10 10:43:20 -0700577 bool keepFile = false;
578 auto it = mMetricsManagers.find(key);
579 if (it != mMetricsManagers.end() && it->second->shouldPersistLocalHistory()) {
580 keepFile = true;
581 }
582
yro947fbce2017-11-15 22:50:23 -0800583 // Then, check stats-data directory to see there's any file containing
584 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700585 StorageManager::appendConfigMetricsReport(
586 key, proto, erase_data && !keepFile /* should remove file after appending it */,
587 dumpReportReason == ADB_DUMP /*if caller is adb*/);
yro947fbce2017-11-15 22:50:23 -0800588
Yangster-mace68f3a52018-04-04 00:01:43 -0700589 if (it != mMetricsManagers.end()) {
590 // This allows another broadcast to be sent within the rate-limit period if we get close to
591 // filling the buffer again soon.
592 mLastBroadcastTimes.erase(key);
593
Yao Chen9a43b4f2019-04-10 10:43:20 -0700594 vector<uint8_t> buffer;
595 onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket,
596 erase_data, dumpReportReason, dumpLatency,
597 false /* is this data going to be saved on disk */, &buffer);
598 proto->write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS,
599 reinterpret_cast<char*>(buffer.data()), buffer.size());
Yangster-mace68f3a52018-04-04 00:01:43 -0700600 } else {
601 ALOGW("Config source %s does not exist", key.ToString().c_str());
602 }
Bookatzff71cad2018-09-20 17:17:49 -0700603}
604
605/*
606 * onDumpReport dumps serialized ConfigMetricsReportList into outData.
607 */
608void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
609 const bool include_current_partial_bucket,
610 const bool erase_data,
611 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000612 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700613 vector<uint8_t>* outData) {
614 ProtoOutputStream proto;
615 onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000616 dumpReportReason, dumpLatency, &proto);
Yangster-mace68f3a52018-04-04 00:01:43 -0700617
David Chen1d7b0cd2017-11-15 14:20:04 -0800618 if (outData != nullptr) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700619 flushProtoToBuffer(proto, outData);
620 VLOG("output data size %zu", outData->size());
yro17adac92017-11-08 23:16:29 -0800621 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800622
Yangster-mace68f3a52018-04-04 00:01:43 -0700623 StatsdStats::getInstance().noteMetricsReportSent(key, proto.size());
Yao Chen729093d2017-10-16 10:33:26 -0700624}
625
yro4beccbe2018-03-15 19:42:05 -0700626/*
627 * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData.
628 */
Yao Chen9a43b4f2019-04-10 10:43:20 -0700629void StatsLogProcessor::onConfigMetricsReportLocked(
630 const ConfigKey& key, const int64_t dumpTimeStampNs,
631 const bool include_current_partial_bucket, const bool erase_data,
632 const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
633 const bool dataSavedOnDisk, vector<uint8_t>* buffer) {
yro4beccbe2018-03-15 19:42:05 -0700634 // We already checked whether key exists in mMetricsManagers in
635 // WriteDataToDisk.
636 auto it = mMetricsManagers.find(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700637 if (it == mMetricsManagers.end()) {
638 return;
639 }
yro4beccbe2018-03-15 19:42:05 -0700640 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
641 int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
642
Yangster-mac9def8e32018-04-17 13:55:51 -0700643 std::set<string> str_set;
644
Yao Chen9a43b4f2019-04-10 10:43:20 -0700645 ProtoOutputStream tempProto;
yro4beccbe2018-03-15 19:42:05 -0700646 // First, fill in ConfigMetricsReport using current data on memory, which
647 // starts from filling in StatsLogReport's.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700648 it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
649 dumpLatency, &str_set, &tempProto);
yro4beccbe2018-03-15 19:42:05 -0700650
David Chen9e6dbbd2018-05-07 17:52:29 -0700651 // Fill in UidMap if there is at least one metric to report.
652 // This skips the uid map if it's an empty config.
653 if (it->second->getNumMetrics() > 0) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700654 uint64_t uidMapToken = tempProto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP);
dwchen730403e2018-10-29 11:41:56 -0700655 mUidMap->appendUidMap(
656 dumpTimeStampNs, key, it->second->hashStringInReport() ? &str_set : nullptr,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700657 it->second->versionStringsInReport(), it->second->installerInReport(), &tempProto);
658 tempProto.end(uidMapToken);
David Chen9e6dbbd2018-05-07 17:52:29 -0700659 }
yro4beccbe2018-03-15 19:42:05 -0700660
661 // Fill in the timestamps.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700662 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
663 (long long)lastReportTimeNs);
664 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
665 (long long)dumpTimeStampNs);
666 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
667 (long long)lastReportWallClockNs);
668 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
669 (long long)getWallClockNs());
Chenjie Yue36018b2018-04-16 15:18:30 -0700670 // Dump report reason
Yao Chen9a43b4f2019-04-10 10:43:20 -0700671 tempProto.write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason);
Yangster-mac9def8e32018-04-17 13:55:51 -0700672
David Chen56ae0d92018-05-11 16:00:22 -0700673 for (const auto& str : str_set) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700674 tempProto.write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str);
675 }
676
677 flushProtoToBuffer(tempProto, buffer);
678
679 // save buffer to disk if needed
680 if (erase_data && !dataSavedOnDisk && it->second->shouldPersistLocalHistory()) {
681 VLOG("save history to disk");
682 string file_name = StorageManager::getDataHistoryFileName((long)getWallClockSec(),
683 key.GetUid(), key.GetId());
684 StorageManager::writeFile(file_name.c_str(), buffer->data(), buffer->size());
Yangster-mac9def8e32018-04-17 13:55:51 -0700685 }
Yangster-macb142cc82018-03-30 15:22:08 -0700686}
yro4beccbe2018-03-15 19:42:05 -0700687
Yao Chen163d2602018-04-10 10:39:53 -0700688void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs,
689 const std::vector<ConfigKey>& configs) {
690 for (const auto& key : configs) {
Yangster-macb142cc82018-03-30 15:22:08 -0700691 StatsdConfig config;
692 if (StorageManager::readConfigFromDisk(key, &config)) {
693 OnConfigUpdatedLocked(timestampNs, key, config);
694 StatsdStats::getInstance().noteConfigReset(key);
695 } else {
696 ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str());
697 auto it = mMetricsManagers.find(key);
698 if (it != mMetricsManagers.end()) {
699 it->second->refreshTtl(timestampNs);
700 }
701 }
702 }
yro4beccbe2018-03-15 19:42:05 -0700703}
704
Yao Chen163d2602018-04-10 10:39:53 -0700705void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) {
706 std::vector<ConfigKey> configKeysTtlExpired;
707 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
708 if (it->second != nullptr && !it->second->isInTtl(timestampNs)) {
709 configKeysTtlExpired.push_back(it->first);
710 }
711 }
712 if (configKeysTtlExpired.size() > 0) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000713 WriteDataToDiskLocked(CONFIG_RESET, NO_TIME_CONSTRAINTS);
Yao Chen163d2602018-04-10 10:39:53 -0700714 resetConfigsLocked(timestampNs, configKeysTtlExpired);
715 }
716}
717
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700718void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800719 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700720 auto it = mMetricsManagers.find(key);
721 if (it != mMetricsManagers.end()) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700722 WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000723 NO_TIME_CONSTRAINTS);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700724 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700725 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700726 }
Yao Chenb3561512017-11-21 18:07:17 -0800727 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800728
David Chen1d7b0cd2017-11-15 14:20:04 -0800729 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800730
Tej Singh6ede28b2019-01-29 17:06:54 -0800731 int uid = key.GetUid();
732 bool lastConfigForUid = true;
733 for (auto it : mMetricsManagers) {
734 if (it.first.GetUid() == uid) {
735 lastConfigForUid = false;
736 break;
737 }
738 }
739 if (lastConfigForUid) {
740 mLastActivationBroadcastTimes.erase(uid);
741 }
742
Chenjie Yufa22d652018-02-05 14:37:48 -0800743 if (mMetricsManagers.empty()) {
Chenjie Yue2219202018-06-08 10:07:51 -0700744 mPullerManager->ForceClearPullerCache();
Chenjie Yufa22d652018-02-05 14:37:48 -0800745 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700746}
747
Tej Singh480392f2019-10-23 15:53:46 -0700748void StatsLogProcessor::flushIfNecessaryLocked(const ConfigKey& key,
749 MetricsManager& metricsManager) {
750 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
David Chend9269e22017-12-05 13:43:51 -0800751 auto lastCheckTime = mLastByteSizeTimes.find(key);
752 if (lastCheckTime != mLastByteSizeTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700753 if (elapsedRealtimeNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800754 return;
755 }
756 }
757
758 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
759 size_t totalBytes = metricsManager.byteSize();
Tej Singh480392f2019-10-23 15:53:46 -0700760 mLastByteSizeTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700761 bool requestDump = false;
Tej Singh480392f2019-10-23 15:53:46 -0700762 if (totalBytes > StatsdStats::kMaxMetricsBytesPerConfig) {
763 // Too late. We need to start clearing data.
764 metricsManager.dropData(elapsedRealtimeNs);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700765 StatsdStats::getInstance().noteDataDropped(key, totalBytes);
David Chen12942952017-12-04 14:28:43 -0800766 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chen48944902018-05-03 10:29:11 -0700767 } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) ||
768 (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) {
769 // Request to send a broadcast if:
770 // 1. in memory data > threshold OR
771 // 2. config has old data report on disk.
772 requestDump = true;
773 }
774
775 if (requestDump) {
David Chend9269e22017-12-05 13:43:51 -0800776 // Send broadcast so that receivers can pull data.
777 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
778 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700779 if (elapsedRealtimeNs - lastBroadcastTime->second <
780 StatsdStats::kMinBroadcastPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800781 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800782 return;
783 }
784 }
David Chen48944902018-05-03 10:29:11 -0700785 if (mSendBroadcast(key)) {
786 mOnDiskDataConfigs.erase(key);
787 VLOG("StatsD triggered data fetch for %s", key.ToString().c_str());
Tej Singh480392f2019-10-23 15:53:46 -0700788 mLastBroadcastTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700789 StatsdStats::getInstance().noteBroadcastSent(key);
790 }
yro31eb67b2017-10-24 13:33:21 -0700791 }
792}
793
Chenjie Yue36018b2018-04-16 15:18:30 -0700794void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key,
Yangster-mac892f3d32018-05-02 14:16:48 -0700795 const int64_t timestampNs,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000796 const DumpReportReason dumpReportReason,
797 const DumpLatency dumpLatency) {
yro028091c2018-05-09 16:03:27 -0700798 if (mMetricsManagers.find(key) == mMetricsManagers.end() ||
799 !mMetricsManagers.find(key)->second->shouldWriteToDisk()) {
Yangster-mac892f3d32018-05-02 14:16:48 -0700800 return;
801 }
Yao Chen9a43b4f2019-04-10 10:43:20 -0700802 vector<uint8_t> buffer;
David Chen56ae0d92018-05-11 16:00:22 -0700803 onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700804 true /* erase_data */, dumpReportReason, dumpLatency, true,
805 &buffer);
806 string file_name =
807 StorageManager::getDataFileName((long)getWallClockSec(), key.GetUid(), key.GetId());
808 StorageManager::writeFile(file_name.c_str(), buffer.data(), buffer.size());
809
David Chen48944902018-05-03 10:29:11 -0700810 // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP.
811 mOnDiskDataConfigs.insert(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700812}
813
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700814void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) {
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800815 std::lock_guard<std::mutex> lock(mMetricsMutex);
jianjin996f40d2019-03-29 13:53:48 -0700816 const int64_t timeNs = getElapsedRealtimeNs();
817 // Do not write to disk if we already have in the last few seconds.
jianjin996f40d2019-03-29 13:53:48 -0700818 if (static_cast<unsigned long long> (timeNs) <
819 mLastActiveMetricsWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
820 ALOGI("Statsd skipping writing active metrics to disk. Already wrote data in last %d seconds",
821 WRITE_DATA_COOL_DOWN_SEC);
822 return;
823 }
824 mLastActiveMetricsWriteNs = timeNs;
825
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800826 ProtoOutputStream proto;
Tej Singhf53d4452019-05-09 18:17:59 -0700827 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, DEVICE_SHUTDOWN, &proto);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800828
829 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
830 StorageManager::deleteFile(file_name.c_str());
831 android::base::unique_fd fd(
832 open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR));
833 if (fd == -1) {
834 ALOGE("Attempt to write %s but failed", file_name.c_str());
835 return;
836 }
837 proto.flush(fd.get());
838}
839
Jeffrey Huangb8f54032020-03-23 13:42:42 -0700840void StatsLogProcessor::SaveMetadataToDisk(int64_t currentWallClockTimeNs,
841 int64_t systemElapsedTimeNs) {
842 std::lock_guard<std::mutex> lock(mMetricsMutex);
843 // Do not write to disk if we already have in the last few seconds.
844 if (static_cast<unsigned long long> (systemElapsedTimeNs) <
845 mLastMetadataWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
846 ALOGI("Statsd skipping writing metadata to disk. Already wrote data in last %d seconds",
847 WRITE_DATA_COOL_DOWN_SEC);
848 return;
849 }
850 mLastMetadataWriteNs = systemElapsedTimeNs;
851
852 metadata::StatsMetadataList metadataList;
853 WriteMetadataToProtoLocked(
854 currentWallClockTimeNs, systemElapsedTimeNs, &metadataList);
855
856 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
857 StorageManager::deleteFile(file_name.c_str());
858
859 if (metadataList.stats_metadata_size() == 0) {
860 // Skip the write if we have nothing to write.
861 return;
862 }
863
864 std::string data;
865 metadataList.SerializeToString(&data);
866 StorageManager::writeFile(file_name.c_str(), data.c_str(), data.size());
867}
868
869void StatsLogProcessor::WriteMetadataToProto(int64_t currentWallClockTimeNs,
870 int64_t systemElapsedTimeNs,
871 metadata::StatsMetadataList* metadataList) {
872 std::lock_guard<std::mutex> lock(mMetricsMutex);
873 WriteMetadataToProtoLocked(currentWallClockTimeNs, systemElapsedTimeNs, metadataList);
874}
875
876void StatsLogProcessor::WriteMetadataToProtoLocked(int64_t currentWallClockTimeNs,
877 int64_t systemElapsedTimeNs,
878 metadata::StatsMetadataList* metadataList) {
879 for (const auto& pair : mMetricsManagers) {
880 const sp<MetricsManager>& metricsManager = pair.second;
881 metadata::StatsMetadata* statsMetadata = metadataList->add_stats_metadata();
882 bool metadataWritten = metricsManager->writeMetadataToProto(currentWallClockTimeNs,
883 systemElapsedTimeNs, statsMetadata);
884 if (!metadataWritten) {
885 metadataList->mutable_stats_metadata()->RemoveLast();
886 }
887 }
888}
889
Jeffrey Huang475677e2020-03-30 19:52:07 -0700890void StatsLogProcessor::LoadMetadataFromDisk(int64_t currentWallClockTimeNs,
891 int64_t systemElapsedTimeNs) {
892 std::lock_guard<std::mutex> lock(mMetricsMutex);
893 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
894 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
895 if (-1 == fd) {
896 VLOG("Attempt to read %s but failed", file_name.c_str());
897 StorageManager::deleteFile(file_name.c_str());
898 return;
899 }
900 string content;
901 if (!android::base::ReadFdToString(fd, &content)) {
902 ALOGE("Attempt to read %s but failed", file_name.c_str());
903 close(fd);
904 StorageManager::deleteFile(file_name.c_str());
905 return;
906 }
907
908 close(fd);
909
910 metadata::StatsMetadataList statsMetadataList;
911 if (!statsMetadataList.ParseFromString(content)) {
912 ALOGE("Attempt to read %s but failed; failed to metadata", file_name.c_str());
913 StorageManager::deleteFile(file_name.c_str());
914 return;
915 }
916 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
917 StorageManager::deleteFile(file_name.c_str());
918}
919
920void StatsLogProcessor::SetMetadataState(const metadata::StatsMetadataList& statsMetadataList,
921 int64_t currentWallClockTimeNs,
922 int64_t systemElapsedTimeNs) {
923 std::lock_guard<std::mutex> lock(mMetricsMutex);
924 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
925}
926
927void StatsLogProcessor::SetMetadataStateLocked(
928 const metadata::StatsMetadataList& statsMetadataList,
929 int64_t currentWallClockTimeNs,
930 int64_t systemElapsedTimeNs) {
931 for (const metadata::StatsMetadata& metadata : statsMetadataList.stats_metadata()) {
932 ConfigKey key(metadata.config_key().uid(), metadata.config_key().config_id());
933 auto it = mMetricsManagers.find(key);
934 if (it == mMetricsManagers.end()) {
935 ALOGE("No config found for configKey %s", key.ToString().c_str());
936 continue;
937 }
938 VLOG("Setting metadata %s", key.ToString().c_str());
939 it->second->loadMetadata(metadata, currentWallClockTimeNs, systemElapsedTimeNs);
940 }
941 VLOG("Successfully loaded %d metadata.", statsMetadataList.stats_metadata_size());
942}
943
Tej Singhf53d4452019-05-09 18:17:59 -0700944void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream(
945 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
946 std::lock_guard<std::mutex> lock(mMetricsMutex);
947 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, reason, proto);
948}
949
950void StatsLogProcessor::WriteActiveConfigsToProtoOutputStreamLocked(
951 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
952 for (const auto& pair : mMetricsManagers) {
953 const sp<MetricsManager>& metricsManager = pair.second;
954 uint64_t configToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
955 FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG);
956 metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, reason, proto);
957 proto->end(configToken);
958 }
959}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700960void StatsLogProcessor::LoadActiveConfigsFromDisk() {
961 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800962 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
963 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700964 if (-1 == fd) {
965 VLOG("Attempt to read %s but failed", file_name.c_str());
966 StorageManager::deleteFile(file_name.c_str());
967 return;
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800968 }
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700969 string content;
970 if (!android::base::ReadFdToString(fd, &content)) {
971 ALOGE("Attempt to read %s but failed", file_name.c_str());
972 close(fd);
973 StorageManager::deleteFile(file_name.c_str());
974 return;
975 }
976
977 close(fd);
978
979 ActiveConfigList activeConfigList;
980 if (!activeConfigList.ParseFromString(content)) {
981 ALOGE("Attempt to read %s but failed; failed to load active configs", file_name.c_str());
982 StorageManager::deleteFile(file_name.c_str());
983 return;
984 }
Tej Singhf53d4452019-05-09 18:17:59 -0700985 // Passing in mTimeBaseNs only works as long as we only load from disk is when statsd starts.
986 SetConfigsActiveStateLocked(activeConfigList, mTimeBaseNs);
987 StorageManager::deleteFile(file_name.c_str());
988}
989
990void StatsLogProcessor::SetConfigsActiveState(const ActiveConfigList& activeConfigList,
991 int64_t currentTimeNs) {
992 std::lock_guard<std::mutex> lock(mMetricsMutex);
993 SetConfigsActiveStateLocked(activeConfigList, currentTimeNs);
994}
995
996void StatsLogProcessor::SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
997 int64_t currentTimeNs) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700998 for (int i = 0; i < activeConfigList.config_size(); i++) {
999 const auto& config = activeConfigList.config(i);
1000 ConfigKey key(config.uid(), config.id());
1001 auto it = mMetricsManagers.find(key);
1002 if (it == mMetricsManagers.end()) {
1003 ALOGE("No config found for config %s", key.ToString().c_str());
1004 continue;
1005 }
1006 VLOG("Setting active config %s", key.ToString().c_str());
Tej Singhf53d4452019-05-09 18:17:59 -07001007 it->second->loadActiveConfig(config, currentTimeNs);
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001008 }
1009 VLOG("Successfully loaded %d active configs.", activeConfigList.config_size());
Chenjie Yuc7939cb2019-02-04 17:25:45 -08001010}
1011
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001012void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
1013 const DumpLatency dumpLatency) {
Yangster-mac892f3d32018-05-02 14:16:48 -07001014 const int64_t timeNs = getElapsedRealtimeNs();
Tej Singh42f9e062018-11-09 10:01:00 -08001015 // Do not write to disk if we already have in the last few seconds.
1016 // This is to avoid overwriting files that would have the same name if we
1017 // write twice in the same second.
1018 if (static_cast<unsigned long long> (timeNs) <
1019 mLastWriteTimeNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
1020 ALOGI("Statsd skipping writing data to disk. Already wrote data in last %d seconds",
1021 WRITE_DATA_COOL_DOWN_SEC);
1022 return;
1023 }
1024 mLastWriteTimeNs = timeNs;
Yangster-mace68f3a52018-04-04 00:01:43 -07001025 for (auto& pair : mMetricsManagers) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001026 WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason, dumpLatency);
Yangster-mace68f3a52018-04-04 00:01:43 -07001027 }
1028}
1029
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001030void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001031 const DumpLatency dumpLatency) {
Yangster-macb0d06282018-01-05 15:44:07 -08001032 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001033 WriteDataToDiskLocked(dumpReportReason, dumpLatency);
yro947fbce2017-11-15 22:50:23 -08001034}
1035
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001036void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) {
Yangster6df5fcc2018-04-12 11:04:29 -07001037 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yue2219202018-06-08 10:07:51 -07001038 mPullerManager->OnAlarmFired(timestampNs);
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001039}
1040
David Chend37bc232018-04-12 18:05:11 -07001041int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) {
1042 auto it = mMetricsManagers.find(key);
1043 if (it == mMetricsManagers.end()) {
1044 return 0;
1045 } else {
1046 return it->second->getLastReportTimeNs();
1047 }
1048}
1049
Tej Singh9ec159a2019-11-14 11:59:48 -08001050void StatsLogProcessor::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk,
1051 const int uid, const int64_t version) {
1052 std::lock_guard<std::mutex> lock(mMetricsMutex);
1053 ALOGW("Received app upgrade");
1054 for (auto it : mMetricsManagers) {
1055 it.second->notifyAppUpgrade(eventTimeNs, apk, uid, version);
1056 }
1057}
1058
1059void StatsLogProcessor::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
1060 const int uid) {
1061 std::lock_guard<std::mutex> lock(mMetricsMutex);
1062 ALOGW("Received app removed");
1063 for (auto it : mMetricsManagers) {
1064 it.second->notifyAppRemoved(eventTimeNs, apk, uid);
1065 }
1066}
1067
1068void StatsLogProcessor::onUidMapReceived(const int64_t& eventTimeNs) {
1069 std::lock_guard<std::mutex> lock(mMetricsMutex);
1070 ALOGW("Received uid map");
1071 for (auto it : mMetricsManagers) {
1072 it.second->onUidMapReceived(eventTimeNs);
1073 }
1074}
1075
David Chen48944902018-05-03 10:29:11 -07001076void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) {
1077 std::lock_guard<std::mutex> lock(mMetricsMutex);
1078 mOnDiskDataConfigs.insert(key);
1079}
1080
Yao Chenef99c4f2017-09-22 16:26:54 -07001081} // namespace statsd
1082} // namespace os
1083} // namespace android