blob: 24243afa71dfc92788c69e51e6e70e396114b655 [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 Chenb3561512017-11-21 18:07:17 -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"
yro947fbce2017-11-15 22:50:23 -080024#include "android-base/stringprintf.h"
Yao Chenb3561512017-11-21 18:07:17 -080025#include "guardrail/StatsdStats.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include "metrics/CountMetricProducer.h"
Chenjie Yu85ed8382017-12-14 16:48:54 -080027#include "external/StatsPullerManager.h"
Yangster-macd40053e2018-01-09 16:29:22 -080028#include "dimension.h"
29#include "field_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070030#include "stats_util.h"
yro947fbce2017-11-15 22:50:23 -080031#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070032
yro00698da2017-09-15 10:06:40 -070033#include <log/log_event_list.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070034#include <utils/Errors.h>
David Chen16049572018-02-01 18:27:51 -080035#include <utils/SystemClock.h>
Yao Chenab273e22017-09-06 12:53:50 -070036
37using namespace android;
yro947fbce2017-11-15 22:50:23 -080038using android::base::StringPrintf;
yrob0378b02017-11-09 20:36:25 -080039using android::util::FIELD_COUNT_REPEATED;
yro17adac92017-11-08 23:16:29 -080040using android::util::FIELD_TYPE_BOOL;
41using android::util::FIELD_TYPE_FLOAT;
42using android::util::FIELD_TYPE_INT32;
43using android::util::FIELD_TYPE_INT64;
44using android::util::FIELD_TYPE_MESSAGE;
45using android::util::FIELD_TYPE_STRING;
46using android::util::ProtoOutputStream;
Yao Chen44cf27c2017-09-14 22:32:50 -070047using std::make_unique;
48using std::unique_ptr;
49using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070050
51namespace android {
52namespace os {
53namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070054
yro947fbce2017-11-15 22:50:23 -080055// for ConfigMetricsReportList
yro17adac92017-11-08 23:16:29 -080056const int FIELD_ID_CONFIG_KEY = 1;
yro947fbce2017-11-15 22:50:23 -080057const int FIELD_ID_REPORTS = 2;
yro17adac92017-11-08 23:16:29 -080058// for ConfigKey
59const int FIELD_ID_UID = 1;
Yangster-mac94e197c2018-01-02 16:03:03 -080060const int FIELD_ID_ID = 2;
yro947fbce2017-11-15 22:50:23 -080061// for ConfigMetricsReport
62const int FIELD_ID_METRICS = 1;
63const int FIELD_ID_UID_MAP = 2;
David Chen16049572018-02-01 18:27:51 -080064const int FIELD_ID_LAST_REPORT_NANOS = 3;
65const int FIELD_ID_CURRENT_REPORT_NANOS = 4;
yro947fbce2017-11-15 22:50:23 -080066
yro03faf092017-12-12 00:17:50 -080067#define STATS_DATA_DIR "/data/misc/stats-data"
yro17adac92017-11-08 23:16:29 -080068
yro31eb67b2017-10-24 13:33:21 -070069StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
Yangster-mace2cd6d52017-11-09 20:38:30 -080070 const sp<AnomalyMonitor>& anomalyMonitor,
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),
74 mAnomalyMonitor(anomalyMonitor),
75 mSendBroadcast(sendBroadcast),
Yangster-mac20877162017-12-22 17:19:39 -080076 mTimeBaseSec(timeBaseSec) {
yro947fbce2017-11-15 22:50:23 -080077 // On each initialization of StatsLogProcessor, check stats-data directory to see if there is
78 // any left over data to be read.
79 StorageManager::sendBroadcast(STATS_DATA_DIR, mSendBroadcast);
Chenjie Yu85ed8382017-12-14 16:48:54 -080080 StatsPullerManager statsPullerManager;
81 statsPullerManager.SetTimeBaseSec(mTimeBaseSec);
Yao Chenab273e22017-09-06 12:53:50 -070082}
83
Yao Chenef99c4f2017-09-22 16:26:54 -070084StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070085}
86
Yangster-mace2cd6d52017-11-09 20:38:30 -080087void StatsLogProcessor::onAnomalyAlarmFired(
88 const uint64_t timestampNs,
89 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet) {
Yangster-macb0d06282018-01-05 15:44:07 -080090 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef2017-11-21 14:36:23 -080091 for (const auto& itr : mMetricsManagers) {
92 itr.second->onAnomalyAlarmFired(timestampNs, anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080093 }
94}
95
Yangster-macd40053e2018-01-09 16:29:22 -080096void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Yangster-mac7ba8fc32018-01-24 16:16:46 -080097 std::set<Field, FieldCmp> uidFields;
Yangster-mac68985802018-01-21 10:05:09 -080098 if (android::util::kAtomsWithAttributionChain.find(event->GetTagId()) !=
99 android::util::kAtomsWithAttributionChain.end()) {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800100 FieldMatcher matcher;
101 buildAttributionUidFieldMatcher(event->GetTagId(), Position::ANY, &matcher);
102 findFields(event->getFieldValueMap(), matcher, &uidFields);
Yangster-mac68985802018-01-21 10:05:09 -0800103 } else if (android::util::kAtomsWithUidField.find(event->GetTagId()) !=
104 android::util::kAtomsWithUidField.end()) {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800105 FieldMatcher matcher;
106 buildSimpleAtomFieldMatcher(
107 event->GetTagId(), 1 /* uid is always the 1st field. */, &matcher);
108 findFields(event->getFieldValueMap(), matcher, &uidFields);
Yangster-mac68985802018-01-21 10:05:09 -0800109 }
110
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800111 for (const auto& uidField : uidFields) {
112 DimensionsValue* value = event->findFieldValueOrNull(uidField);
Yangster-macd40053e2018-01-09 16:29:22 -0800113 if (value != nullptr && value->value_case() == DimensionsValue::ValueCase::kValueInt) {
114 const int uid = mUidMap->getHostUidOrSelf(value->value_int());
115 value->set_value_int(uid);
116 }
Yao Chen312e8982017-12-05 15:29:03 -0800117 }
Yangster-macd40053e2018-01-09 16:29:22 -0800118}
119
120void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
121 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
122 bool is_create = event.GetBool(3, &err);
123 auto parent_uid = int(event.GetLong(1, &err2));
124 auto isolated_uid = int(event.GetLong(2, &err3));
125 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
126 if (is_create) {
127 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
128 } else {
129 mUidMap->removeIsolatedUid(isolated_uid, parent_uid);
130 }
131 } else {
132 ALOGE("Failed to parse uid in the isolated uid change event.");
133 }
134}
135
136// TODO: what if statsd service restarts? How do we know what logs are already processed before?
137void StatsLogProcessor::OnLogEvent(LogEvent* event) {
138 std::lock_guard<std::mutex> lock(mMetricsMutex);
139 StatsdStats::getInstance().noteAtomLogged(
140 event->GetTagId(), event->GetTimestampNs() / NS_PER_SEC);
141
David Chen21582962017-11-01 17:32:46 -0700142 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800143 // The field numbers need to be currently updated by hand with atoms.proto
Yangster-macd40053e2018-01-09 16:29:22 -0800144 if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) {
145 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800146 }
147
148 if (mMetricsManagers.empty()) {
149 return;
150 }
151
Chenjie Yufa22d652018-02-05 14:37:48 -0800152 long curTime = time(nullptr);
153 if (curTime - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
154 mStatsPullerManager.ClearPullerCacheIfNecessary(curTime);
155 mLastPullerCacheClearTimeSec = curTime;
156 }
157
David Chencfc311d2018-01-23 17:55:54 -0800158 if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800159 // Map the isolated uid to host uid if necessary.
160 mapIsolatedUidToHostUidIfNecessaryLocked(event);
161 }
162
163 // pass the event to metrics managers.
164 for (auto& pair : mMetricsManagers) {
165 pair.second->onLogEvent(*event);
166 flushIfNecessaryLocked(event->GetTimestampNs(), pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700167 }
Yao Chenab273e22017-09-06 12:53:50 -0700168}
169
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700170void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800171 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chenb3561512017-11-21 18:07:17 -0800172 ALOGD("Updated configuration for key %s", key.ToString().c_str());
Yao Chend10f7b12017-12-18 12:53:50 -0800173 sp<MetricsManager> newMetricsManager = new MetricsManager(key, config, mTimeBaseSec, mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700174 auto it = mMetricsManagers.find(key);
Yao Chen288c6002017-12-12 13:43:18 -0800175 if (it == mMetricsManagers.end() && mMetricsManagers.size() > StatsdStats::kMaxConfigCount) {
Yao Chenb3561512017-11-21 18:07:17 -0800176 ALOGE("Can't accept more configs!");
177 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700178 }
179
Yao Chencaf339d2017-10-06 16:01:10 -0700180 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -0700181 mUidMap->OnConfigUpdated(key);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800182 newMetricsManager->setAnomalyMonitor(mAnomalyMonitor);
Yao Chen147ce602017-12-22 14:35:34 -0800183 if (newMetricsManager->shouldAddUidMapListener()) {
Yao Chend10f7b12017-12-18 12:53:50 -0800184 // We have to add listener after the MetricsManager is constructed because it's
185 // not safe to create wp or sp from this pointer inside its constructor.
186 mUidMap->addListener(newMetricsManager.get());
187 }
188 mMetricsManagers[key] = newMetricsManager;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700189 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chenb3561512017-11-21 18:07:17 -0800190 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700191 } else {
192 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800193 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700194 }
yro00698da2017-09-15 10:06:40 -0700195}
Bookatz906a35c2017-09-20 15:26:44 -0700196
Yangster7c334a12017-11-22 14:24:24 -0800197size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800198 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700199 auto it = mMetricsManagers.find(key);
200 if (it == mMetricsManagers.end()) {
201 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800202 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700203 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800204 return it->second->byteSize();
205}
206
Yao Chen884c8c12018-01-26 10:36:25 -0800207void StatsLogProcessor::dumpStates(FILE* out, bool verbose) {
208 std::lock_guard<std::mutex> lock(mMetricsMutex);
209 fprintf(out, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
210 for (auto metricsManager : mMetricsManagers) {
211 metricsManager.second->dumpStates(out, verbose);
212 }
213}
214
Yao Chen147ce602017-12-22 14:35:34 -0800215void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t& dumpTimeStampNs,
216 ConfigMetricsReportList* report) {
Yangster-macb0d06282018-01-05 15:44:07 -0800217 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac20877162017-12-22 17:19:39 -0800218 auto it = mMetricsManagers.find(key);
219 if (it == mMetricsManagers.end()) {
220 ALOGW("Config source %s does not exist", key.ToString().c_str());
221 return;
222 }
223 report->mutable_config_key()->set_uid(key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800224 report->mutable_config_key()->set_id(key.GetId());
Yangster-mac20877162017-12-22 17:19:39 -0800225 ConfigMetricsReport* configMetricsReport = report->add_reports();
226 it->second->onDumpReport(dumpTimeStampNs, configMetricsReport);
227 // TODO: dump uid mapping.
228}
229
David Chen1d7b0cd2017-11-15 14:20:04 -0800230void StatsLogProcessor::onDumpReport(const ConfigKey& key, vector<uint8_t>* outData) {
Yangster-macb0d06282018-01-05 15:44:07 -0800231 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac86179502018-01-23 15:47:15 -0800232 onDumpReportLocked(key, outData);
233}
234
235void StatsLogProcessor::onDumpReportLocked(const ConfigKey& key, vector<uint8_t>* outData) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800236 auto it = mMetricsManagers.find(key);
237 if (it == mMetricsManagers.end()) {
238 ALOGW("Config source %s does not exist", key.ToString().c_str());
239 return;
240 }
241
242 // This allows another broadcast to be sent within the rate-limit period if we get close to
243 // filling the buffer again soon.
David Chen1d7b0cd2017-11-15 14:20:04 -0800244 mLastBroadcastTimes.erase(key);
Yao Chen729093d2017-10-16 10:33:26 -0700245
yro17adac92017-11-08 23:16:29 -0800246 ProtoOutputStream proto;
247
yro947fbce2017-11-15 22:50:23 -0800248 // Start of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800249 long long configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
250 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800251 proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
yro17adac92017-11-08 23:16:29 -0800252 proto.end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800253 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800254
yro947fbce2017-11-15 22:50:23 -0800255 // Start of ConfigMetricsReport (reports).
256 long long reportsToken =
257 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);
258
259 // First, fill in ConfigMetricsReport using current data on memory, which
260 // starts from filling in StatsLogReport's.
Yao Chen288c6002017-12-12 13:43:18 -0800261 it->second->onDumpReport(&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.
271 proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_NANOS,
272 (long long)it->second->getLastReportTimeNs());
273 proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_NANOS,
274 (long long)::android::elapsedRealtimeNano());
275
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 Chen69f1baf2017-11-27 17:25:36 -0800295 StatsdStats::getInstance().noteMetricsReportSent(key);
Yao Chen729093d2017-10-16 10:33:26 -0700296}
297
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700298void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800299 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700300 auto it = mMetricsManagers.find(key);
301 if (it != mMetricsManagers.end()) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700302 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700303 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700304 }
Yao Chenb3561512017-11-21 18:07:17 -0800305 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800306
David Chen1d7b0cd2017-11-15 14:20:04 -0800307 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800308
309 if (mMetricsManagers.empty()) {
310 mStatsPullerManager.ForceClearPullerCache();
311 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700312}
313
Yangster-macb0d06282018-01-05 15:44:07 -0800314void StatsLogProcessor::flushIfNecessaryLocked(
315 uint64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) {
David Chend9269e22017-12-05 13:43:51 -0800316 auto lastCheckTime = mLastByteSizeTimes.find(key);
317 if (lastCheckTime != mLastByteSizeTimes.end()) {
318 if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
319 return;
320 }
321 }
322
323 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
324 size_t totalBytes = metricsManager.byteSize();
325 mLastByteSizeTimes[key] = timestampNs;
326 if (totalBytes >
327 StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data.
Yao Chen288c6002017-12-12 13:43:18 -0800328 // TODO(b/70571383): By 12/15/2017 add API to drop data directly
329 ProtoOutputStream proto;
330 metricsManager.onDumpReport(&proto);
David Chen12942952017-12-04 14:28:43 -0800331 StatsdStats::getInstance().noteDataDropped(key);
332 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chend9269e22017-12-05 13:43:51 -0800333 } else if (totalBytes > .9 * StatsdStats::kMaxMetricsBytesPerConfig) {
334 // Send broadcast so that receivers can pull data.
335 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
336 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
337 if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) {
338 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800339 return;
340 }
341 }
342 mLastBroadcastTimes[key] = timestampNs;
Yao Chenb3561512017-11-21 18:07:17 -0800343 VLOG("StatsD requesting broadcast for %s", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800344 mSendBroadcast(key);
Yao Chenb3561512017-11-21 18:07:17 -0800345 StatsdStats::getInstance().noteBroadcastSent(key);
yro31eb67b2017-10-24 13:33:21 -0700346 }
347}
348
yro947fbce2017-11-15 22:50:23 -0800349void StatsLogProcessor::WriteDataToDisk() {
Yangster-macb0d06282018-01-05 15:44:07 -0800350 std::lock_guard<std::mutex> lock(mMetricsMutex);
yro947fbce2017-11-15 22:50:23 -0800351 for (auto& pair : mMetricsManagers) {
352 const ConfigKey& key = pair.first;
353 vector<uint8_t> data;
Yangster-mac86179502018-01-23 15:47:15 -0800354 onDumpReportLocked(key, &data);
yro947fbce2017-11-15 22:50:23 -0800355 // TODO: Add a guardrail to prevent accumulation of file on disk.
yro98a28502018-01-18 17:00:14 -0800356 string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR, time(nullptr),
357 key.GetUid(), (long long)key.GetId());
yro947fbce2017-11-15 22:50:23 -0800358 StorageManager::writeFile(file_name.c_str(), &data[0], data.size());
359 }
360}
361
Yao Chenef99c4f2017-09-22 16:26:54 -0700362} // namespace statsd
363} // namespace os
364} // namespace android