blob: f4d0144e88789ae24ef498abf9fafadb07a9d8ff [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}},
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 Chenc136f452017-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 Chenc136f452017-11-27 11:52:26 -0800249}
250
David Chenbd125272018-04-04 19:02:50 -0700251void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f452017-11-27 11:52:26 -0800252 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700253 mUidMapStats.deleted_apps++;
David Chenc136f452017-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 Chenc136f452017-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 Chenc136f452017-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
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800376void StatsdStats::notePullTimeout(int pullAtomId) {
377 lock_guard<std::mutex> lock(mLock);
378 mPulledAtomStats[pullAtomId].pullTimeout++;
379}
380
381void StatsdStats::notePullExceedMaxDelay(int pullAtomId) {
382 lock_guard<std::mutex> lock(mLock);
383 mPulledAtomStats[pullAtomId].pullExceedMaxDelay++;
384}
385
Yao Chenb3561512017-11-21 18:07:17 -0800386void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
387 lock_guard<std::mutex> lock(mLock);
388
Yao Chenb3561512017-11-21 18:07:17 -0800389 if (atomId > android::util::kMaxPushedAtomId) {
390 ALOGW("not interested in atom %d", atomId);
391 return;
392 }
393
394 mPushedAtomStats[atomId]++;
395}
396
Yangster-mac892f3d32018-05-02 14:16:48 -0700397void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
398 lock_guard<std::mutex> lock(mLock);
399
400 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
401 mSystemServerRestartSec.pop_front();
402 }
403 mSystemServerRestartSec.push_back(timeSec);
404}
405
Yao Chenb3561512017-11-21 18:07:17 -0800406void StatsdStats::reset() {
407 lock_guard<std::mutex> lock(mLock);
408 resetInternalLocked();
409}
410
411void StatsdStats::resetInternalLocked() {
412 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800413 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800414 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800415 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800416 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800417 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700418 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700419 mLogLossStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800420 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800421 config.second->broadcast_sent_time_sec.clear();
422 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700423 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700424 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700425 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800426 config.second->matcher_stats.clear();
427 config.second->condition_stats.clear();
428 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700429 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800430 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800431 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700432 for (auto& pullStats : mPulledAtomStats) {
433 pullStats.second.totalPull = 0;
434 pullStats.second.totalPullFromCache = 0;
435 pullStats.second.avgPullTimeNs = 0;
436 pullStats.second.maxPullTimeNs = 0;
437 pullStats.second.numPullTime = 0;
438 pullStats.second.avgPullDelayNs = 0;
439 pullStats.second.maxPullDelayNs = 0;
440 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700441 pullStats.second.dataError = 0;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800442 pullStats.second.pullTimeout = 0;
443 pullStats.second.pullExceedMaxDelay = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700444 }
Yao Chenb3561512017-11-21 18:07:17 -0800445}
446
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700447string buildTimeString(int64_t timeSec) {
448 time_t t = timeSec;
449 struct tm* tm = localtime(&t);
450 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700451 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700452 return string(timeBuffer);
453}
454
Yao Chena80e5c02018-09-04 13:55:29 -0700455void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800456 lock_guard<std::mutex> lock(mLock);
457 time_t t = mStartTimeSec;
458 struct tm* tm = localtime(&t);
459 char timeBuffer[80];
460 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700461 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
462 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800463 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700464 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700465 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800466 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800467 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700468 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700469 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
470 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800471
Yao Chen20e9e622018-02-28 11:18:51 -0800472 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700473 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800474 }
475
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700476 auto dropTimePtr = configStats->data_drop_time_sec.begin();
477 auto dropBytesPtr = configStats->data_drop_bytes.begin();
478 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
479 i++, dropTimePtr++, dropBytesPtr++) {
480 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
481 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800482 }
483 }
Yao Chena80e5c02018-09-04 13:55:29 -0700484 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800485 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800486 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700487 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800488 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
489 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800490 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
491 configStats->deletion_time_sec, configStats->metric_count,
492 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
493 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700494 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700495 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700496 annotation.second);
497 }
498
Yao Chen20e9e622018-02-28 11:18:51 -0800499 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700500 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
501 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800502 }
503
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700504 auto dropTimePtr = configStats->data_drop_time_sec.begin();
505 auto dropBytesPtr = configStats->data_drop_bytes.begin();
506 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
507 i++, dropTimePtr++, dropBytesPtr++) {
508 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
509 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
510 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800511 }
512
Yangster-mace68f3a52018-04-04 00:01:43 -0700513 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700514 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700515 buildTimeString(dump.first).c_str(), (long long)dump.first,
516 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800517 }
518
Yao Chen20e9e622018-02-28 11:18:51 -0800519 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700520 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800521 }
Yao Chen20e9e622018-02-28 11:18:51 -0800522
523 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700524 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800525 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800526 }
Yao Chen20e9e622018-02-28 11:18:51 -0800527
528 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700529 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800530 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800531 }
Yao Chen20e9e622018-02-28 11:18:51 -0800532
533 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700534 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800535 }
536 }
Yao Chena80e5c02018-09-04 13:55:29 -0700537 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700538 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700539 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800540 const size_t atomCounts = mPushedAtomStats.size();
541 for (size_t i = 2; i < atomCounts; i++) {
542 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700543 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800544 }
545 }
546
Yao Chena80e5c02018-09-04 13:55:29 -0700547 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800548 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700549 dprintf(out,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800550 "Atom %d->(total pull)%ld, (pull from cache)%ld, (min pull interval)%ld \n"
551 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
552 "nanos)%lld, "
553 " (max pull delay nanos)%lld, (data error)%ld\n"
554 " (pull timeout)%ld, (pull exceed max delay)%ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700555 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
556 (long)pair.second.minPullIntervalSec, (long long)pair.second.avgPullTimeNs,
557 (long long)pair.second.maxPullTimeNs, (long long)pair.second.avgPullDelayNs,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800558 (long long)pair.second.maxPullDelayNs, pair.second.dataError,
559 pair.second.pullTimeout, pair.second.pullExceedMaxDelay);
Yao Chenf5acabe2018-01-17 14:10:34 -0800560 }
561
562 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700563 dprintf(out, "********AnomalyAlarmStats stats***********\n");
564 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800565 }
566
Yangster-mac932ecec2018-02-01 10:23:52 -0800567 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700568 dprintf(out, "********SubscriberAlarmStats stats***********\n");
569 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800570 }
571
Yao Chena80e5c02018-09-04 13:55:29 -0700572 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700573 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
574 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800575
Yangster-mac892f3d32018-05-02 14:16:48 -0700576 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700577 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
578 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700579 }
580
Yao Chen0f5d6612018-08-22 14:11:51 -0700581 for (const auto& loss : mLogLossStats) {
Yao Chen39b67992018-11-08 15:32:17 -0800582 dprintf(out, "Log loss: %lld (wall clock sec) - %d (count) %d (last error)\n",
583 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError);
Yangster-macb8382a12018-04-04 10:39:12 -0700584 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800585}
586
Yao Chen20e9e622018-02-28 11:18:51 -0800587void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800588 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800589 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
590 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
591 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
592 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700593 if (configStats.reset_time_sec != 0) {
594 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
595 }
Yao Chen20e9e622018-02-28 11:18:51 -0800596 if (configStats.deletion_time_sec != 0) {
597 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
598 configStats.deletion_time_sec);
599 }
600 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
601 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
602 configStats.condition_count);
603 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
604 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
605 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
606
607 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
608 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
609 broadcast);
610 }
611
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700612 for (const auto& drop_time : configStats.data_drop_time_sec) {
613 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
614 drop_time);
615 }
616
617 for (const auto& drop_bytes : configStats.data_drop_bytes) {
618 proto->write(
619 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
620 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800621 }
622
Yangster-mace68f3a52018-04-04 00:01:43 -0700623 for (const auto& dump : configStats.dump_report_stats) {
624 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
625 FIELD_COUNT_REPEATED,
626 dump.first);
627 }
628
629 for (const auto& dump : configStats.dump_report_stats) {
630 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
631 FIELD_COUNT_REPEATED,
632 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800633 }
634
David Chenfaa1af52018-03-30 15:14:04 -0700635 for (const auto& annotation : configStats.annotations) {
636 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
637 FIELD_ID_CONFIG_STATS_ANNOTATION);
638 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
639 (long long)annotation.first);
640 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
641 proto->end(token);
642 }
643
Yao Chen20e9e622018-02-28 11:18:51 -0800644 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800645 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800646 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
647 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
648 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
649 proto->end(tmpToken);
650 }
651
652 for (const auto& pair : configStats.condition_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_CONDITION_STATS);
655 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
656 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
657 proto->end(tmpToken);
658 }
659
660 for (const auto& pair : configStats.metric_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_METRIC_STATS);
663 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
664 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
665 proto->end(tmpToken);
666 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700667 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
668 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
669 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
670 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
671 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
672 proto->end(tmpToken);
673 }
Yao Chen20e9e622018-02-28 11:18:51 -0800674
675 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800676 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800677 FIELD_ID_CONFIG_STATS_ALERT_STATS);
678 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
679 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
680 proto->end(tmpToken);
681 }
682
683 proto->end(token);
684}
685
Yao Chen69f1baf2017-11-27 17:25:36 -0800686void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800687 lock_guard<std::mutex> lock(mLock);
688
Yao Chenb3561512017-11-21 18:07:17 -0800689 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800690 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800691 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800692
Yao Chenb3561512017-11-21 18:07:17 -0800693 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800694 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800695 }
696
697 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800698 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800699 }
700
Yao Chenb3561512017-11-21 18:07:17 -0800701 const size_t atomCounts = mPushedAtomStats.size();
702 for (size_t i = 2; i < atomCounts; i++) {
703 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800704 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800705 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
706 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
707 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
708 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800709 }
710 }
711
Chenjie Yub038b702017-12-18 15:15:34 -0800712 for (const auto& pair : mPulledAtomStats) {
713 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800714 }
715
Bookatz1d0136d2017-12-01 11:13:32 -0800716 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800717 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800718 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
719 mAnomalyAlarmRegisteredStats);
720 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800721 }
722
Yangster-mac932ecec2018-02-01 10:23:52 -0800723 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700724 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
725 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800726 mPeriodicAlarmRegisteredStats);
727 proto.end(token);
728 }
729
Yi Jin5ee07872018-03-05 18:18:27 -0800730 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800731 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
732 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800733 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700734 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800735 proto.end(uidMapToken);
David Chenc136f452017-11-27 11:52:26 -0800736
Yao Chen0f5d6612018-08-22 14:11:51 -0700737 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800738 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800739 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800740 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
741 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
742 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen884c8c12018-01-26 10:36:25 -0800743 proto.end(token);
744 }
745
Yangster-mac892f3d32018-05-02 14:16:48 -0700746 for (const auto& restart : mSystemServerRestartSec) {
747 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
748 restart);
749 }
750
Yao Chenb3561512017-11-21 18:07:17 -0800751 output->clear();
752 size_t bufferSize = proto.size();
753 output->resize(bufferSize);
754
755 size_t pos = 0;
756 auto it = proto.data();
757 while (it.readBuffer() != NULL) {
758 size_t toRead = it.currentToRead();
759 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
760 pos += toRead;
761 it.rp()->move(toRead);
762 }
763
764 if (reset) {
765 resetInternalLocked();
766 }
767
768 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800769}
770
771} // namespace statsd
772} // namespace os
yro255f72e2018-02-26 15:15:17 -0800773} // namespace android