blob: 9b58a14de0be0ba37d846ae0119d7ed62ab6d48d [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53:50 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yao Chen8a8d16c2018-02-08 14:50:40 -080017#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
David Chen21582962017-11-01 17:32:46 -070019#include "statslog.h"
Yao Chenab273e22017-09-06 12:53:50 -070020
yro947fbce2017-11-15 22:50:23 -080021#include <android-base/file.h>
22#include <dirent.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "StatsLogProcessor.h"
Yangster-mac330af582018-02-08 15:24:38 -080024#include "stats_log_util.h"
yro947fbce2017-11-15 22:50:23 -080025#include "android-base/stringprintf.h"
Yao Chenb3561512017-11-21 18:07:17 -080026#include "guardrail/StatsdStats.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070027#include "metrics/CountMetricProducer.h"
Chenjie Yu85ed8382017-12-14 16:48:54 -080028#include "external/StatsPullerManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070029#include "stats_util.h"
yro947fbce2017-11-15 22:50:23 -080030#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031
yro00698da2017-09-15 10:06:40 -070032#include <log/log_event_list.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070033#include <utils/Errors.h>
David Chen16049572018-02-01 18:27:51 -080034#include <utils/SystemClock.h>
Yao Chenab273e22017-09-06 12:53:50 -070035
36using namespace android;
yro947fbce2017-11-15 22:50:23 -080037using android::base::StringPrintf;
yrob0378b02017-11-09 20:36:25 -080038using android::util::FIELD_COUNT_REPEATED;
yro17adac92017-11-08 23:16:29 -080039using android::util::FIELD_TYPE_BOOL;
40using android::util::FIELD_TYPE_FLOAT;
41using android::util::FIELD_TYPE_INT32;
42using android::util::FIELD_TYPE_INT64;
43using android::util::FIELD_TYPE_MESSAGE;
44using android::util::FIELD_TYPE_STRING;
45using android::util::ProtoOutputStream;
Yao Chen44cf27c2017-09-14 22:32:50 -070046using std::make_unique;
47using std::unique_ptr;
48using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070049
50namespace android {
51namespace os {
52namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070053
yro947fbce2017-11-15 22:50:23 -080054// for ConfigMetricsReportList
yro17adac92017-11-08 23:16:29 -080055const int FIELD_ID_CONFIG_KEY = 1;
yro947fbce2017-11-15 22:50:23 -080056const int FIELD_ID_REPORTS = 2;
yro17adac92017-11-08 23:16:29 -080057// for ConfigKey
58const int FIELD_ID_UID = 1;
Yangster-mac94e197c2018-01-02 16:03:03 -080059const int FIELD_ID_ID = 2;
yro947fbce2017-11-15 22:50:23 -080060// for ConfigMetricsReport
Yao Chen4c959cb2018-02-13 13:27:48 -080061// const int FIELD_ID_METRICS = 1; // written in MetricsManager.cpp
yro947fbce2017-11-15 22:50:23 -080062const int FIELD_ID_UID_MAP = 2;
Yangster-mac330af582018-02-08 15:24:38 -080063const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
64const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;
yro947fbce2017-11-15 22:50:23 -080065
yro03faf092017-12-12 00:17:50 -080066#define STATS_DATA_DIR "/data/misc/stats-data"
yro17adac92017-11-08 23:16:29 -080067
yro31eb67b2017-10-24 13:33:21 -070068StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
Yangster-mac932ecec2018-02-01 10:23:52 -080069 const sp<AlarmMonitor>& anomalyAlarmMonitor,
70 const sp<AlarmMonitor>& periodicAlarmMonitor,
Yangster-mac20877162017-12-22 17:19:39 -080071 const long timeBaseSec,
David Chen1d7b0cd2017-11-15 14:20:04 -080072 const std::function<void(const ConfigKey&)>& sendBroadcast)
Chenjie Yu85ed8382017-12-14 16:48:54 -080073 : mUidMap(uidMap),
Yangster-mac932ecec2018-02-01 10:23:52 -080074 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
75 mPeriodicAlarmMonitor(periodicAlarmMonitor),
Chenjie Yu85ed8382017-12-14 16:48:54 -080076 mSendBroadcast(sendBroadcast),
Yao Chen8f42ba02018-02-27 15:17:07 -080077 mTimeBaseSec(timeBaseSec),
78 mLastLogTimestamp(0) {
Chenjie Yu85ed8382017-12-14 16:48:54 -080079 StatsPullerManager statsPullerManager;
80 statsPullerManager.SetTimeBaseSec(mTimeBaseSec);
Yao Chenab273e22017-09-06 12:53:50 -070081}
82
Yao Chenef99c4f2017-09-22 16:26:54 -070083StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070084}
85
Yangster-mace2cd6d52017-11-09 20:38:30 -080086void StatsLogProcessor::onAnomalyAlarmFired(
87 const uint64_t timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080088 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
Yangster-macb0d06282018-01-05 15:44:07 -080089 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef22017-11-21 14:36:23 -080090 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -080091 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
92 }
93}
94void StatsLogProcessor::onPeriodicAlarmFired(
95 const uint64_t timestampNs,
96 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
97
98 std::lock_guard<std::mutex> lock(mMetricsMutex);
99 for (const auto& itr : mMetricsManagers) {
100 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101 }
102}
103
Yao Chen8a8d16c2018-02-08 14:50:40 -0800104void updateUid(Value* value, int hostUid) {
105 int uid = value->int_value;
106 if (uid != hostUid) {
107 value->setInt(hostUid);
108 }
109}
110
Yangster-macd40053e2018-01-09 16:29:22 -0800111void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Yangster-mac68985802018-01-21 10:05:09 -0800112 if (android::util::kAtomsWithAttributionChain.find(event->GetTagId()) !=
113 android::util::kAtomsWithAttributionChain.end()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800114 for (auto& value : *(event->getMutableValues())) {
115 if (value.mField.getPosAtDepth(0) > kAttributionField) {
116 break;
117 }
118 if (isAttributionUidField(value)) {
119 const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value);
120 updateUid(&value.mValue, hostUid);
121 }
Yangster-macd40053e2018-01-09 16:29:22 -0800122 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800123 } else if (android::util::kAtomsWithUidField.find(event->GetTagId()) !=
124 android::util::kAtomsWithUidField.end() &&
125 event->getValues().size() > 0 && (event->getValues())[0].mValue.getType() == INT) {
126 Value& value = (*event->getMutableValues())[0].mValue;
127 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
128 updateUid(&value, hostUid);
Yao Chen312e8982017-12-05 15:29:03 -0800129 }
Yangster-macd40053e2018-01-09 16:29:22 -0800130}
131
132void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
133 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
134 bool is_create = event.GetBool(3, &err);
135 auto parent_uid = int(event.GetLong(1, &err2));
136 auto isolated_uid = int(event.GetLong(2, &err3));
137 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
138 if (is_create) {
139 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
140 } else {
141 mUidMap->removeIsolatedUid(isolated_uid, parent_uid);
142 }
143 } else {
144 ALOGE("Failed to parse uid in the isolated uid change event.");
145 }
146}
147
Yangster-macd40053e2018-01-09 16:29:22 -0800148void StatsLogProcessor::OnLogEvent(LogEvent* event) {
149 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen8f42ba02018-02-27 15:17:07 -0800150 if (event->GetElapsedTimestampNs() < mLastLogTimestamp) {
151 return;
152 }
153 mLastLogTimestamp = event->GetElapsedTimestampNs();
Yangster-macd40053e2018-01-09 16:29:22 -0800154 StatsdStats::getInstance().noteAtomLogged(
Yangster-mac330af582018-02-08 15:24:38 -0800155 event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC);
Yangster-macd40053e2018-01-09 16:29:22 -0800156
David Chen21582962017-11-01 17:32:46 -0700157 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800158 // The field numbers need to be currently updated by hand with atoms.proto
Yangster-macd40053e2018-01-09 16:29:22 -0800159 if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) {
160 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800161 }
162
163 if (mMetricsManagers.empty()) {
164 return;
165 }
166
Yangster-mac330af582018-02-08 15:24:38 -0800167 uint64_t curTimeSec = getElapsedRealtimeSec();
168 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
169 mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec);
170 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800171 }
172
David Chencfc311d2018-01-23 17:55:54 -0800173 if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800174 // Map the isolated uid to host uid if necessary.
175 mapIsolatedUidToHostUidIfNecessaryLocked(event);
176 }
177
178 // pass the event to metrics managers.
179 for (auto& pair : mMetricsManagers) {
180 pair.second->onLogEvent(*event);
Yangster-mac330af582018-02-08 15:24:38 -0800181 flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700182 }
Yao Chenab273e22017-09-06 12:53:50 -0700183}
184
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700185void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800186 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singh484524a2018-02-01 15:10:05 -0800187 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800188 sp<MetricsManager> newMetricsManager =
189 new MetricsManager(key, config, mTimeBaseSec, mUidMap,
190 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700191 auto it = mMetricsManagers.find(key);
Yao Chen288c6002017-12-12 13:43:18 -0800192 if (it == mMetricsManagers.end() && mMetricsManagers.size() > StatsdStats::kMaxConfigCount) {
Yao Chenb3561512017-11-21 18:07:17 -0800193 ALOGE("Can't accept more configs!");
194 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700195 }
196
Yao Chencaf339d2017-10-06 16:01:10 -0700197 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -0700198 mUidMap->OnConfigUpdated(key);
Yao Chen147ce602017-12-22 14:35:34 -0800199 if (newMetricsManager->shouldAddUidMapListener()) {
Yao Chend10f7b12017-12-18 12:53:50 -0800200 // We have to add listener after the MetricsManager is constructed because it's
201 // not safe to create wp or sp from this pointer inside its constructor.
202 mUidMap->addListener(newMetricsManager.get());
203 }
204 mMetricsManagers[key] = newMetricsManager;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700205 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chenb3561512017-11-21 18:07:17 -0800206 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700207 } else {
208 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800209 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700210 }
yro00698da2017-09-15 10:06:40 -0700211}
Bookatz906a35c2017-09-20 15:26:44 -0700212
Yangster7c334a12017-11-22 14:24:24 -0800213size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800214 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700215 auto it = mMetricsManagers.find(key);
216 if (it == mMetricsManagers.end()) {
217 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800218 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700219 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800220 return it->second->byteSize();
221}
222
Yao Chen884c8c12018-01-26 10:36:25 -0800223void StatsLogProcessor::dumpStates(FILE* out, bool verbose) {
224 std::lock_guard<std::mutex> lock(mMetricsMutex);
225 fprintf(out, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
226 for (auto metricsManager : mMetricsManagers) {
227 metricsManager.second->dumpStates(out, verbose);
228 }
229}
230
Yao Chen8a8d16c2018-02-08 14:50:40 -0800231void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t dumpTimeStampNs,
232 vector<uint8_t>* outData) {
Yangster-macb0d06282018-01-05 15:44:07 -0800233 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800234 onDumpReportLocked(key, dumpTimeStampNs, outData);
Yangster-mac20877162017-12-22 17:19:39 -0800235}
236
Yao Chen8a8d16c2018-02-08 14:50:40 -0800237void StatsLogProcessor::onDumpReportLocked(const ConfigKey& key, const uint64_t dumpTimeStampNs,
238 vector<uint8_t>* outData) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800239 auto it = mMetricsManagers.find(key);
240 if (it == mMetricsManagers.end()) {
241 ALOGW("Config source %s does not exist", key.ToString().c_str());
242 return;
243 }
244
245 // This allows another broadcast to be sent within the rate-limit period if we get close to
246 // filling the buffer again soon.
David Chen1d7b0cd2017-11-15 14:20:04 -0800247 mLastBroadcastTimes.erase(key);
Yao Chen729093d2017-10-16 10:33:26 -0700248
yro17adac92017-11-08 23:16:29 -0800249 ProtoOutputStream proto;
250
yro947fbce2017-11-15 22:50:23 -0800251 // Start of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800252 long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
253 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800254 proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
yro17adac92017-11-08 23:16:29 -0800255 proto.end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800256 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800257
yro947fbce2017-11-15 22:50:23 -0800258 // Start of ConfigMetricsReport (reports).
259 long long reportsToken =
260 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);
261
Yangster-mac330af582018-02-08 15:24:38 -0800262 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
yro947fbce2017-11-15 22:50:23 -0800263 // First, fill in ConfigMetricsReport using current data on memory, which
264 // starts from filling in StatsLogReport's.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800265 it->second->onDumpReport(dumpTimeStampNs, &proto);
yro17adac92017-11-08 23:16:29 -0800266
267 // Fill in UidMap.
268 auto uidMap = mUidMap->getOutput(key);
269 const int uidMapSize = uidMap.ByteSize();
270 char uidMapBuffer[uidMapSize];
271 uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize);
272 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize);
273
David Chen16049572018-02-01 18:27:51 -0800274 // Fill in the timestamps.
Yangster-mac330af582018-02-08 15:24:38 -0800275 proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
276 (long long)lastReportTimeNs);
277 proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
278 (long long)dumpTimeStampNs);
David Chen16049572018-02-01 18:27:51 -0800279
yro947fbce2017-11-15 22:50:23 -0800280 // End of ConfigMetricsReport (reports).
281 proto.end(reportsToken);
282
283 // Then, check stats-data directory to see there's any file containing
284 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
yro98a28502018-01-18 17:00:14 -0800285 StorageManager::appendConfigMetricsReport(proto);
yro947fbce2017-11-15 22:50:23 -0800286
David Chen1d7b0cd2017-11-15 14:20:04 -0800287 if (outData != nullptr) {
288 outData->clear();
289 outData->resize(proto.size());
290 size_t pos = 0;
291 auto iter = proto.data();
292 while (iter.readBuffer() != NULL) {
293 size_t toRead = iter.currentToRead();
294 std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead);
295 pos += toRead;
296 iter.rp()->move(toRead);
297 }
yro17adac92017-11-08 23:16:29 -0800298 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800299
Yao Chen69f1baf2017-11-27 17:25:36 -0800300 StatsdStats::getInstance().noteMetricsReportSent(key);
Yao Chen729093d2017-10-16 10:33:26 -0700301}
302
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700303void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800304 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700305 auto it = mMetricsManagers.find(key);
306 if (it != mMetricsManagers.end()) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700307 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700308 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700309 }
Yao Chenb3561512017-11-21 18:07:17 -0800310 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800311
David Chen1d7b0cd2017-11-15 14:20:04 -0800312 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800313
314 if (mMetricsManagers.empty()) {
315 mStatsPullerManager.ForceClearPullerCache();
316 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700317}
318
Yangster-macb0d06282018-01-05 15:44:07 -0800319void StatsLogProcessor::flushIfNecessaryLocked(
320 uint64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) {
David Chend9269e22017-12-05 13:43:51 -0800321 auto lastCheckTime = mLastByteSizeTimes.find(key);
322 if (lastCheckTime != mLastByteSizeTimes.end()) {
323 if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
324 return;
325 }
326 }
327
328 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
329 size_t totalBytes = metricsManager.byteSize();
330 mLastByteSizeTimes[key] = timestampNs;
331 if (totalBytes >
332 StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data.
Yao Chen288c6002017-12-12 13:43:18 -0800333 // TODO(b/70571383): By 12/15/2017 add API to drop data directly
334 ProtoOutputStream proto;
David Chen926fc752018-02-23 13:31:43 -0800335 metricsManager.onDumpReport(timestampNs, &proto);
David Chen12942952017-12-04 14:28:43 -0800336 StatsdStats::getInstance().noteDataDropped(key);
337 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chend9269e22017-12-05 13:43:51 -0800338 } else if (totalBytes > .9 * StatsdStats::kMaxMetricsBytesPerConfig) {
339 // Send broadcast so that receivers can pull data.
340 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
341 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
342 if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) {
343 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800344 return;
345 }
346 }
347 mLastBroadcastTimes[key] = timestampNs;
Yao Chenb3561512017-11-21 18:07:17 -0800348 VLOG("StatsD requesting broadcast for %s", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800349 mSendBroadcast(key);
Yao Chenb3561512017-11-21 18:07:17 -0800350 StatsdStats::getInstance().noteBroadcastSent(key);
yro31eb67b2017-10-24 13:33:21 -0700351 }
352}
353
yro947fbce2017-11-15 22:50:23 -0800354void StatsLogProcessor::WriteDataToDisk() {
Yangster-macb0d06282018-01-05 15:44:07 -0800355 std::lock_guard<std::mutex> lock(mMetricsMutex);
yro947fbce2017-11-15 22:50:23 -0800356 for (auto& pair : mMetricsManagers) {
357 const ConfigKey& key = pair.first;
358 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800359 onDumpReportLocked(key, getElapsedRealtimeNs(), &data);
yro947fbce2017-11-15 22:50:23 -0800360 // TODO: Add a guardrail to prevent accumulation of file on disk.
Yangster-mac330af582018-02-08 15:24:38 -0800361 string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
362 (long)getWallClockSec(), key.GetUid(), (long long)key.GetId());
yro947fbce2017-11-15 22:50:23 -0800363 StorageManager::writeFile(file_name.c_str(), &data[0], data.size());
364 }
365}
366
Yao Chenef99c4f2017-09-22 16:26:54 -0700367} // namespace statsd
368} // namespace os
369} // namespace android