blob: ce0e561f291b48ef74cb264b37b9c46e00d94c7e [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 Chen0f861862019-03-27 11:51:15 -070053const int FIELD_ID_OVERFLOW = 18;
Yao Chenb3561512017-11-21 18:07:17 -080054
Yao Chenb3561512017-11-21 18:07:17 -080055const int FIELD_ID_ATOM_STATS_TAG = 1;
56const int FIELD_ID_ATOM_STATS_COUNT = 2;
57
Bookatz1d0136d2017-12-01 11:13:32 -080058const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
Yangster-macb8382a12018-04-04 10:39:12 -070059const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1;
Bookatz1d0136d2017-12-01 11:13:32 -080060
Yao Chen39b67992018-11-08 15:32:17 -080061const int FIELD_ID_LOG_LOSS_STATS_TIME = 1;
62const int FIELD_ID_LOG_LOSS_STATS_COUNT = 2;
63const int FIELD_ID_LOG_LOSS_STATS_ERROR = 3;
Yao Chen49d7dd72019-03-26 14:02:11 -070064const int FIELD_ID_LOG_LOSS_STATS_TAG = 4;
65const int FIELD_ID_LOG_LOSS_STATS_UID = 5;
66const int FIELD_ID_LOG_LOSS_STATS_PID = 6;
Yao Chen884c8c12018-01-26 10:36:25 -080067
Yao Chen0f861862019-03-27 11:51:15 -070068const int FIELD_ID_OVERFLOW_COUNT = 1;
69const int FIELD_ID_OVERFLOW_MAX_HISTORY = 2;
70const int FIELD_ID_OVERFLOW_MIN_HISTORY = 3;
71
Yao Chen20e9e622018-02-28 11:18:51 -080072const int FIELD_ID_CONFIG_STATS_UID = 1;
73const int FIELD_ID_CONFIG_STATS_ID = 2;
74const int FIELD_ID_CONFIG_STATS_CREATION = 3;
Tej Singh5260eea2018-04-04 17:27:14 -070075const int FIELD_ID_CONFIG_STATS_RESET = 19;
Yao Chen20e9e622018-02-28 11:18:51 -080076const int FIELD_ID_CONFIG_STATS_DELETION = 4;
77const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
78const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
79const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
80const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
81const int FIELD_ID_CONFIG_STATS_VALID = 9;
82const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
Chenjie Yuc3c30c02018-10-26 09:48:07 -070083const int FIELD_ID_CONFIG_STATS_DATA_DROP_TIME = 11;
84const int FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES = 21;
Yangster-mace68f3a52018-04-04 00:01:43 -070085const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12;
86const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20;
Yao Chen20e9e622018-02-28 11:18:51 -080087const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
88const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
89const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
90const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
Yangster-mac306ccc22018-03-24 15:03:40 -070091const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17;
David Chenfaa1af52018-03-30 15:14:04 -070092const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18;
Tej Singh6ede28b2019-01-29 17:06:54 -080093const int FIELD_ID_CONFIG_STATS_ACTIVATION = 22;
94const int FIELD_ID_CONFIG_STATS_DEACTIVATION = 23;
David Chenfaa1af52018-03-30 15:14:04 -070095const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
96const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
Yao Chen20e9e622018-02-28 11:18:51 -080097
98const int FIELD_ID_MATCHER_STATS_ID = 1;
99const int FIELD_ID_MATCHER_STATS_COUNT = 2;
100const int FIELD_ID_CONDITION_STATS_ID = 1;
101const int FIELD_ID_CONDITION_STATS_COUNT = 2;
102const int FIELD_ID_METRIC_STATS_ID = 1;
103const int FIELD_ID_METRIC_STATS_COUNT = 2;
104const int FIELD_ID_ALERT_STATS_ID = 1;
105const int FIELD_ID_ALERT_STATS_COUNT = 2;
106
David Chenbd125272018-04-04 19:02:50 -0700107const int FIELD_ID_UID_MAP_CHANGES = 1;
108const int FIELD_ID_UID_MAP_BYTES_USED = 2;
109const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
110const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
Yao Chen20e9e622018-02-28 11:18:51 -0800111
Chenjie Yuc5875052018-03-09 10:13:11 -0800112const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100113 {android::util::BINDER_CALLS, {6000, 10000}},
Olivier Gaillardc840ed92018-11-30 13:06:18 +0000114 {android::util::LOOPER_STATS, {1500, 2500}},
Chenjie Yuc5875052018-03-09 10:13:11 -0800115 {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
116};
117
Yao Chenb3561512017-11-21 18:07:17 -0800118StatsdStats::StatsdStats() {
119 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -0800120 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800121}
122
123StatsdStats& StatsdStats::getInstance() {
124 static StatsdStats statsInstance;
125 return statsInstance;
126}
127
Yao Chen20e9e622018-02-28 11:18:51 -0800128void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
Yao Chenf6723df2018-01-08 15:11:58 -0800129 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
130 if (mIceBox.size() == kMaxIceBoxSize) {
131 mIceBox.pop_front();
132 }
133 mIceBox.push_back(stats);
134}
135
David Chenfaa1af52018-03-30 15:14:04 -0700136void StatsdStats::noteConfigReceived(
137 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
138 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
139 bool isValid) {
Yao Chenb3561512017-11-21 18:07:17 -0800140 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -0800141 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800142
143 // If there is an existing config for the same key, icebox the old config.
144 noteConfigRemovedInternalLocked(key);
145
Yao Chen20e9e622018-02-28 11:18:51 -0800146 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
147 configStats->uid = key.GetUid();
148 configStats->id = key.GetId();
149 configStats->creation_time_sec = nowTimeSec;
150 configStats->metric_count = metricsCount;
151 configStats->condition_count = conditionsCount;
152 configStats->matcher_count = matchersCount;
153 configStats->alert_count = alertsCount;
154 configStats->is_valid = isValid;
David Chenfaa1af52018-03-30 15:14:04 -0700155 for (auto& v : annotations) {
156 configStats->annotations.emplace_back(v);
157 }
Yao Chenb3561512017-11-21 18:07:17 -0800158
159 if (isValid) {
160 mConfigStats[key] = configStats;
161 } else {
Yao Chen20e9e622018-02-28 11:18:51 -0800162 configStats->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800163 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800164 }
165}
166
167void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
168 auto it = mConfigStats.find(key);
169 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800170 int32_t nowTimeSec = getWallClockSec();
Yao Chen20e9e622018-02-28 11:18:51 -0800171 it->second->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800172 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800173 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800174 }
175}
176
177void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
178 lock_guard<std::mutex> lock(mLock);
179 noteConfigRemovedInternalLocked(key);
180}
181
Yangster-macb142cc82018-03-30 15:22:08 -0700182void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
183 auto it = mConfigStats.find(key);
184 if (it != mConfigStats.end()) {
185 it->second->reset_time_sec = getWallClockSec();
186 }
187}
188
189void StatsdStats::noteConfigReset(const ConfigKey& key) {
190 lock_guard<std::mutex> lock(mLock);
191 noteConfigResetInternalLocked(key);
192}
193
Yao Chen49d7dd72019-03-26 14:02:11 -0700194void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError,
195 int32_t lastTag, int32_t uid, int32_t pid) {
Yao Chen163d2602018-04-10 10:39:53 -0700196 lock_guard<std::mutex> lock(mLock);
Yao Chen0f5d6612018-08-22 14:11:51 -0700197 if (mLogLossStats.size() == kMaxLoggerErrors) {
198 mLogLossStats.pop_front();
Yao Chen163d2602018-04-10 10:39:53 -0700199 }
Yao Chen49d7dd72019-03-26 14:02:11 -0700200 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError, lastTag, uid, pid);
Yao Chen163d2602018-04-10 10:39:53 -0700201}
202
Yao Chenb3561512017-11-21 18:07:17 -0800203void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800204 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800205}
206
207void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800208 lock_guard<std::mutex> lock(mLock);
209 auto it = mConfigStats.find(key);
210 if (it == mConfigStats.end()) {
211 ALOGE("Config key %s not found!", key.ToString().c_str());
212 return;
213 }
Yao Chen20e9e622018-02-28 11:18:51 -0800214 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
215 it->second->broadcast_sent_time_sec.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800216 }
Yao Chen20e9e622018-02-28 11:18:51 -0800217 it->second->broadcast_sent_time_sec.push_back(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800218}
219
Tej Singh6ede28b2019-01-29 17:06:54 -0800220void StatsdStats::noteActiveStatusChanged(const ConfigKey& key, bool activated) {
221 noteActiveStatusChanged(key, activated, getWallClockSec());
222}
223
224void StatsdStats::noteActiveStatusChanged(const ConfigKey& key, bool activated, int32_t timeSec) {
225 lock_guard<std::mutex> lock(mLock);
226 auto it = mConfigStats.find(key);
227 if (it == mConfigStats.end()) {
228 ALOGE("Config key %s not found!", key.ToString().c_str());
229 return;
230 }
231 auto& vec = activated ? it->second->activation_time_sec
232 : it->second->deactivation_time_sec;
233 if (vec.size() == kMaxTimestampCount) {
234 vec.pop_front();
235 }
236 vec.push_back(timeSec);
237}
238
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700239void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
240 noteDataDropped(key, totalBytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800241}
242
Yao Chen0f861862019-03-27 11:51:15 -0700243void StatsdStats::noteEventQueueOverflow(int64_t oldestEventTimestampNs) {
244 lock_guard<std::mutex> lock(mLock);
245
246 mOverflowCount++;
247
248 int64_t history = getElapsedRealtimeNs() - oldestEventTimestampNs;
249
250 if (history > mMaxQueueHistoryNs) {
251 mMaxQueueHistoryNs = history;
252 }
253
254 if (history < mMinQueueHistoryNs) {
255 mMinQueueHistoryNs = history;
256 }
257}
258
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700259void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800260 lock_guard<std::mutex> lock(mLock);
261 auto it = mConfigStats.find(key);
262 if (it == mConfigStats.end()) {
263 ALOGE("Config key %s not found!", key.ToString().c_str());
264 return;
265 }
Yao Chen20e9e622018-02-28 11:18:51 -0800266 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
267 it->second->data_drop_time_sec.pop_front();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700268 it->second->data_drop_bytes.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800269 }
Yao Chen20e9e622018-02-28 11:18:51 -0800270 it->second->data_drop_time_sec.push_back(timeSec);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700271 it->second->data_drop_bytes.push_back(totalBytes);
Yao Chenb3561512017-11-21 18:07:17 -0800272}
273
Yangster-mace68f3a52018-04-04 00:01:43 -0700274void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
275 noteMetricsReportSent(key, num_bytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800276}
277
Yangster-mace68f3a52018-04-04 00:01:43 -0700278void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
279 int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800280 lock_guard<std::mutex> lock(mLock);
281 auto it = mConfigStats.find(key);
282 if (it == mConfigStats.end()) {
283 ALOGE("Config key %s not found!", key.ToString().c_str());
284 return;
285 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700286 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
287 it->second->dump_report_stats.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800288 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700289 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
Yao Chen69f1baf2017-11-27 17:25:36 -0800290}
291
David Chenbd125272018-04-04 19:02:50 -0700292void StatsdStats::noteUidMapDropped(int deltas) {
David Chenc136f45a2017-11-27 11:52:26 -0800293 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800294 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
David Chenc136f45a2017-11-27 11:52:26 -0800295}
296
David Chenbd125272018-04-04 19:02:50 -0700297void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f45a2017-11-27 11:52:26 -0800298 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700299 mUidMapStats.deleted_apps++;
David Chenc136f45a2017-11-27 11:52:26 -0800300}
301
302void StatsdStats::setUidMapChanges(int changes) {
303 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800304 mUidMapStats.changes = changes;
David Chenc136f45a2017-11-27 11:52:26 -0800305}
306
307void StatsdStats::setCurrentUidMapMemory(int bytes) {
308 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800309 mUidMapStats.bytes_used = bytes;
David Chenc136f45a2017-11-27 11:52:26 -0800310}
311
Yangster-mac94e197c2018-01-02 16:03:03 -0800312void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800313 lock_guard<std::mutex> lock(mLock);
314 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800315 auto statsIt = mConfigStats.find(key);
316 if (statsIt == mConfigStats.end()) {
317 return;
318 }
319
320 auto& conditionSizeMap = statsIt->second->condition_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800321 if (size > conditionSizeMap[id]) {
322 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800323 }
324}
325
Yangster-mac94e197c2018-01-02 16:03:03 -0800326void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800327 lock_guard<std::mutex> lock(mLock);
328 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800329 auto statsIt = mConfigStats.find(key);
330 if (statsIt == mConfigStats.end()) {
331 return;
332 }
333 auto& metricsDimensionMap = statsIt->second->metric_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800334 if (size > metricsDimensionMap[id]) {
335 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800336 }
337}
338
Yangster-mac306ccc22018-03-24 15:03:40 -0700339void StatsdStats::noteMetricDimensionInConditionSize(
340 const ConfigKey& key, const int64_t& id, int size) {
341 lock_guard<std::mutex> lock(mLock);
342 // if name doesn't exist before, it will create the key with count 0.
343 auto statsIt = mConfigStats.find(key);
344 if (statsIt == mConfigStats.end()) {
345 return;
346 }
347 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
348 if (size > metricsDimensionMap[id]) {
349 metricsDimensionMap[id] = size;
350 }
351}
352
Yangster-mac94e197c2018-01-02 16:03:03 -0800353void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800354 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800355
356 auto statsIt = mConfigStats.find(key);
357 if (statsIt == mConfigStats.end()) {
358 return;
359 }
360 statsIt->second->matcher_stats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800361}
362
Yangster-mac94e197c2018-01-02 16:03:03 -0800363void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800364 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800365 auto statsIt = mConfigStats.find(key);
366 if (statsIt == mConfigStats.end()) {
367 return;
368 }
369 statsIt->second->alert_stats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800370}
371
Bookatz1d0136d2017-12-01 11:13:32 -0800372void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
373 lock_guard<std::mutex> lock(mLock);
374 mAnomalyAlarmRegisteredStats++;
375}
376
Yangster-mac932ecec2018-02-01 10:23:52 -0800377void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
378 lock_guard<std::mutex> lock(mLock);
379 mPeriodicAlarmRegisteredStats++;
380}
381
Chenjie Yub038b702017-12-18 15:15:34 -0800382void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
383 lock_guard<std::mutex> lock(mLock);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700384 mPulledAtomStats[pullAtomId].minPullIntervalSec =
385 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
Chenjie Yub038b702017-12-18 15:15:34 -0800386}
387
388void StatsdStats::notePull(int pullAtomId) {
389 lock_guard<std::mutex> lock(mLock);
390 mPulledAtomStats[pullAtomId].totalPull++;
391}
392
393void StatsdStats::notePullFromCache(int pullAtomId) {
394 lock_guard<std::mutex> lock(mLock);
395 mPulledAtomStats[pullAtomId].totalPullFromCache++;
396}
397
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700398void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
399 lock_guard<std::mutex> lock(mLock);
400 auto& pullStats = mPulledAtomStats[pullAtomId];
401 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
402 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
403 (pullStats.numPullTime + 1);
404 pullStats.numPullTime += 1;
405}
406
407void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
408 lock_guard<std::mutex> lock(mLock);
409 auto& pullStats = mPulledAtomStats[pullAtomId];
410 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700411 pullStats.avgPullDelayNs =
412 (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
413 (pullStats.numPullDelay + 1);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700414 pullStats.numPullDelay += 1;
415}
416
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700417void StatsdStats::notePullDataError(int pullAtomId) {
418 lock_guard<std::mutex> lock(mLock);
419 mPulledAtomStats[pullAtomId].dataError++;
420}
421
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800422void StatsdStats::notePullTimeout(int pullAtomId) {
423 lock_guard<std::mutex> lock(mLock);
424 mPulledAtomStats[pullAtomId].pullTimeout++;
425}
426
427void StatsdStats::notePullExceedMaxDelay(int pullAtomId) {
428 lock_guard<std::mutex> lock(mLock);
429 mPulledAtomStats[pullAtomId].pullExceedMaxDelay++;
430}
431
Yao Chenb3561512017-11-21 18:07:17 -0800432void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
433 lock_guard<std::mutex> lock(mLock);
434
Tej Singh35c7a572019-05-01 16:47:54 -0700435 if (atomId <= android::util::kMaxPushedAtomId) {
436 mPushedAtomStats[atomId]++;
437 } else {
438 if (mNonPlatformPushedAtomStats.size() < kMaxNonPlatformPushedAtoms) {
439 mNonPlatformPushedAtomStats[atomId]++;
440 }
Yao Chenb3561512017-11-21 18:07:17 -0800441 }
Yao Chenb3561512017-11-21 18:07:17 -0800442}
443
Yangster-mac892f3d32018-05-02 14:16:48 -0700444void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
445 lock_guard<std::mutex> lock(mLock);
446
447 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
448 mSystemServerRestartSec.pop_front();
449 }
450 mSystemServerRestartSec.push_back(timeSec);
451}
452
Misha Wagner1eee2212019-01-22 11:47:11 +0000453void StatsdStats::notePullFailed(int atomId) {
454 lock_guard<std::mutex> lock(mLock);
455 mPulledAtomStats[atomId].pullFailed++;
456}
457
458void StatsdStats::noteStatsCompanionPullFailed(int atomId) {
459 lock_guard<std::mutex> lock(mLock);
460 mPulledAtomStats[atomId].statsCompanionPullFailed++;
461}
462
463void StatsdStats::noteStatsCompanionPullBinderTransactionFailed(int atomId) {
464 lock_guard<std::mutex> lock(mLock);
465 mPulledAtomStats[atomId].statsCompanionPullBinderTransactionFailed++;
466}
467
468void StatsdStats::noteEmptyData(int atomId) {
469 lock_guard<std::mutex> lock(mLock);
470 mPulledAtomStats[atomId].emptyData++;
471}
472
Tej Singha0c89dd2019-01-25 16:39:18 -0800473void StatsdStats::notePullerCallbackRegistrationChanged(int atomId, bool registered) {
474 lock_guard<std::mutex> lock(mLock);
475 if (registered) {
476 mPulledAtomStats[atomId].registeredCount++;
477 } else {
478 mPulledAtomStats[atomId].unregisteredCount++;
479 }
480}
481
Olivier Gaillard2310b192019-02-08 10:23:05 +0000482void StatsdStats::noteHardDimensionLimitReached(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000483 lock_guard<std::mutex> lock(mLock);
484 getAtomMetricStats(metricId).hardDimensionLimitReached++;
485}
486
Olivier Gaillard2310b192019-02-08 10:23:05 +0000487void StatsdStats::noteLateLogEventSkipped(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000488 lock_guard<std::mutex> lock(mLock);
489 getAtomMetricStats(metricId).lateLogEventSkipped++;
490}
491
Olivier Gaillard2310b192019-02-08 10:23:05 +0000492void StatsdStats::noteSkippedForwardBuckets(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000493 lock_guard<std::mutex> lock(mLock);
494 getAtomMetricStats(metricId).skippedForwardBuckets++;
495}
496
Olivier Gaillard2310b192019-02-08 10:23:05 +0000497void StatsdStats::noteBadValueType(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000498 lock_guard<std::mutex> lock(mLock);
499 getAtomMetricStats(metricId).badValueType++;
500}
501
Olivier Gaillard2310b192019-02-08 10:23:05 +0000502void StatsdStats::noteBucketDropped(int64_t metricId) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000503 lock_guard<std::mutex> lock(mLock);
504 getAtomMetricStats(metricId).bucketDropped++;
505}
506
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000507void StatsdStats::noteBucketUnknownCondition(int64_t metricId) {
508 lock_guard<std::mutex> lock(mLock);
509 getAtomMetricStats(metricId).bucketUnknownCondition++;
510}
511
Olivier Gaillard2310b192019-02-08 10:23:05 +0000512void StatsdStats::noteConditionChangeInNextBucket(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000513 lock_guard<std::mutex> lock(mLock);
514 getAtomMetricStats(metricId).conditionChangeInNextBucket++;
515}
516
Olivier Gaillard2310b192019-02-08 10:23:05 +0000517void StatsdStats::noteInvalidatedBucket(int64_t metricId) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000518 lock_guard<std::mutex> lock(mLock);
519 getAtomMetricStats(metricId).invalidatedBucket++;
520}
521
Olivier Gaillardf248c0d2019-02-21 15:56:58 +0000522void StatsdStats::noteBucketCount(int64_t metricId) {
523 lock_guard<std::mutex> lock(mLock);
524 getAtomMetricStats(metricId).bucketCount++;
525}
526
Olivier Gaillard3ba2e932019-02-08 14:25:35 +0000527void StatsdStats::noteBucketBoundaryDelayNs(int64_t metricId, int64_t timeDelayNs) {
528 lock_guard<std::mutex> lock(mLock);
529 AtomMetricStats& pullStats = getAtomMetricStats(metricId);
530 pullStats.maxBucketBoundaryDelayNs =
531 std::max(pullStats.maxBucketBoundaryDelayNs, timeDelayNs);
532 pullStats.minBucketBoundaryDelayNs =
533 std::min(pullStats.minBucketBoundaryDelayNs, timeDelayNs);
534}
535
Olivier Gaillardf48fb042019-02-13 11:03:20 +0000536StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000537 auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
538 if (atomMetricStatsIter != mAtomMetricStats.end()) {
539 return atomMetricStatsIter->second;
540 }
541 auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
542 return emplaceResult.first->second;
543}
544
Yao Chenb3561512017-11-21 18:07:17 -0800545void StatsdStats::reset() {
546 lock_guard<std::mutex> lock(mLock);
547 resetInternalLocked();
548}
549
550void StatsdStats::resetInternalLocked() {
551 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800552 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800553 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800554 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Tej Singh35c7a572019-05-01 16:47:54 -0700555 mNonPlatformPushedAtomStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800556 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800557 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700558 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700559 mLogLossStats.clear();
Yao Chen0f861862019-03-27 11:51:15 -0700560 mOverflowCount = 0;
561 mMinQueueHistoryNs = kInt64Max;
562 mMaxQueueHistoryNs = 0;
Yao Chen0fac5b12017-11-28 16:07:02 -0800563 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800564 config.second->broadcast_sent_time_sec.clear();
Tej Singh6ede28b2019-01-29 17:06:54 -0800565 config.second->activation_time_sec.clear();
566 config.second->deactivation_time_sec.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800567 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700568 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700569 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700570 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800571 config.second->matcher_stats.clear();
572 config.second->condition_stats.clear();
573 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700574 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800575 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800576 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700577 for (auto& pullStats : mPulledAtomStats) {
578 pullStats.second.totalPull = 0;
579 pullStats.second.totalPullFromCache = 0;
580 pullStats.second.avgPullTimeNs = 0;
581 pullStats.second.maxPullTimeNs = 0;
582 pullStats.second.numPullTime = 0;
583 pullStats.second.avgPullDelayNs = 0;
584 pullStats.second.maxPullDelayNs = 0;
585 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700586 pullStats.second.dataError = 0;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800587 pullStats.second.pullTimeout = 0;
588 pullStats.second.pullExceedMaxDelay = 0;
Tej Singha0c89dd2019-01-25 16:39:18 -0800589 pullStats.second.registeredCount = 0;
590 pullStats.second.unregisteredCount = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700591 }
Misha Wagner1eee2212019-01-22 11:47:11 +0000592 mAtomMetricStats.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800593}
594
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700595string buildTimeString(int64_t timeSec) {
596 time_t t = timeSec;
597 struct tm* tm = localtime(&t);
598 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700599 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700600 return string(timeBuffer);
601}
602
Yao Chena80e5c02018-09-04 13:55:29 -0700603void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800604 lock_guard<std::mutex> lock(mLock);
605 time_t t = mStartTimeSec;
606 struct tm* tm = localtime(&t);
607 char timeBuffer[80];
608 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700609 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
610 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800611 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700612 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700613 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800614 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800615 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700616 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700617 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
618 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800619
Yao Chen20e9e622018-02-28 11:18:51 -0800620 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700621 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800622 }
623
Tej Singh6ede28b2019-01-29 17:06:54 -0800624 for (const int& activationTime : configStats->activation_time_sec) {
625 dprintf(out, "\tactivation time: %d\n", activationTime);
626 }
627
628 for (const int& deactivationTime : configStats->deactivation_time_sec) {
629 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
630 }
631
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700632 auto dropTimePtr = configStats->data_drop_time_sec.begin();
633 auto dropBytesPtr = configStats->data_drop_bytes.begin();
634 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
635 i++, dropTimePtr++, dropBytesPtr++) {
636 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
637 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800638 }
639 }
Yao Chena80e5c02018-09-04 13:55:29 -0700640 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800641 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800642 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700643 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800644 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
645 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800646 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
647 configStats->deletion_time_sec, configStats->metric_count,
648 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
649 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700650 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700651 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700652 annotation.second);
653 }
654
Yao Chen20e9e622018-02-28 11:18:51 -0800655 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700656 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
657 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800658 }
659
Tej Singh6ede28b2019-01-29 17:06:54 -0800660 for (const int& activationTime : configStats->activation_time_sec) {
661 dprintf(out, "\tactivation time: %d\n", activationTime);
662 }
663
664 for (const int& deactivationTime : configStats->deactivation_time_sec) {
665 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
666 }
667
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700668 auto dropTimePtr = configStats->data_drop_time_sec.begin();
669 auto dropBytesPtr = configStats->data_drop_bytes.begin();
670 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
671 i++, dropTimePtr++, dropBytesPtr++) {
672 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
673 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
674 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800675 }
676
Yangster-mace68f3a52018-04-04 00:01:43 -0700677 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700678 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700679 buildTimeString(dump.first).c_str(), (long long)dump.first,
680 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800681 }
682
Yao Chen20e9e622018-02-28 11:18:51 -0800683 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700684 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800685 }
Yao Chen20e9e622018-02-28 11:18:51 -0800686
687 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700688 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800689 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800690 }
Yao Chen20e9e622018-02-28 11:18:51 -0800691
692 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700693 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800694 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800695 }
Yao Chen20e9e622018-02-28 11:18:51 -0800696
697 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700698 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800699 }
700 }
Yao Chena80e5c02018-09-04 13:55:29 -0700701 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700702 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700703 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800704 const size_t atomCounts = mPushedAtomStats.size();
705 for (size_t i = 2; i < atomCounts; i++) {
706 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700707 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800708 }
709 }
Tej Singh35c7a572019-05-01 16:47:54 -0700710 for (const auto& pair : mNonPlatformPushedAtomStats) {
711 dprintf(out, "Atom %lu->%d\n", (unsigned long)pair.first, pair.second);
712 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800713
Yao Chena80e5c02018-09-04 13:55:29 -0700714 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800715 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700716 dprintf(out,
Rafal Slawik2065e5b2019-03-11 18:21:54 +0000717 "Atom %d->(total pull)%ld, (pull from cache)%ld, "
718 "(pull failed)%ld, (min pull interval)%ld \n"
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800719 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
720 "nanos)%lld, "
721 " (max pull delay nanos)%lld, (data error)%ld\n"
Tej Singha0c89dd2019-01-25 16:39:18 -0800722 " (pull timeout)%ld, (pull exceed max delay)%ld\n"
723 " (registered count) %ld, (unregistered count) %ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700724 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
Rafal Slawik2065e5b2019-03-11 18:21:54 +0000725 (long)pair.second.pullFailed, (long)pair.second.minPullIntervalSec,
726 (long long)pair.second.avgPullTimeNs, (long long)pair.second.maxPullTimeNs,
727 (long long)pair.second.avgPullDelayNs, (long long)pair.second.maxPullDelayNs,
728 pair.second.dataError, pair.second.pullTimeout, pair.second.pullExceedMaxDelay,
Tej Singha0c89dd2019-01-25 16:39:18 -0800729 pair.second.registeredCount, pair.second.unregisteredCount);
Yao Chenf5acabe2018-01-17 14:10:34 -0800730 }
731
732 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700733 dprintf(out, "********AnomalyAlarmStats stats***********\n");
734 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800735 }
736
Yangster-mac932ecec2018-02-01 10:23:52 -0800737 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700738 dprintf(out, "********SubscriberAlarmStats stats***********\n");
739 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800740 }
741
Yao Chena80e5c02018-09-04 13:55:29 -0700742 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700743 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
744 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800745
Yangster-mac892f3d32018-05-02 14:16:48 -0700746 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700747 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
748 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700749 }
750
Yao Chen0f5d6612018-08-22 14:11:51 -0700751 for (const auto& loss : mLogLossStats) {
Yao Chen49d7dd72019-03-26 14:02:11 -0700752 dprintf(out,
753 "Log loss: %lld (wall clock sec) - %d (count), %d (last error), %d (last tag), %d "
754 "(uid), %d (pid)\n",
755 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError, loss.mLastTag,
756 loss.mUid, loss.mPid);
Yangster-macb8382a12018-04-04 10:39:12 -0700757 }
Yao Chen0f861862019-03-27 11:51:15 -0700758
759 dprintf(out, "Event queue overflow: %d; MaxHistoryNs: %lld; MinHistoryNs: %lld\n",
760 mOverflowCount, (long long)mMaxQueueHistoryNs, (long long)mMinQueueHistoryNs);
Yao Chenf5acabe2018-01-17 14:10:34 -0800761}
762
Yao Chen20e9e622018-02-28 11:18:51 -0800763void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800764 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800765 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
766 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
767 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
768 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700769 if (configStats.reset_time_sec != 0) {
770 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
771 }
Yao Chen20e9e622018-02-28 11:18:51 -0800772 if (configStats.deletion_time_sec != 0) {
773 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
774 configStats.deletion_time_sec);
775 }
776 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
777 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
778 configStats.condition_count);
779 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
780 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
781 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
782
783 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
784 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
785 broadcast);
786 }
787
Tej Singh6ede28b2019-01-29 17:06:54 -0800788 for (const auto& activation : configStats.activation_time_sec) {
789 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ACTIVATION | FIELD_COUNT_REPEATED,
790 activation);
791 }
792
793 for (const auto& deactivation : configStats.deactivation_time_sec) {
794 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DEACTIVATION | FIELD_COUNT_REPEATED,
795 deactivation);
796 }
797
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700798 for (const auto& drop_time : configStats.data_drop_time_sec) {
799 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
800 drop_time);
801 }
802
803 for (const auto& drop_bytes : configStats.data_drop_bytes) {
804 proto->write(
805 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
806 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800807 }
808
Yangster-mace68f3a52018-04-04 00:01:43 -0700809 for (const auto& dump : configStats.dump_report_stats) {
810 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
811 FIELD_COUNT_REPEATED,
812 dump.first);
813 }
814
815 for (const auto& dump : configStats.dump_report_stats) {
816 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
817 FIELD_COUNT_REPEATED,
818 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800819 }
820
David Chenfaa1af52018-03-30 15:14:04 -0700821 for (const auto& annotation : configStats.annotations) {
822 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
823 FIELD_ID_CONFIG_STATS_ANNOTATION);
824 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
825 (long long)annotation.first);
826 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
827 proto->end(token);
828 }
829
Yao Chen20e9e622018-02-28 11:18:51 -0800830 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800831 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800832 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
833 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
834 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
835 proto->end(tmpToken);
836 }
837
838 for (const auto& pair : configStats.condition_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800839 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800840 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
841 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
842 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
843 proto->end(tmpToken);
844 }
845
846 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800847 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800848 FIELD_ID_CONFIG_STATS_METRIC_STATS);
849 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
850 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
851 proto->end(tmpToken);
852 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700853 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
854 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
855 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
856 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
857 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
858 proto->end(tmpToken);
859 }
Yao Chen20e9e622018-02-28 11:18:51 -0800860
861 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800862 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800863 FIELD_ID_CONFIG_STATS_ALERT_STATS);
864 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
865 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
866 proto->end(tmpToken);
867 }
868
869 proto->end(token);
870}
871
Yao Chen69f1baf2017-11-27 17:25:36 -0800872void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800873 lock_guard<std::mutex> lock(mLock);
874
Yao Chenb3561512017-11-21 18:07:17 -0800875 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800876 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800877 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800878
Yao Chenb3561512017-11-21 18:07:17 -0800879 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800880 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800881 }
882
883 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800884 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800885 }
886
Yao Chenb3561512017-11-21 18:07:17 -0800887 const size_t atomCounts = mPushedAtomStats.size();
888 for (size_t i = 2; i < atomCounts; i++) {
889 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800890 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800891 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
892 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
893 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
894 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800895 }
896 }
897
Tej Singh35c7a572019-05-01 16:47:54 -0700898 for (const auto& pair : mNonPlatformPushedAtomStats) {
899 uint64_t token =
900 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
901 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, pair.first);
902 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, pair.second);
903 proto.end(token);
904 }
905
Chenjie Yub038b702017-12-18 15:15:34 -0800906 for (const auto& pair : mPulledAtomStats) {
907 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800908 }
909
Misha Wagner1eee2212019-01-22 11:47:11 +0000910 for (const auto& pair : mAtomMetricStats) {
911 android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
912 }
913
Bookatz1d0136d2017-12-01 11:13:32 -0800914 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800915 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800916 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
917 mAnomalyAlarmRegisteredStats);
918 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800919 }
920
Yangster-mac932ecec2018-02-01 10:23:52 -0800921 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700922 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
923 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800924 mPeriodicAlarmRegisteredStats);
925 proto.end(token);
926 }
927
Yi Jin5ee07872018-03-05 18:18:27 -0800928 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800929 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
930 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800931 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700932 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800933 proto.end(uidMapToken);
David Chenc136f45a2017-11-27 11:52:26 -0800934
Yao Chen0f5d6612018-08-22 14:11:51 -0700935 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800936 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800937 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800938 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
939 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
940 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen49d7dd72019-03-26 14:02:11 -0700941 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TAG, error.mLastTag);
942 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_UID, error.mUid);
943 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_PID, error.mPid);
Yao Chen884c8c12018-01-26 10:36:25 -0800944 proto.end(token);
945 }
946
Yao Chen0f861862019-03-27 11:51:15 -0700947 if (mOverflowCount > 0) {
948 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_OVERFLOW);
949 proto.write(FIELD_TYPE_INT32 | FIELD_ID_OVERFLOW_COUNT, (int32_t)mOverflowCount);
950 proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MAX_HISTORY,
951 (long long)mMaxQueueHistoryNs);
952 proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MIN_HISTORY,
953 (long long)mMinQueueHistoryNs);
954 proto.end(token);
955 }
956
Yangster-mac892f3d32018-05-02 14:16:48 -0700957 for (const auto& restart : mSystemServerRestartSec) {
958 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
959 restart);
960 }
961
Yao Chenb3561512017-11-21 18:07:17 -0800962 output->clear();
963 size_t bufferSize = proto.size();
964 output->resize(bufferSize);
965
966 size_t pos = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000967 sp<android::util::ProtoReader> reader = proto.data();
968 while (reader->readBuffer() != NULL) {
969 size_t toRead = reader->currentToRead();
970 std::memcpy(&((*output)[pos]), reader->readBuffer(), toRead);
Yao Chenb3561512017-11-21 18:07:17 -0800971 pos += toRead;
Joe Onorato99598ee2019-02-11 15:55:13 +0000972 reader->move(toRead);
Yao Chenb3561512017-11-21 18:07:17 -0800973 }
974
975 if (reset) {
976 resetInternalLocked();
977 }
978
979 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800980}
981
982} // namespace statsd
983} // namespace os
yro255f72e2018-02-26 15:15:17 -0800984} // namespace android