blob: 08786b9a827fa005e38dca156cd6211c2b688e4a [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
Yao Chen8a8d16c2018-02-08 14:50:40 -0800141void updateUid(Value* value, int hostUid) {
142 int uid = value->int_value;
143 if (uid != hostUid) {
144 value->setInt(hostUid);
145 }
146}
147
Yangster-macd40053e2018-01-09 16:29:22 -0800148void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Yao Chenc40a19d2018-03-15 16:48:25 -0700149 if (android::util::AtomsInfo::kAtomsWithAttributionChain.find(event->GetTagId()) !=
150 android::util::AtomsInfo::kAtomsWithAttributionChain.end()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800151 for (auto& value : *(event->getMutableValues())) {
152 if (value.mField.getPosAtDepth(0) > kAttributionField) {
153 break;
154 }
155 if (isAttributionUidField(value)) {
156 const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value);
157 updateUid(&value.mValue, hostUid);
158 }
Yangster-macd40053e2018-01-09 16:29:22 -0800159 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700160 } else {
161 auto it = android::util::AtomsInfo::kAtomsWithUidField.find(event->GetTagId());
162 if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) {
163 int uidField = it->second; // uidField is the field number in proto,
164 // starting from 1
165 if (uidField > 0 && (int)event->getValues().size() >= uidField &&
166 (event->getValues())[uidField - 1].mValue.getType() == INT) {
167 Value& value = (*event->getMutableValues())[uidField - 1].mValue;
168 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
169 updateUid(&value, hostUid);
170 } else {
171 ALOGE("Malformed log, uid not found. %s", event->ToString().c_str());
172 }
173 }
Yao Chen312e8982017-12-05 15:29:03 -0800174 }
Yangster-macd40053e2018-01-09 16:29:22 -0800175}
176
177void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
178 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
179 bool is_create = event.GetBool(3, &err);
180 auto parent_uid = int(event.GetLong(1, &err2));
181 auto isolated_uid = int(event.GetLong(2, &err3));
182 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
183 if (is_create) {
184 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
185 } else {
Bookatz3c648862018-05-25 13:32:43 -0700186 mUidMap->removeIsolatedUid(isolated_uid);
Yangster-macd40053e2018-01-09 16:29:22 -0800187 }
188 } else {
189 ALOGE("Failed to parse uid in the isolated uid change event.");
190 }
191}
192
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800193void StatsLogProcessor::onBinaryPushStateChangedEventLocked(LogEvent* event) {
194 pid_t pid = event->GetPid();
195 uid_t uid = event->GetUid();
196 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
197 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
198 return;
199 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800200 // The Get* functions don't modify the status on success, they only write in
201 // failure statuses, so we can use one status variable for all calls then
202 // check if it is no longer NO_ERROR.
203 status_t err = NO_ERROR;
204 InstallTrainInfo trainInfo;
205 trainInfo.trainName = string(event->GetString(1 /*train name field id*/, &err));
206 trainInfo.trainVersionCode = event->GetLong(2 /*train version field id*/, &err);
207 trainInfo.requiresStaging = event->GetBool(3 /*requires staging field id*/, &err);
208 trainInfo.rollbackEnabled = event->GetBool(4 /*rollback enabled field id*/, &err);
209 trainInfo.requiresLowLatencyMonitor =
210 event->GetBool(5 /*requires low latency monitor field id*/, &err);
211 trainInfo.status = int32_t(event->GetLong(6 /*state field id*/, &err));
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800212 std::vector<uint8_t> trainExperimentIdBytes =
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800213 event->GetStorage(7 /*experiment ids field id*/, &err);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800214 bool is_rollback = event->GetBool(10 /*is rollback field id*/, &err);
215
216 if (err != NO_ERROR) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800217 ALOGE("Failed to parse fields in binary push state changed log event");
218 return;
219 }
220 ExperimentIds trainExperimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800221 if (!trainExperimentIds.ParseFromArray(trainExperimentIdBytes.data(),
222 trainExperimentIdBytes.size())) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800223 ALOGE("Failed to parse experimentids in binary push state changed.");
224 return;
225 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800226 trainInfo.experimentIds = {trainExperimentIds.experiment_id().begin(),
227 trainExperimentIds.experiment_id().end()};
228
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800229 // Update the train info on disk and get any data the logevent is missing.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800230 getAndUpdateTrainInfoOnDisk(is_rollback, &trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800231
232 std::vector<uint8_t> trainExperimentIdProto;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800233 writeExperimentIdsToProto(trainInfo.experimentIds, &trainExperimentIdProto);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800234 int32_t userId = multiuser_get_user_id(uid);
235
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800236 event->updateValue(2 /*train version field id*/, trainInfo.trainVersionCode, LONG);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800237 event->updateValue(7 /*experiment ids field id*/, trainExperimentIdProto, STORAGE);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800238 event->updateValue(8 /*user id field id*/, userId, INT);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800239
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800240 // If this event is a rollback event, then the following bits in the event
241 // are invalid and we will need to update them with the values we pulled
242 // from disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800243 if (is_rollback) {
244 int bit = trainInfo.requiresStaging ? 1 : 0;
245 event->updateValue(3 /*requires staging field id*/, bit, INT);
246 bit = trainInfo.rollbackEnabled ? 1 : 0;
247 event->updateValue(4 /*rollback enabled field id*/, bit, INT);
248 bit = trainInfo.requiresLowLatencyMonitor ? 1 : 0;
249 event->updateValue(5 /*requires low latency monitor field id*/, bit, INT);
250 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800251}
252
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800253void StatsLogProcessor::getAndUpdateTrainInfoOnDisk(bool is_rollback,
254 InstallTrainInfo* trainInfo) {
255 // If the train name is empty, we don't know which train to attribute the
256 // event to, so return early.
257 if (trainInfo->trainName.empty()) {
258 return;
259 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800260 bool readTrainInfoSuccess = false;
261 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800262 readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfo->trainName, trainInfoOnDisk);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800263
264 bool resetExperimentIds = false;
265 if (readTrainInfoSuccess) {
266 // Keep the old train version if we received an empty version.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800267 if (trainInfo->trainVersionCode == -1) {
268 trainInfo->trainVersionCode = trainInfoOnDisk.trainVersionCode;
269 } else if (trainInfo->trainVersionCode != trainInfoOnDisk.trainVersionCode) {
270 // Reset experiment ids if we receive a new non-empty train version.
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800271 resetExperimentIds = true;
272 }
273
274 // Reset if we received a different experiment id.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800275 if (!trainInfo->experimentIds.empty() &&
276 (trainInfoOnDisk.experimentIds.empty() ||
277 trainInfo->experimentIds.at(0) != trainInfoOnDisk.experimentIds[0])) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800278 resetExperimentIds = true;
279 }
280 }
281
282 // Find the right experiment IDs
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800283 if ((!resetExperimentIds || is_rollback) && readTrainInfoSuccess) {
284 trainInfo->experimentIds = trainInfoOnDisk.experimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800285 }
286
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800287 if (!trainInfo->experimentIds.empty()) {
288 int64_t firstId = trainInfo->experimentIds.at(0);
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800289 auto& ids = trainInfo->experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800290 switch (trainInfo->status) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800291 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALL_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800292 if (find(ids.begin(), ids.end(), firstId + 1) == ids.end()) {
293 ids.push_back(firstId + 1);
294 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800295 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800296 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_INITIATED:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800297 if (find(ids.begin(), ids.end(), firstId + 2) == ids.end()) {
298 ids.push_back(firstId + 2);
299 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800300 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800301 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800302 if (find(ids.begin(), ids.end(), firstId + 3) == ids.end()) {
303 ids.push_back(firstId + 3);
304 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800305 break;
306 }
307 }
308
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800309 // If this event is a rollback event, the following fields are invalid and
310 // need to be replaced by the fields stored to disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800311 if (is_rollback) {
312 trainInfo->requiresStaging = trainInfoOnDisk.requiresStaging;
313 trainInfo->rollbackEnabled = trainInfoOnDisk.rollbackEnabled;
314 trainInfo->requiresLowLatencyMonitor = trainInfoOnDisk.requiresLowLatencyMonitor;
315 }
316
317 StorageManager::writeTrainInfo(*trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800318}
319
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800320void StatsLogProcessor::onWatchdogRollbackOccurredLocked(LogEvent* event) {
321 pid_t pid = event->GetPid();
322 uid_t uid = event->GetUid();
323 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
324 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
325 return;
326 }
327 // The Get* functions don't modify the status on success, they only write in
328 // failure statuses, so we can use one status variable for all calls then
329 // check if it is no longer NO_ERROR.
330 status_t err = NO_ERROR;
331 int32_t rollbackType = int32_t(event->GetInt(1 /*rollback type field id*/, &err));
332 string packageName = string(event->GetString(2 /*package name field id*/, &err));
333
334 if (err != NO_ERROR) {
335 ALOGE("Failed to parse fields in watchdog rollback occurred log event");
336 return;
337 }
338
339 vector<int64_t> experimentIds =
340 processWatchdogRollbackOccurred(rollbackType, packageName);
341 vector<uint8_t> experimentIdProto;
342 writeExperimentIdsToProto(experimentIds, &experimentIdProto);
343
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800344 event->updateValue(6 /*experiment ids field id*/, experimentIdProto, STORAGE);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800345}
346
347vector<int64_t> StatsLogProcessor::processWatchdogRollbackOccurred(const int32_t rollbackTypeIn,
348 const string& packageNameIn) {
349 // If the package name is empty, we can't attribute it to any train, so
350 // return early.
351 if (packageNameIn.empty()) {
352 return vector<int64_t>();
353 }
354 bool readTrainInfoSuccess = false;
355 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800356 // We use the package name of the event as the train name.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800357 readTrainInfoSuccess = StorageManager::readTrainInfo(packageNameIn, trainInfoOnDisk);
358
359 if (!readTrainInfoSuccess) {
360 return vector<int64_t>();
361 }
362
363 if (trainInfoOnDisk.experimentIds.empty()) {
364 return vector<int64_t>();
365 }
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800366
367 int64_t firstId = trainInfoOnDisk.experimentIds[0];
368 auto& ids = trainInfoOnDisk.experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800369 switch (rollbackTypeIn) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800370 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_INITIATE:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800371 if (find(ids.begin(), ids.end(), firstId + 4) == ids.end()) {
372 ids.push_back(firstId + 4);
373 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800374 StorageManager::writeTrainInfo(trainInfoOnDisk);
375 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800376 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800377 if (find(ids.begin(), ids.end(), firstId + 5) == ids.end()) {
378 ids.push_back(firstId + 5);
379 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800380 StorageManager::writeTrainInfo(trainInfoOnDisk);
381 break;
382 }
383
384 return trainInfoOnDisk.experimentIds;
385}
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800386
Yangster-mac892f3d32018-05-02 14:16:48 -0700387void StatsLogProcessor::resetConfigs() {
388 std::lock_guard<std::mutex> lock(mMetricsMutex);
389 resetConfigsLocked(getElapsedRealtimeNs());
390}
391
392void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) {
393 std::vector<ConfigKey> configKeys;
394 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
395 configKeys.push_back(it->first);
396 }
397 resetConfigsLocked(timestampNs, configKeys);
398}
399
Yao Chen3ff3a492018-08-06 16:17:37 -0700400void StatsLogProcessor::OnLogEvent(LogEvent* event) {
Tej Singh480392f2019-10-23 15:53:46 -0700401 OnLogEvent(event, getElapsedRealtimeNs());
402}
403
404void StatsLogProcessor::OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs) {
Yangster-macd40053e2018-01-09 16:29:22 -0800405 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen876889c2018-05-02 11:16:16 -0700406
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700407 // Tell StatsdStats about new event
408 const int64_t eventElapsedTimeNs = event->GetElapsedTimestampNs();
409 int atomId = event->GetTagId();
410 StatsdStats::getInstance().noteAtomLogged(atomId, eventElapsedTimeNs / NS_PER_SEC);
411 if (!event->isValid()) {
412 StatsdStats::getInstance().noteAtomError(atomId);
413 return;
414 }
415
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800416 // Hard-coded logic to update train info on disk and fill in any information
417 // this log event may be missing.
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700418 if (atomId == android::os::statsd::util::BINARY_PUSH_STATE_CHANGED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800419 onBinaryPushStateChangedEventLocked(event);
420 }
421
422 // Hard-coded logic to update experiment ids on disk for certain rollback
423 // types and fill the rollback atom with experiment ids
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700424 if (atomId == android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800425 onWatchdogRollbackOccurredLocked(event);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800426 }
427
Yao Chen876889c2018-05-02 11:16:16 -0700428#ifdef VERY_VERBOSE_PRINTING
429 if (mPrintAllLogs) {
430 ALOGI("%s", event->ToString().c_str());
431 }
432#endif
Tej Singh480392f2019-10-23 15:53:46 -0700433 resetIfConfigTtlExpiredLocked(eventElapsedTimeNs);
Yangster-macb142cc82018-03-30 15:22:08 -0700434
David Chen21582962017-11-01 17:32:46 -0700435 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800436 // The field numbers need to be currently updated by hand with atoms.proto
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 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800439 }
440
tsaichristined21aacf2019-10-07 14:47:38 -0700441 StateManager::getInstance().onLogEvent(*event);
442
David Chencfc311d2018-01-23 17:55:54 -0800443 if (mMetricsManagers.empty()) {
444 return;
445 }
446
Yangster-macb142cc82018-03-30 15:22:08 -0700447 int64_t curTimeSec = getElapsedRealtimeSec();
Yangster-mac330af582018-02-08 15:24:38 -0800448 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
Chenjie Yue2219202018-06-08 10:07:51 -0700449 mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC);
Yangster-mac330af582018-02-08 15:24:38 -0800450 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800451 }
452
Yangster-macb142cc82018-03-30 15:22:08 -0700453
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700454 if (atomId != android::os::statsd::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800455 // Map the isolated uid to host uid if necessary.
456 mapIsolatedUidToHostUidIfNecessaryLocked(event);
457 }
458
Tej Singh6ede28b2019-01-29 17:06:54 -0800459 std::unordered_set<int> uidsWithActiveConfigsChanged;
460 std::unordered_map<int, std::vector<int64_t>> activeConfigsPerUid;
Yangster-macd40053e2018-01-09 16:29:22 -0800461 // pass the event to metrics managers.
462 for (auto& pair : mMetricsManagers) {
Tej Singh6ede28b2019-01-29 17:06:54 -0800463 int uid = pair.first.GetUid();
464 int64_t configId = pair.first.GetId();
465 bool isPrevActive = pair.second->isActive();
Yangster-macd40053e2018-01-09 16:29:22 -0800466 pair.second->onLogEvent(*event);
Tej Singh6ede28b2019-01-29 17:06:54 -0800467 bool isCurActive = pair.second->isActive();
468 // Map all active configs by uid.
469 if (isCurActive) {
470 auto activeConfigs = activeConfigsPerUid.find(uid);
471 if (activeConfigs != activeConfigsPerUid.end()) {
472 activeConfigs->second.push_back(configId);
473 } else {
474 vector<int64_t> newActiveConfigs;
475 newActiveConfigs.push_back(configId);
476 activeConfigsPerUid[uid] = newActiveConfigs;
477 }
478 }
479 // The activation state of this config changed.
480 if (isPrevActive != isCurActive) {
481 VLOG("Active status changed for uid %d", uid);
482 uidsWithActiveConfigsChanged.insert(uid);
483 StatsdStats::getInstance().noteActiveStatusChanged(pair.first, isCurActive);
484 }
Tej Singh480392f2019-10-23 15:53:46 -0700485 flushIfNecessaryLocked(pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700486 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800487
Tej Singh480392f2019-10-23 15:53:46 -0700488 // Don't use the event timestamp for the guardrail.
Tej Singh6ede28b2019-01-29 17:06:54 -0800489 for (int uid : uidsWithActiveConfigsChanged) {
490 // Send broadcast so that receivers can pull data.
491 auto lastBroadcastTime = mLastActivationBroadcastTimes.find(uid);
492 if (lastBroadcastTime != mLastActivationBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700493 if (elapsedRealtimeNs - lastBroadcastTime->second <
494 StatsdStats::kMinActivationBroadcastPeriodNs) {
Tej Singh16ca28f2019-06-24 11:58:23 -0700495 StatsdStats::getInstance().noteActivationBroadcastGuardrailHit(uid);
Tej Singh6ede28b2019-01-29 17:06:54 -0800496 VLOG("StatsD would've sent an activation broadcast but the rate limit stopped us.");
497 return;
498 }
499 }
500 auto activeConfigs = activeConfigsPerUid.find(uid);
501 if (activeConfigs != activeConfigsPerUid.end()) {
502 if (mSendActivationBroadcast(uid, activeConfigs->second)) {
503 VLOG("StatsD sent activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700504 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800505 }
506 } else {
507 std::vector<int64_t> emptyActiveConfigs;
508 if (mSendActivationBroadcast(uid, emptyActiveConfigs)) {
509 VLOG("StatsD sent EMPTY activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700510 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800511 }
512 }
513 }
514}
515
516void StatsLogProcessor::GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs) {
517 std::lock_guard<std::mutex> lock(mMetricsMutex);
518 GetActiveConfigsLocked(uid, outActiveConfigs);
519}
520
521void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs) {
522 outActiveConfigs.clear();
523 for (auto& pair : mMetricsManagers) {
524 if (pair.first.GetUid() == uid && pair.second->isActive()) {
525 outActiveConfigs.push_back(pair.first.GetId());
526 }
527 }
Yao Chenab273e22017-09-06 12:53:50 -0700528}
529
Yangster-macc04feba2018-04-02 14:37:33 -0700530void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
531 const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800532 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000533 WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED, NO_TIME_CONSTRAINTS);
Yangster-macb142cc82018-03-30 15:22:08 -0700534 OnConfigUpdatedLocked(timestampNs, key, config);
535}
536
537void StatsLogProcessor::OnConfigUpdatedLocked(
538 const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) {
Tej Singh484524a2018-02-01 15:10:05 -0800539 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800540 sp<MetricsManager> newMetricsManager =
Chenjie Yue2219202018-06-08 10:07:51 -0700541 new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager,
542 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Yao Chencaf339d2017-10-06 16:01:10 -0700543 if (newMetricsManager->isConfigValid()) {
Tej Singh3be093b2020-03-04 20:08:38 -0800544 newMetricsManager->init();
David Chend6896892017-10-25 11:49:03 -0700545 mUidMap->OnConfigUpdated(key);
Yangster-macb142cc82018-03-30 15:22:08 -0700546 newMetricsManager->refreshTtl(timestampNs);
Yao Chend10f7b12017-12-18 12:53:50 -0800547 mMetricsManagers[key] = newMetricsManager;
Yao Chenb3561512017-11-21 18:07:17 -0800548 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700549 } else {
550 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800551 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700552 }
yro00698da2017-09-15 10:06:40 -0700553}
Bookatz906a35c2017-09-20 15:26:44 -0700554
Yangster7c334a12017-11-22 14:24:24 -0800555size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800556 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700557 auto it = mMetricsManagers.find(key);
558 if (it == mMetricsManagers.end()) {
559 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800560 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700561 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800562 return it->second->byteSize();
563}
564
Yao Chena80e5c02018-09-04 13:55:29 -0700565void StatsLogProcessor::dumpStates(int out, bool verbose) {
Yao Chen884c8c12018-01-26 10:36:25 -0800566 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chena80e5c02018-09-04 13:55:29 -0700567 FILE* fout = fdopen(out, "w");
568 if (fout == NULL) {
569 return;
Yao Chen884c8c12018-01-26 10:36:25 -0800570 }
Yao Chena80e5c02018-09-04 13:55:29 -0700571 fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
572 for (auto metricsManager : mMetricsManagers) {
573 metricsManager.second->dumpStates(fout, verbose);
574 }
575
576 fclose(fout);
Yao Chen884c8c12018-01-26 10:36:25 -0800577}
578
yro4beccbe2018-03-15 19:42:05 -0700579/*
Bookatz9cc7b662018-11-06 10:39:21 -0800580 * onDumpReport dumps serialized ConfigMetricsReportList into proto.
yro4beccbe2018-03-15 19:42:05 -0700581 */
Yangster-macb142cc82018-03-30 15:22:08 -0700582void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700583 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700584 const bool erase_data,
Chenjie Yue36018b2018-04-16 15:18:30 -0700585 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000586 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700587 ProtoOutputStream* proto) {
Yangster-macb0d06282018-01-05 15:44:07 -0800588 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac20877162017-12-22 17:19:39 -0800589
yro947fbce2017-11-15 22:50:23 -0800590 // Start of ConfigKey.
Bookatzff71cad2018-09-20 17:17:49 -0700591 uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
592 proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
593 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
594 proto->end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800595 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800596
Yao Chen9a43b4f2019-04-10 10:43:20 -0700597 bool keepFile = false;
598 auto it = mMetricsManagers.find(key);
599 if (it != mMetricsManagers.end() && it->second->shouldPersistLocalHistory()) {
600 keepFile = true;
601 }
602
yro947fbce2017-11-15 22:50:23 -0800603 // Then, check stats-data directory to see there's any file containing
604 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700605 StorageManager::appendConfigMetricsReport(
606 key, proto, erase_data && !keepFile /* should remove file after appending it */,
607 dumpReportReason == ADB_DUMP /*if caller is adb*/);
yro947fbce2017-11-15 22:50:23 -0800608
Yangster-mace68f3a52018-04-04 00:01:43 -0700609 if (it != mMetricsManagers.end()) {
610 // This allows another broadcast to be sent within the rate-limit period if we get close to
611 // filling the buffer again soon.
612 mLastBroadcastTimes.erase(key);
613
Yao Chen9a43b4f2019-04-10 10:43:20 -0700614 vector<uint8_t> buffer;
615 onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket,
616 erase_data, dumpReportReason, dumpLatency,
617 false /* is this data going to be saved on disk */, &buffer);
618 proto->write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS,
619 reinterpret_cast<char*>(buffer.data()), buffer.size());
Yangster-mace68f3a52018-04-04 00:01:43 -0700620 } else {
621 ALOGW("Config source %s does not exist", key.ToString().c_str());
622 }
Bookatzff71cad2018-09-20 17:17:49 -0700623}
624
625/*
626 * onDumpReport dumps serialized ConfigMetricsReportList into outData.
627 */
628void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
629 const bool include_current_partial_bucket,
630 const bool erase_data,
631 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000632 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700633 vector<uint8_t>* outData) {
634 ProtoOutputStream proto;
635 onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000636 dumpReportReason, dumpLatency, &proto);
Yangster-mace68f3a52018-04-04 00:01:43 -0700637
David Chen1d7b0cd2017-11-15 14:20:04 -0800638 if (outData != nullptr) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700639 flushProtoToBuffer(proto, outData);
640 VLOG("output data size %zu", outData->size());
yro17adac92017-11-08 23:16:29 -0800641 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800642
Yangster-mace68f3a52018-04-04 00:01:43 -0700643 StatsdStats::getInstance().noteMetricsReportSent(key, proto.size());
Yao Chen729093d2017-10-16 10:33:26 -0700644}
645
yro4beccbe2018-03-15 19:42:05 -0700646/*
647 * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData.
648 */
Yao Chen9a43b4f2019-04-10 10:43:20 -0700649void StatsLogProcessor::onConfigMetricsReportLocked(
650 const ConfigKey& key, const int64_t dumpTimeStampNs,
651 const bool include_current_partial_bucket, const bool erase_data,
652 const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
653 const bool dataSavedOnDisk, vector<uint8_t>* buffer) {
yro4beccbe2018-03-15 19:42:05 -0700654 // We already checked whether key exists in mMetricsManagers in
655 // WriteDataToDisk.
656 auto it = mMetricsManagers.find(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700657 if (it == mMetricsManagers.end()) {
658 return;
659 }
yro4beccbe2018-03-15 19:42:05 -0700660 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
661 int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
662
Yangster-mac9def8e32018-04-17 13:55:51 -0700663 std::set<string> str_set;
664
Yao Chen9a43b4f2019-04-10 10:43:20 -0700665 ProtoOutputStream tempProto;
yro4beccbe2018-03-15 19:42:05 -0700666 // First, fill in ConfigMetricsReport using current data on memory, which
667 // starts from filling in StatsLogReport's.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700668 it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
669 dumpLatency, &str_set, &tempProto);
yro4beccbe2018-03-15 19:42:05 -0700670
David Chen9e6dbbd2018-05-07 17:52:29 -0700671 // Fill in UidMap if there is at least one metric to report.
672 // This skips the uid map if it's an empty config.
673 if (it->second->getNumMetrics() > 0) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700674 uint64_t uidMapToken = tempProto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP);
dwchen730403e2018-10-29 11:41:56 -0700675 mUidMap->appendUidMap(
676 dumpTimeStampNs, key, it->second->hashStringInReport() ? &str_set : nullptr,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700677 it->second->versionStringsInReport(), it->second->installerInReport(), &tempProto);
678 tempProto.end(uidMapToken);
David Chen9e6dbbd2018-05-07 17:52:29 -0700679 }
yro4beccbe2018-03-15 19:42:05 -0700680
681 // Fill in the timestamps.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700682 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
683 (long long)lastReportTimeNs);
684 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
685 (long long)dumpTimeStampNs);
686 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
687 (long long)lastReportWallClockNs);
688 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
689 (long long)getWallClockNs());
Chenjie Yue36018b2018-04-16 15:18:30 -0700690 // Dump report reason
Yao Chen9a43b4f2019-04-10 10:43:20 -0700691 tempProto.write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason);
Yangster-mac9def8e32018-04-17 13:55:51 -0700692
David Chen56ae0d92018-05-11 16:00:22 -0700693 for (const auto& str : str_set) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700694 tempProto.write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str);
695 }
696
697 flushProtoToBuffer(tempProto, buffer);
698
699 // save buffer to disk if needed
700 if (erase_data && !dataSavedOnDisk && it->second->shouldPersistLocalHistory()) {
701 VLOG("save history to disk");
702 string file_name = StorageManager::getDataHistoryFileName((long)getWallClockSec(),
703 key.GetUid(), key.GetId());
704 StorageManager::writeFile(file_name.c_str(), buffer->data(), buffer->size());
Yangster-mac9def8e32018-04-17 13:55:51 -0700705 }
Yangster-macb142cc82018-03-30 15:22:08 -0700706}
yro4beccbe2018-03-15 19:42:05 -0700707
Yao Chen163d2602018-04-10 10:39:53 -0700708void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs,
709 const std::vector<ConfigKey>& configs) {
710 for (const auto& key : configs) {
Yangster-macb142cc82018-03-30 15:22:08 -0700711 StatsdConfig config;
712 if (StorageManager::readConfigFromDisk(key, &config)) {
713 OnConfigUpdatedLocked(timestampNs, key, config);
714 StatsdStats::getInstance().noteConfigReset(key);
715 } else {
716 ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str());
717 auto it = mMetricsManagers.find(key);
718 if (it != mMetricsManagers.end()) {
719 it->second->refreshTtl(timestampNs);
720 }
721 }
722 }
yro4beccbe2018-03-15 19:42:05 -0700723}
724
Yao Chen163d2602018-04-10 10:39:53 -0700725void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) {
726 std::vector<ConfigKey> configKeysTtlExpired;
727 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
728 if (it->second != nullptr && !it->second->isInTtl(timestampNs)) {
729 configKeysTtlExpired.push_back(it->first);
730 }
731 }
732 if (configKeysTtlExpired.size() > 0) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000733 WriteDataToDiskLocked(CONFIG_RESET, NO_TIME_CONSTRAINTS);
Yao Chen163d2602018-04-10 10:39:53 -0700734 resetConfigsLocked(timestampNs, configKeysTtlExpired);
735 }
736}
737
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700738void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800739 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700740 auto it = mMetricsManagers.find(key);
741 if (it != mMetricsManagers.end()) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700742 WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000743 NO_TIME_CONSTRAINTS);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700744 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700745 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700746 }
Yao Chenb3561512017-11-21 18:07:17 -0800747 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800748
David Chen1d7b0cd2017-11-15 14:20:04 -0800749 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800750
Tej Singh6ede28b2019-01-29 17:06:54 -0800751 int uid = key.GetUid();
752 bool lastConfigForUid = true;
753 for (auto it : mMetricsManagers) {
754 if (it.first.GetUid() == uid) {
755 lastConfigForUid = false;
756 break;
757 }
758 }
759 if (lastConfigForUid) {
760 mLastActivationBroadcastTimes.erase(uid);
761 }
762
Chenjie Yufa22d652018-02-05 14:37:48 -0800763 if (mMetricsManagers.empty()) {
Chenjie Yue2219202018-06-08 10:07:51 -0700764 mPullerManager->ForceClearPullerCache();
Chenjie Yufa22d652018-02-05 14:37:48 -0800765 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700766}
767
Tej Singh480392f2019-10-23 15:53:46 -0700768void StatsLogProcessor::flushIfNecessaryLocked(const ConfigKey& key,
769 MetricsManager& metricsManager) {
770 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
David Chend9269e22017-12-05 13:43:51 -0800771 auto lastCheckTime = mLastByteSizeTimes.find(key);
772 if (lastCheckTime != mLastByteSizeTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700773 if (elapsedRealtimeNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800774 return;
775 }
776 }
777
778 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
779 size_t totalBytes = metricsManager.byteSize();
Tej Singh480392f2019-10-23 15:53:46 -0700780 mLastByteSizeTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700781 bool requestDump = false;
Tej Singh480392f2019-10-23 15:53:46 -0700782 if (totalBytes > StatsdStats::kMaxMetricsBytesPerConfig) {
783 // Too late. We need to start clearing data.
784 metricsManager.dropData(elapsedRealtimeNs);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700785 StatsdStats::getInstance().noteDataDropped(key, totalBytes);
David Chen12942952017-12-04 14:28:43 -0800786 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chen48944902018-05-03 10:29:11 -0700787 } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) ||
788 (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) {
789 // Request to send a broadcast if:
790 // 1. in memory data > threshold OR
791 // 2. config has old data report on disk.
792 requestDump = true;
793 }
794
795 if (requestDump) {
David Chend9269e22017-12-05 13:43:51 -0800796 // Send broadcast so that receivers can pull data.
797 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
798 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700799 if (elapsedRealtimeNs - lastBroadcastTime->second <
800 StatsdStats::kMinBroadcastPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800801 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800802 return;
803 }
804 }
David Chen48944902018-05-03 10:29:11 -0700805 if (mSendBroadcast(key)) {
806 mOnDiskDataConfigs.erase(key);
807 VLOG("StatsD triggered data fetch for %s", key.ToString().c_str());
Tej Singh480392f2019-10-23 15:53:46 -0700808 mLastBroadcastTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700809 StatsdStats::getInstance().noteBroadcastSent(key);
810 }
yro31eb67b2017-10-24 13:33:21 -0700811 }
812}
813
Chenjie Yue36018b2018-04-16 15:18:30 -0700814void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key,
Yangster-mac892f3d32018-05-02 14:16:48 -0700815 const int64_t timestampNs,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000816 const DumpReportReason dumpReportReason,
817 const DumpLatency dumpLatency) {
yro028091c2018-05-09 16:03:27 -0700818 if (mMetricsManagers.find(key) == mMetricsManagers.end() ||
819 !mMetricsManagers.find(key)->second->shouldWriteToDisk()) {
Yangster-mac892f3d32018-05-02 14:16:48 -0700820 return;
821 }
Yao Chen9a43b4f2019-04-10 10:43:20 -0700822 vector<uint8_t> buffer;
David Chen56ae0d92018-05-11 16:00:22 -0700823 onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700824 true /* erase_data */, dumpReportReason, dumpLatency, true,
825 &buffer);
826 string file_name =
827 StorageManager::getDataFileName((long)getWallClockSec(), key.GetUid(), key.GetId());
828 StorageManager::writeFile(file_name.c_str(), buffer.data(), buffer.size());
829
David Chen48944902018-05-03 10:29:11 -0700830 // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP.
831 mOnDiskDataConfigs.insert(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700832}
833
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700834void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) {
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800835 std::lock_guard<std::mutex> lock(mMetricsMutex);
jianjin996f40d2019-03-29 13:53:48 -0700836 const int64_t timeNs = getElapsedRealtimeNs();
837 // Do not write to disk if we already have in the last few seconds.
jianjin996f40d2019-03-29 13:53:48 -0700838 if (static_cast<unsigned long long> (timeNs) <
839 mLastActiveMetricsWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
840 ALOGI("Statsd skipping writing active metrics to disk. Already wrote data in last %d seconds",
841 WRITE_DATA_COOL_DOWN_SEC);
842 return;
843 }
844 mLastActiveMetricsWriteNs = timeNs;
845
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800846 ProtoOutputStream proto;
Tej Singhf53d4452019-05-09 18:17:59 -0700847 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, DEVICE_SHUTDOWN, &proto);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800848
849 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
850 StorageManager::deleteFile(file_name.c_str());
851 android::base::unique_fd fd(
852 open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR));
853 if (fd == -1) {
854 ALOGE("Attempt to write %s but failed", file_name.c_str());
855 return;
856 }
857 proto.flush(fd.get());
858}
859
Jeffrey Huangb8f54032020-03-23 13:42:42 -0700860void StatsLogProcessor::SaveMetadataToDisk(int64_t currentWallClockTimeNs,
861 int64_t systemElapsedTimeNs) {
862 std::lock_guard<std::mutex> lock(mMetricsMutex);
863 // Do not write to disk if we already have in the last few seconds.
864 if (static_cast<unsigned long long> (systemElapsedTimeNs) <
865 mLastMetadataWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
866 ALOGI("Statsd skipping writing metadata to disk. Already wrote data in last %d seconds",
867 WRITE_DATA_COOL_DOWN_SEC);
868 return;
869 }
870 mLastMetadataWriteNs = systemElapsedTimeNs;
871
872 metadata::StatsMetadataList metadataList;
873 WriteMetadataToProtoLocked(
874 currentWallClockTimeNs, systemElapsedTimeNs, &metadataList);
875
876 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
877 StorageManager::deleteFile(file_name.c_str());
878
879 if (metadataList.stats_metadata_size() == 0) {
880 // Skip the write if we have nothing to write.
881 return;
882 }
883
884 std::string data;
885 metadataList.SerializeToString(&data);
886 StorageManager::writeFile(file_name.c_str(), data.c_str(), data.size());
887}
888
889void StatsLogProcessor::WriteMetadataToProto(int64_t currentWallClockTimeNs,
890 int64_t systemElapsedTimeNs,
891 metadata::StatsMetadataList* metadataList) {
892 std::lock_guard<std::mutex> lock(mMetricsMutex);
893 WriteMetadataToProtoLocked(currentWallClockTimeNs, systemElapsedTimeNs, metadataList);
894}
895
896void StatsLogProcessor::WriteMetadataToProtoLocked(int64_t currentWallClockTimeNs,
897 int64_t systemElapsedTimeNs,
898 metadata::StatsMetadataList* metadataList) {
899 for (const auto& pair : mMetricsManagers) {
900 const sp<MetricsManager>& metricsManager = pair.second;
901 metadata::StatsMetadata* statsMetadata = metadataList->add_stats_metadata();
902 bool metadataWritten = metricsManager->writeMetadataToProto(currentWallClockTimeNs,
903 systemElapsedTimeNs, statsMetadata);
904 if (!metadataWritten) {
905 metadataList->mutable_stats_metadata()->RemoveLast();
906 }
907 }
908}
909
Jeffrey Huang475677e2020-03-30 19:52:07 -0700910void StatsLogProcessor::LoadMetadataFromDisk(int64_t currentWallClockTimeNs,
911 int64_t systemElapsedTimeNs) {
912 std::lock_guard<std::mutex> lock(mMetricsMutex);
913 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
914 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
915 if (-1 == fd) {
916 VLOG("Attempt to read %s but failed", file_name.c_str());
917 StorageManager::deleteFile(file_name.c_str());
918 return;
919 }
920 string content;
921 if (!android::base::ReadFdToString(fd, &content)) {
922 ALOGE("Attempt to read %s but failed", file_name.c_str());
923 close(fd);
924 StorageManager::deleteFile(file_name.c_str());
925 return;
926 }
927
928 close(fd);
929
930 metadata::StatsMetadataList statsMetadataList;
931 if (!statsMetadataList.ParseFromString(content)) {
932 ALOGE("Attempt to read %s but failed; failed to metadata", file_name.c_str());
933 StorageManager::deleteFile(file_name.c_str());
934 return;
935 }
936 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
937 StorageManager::deleteFile(file_name.c_str());
938}
939
940void StatsLogProcessor::SetMetadataState(const metadata::StatsMetadataList& statsMetadataList,
941 int64_t currentWallClockTimeNs,
942 int64_t systemElapsedTimeNs) {
943 std::lock_guard<std::mutex> lock(mMetricsMutex);
944 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
945}
946
947void StatsLogProcessor::SetMetadataStateLocked(
948 const metadata::StatsMetadataList& statsMetadataList,
949 int64_t currentWallClockTimeNs,
950 int64_t systemElapsedTimeNs) {
951 for (const metadata::StatsMetadata& metadata : statsMetadataList.stats_metadata()) {
952 ConfigKey key(metadata.config_key().uid(), metadata.config_key().config_id());
953 auto it = mMetricsManagers.find(key);
954 if (it == mMetricsManagers.end()) {
955 ALOGE("No config found for configKey %s", key.ToString().c_str());
956 continue;
957 }
958 VLOG("Setting metadata %s", key.ToString().c_str());
959 it->second->loadMetadata(metadata, currentWallClockTimeNs, systemElapsedTimeNs);
960 }
961 VLOG("Successfully loaded %d metadata.", statsMetadataList.stats_metadata_size());
962}
963
Tej Singhf53d4452019-05-09 18:17:59 -0700964void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream(
965 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
966 std::lock_guard<std::mutex> lock(mMetricsMutex);
967 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, reason, proto);
968}
969
970void StatsLogProcessor::WriteActiveConfigsToProtoOutputStreamLocked(
971 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
972 for (const auto& pair : mMetricsManagers) {
973 const sp<MetricsManager>& metricsManager = pair.second;
974 uint64_t configToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
975 FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG);
976 metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, reason, proto);
977 proto->end(configToken);
978 }
979}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700980void StatsLogProcessor::LoadActiveConfigsFromDisk() {
981 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800982 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
983 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700984 if (-1 == fd) {
985 VLOG("Attempt to read %s but failed", file_name.c_str());
986 StorageManager::deleteFile(file_name.c_str());
987 return;
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800988 }
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700989 string content;
990 if (!android::base::ReadFdToString(fd, &content)) {
991 ALOGE("Attempt to read %s but failed", file_name.c_str());
992 close(fd);
993 StorageManager::deleteFile(file_name.c_str());
994 return;
995 }
996
997 close(fd);
998
999 ActiveConfigList activeConfigList;
1000 if (!activeConfigList.ParseFromString(content)) {
1001 ALOGE("Attempt to read %s but failed; failed to load active configs", file_name.c_str());
1002 StorageManager::deleteFile(file_name.c_str());
1003 return;
1004 }
Tej Singhf53d4452019-05-09 18:17:59 -07001005 // Passing in mTimeBaseNs only works as long as we only load from disk is when statsd starts.
1006 SetConfigsActiveStateLocked(activeConfigList, mTimeBaseNs);
1007 StorageManager::deleteFile(file_name.c_str());
1008}
1009
1010void StatsLogProcessor::SetConfigsActiveState(const ActiveConfigList& activeConfigList,
1011 int64_t currentTimeNs) {
1012 std::lock_guard<std::mutex> lock(mMetricsMutex);
1013 SetConfigsActiveStateLocked(activeConfigList, currentTimeNs);
1014}
1015
1016void StatsLogProcessor::SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
1017 int64_t currentTimeNs) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001018 for (int i = 0; i < activeConfigList.config_size(); i++) {
1019 const auto& config = activeConfigList.config(i);
1020 ConfigKey key(config.uid(), config.id());
1021 auto it = mMetricsManagers.find(key);
1022 if (it == mMetricsManagers.end()) {
1023 ALOGE("No config found for config %s", key.ToString().c_str());
1024 continue;
1025 }
1026 VLOG("Setting active config %s", key.ToString().c_str());
Tej Singhf53d4452019-05-09 18:17:59 -07001027 it->second->loadActiveConfig(config, currentTimeNs);
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001028 }
1029 VLOG("Successfully loaded %d active configs.", activeConfigList.config_size());
Chenjie Yuc7939cb2019-02-04 17:25:45 -08001030}
1031
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001032void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
1033 const DumpLatency dumpLatency) {
Yangster-mac892f3d32018-05-02 14:16:48 -07001034 const int64_t timeNs = getElapsedRealtimeNs();
Tej Singh42f9e062018-11-09 10:01:00 -08001035 // Do not write to disk if we already have in the last few seconds.
1036 // This is to avoid overwriting files that would have the same name if we
1037 // write twice in the same second.
1038 if (static_cast<unsigned long long> (timeNs) <
1039 mLastWriteTimeNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
1040 ALOGI("Statsd skipping writing data to disk. Already wrote data in last %d seconds",
1041 WRITE_DATA_COOL_DOWN_SEC);
1042 return;
1043 }
1044 mLastWriteTimeNs = timeNs;
Yangster-mace68f3a52018-04-04 00:01:43 -07001045 for (auto& pair : mMetricsManagers) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001046 WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason, dumpLatency);
Yangster-mace68f3a52018-04-04 00:01:43 -07001047 }
1048}
1049
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001050void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001051 const DumpLatency dumpLatency) {
Yangster-macb0d06282018-01-05 15:44:07 -08001052 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001053 WriteDataToDiskLocked(dumpReportReason, dumpLatency);
yro947fbce2017-11-15 22:50:23 -08001054}
1055
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001056void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) {
Yangster6df5fcc2018-04-12 11:04:29 -07001057 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yue2219202018-06-08 10:07:51 -07001058 mPullerManager->OnAlarmFired(timestampNs);
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001059}
1060
David Chend37bc232018-04-12 18:05:11 -07001061int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) {
1062 auto it = mMetricsManagers.find(key);
1063 if (it == mMetricsManagers.end()) {
1064 return 0;
1065 } else {
1066 return it->second->getLastReportTimeNs();
1067 }
1068}
1069
Tej Singh9ec159a2019-11-14 11:59:48 -08001070void StatsLogProcessor::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk,
1071 const int uid, const int64_t version) {
1072 std::lock_guard<std::mutex> lock(mMetricsMutex);
1073 ALOGW("Received app upgrade");
1074 for (auto it : mMetricsManagers) {
1075 it.second->notifyAppUpgrade(eventTimeNs, apk, uid, version);
1076 }
1077}
1078
1079void StatsLogProcessor::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
1080 const int uid) {
1081 std::lock_guard<std::mutex> lock(mMetricsMutex);
1082 ALOGW("Received app removed");
1083 for (auto it : mMetricsManagers) {
1084 it.second->notifyAppRemoved(eventTimeNs, apk, uid);
1085 }
1086}
1087
1088void StatsLogProcessor::onUidMapReceived(const int64_t& eventTimeNs) {
1089 std::lock_guard<std::mutex> lock(mMetricsMutex);
1090 ALOGW("Received uid map");
1091 for (auto it : mMetricsManagers) {
1092 it.second->onUidMapReceived(eventTimeNs);
1093 }
1094}
1095
David Chen48944902018-05-03 10:29:11 -07001096void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) {
1097 std::lock_guard<std::mutex> lock(mMetricsMutex);
1098 mOnDiskDataConfigs.insert(key);
1099}
1100
Yao Chenef99c4f2017-09-22 16:26:54 -07001101} // namespace statsd
1102} // namespace os
1103} // namespace android