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" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 19 | #include "statslog.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 20 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | #include <dirent.h> |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 23 | #include <frameworks/base/cmds/statsd/src/active_config_list.pb.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 24 | #include "StatsLogProcessor.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 26 | #include "external/StatsPullerManager.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 27 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | #include "metrics/CountMetricProducer.h" |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 29 | #include "stats_log_util.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 30 | #include "stats_util.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 31 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 32 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 33 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 34 | #include <utils/Errors.h> |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 35 | #include <utils/SystemClock.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 36 | |
| 37 | using namespace android; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 38 | using android::base::StringPrintf; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 39 | using android::util::FIELD_COUNT_REPEATED; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 40 | using android::util::FIELD_TYPE_BOOL; |
| 41 | using android::util::FIELD_TYPE_FLOAT; |
| 42 | using android::util::FIELD_TYPE_INT32; |
| 43 | using android::util::FIELD_TYPE_INT64; |
| 44 | using android::util::FIELD_TYPE_MESSAGE; |
| 45 | using android::util::FIELD_TYPE_STRING; |
| 46 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 47 | using std::make_unique; |
| 48 | using std::unique_ptr; |
| 49 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 50 | |
| 51 | namespace android { |
| 52 | namespace os { |
| 53 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 54 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 55 | // for ConfigMetricsReportList |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 56 | const int FIELD_ID_CONFIG_KEY = 1; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 57 | const int FIELD_ID_REPORTS = 2; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 58 | // for ConfigKey |
| 59 | const int FIELD_ID_UID = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 60 | const int FIELD_ID_ID = 2; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 61 | // for ConfigMetricsReport |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 62 | // const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 63 | const int FIELD_ID_UID_MAP = 2; |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 64 | const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3; |
| 65 | const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4; |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 66 | const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5; |
| 67 | const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6; |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 68 | const int FIELD_ID_DUMP_REPORT_REASON = 8; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 69 | const int FIELD_ID_STRINGS = 9; |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 70 | |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 71 | const int FIELD_ID_ACTIVE_CONFIG_LIST = 1; |
| 72 | const int FIELD_ID_CONFIG_ID = 1; |
| 73 | const int FIELD_ID_CONFIG_UID = 2; |
| 74 | const int FIELD_ID_ACTIVE_METRIC = 3; |
| 75 | const int FIELD_ID_METRIC_ID = 1; |
| 76 | const int FIELD_ID_TIME_TO_LIVE_NANOS = 2; |
| 77 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 78 | #define NS_PER_HOUR 3600 * NS_PER_SEC |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 79 | |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 80 | #define STATS_DATA_DIR "/data/misc/stats-data" |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 81 | #define STATS_ACTIVE_METRIC_DIR "/data/misc/stats-active-metric" |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 82 | |
Tej Singh | 42f9e06 | 2018-11-09 10:01:00 -0800 | [diff] [blame] | 83 | // Cool down period for writing data to disk to avoid overwriting files. |
| 84 | #define WRITE_DATA_COOL_DOWN_SEC 5 |
| 85 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 86 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 87 | const sp<StatsPullerManager>& pullerManager, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 88 | const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 89 | const sp<AlarmMonitor>& periodicAlarmMonitor, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 90 | const int64_t timeBaseNs, |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 91 | const std::function<bool(const ConfigKey&)>& sendBroadcast, |
| 92 | const std::function<bool( |
| 93 | const int&, const vector<int64_t>&)>& activateBroadcast) |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 94 | : mUidMap(uidMap), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 95 | mPullerManager(pullerManager), |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 96 | mAnomalyAlarmMonitor(anomalyAlarmMonitor), |
| 97 | mPeriodicAlarmMonitor(periodicAlarmMonitor), |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 98 | mSendBroadcast(sendBroadcast), |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 99 | mSendActivationBroadcast(activateBroadcast), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 100 | mTimeBaseNs(timeBaseNs), |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 101 | mLargestTimestampSeen(0), |
| 102 | mLastTimestampSeen(0) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 103 | mPullerManager->ForceClearPullerCache(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 106 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 109 | void StatsLogProcessor::onAnomalyAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 110 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 111 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 112 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 113 | for (const auto& itr : mMetricsManagers) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 114 | itr.second->onAnomalyAlarmFired(timestampNs, alarmSet); |
| 115 | } |
| 116 | } |
| 117 | void StatsLogProcessor::onPeriodicAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 118 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 119 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) { |
| 120 | |
| 121 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 122 | for (const auto& itr : mMetricsManagers) { |
| 123 | itr.second->onPeriodicAlarmFired(timestampNs, alarmSet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 127 | void updateUid(Value* value, int hostUid) { |
| 128 | int uid = value->int_value; |
| 129 | if (uid != hostUid) { |
| 130 | value->setInt(hostUid); |
| 131 | } |
| 132 | } |
| 133 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 134 | void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const { |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 135 | if (android::util::AtomsInfo::kAtomsWithAttributionChain.find(event->GetTagId()) != |
| 136 | android::util::AtomsInfo::kAtomsWithAttributionChain.end()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 137 | for (auto& value : *(event->getMutableValues())) { |
| 138 | if (value.mField.getPosAtDepth(0) > kAttributionField) { |
| 139 | break; |
| 140 | } |
| 141 | if (isAttributionUidField(value)) { |
| 142 | const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value); |
| 143 | updateUid(&value.mValue, hostUid); |
| 144 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 145 | } |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 146 | } else { |
| 147 | auto it = android::util::AtomsInfo::kAtomsWithUidField.find(event->GetTagId()); |
| 148 | if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) { |
| 149 | int uidField = it->second; // uidField is the field number in proto, |
| 150 | // starting from 1 |
| 151 | if (uidField > 0 && (int)event->getValues().size() >= uidField && |
| 152 | (event->getValues())[uidField - 1].mValue.getType() == INT) { |
| 153 | Value& value = (*event->getMutableValues())[uidField - 1].mValue; |
| 154 | const int hostUid = mUidMap->getHostUidOrSelf(value.int_value); |
| 155 | updateUid(&value, hostUid); |
| 156 | } else { |
| 157 | ALOGE("Malformed log, uid not found. %s", event->ToString().c_str()); |
| 158 | } |
| 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 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 179 | void StatsLogProcessor::resetConfigs() { |
| 180 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 181 | resetConfigsLocked(getElapsedRealtimeNs()); |
| 182 | } |
| 183 | |
| 184 | void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) { |
| 185 | std::vector<ConfigKey> configKeys; |
| 186 | for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) { |
| 187 | configKeys.push_back(it->first); |
| 188 | } |
| 189 | resetConfigsLocked(timestampNs, configKeys); |
| 190 | } |
| 191 | |
Yao Chen | 3ff3a49 | 2018-08-06 16:17:37 -0700 | [diff] [blame] | 192 | void StatsLogProcessor::OnLogEvent(LogEvent* event) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 193 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 194 | |
| 195 | #ifdef VERY_VERBOSE_PRINTING |
| 196 | if (mPrintAllLogs) { |
| 197 | ALOGI("%s", event->ToString().c_str()); |
| 198 | } |
| 199 | #endif |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 200 | const int64_t currentTimestampNs = event->GetElapsedTimestampNs(); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 201 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 202 | resetIfConfigTtlExpiredLocked(currentTimestampNs); |
| 203 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 204 | StatsdStats::getInstance().noteAtomLogged( |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 205 | event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 206 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 207 | // 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] | 208 | // The field numbers need to be currently updated by hand with atoms.proto |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 209 | if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) { |
| 210 | onIsolatedUidChangedEventLocked(*event); |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (mMetricsManagers.empty()) { |
| 214 | return; |
| 215 | } |
| 216 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 217 | int64_t curTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 218 | if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 219 | mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 220 | mLastPullerCacheClearTimeSec = curTimeSec; |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 223 | |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 224 | if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 225 | // Map the isolated uid to host uid if necessary. |
| 226 | mapIsolatedUidToHostUidIfNecessaryLocked(event); |
| 227 | } |
| 228 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 229 | std::unordered_set<int> uidsWithActiveConfigsChanged; |
| 230 | std::unordered_map<int, std::vector<int64_t>> activeConfigsPerUid; |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 231 | // pass the event to metrics managers. |
| 232 | for (auto& pair : mMetricsManagers) { |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 233 | int uid = pair.first.GetUid(); |
| 234 | int64_t configId = pair.first.GetId(); |
| 235 | bool isPrevActive = pair.second->isActive(); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 236 | pair.second->onLogEvent(*event); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 237 | bool isCurActive = pair.second->isActive(); |
| 238 | // Map all active configs by uid. |
| 239 | if (isCurActive) { |
| 240 | auto activeConfigs = activeConfigsPerUid.find(uid); |
| 241 | if (activeConfigs != activeConfigsPerUid.end()) { |
| 242 | activeConfigs->second.push_back(configId); |
| 243 | } else { |
| 244 | vector<int64_t> newActiveConfigs; |
| 245 | newActiveConfigs.push_back(configId); |
| 246 | activeConfigsPerUid[uid] = newActiveConfigs; |
| 247 | } |
| 248 | } |
| 249 | // The activation state of this config changed. |
| 250 | if (isPrevActive != isCurActive) { |
| 251 | VLOG("Active status changed for uid %d", uid); |
| 252 | uidsWithActiveConfigsChanged.insert(uid); |
| 253 | StatsdStats::getInstance().noteActiveStatusChanged(pair.first, isCurActive); |
| 254 | } |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 255 | flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second)); |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 256 | } |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 257 | |
| 258 | for (int uid : uidsWithActiveConfigsChanged) { |
| 259 | // Send broadcast so that receivers can pull data. |
| 260 | auto lastBroadcastTime = mLastActivationBroadcastTimes.find(uid); |
| 261 | if (lastBroadcastTime != mLastActivationBroadcastTimes.end()) { |
| 262 | if (currentTimestampNs - lastBroadcastTime->second < |
| 263 | StatsdStats::kMinActivationBroadcastPeriodNs) { |
| 264 | VLOG("StatsD would've sent an activation broadcast but the rate limit stopped us."); |
| 265 | return; |
| 266 | } |
| 267 | } |
| 268 | auto activeConfigs = activeConfigsPerUid.find(uid); |
| 269 | if (activeConfigs != activeConfigsPerUid.end()) { |
| 270 | if (mSendActivationBroadcast(uid, activeConfigs->second)) { |
| 271 | VLOG("StatsD sent activation notice for uid %d", uid); |
| 272 | mLastActivationBroadcastTimes[uid] = currentTimestampNs; |
| 273 | } |
| 274 | } else { |
| 275 | std::vector<int64_t> emptyActiveConfigs; |
| 276 | if (mSendActivationBroadcast(uid, emptyActiveConfigs)) { |
| 277 | VLOG("StatsD sent EMPTY activation notice for uid %d", uid); |
| 278 | mLastActivationBroadcastTimes[uid] = currentTimestampNs; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void StatsLogProcessor::GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs) { |
| 285 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 286 | GetActiveConfigsLocked(uid, outActiveConfigs); |
| 287 | } |
| 288 | |
| 289 | void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs) { |
| 290 | outActiveConfigs.clear(); |
| 291 | for (auto& pair : mMetricsManagers) { |
| 292 | if (pair.first.GetUid() == uid && pair.second->isActive()) { |
| 293 | outActiveConfigs.push_back(pair.first.GetId()); |
| 294 | } |
| 295 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 298 | void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key, |
| 299 | const StatsdConfig& config) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 300 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 301 | WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 302 | OnConfigUpdatedLocked(timestampNs, key, config); |
| 303 | } |
| 304 | |
| 305 | void StatsLogProcessor::OnConfigUpdatedLocked( |
| 306 | const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 307 | VLOG("Updated configuration for key %s", key.ToString().c_str()); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 308 | sp<MetricsManager> newMetricsManager = |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 309 | new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager, |
| 310 | mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 311 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 312 | mUidMap->OnConfigUpdated(key); |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 313 | if (newMetricsManager->shouldAddUidMapListener()) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 314 | // We have to add listener after the MetricsManager is constructed because it's |
| 315 | // not safe to create wp or sp from this pointer inside its constructor. |
| 316 | mUidMap->addListener(newMetricsManager.get()); |
| 317 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 318 | newMetricsManager->refreshTtl(timestampNs); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 319 | mMetricsManagers[key] = newMetricsManager; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 320 | VLOG("StatsdConfig valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 321 | } else { |
| 322 | // If there is any error in the config, don't use it. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 323 | ALOGE("StatsdConfig NOT valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 324 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 325 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 326 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 327 | size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 328 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 329 | auto it = mMetricsManagers.find(key); |
| 330 | if (it == mMetricsManagers.end()) { |
| 331 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 332 | return 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 333 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 334 | return it->second->byteSize(); |
| 335 | } |
| 336 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 337 | void StatsLogProcessor::dumpStates(int out, bool verbose) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 338 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 339 | FILE* fout = fdopen(out, "w"); |
| 340 | if (fout == NULL) { |
| 341 | return; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 342 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 343 | fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size()); |
| 344 | for (auto metricsManager : mMetricsManagers) { |
| 345 | metricsManager.second->dumpStates(fout, verbose); |
| 346 | } |
| 347 | |
| 348 | fclose(fout); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 349 | } |
| 350 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 351 | /* |
Bookatz | 9cc7b66 | 2018-11-06 10:39:21 -0800 | [diff] [blame] | 352 | * onDumpReport dumps serialized ConfigMetricsReportList into proto. |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 353 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 354 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 355 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 356 | const bool erase_data, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 357 | const DumpReportReason dumpReportReason, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 358 | ProtoOutputStream* proto) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 359 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 360 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 361 | // Start of ConfigKey. |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 362 | uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 363 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
| 364 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId()); |
| 365 | proto->end(configKeyToken); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 366 | // End of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 367 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 368 | // Then, check stats-data directory to see there's any file containing |
| 369 | // ConfigMetricsReport from previous shutdowns to concatenate to reports. |
Bookatz | c71d901 | 2018-12-19 12:28:38 -0800 | [diff] [blame] | 370 | StorageManager::appendConfigMetricsReport(key, proto, erase_data); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 371 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 372 | auto it = mMetricsManagers.find(key); |
| 373 | if (it != mMetricsManagers.end()) { |
| 374 | // This allows another broadcast to be sent within the rate-limit period if we get close to |
| 375 | // filling the buffer again soon. |
| 376 | mLastBroadcastTimes.erase(key); |
| 377 | |
| 378 | // Start of ConfigMetricsReport (reports). |
| 379 | uint64_t reportsToken = |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 380 | proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 381 | onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 382 | erase_data, dumpReportReason, proto); |
| 383 | proto->end(reportsToken); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 384 | // End of ConfigMetricsReport (reports). |
| 385 | } else { |
| 386 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 387 | } |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * onDumpReport dumps serialized ConfigMetricsReportList into outData. |
| 392 | */ |
| 393 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, |
| 394 | const bool include_current_partial_bucket, |
| 395 | const bool erase_data, |
| 396 | const DumpReportReason dumpReportReason, |
| 397 | vector<uint8_t>* outData) { |
| 398 | ProtoOutputStream proto; |
| 399 | onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data, |
| 400 | dumpReportReason, &proto); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 401 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 402 | if (outData != nullptr) { |
| 403 | outData->clear(); |
| 404 | outData->resize(proto.size()); |
| 405 | size_t pos = 0; |
| 406 | auto iter = proto.data(); |
| 407 | while (iter.readBuffer() != NULL) { |
| 408 | size_t toRead = iter.currentToRead(); |
| 409 | std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead); |
| 410 | pos += toRead; |
| 411 | iter.rp()->move(toRead); |
| 412 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 413 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 414 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 415 | StatsdStats::getInstance().noteMetricsReportSent(key, proto.size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 416 | } |
| 417 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 418 | /* |
| 419 | * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData. |
| 420 | */ |
| 421 | void StatsLogProcessor::onConfigMetricsReportLocked(const ConfigKey& key, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 422 | const int64_t dumpTimeStampNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 423 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 424 | const bool erase_data, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 425 | const DumpReportReason dumpReportReason, |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 426 | ProtoOutputStream* proto) { |
| 427 | // We already checked whether key exists in mMetricsManagers in |
| 428 | // WriteDataToDisk. |
| 429 | auto it = mMetricsManagers.find(key); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 430 | if (it == mMetricsManagers.end()) { |
| 431 | return; |
| 432 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 433 | int64_t lastReportTimeNs = it->second->getLastReportTimeNs(); |
| 434 | int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs(); |
| 435 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 436 | std::set<string> str_set; |
| 437 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 438 | // First, fill in ConfigMetricsReport using current data on memory, which |
| 439 | // starts from filling in StatsLogReport's. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 440 | it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 441 | erase_data, &str_set, proto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 442 | |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 443 | // Fill in UidMap if there is at least one metric to report. |
| 444 | // This skips the uid map if it's an empty config. |
| 445 | if (it->second->getNumMetrics() > 0) { |
| 446 | uint64_t uidMapToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 447 | mUidMap->appendUidMap( |
| 448 | dumpTimeStampNs, key, it->second->hashStringInReport() ? &str_set : nullptr, |
| 449 | it->second->versionStringsInReport(), it->second->installerInReport(), proto); |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 450 | proto->end(uidMapToken); |
| 451 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 452 | |
| 453 | // Fill in the timestamps. |
| 454 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS, |
| 455 | (long long)lastReportTimeNs); |
| 456 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS, |
| 457 | (long long)dumpTimeStampNs); |
| 458 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS, |
| 459 | (long long)lastReportWallClockNs); |
| 460 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS, |
| 461 | (long long)getWallClockNs()); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 462 | // Dump report reason |
| 463 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 464 | |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 465 | for (const auto& str : str_set) { |
| 466 | proto->write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 467 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 468 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 469 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 470 | void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs, |
| 471 | const std::vector<ConfigKey>& configs) { |
| 472 | for (const auto& key : configs) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 473 | StatsdConfig config; |
| 474 | if (StorageManager::readConfigFromDisk(key, &config)) { |
| 475 | OnConfigUpdatedLocked(timestampNs, key, config); |
| 476 | StatsdStats::getInstance().noteConfigReset(key); |
| 477 | } else { |
| 478 | ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str()); |
| 479 | auto it = mMetricsManagers.find(key); |
| 480 | if (it != mMetricsManagers.end()) { |
| 481 | it->second->refreshTtl(timestampNs); |
| 482 | } |
| 483 | } |
| 484 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 487 | void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) { |
| 488 | std::vector<ConfigKey> configKeysTtlExpired; |
| 489 | for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) { |
| 490 | if (it->second != nullptr && !it->second->isInTtl(timestampNs)) { |
| 491 | configKeysTtlExpired.push_back(it->first); |
| 492 | } |
| 493 | } |
| 494 | if (configKeysTtlExpired.size() > 0) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 495 | WriteDataToDiskLocked(CONFIG_RESET); |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 496 | resetConfigsLocked(timestampNs, configKeysTtlExpired); |
| 497 | } |
| 498 | } |
| 499 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 500 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 501 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 502 | auto it = mMetricsManagers.find(key); |
| 503 | if (it != mMetricsManagers.end()) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 504 | WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 505 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 506 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 507 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 508 | StatsdStats::getInstance().noteConfigRemoved(key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 509 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 510 | mLastBroadcastTimes.erase(key); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 511 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame^] | 512 | int uid = key.GetUid(); |
| 513 | bool lastConfigForUid = true; |
| 514 | for (auto it : mMetricsManagers) { |
| 515 | if (it.first.GetUid() == uid) { |
| 516 | lastConfigForUid = false; |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | if (lastConfigForUid) { |
| 521 | mLastActivationBroadcastTimes.erase(uid); |
| 522 | } |
| 523 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 524 | if (mMetricsManagers.empty()) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 525 | mPullerManager->ForceClearPullerCache(); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 526 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 529 | void StatsLogProcessor::flushIfNecessaryLocked( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 530 | int64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 531 | auto lastCheckTime = mLastByteSizeTimes.find(key); |
| 532 | if (lastCheckTime != mLastByteSizeTimes.end()) { |
| 533 | if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) { |
| 534 | return; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // We suspect that the byteSize() computation is expensive, so we set a rate limit. |
| 539 | size_t totalBytes = metricsManager.byteSize(); |
| 540 | mLastByteSizeTimes[key] = timestampNs; |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 541 | bool requestDump = false; |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 542 | if (totalBytes > |
| 543 | StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data. |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 544 | metricsManager.dropData(timestampNs); |
Chenjie Yu | c3c30c0 | 2018-10-26 09:48:07 -0700 | [diff] [blame] | 545 | StatsdStats::getInstance().noteDataDropped(key, totalBytes); |
David Chen | 1294295 | 2017-12-04 14:28:43 -0800 | [diff] [blame] | 546 | 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] | 547 | } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) || |
| 548 | (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) { |
| 549 | // Request to send a broadcast if: |
| 550 | // 1. in memory data > threshold OR |
| 551 | // 2. config has old data report on disk. |
| 552 | requestDump = true; |
| 553 | } |
| 554 | |
| 555 | if (requestDump) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 556 | // Send broadcast so that receivers can pull data. |
| 557 | auto lastBroadcastTime = mLastBroadcastTimes.find(key); |
| 558 | if (lastBroadcastTime != mLastBroadcastTimes.end()) { |
| 559 | if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) { |
| 560 | 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] | 561 | return; |
| 562 | } |
| 563 | } |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 564 | if (mSendBroadcast(key)) { |
| 565 | mOnDiskDataConfigs.erase(key); |
| 566 | VLOG("StatsD triggered data fetch for %s", key.ToString().c_str()); |
| 567 | mLastBroadcastTimes[key] = timestampNs; |
| 568 | StatsdStats::getInstance().noteBroadcastSent(key); |
| 569 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 573 | void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key, |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 574 | const int64_t timestampNs, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 575 | const DumpReportReason dumpReportReason) { |
yro | 028091c | 2018-05-09 16:03:27 -0700 | [diff] [blame] | 576 | if (mMetricsManagers.find(key) == mMetricsManagers.end() || |
| 577 | !mMetricsManagers.find(key)->second->shouldWriteToDisk()) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 578 | return; |
| 579 | } |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 580 | ProtoOutputStream proto; |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 581 | onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 582 | true /* erase_data */, dumpReportReason, &proto); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 583 | string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR, |
| 584 | (long)getWallClockSec(), key.GetUid(), (long long)key.GetId()); |
| 585 | android::base::unique_fd fd(open(file_name.c_str(), |
| 586 | O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)); |
| 587 | if (fd == -1) { |
| 588 | ALOGE("Attempt to write %s but failed", file_name.c_str()); |
| 589 | return; |
| 590 | } |
| 591 | proto.flush(fd.get()); |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 592 | // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP. |
| 593 | mOnDiskDataConfigs.insert(key); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 596 | void StatsLogProcessor::WriteMetricsActivationToDisk(int64_t currentTimeNs) { |
| 597 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 598 | ProtoOutputStream proto; |
| 599 | |
| 600 | for (const auto& pair : mMetricsManagers) { |
| 601 | uint64_t activeConfigListToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 602 | FIELD_ID_ACTIVE_CONFIG_LIST); |
| 603 | proto.write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_ID, (long long)pair.first.GetId()); |
| 604 | proto.write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_UID, pair.first.GetUid()); |
| 605 | |
Chenjie Yu | a9a310e | 2019-02-06 13:40:10 -0800 | [diff] [blame] | 606 | vector<MetricProducer*> activeMetrics; |
| 607 | pair.second->prepForShutDown(currentTimeNs); |
| 608 | pair.second->getActiveMetrics(activeMetrics); |
| 609 | for (MetricProducer* metric : activeMetrics) { |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 610 | if (metric->isActive()) { |
| 611 | uint64_t metricToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 612 | FIELD_ID_ACTIVE_METRIC); |
| 613 | proto.write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_ID, |
| 614 | (long long)metric->getMetricId()); |
| 615 | proto.write(FIELD_TYPE_INT64 | FIELD_ID_TIME_TO_LIVE_NANOS, |
| 616 | (long long)metric->getRemainingTtlNs(currentTimeNs)); |
| 617 | proto.end(metricToken); |
| 618 | } |
| 619 | } |
| 620 | proto.end(activeConfigListToken); |
| 621 | } |
| 622 | |
| 623 | string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR); |
| 624 | StorageManager::deleteFile(file_name.c_str()); |
| 625 | android::base::unique_fd fd( |
| 626 | open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)); |
| 627 | if (fd == -1) { |
| 628 | ALOGE("Attempt to write %s but failed", file_name.c_str()); |
| 629 | return; |
| 630 | } |
| 631 | proto.flush(fd.get()); |
| 632 | } |
| 633 | |
| 634 | void StatsLogProcessor::LoadMetricsActivationFromDisk() { |
| 635 | string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR); |
| 636 | int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC); |
| 637 | if (fd != -1) { |
| 638 | string content; |
| 639 | if (android::base::ReadFdToString(fd, &content)) { |
| 640 | ActiveConfigList activeConfigList; |
| 641 | if (activeConfigList.ParseFromString(content)) { |
| 642 | for (int i = 0; i < activeConfigList.active_config_size(); i++) { |
| 643 | const auto& config = activeConfigList.active_config(i); |
| 644 | ConfigKey key(config.uid(), config.config_id()); |
| 645 | auto it = mMetricsManagers.find(key); |
| 646 | if (it == mMetricsManagers.end()) { |
| 647 | ALOGE("No config found for config %s", key.ToString().c_str()); |
| 648 | continue; |
| 649 | } |
| 650 | VLOG("Setting active config %s", key.ToString().c_str()); |
| 651 | it->second->setActiveMetrics(config, mTimeBaseNs); |
| 652 | } |
| 653 | } |
| 654 | VLOG("Successfully loaded %d active configs.", activeConfigList.active_config_size()); |
| 655 | } |
| 656 | close(fd); |
| 657 | } |
| 658 | StorageManager::deleteFile(file_name.c_str()); |
| 659 | } |
| 660 | |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 661 | void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 662 | const int64_t timeNs = getElapsedRealtimeNs(); |
Tej Singh | 42f9e06 | 2018-11-09 10:01:00 -0800 | [diff] [blame] | 663 | // Do not write to disk if we already have in the last few seconds. |
| 664 | // This is to avoid overwriting files that would have the same name if we |
| 665 | // write twice in the same second. |
| 666 | if (static_cast<unsigned long long> (timeNs) < |
| 667 | mLastWriteTimeNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) { |
| 668 | ALOGI("Statsd skipping writing data to disk. Already wrote data in last %d seconds", |
| 669 | WRITE_DATA_COOL_DOWN_SEC); |
| 670 | return; |
| 671 | } |
| 672 | mLastWriteTimeNs = timeNs; |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 673 | for (auto& pair : mMetricsManagers) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 674 | WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 678 | void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 679 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 680 | WriteDataToDiskLocked(dumpReportReason); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 683 | void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) { |
Yangster | 6df5fcc | 2018-04-12 11:04:29 -0700 | [diff] [blame] | 684 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 685 | mPullerManager->OnAlarmFired(timestampNs); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 686 | } |
| 687 | |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 688 | int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) { |
| 689 | auto it = mMetricsManagers.find(key); |
| 690 | if (it == mMetricsManagers.end()) { |
| 691 | return 0; |
| 692 | } else { |
| 693 | return it->second->getLastReportTimeNs(); |
| 694 | } |
| 695 | } |
| 696 | |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 697 | void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) { |
| 698 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 699 | mOnDiskDataConfigs.insert(key); |
| 700 | } |
| 701 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 702 | } // namespace statsd |
| 703 | } // namespace os |
| 704 | } // namespace android |