blob: c1ff27545401631b36811c58741d11d8e4f99083 [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;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080065const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5;
66const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6;
67
yro947fbce2017-11-15 22:50:23 -080068
yro03faf092017-12-12 00:17:50 -080069#define STATS_DATA_DIR "/data/misc/stats-data"
yro17adac92017-11-08 23:16:29 -080070
yro31eb67b2017-10-24 13:33:21 -070071StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap,
Yangster-mac932ecec2018-02-01 10:23:52 -080072 const sp<AlarmMonitor>& anomalyAlarmMonitor,
73 const sp<AlarmMonitor>& periodicAlarmMonitor,
Yangster-mac20877162017-12-22 17:19:39 -080074 const long timeBaseSec,
David Chen1d7b0cd2017-11-15 14:20:04 -080075 const std::function<void(const ConfigKey&)>& sendBroadcast)
Chenjie Yu85ed8382017-12-14 16:48:54 -080076 : mUidMap(uidMap),
Yangster-mac932ecec2018-02-01 10:23:52 -080077 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
78 mPeriodicAlarmMonitor(periodicAlarmMonitor),
Chenjie Yu85ed8382017-12-14 16:48:54 -080079 mSendBroadcast(sendBroadcast),
Yao Chen8f42ba02018-02-27 15:17:07 -080080 mTimeBaseSec(timeBaseSec),
81 mLastLogTimestamp(0) {
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,
Yangster-mac932ecec2018-02-01 10:23:52 -080089 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
Yangster-macb0d06282018-01-05 15:44:07 -080090 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef22017-11-21 14:36:23 -080091 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -080092 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
93 }
94}
95void StatsLogProcessor::onPeriodicAlarmFired(
96 const uint64_t timestampNs,
97 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
98
99 std::lock_guard<std::mutex> lock(mMetricsMutex);
100 for (const auto& itr : mMetricsManagers) {
101 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800102 }
103}
104
Yao Chen8a8d16c2018-02-08 14:50:40 -0800105void updateUid(Value* value, int hostUid) {
106 int uid = value->int_value;
107 if (uid != hostUid) {
108 value->setInt(hostUid);
109 }
110}
111
Yangster-macd40053e2018-01-09 16:29:22 -0800112void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Yao Chenc40a19d2018-03-15 16:48:25 -0700113 if (android::util::AtomsInfo::kAtomsWithAttributionChain.find(event->GetTagId()) !=
114 android::util::AtomsInfo::kAtomsWithAttributionChain.end()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800115 for (auto& value : *(event->getMutableValues())) {
116 if (value.mField.getPosAtDepth(0) > kAttributionField) {
117 break;
118 }
119 if (isAttributionUidField(value)) {
120 const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value);
121 updateUid(&value.mValue, hostUid);
122 }
Yangster-macd40053e2018-01-09 16:29:22 -0800123 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700124 } else {
125 auto it = android::util::AtomsInfo::kAtomsWithUidField.find(event->GetTagId());
126 if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) {
127 int uidField = it->second; // uidField is the field number in proto,
128 // starting from 1
129 if (uidField > 0 && (int)event->getValues().size() >= uidField &&
130 (event->getValues())[uidField - 1].mValue.getType() == INT) {
131 Value& value = (*event->getMutableValues())[uidField - 1].mValue;
132 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
133 updateUid(&value, hostUid);
134 } else {
135 ALOGE("Malformed log, uid not found. %s", event->ToString().c_str());
136 }
137 }
Yao Chen312e8982017-12-05 15:29:03 -0800138 }
Yangster-macd40053e2018-01-09 16:29:22 -0800139}
140
141void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
142 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
143 bool is_create = event.GetBool(3, &err);
144 auto parent_uid = int(event.GetLong(1, &err2));
145 auto isolated_uid = int(event.GetLong(2, &err3));
146 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
147 if (is_create) {
148 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
149 } else {
150 mUidMap->removeIsolatedUid(isolated_uid, parent_uid);
151 }
152 } else {
153 ALOGE("Failed to parse uid in the isolated uid change event.");
154 }
155}
156
Yangster-macd40053e2018-01-09 16:29:22 -0800157void StatsLogProcessor::OnLogEvent(LogEvent* event) {
158 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen8f42ba02018-02-27 15:17:07 -0800159 if (event->GetElapsedTimestampNs() < mLastLogTimestamp) {
160 return;
161 }
162 mLastLogTimestamp = event->GetElapsedTimestampNs();
Yangster-macd40053e2018-01-09 16:29:22 -0800163 StatsdStats::getInstance().noteAtomLogged(
Yangster-mac330af582018-02-08 15:24:38 -0800164 event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC);
Yangster-macd40053e2018-01-09 16:29:22 -0800165
David Chen21582962017-11-01 17:32:46 -0700166 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800167 // The field numbers need to be currently updated by hand with atoms.proto
Yangster-macd40053e2018-01-09 16:29:22 -0800168 if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) {
169 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800170 }
171
172 if (mMetricsManagers.empty()) {
173 return;
174 }
175
Yangster-mac330af582018-02-08 15:24:38 -0800176 uint64_t curTimeSec = getElapsedRealtimeSec();
177 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700178 mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC);
Yangster-mac330af582018-02-08 15:24:38 -0800179 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800180 }
181
David Chencfc311d2018-01-23 17:55:54 -0800182 if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800183 // Map the isolated uid to host uid if necessary.
184 mapIsolatedUidToHostUidIfNecessaryLocked(event);
185 }
186
187 // pass the event to metrics managers.
188 for (auto& pair : mMetricsManagers) {
189 pair.second->onLogEvent(*event);
Yangster-mac330af582018-02-08 15:24:38 -0800190 flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700191 }
Yao Chenab273e22017-09-06 12:53:50 -0700192}
193
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700194void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800195 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singh484524a2018-02-01 15:10:05 -0800196 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800197 sp<MetricsManager> newMetricsManager =
198 new MetricsManager(key, config, mTimeBaseSec, mUidMap,
199 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Yao Chen44cf27c2017-09-14 22:32:50 -0700200
Yao Chencaf339d2017-10-06 16:01:10 -0700201 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -0700202 mUidMap->OnConfigUpdated(key);
Yao Chen147ce602017-12-22 14:35:34 -0800203 if (newMetricsManager->shouldAddUidMapListener()) {
Yao Chend10f7b12017-12-18 12:53:50 -0800204 // We have to add listener after the MetricsManager is constructed because it's
205 // not safe to create wp or sp from this pointer inside its constructor.
206 mUidMap->addListener(newMetricsManager.get());
207 }
208 mMetricsManagers[key] = newMetricsManager;
Yao Chenb3561512017-11-21 18:07:17 -0800209 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700210 } else {
211 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800212 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700213 }
yro00698da2017-09-15 10:06:40 -0700214}
Bookatz906a35c2017-09-20 15:26:44 -0700215
Yangster7c334a12017-11-22 14:24:24 -0800216size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800217 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700218 auto it = mMetricsManagers.find(key);
219 if (it == mMetricsManagers.end()) {
220 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800221 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700222 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800223 return it->second->byteSize();
224}
225
Yao Chen884c8c12018-01-26 10:36:25 -0800226void StatsLogProcessor::dumpStates(FILE* out, bool verbose) {
227 std::lock_guard<std::mutex> lock(mMetricsMutex);
228 fprintf(out, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
229 for (auto metricsManager : mMetricsManagers) {
230 metricsManager.second->dumpStates(out, verbose);
231 }
232}
233
yro4beccbe2018-03-15 19:42:05 -0700234/*
235 * onDumpReport dumps serialized ConfigMetricsReportList into outData.
236 */
Yao Chen8a8d16c2018-02-08 14:50:40 -0800237void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t dumpTimeStampNs,
238 vector<uint8_t>* outData) {
Yangster-macb0d06282018-01-05 15:44:07 -0800239 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yangster-mac20877162017-12-22 17:19:39 -0800240
David Chen1d7b0cd2017-11-15 14:20:04 -0800241 auto it = mMetricsManagers.find(key);
242 if (it == mMetricsManagers.end()) {
243 ALOGW("Config source %s does not exist", key.ToString().c_str());
244 return;
245 }
246
247 // This allows another broadcast to be sent within the rate-limit period if we get close to
248 // filling the buffer again soon.
David Chen1d7b0cd2017-11-15 14:20:04 -0800249 mLastBroadcastTimes.erase(key);
Yao Chen729093d2017-10-16 10:33:26 -0700250
yro17adac92017-11-08 23:16:29 -0800251 ProtoOutputStream proto;
252
yro947fbce2017-11-15 22:50:23 -0800253 // Start of ConfigKey.
Yi Jin5ee07872018-03-05 18:18:27 -0800254 uint64_t configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
yro17adac92017-11-08 23:16:29 -0800255 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800256 proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
yro17adac92017-11-08 23:16:29 -0800257 proto.end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800258 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800259
yro947fbce2017-11-15 22:50:23 -0800260 // Start of ConfigMetricsReport (reports).
Yi Jin5ee07872018-03-05 18:18:27 -0800261 uint64_t reportsToken =
yro947fbce2017-11-15 22:50:23 -0800262 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);
yro4beccbe2018-03-15 19:42:05 -0700263 onConfigMetricsReportLocked(key, dumpTimeStampNs, &proto);
yro947fbce2017-11-15 22:50:23 -0800264 proto.end(reportsToken);
yro4beccbe2018-03-15 19:42:05 -0700265 // End of ConfigMetricsReport (reports).
266
yro947fbce2017-11-15 22:50:23 -0800267
268 // Then, check stats-data directory to see there's any file containing
269 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
yro4beccbe2018-03-15 19:42:05 -0700270 StorageManager::appendConfigMetricsReport(key, &proto);
yro947fbce2017-11-15 22:50:23 -0800271
David Chen1d7b0cd2017-11-15 14:20:04 -0800272 if (outData != nullptr) {
273 outData->clear();
274 outData->resize(proto.size());
275 size_t pos = 0;
276 auto iter = proto.data();
277 while (iter.readBuffer() != NULL) {
278 size_t toRead = iter.currentToRead();
279 std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead);
280 pos += toRead;
281 iter.rp()->move(toRead);
282 }
yro17adac92017-11-08 23:16:29 -0800283 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800284
Yao Chen69f1baf2017-11-27 17:25:36 -0800285 StatsdStats::getInstance().noteMetricsReportSent(key);
Yao Chen729093d2017-10-16 10:33:26 -0700286}
287
yro4beccbe2018-03-15 19:42:05 -0700288/*
289 * onConfigMetricsReportLocked dumps serialized ConfigMetricsReport into outData.
290 */
291void StatsLogProcessor::onConfigMetricsReportLocked(const ConfigKey& key,
292 const uint64_t dumpTimeStampNs,
293 ProtoOutputStream* proto) {
294 // We already checked whether key exists in mMetricsManagers in
295 // WriteDataToDisk.
296 auto it = mMetricsManagers.find(key);
297 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
298 int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
299
300 // First, fill in ConfigMetricsReport using current data on memory, which
301 // starts from filling in StatsLogReport's.
302 it->second->onDumpReport(dumpTimeStampNs, proto);
303
304 // Fill in UidMap.
305 uint64_t uidMapToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP);
306 mUidMap->appendUidMap(key, proto);
307 proto->end(uidMapToken);
308
309 // Fill in the timestamps.
310 proto->write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
311 (long long)lastReportTimeNs);
312 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
313 (long long)dumpTimeStampNs);
314 proto->write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
315 (long long)lastReportWallClockNs);
316 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
317 (long long)getWallClockNs());
318
319
320}
321
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700322void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800323 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700324 auto it = mMetricsManagers.find(key);
325 if (it != mMetricsManagers.end()) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700326 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700327 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700328 }
Yao Chenb3561512017-11-21 18:07:17 -0800329 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800330
David Chen1d7b0cd2017-11-15 14:20:04 -0800331 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800332
333 if (mMetricsManagers.empty()) {
334 mStatsPullerManager.ForceClearPullerCache();
335 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700336}
337
Yangster-macb0d06282018-01-05 15:44:07 -0800338void StatsLogProcessor::flushIfNecessaryLocked(
339 uint64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) {
David Chend9269e22017-12-05 13:43:51 -0800340 auto lastCheckTime = mLastByteSizeTimes.find(key);
341 if (lastCheckTime != mLastByteSizeTimes.end()) {
342 if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
343 return;
344 }
345 }
346
347 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
348 size_t totalBytes = metricsManager.byteSize();
349 mLastByteSizeTimes[key] = timestampNs;
350 if (totalBytes >
351 StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data.
Yao Chen06dba5d2018-01-26 13:38:16 -0800352 metricsManager.dropData(timestampNs);
David Chen12942952017-12-04 14:28:43 -0800353 StatsdStats::getInstance().noteDataDropped(key);
354 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chen4c6d97a2018-03-22 16:31:40 -0700355 } else if (totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) {
David Chend9269e22017-12-05 13:43:51 -0800356 // Send broadcast so that receivers can pull data.
357 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
358 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
359 if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) {
360 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800361 return;
362 }
363 }
364 mLastBroadcastTimes[key] = timestampNs;
Yao Chenb3561512017-11-21 18:07:17 -0800365 VLOG("StatsD requesting broadcast for %s", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800366 mSendBroadcast(key);
Yao Chenb3561512017-11-21 18:07:17 -0800367 StatsdStats::getInstance().noteBroadcastSent(key);
yro31eb67b2017-10-24 13:33:21 -0700368 }
369}
370
yro947fbce2017-11-15 22:50:23 -0800371void StatsLogProcessor::WriteDataToDisk() {
Yangster-macb0d06282018-01-05 15:44:07 -0800372 std::lock_guard<std::mutex> lock(mMetricsMutex);
yro947fbce2017-11-15 22:50:23 -0800373 for (auto& pair : mMetricsManagers) {
374 const ConfigKey& key = pair.first;
yro4beccbe2018-03-15 19:42:05 -0700375 ProtoOutputStream proto;
376 onConfigMetricsReportLocked(key, getElapsedRealtimeNs(), &proto);
Yangster-mac330af582018-02-08 15:24:38 -0800377 string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
378 (long)getWallClockSec(), key.GetUid(), (long long)key.GetId());
yro4beccbe2018-03-15 19:42:05 -0700379 android::base::unique_fd fd(open(file_name.c_str(),
380 O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR));
381 if (fd == -1) {
382 VLOG("Attempt to write %s but failed", file_name.c_str());
383 return;
384 }
385 proto.flush(fd.get());
yro947fbce2017-11-15 22:50:23 -0800386 }
387}
388
Yao Chenef99c4f2017-09-22 16:26:54 -0700389} // namespace statsd
390} // namespace os
391} // namespace android