blob: a4c9d1845461d1ab42cef69f6a00da10dd6c86ee [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),
Yangster-mac20877162017-12-22 17:19:39 -080077 mTimeBaseSec(timeBaseSec) {
Chenjie Yu85ed8382017-12-14 16:48:54 -080078 StatsPullerManager statsPullerManager;
79 statsPullerManager.SetTimeBaseSec(mTimeBaseSec);
Yao Chenab273e22017-09-06 12:53:50 -070080}
81
Yao Chenef99c4f2017-09-22 16:26:54 -070082StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070083}
84
Yangster-mace2cd6d52017-11-09 20:38:30 -080085void StatsLogProcessor::onAnomalyAlarmFired(
86 const uint64_t timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080087 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
Yangster-macb0d06282018-01-05 15:44:07 -080088 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef22017-11-21 14:36:23 -080089 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -080090 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
91 }
92}
93void StatsLogProcessor::onPeriodicAlarmFired(
94 const uint64_t timestampNs,
95 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
96
97 std::lock_guard<std::mutex> lock(mMetricsMutex);
98 for (const auto& itr : mMetricsManagers) {
99 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800100 }
101}
102
Yao Chen8a8d16c2018-02-08 14:50:40 -0800103void updateUid(Value* value, int hostUid) {
104 int uid = value->int_value;
105 if (uid != hostUid) {
106 value->setInt(hostUid);
107 }
108}
109
Yangster-macd40053e2018-01-09 16:29:22 -0800110void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Yangster-mac68985802018-01-21 10:05:09 -0800111 if (android::util::kAtomsWithAttributionChain.find(event->GetTagId()) !=
112 android::util::kAtomsWithAttributionChain.end()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800113 for (auto& value : *(event->getMutableValues())) {
114 if (value.mField.getPosAtDepth(0) > kAttributionField) {
115 break;
116 }
117 if (isAttributionUidField(value)) {
118 const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value);
119 updateUid(&value.mValue, hostUid);
120 }
Yangster-macd40053e2018-01-09 16:29:22 -0800121 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800122 } else if (android::util::kAtomsWithUidField.find(event->GetTagId()) !=
123 android::util::kAtomsWithUidField.end() &&
124 event->getValues().size() > 0 && (event->getValues())[0].mValue.getType() == INT) {
125 Value& value = (*event->getMutableValues())[0].mValue;
126 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
127 updateUid(&value, hostUid);
Yao Chen312e8982017-12-05 15:29:03 -0800128 }
Yangster-macd40053e2018-01-09 16:29:22 -0800129}
130
131void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
132 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
133 bool is_create = event.GetBool(3, &err);
134 auto parent_uid = int(event.GetLong(1, &err2));
135 auto isolated_uid = int(event.GetLong(2, &err3));
136 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
137 if (is_create) {
138 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
139 } else {
140 mUidMap->removeIsolatedUid(isolated_uid, parent_uid);
141 }
142 } else {
143 ALOGE("Failed to parse uid in the isolated uid change event.");
144 }
145}
146
147// TODO: what if statsd service restarts? How do we know what logs are already processed before?
148void StatsLogProcessor::OnLogEvent(LogEvent* event) {
149 std::lock_guard<std::mutex> lock(mMetricsMutex);
150 StatsdStats::getInstance().noteAtomLogged(
Yangster-mac330af582018-02-08 15:24:38 -0800151 event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC);
Yangster-macd40053e2018-01-09 16:29:22 -0800152
David Chen21582962017-11-01 17:32:46 -0700153 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800154 // The field numbers need to be currently updated by hand with atoms.proto
Yangster-macd40053e2018-01-09 16:29:22 -0800155 if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) {
156 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800157 }
158
159 if (mMetricsManagers.empty()) {
160 return;
161 }
162
Yangster-mac330af582018-02-08 15:24:38 -0800163 uint64_t curTimeSec = getElapsedRealtimeSec();
164 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
165 mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec);
166 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800167 }
168
David Chencfc311d2018-01-23 17:55:54 -0800169 if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800170 // Map the isolated uid to host uid if necessary.
171 mapIsolatedUidToHostUidIfNecessaryLocked(event);
172 }
173
174 // pass the event to metrics managers.
175 for (auto& pair : mMetricsManagers) {
176 pair.second->onLogEvent(*event);
Yangster-mac330af582018-02-08 15:24:38 -0800177 flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700178 }
Yao Chenab273e22017-09-06 12:53:50 -0700179}
180
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700181void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800182 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singh484524a2018-02-01 15:10:05 -0800183 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800184 sp<MetricsManager> newMetricsManager =
185 new MetricsManager(key, config, mTimeBaseSec, mUidMap,
186 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700187 auto it = mMetricsManagers.find(key);
Yao Chen288c6002017-12-12 13:43:18 -0800188 if (it == mMetricsManagers.end() && mMetricsManagers.size() > StatsdStats::kMaxConfigCount) {
Yao Chenb3561512017-11-21 18:07:17 -0800189 ALOGE("Can't accept more configs!");
190 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700191 }
192
Yao Chencaf339d2017-10-06 16:01:10 -0700193 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -0700194 mUidMap->OnConfigUpdated(key);
Yao Chen147ce602017-12-22 14:35:34 -0800195 if (newMetricsManager->shouldAddUidMapListener()) {
Yao Chend10f7b12017-12-18 12:53:50 -0800196 // We have to add listener after the MetricsManager is constructed because it's
197 // not safe to create wp or sp from this pointer inside its constructor.
198 mUidMap->addListener(newMetricsManager.get());
199 }
200 mMetricsManagers[key] = newMetricsManager;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700201 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chenb3561512017-11-21 18:07:17 -0800202 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700203 } else {
204 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800205 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700206 }
yro00698da2017-09-15 10:06:40 -0700207}
Bookatz906a35c2017-09-20 15:26:44 -0700208
Yangster7c334a12017-11-22 14:24:24 -0800209size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800210 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700211 auto it = mMetricsManagers.find(key);
212 if (it == mMetricsManagers.end()) {
213 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800214 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700215 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800216 return it->second->byteSize();
217}
218
Yao Chen884c8c12018-01-26 10:36:25 -0800219void StatsLogProcessor::dumpStates(FILE* out, bool verbose) {
220 std::lock_guard<std::mutex> lock(mMetricsMutex);
221 fprintf(out, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
222 for (auto metricsManager : mMetricsManagers) {
223 metricsManager.second->dumpStates(out, verbose);
224 }
225}
226
Yao Chen8a8d16c2018-02-08 14:50:40 -0800227void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t dumpTimeStampNs,
228 vector<uint8_t>* outData) {
Yangster-macb0d06282018-01-05 15:44:07 -0800229 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800230 onDumpReportLocked(key, dumpTimeStampNs, outData);
Yangster-mac20877162017-12-22 17:19:39 -0800231}
232
Yao Chen8a8d16c2018-02-08 14:50:40 -0800233void StatsLogProcessor::onDumpReportLocked(const ConfigKey& key, const uint64_t dumpTimeStampNs,
234 vector<uint8_t>* outData) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800235 auto it = mMetricsManagers.find(key);
236 if (it == mMetricsManagers.end()) {
237 ALOGW("Config source %s does not exist", key.ToString().c_str());
238 return;
239 }
240
241 // This allows another broadcast to be sent within the rate-limit period if we get close to
242 // filling the buffer again soon.
David Chen1d7b0cd2017-11-15 14:20:04 -0800243 mLastBroadcastTimes.erase(key);
Yao Chen729093d2017-10-16 10:33:26 -0700244
yro17adac92017-11-08 23:16:29 -0800245 ProtoOutputStream proto;
246
yro947fbce2017-11-15 22:50:23 -0800247 // Start of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800248 long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
249 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800250 proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
yro17adac92017-11-08 23:16:29 -0800251 proto.end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800252 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800253
yro947fbce2017-11-15 22:50:23 -0800254 // Start of ConfigMetricsReport (reports).
255 long long reportsToken =
256 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);
257
Yangster-mac330af582018-02-08 15:24:38 -0800258 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
yro947fbce2017-11-15 22:50:23 -0800259 // First, fill in ConfigMetricsReport using current data on memory, which
260 // starts from filling in StatsLogReport's.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800261 it->second->onDumpReport(dumpTimeStampNs, &proto);
yro17adac92017-11-08 23:16:29 -0800262
263 // Fill in UidMap.
264 auto uidMap = mUidMap->getOutput(key);
265 const int uidMapSize = uidMap.ByteSize();
266 char uidMapBuffer[uidMapSize];
267 uidMap.SerializeToArray(&uidMapBuffer[0], uidMapSize);
268 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize);
269
David Chen16049572018-02-01 18:27:51 -0800270 // Fill in the timestamps.
Yangster-mac330af582018-02-08 15:24:38 -0800271 proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
272 (long long)lastReportTimeNs);
273 proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
274 (long long)dumpTimeStampNs);
David Chen16049572018-02-01 18:27:51 -0800275
yro947fbce2017-11-15 22:50:23 -0800276 // End of ConfigMetricsReport (reports).
277 proto.end(reportsToken);
278
279 // Then, check stats-data directory to see there's any file containing
280 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
yro98a28502018-01-18 17:00:14 -0800281 StorageManager::appendConfigMetricsReport(proto);
yro947fbce2017-11-15 22:50:23 -0800282
David Chen1d7b0cd2017-11-15 14:20:04 -0800283 if (outData != nullptr) {
284 outData->clear();
285 outData->resize(proto.size());
286 size_t pos = 0;
287 auto iter = proto.data();
288 while (iter.readBuffer() != NULL) {
289 size_t toRead = iter.currentToRead();
290 std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead);
291 pos += toRead;
292 iter.rp()->move(toRead);
293 }
yro17adac92017-11-08 23:16:29 -0800294 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800295
Yao Chen69f1baf2017-11-27 17:25:36 -0800296 StatsdStats::getInstance().noteMetricsReportSent(key);
Yao Chen729093d2017-10-16 10:33:26 -0700297}
298
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700299void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800300 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700301 auto it = mMetricsManagers.find(key);
302 if (it != mMetricsManagers.end()) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700303 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700304 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700305 }
Yao Chenb3561512017-11-21 18:07:17 -0800306 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800307
David Chen1d7b0cd2017-11-15 14:20:04 -0800308 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800309
310 if (mMetricsManagers.empty()) {
311 mStatsPullerManager.ForceClearPullerCache();
312 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700313}
314
Yangster-macb0d06282018-01-05 15:44:07 -0800315void StatsLogProcessor::flushIfNecessaryLocked(
316 uint64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) {
David Chend9269e22017-12-05 13:43:51 -0800317 auto lastCheckTime = mLastByteSizeTimes.find(key);
318 if (lastCheckTime != mLastByteSizeTimes.end()) {
319 if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
320 return;
321 }
322 }
323
324 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
325 size_t totalBytes = metricsManager.byteSize();
326 mLastByteSizeTimes[key] = timestampNs;
327 if (totalBytes >
328 StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data.
Yao Chen288c6002017-12-12 13:43:18 -0800329 // TODO(b/70571383): By 12/15/2017 add API to drop data directly
330 ProtoOutputStream proto;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800331 metricsManager.onDumpReport(time(nullptr) * NS_PER_SEC, &proto);
David Chen12942952017-12-04 14:28:43 -0800332 StatsdStats::getInstance().noteDataDropped(key);
333 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chend9269e22017-12-05 13:43:51 -0800334 } else if (totalBytes > .9 * StatsdStats::kMaxMetricsBytesPerConfig) {
335 // Send broadcast so that receivers can pull data.
336 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
337 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
338 if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) {
339 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800340 return;
341 }
342 }
343 mLastBroadcastTimes[key] = timestampNs;
Yao Chenb3561512017-11-21 18:07:17 -0800344 VLOG("StatsD requesting broadcast for %s", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800345 mSendBroadcast(key);
Yao Chenb3561512017-11-21 18:07:17 -0800346 StatsdStats::getInstance().noteBroadcastSent(key);
yro31eb67b2017-10-24 13:33:21 -0700347 }
348}
349
yro947fbce2017-11-15 22:50:23 -0800350void StatsLogProcessor::WriteDataToDisk() {
Yangster-macb0d06282018-01-05 15:44:07 -0800351 std::lock_guard<std::mutex> lock(mMetricsMutex);
yro947fbce2017-11-15 22:50:23 -0800352 for (auto& pair : mMetricsManagers) {
353 const ConfigKey& key = pair.first;
354 vector<uint8_t> data;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800355 onDumpReportLocked(key, time(nullptr) * NS_PER_SEC, &data);
yro947fbce2017-11-15 22:50:23 -0800356 // TODO: Add a guardrail to prevent accumulation of file on disk.
Yangster-mac330af582018-02-08 15:24:38 -0800357 string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
358 (long)getWallClockSec(), key.GetUid(), (long long)key.GetId());
yro947fbce2017-11-15 22:50:23 -0800359 StorageManager::writeFile(file_name.c_str(), &data[0], data.size());
360 }
361}
362
Yao Chenef99c4f2017-09-22 16:26:54 -0700363} // namespace statsd
364} // namespace os
365} // namespace android