blob: 40329b7c86ab525af80909a96c04be469692e00e [file] [log] [blame]
Yao Chenb3561512017-11-21 18:07:17 -08001/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Tej Singh484524a2018-02-01 15:10:05 -080016#define DEBUG false // STOPSHIP if true
Yao Chenb3561512017-11-21 18:07:17 -080017#include "Log.h"
18
19#include "StatsdStats.h"
20
21#include <android/util/ProtoOutputStream.h>
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "../stats_log_util.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "statslog.h"
yro665208d2018-03-13 18:08:09 -070024#include "storage/StorageManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080025
26namespace android {
27namespace os {
28namespace statsd {
29
30using android::util::FIELD_COUNT_REPEATED;
31using android::util::FIELD_TYPE_BOOL;
32using android::util::FIELD_TYPE_FLOAT;
33using android::util::FIELD_TYPE_INT32;
34using android::util::FIELD_TYPE_INT64;
35using android::util::FIELD_TYPE_MESSAGE;
36using android::util::FIELD_TYPE_STRING;
37using android::util::ProtoOutputStream;
38using std::lock_guard;
39using std::map;
Yao Chen20e9e622018-02-28 11:18:51 -080040using std::shared_ptr;
Yao Chenb3561512017-11-21 18:07:17 -080041using std::string;
42using std::vector;
43
44const int FIELD_ID_BEGIN_TIME = 1;
45const int FIELD_ID_END_TIME = 2;
46const int FIELD_ID_CONFIG_STATS = 3;
Yao Chenb3561512017-11-21 18:07:17 -080047const int FIELD_ID_ATOM_STATS = 7;
David Chenc136f452017-11-27 11:52:26 -080048const int FIELD_ID_UIDMAP_STATS = 8;
Bookatz1d0136d2017-12-01 11:13:32 -080049const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
Yangster-macb8382a12018-04-04 10:39:12 -070050const int FIELD_ID_PERIODIC_ALARM_STATS = 12;
Yangster-mac892f3d32018-05-02 14:16:48 -070051const int FIELD_ID_SYSTEM_SERVER_RESTART = 15;
Yao Chen39b67992018-11-08 15:32:17 -080052const int FIELD_ID_LOGGER_ERROR_STATS = 16;
Yao Chenb3561512017-11-21 18:07:17 -080053
Yao Chenb3561512017-11-21 18:07:17 -080054const int FIELD_ID_ATOM_STATS_TAG = 1;
55const int FIELD_ID_ATOM_STATS_COUNT = 2;
56
Bookatz1d0136d2017-12-01 11:13:32 -080057const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
Yangster-macb8382a12018-04-04 10:39:12 -070058const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1;
Bookatz1d0136d2017-12-01 11:13:32 -080059
Yao Chen39b67992018-11-08 15:32:17 -080060const int FIELD_ID_LOG_LOSS_STATS_TIME = 1;
61const int FIELD_ID_LOG_LOSS_STATS_COUNT = 2;
62const int FIELD_ID_LOG_LOSS_STATS_ERROR = 3;
Yao Chen884c8c12018-01-26 10:36:25 -080063
Yao Chen20e9e622018-02-28 11:18:51 -080064const int FIELD_ID_CONFIG_STATS_UID = 1;
65const int FIELD_ID_CONFIG_STATS_ID = 2;
66const int FIELD_ID_CONFIG_STATS_CREATION = 3;
Tej Singh5260eea2018-04-04 17:27:14 -070067const int FIELD_ID_CONFIG_STATS_RESET = 19;
Yao Chen20e9e622018-02-28 11:18:51 -080068const int FIELD_ID_CONFIG_STATS_DELETION = 4;
69const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
70const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
71const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
72const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
73const int FIELD_ID_CONFIG_STATS_VALID = 9;
74const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
Chenjie Yuc3c30c02018-10-26 09:48:07 -070075const int FIELD_ID_CONFIG_STATS_DATA_DROP_TIME = 11;
76const int FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES = 21;
Yangster-mace68f3a52018-04-04 00:01:43 -070077const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12;
78const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20;
Yao Chen20e9e622018-02-28 11:18:51 -080079const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
80const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
81const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
82const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
Yangster-mac306ccc22018-03-24 15:03:40 -070083const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17;
David Chenfaa1af52018-03-30 15:14:04 -070084const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18;
85const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
86const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
Yao Chen20e9e622018-02-28 11:18:51 -080087
88const int FIELD_ID_MATCHER_STATS_ID = 1;
89const int FIELD_ID_MATCHER_STATS_COUNT = 2;
90const int FIELD_ID_CONDITION_STATS_ID = 1;
91const int FIELD_ID_CONDITION_STATS_COUNT = 2;
92const int FIELD_ID_METRIC_STATS_ID = 1;
93const int FIELD_ID_METRIC_STATS_COUNT = 2;
94const int FIELD_ID_ALERT_STATS_ID = 1;
95const int FIELD_ID_ALERT_STATS_COUNT = 2;
96
David Chenbd125272018-04-04 19:02:50 -070097const int FIELD_ID_UID_MAP_CHANGES = 1;
98const int FIELD_ID_UID_MAP_BYTES_USED = 2;
99const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
100const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
Yao Chen20e9e622018-02-28 11:18:51 -0800101
Chenjie Yuc5875052018-03-09 10:13:11 -0800102const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100103 {android::util::BINDER_CALLS, {6000, 10000}},
Olivier Gaillardc840ed92018-11-30 13:06:18 +0000104 {android::util::LOOPER_STATS, {1500, 2500}},
Chenjie Yuc5875052018-03-09 10:13:11 -0800105 {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
106};
107
Yao Chenb3561512017-11-21 18:07:17 -0800108StatsdStats::StatsdStats() {
109 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -0800110 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800111}
112
113StatsdStats& StatsdStats::getInstance() {
114 static StatsdStats statsInstance;
115 return statsInstance;
116}
117
Yao Chen20e9e622018-02-28 11:18:51 -0800118void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
Yao Chenf6723df2018-01-08 15:11:58 -0800119 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
120 if (mIceBox.size() == kMaxIceBoxSize) {
121 mIceBox.pop_front();
122 }
123 mIceBox.push_back(stats);
124}
125
David Chenfaa1af52018-03-30 15:14:04 -0700126void StatsdStats::noteConfigReceived(
127 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
128 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
129 bool isValid) {
Yao Chenb3561512017-11-21 18:07:17 -0800130 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -0800131 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800132
133 // If there is an existing config for the same key, icebox the old config.
134 noteConfigRemovedInternalLocked(key);
135
Yao Chen20e9e622018-02-28 11:18:51 -0800136 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
137 configStats->uid = key.GetUid();
138 configStats->id = key.GetId();
139 configStats->creation_time_sec = nowTimeSec;
140 configStats->metric_count = metricsCount;
141 configStats->condition_count = conditionsCount;
142 configStats->matcher_count = matchersCount;
143 configStats->alert_count = alertsCount;
144 configStats->is_valid = isValid;
David Chenfaa1af52018-03-30 15:14:04 -0700145 for (auto& v : annotations) {
146 configStats->annotations.emplace_back(v);
147 }
Yao Chenb3561512017-11-21 18:07:17 -0800148
149 if (isValid) {
150 mConfigStats[key] = configStats;
151 } else {
Yao Chen20e9e622018-02-28 11:18:51 -0800152 configStats->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800153 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800154 }
155}
156
157void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
158 auto it = mConfigStats.find(key);
159 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800160 int32_t nowTimeSec = getWallClockSec();
Yao Chen20e9e622018-02-28 11:18:51 -0800161 it->second->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800162 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800163 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800164 }
165}
166
167void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
168 lock_guard<std::mutex> lock(mLock);
169 noteConfigRemovedInternalLocked(key);
170}
171
Yangster-macb142cc82018-03-30 15:22:08 -0700172void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
173 auto it = mConfigStats.find(key);
174 if (it != mConfigStats.end()) {
175 it->second->reset_time_sec = getWallClockSec();
176 }
177}
178
179void StatsdStats::noteConfigReset(const ConfigKey& key) {
180 lock_guard<std::mutex> lock(mLock);
181 noteConfigResetInternalLocked(key);
182}
183
Yao Chen39b67992018-11-08 15:32:17 -0800184void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError) {
Yao Chen163d2602018-04-10 10:39:53 -0700185 lock_guard<std::mutex> lock(mLock);
Yao Chen0f5d6612018-08-22 14:11:51 -0700186 if (mLogLossStats.size() == kMaxLoggerErrors) {
187 mLogLossStats.pop_front();
Yao Chen163d2602018-04-10 10:39:53 -0700188 }
Yao Chen39b67992018-11-08 15:32:17 -0800189 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError);
Yao Chen163d2602018-04-10 10:39:53 -0700190}
191
Yao Chenb3561512017-11-21 18:07:17 -0800192void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800193 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800194}
195
196void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800197 lock_guard<std::mutex> lock(mLock);
198 auto it = mConfigStats.find(key);
199 if (it == mConfigStats.end()) {
200 ALOGE("Config key %s not found!", key.ToString().c_str());
201 return;
202 }
Yao Chen20e9e622018-02-28 11:18:51 -0800203 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
204 it->second->broadcast_sent_time_sec.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800205 }
Yao Chen20e9e622018-02-28 11:18:51 -0800206 it->second->broadcast_sent_time_sec.push_back(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800207}
208
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700209void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
210 noteDataDropped(key, totalBytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800211}
212
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700213void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800214 lock_guard<std::mutex> lock(mLock);
215 auto it = mConfigStats.find(key);
216 if (it == mConfigStats.end()) {
217 ALOGE("Config key %s not found!", key.ToString().c_str());
218 return;
219 }
Yao Chen20e9e622018-02-28 11:18:51 -0800220 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
221 it->second->data_drop_time_sec.pop_front();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700222 it->second->data_drop_bytes.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800223 }
Yao Chen20e9e622018-02-28 11:18:51 -0800224 it->second->data_drop_time_sec.push_back(timeSec);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700225 it->second->data_drop_bytes.push_back(totalBytes);
Yao Chenb3561512017-11-21 18:07:17 -0800226}
227
Yangster-mace68f3a52018-04-04 00:01:43 -0700228void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
229 noteMetricsReportSent(key, num_bytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800230}
231
Yangster-mace68f3a52018-04-04 00:01:43 -0700232void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
233 int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800234 lock_guard<std::mutex> lock(mLock);
235 auto it = mConfigStats.find(key);
236 if (it == mConfigStats.end()) {
237 ALOGE("Config key %s not found!", key.ToString().c_str());
238 return;
239 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700240 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
241 it->second->dump_report_stats.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800242 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700243 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
Yao Chen69f1baf2017-11-27 17:25:36 -0800244}
245
David Chenbd125272018-04-04 19:02:50 -0700246void StatsdStats::noteUidMapDropped(int deltas) {
David Chenc136f452017-11-27 11:52:26 -0800247 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800248 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
David Chenc136f452017-11-27 11:52:26 -0800249}
250
David Chenbd125272018-04-04 19:02:50 -0700251void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f452017-11-27 11:52:26 -0800252 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700253 mUidMapStats.deleted_apps++;
David Chenc136f452017-11-27 11:52:26 -0800254}
255
256void StatsdStats::setUidMapChanges(int changes) {
257 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800258 mUidMapStats.changes = changes;
David Chenc136f452017-11-27 11:52:26 -0800259}
260
261void StatsdStats::setCurrentUidMapMemory(int bytes) {
262 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800263 mUidMapStats.bytes_used = bytes;
David Chenc136f452017-11-27 11:52:26 -0800264}
265
Yangster-mac94e197c2018-01-02 16:03:03 -0800266void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800267 lock_guard<std::mutex> lock(mLock);
268 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800269 auto statsIt = mConfigStats.find(key);
270 if (statsIt == mConfigStats.end()) {
271 return;
272 }
273
274 auto& conditionSizeMap = statsIt->second->condition_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800275 if (size > conditionSizeMap[id]) {
276 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800277 }
278}
279
Yangster-mac94e197c2018-01-02 16:03:03 -0800280void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800281 lock_guard<std::mutex> lock(mLock);
282 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800283 auto statsIt = mConfigStats.find(key);
284 if (statsIt == mConfigStats.end()) {
285 return;
286 }
287 auto& metricsDimensionMap = statsIt->second->metric_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800288 if (size > metricsDimensionMap[id]) {
289 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800290 }
291}
292
Yangster-mac306ccc22018-03-24 15:03:40 -0700293void StatsdStats::noteMetricDimensionInConditionSize(
294 const ConfigKey& key, const int64_t& id, int size) {
295 lock_guard<std::mutex> lock(mLock);
296 // if name doesn't exist before, it will create the key with count 0.
297 auto statsIt = mConfigStats.find(key);
298 if (statsIt == mConfigStats.end()) {
299 return;
300 }
301 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
302 if (size > metricsDimensionMap[id]) {
303 metricsDimensionMap[id] = size;
304 }
305}
306
Yangster-mac94e197c2018-01-02 16:03:03 -0800307void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800308 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800309
310 auto statsIt = mConfigStats.find(key);
311 if (statsIt == mConfigStats.end()) {
312 return;
313 }
314 statsIt->second->matcher_stats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800315}
316
Yangster-mac94e197c2018-01-02 16:03:03 -0800317void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800318 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800319 auto statsIt = mConfigStats.find(key);
320 if (statsIt == mConfigStats.end()) {
321 return;
322 }
323 statsIt->second->alert_stats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800324}
325
Bookatz1d0136d2017-12-01 11:13:32 -0800326void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
327 lock_guard<std::mutex> lock(mLock);
328 mAnomalyAlarmRegisteredStats++;
329}
330
Yangster-mac932ecec2018-02-01 10:23:52 -0800331void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
332 lock_guard<std::mutex> lock(mLock);
333 mPeriodicAlarmRegisteredStats++;
334}
335
Chenjie Yub038b702017-12-18 15:15:34 -0800336void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
337 lock_guard<std::mutex> lock(mLock);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700338 mPulledAtomStats[pullAtomId].minPullIntervalSec =
339 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
Chenjie Yub038b702017-12-18 15:15:34 -0800340}
341
342void StatsdStats::notePull(int pullAtomId) {
343 lock_guard<std::mutex> lock(mLock);
344 mPulledAtomStats[pullAtomId].totalPull++;
345}
346
347void StatsdStats::notePullFromCache(int pullAtomId) {
348 lock_guard<std::mutex> lock(mLock);
349 mPulledAtomStats[pullAtomId].totalPullFromCache++;
350}
351
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700352void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
353 lock_guard<std::mutex> lock(mLock);
354 auto& pullStats = mPulledAtomStats[pullAtomId];
355 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
356 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
357 (pullStats.numPullTime + 1);
358 pullStats.numPullTime += 1;
359}
360
361void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
362 lock_guard<std::mutex> lock(mLock);
363 auto& pullStats = mPulledAtomStats[pullAtomId];
364 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700365 pullStats.avgPullDelayNs =
366 (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
367 (pullStats.numPullDelay + 1);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700368 pullStats.numPullDelay += 1;
369}
370
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700371void StatsdStats::notePullDataError(int pullAtomId) {
372 lock_guard<std::mutex> lock(mLock);
373 mPulledAtomStats[pullAtomId].dataError++;
374}
375
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800376void StatsdStats::notePullTimeout(int pullAtomId) {
377 lock_guard<std::mutex> lock(mLock);
378 mPulledAtomStats[pullAtomId].pullTimeout++;
379}
380
381void StatsdStats::notePullExceedMaxDelay(int pullAtomId) {
382 lock_guard<std::mutex> lock(mLock);
383 mPulledAtomStats[pullAtomId].pullExceedMaxDelay++;
384}
385
Yao Chenb3561512017-11-21 18:07:17 -0800386void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
387 lock_guard<std::mutex> lock(mLock);
388
Yao Chenb3561512017-11-21 18:07:17 -0800389 if (atomId > android::util::kMaxPushedAtomId) {
390 ALOGW("not interested in atom %d", atomId);
391 return;
392 }
393
394 mPushedAtomStats[atomId]++;
395}
396
Yangster-mac892f3d32018-05-02 14:16:48 -0700397void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
398 lock_guard<std::mutex> lock(mLock);
399
400 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
401 mSystemServerRestartSec.pop_front();
402 }
403 mSystemServerRestartSec.push_back(timeSec);
404}
405
Misha Wagner1eee2212019-01-22 11:47:11 +0000406void StatsdStats::notePullFailed(int atomId) {
407 lock_guard<std::mutex> lock(mLock);
408 mPulledAtomStats[atomId].pullFailed++;
409}
410
411void StatsdStats::noteStatsCompanionPullFailed(int atomId) {
412 lock_guard<std::mutex> lock(mLock);
413 mPulledAtomStats[atomId].statsCompanionPullFailed++;
414}
415
416void StatsdStats::noteStatsCompanionPullBinderTransactionFailed(int atomId) {
417 lock_guard<std::mutex> lock(mLock);
418 mPulledAtomStats[atomId].statsCompanionPullBinderTransactionFailed++;
419}
420
421void StatsdStats::noteEmptyData(int atomId) {
422 lock_guard<std::mutex> lock(mLock);
423 mPulledAtomStats[atomId].emptyData++;
424}
425
Tej Singha0c89dd2019-01-25 16:39:18 -0800426void StatsdStats::notePullerCallbackRegistrationChanged(int atomId, bool registered) {
427 lock_guard<std::mutex> lock(mLock);
428 if (registered) {
429 mPulledAtomStats[atomId].registeredCount++;
430 } else {
431 mPulledAtomStats[atomId].unregisteredCount++;
432 }
433}
434
Olivier Gaillard2310b192019-02-08 10:23:05 +0000435void StatsdStats::noteHardDimensionLimitReached(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000436 lock_guard<std::mutex> lock(mLock);
437 getAtomMetricStats(metricId).hardDimensionLimitReached++;
438}
439
Olivier Gaillard2310b192019-02-08 10:23:05 +0000440void StatsdStats::noteLateLogEventSkipped(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000441 lock_guard<std::mutex> lock(mLock);
442 getAtomMetricStats(metricId).lateLogEventSkipped++;
443}
444
Olivier Gaillard2310b192019-02-08 10:23:05 +0000445void StatsdStats::noteSkippedForwardBuckets(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000446 lock_guard<std::mutex> lock(mLock);
447 getAtomMetricStats(metricId).skippedForwardBuckets++;
448}
449
Olivier Gaillard2310b192019-02-08 10:23:05 +0000450void StatsdStats::noteBadValueType(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000451 lock_guard<std::mutex> lock(mLock);
452 getAtomMetricStats(metricId).badValueType++;
453}
454
Olivier Gaillard2310b192019-02-08 10:23:05 +0000455void StatsdStats::noteBucketDropped(int64_t metricId) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000456 lock_guard<std::mutex> lock(mLock);
457 getAtomMetricStats(metricId).bucketDropped++;
458}
459
Olivier Gaillard2310b192019-02-08 10:23:05 +0000460void StatsdStats::noteConditionChangeInNextBucket(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000461 lock_guard<std::mutex> lock(mLock);
462 getAtomMetricStats(metricId).conditionChangeInNextBucket++;
463}
464
Olivier Gaillard2310b192019-02-08 10:23:05 +0000465void StatsdStats::noteInvalidatedBucket(int64_t metricId) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000466 lock_guard<std::mutex> lock(mLock);
467 getAtomMetricStats(metricId).invalidatedBucket++;
468}
469
Olivier Gaillard3ba2e932019-02-08 14:25:35 +0000470void StatsdStats::noteBucketBoundaryDelayNs(int64_t metricId, int64_t timeDelayNs) {
471 lock_guard<std::mutex> lock(mLock);
472 AtomMetricStats& pullStats = getAtomMetricStats(metricId);
473 pullStats.maxBucketBoundaryDelayNs =
474 std::max(pullStats.maxBucketBoundaryDelayNs, timeDelayNs);
475 pullStats.minBucketBoundaryDelayNs =
476 std::min(pullStats.minBucketBoundaryDelayNs, timeDelayNs);
477}
478
Misha Wagner1eee2212019-01-22 11:47:11 +0000479StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int metricId) {
480 auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
481 if (atomMetricStatsIter != mAtomMetricStats.end()) {
482 return atomMetricStatsIter->second;
483 }
484 auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
485 return emplaceResult.first->second;
486}
487
Yao Chenb3561512017-11-21 18:07:17 -0800488void StatsdStats::reset() {
489 lock_guard<std::mutex> lock(mLock);
490 resetInternalLocked();
491}
492
493void StatsdStats::resetInternalLocked() {
494 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800495 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800496 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800497 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800498 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800499 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700500 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700501 mLogLossStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800502 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800503 config.second->broadcast_sent_time_sec.clear();
504 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700505 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700506 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700507 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800508 config.second->matcher_stats.clear();
509 config.second->condition_stats.clear();
510 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700511 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800512 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800513 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700514 for (auto& pullStats : mPulledAtomStats) {
515 pullStats.second.totalPull = 0;
516 pullStats.second.totalPullFromCache = 0;
517 pullStats.second.avgPullTimeNs = 0;
518 pullStats.second.maxPullTimeNs = 0;
519 pullStats.second.numPullTime = 0;
520 pullStats.second.avgPullDelayNs = 0;
521 pullStats.second.maxPullDelayNs = 0;
522 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700523 pullStats.second.dataError = 0;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800524 pullStats.second.pullTimeout = 0;
525 pullStats.second.pullExceedMaxDelay = 0;
Tej Singha0c89dd2019-01-25 16:39:18 -0800526 pullStats.second.registeredCount = 0;
527 pullStats.second.unregisteredCount = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700528 }
Misha Wagner1eee2212019-01-22 11:47:11 +0000529 mAtomMetricStats.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800530}
531
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700532string buildTimeString(int64_t timeSec) {
533 time_t t = timeSec;
534 struct tm* tm = localtime(&t);
535 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700536 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700537 return string(timeBuffer);
538}
539
Yao Chena80e5c02018-09-04 13:55:29 -0700540void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800541 lock_guard<std::mutex> lock(mLock);
542 time_t t = mStartTimeSec;
543 struct tm* tm = localtime(&t);
544 char timeBuffer[80];
545 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700546 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
547 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800548 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700549 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700550 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800551 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800552 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700553 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700554 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
555 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800556
Yao Chen20e9e622018-02-28 11:18:51 -0800557 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700558 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800559 }
560
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700561 auto dropTimePtr = configStats->data_drop_time_sec.begin();
562 auto dropBytesPtr = configStats->data_drop_bytes.begin();
563 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
564 i++, dropTimePtr++, dropBytesPtr++) {
565 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
566 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800567 }
568 }
Yao Chena80e5c02018-09-04 13:55:29 -0700569 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800570 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800571 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700572 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800573 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
574 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800575 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
576 configStats->deletion_time_sec, configStats->metric_count,
577 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
578 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700579 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700580 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700581 annotation.second);
582 }
583
Yao Chen20e9e622018-02-28 11:18:51 -0800584 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700585 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
586 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800587 }
588
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700589 auto dropTimePtr = configStats->data_drop_time_sec.begin();
590 auto dropBytesPtr = configStats->data_drop_bytes.begin();
591 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
592 i++, dropTimePtr++, dropBytesPtr++) {
593 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
594 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
595 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800596 }
597
Yangster-mace68f3a52018-04-04 00:01:43 -0700598 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700599 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700600 buildTimeString(dump.first).c_str(), (long long)dump.first,
601 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800602 }
603
Yao Chen20e9e622018-02-28 11:18:51 -0800604 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700605 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800606 }
Yao Chen20e9e622018-02-28 11:18:51 -0800607
608 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700609 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800610 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800611 }
Yao Chen20e9e622018-02-28 11:18:51 -0800612
613 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700614 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800615 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800616 }
Yao Chen20e9e622018-02-28 11:18:51 -0800617
618 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700619 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800620 }
621 }
Yao Chena80e5c02018-09-04 13:55:29 -0700622 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700623 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700624 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800625 const size_t atomCounts = mPushedAtomStats.size();
626 for (size_t i = 2; i < atomCounts; i++) {
627 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700628 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800629 }
630 }
631
Yao Chena80e5c02018-09-04 13:55:29 -0700632 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800633 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700634 dprintf(out,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800635 "Atom %d->(total pull)%ld, (pull from cache)%ld, (min pull interval)%ld \n"
636 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
637 "nanos)%lld, "
638 " (max pull delay nanos)%lld, (data error)%ld\n"
Tej Singha0c89dd2019-01-25 16:39:18 -0800639 " (pull timeout)%ld, (pull exceed max delay)%ld\n"
640 " (registered count) %ld, (unregistered count) %ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700641 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
642 (long)pair.second.minPullIntervalSec, (long long)pair.second.avgPullTimeNs,
643 (long long)pair.second.maxPullTimeNs, (long long)pair.second.avgPullDelayNs,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800644 (long long)pair.second.maxPullDelayNs, pair.second.dataError,
Tej Singha0c89dd2019-01-25 16:39:18 -0800645 pair.second.pullTimeout, pair.second.pullExceedMaxDelay,
646 pair.second.registeredCount, pair.second.unregisteredCount);
Yao Chenf5acabe2018-01-17 14:10:34 -0800647 }
648
649 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700650 dprintf(out, "********AnomalyAlarmStats stats***********\n");
651 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800652 }
653
Yangster-mac932ecec2018-02-01 10:23:52 -0800654 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700655 dprintf(out, "********SubscriberAlarmStats stats***********\n");
656 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800657 }
658
Yao Chena80e5c02018-09-04 13:55:29 -0700659 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700660 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
661 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800662
Yangster-mac892f3d32018-05-02 14:16:48 -0700663 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700664 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
665 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700666 }
667
Yao Chen0f5d6612018-08-22 14:11:51 -0700668 for (const auto& loss : mLogLossStats) {
Yao Chen39b67992018-11-08 15:32:17 -0800669 dprintf(out, "Log loss: %lld (wall clock sec) - %d (count) %d (last error)\n",
670 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError);
Yangster-macb8382a12018-04-04 10:39:12 -0700671 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800672}
673
Yao Chen20e9e622018-02-28 11:18:51 -0800674void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800675 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800676 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
677 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
678 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
679 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700680 if (configStats.reset_time_sec != 0) {
681 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
682 }
Yao Chen20e9e622018-02-28 11:18:51 -0800683 if (configStats.deletion_time_sec != 0) {
684 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
685 configStats.deletion_time_sec);
686 }
687 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
688 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
689 configStats.condition_count);
690 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
691 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
692 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
693
694 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
695 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
696 broadcast);
697 }
698
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700699 for (const auto& drop_time : configStats.data_drop_time_sec) {
700 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
701 drop_time);
702 }
703
704 for (const auto& drop_bytes : configStats.data_drop_bytes) {
705 proto->write(
706 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
707 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800708 }
709
Yangster-mace68f3a52018-04-04 00:01:43 -0700710 for (const auto& dump : configStats.dump_report_stats) {
711 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
712 FIELD_COUNT_REPEATED,
713 dump.first);
714 }
715
716 for (const auto& dump : configStats.dump_report_stats) {
717 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
718 FIELD_COUNT_REPEATED,
719 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800720 }
721
David Chenfaa1af52018-03-30 15:14:04 -0700722 for (const auto& annotation : configStats.annotations) {
723 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
724 FIELD_ID_CONFIG_STATS_ANNOTATION);
725 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
726 (long long)annotation.first);
727 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
728 proto->end(token);
729 }
730
Yao Chen20e9e622018-02-28 11:18:51 -0800731 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800732 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800733 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
734 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
735 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
736 proto->end(tmpToken);
737 }
738
739 for (const auto& pair : configStats.condition_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800740 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800741 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
742 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
743 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
744 proto->end(tmpToken);
745 }
746
747 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800748 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800749 FIELD_ID_CONFIG_STATS_METRIC_STATS);
750 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
751 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
752 proto->end(tmpToken);
753 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700754 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
755 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
756 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
757 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
758 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
759 proto->end(tmpToken);
760 }
Yao Chen20e9e622018-02-28 11:18:51 -0800761
762 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800763 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800764 FIELD_ID_CONFIG_STATS_ALERT_STATS);
765 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
766 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
767 proto->end(tmpToken);
768 }
769
770 proto->end(token);
771}
772
Yao Chen69f1baf2017-11-27 17:25:36 -0800773void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800774 lock_guard<std::mutex> lock(mLock);
775
Yao Chenb3561512017-11-21 18:07:17 -0800776 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800777 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800778 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800779
Yao Chenb3561512017-11-21 18:07:17 -0800780 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800781 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800782 }
783
784 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800785 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800786 }
787
Yao Chenb3561512017-11-21 18:07:17 -0800788 const size_t atomCounts = mPushedAtomStats.size();
789 for (size_t i = 2; i < atomCounts; i++) {
790 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800791 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800792 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
793 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
794 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
795 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800796 }
797 }
798
Chenjie Yub038b702017-12-18 15:15:34 -0800799 for (const auto& pair : mPulledAtomStats) {
800 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800801 }
802
Misha Wagner1eee2212019-01-22 11:47:11 +0000803 for (const auto& pair : mAtomMetricStats) {
804 android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
805 }
806
Bookatz1d0136d2017-12-01 11:13:32 -0800807 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800808 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800809 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
810 mAnomalyAlarmRegisteredStats);
811 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800812 }
813
Yangster-mac932ecec2018-02-01 10:23:52 -0800814 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700815 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
816 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800817 mPeriodicAlarmRegisteredStats);
818 proto.end(token);
819 }
820
Yi Jin5ee07872018-03-05 18:18:27 -0800821 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800822 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
823 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800824 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700825 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800826 proto.end(uidMapToken);
David Chenc136f452017-11-27 11:52:26 -0800827
Yao Chen0f5d6612018-08-22 14:11:51 -0700828 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800829 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800830 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800831 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
832 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
833 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen884c8c12018-01-26 10:36:25 -0800834 proto.end(token);
835 }
836
Yangster-mac892f3d32018-05-02 14:16:48 -0700837 for (const auto& restart : mSystemServerRestartSec) {
838 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
839 restart);
840 }
841
Yao Chenb3561512017-11-21 18:07:17 -0800842 output->clear();
843 size_t bufferSize = proto.size();
844 output->resize(bufferSize);
845
846 size_t pos = 0;
847 auto it = proto.data();
848 while (it.readBuffer() != NULL) {
849 size_t toRead = it.currentToRead();
850 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
851 pos += toRead;
852 it.rp()->move(toRead);
853 }
854
855 if (reset) {
856 resetInternalLocked();
857 }
858
859 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800860}
861
862} // namespace statsd
863} // namespace os
yro255f72e2018-02-26 15:15:17 -0800864} // namespace android