Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 19 | |
| 20 | #include "StatsLogProcessor.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 21 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 23 | #include <cutils/multiuser.h> |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 24 | #include <frameworks/base/cmds/statsd/src/active_config_list.pb.h> |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 25 | #include <frameworks/base/cmds/statsd/src/experiment_ids.pb.h> |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 26 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 28 | #include "atoms_info.h" |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 29 | #include "external/StatsPullerManager.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 30 | #include "guardrail/StatsdStats.h" |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 31 | #include "logd/LogEvent.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 32 | #include "metrics/CountMetricProducer.h" |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 33 | #include "StatsService.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 34 | #include "state/StateManager.h" |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 35 | #include "stats_log_util.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 36 | #include "stats_util.h" |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 37 | #include "statslog_statsd.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 38 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 39 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 40 | using namespace android; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 41 | using android::base::StringPrintf; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 42 | using android::util::FIELD_COUNT_REPEATED; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 43 | using android::util::FIELD_TYPE_BOOL; |
| 44 | using android::util::FIELD_TYPE_FLOAT; |
| 45 | using android::util::FIELD_TYPE_INT32; |
| 46 | using android::util::FIELD_TYPE_INT64; |
| 47 | using android::util::FIELD_TYPE_MESSAGE; |
| 48 | using android::util::FIELD_TYPE_STRING; |
| 49 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 50 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 51 | |
| 52 | namespace android { |
| 53 | namespace os { |
| 54 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 55 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 56 | // for ConfigMetricsReportList |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 57 | const int FIELD_ID_CONFIG_KEY = 1; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 58 | const int FIELD_ID_REPORTS = 2; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 59 | // for ConfigKey |
| 60 | const int FIELD_ID_UID = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 61 | const int FIELD_ID_ID = 2; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 62 | // for ConfigMetricsReport |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 63 | // const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 64 | const int FIELD_ID_UID_MAP = 2; |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 65 | const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3; |
| 66 | const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4; |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 67 | const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5; |
| 68 | const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6; |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 69 | const int FIELD_ID_DUMP_REPORT_REASON = 8; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 70 | const int FIELD_ID_STRINGS = 9; |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 71 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 72 | // for ActiveConfigList |
| 73 | const int FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG = 1; |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 74 | |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 75 | // for permissions checks |
| 76 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
| 77 | constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS"; |
| 78 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 79 | #define NS_PER_HOUR 3600 * NS_PER_SEC |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 80 | |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 81 | #define STATS_ACTIVE_METRIC_DIR "/data/misc/stats-active-metric" |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 82 | #define STATS_METADATA_DIR "/data/misc/stats-metadata" |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 83 | |
Tej Singh | 42f9e06 | 2018-11-09 10:01:00 -0800 | [diff] [blame] | 84 | // Cool down period for writing data to disk to avoid overwriting files. |
| 85 | #define WRITE_DATA_COOL_DOWN_SEC 5 |
| 86 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 87 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 88 | const sp<StatsPullerManager>& pullerManager, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 89 | const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 90 | const sp<AlarmMonitor>& periodicAlarmMonitor, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 91 | const int64_t timeBaseNs, |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 92 | const std::function<bool(const ConfigKey&)>& sendBroadcast, |
| 93 | const std::function<bool( |
| 94 | const int&, const vector<int64_t>&)>& activateBroadcast) |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 95 | : mUidMap(uidMap), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 96 | mPullerManager(pullerManager), |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 97 | mAnomalyAlarmMonitor(anomalyAlarmMonitor), |
| 98 | mPeriodicAlarmMonitor(periodicAlarmMonitor), |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 99 | mSendBroadcast(sendBroadcast), |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 100 | mSendActivationBroadcast(activateBroadcast), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 101 | mTimeBaseNs(timeBaseNs), |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 102 | mLargestTimestampSeen(0), |
| 103 | mLastTimestampSeen(0) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 104 | mPullerManager->ForceClearPullerCache(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 107 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 110 | static 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 123 | void StatsLogProcessor::onAnomalyAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 124 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 125 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 126 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 127 | for (const auto& itr : mMetricsManagers) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 128 | itr.second->onAnomalyAlarmFired(timestampNs, alarmSet); |
| 129 | } |
| 130 | } |
| 131 | void StatsLogProcessor::onPeriodicAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 132 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 133 | 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 141 | void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const { |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 142 | if (android::util::AtomsInfo::kAtomsWithAttributionChain.find(event->GetTagId()) != |
| 143 | android::util::AtomsInfo::kAtomsWithAttributionChain.end()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 144 | for (auto& value : *(event->getMutableValues())) { |
| 145 | if (value.mField.getPosAtDepth(0) > kAttributionField) { |
| 146 | break; |
| 147 | } |
| 148 | if (isAttributionUidField(value)) { |
| 149 | const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value); |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame^] | 150 | value.mValue.setInt(hostUid); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 151 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 152 | } |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 153 | } else { |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame^] | 154 | int uidFieldIndex = event->getUidFieldIndex(); |
| 155 | if (uidFieldIndex != -1) { |
| 156 | Value& value = (*event->getMutableValues())[uidFieldIndex].mValue; |
| 157 | const int hostUid = mUidMap->getHostUidOrSelf(value.int_value); |
| 158 | value.setInt(hostUid); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 159 | } |
Yao Chen | 312e898 | 2017-12-05 15:29:03 -0800 | [diff] [blame] | 160 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) { |
| 164 | status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR; |
| 165 | bool is_create = event.GetBool(3, &err); |
| 166 | auto parent_uid = int(event.GetLong(1, &err2)); |
| 167 | auto isolated_uid = int(event.GetLong(2, &err3)); |
| 168 | if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) { |
| 169 | if (is_create) { |
| 170 | mUidMap->assignIsolatedUid(isolated_uid, parent_uid); |
| 171 | } else { |
Bookatz | 3c64886 | 2018-05-25 13:32:43 -0700 | [diff] [blame] | 172 | mUidMap->removeIsolatedUid(isolated_uid); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 173 | } |
| 174 | } else { |
| 175 | ALOGE("Failed to parse uid in the isolated uid change event."); |
| 176 | } |
| 177 | } |
| 178 | |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 179 | void StatsLogProcessor::onBinaryPushStateChangedEventLocked(LogEvent* event) { |
| 180 | pid_t pid = event->GetPid(); |
| 181 | uid_t uid = event->GetUid(); |
| 182 | if (!checkPermissionForIds(kPermissionDump, pid, uid) || |
| 183 | !checkPermissionForIds(kPermissionUsage, pid, uid)) { |
| 184 | return; |
| 185 | } |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 186 | // The Get* functions don't modify the status on success, they only write in |
| 187 | // failure statuses, so we can use one status variable for all calls then |
| 188 | // check if it is no longer NO_ERROR. |
| 189 | status_t err = NO_ERROR; |
| 190 | InstallTrainInfo trainInfo; |
| 191 | trainInfo.trainName = string(event->GetString(1 /*train name field id*/, &err)); |
| 192 | trainInfo.trainVersionCode = event->GetLong(2 /*train version field id*/, &err); |
| 193 | trainInfo.requiresStaging = event->GetBool(3 /*requires staging field id*/, &err); |
| 194 | trainInfo.rollbackEnabled = event->GetBool(4 /*rollback enabled field id*/, &err); |
| 195 | trainInfo.requiresLowLatencyMonitor = |
| 196 | event->GetBool(5 /*requires low latency monitor field id*/, &err); |
| 197 | trainInfo.status = int32_t(event->GetLong(6 /*state field id*/, &err)); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 198 | std::vector<uint8_t> trainExperimentIdBytes = |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 199 | event->GetStorage(7 /*experiment ids field id*/, &err); |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 200 | bool is_rollback = event->GetBool(10 /*is rollback field id*/, &err); |
| 201 | |
| 202 | if (err != NO_ERROR) { |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 203 | ALOGE("Failed to parse fields in binary push state changed log event"); |
| 204 | return; |
| 205 | } |
| 206 | ExperimentIds trainExperimentIds; |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 207 | if (!trainExperimentIds.ParseFromArray(trainExperimentIdBytes.data(), |
| 208 | trainExperimentIdBytes.size())) { |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 209 | ALOGE("Failed to parse experimentids in binary push state changed."); |
| 210 | return; |
| 211 | } |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 212 | trainInfo.experimentIds = {trainExperimentIds.experiment_id().begin(), |
| 213 | trainExperimentIds.experiment_id().end()}; |
| 214 | |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 215 | // Update the train info on disk and get any data the logevent is missing. |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 216 | getAndUpdateTrainInfoOnDisk(is_rollback, &trainInfo); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 217 | |
| 218 | std::vector<uint8_t> trainExperimentIdProto; |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 219 | writeExperimentIdsToProto(trainInfo.experimentIds, &trainExperimentIdProto); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 220 | int32_t userId = multiuser_get_user_id(uid); |
| 221 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 222 | event->updateValue(2 /*train version field id*/, trainInfo.trainVersionCode, LONG); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 223 | event->updateValue(7 /*experiment ids field id*/, trainExperimentIdProto, STORAGE); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 224 | event->updateValue(8 /*user id field id*/, userId, INT); |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 225 | |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 226 | // If this event is a rollback event, then the following bits in the event |
| 227 | // are invalid and we will need to update them with the values we pulled |
| 228 | // from disk. |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 229 | if (is_rollback) { |
| 230 | int bit = trainInfo.requiresStaging ? 1 : 0; |
| 231 | event->updateValue(3 /*requires staging field id*/, bit, INT); |
| 232 | bit = trainInfo.rollbackEnabled ? 1 : 0; |
| 233 | event->updateValue(4 /*rollback enabled field id*/, bit, INT); |
| 234 | bit = trainInfo.requiresLowLatencyMonitor ? 1 : 0; |
| 235 | event->updateValue(5 /*requires low latency monitor field id*/, bit, INT); |
| 236 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 239 | void StatsLogProcessor::getAndUpdateTrainInfoOnDisk(bool is_rollback, |
| 240 | InstallTrainInfo* trainInfo) { |
| 241 | // If the train name is empty, we don't know which train to attribute the |
| 242 | // event to, so return early. |
| 243 | if (trainInfo->trainName.empty()) { |
| 244 | return; |
| 245 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 246 | bool readTrainInfoSuccess = false; |
| 247 | InstallTrainInfo trainInfoOnDisk; |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 248 | readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfo->trainName, trainInfoOnDisk); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 249 | |
| 250 | bool resetExperimentIds = false; |
| 251 | if (readTrainInfoSuccess) { |
| 252 | // Keep the old train version if we received an empty version. |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 253 | if (trainInfo->trainVersionCode == -1) { |
| 254 | trainInfo->trainVersionCode = trainInfoOnDisk.trainVersionCode; |
| 255 | } else if (trainInfo->trainVersionCode != trainInfoOnDisk.trainVersionCode) { |
| 256 | // Reset experiment ids if we receive a new non-empty train version. |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 257 | resetExperimentIds = true; |
| 258 | } |
| 259 | |
| 260 | // Reset if we received a different experiment id. |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 261 | if (!trainInfo->experimentIds.empty() && |
| 262 | (trainInfoOnDisk.experimentIds.empty() || |
| 263 | trainInfo->experimentIds.at(0) != trainInfoOnDisk.experimentIds[0])) { |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 264 | resetExperimentIds = true; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Find the right experiment IDs |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 269 | if ((!resetExperimentIds || is_rollback) && readTrainInfoSuccess) { |
| 270 | trainInfo->experimentIds = trainInfoOnDisk.experimentIds; |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 273 | if (!trainInfo->experimentIds.empty()) { |
| 274 | int64_t firstId = trainInfo->experimentIds.at(0); |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 275 | auto& ids = trainInfo->experimentIds; |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 276 | switch (trainInfo->status) { |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 277 | case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALL_SUCCESS: |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 278 | if (find(ids.begin(), ids.end(), firstId + 1) == ids.end()) { |
| 279 | ids.push_back(firstId + 1); |
| 280 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 281 | break; |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 282 | case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_INITIATED: |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 283 | if (find(ids.begin(), ids.end(), firstId + 2) == ids.end()) { |
| 284 | ids.push_back(firstId + 2); |
| 285 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 286 | break; |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 287 | case android::os::statsd::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_SUCCESS: |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 288 | if (find(ids.begin(), ids.end(), firstId + 3) == ids.end()) { |
| 289 | ids.push_back(firstId + 3); |
| 290 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 295 | // If this event is a rollback event, the following fields are invalid and |
| 296 | // need to be replaced by the fields stored to disk. |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 297 | if (is_rollback) { |
| 298 | trainInfo->requiresStaging = trainInfoOnDisk.requiresStaging; |
| 299 | trainInfo->rollbackEnabled = trainInfoOnDisk.rollbackEnabled; |
| 300 | trainInfo->requiresLowLatencyMonitor = trainInfoOnDisk.requiresLowLatencyMonitor; |
| 301 | } |
| 302 | |
| 303 | StorageManager::writeTrainInfo(*trainInfo); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 306 | void StatsLogProcessor::onWatchdogRollbackOccurredLocked(LogEvent* event) { |
| 307 | pid_t pid = event->GetPid(); |
| 308 | uid_t uid = event->GetUid(); |
| 309 | if (!checkPermissionForIds(kPermissionDump, pid, uid) || |
| 310 | !checkPermissionForIds(kPermissionUsage, pid, uid)) { |
| 311 | return; |
| 312 | } |
| 313 | // The Get* functions don't modify the status on success, they only write in |
| 314 | // failure statuses, so we can use one status variable for all calls then |
| 315 | // check if it is no longer NO_ERROR. |
| 316 | status_t err = NO_ERROR; |
| 317 | int32_t rollbackType = int32_t(event->GetInt(1 /*rollback type field id*/, &err)); |
| 318 | string packageName = string(event->GetString(2 /*package name field id*/, &err)); |
| 319 | |
| 320 | if (err != NO_ERROR) { |
| 321 | ALOGE("Failed to parse fields in watchdog rollback occurred log event"); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | vector<int64_t> experimentIds = |
| 326 | processWatchdogRollbackOccurred(rollbackType, packageName); |
| 327 | vector<uint8_t> experimentIdProto; |
| 328 | writeExperimentIdsToProto(experimentIds, &experimentIdProto); |
| 329 | |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 330 | event->updateValue(6 /*experiment ids field id*/, experimentIdProto, STORAGE); |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | vector<int64_t> StatsLogProcessor::processWatchdogRollbackOccurred(const int32_t rollbackTypeIn, |
| 334 | const string& packageNameIn) { |
| 335 | // If the package name is empty, we can't attribute it to any train, so |
| 336 | // return early. |
| 337 | if (packageNameIn.empty()) { |
| 338 | return vector<int64_t>(); |
| 339 | } |
| 340 | bool readTrainInfoSuccess = false; |
| 341 | InstallTrainInfo trainInfoOnDisk; |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 342 | // We use the package name of the event as the train name. |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 343 | readTrainInfoSuccess = StorageManager::readTrainInfo(packageNameIn, trainInfoOnDisk); |
| 344 | |
| 345 | if (!readTrainInfoSuccess) { |
| 346 | return vector<int64_t>(); |
| 347 | } |
| 348 | |
| 349 | if (trainInfoOnDisk.experimentIds.empty()) { |
| 350 | return vector<int64_t>(); |
| 351 | } |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 352 | |
| 353 | int64_t firstId = trainInfoOnDisk.experimentIds[0]; |
| 354 | auto& ids = trainInfoOnDisk.experimentIds; |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 355 | switch (rollbackTypeIn) { |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 356 | case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_INITIATE: |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 357 | if (find(ids.begin(), ids.end(), firstId + 4) == ids.end()) { |
| 358 | ids.push_back(firstId + 4); |
| 359 | } |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 360 | StorageManager::writeTrainInfo(trainInfoOnDisk); |
| 361 | break; |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 362 | case android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED__ROLLBACK_TYPE__ROLLBACK_SUCCESS: |
Jonathan Nguyen | 727a964 | 2020-02-12 17:24:32 -0800 | [diff] [blame] | 363 | if (find(ids.begin(), ids.end(), firstId + 5) == ids.end()) { |
| 364 | ids.push_back(firstId + 5); |
| 365 | } |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 366 | StorageManager::writeTrainInfo(trainInfoOnDisk); |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | return trainInfoOnDisk.experimentIds; |
| 371 | } |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 372 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 373 | void StatsLogProcessor::resetConfigs() { |
| 374 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 375 | resetConfigsLocked(getElapsedRealtimeNs()); |
| 376 | } |
| 377 | |
| 378 | void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) { |
| 379 | std::vector<ConfigKey> configKeys; |
| 380 | for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) { |
| 381 | configKeys.push_back(it->first); |
| 382 | } |
| 383 | resetConfigsLocked(timestampNs, configKeys); |
| 384 | } |
| 385 | |
Yao Chen | 3ff3a49 | 2018-08-06 16:17:37 -0700 | [diff] [blame] | 386 | void StatsLogProcessor::OnLogEvent(LogEvent* event) { |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 387 | OnLogEvent(event, getElapsedRealtimeNs()); |
| 388 | } |
| 389 | |
| 390 | void StatsLogProcessor::OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 391 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 392 | |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 393 | // Hard-coded logic to update train info on disk and fill in any information |
| 394 | // this log event may be missing. |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 395 | if (event->GetTagId() == android::os::statsd::util::BINARY_PUSH_STATE_CHANGED) { |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 396 | onBinaryPushStateChangedEventLocked(event); |
| 397 | } |
| 398 | |
| 399 | // Hard-coded logic to update experiment ids on disk for certain rollback |
| 400 | // types and fill the rollback atom with experiment ids |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 401 | if (event->GetTagId() == android::os::statsd::util::WATCHDOG_ROLLBACK_OCCURRED) { |
Jonathan Nguyen | 703c42f | 2020-02-04 15:54:26 -0800 | [diff] [blame] | 402 | onWatchdogRollbackOccurredLocked(event); |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 405 | #ifdef VERY_VERBOSE_PRINTING |
| 406 | if (mPrintAllLogs) { |
| 407 | ALOGI("%s", event->ToString().c_str()); |
| 408 | } |
| 409 | #endif |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 410 | const int64_t eventElapsedTimeNs = event->GetElapsedTimestampNs(); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 411 | |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 412 | resetIfConfigTtlExpiredLocked(eventElapsedTimeNs); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 413 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 414 | StatsdStats::getInstance().noteAtomLogged( |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 415 | event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 416 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 417 | // Hard-coded logic to update the isolated uid's in the uid-map. |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 418 | // The field numbers need to be currently updated by hand with atoms.proto |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 419 | if (event->GetTagId() == android::os::statsd::util::ISOLATED_UID_CHANGED) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 420 | onIsolatedUidChangedEventLocked(*event); |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 421 | } |
| 422 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 423 | StateManager::getInstance().onLogEvent(*event); |
| 424 | |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 425 | if (mMetricsManagers.empty()) { |
| 426 | return; |
| 427 | } |
| 428 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 429 | int64_t curTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 430 | if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 431 | mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 432 | mLastPullerCacheClearTimeSec = curTimeSec; |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 435 | |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 436 | if (event->GetTagId() != android::os::statsd::util::ISOLATED_UID_CHANGED) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 437 | // Map the isolated uid to host uid if necessary. |
| 438 | mapIsolatedUidToHostUidIfNecessaryLocked(event); |
| 439 | } |
| 440 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 441 | std::unordered_set<int> uidsWithActiveConfigsChanged; |
| 442 | std::unordered_map<int, std::vector<int64_t>> activeConfigsPerUid; |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 443 | // pass the event to metrics managers. |
| 444 | for (auto& pair : mMetricsManagers) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 445 | int uid = pair.first.GetUid(); |
| 446 | int64_t configId = pair.first.GetId(); |
| 447 | bool isPrevActive = pair.second->isActive(); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 448 | pair.second->onLogEvent(*event); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 449 | bool isCurActive = pair.second->isActive(); |
| 450 | // Map all active configs by uid. |
| 451 | if (isCurActive) { |
| 452 | auto activeConfigs = activeConfigsPerUid.find(uid); |
| 453 | if (activeConfigs != activeConfigsPerUid.end()) { |
| 454 | activeConfigs->second.push_back(configId); |
| 455 | } else { |
| 456 | vector<int64_t> newActiveConfigs; |
| 457 | newActiveConfigs.push_back(configId); |
| 458 | activeConfigsPerUid[uid] = newActiveConfigs; |
| 459 | } |
| 460 | } |
| 461 | // The activation state of this config changed. |
| 462 | if (isPrevActive != isCurActive) { |
| 463 | VLOG("Active status changed for uid %d", uid); |
| 464 | uidsWithActiveConfigsChanged.insert(uid); |
| 465 | StatsdStats::getInstance().noteActiveStatusChanged(pair.first, isCurActive); |
| 466 | } |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 467 | flushIfNecessaryLocked(pair.first, *(pair.second)); |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 468 | } |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 469 | |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 470 | // Don't use the event timestamp for the guardrail. |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 471 | for (int uid : uidsWithActiveConfigsChanged) { |
| 472 | // Send broadcast so that receivers can pull data. |
| 473 | auto lastBroadcastTime = mLastActivationBroadcastTimes.find(uid); |
| 474 | if (lastBroadcastTime != mLastActivationBroadcastTimes.end()) { |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 475 | if (elapsedRealtimeNs - lastBroadcastTime->second < |
| 476 | StatsdStats::kMinActivationBroadcastPeriodNs) { |
Tej Singh | 16ca28f | 2019-06-24 11:58:23 -0700 | [diff] [blame] | 477 | StatsdStats::getInstance().noteActivationBroadcastGuardrailHit(uid); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 478 | VLOG("StatsD would've sent an activation broadcast but the rate limit stopped us."); |
| 479 | return; |
| 480 | } |
| 481 | } |
| 482 | auto activeConfigs = activeConfigsPerUid.find(uid); |
| 483 | if (activeConfigs != activeConfigsPerUid.end()) { |
| 484 | if (mSendActivationBroadcast(uid, activeConfigs->second)) { |
| 485 | VLOG("StatsD sent activation notice for uid %d", uid); |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 486 | mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 487 | } |
| 488 | } else { |
| 489 | std::vector<int64_t> emptyActiveConfigs; |
| 490 | if (mSendActivationBroadcast(uid, emptyActiveConfigs)) { |
| 491 | VLOG("StatsD sent EMPTY activation notice for uid %d", uid); |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 492 | mLastActivationBroadcastTimes[uid] = elapsedRealtimeNs; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void StatsLogProcessor::GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs) { |
| 499 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 500 | GetActiveConfigsLocked(uid, outActiveConfigs); |
| 501 | } |
| 502 | |
| 503 | void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs) { |
| 504 | outActiveConfigs.clear(); |
| 505 | for (auto& pair : mMetricsManagers) { |
| 506 | if (pair.first.GetUid() == uid && pair.second->isActive()) { |
| 507 | outActiveConfigs.push_back(pair.first.GetId()); |
| 508 | } |
| 509 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 512 | void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key, |
| 513 | const StatsdConfig& config) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 514 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 515 | WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED, NO_TIME_CONSTRAINTS); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 516 | OnConfigUpdatedLocked(timestampNs, key, config); |
| 517 | } |
| 518 | |
| 519 | void StatsLogProcessor::OnConfigUpdatedLocked( |
| 520 | const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 521 | VLOG("Updated configuration for key %s", key.ToString().c_str()); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 522 | sp<MetricsManager> newMetricsManager = |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 523 | new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager, |
| 524 | mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 525 | if (newMetricsManager->isConfigValid()) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 526 | newMetricsManager->init(); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 527 | mUidMap->OnConfigUpdated(key); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 528 | newMetricsManager->refreshTtl(timestampNs); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 529 | mMetricsManagers[key] = newMetricsManager; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 530 | VLOG("StatsdConfig valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 531 | } else { |
| 532 | // If there is any error in the config, don't use it. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 533 | ALOGE("StatsdConfig NOT valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 534 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 535 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 536 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 537 | size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 538 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 539 | auto it = mMetricsManagers.find(key); |
| 540 | if (it == mMetricsManagers.end()) { |
| 541 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 542 | return 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 543 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 544 | return it->second->byteSize(); |
| 545 | } |
| 546 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 547 | void StatsLogProcessor::dumpStates(int out, bool verbose) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 548 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 549 | FILE* fout = fdopen(out, "w"); |
| 550 | if (fout == NULL) { |
| 551 | return; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 552 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 553 | fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size()); |
| 554 | for (auto metricsManager : mMetricsManagers) { |
| 555 | metricsManager.second->dumpStates(fout, verbose); |
| 556 | } |
| 557 | |
| 558 | fclose(fout); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 559 | } |
| 560 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 561 | /* |
Bookatz | 9cc7b66 | 2018-11-06 10:39:21 -0800 | [diff] [blame] | 562 | * onDumpReport dumps serialized ConfigMetricsReportList into proto. |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 563 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 564 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 565 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 566 | const bool erase_data, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 567 | const DumpReportReason dumpReportReason, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 568 | const DumpLatency dumpLatency, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 569 | ProtoOutputStream* proto) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 570 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 571 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 572 | // Start of ConfigKey. |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 573 | uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 574 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
| 575 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId()); |
| 576 | proto->end(configKeyToken); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 577 | // End of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 578 | |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 579 | bool keepFile = false; |
| 580 | auto it = mMetricsManagers.find(key); |
| 581 | if (it != mMetricsManagers.end() && it->second->shouldPersistLocalHistory()) { |
| 582 | keepFile = true; |
| 583 | } |
| 584 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 585 | // Then, check stats-data directory to see there's any file containing |
| 586 | // ConfigMetricsReport from previous shutdowns to concatenate to reports. |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 587 | StorageManager::appendConfigMetricsReport( |
| 588 | key, proto, erase_data && !keepFile /* should remove file after appending it */, |
| 589 | dumpReportReason == ADB_DUMP /*if caller is adb*/); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 590 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 591 | if (it != mMetricsManagers.end()) { |
| 592 | // This allows another broadcast to be sent within the rate-limit period if we get close to |
| 593 | // filling the buffer again soon. |
| 594 | mLastBroadcastTimes.erase(key); |
| 595 | |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 596 | vector<uint8_t> buffer; |
| 597 | onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket, |
| 598 | erase_data, dumpReportReason, dumpLatency, |
| 599 | false /* is this data going to be saved on disk */, &buffer); |
| 600 | proto->write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS, |
| 601 | reinterpret_cast<char*>(buffer.data()), buffer.size()); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 602 | } else { |
| 603 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 604 | } |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
| 608 | * onDumpReport dumps serialized ConfigMetricsReportList into outData. |
| 609 | */ |
| 610 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, |
| 611 | const bool include_current_partial_bucket, |
| 612 | const bool erase_data, |
| 613 | const DumpReportReason dumpReportReason, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 614 | const DumpLatency dumpLatency, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 615 | vector<uint8_t>* outData) { |
| 616 | ProtoOutputStream proto; |
| 617 | onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 618 | dumpReportReason, dumpLatency, &proto); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 619 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 620 | if (outData != nullptr) { |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 621 | flushProtoToBuffer(proto, outData); |
| 622 | VLOG("output data size %zu", outData->size()); |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 623 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 624 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 625 | StatsdStats::getInstance().noteMetricsReportSent(key, proto.size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 626 | } |
| 627 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 628 | /* |
| 629 | * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData. |
| 630 | */ |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 631 | void StatsLogProcessor::onConfigMetricsReportLocked( |
| 632 | const ConfigKey& key, const int64_t dumpTimeStampNs, |
| 633 | const bool include_current_partial_bucket, const bool erase_data, |
| 634 | const DumpReportReason dumpReportReason, const DumpLatency dumpLatency, |
| 635 | const bool dataSavedOnDisk, vector<uint8_t>* buffer) { |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 636 | // We already checked whether key exists in mMetricsManagers in |
| 637 | // WriteDataToDisk. |
| 638 | auto it = mMetricsManagers.find(key); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 639 | if (it == mMetricsManagers.end()) { |
| 640 | return; |
| 641 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 642 | int64_t lastReportTimeNs = it->second->getLastReportTimeNs(); |
| 643 | int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs(); |
| 644 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 645 | std::set<string> str_set; |
| 646 | |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 647 | ProtoOutputStream tempProto; |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 648 | // First, fill in ConfigMetricsReport using current data on memory, which |
| 649 | // starts from filling in StatsLogReport's. |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 650 | it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data, |
| 651 | dumpLatency, &str_set, &tempProto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 652 | |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 653 | // Fill in UidMap if there is at least one metric to report. |
| 654 | // This skips the uid map if it's an empty config. |
| 655 | if (it->second->getNumMetrics() > 0) { |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 656 | uint64_t uidMapToken = tempProto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 657 | mUidMap->appendUidMap( |
| 658 | dumpTimeStampNs, key, it->second->hashStringInReport() ? &str_set : nullptr, |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 659 | it->second->versionStringsInReport(), it->second->installerInReport(), &tempProto); |
| 660 | tempProto.end(uidMapToken); |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 661 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 662 | |
| 663 | // Fill in the timestamps. |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 664 | tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS, |
| 665 | (long long)lastReportTimeNs); |
| 666 | tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS, |
| 667 | (long long)dumpTimeStampNs); |
| 668 | tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS, |
| 669 | (long long)lastReportWallClockNs); |
| 670 | tempProto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS, |
| 671 | (long long)getWallClockNs()); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 672 | // Dump report reason |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 673 | tempProto.write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 674 | |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 675 | for (const auto& str : str_set) { |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 676 | tempProto.write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str); |
| 677 | } |
| 678 | |
| 679 | flushProtoToBuffer(tempProto, buffer); |
| 680 | |
| 681 | // save buffer to disk if needed |
| 682 | if (erase_data && !dataSavedOnDisk && it->second->shouldPersistLocalHistory()) { |
| 683 | VLOG("save history to disk"); |
| 684 | string file_name = StorageManager::getDataHistoryFileName((long)getWallClockSec(), |
| 685 | key.GetUid(), key.GetId()); |
| 686 | StorageManager::writeFile(file_name.c_str(), buffer->data(), buffer->size()); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 687 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 688 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 689 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 690 | void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs, |
| 691 | const std::vector<ConfigKey>& configs) { |
| 692 | for (const auto& key : configs) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 693 | StatsdConfig config; |
| 694 | if (StorageManager::readConfigFromDisk(key, &config)) { |
| 695 | OnConfigUpdatedLocked(timestampNs, key, config); |
| 696 | StatsdStats::getInstance().noteConfigReset(key); |
| 697 | } else { |
| 698 | ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str()); |
| 699 | auto it = mMetricsManagers.find(key); |
| 700 | if (it != mMetricsManagers.end()) { |
| 701 | it->second->refreshTtl(timestampNs); |
| 702 | } |
| 703 | } |
| 704 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 707 | void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) { |
| 708 | std::vector<ConfigKey> configKeysTtlExpired; |
| 709 | for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) { |
| 710 | if (it->second != nullptr && !it->second->isInTtl(timestampNs)) { |
| 711 | configKeysTtlExpired.push_back(it->first); |
| 712 | } |
| 713 | } |
| 714 | if (configKeysTtlExpired.size() > 0) { |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 715 | WriteDataToDiskLocked(CONFIG_RESET, NO_TIME_CONSTRAINTS); |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 716 | resetConfigsLocked(timestampNs, configKeysTtlExpired); |
| 717 | } |
| 718 | } |
| 719 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 720 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 721 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 722 | auto it = mMetricsManagers.find(key); |
| 723 | if (it != mMetricsManagers.end()) { |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 724 | WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 725 | NO_TIME_CONSTRAINTS); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 726 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 727 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 728 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 729 | StatsdStats::getInstance().noteConfigRemoved(key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 730 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 731 | mLastBroadcastTimes.erase(key); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 732 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 733 | int uid = key.GetUid(); |
| 734 | bool lastConfigForUid = true; |
| 735 | for (auto it : mMetricsManagers) { |
| 736 | if (it.first.GetUid() == uid) { |
| 737 | lastConfigForUid = false; |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | if (lastConfigForUid) { |
| 742 | mLastActivationBroadcastTimes.erase(uid); |
| 743 | } |
| 744 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 745 | if (mMetricsManagers.empty()) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 746 | mPullerManager->ForceClearPullerCache(); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 747 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 750 | void StatsLogProcessor::flushIfNecessaryLocked(const ConfigKey& key, |
| 751 | MetricsManager& metricsManager) { |
| 752 | int64_t elapsedRealtimeNs = getElapsedRealtimeNs(); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 753 | auto lastCheckTime = mLastByteSizeTimes.find(key); |
| 754 | if (lastCheckTime != mLastByteSizeTimes.end()) { |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 755 | if (elapsedRealtimeNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 756 | return; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | // We suspect that the byteSize() computation is expensive, so we set a rate limit. |
| 761 | size_t totalBytes = metricsManager.byteSize(); |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 762 | mLastByteSizeTimes[key] = elapsedRealtimeNs; |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 763 | bool requestDump = false; |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 764 | if (totalBytes > StatsdStats::kMaxMetricsBytesPerConfig) { |
| 765 | // Too late. We need to start clearing data. |
| 766 | metricsManager.dropData(elapsedRealtimeNs); |
Chenjie Yu | c3c30c0 | 2018-10-26 09:48:07 -0700 | [diff] [blame] | 767 | StatsdStats::getInstance().noteDataDropped(key, totalBytes); |
David Chen | 1294295 | 2017-12-04 14:28:43 -0800 | [diff] [blame] | 768 | VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str()); |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 769 | } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) || |
| 770 | (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) { |
| 771 | // Request to send a broadcast if: |
| 772 | // 1. in memory data > threshold OR |
| 773 | // 2. config has old data report on disk. |
| 774 | requestDump = true; |
| 775 | } |
| 776 | |
| 777 | if (requestDump) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 778 | // Send broadcast so that receivers can pull data. |
| 779 | auto lastBroadcastTime = mLastBroadcastTimes.find(key); |
| 780 | if (lastBroadcastTime != mLastBroadcastTimes.end()) { |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 781 | if (elapsedRealtimeNs - lastBroadcastTime->second < |
| 782 | StatsdStats::kMinBroadcastPeriodNs) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 783 | VLOG("StatsD would've sent a broadcast but the rate limit stopped us."); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | } |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 787 | if (mSendBroadcast(key)) { |
| 788 | mOnDiskDataConfigs.erase(key); |
| 789 | VLOG("StatsD triggered data fetch for %s", key.ToString().c_str()); |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 790 | mLastBroadcastTimes[key] = elapsedRealtimeNs; |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 791 | StatsdStats::getInstance().noteBroadcastSent(key); |
| 792 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 796 | void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key, |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 797 | const int64_t timestampNs, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 798 | const DumpReportReason dumpReportReason, |
| 799 | const DumpLatency dumpLatency) { |
yro | 028091c | 2018-05-09 16:03:27 -0700 | [diff] [blame] | 800 | if (mMetricsManagers.find(key) == mMetricsManagers.end() || |
| 801 | !mMetricsManagers.find(key)->second->shouldWriteToDisk()) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 802 | return; |
| 803 | } |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 804 | vector<uint8_t> buffer; |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 805 | onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/, |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 806 | true /* erase_data */, dumpReportReason, dumpLatency, true, |
| 807 | &buffer); |
| 808 | string file_name = |
| 809 | StorageManager::getDataFileName((long)getWallClockSec(), key.GetUid(), key.GetId()); |
| 810 | StorageManager::writeFile(file_name.c_str(), buffer.data(), buffer.size()); |
| 811 | |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 812 | // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP. |
| 813 | mOnDiskDataConfigs.insert(key); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 816 | void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) { |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 817 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
jianjin | 996f40d | 2019-03-29 13:53:48 -0700 | [diff] [blame] | 818 | const int64_t timeNs = getElapsedRealtimeNs(); |
| 819 | // Do not write to disk if we already have in the last few seconds. |
jianjin | 996f40d | 2019-03-29 13:53:48 -0700 | [diff] [blame] | 820 | if (static_cast<unsigned long long> (timeNs) < |
| 821 | mLastActiveMetricsWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) { |
| 822 | ALOGI("Statsd skipping writing active metrics to disk. Already wrote data in last %d seconds", |
| 823 | WRITE_DATA_COOL_DOWN_SEC); |
| 824 | return; |
| 825 | } |
| 826 | mLastActiveMetricsWriteNs = timeNs; |
| 827 | |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 828 | ProtoOutputStream proto; |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 829 | WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, DEVICE_SHUTDOWN, &proto); |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 830 | |
| 831 | string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR); |
| 832 | StorageManager::deleteFile(file_name.c_str()); |
| 833 | android::base::unique_fd fd( |
| 834 | open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)); |
| 835 | if (fd == -1) { |
| 836 | ALOGE("Attempt to write %s but failed", file_name.c_str()); |
| 837 | return; |
| 838 | } |
| 839 | proto.flush(fd.get()); |
| 840 | } |
| 841 | |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 842 | void StatsLogProcessor::SaveMetadataToDisk(int64_t currentWallClockTimeNs, |
| 843 | int64_t systemElapsedTimeNs) { |
| 844 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 845 | // Do not write to disk if we already have in the last few seconds. |
| 846 | if (static_cast<unsigned long long> (systemElapsedTimeNs) < |
| 847 | mLastMetadataWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) { |
| 848 | ALOGI("Statsd skipping writing metadata to disk. Already wrote data in last %d seconds", |
| 849 | WRITE_DATA_COOL_DOWN_SEC); |
| 850 | return; |
| 851 | } |
| 852 | mLastMetadataWriteNs = systemElapsedTimeNs; |
| 853 | |
| 854 | metadata::StatsMetadataList metadataList; |
| 855 | WriteMetadataToProtoLocked( |
| 856 | currentWallClockTimeNs, systemElapsedTimeNs, &metadataList); |
| 857 | |
| 858 | string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR); |
| 859 | StorageManager::deleteFile(file_name.c_str()); |
| 860 | |
| 861 | if (metadataList.stats_metadata_size() == 0) { |
| 862 | // Skip the write if we have nothing to write. |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | std::string data; |
| 867 | metadataList.SerializeToString(&data); |
| 868 | StorageManager::writeFile(file_name.c_str(), data.c_str(), data.size()); |
| 869 | } |
| 870 | |
| 871 | void StatsLogProcessor::WriteMetadataToProto(int64_t currentWallClockTimeNs, |
| 872 | int64_t systemElapsedTimeNs, |
| 873 | metadata::StatsMetadataList* metadataList) { |
| 874 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 875 | WriteMetadataToProtoLocked(currentWallClockTimeNs, systemElapsedTimeNs, metadataList); |
| 876 | } |
| 877 | |
| 878 | void StatsLogProcessor::WriteMetadataToProtoLocked(int64_t currentWallClockTimeNs, |
| 879 | int64_t systemElapsedTimeNs, |
| 880 | metadata::StatsMetadataList* metadataList) { |
| 881 | for (const auto& pair : mMetricsManagers) { |
| 882 | const sp<MetricsManager>& metricsManager = pair.second; |
| 883 | metadata::StatsMetadata* statsMetadata = metadataList->add_stats_metadata(); |
| 884 | bool metadataWritten = metricsManager->writeMetadataToProto(currentWallClockTimeNs, |
| 885 | systemElapsedTimeNs, statsMetadata); |
| 886 | if (!metadataWritten) { |
| 887 | metadataList->mutable_stats_metadata()->RemoveLast(); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
Jeffrey Huang | 475677e | 2020-03-30 19:52:07 -0700 | [diff] [blame] | 892 | void StatsLogProcessor::LoadMetadataFromDisk(int64_t currentWallClockTimeNs, |
| 893 | int64_t systemElapsedTimeNs) { |
| 894 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 895 | string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR); |
| 896 | int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC); |
| 897 | if (-1 == fd) { |
| 898 | VLOG("Attempt to read %s but failed", file_name.c_str()); |
| 899 | StorageManager::deleteFile(file_name.c_str()); |
| 900 | return; |
| 901 | } |
| 902 | string content; |
| 903 | if (!android::base::ReadFdToString(fd, &content)) { |
| 904 | ALOGE("Attempt to read %s but failed", file_name.c_str()); |
| 905 | close(fd); |
| 906 | StorageManager::deleteFile(file_name.c_str()); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | close(fd); |
| 911 | |
| 912 | metadata::StatsMetadataList statsMetadataList; |
| 913 | if (!statsMetadataList.ParseFromString(content)) { |
| 914 | ALOGE("Attempt to read %s but failed; failed to metadata", file_name.c_str()); |
| 915 | StorageManager::deleteFile(file_name.c_str()); |
| 916 | return; |
| 917 | } |
| 918 | SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs); |
| 919 | StorageManager::deleteFile(file_name.c_str()); |
| 920 | } |
| 921 | |
| 922 | void StatsLogProcessor::SetMetadataState(const metadata::StatsMetadataList& statsMetadataList, |
| 923 | int64_t currentWallClockTimeNs, |
| 924 | int64_t systemElapsedTimeNs) { |
| 925 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 926 | SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs); |
| 927 | } |
| 928 | |
| 929 | void StatsLogProcessor::SetMetadataStateLocked( |
| 930 | const metadata::StatsMetadataList& statsMetadataList, |
| 931 | int64_t currentWallClockTimeNs, |
| 932 | int64_t systemElapsedTimeNs) { |
| 933 | for (const metadata::StatsMetadata& metadata : statsMetadataList.stats_metadata()) { |
| 934 | ConfigKey key(metadata.config_key().uid(), metadata.config_key().config_id()); |
| 935 | auto it = mMetricsManagers.find(key); |
| 936 | if (it == mMetricsManagers.end()) { |
| 937 | ALOGE("No config found for configKey %s", key.ToString().c_str()); |
| 938 | continue; |
| 939 | } |
| 940 | VLOG("Setting metadata %s", key.ToString().c_str()); |
| 941 | it->second->loadMetadata(metadata, currentWallClockTimeNs, systemElapsedTimeNs); |
| 942 | } |
| 943 | VLOG("Successfully loaded %d metadata.", statsMetadataList.stats_metadata_size()); |
| 944 | } |
| 945 | |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 946 | void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream( |
| 947 | int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) { |
| 948 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 949 | WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, reason, proto); |
| 950 | } |
| 951 | |
| 952 | void StatsLogProcessor::WriteActiveConfigsToProtoOutputStreamLocked( |
| 953 | int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) { |
| 954 | for (const auto& pair : mMetricsManagers) { |
| 955 | const sp<MetricsManager>& metricsManager = pair.second; |
| 956 | uint64_t configToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 957 | FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG); |
| 958 | metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, reason, proto); |
| 959 | proto->end(configToken); |
| 960 | } |
| 961 | } |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 962 | void StatsLogProcessor::LoadActiveConfigsFromDisk() { |
| 963 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 964 | string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR); |
| 965 | int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 966 | if (-1 == fd) { |
| 967 | VLOG("Attempt to read %s but failed", file_name.c_str()); |
| 968 | StorageManager::deleteFile(file_name.c_str()); |
| 969 | return; |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 970 | } |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 971 | string content; |
| 972 | if (!android::base::ReadFdToString(fd, &content)) { |
| 973 | ALOGE("Attempt to read %s but failed", file_name.c_str()); |
| 974 | close(fd); |
| 975 | StorageManager::deleteFile(file_name.c_str()); |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | close(fd); |
| 980 | |
| 981 | ActiveConfigList activeConfigList; |
| 982 | if (!activeConfigList.ParseFromString(content)) { |
| 983 | ALOGE("Attempt to read %s but failed; failed to load active configs", file_name.c_str()); |
| 984 | StorageManager::deleteFile(file_name.c_str()); |
| 985 | return; |
| 986 | } |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 987 | // Passing in mTimeBaseNs only works as long as we only load from disk is when statsd starts. |
| 988 | SetConfigsActiveStateLocked(activeConfigList, mTimeBaseNs); |
| 989 | StorageManager::deleteFile(file_name.c_str()); |
| 990 | } |
| 991 | |
| 992 | void StatsLogProcessor::SetConfigsActiveState(const ActiveConfigList& activeConfigList, |
| 993 | int64_t currentTimeNs) { |
| 994 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 995 | SetConfigsActiveStateLocked(activeConfigList, currentTimeNs); |
| 996 | } |
| 997 | |
| 998 | void StatsLogProcessor::SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList, |
| 999 | int64_t currentTimeNs) { |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1000 | for (int i = 0; i < activeConfigList.config_size(); i++) { |
| 1001 | const auto& config = activeConfigList.config(i); |
| 1002 | ConfigKey key(config.uid(), config.id()); |
| 1003 | auto it = mMetricsManagers.find(key); |
| 1004 | if (it == mMetricsManagers.end()) { |
| 1005 | ALOGE("No config found for config %s", key.ToString().c_str()); |
| 1006 | continue; |
| 1007 | } |
| 1008 | VLOG("Setting active config %s", key.ToString().c_str()); |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1009 | it->second->loadActiveConfig(config, currentTimeNs); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1010 | } |
| 1011 | VLOG("Successfully loaded %d active configs.", activeConfigList.config_size()); |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1014 | void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason, |
| 1015 | const DumpLatency dumpLatency) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 1016 | const int64_t timeNs = getElapsedRealtimeNs(); |
Tej Singh | 42f9e06 | 2018-11-09 10:01:00 -0800 | [diff] [blame] | 1017 | // Do not write to disk if we already have in the last few seconds. |
| 1018 | // This is to avoid overwriting files that would have the same name if we |
| 1019 | // write twice in the same second. |
| 1020 | if (static_cast<unsigned long long> (timeNs) < |
| 1021 | mLastWriteTimeNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) { |
| 1022 | ALOGI("Statsd skipping writing data to disk. Already wrote data in last %d seconds", |
| 1023 | WRITE_DATA_COOL_DOWN_SEC); |
| 1024 | return; |
| 1025 | } |
| 1026 | mLastWriteTimeNs = timeNs; |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 1027 | for (auto& pair : mMetricsManagers) { |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1028 | WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason, dumpLatency); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 1032 | void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1033 | const DumpLatency dumpLatency) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 1034 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 1035 | WriteDataToDiskLocked(dumpReportReason, dumpLatency); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 1038 | void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) { |
Yangster | 6df5fcc | 2018-04-12 11:04:29 -0700 | [diff] [blame] | 1039 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1040 | mPullerManager->OnAlarmFired(timestampNs); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 1043 | int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) { |
| 1044 | auto it = mMetricsManagers.find(key); |
| 1045 | if (it == mMetricsManagers.end()) { |
| 1046 | return 0; |
| 1047 | } else { |
| 1048 | return it->second->getLastReportTimeNs(); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 1052 | void StatsLogProcessor::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, |
| 1053 | const int uid, const int64_t version) { |
| 1054 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 1055 | ALOGW("Received app upgrade"); |
| 1056 | for (auto it : mMetricsManagers) { |
| 1057 | it.second->notifyAppUpgrade(eventTimeNs, apk, uid, version); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | void StatsLogProcessor::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, |
| 1062 | const int uid) { |
| 1063 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 1064 | ALOGW("Received app removed"); |
| 1065 | for (auto it : mMetricsManagers) { |
| 1066 | it.second->notifyAppRemoved(eventTimeNs, apk, uid); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | void StatsLogProcessor::onUidMapReceived(const int64_t& eventTimeNs) { |
| 1071 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 1072 | ALOGW("Received uid map"); |
| 1073 | for (auto it : mMetricsManagers) { |
| 1074 | it.second->onUidMapReceived(eventTimeNs); |
| 1075 | } |
| 1076 | } |
| 1077 | |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 1078 | void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) { |
| 1079 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 1080 | mOnDiskDataConfigs.insert(key); |
| 1081 | } |
| 1082 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 1083 | } // namespace statsd |
| 1084 | } // namespace os |
| 1085 | } // namespace android |