blob: c10f248f47023deaf8ad452aed9a755d06ce767b [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53:50 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chenjie Yuc7939cb2019-02-04 17:25:45 -080017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
tsaichristined21aacf2019-10-07 14:47:38 -070019
20#include "StatsLogProcessor.h"
Yao Chenab273e22017-09-06 12:53:50 -070021
yro947fbce2017-11-15 22:50:23 -080022#include <android-base/file.h>
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080023#include <cutils/multiuser.h>
Chenjie Yuc7939cb2019-02-04 17:25:45 -080024#include <frameworks/base/cmds/statsd/src/active_config_list.pb.h>
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080025#include <frameworks/base/cmds/statsd/src/experiment_ids.pb.h>
tsaichristined21aacf2019-10-07 14:47:38 -070026
yro947fbce2017-11-15 22:50:23 -080027#include "android-base/stringprintf.h"
Chenjie Yuc7939cb2019-02-04 17:25:45 -080028#include "external/StatsPullerManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080029#include "guardrail/StatsdStats.h"
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080030#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031#include "metrics/CountMetricProducer.h"
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080032#include "StatsService.h"
tsaichristined21aacf2019-10-07 14:47:38 -070033#include "state/StateManager.h"
Chenjie Yuc7939cb2019-02-04 17:25:45 -080034#include "stats_log_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035#include "stats_util.h"
Jeffrey Huang74fc4352020-03-06 15:18:33 -080036#include "statslog_statsd.h"
yro947fbce2017-11-15 22:50:23 -080037#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070038
Yao Chenab273e22017-09-06 12:53:50 -070039using namespace android;
yro947fbce2017-11-15 22:50:23 -080040using android::base::StringPrintf;
yrob0378b02017-11-09 20:36:25 -080041using android::util::FIELD_COUNT_REPEATED;
yro17adac92017-11-08 23:16:29 -080042using android::util::FIELD_TYPE_BOOL;
43using android::util::FIELD_TYPE_FLOAT;
44using android::util::FIELD_TYPE_INT32;
45using android::util::FIELD_TYPE_INT64;
46using android::util::FIELD_TYPE_MESSAGE;
47using android::util::FIELD_TYPE_STRING;
48using android::util::ProtoOutputStream;
Yao Chen44cf27c2017-09-14 22:32:50 -070049using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070050
51namespace android {
52namespace os {
53namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070054
yro947fbce2017-11-15 22:50:23 -080055// for ConfigMetricsReportList
yro17adac92017-11-08 23:16:29 -080056const int FIELD_ID_CONFIG_KEY = 1;
yro947fbce2017-11-15 22:50:23 -080057const int FIELD_ID_REPORTS = 2;
yro17adac92017-11-08 23:16:29 -080058// for ConfigKey
59const int FIELD_ID_UID = 1;
Yangster-mac94e197c2018-01-02 16:03:03 -080060const int FIELD_ID_ID = 2;
yro947fbce2017-11-15 22:50:23 -080061// for ConfigMetricsReport
Yao Chen4c959cb2018-02-13 13:27:48 -080062// const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp
yro947fbce2017-11-15 22:50:23 -080063const int FIELD_ID_UID_MAP = 2;
Yangster-mac330af582018-02-08 15:24:38 -080064const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
65const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080066const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5;
67const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6;
Chenjie Yue36018b2018-04-16 15:18:30 -070068const int FIELD_ID_DUMP_REPORT_REASON = 8;
Yangster-mac9def8e32018-04-17 13:55:51 -070069const int FIELD_ID_STRINGS = 9;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080070
Muhammad Qureshi844694b2019-04-05 10:10:40 -070071// for ActiveConfigList
72const int FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG = 1;
Chenjie Yuc7939cb2019-02-04 17:25:45 -080073
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080074// for permissions checks
75constexpr const char* kPermissionDump = "android.permission.DUMP";
76constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS";
77
Yangster-macb142cc82018-03-30 15:22:08 -070078#define NS_PER_HOUR 3600 * NS_PER_SEC
yro947fbce2017-11-15 22:50:23 -080079
Chenjie Yuc7939cb2019-02-04 17:25:45 -080080#define STATS_ACTIVE_METRIC_DIR "/data/misc/stats-active-metric"
Jeffrey Huangb8f54032020-03-23 13:42:42 -070081#define STATS_METADATA_DIR "/data/misc/stats-metadata"
yro17adac92017-11-08 23:16:29 -080082
Tej Singh42f9e062018-11-09 10:01:00 -080083// Cool down period for writing data to disk to avoid overwriting files.
84#define WRITE_DATA_COOL_DOWN_SEC 5
85
yro31eb67b2017-10-24 13:33:21 -070086StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
Chenjie Yue2219202018-06-08 10:07:51 -070087 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -080088 const sp<AlarmMonitor>& anomalyAlarmMonitor,
89 const sp<AlarmMonitor>& periodicAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070090 const int64_t timeBaseNs,
Tej Singh6ede28b2019-01-29 17:06:54 -080091 const std::function<bool(const ConfigKey&)>& sendBroadcast,
92 const std::function<bool(
93 const int&, const vector<int64_t>&)>& activateBroadcast)
Chenjie Yu85ed8382017-12-14 16:48:54 -080094 : mUidMap(uidMap),
Chenjie Yue2219202018-06-08 10:07:51 -070095 mPullerManager(pullerManager),
Yangster-mac932ecec2018-02-01 10:23:52 -080096 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
97 mPeriodicAlarmMonitor(periodicAlarmMonitor),
Chenjie Yu85ed8382017-12-14 16:48:54 -080098 mSendBroadcast(sendBroadcast),
Tej Singh6ede28b2019-01-29 17:06:54 -080099 mSendActivationBroadcast(activateBroadcast),
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700100 mTimeBaseNs(timeBaseNs),
Yao Chen163d2602018-04-10 10:39:53 -0700101 mLargestTimestampSeen(0),
102 mLastTimestampSeen(0) {
Chenjie Yue2219202018-06-08 10:07:51 -0700103 mPullerManager->ForceClearPullerCache();
Muhammad Qureshi7ce22562020-05-12 01:08:00 -0700104 StateManager::getInstance().updateLogSources(uidMap);
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
Tej Singh8b914c02020-07-14 23:16:24 -0700123void StatsLogProcessor::processFiredAnomalyAlarmsLocked(
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) {
Bookatzcc5adef22017-11-21 14:36:23 -0800126 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800127 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
128 }
129}
130void StatsLogProcessor::onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700131 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800132 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
133
134 std::lock_guard<std::mutex> lock(mMetricsMutex);
135 for (const auto& itr : mMetricsManagers) {
136 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800137 }
138}
139
Yangster-macd40053e2018-01-09 16:29:22 -0800140void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Muhammad Qureshi83aaa662020-04-21 12:51:36 -0700141 if (std::pair<int, int> indexRange; event->hasAttributionChain(&indexRange)) {
142 vector<FieldValue>* const fieldValues = event->getMutableValues();
143 for (int i = indexRange.first; i <= indexRange.second; i++) {
144 FieldValue& fieldValue = fieldValues->at(i);
145 if (isAttributionUidField(fieldValue)) {
146 const int hostUid = mUidMap->getHostUidOrSelf(fieldValue.mValue.int_value);
147 fieldValue.mValue.setInt(hostUid);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800148 }
Yangster-macd40053e2018-01-09 16:29:22 -0800149 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700150 } else {
Ruchir Rastogiffa34f052020-04-04 15:30:11 -0700151 int uidFieldIndex = event->getUidFieldIndex();
152 if (uidFieldIndex != -1) {
153 Value& value = (*event->getMutableValues())[uidFieldIndex].mValue;
154 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
155 value.setInt(hostUid);
Yao Chenc40a19d2018-03-15 16:48:25 -0700156 }
Yao Chen312e8982017-12-05 15:29:03 -0800157 }
Yangster-macd40053e2018-01-09 16:29:22 -0800158}
159
160void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
161 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
162 bool is_create = event.GetBool(3, &err);
163 auto parent_uid = int(event.GetLong(1, &err2));
164 auto isolated_uid = int(event.GetLong(2, &err3));
165 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
166 if (is_create) {
167 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
168 } else {
Bookatz3c648862018-05-25 13:32:43 -0700169 mUidMap->removeIsolatedUid(isolated_uid);
Yangster-macd40053e2018-01-09 16:29:22 -0800170 }
171 } else {
172 ALOGE("Failed to parse uid in the isolated uid change event.");
173 }
174}
175
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800176void StatsLogProcessor::onBinaryPushStateChangedEventLocked(LogEvent* event) {
177 pid_t pid = event->GetPid();
178 uid_t uid = event->GetUid();
179 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
180 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
181 return;
182 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800183 // The Get* functions don't modify the status on success, they only write in
184 // failure statuses, so we can use one status variable for all calls then
185 // check if it is no longer NO_ERROR.
186 status_t err = NO_ERROR;
187 InstallTrainInfo trainInfo;
188 trainInfo.trainName = string(event->GetString(1 /*train name field id*/, &err));
189 trainInfo.trainVersionCode = event->GetLong(2 /*train version field id*/, &err);
190 trainInfo.requiresStaging = event->GetBool(3 /*requires staging field id*/, &err);
191 trainInfo.rollbackEnabled = event->GetBool(4 /*rollback enabled field id*/, &err);
192 trainInfo.requiresLowLatencyMonitor =
193 event->GetBool(5 /*requires low latency monitor field id*/, &err);
194 trainInfo.status = int32_t(event->GetLong(6 /*state field id*/, &err));
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800195 std::vector<uint8_t> trainExperimentIdBytes =
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800196 event->GetStorage(7 /*experiment ids field id*/, &err);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800197 bool is_rollback = event->GetBool(10 /*is rollback field id*/, &err);
198
199 if (err != NO_ERROR) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800200 ALOGE("Failed to parse fields in binary push state changed log event");
201 return;
202 }
203 ExperimentIds trainExperimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800204 if (!trainExperimentIds.ParseFromArray(trainExperimentIdBytes.data(),
205 trainExperimentIdBytes.size())) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800206 ALOGE("Failed to parse experimentids in binary push state changed.");
207 return;
208 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800209 trainInfo.experimentIds = {trainExperimentIds.experiment_id().begin(),
210 trainExperimentIds.experiment_id().end()};
211
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800212 // Update the train info on disk and get any data the logevent is missing.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800213 getAndUpdateTrainInfoOnDisk(is_rollback, &trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800214
215 std::vector<uint8_t> trainExperimentIdProto;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800216 writeExperimentIdsToProto(trainInfo.experimentIds, &trainExperimentIdProto);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800217 int32_t userId = multiuser_get_user_id(uid);
218
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800219 event->updateValue(2 /*train version field id*/, trainInfo.trainVersionCode, LONG);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800220 event->updateValue(7 /*experiment ids field id*/, trainExperimentIdProto, STORAGE);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800221 event->updateValue(8 /*user id field id*/, userId, INT);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800222
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800223 // If this event is a rollback event, then the following bits in the event
224 // are invalid and we will need to update them with the values we pulled
225 // from disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800226 if (is_rollback) {
227 int bit = trainInfo.requiresStaging ? 1 : 0;
228 event->updateValue(3 /*requires staging field id*/, bit, INT);
229 bit = trainInfo.rollbackEnabled ? 1 : 0;
230 event->updateValue(4 /*rollback enabled field id*/, bit, INT);
231 bit = trainInfo.requiresLowLatencyMonitor ? 1 : 0;
232 event->updateValue(5 /*requires low latency monitor field id*/, bit, INT);
233 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800234}
235
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800236void StatsLogProcessor::getAndUpdateTrainInfoOnDisk(bool is_rollback,
237 InstallTrainInfo* trainInfo) {
238 // If the train name is empty, we don't know which train to attribute the
239 // event to, so return early.
240 if (trainInfo->trainName.empty()) {
241 return;
242 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800243 bool readTrainInfoSuccess = false;
244 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800245 readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfo->trainName, trainInfoOnDisk);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800246
247 bool resetExperimentIds = false;
248 if (readTrainInfoSuccess) {
249 // Keep the old train version if we received an empty version.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800250 if (trainInfo->trainVersionCode == -1) {
251 trainInfo->trainVersionCode = trainInfoOnDisk.trainVersionCode;
252 } else if (trainInfo->trainVersionCode != trainInfoOnDisk.trainVersionCode) {
253 // Reset experiment ids if we receive a new non-empty train version.
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800254 resetExperimentIds = true;
255 }
256
257 // Reset if we received a different experiment id.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800258 if (!trainInfo->experimentIds.empty() &&
259 (trainInfoOnDisk.experimentIds.empty() ||
260 trainInfo->experimentIds.at(0) != trainInfoOnDisk.experimentIds[0])) {
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800261 resetExperimentIds = true;
262 }
263 }
264
265 // Find the right experiment IDs
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800266 if ((!resetExperimentIds || is_rollback) && readTrainInfoSuccess) {
267 trainInfo->experimentIds = trainInfoOnDisk.experimentIds;
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800268 }
269
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800270 if (!trainInfo->experimentIds.empty()) {
271 int64_t firstId = trainInfo->experimentIds.at(0);
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800272 auto& ids = trainInfo->experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800273 switch (trainInfo->status) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800274 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALL_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800275 if (find(ids.begin(), ids.end(), firstId + 1) == ids.end()) {
276 ids.push_back(firstId + 1);
277 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800278 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800279 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_INITIATED:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800280 if (find(ids.begin(), ids.end(), firstId + 2) == ids.end()) {
281 ids.push_back(firstId + 2);
282 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800283 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800284 case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800285 if (find(ids.begin(), ids.end(), firstId + 3) == ids.end()) {
286 ids.push_back(firstId + 3);
287 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800288 break;
289 }
290 }
291
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800292 // If this event is a rollback event, the following fields are invalid and
293 // need to be replaced by the fields stored to disk.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800294 if (is_rollback) {
295 trainInfo->requiresStaging = trainInfoOnDisk.requiresStaging;
296 trainInfo->rollbackEnabled = trainInfoOnDisk.rollbackEnabled;
297 trainInfo->requiresLowLatencyMonitor = trainInfoOnDisk.requiresLowLatencyMonitor;
298 }
299
300 StorageManager::writeTrainInfo(*trainInfo);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800301}
302
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800303void StatsLogProcessor::onWatchdogRollbackOccurredLocked(LogEvent* event) {
304 pid_t pid = event->GetPid();
305 uid_t uid = event->GetUid();
306 if (!checkPermissionForIds(kPermissionDump, pid, uid) ||
307 !checkPermissionForIds(kPermissionUsage, pid, uid)) {
308 return;
309 }
310 // The Get* functions don't modify the status on success, they only write in
311 // failure statuses, so we can use one status variable for all calls then
312 // check if it is no longer NO_ERROR.
313 status_t err = NO_ERROR;
314 int32_t rollbackType = int32_t(event->GetInt(1 /*rollback type field id*/, &err));
315 string packageName = string(event->GetString(2 /*package name field id*/, &err));
316
317 if (err != NO_ERROR) {
318 ALOGE("Failed to parse fields in watchdog rollback occurred log event");
319 return;
320 }
321
322 vector<int64_t> experimentIds =
323 processWatchdogRollbackOccurred(rollbackType, packageName);
324 vector<uint8_t> experimentIdProto;
325 writeExperimentIdsToProto(experimentIds, &experimentIdProto);
326
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800327 event->updateValue(6 /*experiment ids field id*/, experimentIdProto, STORAGE);
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800328}
329
330vector<int64_t> StatsLogProcessor::processWatchdogRollbackOccurred(const int32_t rollbackTypeIn,
331 const string& packageNameIn) {
332 // If the package name is empty, we can't attribute it to any train, so
333 // return early.
334 if (packageNameIn.empty()) {
335 return vector<int64_t>();
336 }
337 bool readTrainInfoSuccess = false;
338 InstallTrainInfo trainInfoOnDisk;
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800339 // We use the package name of the event as the train name.
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800340 readTrainInfoSuccess = StorageManager::readTrainInfo(packageNameIn, trainInfoOnDisk);
341
342 if (!readTrainInfoSuccess) {
343 return vector<int64_t>();
344 }
345
346 if (trainInfoOnDisk.experimentIds.empty()) {
347 return vector<int64_t>();
348 }
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800349
350 int64_t firstId = trainInfoOnDisk.experimentIds[0];
351 auto& ids = trainInfoOnDisk.experimentIds;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800352 switch (rollbackTypeIn) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800353 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_INITIATE:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800354 if (find(ids.begin(), ids.end(), firstId + 4) == ids.end()) {
355 ids.push_back(firstId + 4);
356 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800357 StorageManager::writeTrainInfo(trainInfoOnDisk);
358 break;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800359 case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_SUCCESS:
Jonathan Nguyen727a9642020-02-12 17:24:32 -0800360 if (find(ids.begin(), ids.end(), firstId + 5) == ids.end()) {
361 ids.push_back(firstId + 5);
362 }
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800363 StorageManager::writeTrainInfo(trainInfoOnDisk);
364 break;
365 }
366
367 return trainInfoOnDisk.experimentIds;
368}
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800369
Yangster-mac892f3d32018-05-02 14:16:48 -0700370void StatsLogProcessor::resetConfigs() {
371 std::lock_guard<std::mutex> lock(mMetricsMutex);
372 resetConfigsLocked(getElapsedRealtimeNs());
373}
374
375void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) {
376 std::vector<ConfigKey> configKeys;
377 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
378 configKeys.push_back(it->first);
379 }
380 resetConfigsLocked(timestampNs, configKeys);
381}
382
Yao Chen3ff3a492018-08-06 16:17:37 -0700383void StatsLogProcessor::OnLogEvent(LogEvent* event) {
Tej Singh480392f2019-10-23 15:53:46 -0700384 OnLogEvent(event, getElapsedRealtimeNs());
385}
386
387void StatsLogProcessor::OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs) {
Yangster-macd40053e2018-01-09 16:29:22 -0800388 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen876889c2018-05-02 11:16:16 -0700389
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700390 // Tell StatsdStats about new event
391 const int64_t eventElapsedTimeNs = event->GetElapsedTimestampNs();
392 int atomId = event->GetTagId();
393 StatsdStats::getInstance().noteAtomLogged(atomId, eventElapsedTimeNs / NS_PER_SEC);
394 if (!event->isValid()) {
395 StatsdStats::getInstance().noteAtomError(atomId);
396 return;
397 }
398
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800399 // Hard-coded logic to update train info on disk and fill in any information
400 // this log event may be missing.
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700401 if (atomId == android::os::statsd::util::BINARY_PUSH_STATE_CHANGED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800402 onBinaryPushStateChangedEventLocked(event);
403 }
404
405 // Hard-coded logic to update experiment ids on disk for certain rollback
406 // types and fill the rollback atom with experiment ids
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700407 if (atomId == android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED) {
Jonathan Nguyen703c42f2020-02-04 15:54:26 -0800408 onWatchdogRollbackOccurredLocked(event);
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800409 }
410
Yao Chen876889c2018-05-02 11:16:16 -0700411#ifdef VERY_VERBOSE_PRINTING
412 if (mPrintAllLogs) {
413 ALOGI("%s", event->ToString().c_str());
414 }
415#endif
Tej Singh480392f2019-10-23 15:53:46 -0700416 resetIfConfigTtlExpiredLocked(eventElapsedTimeNs);
Yangster-macb142cc82018-03-30 15:22:08 -0700417
David Chen21582962017-11-01 17:32:46 -0700418 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800419 // The field numbers need to be currently updated by hand with atoms.proto
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -0700420 if (atomId == android::os::statsd::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800421 onIsolatedUidChangedEventLocked(*event);
Muhammad Qureshi7ce22562020-05-12 01:08:00 -0700422 } else {
423 // Map the isolated uid to host uid if necessary.
424 mapIsolatedUidToHostUidIfNecessaryLocked(event);
David Chencfc311d2018-01-23 17:55:54 -0800425 }
426
tsaichristined21aacf2019-10-07 14:47:38 -0700427 StateManager::getInstance().onLogEvent(*event);
428
David Chencfc311d2018-01-23 17:55:54 -0800429 if (mMetricsManagers.empty()) {
430 return;
431 }
432
Tej Singh8b914c02020-07-14 23:16:24 -0700433 bool fireAlarm = false;
434 {
435 std::lock_guard<std::mutex> anomalyLock(mAnomalyAlarmMutex);
436 if (mNextAnomalyAlarmTime != 0 &&
437 MillisToNano(mNextAnomalyAlarmTime) <= elapsedRealtimeNs) {
438 mNextAnomalyAlarmTime = 0;
439 VLOG("informing anomaly alarm at time %lld", (long long)elapsedRealtimeNs);
440 fireAlarm = true;
441 }
442 }
443 if (fireAlarm) {
444 informAnomalyAlarmFiredLocked(NanoToMillis(elapsedRealtimeNs));
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
Tej Singh6ede28b2019-01-29 17:06:54 -0800453 std::unordered_set<int> uidsWithActiveConfigsChanged;
454 std::unordered_map<int, std::vector<int64_t>> activeConfigsPerUid;
Yangster-macd40053e2018-01-09 16:29:22 -0800455 // pass the event to metrics managers.
456 for (auto& pair : mMetricsManagers) {
Tej Singh6ede28b2019-01-29 17:06:54 -0800457 int uid = pair.first.GetUid();
458 int64_t configId = pair.first.GetId();
459 bool isPrevActive = pair.second->isActive();
Yangster-macd40053e2018-01-09 16:29:22 -0800460 pair.second->onLogEvent(*event);
Tej Singh6ede28b2019-01-29 17:06:54 -0800461 bool isCurActive = pair.second->isActive();
462 // Map all active configs by uid.
463 if (isCurActive) {
464 auto activeConfigs = activeConfigsPerUid.find(uid);
465 if (activeConfigs != activeConfigsPerUid.end()) {
466 activeConfigs->second.push_back(configId);
467 } else {
468 vector<int64_t> newActiveConfigs;
469 newActiveConfigs.push_back(configId);
470 activeConfigsPerUid[uid] = newActiveConfigs;
471 }
472 }
473 // The activation state of this config changed.
474 if (isPrevActive != isCurActive) {
475 VLOG("Active status changed for uid %d", uid);
476 uidsWithActiveConfigsChanged.insert(uid);
477 StatsdStats::getInstance().noteActiveStatusChanged(pair.first, isCurActive);
478 }
Tej Singh480392f2019-10-23 15:53:46 -0700479 flushIfNecessaryLocked(pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700480 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800481
Tej Singh480392f2019-10-23 15:53:46 -0700482 // Don't use the event timestamp for the guardrail.
Tej Singh6ede28b2019-01-29 17:06:54 -0800483 for (int uid : uidsWithActiveConfigsChanged) {
484 // Send broadcast so that receivers can pull data.
485 auto lastBroadcastTime = mLastActivationBroadcastTimes.find(uid);
486 if (lastBroadcastTime != mLastActivationBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700487 if (elapsedRealtimeNs - lastBroadcastTime->second <
488 StatsdStats::kMinActivationBroadcastPeriodNs) {
Tej Singh16ca28f2019-06-24 11:58:23 -0700489 StatsdStats::getInstance().noteActivationBroadcastGuardrailHit(uid);
Tej Singh6ede28b2019-01-29 17:06:54 -0800490 VLOG("StatsD would've sent an activation broadcast but the rate limit stopped us.");
491 return;
492 }
493 }
494 auto activeConfigs = activeConfigsPerUid.find(uid);
495 if (activeConfigs != activeConfigsPerUid.end()) {
496 if (mSendActivationBroadcast(uid, activeConfigs->second)) {
497 VLOG("StatsD sent activation notice for uid %d", uid);
Tej Singh480392f2019-10-23 15:53:46 -0700498 mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs;
Tej Singh6ede28b2019-01-29 17:06:54 -0800499 }
500 } else {
501 std::vector<int64_t> emptyActiveConfigs;
502 if (mSendActivationBroadcast(uid, emptyActiveConfigs)) {
503 VLOG("StatsD sent EMPTY 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 }
507 }
508}
509
510void StatsLogProcessor::GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs) {
511 std::lock_guard<std::mutex> lock(mMetricsMutex);
512 GetActiveConfigsLocked(uid, outActiveConfigs);
513}
514
515void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs) {
516 outActiveConfigs.clear();
517 for (auto& pair : mMetricsManagers) {
518 if (pair.first.GetUid() == uid && pair.second->isActive()) {
519 outActiveConfigs.push_back(pair.first.GetId());
520 }
521 }
Yao Chenab273e22017-09-06 12:53:50 -0700522}
523
Yangster-macc04feba2018-04-02 14:37:33 -0700524void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
525 const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800526 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000527 WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED, NO_TIME_CONSTRAINTS);
Yangster-macb142cc82018-03-30 15:22:08 -0700528 OnConfigUpdatedLocked(timestampNs, key, config);
529}
530
531void StatsLogProcessor::OnConfigUpdatedLocked(
532 const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) {
Tej Singh484524a2018-02-01 15:10:05 -0800533 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800534 sp<MetricsManager> newMetricsManager =
Chenjie Yue2219202018-06-08 10:07:51 -0700535 new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager,
536 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Yao Chencaf339d2017-10-06 16:01:10 -0700537 if (newMetricsManager->isConfigValid()) {
Tej Singh3be093b2020-03-04 20:08:38 -0800538 newMetricsManager->init();
David Chend6896892017-10-25 11:49:03 -0700539 mUidMap->OnConfigUpdated(key);
Yangster-macb142cc82018-03-30 15:22:08 -0700540 newMetricsManager->refreshTtl(timestampNs);
Yao Chend10f7b12017-12-18 12:53:50 -0800541 mMetricsManagers[key] = newMetricsManager;
Yao Chenb3561512017-11-21 18:07:17 -0800542 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700543 } else {
544 // If there is any error in the config, don't use it.
Tej Singh5a4727d2020-06-09 18:13:12 -0700545 // Remove any existing config with the same key.
Yao Chenb3561512017-11-21 18:07:17 -0800546 ALOGE("StatsdConfig NOT valid");
Tej Singh5a4727d2020-06-09 18:13:12 -0700547 mMetricsManagers.erase(key);
Yao Chencaf339d2017-10-06 16:01:10 -0700548 }
yro00698da2017-09-15 10:06:40 -0700549}
Bookatz906a35c2017-09-20 15:26:44 -0700550
Yangster7c334a12017-11-22 14:24:24 -0800551size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800552 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700553 auto it = mMetricsManagers.find(key);
554 if (it == mMetricsManagers.end()) {
555 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800556 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700557 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800558 return it->second->byteSize();
559}
560
Yao Chena80e5c02018-09-04 13:55:29 -0700561void StatsLogProcessor::dumpStates(int out, bool verbose) {
Yao Chen884c8c12018-01-26 10:36:25 -0800562 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chena80e5c02018-09-04 13:55:29 -0700563 FILE* fout = fdopen(out, "w");
564 if (fout == NULL) {
565 return;
Yao Chen884c8c12018-01-26 10:36:25 -0800566 }
Yao Chena80e5c02018-09-04 13:55:29 -0700567 fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
568 for (auto metricsManager : mMetricsManagers) {
569 metricsManager.second->dumpStates(fout, verbose);
570 }
571
572 fclose(fout);
Yao Chen884c8c12018-01-26 10:36:25 -0800573}
574
yro4beccbe2018-03-15 19:42:05 -0700575/*
Bookatz9cc7b662018-11-06 10:39:21 -0800576 * onDumpReport dumps serialized ConfigMetricsReportList into proto.
yro4beccbe2018-03-15 19:42:05 -0700577 */
Yangster-macb142cc82018-03-30 15:22:08 -0700578void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700579 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700580 const bool erase_data,
Chenjie Yue36018b2018-04-16 15:18:30 -0700581 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000582 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700583 ProtoOutputStream* proto) {
Yangster-macb0d06282018-01-05 15:44:07 -0800584 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac20877162017-12-22 17:19:39 -0800585
yro947fbce2017-11-15 22:50:23 -0800586 // Start of ConfigKey.
Bookatzff71cad2018-09-20 17:17:49 -0700587 uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
588 proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
589 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
590 proto->end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800591 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800592
Yao Chen9a43b4f2019-04-10 10:43:20 -0700593 bool keepFile = false;
594 auto it = mMetricsManagers.find(key);
595 if (it != mMetricsManagers.end() && it->second->shouldPersistLocalHistory()) {
596 keepFile = true;
597 }
598
yro947fbce2017-11-15 22:50:23 -0800599 // Then, check stats-data directory to see there's any file containing
600 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700601 StorageManager::appendConfigMetricsReport(
602 key, proto, erase_data && !keepFile /* should remove file after appending it */,
603 dumpReportReason == ADB_DUMP /*if caller is adb*/);
yro947fbce2017-11-15 22:50:23 -0800604
Yangster-mace68f3a52018-04-04 00:01:43 -0700605 if (it != mMetricsManagers.end()) {
606 // This allows another broadcast to be sent within the rate-limit period if we get close to
607 // filling the buffer again soon.
608 mLastBroadcastTimes.erase(key);
609
Yao Chen9a43b4f2019-04-10 10:43:20 -0700610 vector<uint8_t> buffer;
611 onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket,
612 erase_data, dumpReportReason, dumpLatency,
613 false /* is this data going to be saved on disk */, &buffer);
614 proto->write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS,
615 reinterpret_cast<char*>(buffer.data()), buffer.size());
Yangster-mace68f3a52018-04-04 00:01:43 -0700616 } else {
617 ALOGW("Config source %s does not exist", key.ToString().c_str());
618 }
Bookatzff71cad2018-09-20 17:17:49 -0700619}
620
621/*
622 * onDumpReport dumps serialized ConfigMetricsReportList into outData.
623 */
624void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
625 const bool include_current_partial_bucket,
626 const bool erase_data,
627 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000628 const DumpLatency dumpLatency,
Bookatzff71cad2018-09-20 17:17:49 -0700629 vector<uint8_t>* outData) {
630 ProtoOutputStream proto;
631 onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000632 dumpReportReason, dumpLatency, &proto);
Yangster-mace68f3a52018-04-04 00:01:43 -0700633
David Chen1d7b0cd2017-11-15 14:20:04 -0800634 if (outData != nullptr) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700635 flushProtoToBuffer(proto, outData);
636 VLOG("output data size %zu", outData->size());
yro17adac92017-11-08 23:16:29 -0800637 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800638
Yangster-mace68f3a52018-04-04 00:01:43 -0700639 StatsdStats::getInstance().noteMetricsReportSent(key, proto.size());
Yao Chen729093d2017-10-16 10:33:26 -0700640}
641
yro4beccbe2018-03-15 19:42:05 -0700642/*
643 * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData.
644 */
Yao Chen9a43b4f2019-04-10 10:43:20 -0700645void StatsLogProcessor::onConfigMetricsReportLocked(
646 const ConfigKey& key, const int64_t dumpTimeStampNs,
647 const bool include_current_partial_bucket, const bool erase_data,
648 const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
649 const bool dataSavedOnDisk, vector<uint8_t>* buffer) {
yro4beccbe2018-03-15 19:42:05 -0700650 // We already checked whether key exists in mMetricsManagers in
651 // WriteDataToDisk.
652 auto it = mMetricsManagers.find(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700653 if (it == mMetricsManagers.end()) {
654 return;
655 }
yro4beccbe2018-03-15 19:42:05 -0700656 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
657 int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
658
Yangster-mac9def8e32018-04-17 13:55:51 -0700659 std::set<string> str_set;
660
Yao Chen9a43b4f2019-04-10 10:43:20 -0700661 ProtoOutputStream tempProto;
yro4beccbe2018-03-15 19:42:05 -0700662 // First, fill in ConfigMetricsReport using current data on memory, which
663 // starts from filling in StatsLogReport's.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700664 it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
665 dumpLatency, &str_set, &tempProto);
yro4beccbe2018-03-15 19:42:05 -0700666
David Chen9e6dbbd2018-05-07 17:52:29 -0700667 // Fill in UidMap if there is at least one metric to report.
668 // This skips the uid map if it's an empty config.
669 if (it->second->getNumMetrics() > 0) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700670 uint64_t uidMapToken = tempProto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP);
dwchen730403e2018-10-29 11:41:56 -0700671 mUidMap->appendUidMap(
672 dumpTimeStampNs, key, it->second->hashStringInReport() ? &str_set : nullptr,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700673 it->second->versionStringsInReport(), it->second->installerInReport(), &tempProto);
674 tempProto.end(uidMapToken);
David Chen9e6dbbd2018-05-07 17:52:29 -0700675 }
yro4beccbe2018-03-15 19:42:05 -0700676
677 // Fill in the timestamps.
Yao Chen9a43b4f2019-04-10 10:43:20 -0700678 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
679 (long long)lastReportTimeNs);
680 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
681 (long long)dumpTimeStampNs);
682 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
683 (long long)lastReportWallClockNs);
684 tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
685 (long long)getWallClockNs());
Chenjie Yue36018b2018-04-16 15:18:30 -0700686 // Dump report reason
Yao Chen9a43b4f2019-04-10 10:43:20 -0700687 tempProto.write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason);
Yangster-mac9def8e32018-04-17 13:55:51 -0700688
David Chen56ae0d92018-05-11 16:00:22 -0700689 for (const auto& str : str_set) {
Yao Chen9a43b4f2019-04-10 10:43:20 -0700690 tempProto.write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str);
691 }
692
693 flushProtoToBuffer(tempProto, buffer);
694
695 // save buffer to disk if needed
696 if (erase_data && !dataSavedOnDisk && it->second->shouldPersistLocalHistory()) {
697 VLOG("save history to disk");
698 string file_name = StorageManager::getDataHistoryFileName((long)getWallClockSec(),
699 key.GetUid(), key.GetId());
700 StorageManager::writeFile(file_name.c_str(), buffer->data(), buffer->size());
Yangster-mac9def8e32018-04-17 13:55:51 -0700701 }
Yangster-macb142cc82018-03-30 15:22:08 -0700702}
yro4beccbe2018-03-15 19:42:05 -0700703
Yao Chen163d2602018-04-10 10:39:53 -0700704void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs,
705 const std::vector<ConfigKey>& configs) {
706 for (const auto& key : configs) {
Yangster-macb142cc82018-03-30 15:22:08 -0700707 StatsdConfig config;
708 if (StorageManager::readConfigFromDisk(key, &config)) {
709 OnConfigUpdatedLocked(timestampNs, key, config);
710 StatsdStats::getInstance().noteConfigReset(key);
711 } else {
712 ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str());
713 auto it = mMetricsManagers.find(key);
714 if (it != mMetricsManagers.end()) {
715 it->second->refreshTtl(timestampNs);
716 }
717 }
718 }
yro4beccbe2018-03-15 19:42:05 -0700719}
720
Yao Chen163d2602018-04-10 10:39:53 -0700721void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) {
722 std::vector<ConfigKey> configKeysTtlExpired;
723 for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
724 if (it->second != nullptr && !it->second->isInTtl(timestampNs)) {
725 configKeysTtlExpired.push_back(it->first);
726 }
727 }
728 if (configKeysTtlExpired.size() > 0) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000729 WriteDataToDiskLocked(CONFIG_RESET, NO_TIME_CONSTRAINTS);
Yao Chen163d2602018-04-10 10:39:53 -0700730 resetConfigsLocked(timestampNs, configKeysTtlExpired);
731 }
732}
733
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700734void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800735 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700736 auto it = mMetricsManagers.find(key);
737 if (it != mMetricsManagers.end()) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700738 WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000739 NO_TIME_CONSTRAINTS);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700740 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700741 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700742 }
Yao Chenb3561512017-11-21 18:07:17 -0800743 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800744
David Chen1d7b0cd2017-11-15 14:20:04 -0800745 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800746
Tej Singh6ede28b2019-01-29 17:06:54 -0800747 int uid = key.GetUid();
748 bool lastConfigForUid = true;
749 for (auto it : mMetricsManagers) {
750 if (it.first.GetUid() == uid) {
751 lastConfigForUid = false;
752 break;
753 }
754 }
755 if (lastConfigForUid) {
756 mLastActivationBroadcastTimes.erase(uid);
757 }
758
Chenjie Yufa22d652018-02-05 14:37:48 -0800759 if (mMetricsManagers.empty()) {
Chenjie Yue2219202018-06-08 10:07:51 -0700760 mPullerManager->ForceClearPullerCache();
Chenjie Yufa22d652018-02-05 14:37:48 -0800761 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700762}
763
Tej Singh480392f2019-10-23 15:53:46 -0700764void StatsLogProcessor::flushIfNecessaryLocked(const ConfigKey& key,
765 MetricsManager& metricsManager) {
766 int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
David Chend9269e22017-12-05 13:43:51 -0800767 auto lastCheckTime = mLastByteSizeTimes.find(key);
768 if (lastCheckTime != mLastByteSizeTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700769 if (elapsedRealtimeNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800770 return;
771 }
772 }
773
774 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
775 size_t totalBytes = metricsManager.byteSize();
Tej Singh480392f2019-10-23 15:53:46 -0700776 mLastByteSizeTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700777 bool requestDump = false;
Tej Singh480392f2019-10-23 15:53:46 -0700778 if (totalBytes > StatsdStats::kMaxMetricsBytesPerConfig) {
779 // Too late. We need to start clearing data.
780 metricsManager.dropData(elapsedRealtimeNs);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700781 StatsdStats::getInstance().noteDataDropped(key, totalBytes);
David Chen12942952017-12-04 14:28:43 -0800782 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chen48944902018-05-03 10:29:11 -0700783 } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) ||
784 (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) {
785 // Request to send a broadcast if:
786 // 1. in memory data > threshold OR
787 // 2. config has old data report on disk.
788 requestDump = true;
789 }
790
791 if (requestDump) {
David Chend9269e22017-12-05 13:43:51 -0800792 // Send broadcast so that receivers can pull data.
793 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
794 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
Tej Singh480392f2019-10-23 15:53:46 -0700795 if (elapsedRealtimeNs - lastBroadcastTime->second <
796 StatsdStats::kMinBroadcastPeriodNs) {
David Chend9269e22017-12-05 13:43:51 -0800797 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800798 return;
799 }
800 }
David Chen48944902018-05-03 10:29:11 -0700801 if (mSendBroadcast(key)) {
802 mOnDiskDataConfigs.erase(key);
803 VLOG("StatsD triggered data fetch for %s", key.ToString().c_str());
Tej Singh480392f2019-10-23 15:53:46 -0700804 mLastBroadcastTimes[key] = elapsedRealtimeNs;
David Chen48944902018-05-03 10:29:11 -0700805 StatsdStats::getInstance().noteBroadcastSent(key);
806 }
yro31eb67b2017-10-24 13:33:21 -0700807 }
808}
809
Chenjie Yue36018b2018-04-16 15:18:30 -0700810void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key,
Yangster-mac892f3d32018-05-02 14:16:48 -0700811 const int64_t timestampNs,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000812 const DumpReportReason dumpReportReason,
813 const DumpLatency dumpLatency) {
yro028091c2018-05-09 16:03:27 -0700814 if (mMetricsManagers.find(key) == mMetricsManagers.end() ||
815 !mMetricsManagers.find(key)->second->shouldWriteToDisk()) {
Yangster-mac892f3d32018-05-02 14:16:48 -0700816 return;
817 }
Yao Chen9a43b4f2019-04-10 10:43:20 -0700818 vector<uint8_t> buffer;
David Chen56ae0d92018-05-11 16:00:22 -0700819 onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/,
Yao Chen9a43b4f2019-04-10 10:43:20 -0700820 true /* erase_data */, dumpReportReason, dumpLatency, true,
821 &buffer);
822 string file_name =
823 StorageManager::getDataFileName((long)getWallClockSec(), key.GetUid(), key.GetId());
824 StorageManager::writeFile(file_name.c_str(), buffer.data(), buffer.size());
825
David Chen48944902018-05-03 10:29:11 -0700826 // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP.
827 mOnDiskDataConfigs.insert(key);
Yangster-mace68f3a52018-04-04 00:01:43 -0700828}
829
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700830void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) {
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800831 std::lock_guard<std::mutex> lock(mMetricsMutex);
jianjin996f40d2019-03-29 13:53:48 -0700832 const int64_t timeNs = getElapsedRealtimeNs();
833 // Do not write to disk if we already have in the last few seconds.
jianjin996f40d2019-03-29 13:53:48 -0700834 if (static_cast<unsigned long long> (timeNs) <
835 mLastActiveMetricsWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
836 ALOGI("Statsd skipping writing active metrics to disk. Already wrote data in last %d seconds",
837 WRITE_DATA_COOL_DOWN_SEC);
838 return;
839 }
840 mLastActiveMetricsWriteNs = timeNs;
841
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800842 ProtoOutputStream proto;
Tej Singhf53d4452019-05-09 18:17:59 -0700843 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, DEVICE_SHUTDOWN, &proto);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800844
845 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
846 StorageManager::deleteFile(file_name.c_str());
847 android::base::unique_fd fd(
848 open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR));
849 if (fd == -1) {
850 ALOGE("Attempt to write %s but failed", file_name.c_str());
851 return;
852 }
853 proto.flush(fd.get());
854}
855
Jeffrey Huangb8f54032020-03-23 13:42:42 -0700856void StatsLogProcessor::SaveMetadataToDisk(int64_t currentWallClockTimeNs,
857 int64_t systemElapsedTimeNs) {
858 std::lock_guard<std::mutex> lock(mMetricsMutex);
859 // Do not write to disk if we already have in the last few seconds.
860 if (static_cast<unsigned long long> (systemElapsedTimeNs) <
861 mLastMetadataWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
862 ALOGI("Statsd skipping writing metadata to disk. Already wrote data in last %d seconds",
863 WRITE_DATA_COOL_DOWN_SEC);
864 return;
865 }
866 mLastMetadataWriteNs = systemElapsedTimeNs;
867
868 metadata::StatsMetadataList metadataList;
869 WriteMetadataToProtoLocked(
870 currentWallClockTimeNs, systemElapsedTimeNs, &metadataList);
871
872 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
873 StorageManager::deleteFile(file_name.c_str());
874
875 if (metadataList.stats_metadata_size() == 0) {
876 // Skip the write if we have nothing to write.
877 return;
878 }
879
880 std::string data;
881 metadataList.SerializeToString(&data);
882 StorageManager::writeFile(file_name.c_str(), data.c_str(), data.size());
883}
884
885void StatsLogProcessor::WriteMetadataToProto(int64_t currentWallClockTimeNs,
886 int64_t systemElapsedTimeNs,
887 metadata::StatsMetadataList* metadataList) {
888 std::lock_guard<std::mutex> lock(mMetricsMutex);
889 WriteMetadataToProtoLocked(currentWallClockTimeNs, systemElapsedTimeNs, metadataList);
890}
891
892void StatsLogProcessor::WriteMetadataToProtoLocked(int64_t currentWallClockTimeNs,
893 int64_t systemElapsedTimeNs,
894 metadata::StatsMetadataList* metadataList) {
895 for (const auto& pair : mMetricsManagers) {
896 const sp<MetricsManager>& metricsManager = pair.second;
897 metadata::StatsMetadata* statsMetadata = metadataList->add_stats_metadata();
898 bool metadataWritten = metricsManager->writeMetadataToProto(currentWallClockTimeNs,
899 systemElapsedTimeNs, statsMetadata);
900 if (!metadataWritten) {
901 metadataList->mutable_stats_metadata()->RemoveLast();
902 }
903 }
904}
905
Jeffrey Huang475677e2020-03-30 19:52:07 -0700906void StatsLogProcessor::LoadMetadataFromDisk(int64_t currentWallClockTimeNs,
907 int64_t systemElapsedTimeNs) {
908 std::lock_guard<std::mutex> lock(mMetricsMutex);
909 string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
910 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
911 if (-1 == fd) {
912 VLOG("Attempt to read %s but failed", file_name.c_str());
913 StorageManager::deleteFile(file_name.c_str());
914 return;
915 }
916 string content;
917 if (!android::base::ReadFdToString(fd, &content)) {
918 ALOGE("Attempt to read %s but failed", file_name.c_str());
919 close(fd);
920 StorageManager::deleteFile(file_name.c_str());
921 return;
922 }
923
924 close(fd);
925
926 metadata::StatsMetadataList statsMetadataList;
927 if (!statsMetadataList.ParseFromString(content)) {
928 ALOGE("Attempt to read %s but failed; failed to metadata", file_name.c_str());
929 StorageManager::deleteFile(file_name.c_str());
930 return;
931 }
932 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
933 StorageManager::deleteFile(file_name.c_str());
934}
935
936void StatsLogProcessor::SetMetadataState(const metadata::StatsMetadataList& statsMetadataList,
937 int64_t currentWallClockTimeNs,
938 int64_t systemElapsedTimeNs) {
939 std::lock_guard<std::mutex> lock(mMetricsMutex);
940 SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
941}
942
943void StatsLogProcessor::SetMetadataStateLocked(
944 const metadata::StatsMetadataList& statsMetadataList,
945 int64_t currentWallClockTimeNs,
946 int64_t systemElapsedTimeNs) {
947 for (const metadata::StatsMetadata& metadata : statsMetadataList.stats_metadata()) {
948 ConfigKey key(metadata.config_key().uid(), metadata.config_key().config_id());
949 auto it = mMetricsManagers.find(key);
950 if (it == mMetricsManagers.end()) {
951 ALOGE("No config found for configKey %s", key.ToString().c_str());
952 continue;
953 }
954 VLOG("Setting metadata %s", key.ToString().c_str());
955 it->second->loadMetadata(metadata, currentWallClockTimeNs, systemElapsedTimeNs);
956 }
957 VLOG("Successfully loaded %d metadata.", statsMetadataList.stats_metadata_size());
958}
959
Tej Singhf53d4452019-05-09 18:17:59 -0700960void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream(
961 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
962 std::lock_guard<std::mutex> lock(mMetricsMutex);
963 WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, reason, proto);
964}
965
966void StatsLogProcessor::WriteActiveConfigsToProtoOutputStreamLocked(
967 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
968 for (const auto& pair : mMetricsManagers) {
969 const sp<MetricsManager>& metricsManager = pair.second;
970 uint64_t configToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
971 FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG);
972 metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, reason, proto);
973 proto->end(configToken);
974 }
975}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700976void StatsLogProcessor::LoadActiveConfigsFromDisk() {
977 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800978 string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR);
979 int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700980 if (-1 == fd) {
981 VLOG("Attempt to read %s but failed", file_name.c_str());
982 StorageManager::deleteFile(file_name.c_str());
983 return;
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800984 }
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700985 string content;
986 if (!android::base::ReadFdToString(fd, &content)) {
987 ALOGE("Attempt to read %s but failed", file_name.c_str());
988 close(fd);
989 StorageManager::deleteFile(file_name.c_str());
990 return;
991 }
992
993 close(fd);
994
995 ActiveConfigList activeConfigList;
996 if (!activeConfigList.ParseFromString(content)) {
997 ALOGE("Attempt to read %s but failed; failed to load active configs", file_name.c_str());
998 StorageManager::deleteFile(file_name.c_str());
999 return;
1000 }
Tej Singhf53d4452019-05-09 18:17:59 -07001001 // Passing in mTimeBaseNs only works as long as we only load from disk is when statsd starts.
1002 SetConfigsActiveStateLocked(activeConfigList, mTimeBaseNs);
1003 StorageManager::deleteFile(file_name.c_str());
1004}
1005
1006void StatsLogProcessor::SetConfigsActiveState(const ActiveConfigList& activeConfigList,
1007 int64_t currentTimeNs) {
1008 std::lock_guard<std::mutex> lock(mMetricsMutex);
1009 SetConfigsActiveStateLocked(activeConfigList, currentTimeNs);
1010}
1011
1012void StatsLogProcessor::SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
1013 int64_t currentTimeNs) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001014 for (int i = 0; i < activeConfigList.config_size(); i++) {
1015 const auto& config = activeConfigList.config(i);
1016 ConfigKey key(config.uid(), config.id());
1017 auto it = mMetricsManagers.find(key);
1018 if (it == mMetricsManagers.end()) {
1019 ALOGE("No config found for config %s", key.ToString().c_str());
1020 continue;
1021 }
1022 VLOG("Setting active config %s", key.ToString().c_str());
Tej Singhf53d4452019-05-09 18:17:59 -07001023 it->second->loadActiveConfig(config, currentTimeNs);
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001024 }
1025 VLOG("Successfully loaded %d active configs.", activeConfigList.config_size());
Chenjie Yuc7939cb2019-02-04 17:25:45 -08001026}
1027
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001028void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
1029 const DumpLatency dumpLatency) {
Yangster-mac892f3d32018-05-02 14:16:48 -07001030 const int64_t timeNs = getElapsedRealtimeNs();
Tej Singh42f9e062018-11-09 10:01:00 -08001031 // Do not write to disk if we already have in the last few seconds.
1032 // This is to avoid overwriting files that would have the same name if we
1033 // write twice in the same second.
1034 if (static_cast<unsigned long long> (timeNs) <
1035 mLastWriteTimeNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) {
1036 ALOGI("Statsd skipping writing data to disk. Already wrote data in last %d seconds",
1037 WRITE_DATA_COOL_DOWN_SEC);
1038 return;
1039 }
1040 mLastWriteTimeNs = timeNs;
Yangster-mace68f3a52018-04-04 00:01:43 -07001041 for (auto& pair : mMetricsManagers) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001042 WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason, dumpLatency);
Yangster-mace68f3a52018-04-04 00:01:43 -07001043 }
1044}
1045
Muhammad Qureshi844694b2019-04-05 10:10:40 -07001046void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001047 const DumpLatency dumpLatency) {
Yangster-macb0d06282018-01-05 15:44:07 -08001048 std::lock_guard<std::mutex> lock(mMetricsMutex);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +00001049 WriteDataToDiskLocked(dumpReportReason, dumpLatency);
yro947fbce2017-11-15 22:50:23 -08001050}
1051
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001052void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) {
Yangster6df5fcc2018-04-12 11:04:29 -07001053 std::lock_guard<std::mutex> lock(mMetricsMutex);
Chenjie Yue2219202018-06-08 10:07:51 -07001054 mPullerManager->OnAlarmFired(timestampNs);
Yangster-mac15f6bbc2018-04-08 11:52:26 -07001055}
1056
David Chend37bc232018-04-12 18:05:11 -07001057int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) {
1058 auto it = mMetricsManagers.find(key);
1059 if (it == mMetricsManagers.end()) {
1060 return 0;
1061 } else {
1062 return it->second->getLastReportTimeNs();
1063 }
1064}
1065
Tej Singh9ec159a2019-11-14 11:59:48 -08001066void StatsLogProcessor::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk,
1067 const int uid, const int64_t version) {
1068 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singhe678cb72020-04-14 16:23:30 -07001069 VLOG("Received app upgrade");
Muhammad Qureshi7ce22562020-05-12 01:08:00 -07001070 StateManager::getInstance().notifyAppChanged(apk, mUidMap);
Tej Singhe678cb72020-04-14 16:23:30 -07001071 for (const auto& it : mMetricsManagers) {
Tej Singh9ec159a2019-11-14 11:59:48 -08001072 it.second->notifyAppUpgrade(eventTimeNs, apk, uid, version);
1073 }
1074}
1075
1076void StatsLogProcessor::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
1077 const int uid) {
1078 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singhe678cb72020-04-14 16:23:30 -07001079 VLOG("Received app removed");
Muhammad Qureshi7ce22562020-05-12 01:08:00 -07001080 StateManager::getInstance().notifyAppChanged(apk, mUidMap);
Tej Singhe678cb72020-04-14 16:23:30 -07001081 for (const auto& it : mMetricsManagers) {
Tej Singh9ec159a2019-11-14 11:59:48 -08001082 it.second->notifyAppRemoved(eventTimeNs, apk, uid);
1083 }
1084}
1085
1086void StatsLogProcessor::onUidMapReceived(const int64_t& eventTimeNs) {
1087 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singhe678cb72020-04-14 16:23:30 -07001088 VLOG("Received uid map");
Muhammad Qureshi7ce22562020-05-12 01:08:00 -07001089 StateManager::getInstance().updateLogSources(mUidMap);
Tej Singhe678cb72020-04-14 16:23:30 -07001090 for (const auto& it : mMetricsManagers) {
Tej Singh9ec159a2019-11-14 11:59:48 -08001091 it.second->onUidMapReceived(eventTimeNs);
1092 }
1093}
1094
Tej Singhe678cb72020-04-14 16:23:30 -07001095void StatsLogProcessor::onStatsdInitCompleted(const int64_t& elapsedTimeNs) {
1096 std::lock_guard<std::mutex> lock(mMetricsMutex);
1097 VLOG("Received boot completed signal");
1098 for (const auto& it : mMetricsManagers) {
1099 it.second->onStatsdInitCompleted(elapsedTimeNs);
1100 }
1101}
1102
David Chen48944902018-05-03 10:29:11 -07001103void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) {
1104 std::lock_guard<std::mutex> lock(mMetricsMutex);
1105 mOnDiskDataConfigs.insert(key);
1106}
1107
Tej Singh8b914c02020-07-14 23:16:24 -07001108void StatsLogProcessor::setAnomalyAlarm(const int64_t elapsedTimeMillis) {
1109 std::lock_guard<std::mutex> lock(mAnomalyAlarmMutex);
1110 mNextAnomalyAlarmTime = elapsedTimeMillis;
1111}
1112
1113void StatsLogProcessor::cancelAnomalyAlarm() {
1114 std::lock_guard<std::mutex> lock(mAnomalyAlarmMutex);
1115 mNextAnomalyAlarmTime = 0;
1116}
1117
1118void StatsLogProcessor::informAnomalyAlarmFiredLocked(const int64_t elapsedTimeMillis) {
1119 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
1120 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
1121 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(elapsedTimeMillis / 1000));
1122 if (alarmSet.size() > 0) {
1123 VLOG("Found periodic alarm fired.");
1124 processFiredAnomalyAlarmsLocked(MillisToNano(elapsedTimeMillis), alarmSet);
1125 } else {
1126 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
1127 }
1128}
1129
Yao Chenef99c4f2017-09-22 16:26:54 -07001130} // namespace statsd
1131} // namespace os
1132} // namespace android