blob: 74a4c87af7c343bfb6411279537931b054b0ff82 [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 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 Chenc136f452017-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 Chenc136f452017-11-27 11:52:26 -0800295}
296
David Chenbd125272018-04-04 19:02:50 -0700297void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f452017-11-27 11:52:26 -0800298 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700299 mUidMapStats.deleted_apps++;
David Chenc136f452017-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 Chenc136f452017-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 Chenc136f452017-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
Yao Chenb3561512017-11-21 18:07:17 -0800435 if (atomId > android::util::kMaxPushedAtomId) {
436 ALOGW("not interested in atom %d", atomId);
437 return;
438 }
439
440 mPushedAtomStats[atomId]++;
441}
442
Yangster-mac892f3d32018-05-02 14:16:48 -0700443void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
444 lock_guard<std::mutex> lock(mLock);
445
446 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
447 mSystemServerRestartSec.pop_front();
448 }
449 mSystemServerRestartSec.push_back(timeSec);
450}
451
Misha Wagner1eee2212019-01-22 11:47:11 +0000452void StatsdStats::notePullFailed(int atomId) {
453 lock_guard<std::mutex> lock(mLock);
454 mPulledAtomStats[atomId].pullFailed++;
455}
456
457void StatsdStats::noteStatsCompanionPullFailed(int atomId) {
458 lock_guard<std::mutex> lock(mLock);
459 mPulledAtomStats[atomId].statsCompanionPullFailed++;
460}
461
462void StatsdStats::noteStatsCompanionPullBinderTransactionFailed(int atomId) {
463 lock_guard<std::mutex> lock(mLock);
464 mPulledAtomStats[atomId].statsCompanionPullBinderTransactionFailed++;
465}
466
467void StatsdStats::noteEmptyData(int atomId) {
468 lock_guard<std::mutex> lock(mLock);
469 mPulledAtomStats[atomId].emptyData++;
470}
471
Tej Singha0c89dd2019-01-25 16:39:18 -0800472void StatsdStats::notePullerCallbackRegistrationChanged(int atomId, bool registered) {
473 lock_guard<std::mutex> lock(mLock);
474 if (registered) {
475 mPulledAtomStats[atomId].registeredCount++;
476 } else {
477 mPulledAtomStats[atomId].unregisteredCount++;
478 }
479}
480
Olivier Gaillard2310b192019-02-08 10:23:05 +0000481void StatsdStats::noteHardDimensionLimitReached(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000482 lock_guard<std::mutex> lock(mLock);
483 getAtomMetricStats(metricId).hardDimensionLimitReached++;
484}
485
Olivier Gaillard2310b192019-02-08 10:23:05 +0000486void StatsdStats::noteLateLogEventSkipped(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000487 lock_guard<std::mutex> lock(mLock);
488 getAtomMetricStats(metricId).lateLogEventSkipped++;
489}
490
Olivier Gaillard2310b192019-02-08 10:23:05 +0000491void StatsdStats::noteSkippedForwardBuckets(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000492 lock_guard<std::mutex> lock(mLock);
493 getAtomMetricStats(metricId).skippedForwardBuckets++;
494}
495
Olivier Gaillard2310b192019-02-08 10:23:05 +0000496void StatsdStats::noteBadValueType(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000497 lock_guard<std::mutex> lock(mLock);
498 getAtomMetricStats(metricId).badValueType++;
499}
500
Olivier Gaillard2310b192019-02-08 10:23:05 +0000501void StatsdStats::noteBucketDropped(int64_t metricId) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000502 lock_guard<std::mutex> lock(mLock);
503 getAtomMetricStats(metricId).bucketDropped++;
504}
505
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000506void StatsdStats::noteBucketUnknownCondition(int64_t metricId) {
507 lock_guard<std::mutex> lock(mLock);
508 getAtomMetricStats(metricId).bucketUnknownCondition++;
509}
510
Olivier Gaillard2310b192019-02-08 10:23:05 +0000511void StatsdStats::noteConditionChangeInNextBucket(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000512 lock_guard<std::mutex> lock(mLock);
513 getAtomMetricStats(metricId).conditionChangeInNextBucket++;
514}
515
Olivier Gaillard2310b192019-02-08 10:23:05 +0000516void StatsdStats::noteInvalidatedBucket(int64_t metricId) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000517 lock_guard<std::mutex> lock(mLock);
518 getAtomMetricStats(metricId).invalidatedBucket++;
519}
520
Olivier Gaillardf248c0d2019-02-21 15:56:58 +0000521void StatsdStats::noteBucketCount(int64_t metricId) {
522 lock_guard<std::mutex> lock(mLock);
523 getAtomMetricStats(metricId).bucketCount++;
524}
525
Olivier Gaillard3ba2e932019-02-08 14:25:35 +0000526void StatsdStats::noteBucketBoundaryDelayNs(int64_t metricId, int64_t timeDelayNs) {
527 lock_guard<std::mutex> lock(mLock);
528 AtomMetricStats& pullStats = getAtomMetricStats(metricId);
529 pullStats.maxBucketBoundaryDelayNs =
530 std::max(pullStats.maxBucketBoundaryDelayNs, timeDelayNs);
531 pullStats.minBucketBoundaryDelayNs =
532 std::min(pullStats.minBucketBoundaryDelayNs, timeDelayNs);
533}
534
Olivier Gaillardf48fb042019-02-13 11:03:20 +0000535StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000536 auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
537 if (atomMetricStatsIter != mAtomMetricStats.end()) {
538 return atomMetricStatsIter->second;
539 }
540 auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
541 return emplaceResult.first->second;
542}
543
Yao Chenb3561512017-11-21 18:07:17 -0800544void StatsdStats::reset() {
545 lock_guard<std::mutex> lock(mLock);
546 resetInternalLocked();
547}
548
549void StatsdStats::resetInternalLocked() {
550 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800551 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800552 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800553 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800554 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800555 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700556 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700557 mLogLossStats.clear();
Yao Chen0f861862019-03-27 11:51:15 -0700558 mOverflowCount = 0;
559 mMinQueueHistoryNs = kInt64Max;
560 mMaxQueueHistoryNs = 0;
Yao Chen0fac5b12017-11-28 16:07:02 -0800561 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800562 config.second->broadcast_sent_time_sec.clear();
Tej Singh6ede28b2019-01-29 17:06:54 -0800563 config.second->activation_time_sec.clear();
564 config.second->deactivation_time_sec.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800565 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700566 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700567 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700568 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800569 config.second->matcher_stats.clear();
570 config.second->condition_stats.clear();
571 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700572 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800573 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800574 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700575 for (auto& pullStats : mPulledAtomStats) {
576 pullStats.second.totalPull = 0;
577 pullStats.second.totalPullFromCache = 0;
578 pullStats.second.avgPullTimeNs = 0;
579 pullStats.second.maxPullTimeNs = 0;
580 pullStats.second.numPullTime = 0;
581 pullStats.second.avgPullDelayNs = 0;
582 pullStats.second.maxPullDelayNs = 0;
583 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700584 pullStats.second.dataError = 0;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800585 pullStats.second.pullTimeout = 0;
586 pullStats.second.pullExceedMaxDelay = 0;
Tej Singha0c89dd2019-01-25 16:39:18 -0800587 pullStats.second.registeredCount = 0;
588 pullStats.second.unregisteredCount = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700589 }
Misha Wagner1eee2212019-01-22 11:47:11 +0000590 mAtomMetricStats.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800591}
592
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700593string buildTimeString(int64_t timeSec) {
594 time_t t = timeSec;
595 struct tm* tm = localtime(&t);
596 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700597 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700598 return string(timeBuffer);
599}
600
Yao Chena80e5c02018-09-04 13:55:29 -0700601void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800602 lock_guard<std::mutex> lock(mLock);
603 time_t t = mStartTimeSec;
604 struct tm* tm = localtime(&t);
605 char timeBuffer[80];
606 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700607 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
608 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800609 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700610 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700611 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800612 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800613 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700614 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700615 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
616 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800617
Yao Chen20e9e622018-02-28 11:18:51 -0800618 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700619 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800620 }
621
Tej Singh6ede28b2019-01-29 17:06:54 -0800622 for (const int& activationTime : configStats->activation_time_sec) {
623 dprintf(out, "\tactivation time: %d\n", activationTime);
624 }
625
626 for (const int& deactivationTime : configStats->deactivation_time_sec) {
627 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
628 }
629
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700630 auto dropTimePtr = configStats->data_drop_time_sec.begin();
631 auto dropBytesPtr = configStats->data_drop_bytes.begin();
632 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
633 i++, dropTimePtr++, dropBytesPtr++) {
634 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
635 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800636 }
637 }
Yao Chena80e5c02018-09-04 13:55:29 -0700638 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800639 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800640 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700641 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800642 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
643 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800644 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
645 configStats->deletion_time_sec, configStats->metric_count,
646 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
647 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700648 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700649 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700650 annotation.second);
651 }
652
Yao Chen20e9e622018-02-28 11:18:51 -0800653 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700654 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
655 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800656 }
657
Tej Singh6ede28b2019-01-29 17:06:54 -0800658 for (const int& activationTime : configStats->activation_time_sec) {
659 dprintf(out, "\tactivation time: %d\n", activationTime);
660 }
661
662 for (const int& deactivationTime : configStats->deactivation_time_sec) {
663 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
664 }
665
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700666 auto dropTimePtr = configStats->data_drop_time_sec.begin();
667 auto dropBytesPtr = configStats->data_drop_bytes.begin();
668 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
669 i++, dropTimePtr++, dropBytesPtr++) {
670 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
671 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
672 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800673 }
674
Yangster-mace68f3a52018-04-04 00:01:43 -0700675 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700676 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700677 buildTimeString(dump.first).c_str(), (long long)dump.first,
678 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800679 }
680
Yao Chen20e9e622018-02-28 11:18:51 -0800681 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700682 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800683 }
Yao Chen20e9e622018-02-28 11:18:51 -0800684
685 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700686 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800687 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800688 }
Yao Chen20e9e622018-02-28 11:18:51 -0800689
690 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700691 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800692 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800693 }
Yao Chen20e9e622018-02-28 11:18:51 -0800694
695 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700696 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800697 }
698 }
Yao Chena80e5c02018-09-04 13:55:29 -0700699 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700700 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700701 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800702 const size_t atomCounts = mPushedAtomStats.size();
703 for (size_t i = 2; i < atomCounts; i++) {
704 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700705 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800706 }
707 }
708
Yao Chena80e5c02018-09-04 13:55:29 -0700709 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800710 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700711 dprintf(out,
Rafal Slawik2065e5b2019-03-11 18:21:54 +0000712 "Atom %d->(total pull)%ld, (pull from cache)%ld, "
713 "(pull failed)%ld, (min pull interval)%ld \n"
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800714 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
715 "nanos)%lld, "
716 " (max pull delay nanos)%lld, (data error)%ld\n"
Tej Singha0c89dd2019-01-25 16:39:18 -0800717 " (pull timeout)%ld, (pull exceed max delay)%ld\n"
718 " (registered count) %ld, (unregistered count) %ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700719 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
Rafal Slawik2065e5b2019-03-11 18:21:54 +0000720 (long)pair.second.pullFailed, (long)pair.second.minPullIntervalSec,
721 (long long)pair.second.avgPullTimeNs, (long long)pair.second.maxPullTimeNs,
722 (long long)pair.second.avgPullDelayNs, (long long)pair.second.maxPullDelayNs,
723 pair.second.dataError, pair.second.pullTimeout, pair.second.pullExceedMaxDelay,
Tej Singha0c89dd2019-01-25 16:39:18 -0800724 pair.second.registeredCount, pair.second.unregisteredCount);
Yao Chenf5acabe2018-01-17 14:10:34 -0800725 }
726
727 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700728 dprintf(out, "********AnomalyAlarmStats stats***********\n");
729 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800730 }
731
Yangster-mac932ecec2018-02-01 10:23:52 -0800732 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700733 dprintf(out, "********SubscriberAlarmStats stats***********\n");
734 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800735 }
736
Yao Chena80e5c02018-09-04 13:55:29 -0700737 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700738 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
739 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800740
Yangster-mac892f3d32018-05-02 14:16:48 -0700741 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700742 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
743 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700744 }
745
Yao Chen0f5d6612018-08-22 14:11:51 -0700746 for (const auto& loss : mLogLossStats) {
Yao Chen49d7dd72019-03-26 14:02:11 -0700747 dprintf(out,
748 "Log loss: %lld (wall clock sec) - %d (count), %d (last error), %d (last tag), %d "
749 "(uid), %d (pid)\n",
750 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError, loss.mLastTag,
751 loss.mUid, loss.mPid);
Yangster-macb8382a12018-04-04 10:39:12 -0700752 }
Yao Chen0f861862019-03-27 11:51:15 -0700753
754 dprintf(out, "Event queue overflow: %d; MaxHistoryNs: %lld; MinHistoryNs: %lld\n",
755 mOverflowCount, (long long)mMaxQueueHistoryNs, (long long)mMinQueueHistoryNs);
Yao Chenf5acabe2018-01-17 14:10:34 -0800756}
757
Yao Chen20e9e622018-02-28 11:18:51 -0800758void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800759 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800760 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
761 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
762 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
763 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700764 if (configStats.reset_time_sec != 0) {
765 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
766 }
Yao Chen20e9e622018-02-28 11:18:51 -0800767 if (configStats.deletion_time_sec != 0) {
768 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
769 configStats.deletion_time_sec);
770 }
771 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
772 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
773 configStats.condition_count);
774 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
775 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
776 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
777
778 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
779 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
780 broadcast);
781 }
782
Tej Singh6ede28b2019-01-29 17:06:54 -0800783 for (const auto& activation : configStats.activation_time_sec) {
784 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ACTIVATION | FIELD_COUNT_REPEATED,
785 activation);
786 }
787
788 for (const auto& deactivation : configStats.deactivation_time_sec) {
789 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DEACTIVATION | FIELD_COUNT_REPEATED,
790 deactivation);
791 }
792
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700793 for (const auto& drop_time : configStats.data_drop_time_sec) {
794 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
795 drop_time);
796 }
797
798 for (const auto& drop_bytes : configStats.data_drop_bytes) {
799 proto->write(
800 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
801 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800802 }
803
Yangster-mace68f3a52018-04-04 00:01:43 -0700804 for (const auto& dump : configStats.dump_report_stats) {
805 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
806 FIELD_COUNT_REPEATED,
807 dump.first);
808 }
809
810 for (const auto& dump : configStats.dump_report_stats) {
811 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
812 FIELD_COUNT_REPEATED,
813 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800814 }
815
David Chenfaa1af52018-03-30 15:14:04 -0700816 for (const auto& annotation : configStats.annotations) {
817 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
818 FIELD_ID_CONFIG_STATS_ANNOTATION);
819 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
820 (long long)annotation.first);
821 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
822 proto->end(token);
823 }
824
Yao Chen20e9e622018-02-28 11:18:51 -0800825 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800826 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800827 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
828 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
829 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
830 proto->end(tmpToken);
831 }
832
833 for (const auto& pair : configStats.condition_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800834 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800835 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
836 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
837 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
838 proto->end(tmpToken);
839 }
840
841 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800842 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800843 FIELD_ID_CONFIG_STATS_METRIC_STATS);
844 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
845 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
846 proto->end(tmpToken);
847 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700848 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
849 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
850 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
851 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
852 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
853 proto->end(tmpToken);
854 }
Yao Chen20e9e622018-02-28 11:18:51 -0800855
856 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800857 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800858 FIELD_ID_CONFIG_STATS_ALERT_STATS);
859 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
860 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
861 proto->end(tmpToken);
862 }
863
864 proto->end(token);
865}
866
Yao Chen69f1baf2017-11-27 17:25:36 -0800867void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800868 lock_guard<std::mutex> lock(mLock);
869
Yao Chenb3561512017-11-21 18:07:17 -0800870 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800871 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800872 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800873
Yao Chenb3561512017-11-21 18:07:17 -0800874 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800875 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800876 }
877
878 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800879 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800880 }
881
Yao Chenb3561512017-11-21 18:07:17 -0800882 const size_t atomCounts = mPushedAtomStats.size();
883 for (size_t i = 2; i < atomCounts; i++) {
884 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800885 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800886 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
887 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
888 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
889 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800890 }
891 }
892
Chenjie Yub038b702017-12-18 15:15:34 -0800893 for (const auto& pair : mPulledAtomStats) {
894 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800895 }
896
Misha Wagner1eee2212019-01-22 11:47:11 +0000897 for (const auto& pair : mAtomMetricStats) {
898 android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
899 }
900
Bookatz1d0136d2017-12-01 11:13:32 -0800901 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800902 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800903 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
904 mAnomalyAlarmRegisteredStats);
905 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800906 }
907
Yangster-mac932ecec2018-02-01 10:23:52 -0800908 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700909 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
910 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800911 mPeriodicAlarmRegisteredStats);
912 proto.end(token);
913 }
914
Yi Jin5ee07872018-03-05 18:18:27 -0800915 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800916 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
917 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800918 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700919 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800920 proto.end(uidMapToken);
David Chenc136f452017-11-27 11:52:26 -0800921
Yao Chen0f5d6612018-08-22 14:11:51 -0700922 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800923 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800924 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800925 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
926 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
927 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen49d7dd72019-03-26 14:02:11 -0700928 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TAG, error.mLastTag);
929 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_UID, error.mUid);
930 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_PID, error.mPid);
Yao Chen884c8c12018-01-26 10:36:25 -0800931 proto.end(token);
932 }
933
Yao Chen0f861862019-03-27 11:51:15 -0700934 if (mOverflowCount > 0) {
935 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_OVERFLOW);
936 proto.write(FIELD_TYPE_INT32 | FIELD_ID_OVERFLOW_COUNT, (int32_t)mOverflowCount);
937 proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MAX_HISTORY,
938 (long long)mMaxQueueHistoryNs);
939 proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MIN_HISTORY,
940 (long long)mMinQueueHistoryNs);
941 proto.end(token);
942 }
943
Yangster-mac892f3d32018-05-02 14:16:48 -0700944 for (const auto& restart : mSystemServerRestartSec) {
945 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
946 restart);
947 }
948
Yao Chenb3561512017-11-21 18:07:17 -0800949 output->clear();
950 size_t bufferSize = proto.size();
951 output->resize(bufferSize);
952
953 size_t pos = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000954 sp<android::util::ProtoReader> reader = proto.data();
955 while (reader->readBuffer() != NULL) {
956 size_t toRead = reader->currentToRead();
957 std::memcpy(&((*output)[pos]), reader->readBuffer(), toRead);
Yao Chenb3561512017-11-21 18:07:17 -0800958 pos += toRead;
Joe Onorato99598ee2019-02-11 15:55:13 +0000959 reader->move(toRead);
Yao Chenb3561512017-11-21 18:07:17 -0800960 }
961
962 if (reset) {
963 resetInternalLocked();
964 }
965
966 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800967}
968
969} // namespace statsd
970} // namespace os
yro255f72e2018-02-26 15:15:17 -0800971} // namespace android