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