blob: 3e5e82f7f4df55c25fb7a1070b3061f37cda0b23 [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 Chenc136f45a2017-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}},
Olivier Gaillardc840ed92018-11-30 13:06:18 +0000104 {android::util::LOOPER_STATS, {1500, 2500}},
Chenjie Yuc5875052018-03-09 10:13:11 -0800105 {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
106};
107
Yao Chenb3561512017-11-21 18:07:17 -0800108StatsdStats::StatsdStats() {
109 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -0800110 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800111}
112
113StatsdStats& StatsdStats::getInstance() {
114 static StatsdStats statsInstance;
115 return statsInstance;
116}
117
Yao Chen20e9e622018-02-28 11:18:51 -0800118void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
Yao Chenf6723df2018-01-08 15:11:58 -0800119 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
120 if (mIceBox.size() == kMaxIceBoxSize) {
121 mIceBox.pop_front();
122 }
123 mIceBox.push_back(stats);
124}
125
David Chenfaa1af52018-03-30 15:14:04 -0700126void StatsdStats::noteConfigReceived(
127 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
128 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
129 bool isValid) {
Yao Chenb3561512017-11-21 18:07:17 -0800130 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -0800131 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800132
133 // If there is an existing config for the same key, icebox the old config.
134 noteConfigRemovedInternalLocked(key);
135
Yao Chen20e9e622018-02-28 11:18:51 -0800136 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
137 configStats->uid = key.GetUid();
138 configStats->id = key.GetId();
139 configStats->creation_time_sec = nowTimeSec;
140 configStats->metric_count = metricsCount;
141 configStats->condition_count = conditionsCount;
142 configStats->matcher_count = matchersCount;
143 configStats->alert_count = alertsCount;
144 configStats->is_valid = isValid;
David Chenfaa1af52018-03-30 15:14:04 -0700145 for (auto& v : annotations) {
146 configStats->annotations.emplace_back(v);
147 }
Yao Chenb3561512017-11-21 18:07:17 -0800148
149 if (isValid) {
150 mConfigStats[key] = configStats;
151 } else {
Yao Chen20e9e622018-02-28 11:18:51 -0800152 configStats->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800153 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800154 }
155}
156
157void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
158 auto it = mConfigStats.find(key);
159 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800160 int32_t nowTimeSec = getWallClockSec();
Yao Chen20e9e622018-02-28 11:18:51 -0800161 it->second->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800162 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800163 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800164 }
165}
166
167void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
168 lock_guard<std::mutex> lock(mLock);
169 noteConfigRemovedInternalLocked(key);
170}
171
Yangster-macb142cc82018-03-30 15:22:08 -0700172void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
173 auto it = mConfigStats.find(key);
174 if (it != mConfigStats.end()) {
175 it->second->reset_time_sec = getWallClockSec();
176 }
177}
178
179void StatsdStats::noteConfigReset(const ConfigKey& key) {
180 lock_guard<std::mutex> lock(mLock);
181 noteConfigResetInternalLocked(key);
182}
183
Yao Chen39b67992018-11-08 15:32:17 -0800184void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError) {
Yao Chen163d2602018-04-10 10:39:53 -0700185 lock_guard<std::mutex> lock(mLock);
Yao Chen0f5d6612018-08-22 14:11:51 -0700186 if (mLogLossStats.size() == kMaxLoggerErrors) {
187 mLogLossStats.pop_front();
Yao Chen163d2602018-04-10 10:39:53 -0700188 }
Yao Chen39b67992018-11-08 15:32:17 -0800189 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError);
Yao Chen163d2602018-04-10 10:39:53 -0700190}
191
Yao Chenb3561512017-11-21 18:07:17 -0800192void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800193 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800194}
195
196void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800197 lock_guard<std::mutex> lock(mLock);
198 auto it = mConfigStats.find(key);
199 if (it == mConfigStats.end()) {
200 ALOGE("Config key %s not found!", key.ToString().c_str());
201 return;
202 }
Yao Chen20e9e622018-02-28 11:18:51 -0800203 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
204 it->second->broadcast_sent_time_sec.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800205 }
Yao Chen20e9e622018-02-28 11:18:51 -0800206 it->second->broadcast_sent_time_sec.push_back(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800207}
208
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700209void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
210 noteDataDropped(key, totalBytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800211}
212
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700213void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800214 lock_guard<std::mutex> lock(mLock);
215 auto it = mConfigStats.find(key);
216 if (it == mConfigStats.end()) {
217 ALOGE("Config key %s not found!", key.ToString().c_str());
218 return;
219 }
Yao Chen20e9e622018-02-28 11:18:51 -0800220 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
221 it->second->data_drop_time_sec.pop_front();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700222 it->second->data_drop_bytes.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800223 }
Yao Chen20e9e622018-02-28 11:18:51 -0800224 it->second->data_drop_time_sec.push_back(timeSec);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700225 it->second->data_drop_bytes.push_back(totalBytes);
Yao Chenb3561512017-11-21 18:07:17 -0800226}
227
Yangster-mace68f3a52018-04-04 00:01:43 -0700228void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
229 noteMetricsReportSent(key, num_bytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800230}
231
Yangster-mace68f3a52018-04-04 00:01:43 -0700232void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
233 int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800234 lock_guard<std::mutex> lock(mLock);
235 auto it = mConfigStats.find(key);
236 if (it == mConfigStats.end()) {
237 ALOGE("Config key %s not found!", key.ToString().c_str());
238 return;
239 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700240 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
241 it->second->dump_report_stats.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800242 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700243 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
Yao Chen69f1baf2017-11-27 17:25:36 -0800244}
245
David Chenbd125272018-04-04 19:02:50 -0700246void StatsdStats::noteUidMapDropped(int deltas) {
David Chenc136f45a2017-11-27 11:52:26 -0800247 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800248 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
David Chenc136f45a2017-11-27 11:52:26 -0800249}
250
David Chenbd125272018-04-04 19:02:50 -0700251void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f45a2017-11-27 11:52:26 -0800252 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700253 mUidMapStats.deleted_apps++;
David Chenc136f45a2017-11-27 11:52:26 -0800254}
255
256void StatsdStats::setUidMapChanges(int changes) {
257 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800258 mUidMapStats.changes = changes;
David Chenc136f45a2017-11-27 11:52:26 -0800259}
260
261void StatsdStats::setCurrentUidMapMemory(int bytes) {
262 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800263 mUidMapStats.bytes_used = bytes;
David Chenc136f45a2017-11-27 11:52:26 -0800264}
265
Yangster-mac94e197c2018-01-02 16:03:03 -0800266void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800267 lock_guard<std::mutex> lock(mLock);
268 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800269 auto statsIt = mConfigStats.find(key);
270 if (statsIt == mConfigStats.end()) {
271 return;
272 }
273
274 auto& conditionSizeMap = statsIt->second->condition_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800275 if (size > conditionSizeMap[id]) {
276 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800277 }
278}
279
Yangster-mac94e197c2018-01-02 16:03:03 -0800280void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800281 lock_guard<std::mutex> lock(mLock);
282 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800283 auto statsIt = mConfigStats.find(key);
284 if (statsIt == mConfigStats.end()) {
285 return;
286 }
287 auto& metricsDimensionMap = statsIt->second->metric_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800288 if (size > metricsDimensionMap[id]) {
289 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800290 }
291}
292
Yangster-mac306ccc22018-03-24 15:03:40 -0700293void StatsdStats::noteMetricDimensionInConditionSize(
294 const ConfigKey& key, const int64_t& id, int size) {
295 lock_guard<std::mutex> lock(mLock);
296 // if name doesn't exist before, it will create the key with count 0.
297 auto statsIt = mConfigStats.find(key);
298 if (statsIt == mConfigStats.end()) {
299 return;
300 }
301 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
302 if (size > metricsDimensionMap[id]) {
303 metricsDimensionMap[id] = size;
304 }
305}
306
Yangster-mac94e197c2018-01-02 16:03:03 -0800307void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800308 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800309
310 auto statsIt = mConfigStats.find(key);
311 if (statsIt == mConfigStats.end()) {
312 return;
313 }
314 statsIt->second->matcher_stats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800315}
316
Yangster-mac94e197c2018-01-02 16:03:03 -0800317void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800318 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800319 auto statsIt = mConfigStats.find(key);
320 if (statsIt == mConfigStats.end()) {
321 return;
322 }
323 statsIt->second->alert_stats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800324}
325
Bookatz1d0136d2017-12-01 11:13:32 -0800326void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
327 lock_guard<std::mutex> lock(mLock);
328 mAnomalyAlarmRegisteredStats++;
329}
330
Yangster-mac932ecec2018-02-01 10:23:52 -0800331void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
332 lock_guard<std::mutex> lock(mLock);
333 mPeriodicAlarmRegisteredStats++;
334}
335
Chenjie Yub038b702017-12-18 15:15:34 -0800336void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
337 lock_guard<std::mutex> lock(mLock);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700338 mPulledAtomStats[pullAtomId].minPullIntervalSec =
339 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
Chenjie Yub038b702017-12-18 15:15:34 -0800340}
341
342void StatsdStats::notePull(int pullAtomId) {
343 lock_guard<std::mutex> lock(mLock);
344 mPulledAtomStats[pullAtomId].totalPull++;
345}
346
347void StatsdStats::notePullFromCache(int pullAtomId) {
348 lock_guard<std::mutex> lock(mLock);
349 mPulledAtomStats[pullAtomId].totalPullFromCache++;
350}
351
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700352void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
353 lock_guard<std::mutex> lock(mLock);
354 auto& pullStats = mPulledAtomStats[pullAtomId];
355 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
356 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
357 (pullStats.numPullTime + 1);
358 pullStats.numPullTime += 1;
359}
360
361void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
362 lock_guard<std::mutex> lock(mLock);
363 auto& pullStats = mPulledAtomStats[pullAtomId];
364 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700365 pullStats.avgPullDelayNs =
366 (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
367 (pullStats.numPullDelay + 1);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700368 pullStats.numPullDelay += 1;
369}
370
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700371void StatsdStats::notePullDataError(int pullAtomId) {
372 lock_guard<std::mutex> lock(mLock);
373 mPulledAtomStats[pullAtomId].dataError++;
374}
375
Yao Chenb3561512017-11-21 18:07:17 -0800376void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
377 lock_guard<std::mutex> lock(mLock);
378
Yao Chenb3561512017-11-21 18:07:17 -0800379 if (atomId > android::util::kMaxPushedAtomId) {
380 ALOGW("not interested in atom %d", atomId);
381 return;
382 }
383
384 mPushedAtomStats[atomId]++;
385}
386
Yangster-mac892f3d32018-05-02 14:16:48 -0700387void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
388 lock_guard<std::mutex> lock(mLock);
389
390 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
391 mSystemServerRestartSec.pop_front();
392 }
393 mSystemServerRestartSec.push_back(timeSec);
394}
395
Yao Chenb3561512017-11-21 18:07:17 -0800396void StatsdStats::reset() {
397 lock_guard<std::mutex> lock(mLock);
398 resetInternalLocked();
399}
400
401void StatsdStats::resetInternalLocked() {
402 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800403 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800404 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800405 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800406 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800407 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700408 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700409 mLogLossStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800410 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800411 config.second->broadcast_sent_time_sec.clear();
412 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700413 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700414 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700415 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800416 config.second->matcher_stats.clear();
417 config.second->condition_stats.clear();
418 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700419 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800420 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800421 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700422 for (auto& pullStats : mPulledAtomStats) {
423 pullStats.second.totalPull = 0;
424 pullStats.second.totalPullFromCache = 0;
425 pullStats.second.avgPullTimeNs = 0;
426 pullStats.second.maxPullTimeNs = 0;
427 pullStats.second.numPullTime = 0;
428 pullStats.second.avgPullDelayNs = 0;
429 pullStats.second.maxPullDelayNs = 0;
430 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700431 pullStats.second.dataError = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700432 }
Yao Chenb3561512017-11-21 18:07:17 -0800433}
434
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700435string buildTimeString(int64_t timeSec) {
436 time_t t = timeSec;
437 struct tm* tm = localtime(&t);
438 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700439 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700440 return string(timeBuffer);
441}
442
Yao Chena80e5c02018-09-04 13:55:29 -0700443void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800444 lock_guard<std::mutex> lock(mLock);
445 time_t t = mStartTimeSec;
446 struct tm* tm = localtime(&t);
447 char timeBuffer[80];
448 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700449 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
450 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800451 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700452 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700453 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800454 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800455 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700456 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700457 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
458 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800459
Yao Chen20e9e622018-02-28 11:18:51 -0800460 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700461 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800462 }
463
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700464 auto dropTimePtr = configStats->data_drop_time_sec.begin();
465 auto dropBytesPtr = configStats->data_drop_bytes.begin();
466 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
467 i++, dropTimePtr++, dropBytesPtr++) {
468 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
469 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800470 }
471 }
Yao Chena80e5c02018-09-04 13:55:29 -0700472 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800473 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800474 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700475 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800476 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
477 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800478 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
479 configStats->deletion_time_sec, configStats->metric_count,
480 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
481 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700482 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700483 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700484 annotation.second);
485 }
486
Yao Chen20e9e622018-02-28 11:18:51 -0800487 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700488 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
489 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800490 }
491
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700492 auto dropTimePtr = configStats->data_drop_time_sec.begin();
493 auto dropBytesPtr = configStats->data_drop_bytes.begin();
494 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
495 i++, dropTimePtr++, dropBytesPtr++) {
496 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
497 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
498 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800499 }
500
Yangster-mace68f3a52018-04-04 00:01:43 -0700501 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700502 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700503 buildTimeString(dump.first).c_str(), (long long)dump.first,
504 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800505 }
506
Yao Chen20e9e622018-02-28 11:18:51 -0800507 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700508 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800509 }
Yao Chen20e9e622018-02-28 11:18:51 -0800510
511 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700512 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800513 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800514 }
Yao Chen20e9e622018-02-28 11:18:51 -0800515
516 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700517 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800518 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800519 }
Yao Chen20e9e622018-02-28 11:18:51 -0800520
521 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700522 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800523 }
524 }
Yao Chena80e5c02018-09-04 13:55:29 -0700525 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700526 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700527 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800528 const size_t atomCounts = mPushedAtomStats.size();
529 for (size_t i = 2; i < atomCounts; i++) {
530 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700531 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800532 }
533 }
534
Yao Chena80e5c02018-09-04 13:55:29 -0700535 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800536 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700537 dprintf(out,
538 "Atom %d->(total pull)%ld, (pull from cache)%ld, (min pull interval)%ld, (average "
539 "pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay nanos)%lld, "
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700540 "(max pull delay nanos)%lld, (data error)%ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700541 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
542 (long)pair.second.minPullIntervalSec, (long long)pair.second.avgPullTimeNs,
543 (long long)pair.second.maxPullTimeNs, (long long)pair.second.avgPullDelayNs,
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700544 (long long)pair.second.maxPullDelayNs, pair.second.dataError);
Yao Chenf5acabe2018-01-17 14:10:34 -0800545 }
546
547 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700548 dprintf(out, "********AnomalyAlarmStats stats***********\n");
549 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800550 }
551
Yangster-mac932ecec2018-02-01 10:23:52 -0800552 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700553 dprintf(out, "********SubscriberAlarmStats stats***********\n");
554 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800555 }
556
Yao Chena80e5c02018-09-04 13:55:29 -0700557 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700558 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
559 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800560
Yangster-mac892f3d32018-05-02 14:16:48 -0700561 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700562 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
563 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700564 }
565
Yao Chen0f5d6612018-08-22 14:11:51 -0700566 for (const auto& loss : mLogLossStats) {
Yao Chen39b67992018-11-08 15:32:17 -0800567 dprintf(out, "Log loss: %lld (wall clock sec) - %d (count) %d (last error)\n",
568 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError);
Yangster-macb8382a12018-04-04 10:39:12 -0700569 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800570}
571
Yao Chen20e9e622018-02-28 11:18:51 -0800572void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800573 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800574 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
575 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
576 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
577 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700578 if (configStats.reset_time_sec != 0) {
579 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
580 }
Yao Chen20e9e622018-02-28 11:18:51 -0800581 if (configStats.deletion_time_sec != 0) {
582 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
583 configStats.deletion_time_sec);
584 }
585 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
586 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
587 configStats.condition_count);
588 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
589 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
590 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
591
592 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
593 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
594 broadcast);
595 }
596
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700597 for (const auto& drop_time : configStats.data_drop_time_sec) {
598 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
599 drop_time);
600 }
601
602 for (const auto& drop_bytes : configStats.data_drop_bytes) {
603 proto->write(
604 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
605 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800606 }
607
Yangster-mace68f3a52018-04-04 00:01:43 -0700608 for (const auto& dump : configStats.dump_report_stats) {
609 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
610 FIELD_COUNT_REPEATED,
611 dump.first);
612 }
613
614 for (const auto& dump : configStats.dump_report_stats) {
615 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
616 FIELD_COUNT_REPEATED,
617 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800618 }
619
David Chenfaa1af52018-03-30 15:14:04 -0700620 for (const auto& annotation : configStats.annotations) {
621 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
622 FIELD_ID_CONFIG_STATS_ANNOTATION);
623 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
624 (long long)annotation.first);
625 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
626 proto->end(token);
627 }
628
Yao Chen20e9e622018-02-28 11:18:51 -0800629 for (const auto& pair : configStats.matcher_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_MATCHER_STATS);
632 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
633 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
634 proto->end(tmpToken);
635 }
636
637 for (const auto& pair : configStats.condition_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_CONDITION_STATS);
640 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
641 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
642 proto->end(tmpToken);
643 }
644
645 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800646 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800647 FIELD_ID_CONFIG_STATS_METRIC_STATS);
648 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
649 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
650 proto->end(tmpToken);
651 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700652 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
653 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
654 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
655 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
656 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
657 proto->end(tmpToken);
658 }
Yao Chen20e9e622018-02-28 11:18:51 -0800659
660 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800661 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800662 FIELD_ID_CONFIG_STATS_ALERT_STATS);
663 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
664 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
665 proto->end(tmpToken);
666 }
667
668 proto->end(token);
669}
670
Yao Chen69f1baf2017-11-27 17:25:36 -0800671void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800672 lock_guard<std::mutex> lock(mLock);
673
Yao Chenb3561512017-11-21 18:07:17 -0800674 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800675 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800676 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800677
Yao Chenb3561512017-11-21 18:07:17 -0800678 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800679 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800680 }
681
682 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800683 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800684 }
685
Yao Chenb3561512017-11-21 18:07:17 -0800686 const size_t atomCounts = mPushedAtomStats.size();
687 for (size_t i = 2; i < atomCounts; i++) {
688 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800689 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800690 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
691 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
692 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
693 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800694 }
695 }
696
Chenjie Yub038b702017-12-18 15:15:34 -0800697 for (const auto& pair : mPulledAtomStats) {
698 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800699 }
700
Bookatz1d0136d2017-12-01 11:13:32 -0800701 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800702 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800703 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
704 mAnomalyAlarmRegisteredStats);
705 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800706 }
707
Yangster-mac932ecec2018-02-01 10:23:52 -0800708 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700709 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
710 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800711 mPeriodicAlarmRegisteredStats);
712 proto.end(token);
713 }
714
Yi Jin5ee07872018-03-05 18:18:27 -0800715 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800716 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
717 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800718 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700719 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800720 proto.end(uidMapToken);
David Chenc136f45a2017-11-27 11:52:26 -0800721
Yao Chen0f5d6612018-08-22 14:11:51 -0700722 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800723 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800724 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800725 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
726 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
727 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen884c8c12018-01-26 10:36:25 -0800728 proto.end(token);
729 }
730
Yangster-mac892f3d32018-05-02 14:16:48 -0700731 for (const auto& restart : mSystemServerRestartSec) {
732 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
733 restart);
734 }
735
Yao Chenb3561512017-11-21 18:07:17 -0800736 output->clear();
737 size_t bufferSize = proto.size();
738 output->resize(bufferSize);
739
740 size_t pos = 0;
741 auto it = proto.data();
742 while (it.readBuffer() != NULL) {
743 size_t toRead = it.currentToRead();
744 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
745 pos += toRead;
746 it.rp()->move(toRead);
747 }
748
749 if (reset) {
750 resetInternalLocked();
751 }
752
753 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800754}
755
756} // namespace statsd
757} // namespace os
yro255f72e2018-02-26 15:15:17 -0800758} // namespace android