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 | |
Yangster-mac | 754e29e | 2018-05-02 12:23:17 -0700 | [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> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "StatsLogProcessor.h" |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 24 | #include "stats_log_util.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 26 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 27 | #include "metrics/CountMetricProducer.h" |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 28 | #include "external/StatsPullerManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 29 | #include "stats_util.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 30 | #include "storage/StorageManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 32 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 33 | #include <utils/Errors.h> |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 34 | #include <utils/SystemClock.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 35 | |
| 36 | using namespace android; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 38 | using android::util::FIELD_COUNT_REPEATED; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 39 | using android::util::FIELD_TYPE_BOOL; |
| 40 | using android::util::FIELD_TYPE_FLOAT; |
| 41 | using android::util::FIELD_TYPE_INT32; |
| 42 | using android::util::FIELD_TYPE_INT64; |
| 43 | using android::util::FIELD_TYPE_MESSAGE; |
| 44 | using android::util::FIELD_TYPE_STRING; |
| 45 | using android::util::ProtoOutputStream; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 46 | using std::make_unique; |
| 47 | using std::unique_ptr; |
| 48 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 49 | |
| 50 | namespace android { |
| 51 | namespace os { |
| 52 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 53 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 54 | // for ConfigMetricsReportList |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 55 | const int FIELD_ID_CONFIG_KEY = 1; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 56 | const int FIELD_ID_REPORTS = 2; |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 57 | // for ConfigKey |
| 58 | const int FIELD_ID_UID = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 59 | const int FIELD_ID_ID = 2; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 60 | // for ConfigMetricsReport |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 61 | // const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 62 | const int FIELD_ID_UID_MAP = 2; |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 63 | const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3; |
| 64 | const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4; |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 65 | const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5; |
| 66 | const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6; |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 67 | const int FIELD_ID_DUMP_REPORT_REASON = 8; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 68 | const int FIELD_ID_STRINGS = 9; |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 69 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 70 | #define NS_PER_HOUR 3600 * NS_PER_SEC |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 71 | |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 72 | #define STATS_DATA_DIR "/data/misc/stats-data" |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 73 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 74 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 75 | const sp<StatsPullerManager>& pullerManager, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 76 | const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 77 | const sp<AlarmMonitor>& periodicAlarmMonitor, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 78 | const int64_t timeBaseNs, |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 79 | const std::function<bool(const ConfigKey&)>& sendBroadcast) |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 80 | : mUidMap(uidMap), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 81 | mPullerManager(pullerManager), |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 82 | mAnomalyAlarmMonitor(anomalyAlarmMonitor), |
| 83 | mPeriodicAlarmMonitor(periodicAlarmMonitor), |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 84 | mSendBroadcast(sendBroadcast), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 85 | mTimeBaseNs(timeBaseNs), |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 86 | mLargestTimestampSeen(0), |
| 87 | mLastTimestampSeen(0) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 88 | mPullerManager->ForceClearPullerCache(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 91 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 94 | void StatsLogProcessor::onAnomalyAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 95 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 96 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 97 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 98 | for (const auto& itr : mMetricsManagers) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 99 | itr.second->onAnomalyAlarmFired(timestampNs, alarmSet); |
| 100 | } |
| 101 | } |
| 102 | void StatsLogProcessor::onPeriodicAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 103 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 104 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) { |
| 105 | |
| 106 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 107 | for (const auto& itr : mMetricsManagers) { |
| 108 | itr.second->onPeriodicAlarmFired(timestampNs, alarmSet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 112 | void updateUid(Value* value, int hostUid) { |
| 113 | int uid = value->int_value; |
| 114 | if (uid != hostUid) { |
| 115 | value->setInt(hostUid); |
| 116 | } |
| 117 | } |
| 118 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 119 | void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const { |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 120 | if (android::util::AtomsInfo::kAtomsWithAttributionChain.find(event->GetTagId()) != |
| 121 | android::util::AtomsInfo::kAtomsWithAttributionChain.end()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 122 | for (auto& value : *(event->getMutableValues())) { |
| 123 | if (value.mField.getPosAtDepth(0) > kAttributionField) { |
| 124 | break; |
| 125 | } |
| 126 | if (isAttributionUidField(value)) { |
| 127 | const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value); |
| 128 | updateUid(&value.mValue, hostUid); |
| 129 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 130 | } |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 131 | } else { |
| 132 | auto it = android::util::AtomsInfo::kAtomsWithUidField.find(event->GetTagId()); |
| 133 | if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) { |
| 134 | int uidField = it->second; // uidField is the field number in proto, |
| 135 | // starting from 1 |
| 136 | if (uidField > 0 && (int)event->getValues().size() >= uidField && |
| 137 | (event->getValues())[uidField - 1].mValue.getType() == INT) { |
| 138 | Value& value = (*event->getMutableValues())[uidField - 1].mValue; |
| 139 | const int hostUid = mUidMap->getHostUidOrSelf(value.int_value); |
| 140 | updateUid(&value, hostUid); |
| 141 | } else { |
| 142 | ALOGE("Malformed log, uid not found. %s", event->ToString().c_str()); |
| 143 | } |
| 144 | } |
Yao Chen | 312e898 | 2017-12-05 15:29:03 -0800 | [diff] [blame] | 145 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) { |
| 149 | status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR; |
| 150 | bool is_create = event.GetBool(3, &err); |
| 151 | auto parent_uid = int(event.GetLong(1, &err2)); |
| 152 | auto isolated_uid = int(event.GetLong(2, &err3)); |
| 153 | if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) { |
| 154 | if (is_create) { |
| 155 | mUidMap->assignIsolatedUid(isolated_uid, parent_uid); |
| 156 | } else { |
Bookatz | 3c64886 | 2018-05-25 13:32:43 -0700 | [diff] [blame] | 157 | mUidMap->removeIsolatedUid(isolated_uid); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 158 | } |
| 159 | } else { |
| 160 | ALOGE("Failed to parse uid in the isolated uid change event."); |
| 161 | } |
| 162 | } |
| 163 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 164 | void StatsLogProcessor::resetConfigs() { |
| 165 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 166 | resetConfigsLocked(getElapsedRealtimeNs()); |
| 167 | } |
| 168 | |
| 169 | void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) { |
| 170 | std::vector<ConfigKey> configKeys; |
| 171 | for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) { |
| 172 | configKeys.push_back(it->first); |
| 173 | } |
| 174 | resetConfigsLocked(timestampNs, configKeys); |
| 175 | } |
| 176 | |
Yao Chen | 3ff3a49 | 2018-08-06 16:17:37 -0700 | [diff] [blame] | 177 | void StatsLogProcessor::OnLogEvent(LogEvent* event) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 178 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 179 | |
| 180 | #ifdef VERY_VERBOSE_PRINTING |
| 181 | if (mPrintAllLogs) { |
| 182 | ALOGI("%s", event->ToString().c_str()); |
| 183 | } |
| 184 | #endif |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 185 | const int64_t currentTimestampNs = event->GetElapsedTimestampNs(); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 186 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 187 | resetIfConfigTtlExpiredLocked(currentTimestampNs); |
| 188 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 189 | StatsdStats::getInstance().noteAtomLogged( |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 190 | event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 191 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 192 | // 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] | 193 | // 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] | 194 | if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) { |
| 195 | onIsolatedUidChangedEventLocked(*event); |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (mMetricsManagers.empty()) { |
| 199 | return; |
| 200 | } |
| 201 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 202 | int64_t curTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 203 | if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 204 | mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 205 | mLastPullerCacheClearTimeSec = curTimeSec; |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 208 | |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 209 | if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) { |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 210 | // Map the isolated uid to host uid if necessary. |
| 211 | mapIsolatedUidToHostUidIfNecessaryLocked(event); |
| 212 | } |
| 213 | |
| 214 | // pass the event to metrics managers. |
| 215 | for (auto& pair : mMetricsManagers) { |
| 216 | pair.second->onLogEvent(*event); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 217 | flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second)); |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 218 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 221 | void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key, |
| 222 | const StatsdConfig& config) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 223 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 224 | WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 225 | OnConfigUpdatedLocked(timestampNs, key, config); |
| 226 | } |
| 227 | |
| 228 | void StatsLogProcessor::OnConfigUpdatedLocked( |
| 229 | const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 230 | VLOG("Updated configuration for key %s", key.ToString().c_str()); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 231 | sp<MetricsManager> newMetricsManager = |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 232 | new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager, |
| 233 | mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 234 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 235 | mUidMap->OnConfigUpdated(key); |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 236 | if (newMetricsManager->shouldAddUidMapListener()) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 237 | // We have to add listener after the MetricsManager is constructed because it's |
| 238 | // not safe to create wp or sp from this pointer inside its constructor. |
| 239 | mUidMap->addListener(newMetricsManager.get()); |
| 240 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 241 | newMetricsManager->refreshTtl(timestampNs); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 242 | mMetricsManagers[key] = newMetricsManager; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 243 | VLOG("StatsdConfig valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 244 | } else { |
| 245 | // If there is any error in the config, don't use it. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 246 | ALOGE("StatsdConfig NOT valid"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 247 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 248 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 249 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 250 | size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 251 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 252 | auto it = mMetricsManagers.find(key); |
| 253 | if (it == mMetricsManagers.end()) { |
| 254 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 255 | return 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 256 | } |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 257 | return it->second->byteSize(); |
| 258 | } |
| 259 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 260 | void StatsLogProcessor::dumpStates(int out, bool verbose) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 261 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 262 | FILE* fout = fdopen(out, "w"); |
| 263 | if (fout == NULL) { |
| 264 | return; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 265 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 266 | fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size()); |
| 267 | for (auto metricsManager : mMetricsManagers) { |
| 268 | metricsManager.second->dumpStates(fout, verbose); |
| 269 | } |
| 270 | |
| 271 | fclose(fout); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 272 | } |
| 273 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 274 | /* |
Bookatz | 9cc7b66 | 2018-11-06 10:39:21 -0800 | [diff] [blame] | 275 | * onDumpReport dumps serialized ConfigMetricsReportList into proto. |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 276 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 277 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 278 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 279 | const bool erase_data, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 280 | const DumpReportReason dumpReportReason, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 281 | ProtoOutputStream* proto) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 282 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 283 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 284 | // Start of ConfigKey. |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 285 | uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); |
| 286 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); |
| 287 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId()); |
| 288 | proto->end(configKeyToken); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 289 | // End of ConfigKey. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 290 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 291 | // Then, check stats-data directory to see there's any file containing |
| 292 | // ConfigMetricsReport from previous shutdowns to concatenate to reports. |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 293 | StorageManager::appendConfigMetricsReport(key, proto); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 294 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 295 | auto it = mMetricsManagers.find(key); |
| 296 | if (it != mMetricsManagers.end()) { |
| 297 | // This allows another broadcast to be sent within the rate-limit period if we get close to |
| 298 | // filling the buffer again soon. |
| 299 | mLastBroadcastTimes.erase(key); |
| 300 | |
| 301 | // Start of ConfigMetricsReport (reports). |
| 302 | uint64_t reportsToken = |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 303 | proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 304 | onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 305 | erase_data, dumpReportReason, proto); |
| 306 | proto->end(reportsToken); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 307 | // End of ConfigMetricsReport (reports). |
| 308 | } else { |
| 309 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
| 310 | } |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* |
| 314 | * onDumpReport dumps serialized ConfigMetricsReportList into outData. |
| 315 | */ |
| 316 | void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, |
| 317 | const bool include_current_partial_bucket, |
| 318 | const bool erase_data, |
| 319 | const DumpReportReason dumpReportReason, |
| 320 | vector<uint8_t>* outData) { |
| 321 | ProtoOutputStream proto; |
| 322 | onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data, |
| 323 | dumpReportReason, &proto); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 324 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 325 | if (outData != nullptr) { |
| 326 | outData->clear(); |
| 327 | outData->resize(proto.size()); |
| 328 | size_t pos = 0; |
| 329 | auto iter = proto.data(); |
| 330 | while (iter.readBuffer() != NULL) { |
| 331 | size_t toRead = iter.currentToRead(); |
| 332 | std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead); |
| 333 | pos += toRead; |
| 334 | iter.rp()->move(toRead); |
| 335 | } |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 336 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 337 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 338 | StatsdStats::getInstance().noteMetricsReportSent(key, proto.size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 339 | } |
| 340 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 341 | /* |
| 342 | * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData. |
| 343 | */ |
| 344 | void StatsLogProcessor::onConfigMetricsReportLocked(const ConfigKey& key, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 345 | const int64_t dumpTimeStampNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 346 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 347 | const bool erase_data, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 348 | const DumpReportReason dumpReportReason, |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 349 | ProtoOutputStream* proto) { |
| 350 | // We already checked whether key exists in mMetricsManagers in |
| 351 | // WriteDataToDisk. |
| 352 | auto it = mMetricsManagers.find(key); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 353 | if (it == mMetricsManagers.end()) { |
| 354 | return; |
| 355 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 356 | int64_t lastReportTimeNs = it->second->getLastReportTimeNs(); |
| 357 | int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs(); |
| 358 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 359 | std::set<string> str_set; |
| 360 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 361 | // First, fill in ConfigMetricsReport using current data on memory, which |
| 362 | // starts from filling in StatsLogReport's. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 363 | it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 364 | erase_data, &str_set, proto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 365 | |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 366 | // Fill in UidMap if there is at least one metric to report. |
| 367 | // This skips the uid map if it's an empty config. |
| 368 | if (it->second->getNumMetrics() > 0) { |
| 369 | uint64_t uidMapToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP); |
Yangster-mac | 1c58f04 | 2018-05-17 15:52:51 -0700 | [diff] [blame] | 370 | if (it->second->hashStringInReport()) { |
| 371 | mUidMap->appendUidMap(dumpTimeStampNs, key, &str_set, proto); |
| 372 | } else { |
| 373 | mUidMap->appendUidMap(dumpTimeStampNs, key, nullptr, proto); |
| 374 | } |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 375 | proto->end(uidMapToken); |
| 376 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 377 | |
| 378 | // Fill in the timestamps. |
| 379 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS, |
| 380 | (long long)lastReportTimeNs); |
| 381 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS, |
| 382 | (long long)dumpTimeStampNs); |
| 383 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS, |
| 384 | (long long)lastReportWallClockNs); |
| 385 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS, |
| 386 | (long long)getWallClockNs()); |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 387 | // Dump report reason |
| 388 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 389 | |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 390 | for (const auto& str : str_set) { |
| 391 | proto->write(FIELD_TYPE_STRING | FIELD_COUNT_REPEATED | FIELD_ID_STRINGS, str); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 392 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 393 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 394 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 395 | void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs, |
| 396 | const std::vector<ConfigKey>& configs) { |
| 397 | for (const auto& key : configs) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 398 | StatsdConfig config; |
| 399 | if (StorageManager::readConfigFromDisk(key, &config)) { |
| 400 | OnConfigUpdatedLocked(timestampNs, key, config); |
| 401 | StatsdStats::getInstance().noteConfigReset(key); |
| 402 | } else { |
| 403 | ALOGE("Failed to read backup config from disk for : %s", key.ToString().c_str()); |
| 404 | auto it = mMetricsManagers.find(key); |
| 405 | if (it != mMetricsManagers.end()) { |
| 406 | it->second->refreshTtl(timestampNs); |
| 407 | } |
| 408 | } |
| 409 | } |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 412 | void StatsLogProcessor::resetIfConfigTtlExpiredLocked(const int64_t timestampNs) { |
| 413 | std::vector<ConfigKey> configKeysTtlExpired; |
| 414 | for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) { |
| 415 | if (it->second != nullptr && !it->second->isInTtl(timestampNs)) { |
| 416 | configKeysTtlExpired.push_back(it->first); |
| 417 | } |
| 418 | } |
| 419 | if (configKeysTtlExpired.size() > 0) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 420 | WriteDataToDiskLocked(CONFIG_RESET); |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 421 | resetConfigsLocked(timestampNs, configKeysTtlExpired); |
| 422 | } |
| 423 | } |
| 424 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 425 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 426 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 427 | auto it = mMetricsManagers.find(key); |
| 428 | if (it != mMetricsManagers.end()) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 429 | WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 430 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 431 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 432 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 433 | StatsdStats::getInstance().noteConfigRemoved(key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 434 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 435 | mLastBroadcastTimes.erase(key); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 436 | |
| 437 | if (mMetricsManagers.empty()) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 438 | mPullerManager->ForceClearPullerCache(); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 439 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 442 | void StatsLogProcessor::flushIfNecessaryLocked( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 443 | int64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 444 | auto lastCheckTime = mLastByteSizeTimes.find(key); |
| 445 | if (lastCheckTime != mLastByteSizeTimes.end()) { |
| 446 | if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) { |
| 447 | return; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // We suspect that the byteSize() computation is expensive, so we set a rate limit. |
| 452 | size_t totalBytes = metricsManager.byteSize(); |
| 453 | mLastByteSizeTimes[key] = timestampNs; |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 454 | bool requestDump = false; |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 455 | if (totalBytes > |
| 456 | StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data. |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 457 | metricsManager.dropData(timestampNs); |
Chenjie Yu | c3c30c0 | 2018-10-26 09:48:07 -0700 | [diff] [blame] | 458 | StatsdStats::getInstance().noteDataDropped(key, totalBytes); |
David Chen | 1294295 | 2017-12-04 14:28:43 -0800 | [diff] [blame] | 459 | 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] | 460 | } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) || |
| 461 | (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) { |
| 462 | // Request to send a broadcast if: |
| 463 | // 1. in memory data > threshold OR |
| 464 | // 2. config has old data report on disk. |
| 465 | requestDump = true; |
| 466 | } |
| 467 | |
| 468 | if (requestDump) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 469 | // Send broadcast so that receivers can pull data. |
| 470 | auto lastBroadcastTime = mLastBroadcastTimes.find(key); |
| 471 | if (lastBroadcastTime != mLastBroadcastTimes.end()) { |
| 472 | if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) { |
| 473 | 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] | 474 | return; |
| 475 | } |
| 476 | } |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 477 | if (mSendBroadcast(key)) { |
| 478 | mOnDiskDataConfigs.erase(key); |
| 479 | VLOG("StatsD triggered data fetch for %s", key.ToString().c_str()); |
| 480 | mLastBroadcastTimes[key] = timestampNs; |
| 481 | StatsdStats::getInstance().noteBroadcastSent(key); |
| 482 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 486 | void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key, |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 487 | const int64_t timestampNs, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 488 | const DumpReportReason dumpReportReason) { |
yro | 028091c | 2018-05-09 16:03:27 -0700 | [diff] [blame] | 489 | if (mMetricsManagers.find(key) == mMetricsManagers.end() || |
| 490 | !mMetricsManagers.find(key)->second->shouldWriteToDisk()) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 491 | return; |
| 492 | } |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 493 | ProtoOutputStream proto; |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 494 | onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 495 | true /* erase_data */, dumpReportReason, &proto); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 496 | string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR, |
| 497 | (long)getWallClockSec(), key.GetUid(), (long long)key.GetId()); |
| 498 | android::base::unique_fd fd(open(file_name.c_str(), |
| 499 | O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)); |
| 500 | if (fd == -1) { |
| 501 | ALOGE("Attempt to write %s but failed", file_name.c_str()); |
| 502 | return; |
| 503 | } |
| 504 | proto.flush(fd.get()); |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 505 | // We were able to write the ConfigMetricsReport to disk, so we should trigger collection ASAP. |
| 506 | mOnDiskDataConfigs.insert(key); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 509 | void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 510 | const int64_t timeNs = getElapsedRealtimeNs(); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 511 | for (auto& pair : mMetricsManagers) { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 512 | WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 516 | void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason) { |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 517 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 518 | WriteDataToDiskLocked(dumpReportReason); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 521 | void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) { |
Yangster | 6df5fcc | 2018-04-12 11:04:29 -0700 | [diff] [blame] | 522 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 523 | mPullerManager->OnAlarmFired(timestampNs); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 524 | } |
| 525 | |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 526 | int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) { |
| 527 | auto it = mMetricsManagers.find(key); |
| 528 | if (it == mMetricsManagers.end()) { |
| 529 | return 0; |
| 530 | } else { |
| 531 | return it->second->getLastReportTimeNs(); |
| 532 | } |
| 533 | } |
| 534 | |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 535 | void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) { |
| 536 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 537 | mOnDiskDataConfigs.insert(key); |
| 538 | } |
| 539 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 540 | } // namespace statsd |
| 541 | } // namespace os |
| 542 | } // namespace android |