blob: 8b681eddb60534eb497c9ca0355288c65afb0d29 [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) {
Chenjie Yu85ed8382017-12-14 16:48:54 -080082 StatsPullerManager statsPullerManager;
83 statsPullerManager.SetTimeBaseSec(mTimeBaseSec);
Yao Chenab273e22017-09-06 12:53:50 -070084}
85
Yao Chenef99c4f2017-09-22 16:26:54 -070086StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070087}
88
Yangster-mace2cd6d52017-11-09 20:38:30 -080089void StatsLogProcessor::onAnomalyAlarmFired(
90 const uint64_t timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080091 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
Yangster-macb0d06282018-01-05 15:44:07 -080092 std::lock_guard<std::mutex> lock(mMetricsMutex);
Bookatzcc5adef22017-11-21 14:36:23 -080093 for (const auto& itr : mMetricsManagers) {
Yangster-mac932ecec2018-02-01 10:23:52 -080094 itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
95 }
96}
97void StatsLogProcessor::onPeriodicAlarmFired(
98 const uint64_t timestampNs,
99 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
100
101 std::lock_guard<std::mutex> lock(mMetricsMutex);
102 for (const auto& itr : mMetricsManagers) {
103 itr.second->onPeriodicAlarmFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800104 }
105}
106
Yao Chen8a8d16c2018-02-08 14:50:40 -0800107void updateUid(Value* value, int hostUid) {
108 int uid = value->int_value;
109 if (uid != hostUid) {
110 value->setInt(hostUid);
111 }
112}
113
Yangster-macd40053e2018-01-09 16:29:22 -0800114void StatsLogProcessor::mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const {
Yao Chenc40a19d2018-03-15 16:48:25 -0700115 if (android::util::AtomsInfo::kAtomsWithAttributionChain.find(event->GetTagId()) !=
116 android::util::AtomsInfo::kAtomsWithAttributionChain.end()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800117 for (auto& value : *(event->getMutableValues())) {
118 if (value.mField.getPosAtDepth(0) > kAttributionField) {
119 break;
120 }
121 if (isAttributionUidField(value)) {
122 const int hostUid = mUidMap->getHostUidOrSelf(value.mValue.int_value);
123 updateUid(&value.mValue, hostUid);
124 }
Yangster-macd40053e2018-01-09 16:29:22 -0800125 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700126 } else {
127 auto it = android::util::AtomsInfo::kAtomsWithUidField.find(event->GetTagId());
128 if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) {
129 int uidField = it->second; // uidField is the field number in proto,
130 // starting from 1
131 if (uidField > 0 && (int)event->getValues().size() >= uidField &&
132 (event->getValues())[uidField - 1].mValue.getType() == INT) {
133 Value& value = (*event->getMutableValues())[uidField - 1].mValue;
134 const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
135 updateUid(&value, hostUid);
136 } else {
137 ALOGE("Malformed log, uid not found. %s", event->ToString().c_str());
138 }
139 }
Yao Chen312e8982017-12-05 15:29:03 -0800140 }
Yangster-macd40053e2018-01-09 16:29:22 -0800141}
142
143void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
144 status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR;
145 bool is_create = event.GetBool(3, &err);
146 auto parent_uid = int(event.GetLong(1, &err2));
147 auto isolated_uid = int(event.GetLong(2, &err3));
148 if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) {
149 if (is_create) {
150 mUidMap->assignIsolatedUid(isolated_uid, parent_uid);
151 } else {
152 mUidMap->removeIsolatedUid(isolated_uid, parent_uid);
153 }
154 } else {
155 ALOGE("Failed to parse uid in the isolated uid change event.");
156 }
157}
158
Yangster-macd40053e2018-01-09 16:29:22 -0800159void StatsLogProcessor::OnLogEvent(LogEvent* event) {
160 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen8f42ba02018-02-27 15:17:07 -0800161 if (event->GetElapsedTimestampNs() < mLastLogTimestamp) {
162 return;
163 }
164 mLastLogTimestamp = event->GetElapsedTimestampNs();
Yangster-macd40053e2018-01-09 16:29:22 -0800165 StatsdStats::getInstance().noteAtomLogged(
Yangster-mac330af582018-02-08 15:24:38 -0800166 event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC);
Yangster-macd40053e2018-01-09 16:29:22 -0800167
David Chen21582962017-11-01 17:32:46 -0700168 // Hard-coded logic to update the isolated uid's in the uid-map.
Stefan Lafonae2df012017-11-14 09:17:21 -0800169 // The field numbers need to be currently updated by hand with atoms.proto
Yangster-macd40053e2018-01-09 16:29:22 -0800170 if (event->GetTagId() == android::util::ISOLATED_UID_CHANGED) {
171 onIsolatedUidChangedEventLocked(*event);
David Chencfc311d2018-01-23 17:55:54 -0800172 }
173
174 if (mMetricsManagers.empty()) {
175 return;
176 }
177
Yangster-mac330af582018-02-08 15:24:38 -0800178 uint64_t curTimeSec = getElapsedRealtimeSec();
179 if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
180 mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec);
181 mLastPullerCacheClearTimeSec = curTimeSec;
Chenjie Yufa22d652018-02-05 14:37:48 -0800182 }
183
David Chencfc311d2018-01-23 17:55:54 -0800184 if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
Yangster-macd40053e2018-01-09 16:29:22 -0800185 // Map the isolated uid to host uid if necessary.
186 mapIsolatedUidToHostUidIfNecessaryLocked(event);
187 }
188
189 // pass the event to metrics managers.
190 for (auto& pair : mMetricsManagers) {
191 pair.second->onLogEvent(*event);
Yangster-mac330af582018-02-08 15:24:38 -0800192 flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second));
David Chen21582962017-11-01 17:32:46 -0700193 }
Yao Chenab273e22017-09-06 12:53:50 -0700194}
195
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700196void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
Yangster-macb0d06282018-01-05 15:44:07 -0800197 std::lock_guard<std::mutex> lock(mMetricsMutex);
Tej Singh484524a2018-02-01 15:10:05 -0800198 VLOG("Updated configuration for key %s", key.ToString().c_str());
Yangster-mac932ecec2018-02-01 10:23:52 -0800199 sp<MetricsManager> newMetricsManager =
200 new MetricsManager(key, config, mTimeBaseSec, mUidMap,
201 mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700202 auto it = mMetricsManagers.find(key);
Yao Chen288c6002017-12-12 13:43:18 -0800203 if (it == mMetricsManagers.end() && mMetricsManagers.size() > StatsdStats::kMaxConfigCount) {
Yao Chenb3561512017-11-21 18:07:17 -0800204 ALOGE("Can't accept more configs!");
205 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700206 }
207
Yao Chencaf339d2017-10-06 16:01:10 -0700208 if (newMetricsManager->isConfigValid()) {
David Chend6896892017-10-25 11:49:03 -0700209 mUidMap->OnConfigUpdated(key);
Yao Chen147ce602017-12-22 14:35:34 -0800210 if (newMetricsManager->shouldAddUidMapListener()) {
Yao Chend10f7b12017-12-18 12:53:50 -0800211 // We have to add listener after the MetricsManager is constructed because it's
212 // not safe to create wp or sp from this pointer inside its constructor.
213 mUidMap->addListener(newMetricsManager.get());
214 }
215 mMetricsManagers[key] = newMetricsManager;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700216 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chenb3561512017-11-21 18:07:17 -0800217 VLOG("StatsdConfig valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700218 } else {
219 // If there is any error in the config, don't use it.
Yao Chenb3561512017-11-21 18:07:17 -0800220 ALOGE("StatsdConfig NOT valid");
Yao Chencaf339d2017-10-06 16:01:10 -0700221 }
yro00698da2017-09-15 10:06:40 -0700222}
Bookatz906a35c2017-09-20 15:26:44 -0700223
Yangster7c334a12017-11-22 14:24:24 -0800224size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
Yangster-macb0d06282018-01-05 15:44:07 -0800225 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen729093d2017-10-16 10:33:26 -0700226 auto it = mMetricsManagers.find(key);
227 if (it == mMetricsManagers.end()) {
228 ALOGW("Config source %s does not exist", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800229 return 0;
Yao Chen729093d2017-10-16 10:33:26 -0700230 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800231 return it->second->byteSize();
232}
233
Yao Chen884c8c12018-01-26 10:36:25 -0800234void StatsLogProcessor::dumpStates(FILE* out, bool verbose) {
235 std::lock_guard<std::mutex> lock(mMetricsMutex);
236 fprintf(out, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size());
237 for (auto metricsManager : mMetricsManagers) {
238 metricsManager.second->dumpStates(out, verbose);
239 }
240}
241
Yao Chen8a8d16c2018-02-08 14:50:40 -0800242void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t dumpTimeStampNs,
243 vector<uint8_t>* outData) {
Yangster-macb0d06282018-01-05 15:44:07 -0800244 std::lock_guard<std::mutex> lock(mMetricsMutex);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800245 onDumpReportLocked(key, dumpTimeStampNs, outData);
Yangster-mac20877162017-12-22 17:19:39 -0800246}
247
Yao Chen8a8d16c2018-02-08 14:50:40 -0800248void StatsLogProcessor::onDumpReportLocked(const ConfigKey& key, const uint64_t dumpTimeStampNs,
249 vector<uint8_t>* outData) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800250 auto it = mMetricsManagers.find(key);
251 if (it == mMetricsManagers.end()) {
252 ALOGW("Config source %s does not exist", key.ToString().c_str());
253 return;
254 }
255
256 // This allows another broadcast to be sent within the rate-limit period if we get close to
257 // filling the buffer again soon.
David Chen1d7b0cd2017-11-15 14:20:04 -0800258 mLastBroadcastTimes.erase(key);
Yao Chen729093d2017-10-16 10:33:26 -0700259
yro17adac92017-11-08 23:16:29 -0800260 ProtoOutputStream proto;
261
yro947fbce2017-11-15 22:50:23 -0800262 // Start of ConfigKey.
Yi Jin5ee07872018-03-05 18:18:27 -0800263 uint64_t configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY);
yro17adac92017-11-08 23:16:29 -0800264 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800265 proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId());
yro17adac92017-11-08 23:16:29 -0800266 proto.end(configKeyToken);
yro947fbce2017-11-15 22:50:23 -0800267 // End of ConfigKey.
yro17adac92017-11-08 23:16:29 -0800268
yro947fbce2017-11-15 22:50:23 -0800269 // Start of ConfigMetricsReport (reports).
Yi Jin5ee07872018-03-05 18:18:27 -0800270 uint64_t reportsToken =
yro947fbce2017-11-15 22:50:23 -0800271 proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);
272
Yangster-mac330af582018-02-08 15:24:38 -0800273 int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
Yangster-mac3fa5d7f2018-03-10 21:50:27 -0800274 int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
275
yro947fbce2017-11-15 22:50:23 -0800276 // First, fill in ConfigMetricsReport using current data on memory, which
277 // starts from filling in StatsLogReport's.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800278 it->second->onDumpReport(dumpTimeStampNs, &proto);
yro17adac92017-11-08 23:16:29 -0800279
280 // Fill in UidMap.
David Chenf384b902018-03-14 18:36:45 -0700281 vector<uint8_t> uidMap;
282 mUidMap->getOutput(key, &uidMap);
283 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMap.data());
yro17adac92017-11-08 23:16:29 -0800284
David Chen16049572018-02-01 18:27:51 -0800285 // Fill in the timestamps.
Yangster-mac330af582018-02-08 15:24:38 -0800286 proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
287 (long long)lastReportTimeNs);
288 proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
289 (long long)dumpTimeStampNs);
Yangster-mac3fa5d7f2018-03-10 21:50:27 -0800290 proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
291 (long long)lastReportWallClockNs);
292 proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
293 (long long)getWallClockNs());
David Chen16049572018-02-01 18:27:51 -0800294
yro947fbce2017-11-15 22:50:23 -0800295 // End of ConfigMetricsReport (reports).
296 proto.end(reportsToken);
297
298 // Then, check stats-data directory to see there's any file containing
299 // ConfigMetricsReport from previous shutdowns to concatenate to reports.
yro98a28502018-01-18 17:00:14 -0800300 StorageManager::appendConfigMetricsReport(proto);
yro947fbce2017-11-15 22:50:23 -0800301
David Chen1d7b0cd2017-11-15 14:20:04 -0800302 if (outData != nullptr) {
303 outData->clear();
304 outData->resize(proto.size());
305 size_t pos = 0;
306 auto iter = proto.data();
307 while (iter.readBuffer() != NULL) {
308 size_t toRead = iter.currentToRead();
309 std::memcpy(&((*outData)[pos]), iter.readBuffer(), toRead);
310 pos += toRead;
311 iter.rp()->move(toRead);
312 }
yro17adac92017-11-08 23:16:29 -0800313 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800314
Yao Chen69f1baf2017-11-27 17:25:36 -0800315 StatsdStats::getInstance().noteMetricsReportSent(key);
Yao Chen729093d2017-10-16 10:33:26 -0700316}
317
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700318void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
Yangster-macb0d06282018-01-05 15:44:07 -0800319 std::lock_guard<std::mutex> lock(mMetricsMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700320 auto it = mMetricsManagers.find(key);
321 if (it != mMetricsManagers.end()) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700322 mMetricsManagers.erase(it);
David Chend6896892017-10-25 11:49:03 -0700323 mUidMap->OnConfigRemoved(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700324 }
Yao Chenb3561512017-11-21 18:07:17 -0800325 StatsdStats::getInstance().noteConfigRemoved(key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800326
David Chen1d7b0cd2017-11-15 14:20:04 -0800327 mLastBroadcastTimes.erase(key);
Chenjie Yufa22d652018-02-05 14:37:48 -0800328
329 if (mMetricsManagers.empty()) {
330 mStatsPullerManager.ForceClearPullerCache();
331 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700332}
333
Yangster-macb0d06282018-01-05 15:44:07 -0800334void StatsLogProcessor::flushIfNecessaryLocked(
335 uint64_t timestampNs, const ConfigKey& key, MetricsManager& metricsManager) {
David Chend9269e22017-12-05 13:43:51 -0800336 auto lastCheckTime = mLastByteSizeTimes.find(key);
337 if (lastCheckTime != mLastByteSizeTimes.end()) {
338 if (timestampNs - lastCheckTime->second < StatsdStats::kMinByteSizeCheckPeriodNs) {
339 return;
340 }
341 }
342
343 // We suspect that the byteSize() computation is expensive, so we set a rate limit.
344 size_t totalBytes = metricsManager.byteSize();
345 mLastByteSizeTimes[key] = timestampNs;
346 if (totalBytes >
347 StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data.
Yao Chen06dba5d2018-01-26 13:38:16 -0800348 metricsManager.dropData(timestampNs);
David Chen12942952017-12-04 14:28:43 -0800349 StatsdStats::getInstance().noteDataDropped(key);
350 VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str());
David Chend9269e22017-12-05 13:43:51 -0800351 } else if (totalBytes > .9 * StatsdStats::kMaxMetricsBytesPerConfig) {
352 // Send broadcast so that receivers can pull data.
353 auto lastBroadcastTime = mLastBroadcastTimes.find(key);
354 if (lastBroadcastTime != mLastBroadcastTimes.end()) {
355 if (timestampNs - lastBroadcastTime->second < StatsdStats::kMinBroadcastPeriodNs) {
356 VLOG("StatsD would've sent a broadcast but the rate limit stopped us.");
David Chen1d7b0cd2017-11-15 14:20:04 -0800357 return;
358 }
359 }
360 mLastBroadcastTimes[key] = timestampNs;
Yao Chenb3561512017-11-21 18:07:17 -0800361 VLOG("StatsD requesting broadcast for %s", key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -0800362 mSendBroadcast(key);
Yao Chenb3561512017-11-21 18:07:17 -0800363 StatsdStats::getInstance().noteBroadcastSent(key);
yro31eb67b2017-10-24 13:33:21 -0700364 }
365}
366
yro947fbce2017-11-15 22:50:23 -0800367void StatsLogProcessor::WriteDataToDisk() {
Yangster-macb0d06282018-01-05 15:44:07 -0800368 std::lock_guard<std::mutex> lock(mMetricsMutex);
yro947fbce2017-11-15 22:50:23 -0800369 for (auto& pair : mMetricsManagers) {
370 const ConfigKey& key = pair.first;
371 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800372 onDumpReportLocked(key, getElapsedRealtimeNs(), &data);
Yangster-mac330af582018-02-08 15:24:38 -0800373 string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
374 (long)getWallClockSec(), key.GetUid(), (long long)key.GetId());
yro947fbce2017-11-15 22:50:23 -0800375 StorageManager::writeFile(file_name.c_str(), &data[0], data.size());
376 }
377}
378
Yao Chenef99c4f2017-09-22 16:26:54 -0700379} // namespace statsd
380} // namespace os
381} // namespace android