blob: 86c6e5c47af782445c30af32360d31d110b4b734 [file] [log] [blame]
Yao Chenb3561512017-11-21 18:07:17 -08001/*
2 * Copyright 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 */
Tej Singh484524a2018-02-01 15:10:05 -080016#define DEBUG false // STOPSHIP if true
Yao Chenb3561512017-11-21 18:07:17 -080017#include "Log.h"
18
19#include "StatsdStats.h"
20
21#include <android/util/ProtoOutputStream.h>
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "../stats_log_util.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "statslog.h"
yro665208d2018-03-13 18:08:09 -070024#include "storage/StorageManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080025
26namespace android {
27namespace os {
28namespace statsd {
29
30using android::util::FIELD_COUNT_REPEATED;
31using android::util::FIELD_TYPE_BOOL;
32using android::util::FIELD_TYPE_FLOAT;
33using android::util::FIELD_TYPE_INT32;
34using android::util::FIELD_TYPE_INT64;
35using android::util::FIELD_TYPE_MESSAGE;
36using android::util::FIELD_TYPE_STRING;
37using android::util::ProtoOutputStream;
38using std::lock_guard;
39using std::map;
Yao Chen20e9e622018-02-28 11:18:51 -080040using std::shared_ptr;
Yao Chenb3561512017-11-21 18:07:17 -080041using std::string;
42using std::vector;
43
44const int FIELD_ID_BEGIN_TIME = 1;
45const int FIELD_ID_END_TIME = 2;
46const int FIELD_ID_CONFIG_STATS = 3;
Yao Chenb3561512017-11-21 18:07:17 -080047const int FIELD_ID_ATOM_STATS = 7;
David Chenc136f452017-11-27 11:52:26 -080048const int FIELD_ID_UIDMAP_STATS = 8;
Bookatz1d0136d2017-12-01 11:13:32 -080049const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
Yangster-macb8382a12018-04-04 10:39:12 -070050const int FIELD_ID_PERIODIC_ALARM_STATS = 12;
Yangster-mac892f3d32018-05-02 14:16:48 -070051const int FIELD_ID_SYSTEM_SERVER_RESTART = 15;
Yao Chen39b67992018-11-08 15:32:17 -080052const int FIELD_ID_LOGGER_ERROR_STATS = 16;
Yao Chenb3561512017-11-21 18:07:17 -080053
Yao Chenb3561512017-11-21 18:07:17 -080054const int FIELD_ID_ATOM_STATS_TAG = 1;
55const int FIELD_ID_ATOM_STATS_COUNT = 2;
56
Bookatz1d0136d2017-12-01 11:13:32 -080057const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
Yangster-macb8382a12018-04-04 10:39:12 -070058const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1;
Bookatz1d0136d2017-12-01 11:13:32 -080059
Yao Chen39b67992018-11-08 15:32:17 -080060const int FIELD_ID_LOG_LOSS_STATS_TIME = 1;
61const int FIELD_ID_LOG_LOSS_STATS_COUNT = 2;
62const int FIELD_ID_LOG_LOSS_STATS_ERROR = 3;
Yao Chen884c8c12018-01-26 10:36:25 -080063
Yao Chen20e9e622018-02-28 11:18:51 -080064const int FIELD_ID_CONFIG_STATS_UID = 1;
65const int FIELD_ID_CONFIG_STATS_ID = 2;
66const int FIELD_ID_CONFIG_STATS_CREATION = 3;
Tej Singh5260eea2018-04-04 17:27:14 -070067const int FIELD_ID_CONFIG_STATS_RESET = 19;
Yao Chen20e9e622018-02-28 11:18:51 -080068const int FIELD_ID_CONFIG_STATS_DELETION = 4;
69const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
70const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
71const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
72const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
73const int FIELD_ID_CONFIG_STATS_VALID = 9;
74const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
Chenjie Yuc3c30c02018-10-26 09:48:07 -070075const int FIELD_ID_CONFIG_STATS_DATA_DROP_TIME = 11;
76const int FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES = 21;
Yangster-mace68f3a52018-04-04 00:01:43 -070077const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12;
78const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20;
Yao Chen20e9e622018-02-28 11:18:51 -080079const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
80const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
81const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
82const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
Yangster-mac306ccc22018-03-24 15:03:40 -070083const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17;
David Chenfaa1af52018-03-30 15:14:04 -070084const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18;
85const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
86const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
Yao Chen20e9e622018-02-28 11:18:51 -080087
88const int FIELD_ID_MATCHER_STATS_ID = 1;
89const int FIELD_ID_MATCHER_STATS_COUNT = 2;
90const int FIELD_ID_CONDITION_STATS_ID = 1;
91const int FIELD_ID_CONDITION_STATS_COUNT = 2;
92const int FIELD_ID_METRIC_STATS_ID = 1;
93const int FIELD_ID_METRIC_STATS_COUNT = 2;
94const int FIELD_ID_ALERT_STATS_ID = 1;
95const int FIELD_ID_ALERT_STATS_COUNT = 2;
96
David Chenbd125272018-04-04 19:02:50 -070097const int FIELD_ID_UID_MAP_CHANGES = 1;
98const int FIELD_ID_UID_MAP_BYTES_USED = 2;
99const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
100const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
Yao Chen20e9e622018-02-28 11:18:51 -0800101
Chenjie Yuc5875052018-03-09 10:13:11 -0800102const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100103 {android::util::BINDER_CALLS, {6000, 10000}},
Chenjie Yuc5875052018-03-09 10:13:11 -0800104 {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
105};
106
Yao Chenb3561512017-11-21 18:07:17 -0800107StatsdStats::StatsdStats() {
108 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -0800109 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800110}
111
112StatsdStats& StatsdStats::getInstance() {
113 static StatsdStats statsInstance;
114 return statsInstance;
115}
116
Yao Chen20e9e622018-02-28 11:18:51 -0800117void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
Yao Chenf6723df2018-01-08 15:11:58 -0800118 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
119 if (mIceBox.size() == kMaxIceBoxSize) {
120 mIceBox.pop_front();
121 }
122 mIceBox.push_back(stats);
123}
124
David Chenfaa1af52018-03-30 15:14:04 -0700125void StatsdStats::noteConfigReceived(
126 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
127 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
128 bool isValid) {
Yao Chenb3561512017-11-21 18:07:17 -0800129 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -0800130 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800131
132 // If there is an existing config for the same key, icebox the old config.
133 noteConfigRemovedInternalLocked(key);
134
Yao Chen20e9e622018-02-28 11:18:51 -0800135 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
136 configStats->uid = key.GetUid();
137 configStats->id = key.GetId();
138 configStats->creation_time_sec = nowTimeSec;
139 configStats->metric_count = metricsCount;
140 configStats->condition_count = conditionsCount;
141 configStats->matcher_count = matchersCount;
142 configStats->alert_count = alertsCount;
143 configStats->is_valid = isValid;
David Chenfaa1af52018-03-30 15:14:04 -0700144 for (auto& v : annotations) {
145 configStats->annotations.emplace_back(v);
146 }
Yao Chenb3561512017-11-21 18:07:17 -0800147
148 if (isValid) {
149 mConfigStats[key] = configStats;
150 } else {
Yao Chen20e9e622018-02-28 11:18:51 -0800151 configStats->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800152 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800153 }
154}
155
156void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
157 auto it = mConfigStats.find(key);
158 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800159 int32_t nowTimeSec = getWallClockSec();
Yao Chen20e9e622018-02-28 11:18:51 -0800160 it->second->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800161 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800162 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800163 }
164}
165
166void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
167 lock_guard<std::mutex> lock(mLock);
168 noteConfigRemovedInternalLocked(key);
169}
170
Yangster-macb142cc82018-03-30 15:22:08 -0700171void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
172 auto it = mConfigStats.find(key);
173 if (it != mConfigStats.end()) {
174 it->second->reset_time_sec = getWallClockSec();
175 }
176}
177
178void StatsdStats::noteConfigReset(const ConfigKey& key) {
179 lock_guard<std::mutex> lock(mLock);
180 noteConfigResetInternalLocked(key);
181}
182
Yao Chen39b67992018-11-08 15:32:17 -0800183void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError) {
Yao Chen163d2602018-04-10 10:39:53 -0700184 lock_guard<std::mutex> lock(mLock);
Yao Chen0f5d6612018-08-22 14:11:51 -0700185 if (mLogLossStats.size() == kMaxLoggerErrors) {
186 mLogLossStats.pop_front();
Yao Chen163d2602018-04-10 10:39:53 -0700187 }
Yao Chen39b67992018-11-08 15:32:17 -0800188 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError);
Yao Chen163d2602018-04-10 10:39:53 -0700189}
190
Yao Chenb3561512017-11-21 18:07:17 -0800191void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800192 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800193}
194
195void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800196 lock_guard<std::mutex> lock(mLock);
197 auto it = mConfigStats.find(key);
198 if (it == mConfigStats.end()) {
199 ALOGE("Config key %s not found!", key.ToString().c_str());
200 return;
201 }
Yao Chen20e9e622018-02-28 11:18:51 -0800202 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
203 it->second->broadcast_sent_time_sec.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800204 }
Yao Chen20e9e622018-02-28 11:18:51 -0800205 it->second->broadcast_sent_time_sec.push_back(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800206}
207
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700208void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
209 noteDataDropped(key, totalBytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800210}
211
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700212void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800213 lock_guard<std::mutex> lock(mLock);
214 auto it = mConfigStats.find(key);
215 if (it == mConfigStats.end()) {
216 ALOGE("Config key %s not found!", key.ToString().c_str());
217 return;
218 }
Yao Chen20e9e622018-02-28 11:18:51 -0800219 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
220 it->second->data_drop_time_sec.pop_front();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700221 it->second->data_drop_bytes.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800222 }
Yao Chen20e9e622018-02-28 11:18:51 -0800223 it->second->data_drop_time_sec.push_back(timeSec);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700224 it->second->data_drop_bytes.push_back(totalBytes);
Yao Chenb3561512017-11-21 18:07:17 -0800225}
226
Yangster-mace68f3a52018-04-04 00:01:43 -0700227void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
228 noteMetricsReportSent(key, num_bytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800229}
230
Yangster-mace68f3a52018-04-04 00:01:43 -0700231void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
232 int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800233 lock_guard<std::mutex> lock(mLock);
234 auto it = mConfigStats.find(key);
235 if (it == mConfigStats.end()) {
236 ALOGE("Config key %s not found!", key.ToString().c_str());
237 return;
238 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700239 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
240 it->second->dump_report_stats.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800241 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700242 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
Yao Chen69f1baf2017-11-27 17:25:36 -0800243}
244
David Chenbd125272018-04-04 19:02:50 -0700245void StatsdStats::noteUidMapDropped(int deltas) {
David Chenc136f452017-11-27 11:52:26 -0800246 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800247 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
David Chenc136f452017-11-27 11:52:26 -0800248}
249
David Chenbd125272018-04-04 19:02:50 -0700250void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f452017-11-27 11:52:26 -0800251 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700252 mUidMapStats.deleted_apps++;
David Chenc136f452017-11-27 11:52:26 -0800253}
254
255void StatsdStats::setUidMapChanges(int changes) {
256 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800257 mUidMapStats.changes = changes;
David Chenc136f452017-11-27 11:52:26 -0800258}
259
260void StatsdStats::setCurrentUidMapMemory(int bytes) {
261 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800262 mUidMapStats.bytes_used = bytes;
David Chenc136f452017-11-27 11:52:26 -0800263}
264
Yangster-mac94e197c2018-01-02 16:03:03 -0800265void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800266 lock_guard<std::mutex> lock(mLock);
267 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800268 auto statsIt = mConfigStats.find(key);
269 if (statsIt == mConfigStats.end()) {
270 return;
271 }
272
273 auto& conditionSizeMap = statsIt->second->condition_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800274 if (size > conditionSizeMap[id]) {
275 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800276 }
277}
278
Yangster-mac94e197c2018-01-02 16:03:03 -0800279void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800280 lock_guard<std::mutex> lock(mLock);
281 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800282 auto statsIt = mConfigStats.find(key);
283 if (statsIt == mConfigStats.end()) {
284 return;
285 }
286 auto& metricsDimensionMap = statsIt->second->metric_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800287 if (size > metricsDimensionMap[id]) {
288 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800289 }
290}
291
Yangster-mac306ccc22018-03-24 15:03:40 -0700292void StatsdStats::noteMetricDimensionInConditionSize(
293 const ConfigKey& key, const int64_t& id, int size) {
294 lock_guard<std::mutex> lock(mLock);
295 // if name doesn't exist before, it will create the key with count 0.
296 auto statsIt = mConfigStats.find(key);
297 if (statsIt == mConfigStats.end()) {
298 return;
299 }
300 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
301 if (size > metricsDimensionMap[id]) {
302 metricsDimensionMap[id] = size;
303 }
304}
305
Yangster-mac94e197c2018-01-02 16:03:03 -0800306void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800307 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800308
309 auto statsIt = mConfigStats.find(key);
310 if (statsIt == mConfigStats.end()) {
311 return;
312 }
313 statsIt->second->matcher_stats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800314}
315
Yangster-mac94e197c2018-01-02 16:03:03 -0800316void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800317 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800318 auto statsIt = mConfigStats.find(key);
319 if (statsIt == mConfigStats.end()) {
320 return;
321 }
322 statsIt->second->alert_stats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800323}
324
Bookatz1d0136d2017-12-01 11:13:32 -0800325void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
326 lock_guard<std::mutex> lock(mLock);
327 mAnomalyAlarmRegisteredStats++;
328}
329
Yangster-mac932ecec2018-02-01 10:23:52 -0800330void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
331 lock_guard<std::mutex> lock(mLock);
332 mPeriodicAlarmRegisteredStats++;
333}
334
Chenjie Yub038b702017-12-18 15:15:34 -0800335void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
336 lock_guard<std::mutex> lock(mLock);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700337 mPulledAtomStats[pullAtomId].minPullIntervalSec =
338 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
Chenjie Yub038b702017-12-18 15:15:34 -0800339}
340
341void StatsdStats::notePull(int pullAtomId) {
342 lock_guard<std::mutex> lock(mLock);
343 mPulledAtomStats[pullAtomId].totalPull++;
344}
345
346void StatsdStats::notePullFromCache(int pullAtomId) {
347 lock_guard<std::mutex> lock(mLock);
348 mPulledAtomStats[pullAtomId].totalPullFromCache++;
349}
350
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700351void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
352 lock_guard<std::mutex> lock(mLock);
353 auto& pullStats = mPulledAtomStats[pullAtomId];
354 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
355 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
356 (pullStats.numPullTime + 1);
357 pullStats.numPullTime += 1;
358}
359
360void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
361 lock_guard<std::mutex> lock(mLock);
362 auto& pullStats = mPulledAtomStats[pullAtomId];
363 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
364 pullStats.avgPullDelayNs = (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
365 (pullStats.numPullDelay + 1);
366 pullStats.numPullDelay += 1;
367}
368
Yao Chenb3561512017-11-21 18:07:17 -0800369void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
370 lock_guard<std::mutex> lock(mLock);
371
Yao Chenb3561512017-11-21 18:07:17 -0800372 if (atomId > android::util::kMaxPushedAtomId) {
373 ALOGW("not interested in atom %d", atomId);
374 return;
375 }
376
377 mPushedAtomStats[atomId]++;
378}
379
Yangster-mac892f3d32018-05-02 14:16:48 -0700380void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
381 lock_guard<std::mutex> lock(mLock);
382
383 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
384 mSystemServerRestartSec.pop_front();
385 }
386 mSystemServerRestartSec.push_back(timeSec);
387}
388
Yao Chenb3561512017-11-21 18:07:17 -0800389void StatsdStats::reset() {
390 lock_guard<std::mutex> lock(mLock);
391 resetInternalLocked();
392}
393
394void StatsdStats::resetInternalLocked() {
395 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800396 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800397 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800398 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800399 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800400 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700401 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700402 mLogLossStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800403 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800404 config.second->broadcast_sent_time_sec.clear();
405 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700406 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700407 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700408 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800409 config.second->matcher_stats.clear();
410 config.second->condition_stats.clear();
411 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700412 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800413 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800414 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700415 for (auto& pullStats : mPulledAtomStats) {
416 pullStats.second.totalPull = 0;
417 pullStats.second.totalPullFromCache = 0;
418 pullStats.second.avgPullTimeNs = 0;
419 pullStats.second.maxPullTimeNs = 0;
420 pullStats.second.numPullTime = 0;
421 pullStats.second.avgPullDelayNs = 0;
422 pullStats.second.maxPullDelayNs = 0;
423 pullStats.second.numPullDelay = 0;
424 }
Yao Chenb3561512017-11-21 18:07:17 -0800425}
426
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700427string buildTimeString(int64_t timeSec) {
428 time_t t = timeSec;
429 struct tm* tm = localtime(&t);
430 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700431 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700432 return string(timeBuffer);
433}
434
Yao Chena80e5c02018-09-04 13:55:29 -0700435void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800436 lock_guard<std::mutex> lock(mLock);
437 time_t t = mStartTimeSec;
438 struct tm* tm = localtime(&t);
439 char timeBuffer[80];
440 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700441 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
442 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800443 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700444 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700445 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800446 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800447 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700448 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700449 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
450 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800451
Yao Chen20e9e622018-02-28 11:18:51 -0800452 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700453 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800454 }
455
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700456 auto dropTimePtr = configStats->data_drop_time_sec.begin();
457 auto dropBytesPtr = configStats->data_drop_bytes.begin();
458 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
459 i++, dropTimePtr++, dropBytesPtr++) {
460 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
461 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800462 }
463 }
Yao Chena80e5c02018-09-04 13:55:29 -0700464 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800465 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800466 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700467 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800468 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
469 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800470 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
471 configStats->deletion_time_sec, configStats->metric_count,
472 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
473 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700474 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700475 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700476 annotation.second);
477 }
478
Yao Chen20e9e622018-02-28 11:18:51 -0800479 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700480 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
481 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800482 }
483
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700484 auto dropTimePtr = configStats->data_drop_time_sec.begin();
485 auto dropBytesPtr = configStats->data_drop_bytes.begin();
486 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
487 i++, dropTimePtr++, dropBytesPtr++) {
488 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
489 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
490 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800491 }
492
Yangster-mace68f3a52018-04-04 00:01:43 -0700493 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700494 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700495 buildTimeString(dump.first).c_str(), (long long)dump.first,
496 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800497 }
498
Yao Chen20e9e622018-02-28 11:18:51 -0800499 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700500 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800501 }
Yao Chen20e9e622018-02-28 11:18:51 -0800502
503 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700504 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800505 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800506 }
Yao Chen20e9e622018-02-28 11:18:51 -0800507
508 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700509 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800510 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800511 }
Yao Chen20e9e622018-02-28 11:18:51 -0800512
513 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700514 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800515 }
516 }
Yao Chena80e5c02018-09-04 13:55:29 -0700517 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700518 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700519 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800520 const size_t atomCounts = mPushedAtomStats.size();
521 for (size_t i = 2; i < atomCounts; i++) {
522 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700523 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800524 }
525 }
526
Yao Chena80e5c02018-09-04 13:55:29 -0700527 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800528 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700529 dprintf(out,
530 "Atom %d->(total pull)%ld, (pull from cache)%ld, (min pull interval)%ld, (average "
531 "pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay nanos)%lld, "
532 "(max pull delay nanos)%lld\n",
533 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
534 (long)pair.second.minPullIntervalSec, (long long)pair.second.avgPullTimeNs,
535 (long long)pair.second.maxPullTimeNs, (long long)pair.second.avgPullDelayNs,
536 (long long)pair.second.maxPullDelayNs);
Yao Chenf5acabe2018-01-17 14:10:34 -0800537 }
538
539 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700540 dprintf(out, "********AnomalyAlarmStats stats***********\n");
541 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800542 }
543
Yangster-mac932ecec2018-02-01 10:23:52 -0800544 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700545 dprintf(out, "********SubscriberAlarmStats stats***********\n");
546 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800547 }
548
Yao Chena80e5c02018-09-04 13:55:29 -0700549 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700550 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
551 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800552
Yangster-mac892f3d32018-05-02 14:16:48 -0700553 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700554 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
555 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700556 }
557
Yao Chen0f5d6612018-08-22 14:11:51 -0700558 for (const auto& loss : mLogLossStats) {
Yao Chen39b67992018-11-08 15:32:17 -0800559 dprintf(out, "Log loss: %lld (wall clock sec) - %d (count) %d (last error)\n",
560 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError);
Yangster-macb8382a12018-04-04 10:39:12 -0700561 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800562}
563
Yao Chen20e9e622018-02-28 11:18:51 -0800564void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800565 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800566 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
567 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
568 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
569 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700570 if (configStats.reset_time_sec != 0) {
571 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
572 }
Yao Chen20e9e622018-02-28 11:18:51 -0800573 if (configStats.deletion_time_sec != 0) {
574 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
575 configStats.deletion_time_sec);
576 }
577 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
578 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
579 configStats.condition_count);
580 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
581 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
582 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
583
584 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
585 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
586 broadcast);
587 }
588
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700589 for (const auto& drop_time : configStats.data_drop_time_sec) {
590 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
591 drop_time);
592 }
593
594 for (const auto& drop_bytes : configStats.data_drop_bytes) {
595 proto->write(
596 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
597 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800598 }
599
Yangster-mace68f3a52018-04-04 00:01:43 -0700600 for (const auto& dump : configStats.dump_report_stats) {
601 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
602 FIELD_COUNT_REPEATED,
603 dump.first);
604 }
605
606 for (const auto& dump : configStats.dump_report_stats) {
607 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
608 FIELD_COUNT_REPEATED,
609 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800610 }
611
David Chenfaa1af52018-03-30 15:14:04 -0700612 for (const auto& annotation : configStats.annotations) {
613 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
614 FIELD_ID_CONFIG_STATS_ANNOTATION);
615 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
616 (long long)annotation.first);
617 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
618 proto->end(token);
619 }
620
Yao Chen20e9e622018-02-28 11:18:51 -0800621 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800622 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800623 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
624 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
625 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
626 proto->end(tmpToken);
627 }
628
629 for (const auto& pair : configStats.condition_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800630 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800631 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
632 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
633 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
634 proto->end(tmpToken);
635 }
636
637 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800638 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800639 FIELD_ID_CONFIG_STATS_METRIC_STATS);
640 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
641 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
642 proto->end(tmpToken);
643 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700644 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
645 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
646 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
647 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
648 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
649 proto->end(tmpToken);
650 }
Yao Chen20e9e622018-02-28 11:18:51 -0800651
652 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800653 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800654 FIELD_ID_CONFIG_STATS_ALERT_STATS);
655 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
656 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
657 proto->end(tmpToken);
658 }
659
660 proto->end(token);
661}
662
Yao Chen69f1baf2017-11-27 17:25:36 -0800663void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800664 lock_guard<std::mutex> lock(mLock);
665
Yao Chenb3561512017-11-21 18:07:17 -0800666 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800667 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800668 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800669
Yao Chenb3561512017-11-21 18:07:17 -0800670 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800671 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800672 }
673
674 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800675 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800676 }
677
Yao Chenb3561512017-11-21 18:07:17 -0800678 const size_t atomCounts = mPushedAtomStats.size();
679 for (size_t i = 2; i < atomCounts; i++) {
680 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800681 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800682 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
683 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
684 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
685 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800686 }
687 }
688
Chenjie Yub038b702017-12-18 15:15:34 -0800689 for (const auto& pair : mPulledAtomStats) {
690 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800691 }
692
Bookatz1d0136d2017-12-01 11:13:32 -0800693 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800694 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800695 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
696 mAnomalyAlarmRegisteredStats);
697 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800698 }
699
Yangster-mac932ecec2018-02-01 10:23:52 -0800700 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700701 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
702 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800703 mPeriodicAlarmRegisteredStats);
704 proto.end(token);
705 }
706
Yi Jin5ee07872018-03-05 18:18:27 -0800707 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800708 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
709 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800710 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700711 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800712 proto.end(uidMapToken);
David Chenc136f452017-11-27 11:52:26 -0800713
Yao Chen0f5d6612018-08-22 14:11:51 -0700714 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800715 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800716 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800717 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
718 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
719 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen884c8c12018-01-26 10:36:25 -0800720 proto.end(token);
721 }
722
Yangster-mac892f3d32018-05-02 14:16:48 -0700723 for (const auto& restart : mSystemServerRestartSec) {
724 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
725 restart);
726 }
727
Yao Chenb3561512017-11-21 18:07:17 -0800728 output->clear();
729 size_t bufferSize = proto.size();
730 output->resize(bufferSize);
731
732 size_t pos = 0;
733 auto it = proto.data();
734 while (it.readBuffer() != NULL) {
735 size_t toRead = it.currentToRead();
736 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
737 pos += toRead;
738 it.rp()->move(toRead);
739 }
740
741 if (reset) {
742 resetInternalLocked();
743 }
744
745 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800746}
747
748} // namespace statsd
749} // namespace os
yro255f72e2018-02-26 15:15:17 -0800750} // namespace android