blob: 37ccad5f4a49ee7f6f7050738a0b6e19f08b1598 [file] [log] [blame]
Yao Chenb3561512017-11-21 18:07:17 -08001/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Tej Singh484524a2018-02-01 15:10:05 -080016#define DEBUG false // STOPSHIP if true
Yao Chenb3561512017-11-21 18:07:17 -080017#include "Log.h"
18
19#include "StatsdStats.h"
20
21#include <android/util/ProtoOutputStream.h>
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "../stats_log_util.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "statslog.h"
yro665208d2018-03-13 18:08:09 -070024#include "storage/StorageManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080025
26namespace android {
27namespace os {
28namespace statsd {
29
30using android::util::FIELD_COUNT_REPEATED;
31using android::util::FIELD_TYPE_BOOL;
32using android::util::FIELD_TYPE_FLOAT;
33using android::util::FIELD_TYPE_INT32;
34using android::util::FIELD_TYPE_INT64;
35using android::util::FIELD_TYPE_MESSAGE;
36using android::util::FIELD_TYPE_STRING;
37using android::util::ProtoOutputStream;
38using std::lock_guard;
39using std::map;
Yao Chen20e9e622018-02-28 11:18:51 -080040using std::shared_ptr;
Yao Chenb3561512017-11-21 18:07:17 -080041using std::string;
42using std::vector;
43
44const int FIELD_ID_BEGIN_TIME = 1;
45const int FIELD_ID_END_TIME = 2;
46const int FIELD_ID_CONFIG_STATS = 3;
Yao Chenb3561512017-11-21 18:07:17 -080047const int FIELD_ID_ATOM_STATS = 7;
David Chenc136f45a2017-11-27 11:52:26 -080048const int FIELD_ID_UIDMAP_STATS = 8;
Bookatz1d0136d2017-12-01 11:13:32 -080049const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
Yangster-macb8382a12018-04-04 10:39:12 -070050const int FIELD_ID_PERIODIC_ALARM_STATS = 12;
Yangster-mac892f3d32018-05-02 14:16:48 -070051const int FIELD_ID_SYSTEM_SERVER_RESTART = 15;
Yao Chen39b67992018-11-08 15:32:17 -080052const int FIELD_ID_LOGGER_ERROR_STATS = 16;
Yao Chenb3561512017-11-21 18:07:17 -080053
Yao Chenb3561512017-11-21 18:07:17 -080054const int FIELD_ID_ATOM_STATS_TAG = 1;
55const int FIELD_ID_ATOM_STATS_COUNT = 2;
56
Bookatz1d0136d2017-12-01 11:13:32 -080057const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
Yangster-macb8382a12018-04-04 10:39:12 -070058const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1;
Bookatz1d0136d2017-12-01 11:13:32 -080059
Yao Chen39b67992018-11-08 15:32:17 -080060const int FIELD_ID_LOG_LOSS_STATS_TIME = 1;
61const int FIELD_ID_LOG_LOSS_STATS_COUNT = 2;
62const int FIELD_ID_LOG_LOSS_STATS_ERROR = 3;
Yao Chen884c8c12018-01-26 10:36:25 -080063
Yao Chen20e9e622018-02-28 11:18:51 -080064const int FIELD_ID_CONFIG_STATS_UID = 1;
65const int FIELD_ID_CONFIG_STATS_ID = 2;
66const int FIELD_ID_CONFIG_STATS_CREATION = 3;
Tej Singh5260eea2018-04-04 17:27:14 -070067const int FIELD_ID_CONFIG_STATS_RESET = 19;
Yao Chen20e9e622018-02-28 11:18:51 -080068const int FIELD_ID_CONFIG_STATS_DELETION = 4;
69const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
70const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
71const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
72const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
73const int FIELD_ID_CONFIG_STATS_VALID = 9;
74const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
Chenjie Yuc3c30c02018-10-26 09:48:07 -070075const int FIELD_ID_CONFIG_STATS_DATA_DROP_TIME = 11;
76const int FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES = 21;
Yangster-mace68f3a52018-04-04 00:01:43 -070077const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12;
78const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20;
Yao Chen20e9e622018-02-28 11:18:51 -080079const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
80const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
81const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
82const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
Yangster-mac306ccc22018-03-24 15:03:40 -070083const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17;
David Chenfaa1af52018-03-30 15:14:04 -070084const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18;
85const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
86const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
Yao Chen20e9e622018-02-28 11:18:51 -080087
88const int FIELD_ID_MATCHER_STATS_ID = 1;
89const int FIELD_ID_MATCHER_STATS_COUNT = 2;
90const int FIELD_ID_CONDITION_STATS_ID = 1;
91const int FIELD_ID_CONDITION_STATS_COUNT = 2;
92const int FIELD_ID_METRIC_STATS_ID = 1;
93const int FIELD_ID_METRIC_STATS_COUNT = 2;
94const int FIELD_ID_ALERT_STATS_ID = 1;
95const int FIELD_ID_ALERT_STATS_COUNT = 2;
96
David Chenbd125272018-04-04 19:02:50 -070097const int FIELD_ID_UID_MAP_CHANGES = 1;
98const int FIELD_ID_UID_MAP_BYTES_USED = 2;
99const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
100const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
Yao Chen20e9e622018-02-28 11:18:51 -0800101
Chenjie Yuc5875052018-03-09 10:13:11 -0800102const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100103 {android::util::BINDER_CALLS, {6000, 10000}},
Olivier Gaillardc840ed92018-11-30 13:06:18 +0000104 {android::util::LOOPER_STATS, {1500, 2500}},
Chenjie Yuc5875052018-03-09 10:13:11 -0800105 {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
106};
107
Yao Chenb3561512017-11-21 18:07:17 -0800108StatsdStats::StatsdStats() {
109 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -0800110 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800111}
112
113StatsdStats& StatsdStats::getInstance() {
114 static StatsdStats statsInstance;
115 return statsInstance;
116}
117
Yao Chen20e9e622018-02-28 11:18:51 -0800118void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
Yao Chenf6723df2018-01-08 15:11:58 -0800119 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
120 if (mIceBox.size() == kMaxIceBoxSize) {
121 mIceBox.pop_front();
122 }
123 mIceBox.push_back(stats);
124}
125
David Chenfaa1af52018-03-30 15:14:04 -0700126void StatsdStats::noteConfigReceived(
127 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
128 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
129 bool isValid) {
Yao Chenb3561512017-11-21 18:07:17 -0800130 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -0800131 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800132
133 // If there is an existing config for the same key, icebox the old config.
134 noteConfigRemovedInternalLocked(key);
135
Yao Chen20e9e622018-02-28 11:18:51 -0800136 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
137 configStats->uid = key.GetUid();
138 configStats->id = key.GetId();
139 configStats->creation_time_sec = nowTimeSec;
140 configStats->metric_count = metricsCount;
141 configStats->condition_count = conditionsCount;
142 configStats->matcher_count = matchersCount;
143 configStats->alert_count = alertsCount;
144 configStats->is_valid = isValid;
David Chenfaa1af52018-03-30 15:14:04 -0700145 for (auto& v : annotations) {
146 configStats->annotations.emplace_back(v);
147 }
Yao Chenb3561512017-11-21 18:07:17 -0800148
149 if (isValid) {
150 mConfigStats[key] = configStats;
151 } else {
Yao Chen20e9e622018-02-28 11:18:51 -0800152 configStats->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800153 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800154 }
155}
156
157void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
158 auto it = mConfigStats.find(key);
159 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800160 int32_t nowTimeSec = getWallClockSec();
Yao Chen20e9e622018-02-28 11:18:51 -0800161 it->second->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800162 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800163 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800164 }
165}
166
167void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
168 lock_guard<std::mutex> lock(mLock);
169 noteConfigRemovedInternalLocked(key);
170}
171
Yangster-macb142cc82018-03-30 15:22:08 -0700172void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
173 auto it = mConfigStats.find(key);
174 if (it != mConfigStats.end()) {
175 it->second->reset_time_sec = getWallClockSec();
176 }
177}
178
179void StatsdStats::noteConfigReset(const ConfigKey& key) {
180 lock_guard<std::mutex> lock(mLock);
181 noteConfigResetInternalLocked(key);
182}
183
Yao Chen39b67992018-11-08 15:32:17 -0800184void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError) {
Yao Chen163d2602018-04-10 10:39:53 -0700185 lock_guard<std::mutex> lock(mLock);
Yao Chen0f5d6612018-08-22 14:11:51 -0700186 if (mLogLossStats.size() == kMaxLoggerErrors) {
187 mLogLossStats.pop_front();
Yao Chen163d2602018-04-10 10:39:53 -0700188 }
Yao Chen39b67992018-11-08 15:32:17 -0800189 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError);
Yao Chen163d2602018-04-10 10:39:53 -0700190}
191
Yao Chenb3561512017-11-21 18:07:17 -0800192void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800193 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800194}
195
196void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800197 lock_guard<std::mutex> lock(mLock);
198 auto it = mConfigStats.find(key);
199 if (it == mConfigStats.end()) {
200 ALOGE("Config key %s not found!", key.ToString().c_str());
201 return;
202 }
Yao Chen20e9e622018-02-28 11:18:51 -0800203 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
204 it->second->broadcast_sent_time_sec.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800205 }
Yao Chen20e9e622018-02-28 11:18:51 -0800206 it->second->broadcast_sent_time_sec.push_back(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800207}
208
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700209void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
210 noteDataDropped(key, totalBytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800211}
212
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700213void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800214 lock_guard<std::mutex> lock(mLock);
215 auto it = mConfigStats.find(key);
216 if (it == mConfigStats.end()) {
217 ALOGE("Config key %s not found!", key.ToString().c_str());
218 return;
219 }
Yao Chen20e9e622018-02-28 11:18:51 -0800220 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
221 it->second->data_drop_time_sec.pop_front();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700222 it->second->data_drop_bytes.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800223 }
Yao Chen20e9e622018-02-28 11:18:51 -0800224 it->second->data_drop_time_sec.push_back(timeSec);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700225 it->second->data_drop_bytes.push_back(totalBytes);
Yao Chenb3561512017-11-21 18:07:17 -0800226}
227
Yangster-mace68f3a52018-04-04 00:01:43 -0700228void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
229 noteMetricsReportSent(key, num_bytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800230}
231
Yangster-mace68f3a52018-04-04 00:01:43 -0700232void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
233 int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800234 lock_guard<std::mutex> lock(mLock);
235 auto it = mConfigStats.find(key);
236 if (it == mConfigStats.end()) {
237 ALOGE("Config key %s not found!", key.ToString().c_str());
238 return;
239 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700240 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
241 it->second->dump_report_stats.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800242 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700243 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
Yao Chen69f1baf2017-11-27 17:25:36 -0800244}
245
David Chenbd125272018-04-04 19:02:50 -0700246void StatsdStats::noteUidMapDropped(int deltas) {
David Chenc136f45a2017-11-27 11:52:26 -0800247 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800248 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
David Chenc136f45a2017-11-27 11:52:26 -0800249}
250
David Chenbd125272018-04-04 19:02:50 -0700251void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f45a2017-11-27 11:52:26 -0800252 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700253 mUidMapStats.deleted_apps++;
David Chenc136f45a2017-11-27 11:52:26 -0800254}
255
256void StatsdStats::setUidMapChanges(int changes) {
257 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800258 mUidMapStats.changes = changes;
David Chenc136f45a2017-11-27 11:52:26 -0800259}
260
261void StatsdStats::setCurrentUidMapMemory(int bytes) {
262 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800263 mUidMapStats.bytes_used = bytes;
David Chenc136f45a2017-11-27 11:52:26 -0800264}
265
Yangster-mac94e197c2018-01-02 16:03:03 -0800266void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800267 lock_guard<std::mutex> lock(mLock);
268 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800269 auto statsIt = mConfigStats.find(key);
270 if (statsIt == mConfigStats.end()) {
271 return;
272 }
273
274 auto& conditionSizeMap = statsIt->second->condition_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800275 if (size > conditionSizeMap[id]) {
276 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800277 }
278}
279
Yangster-mac94e197c2018-01-02 16:03:03 -0800280void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800281 lock_guard<std::mutex> lock(mLock);
282 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800283 auto statsIt = mConfigStats.find(key);
284 if (statsIt == mConfigStats.end()) {
285 return;
286 }
287 auto& metricsDimensionMap = statsIt->second->metric_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800288 if (size > metricsDimensionMap[id]) {
289 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800290 }
291}
292
Yangster-mac306ccc22018-03-24 15:03:40 -0700293void StatsdStats::noteMetricDimensionInConditionSize(
294 const ConfigKey& key, const int64_t& id, int size) {
295 lock_guard<std::mutex> lock(mLock);
296 // if name doesn't exist before, it will create the key with count 0.
297 auto statsIt = mConfigStats.find(key);
298 if (statsIt == mConfigStats.end()) {
299 return;
300 }
301 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
302 if (size > metricsDimensionMap[id]) {
303 metricsDimensionMap[id] = size;
304 }
305}
306
Yangster-mac94e197c2018-01-02 16:03:03 -0800307void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800308 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800309
310 auto statsIt = mConfigStats.find(key);
311 if (statsIt == mConfigStats.end()) {
312 return;
313 }
314 statsIt->second->matcher_stats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800315}
316
Yangster-mac94e197c2018-01-02 16:03:03 -0800317void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800318 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800319 auto statsIt = mConfigStats.find(key);
320 if (statsIt == mConfigStats.end()) {
321 return;
322 }
323 statsIt->second->alert_stats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800324}
325
Bookatz1d0136d2017-12-01 11:13:32 -0800326void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
327 lock_guard<std::mutex> lock(mLock);
328 mAnomalyAlarmRegisteredStats++;
329}
330
Yangster-mac932ecec2018-02-01 10:23:52 -0800331void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
332 lock_guard<std::mutex> lock(mLock);
333 mPeriodicAlarmRegisteredStats++;
334}
335
Chenjie Yub038b702017-12-18 15:15:34 -0800336void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
337 lock_guard<std::mutex> lock(mLock);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700338 mPulledAtomStats[pullAtomId].minPullIntervalSec =
339 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
Chenjie Yub038b702017-12-18 15:15:34 -0800340}
341
342void StatsdStats::notePull(int pullAtomId) {
343 lock_guard<std::mutex> lock(mLock);
344 mPulledAtomStats[pullAtomId].totalPull++;
345}
346
347void StatsdStats::notePullFromCache(int pullAtomId) {
348 lock_guard<std::mutex> lock(mLock);
349 mPulledAtomStats[pullAtomId].totalPullFromCache++;
350}
351
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700352void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
353 lock_guard<std::mutex> lock(mLock);
354 auto& pullStats = mPulledAtomStats[pullAtomId];
355 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
356 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
357 (pullStats.numPullTime + 1);
358 pullStats.numPullTime += 1;
359}
360
361void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
362 lock_guard<std::mutex> lock(mLock);
363 auto& pullStats = mPulledAtomStats[pullAtomId];
364 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700365 pullStats.avgPullDelayNs =
366 (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
367 (pullStats.numPullDelay + 1);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700368 pullStats.numPullDelay += 1;
369}
370
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700371void StatsdStats::notePullDataError(int pullAtomId) {
372 lock_guard<std::mutex> lock(mLock);
373 mPulledAtomStats[pullAtomId].dataError++;
374}
375
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
426void StatsdStats::noteHardDimensionLimitReached(int metricId) {
427 lock_guard<std::mutex> lock(mLock);
428 getAtomMetricStats(metricId).hardDimensionLimitReached++;
429}
430
431void StatsdStats::noteLateLogEventSkipped(int metricId) {
432 lock_guard<std::mutex> lock(mLock);
433 getAtomMetricStats(metricId).lateLogEventSkipped++;
434}
435
436void StatsdStats::noteSkippedForwardBuckets(int metricId) {
437 lock_guard<std::mutex> lock(mLock);
438 getAtomMetricStats(metricId).skippedForwardBuckets++;
439}
440
441void StatsdStats::noteBadValueType(int metricId) {
442 lock_guard<std::mutex> lock(mLock);
443 getAtomMetricStats(metricId).badValueType++;
444}
445
446void StatsdStats::noteConditionChangeInNextBucket(int metricId) {
447 lock_guard<std::mutex> lock(mLock);
448 getAtomMetricStats(metricId).conditionChangeInNextBucket++;
449}
450
451StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int metricId) {
452 auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
453 if (atomMetricStatsIter != mAtomMetricStats.end()) {
454 return atomMetricStatsIter->second;
455 }
456 auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
457 return emplaceResult.first->second;
458}
459
Yao Chenb3561512017-11-21 18:07:17 -0800460void StatsdStats::reset() {
461 lock_guard<std::mutex> lock(mLock);
462 resetInternalLocked();
463}
464
465void StatsdStats::resetInternalLocked() {
466 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800467 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800468 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800469 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800470 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800471 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700472 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700473 mLogLossStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800474 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800475 config.second->broadcast_sent_time_sec.clear();
476 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700477 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700478 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700479 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800480 config.second->matcher_stats.clear();
481 config.second->condition_stats.clear();
482 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700483 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800484 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800485 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700486 for (auto& pullStats : mPulledAtomStats) {
487 pullStats.second.totalPull = 0;
488 pullStats.second.totalPullFromCache = 0;
489 pullStats.second.avgPullTimeNs = 0;
490 pullStats.second.maxPullTimeNs = 0;
491 pullStats.second.numPullTime = 0;
492 pullStats.second.avgPullDelayNs = 0;
493 pullStats.second.maxPullDelayNs = 0;
494 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700495 pullStats.second.dataError = 0;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800496 pullStats.second.pullTimeout = 0;
497 pullStats.second.pullExceedMaxDelay = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700498 }
Misha Wagner1eee2212019-01-22 11:47:11 +0000499 mAtomMetricStats.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800500}
501
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700502string buildTimeString(int64_t timeSec) {
503 time_t t = timeSec;
504 struct tm* tm = localtime(&t);
505 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700506 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700507 return string(timeBuffer);
508}
509
Yao Chena80e5c02018-09-04 13:55:29 -0700510void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800511 lock_guard<std::mutex> lock(mLock);
512 time_t t = mStartTimeSec;
513 struct tm* tm = localtime(&t);
514 char timeBuffer[80];
515 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700516 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
517 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800518 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700519 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700520 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800521 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800522 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
Yangster-macb142cc82018-03-30 15:22:08 -0700523 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700524 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
525 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800526
Yao Chen20e9e622018-02-28 11:18:51 -0800527 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700528 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800529 }
530
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700531 auto dropTimePtr = configStats->data_drop_time_sec.begin();
532 auto dropBytesPtr = configStats->data_drop_bytes.begin();
533 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
534 i++, dropTimePtr++, dropBytesPtr++) {
535 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
536 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800537 }
538 }
Yao Chena80e5c02018-09-04 13:55:29 -0700539 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800540 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800541 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700542 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800543 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
544 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800545 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
546 configStats->deletion_time_sec, configStats->metric_count,
547 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
548 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700549 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700550 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700551 annotation.second);
552 }
553
Yao Chen20e9e622018-02-28 11:18:51 -0800554 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700555 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
556 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800557 }
558
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700559 auto dropTimePtr = configStats->data_drop_time_sec.begin();
560 auto dropBytesPtr = configStats->data_drop_bytes.begin();
561 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
562 i++, dropTimePtr++, dropBytesPtr++) {
563 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
564 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
565 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800566 }
567
Yangster-mace68f3a52018-04-04 00:01:43 -0700568 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700569 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700570 buildTimeString(dump.first).c_str(), (long long)dump.first,
571 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800572 }
573
Yao Chen20e9e622018-02-28 11:18:51 -0800574 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700575 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800576 }
Yao Chen20e9e622018-02-28 11:18:51 -0800577
578 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700579 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800580 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800581 }
Yao Chen20e9e622018-02-28 11:18:51 -0800582
583 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700584 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800585 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800586 }
Yao Chen20e9e622018-02-28 11:18:51 -0800587
588 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700589 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800590 }
591 }
Yao Chena80e5c02018-09-04 13:55:29 -0700592 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700593 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700594 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800595 const size_t atomCounts = mPushedAtomStats.size();
596 for (size_t i = 2; i < atomCounts; i++) {
597 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700598 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800599 }
600 }
601
Yao Chena80e5c02018-09-04 13:55:29 -0700602 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800603 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700604 dprintf(out,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800605 "Atom %d->(total pull)%ld, (pull from cache)%ld, (min pull interval)%ld \n"
606 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
607 "nanos)%lld, "
608 " (max pull delay nanos)%lld, (data error)%ld\n"
609 " (pull timeout)%ld, (pull exceed max delay)%ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700610 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
611 (long)pair.second.minPullIntervalSec, (long long)pair.second.avgPullTimeNs,
612 (long long)pair.second.maxPullTimeNs, (long long)pair.second.avgPullDelayNs,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800613 (long long)pair.second.maxPullDelayNs, pair.second.dataError,
614 pair.second.pullTimeout, pair.second.pullExceedMaxDelay);
Yao Chenf5acabe2018-01-17 14:10:34 -0800615 }
616
617 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700618 dprintf(out, "********AnomalyAlarmStats stats***********\n");
619 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800620 }
621
Yangster-mac932ecec2018-02-01 10:23:52 -0800622 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700623 dprintf(out, "********SubscriberAlarmStats stats***********\n");
624 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800625 }
626
Yao Chena80e5c02018-09-04 13:55:29 -0700627 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700628 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
629 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800630
Yangster-mac892f3d32018-05-02 14:16:48 -0700631 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700632 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
633 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700634 }
635
Yao Chen0f5d6612018-08-22 14:11:51 -0700636 for (const auto& loss : mLogLossStats) {
Yao Chen39b67992018-11-08 15:32:17 -0800637 dprintf(out, "Log loss: %lld (wall clock sec) - %d (count) %d (last error)\n",
638 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError);
Yangster-macb8382a12018-04-04 10:39:12 -0700639 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800640}
641
Yao Chen20e9e622018-02-28 11:18:51 -0800642void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800643 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800644 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
645 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
646 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
647 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700648 if (configStats.reset_time_sec != 0) {
649 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
650 }
Yao Chen20e9e622018-02-28 11:18:51 -0800651 if (configStats.deletion_time_sec != 0) {
652 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
653 configStats.deletion_time_sec);
654 }
655 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
656 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
657 configStats.condition_count);
658 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
659 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
660 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
661
662 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
663 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
664 broadcast);
665 }
666
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700667 for (const auto& drop_time : configStats.data_drop_time_sec) {
668 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
669 drop_time);
670 }
671
672 for (const auto& drop_bytes : configStats.data_drop_bytes) {
673 proto->write(
674 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
675 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800676 }
677
Yangster-mace68f3a52018-04-04 00:01:43 -0700678 for (const auto& dump : configStats.dump_report_stats) {
679 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
680 FIELD_COUNT_REPEATED,
681 dump.first);
682 }
683
684 for (const auto& dump : configStats.dump_report_stats) {
685 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
686 FIELD_COUNT_REPEATED,
687 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800688 }
689
David Chenfaa1af52018-03-30 15:14:04 -0700690 for (const auto& annotation : configStats.annotations) {
691 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
692 FIELD_ID_CONFIG_STATS_ANNOTATION);
693 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
694 (long long)annotation.first);
695 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
696 proto->end(token);
697 }
698
Yao Chen20e9e622018-02-28 11:18:51 -0800699 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800700 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800701 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
702 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
703 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
704 proto->end(tmpToken);
705 }
706
707 for (const auto& pair : configStats.condition_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800708 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800709 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
710 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
711 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
712 proto->end(tmpToken);
713 }
714
715 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800716 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800717 FIELD_ID_CONFIG_STATS_METRIC_STATS);
718 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
719 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
720 proto->end(tmpToken);
721 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700722 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
723 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
724 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
725 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
726 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
727 proto->end(tmpToken);
728 }
Yao Chen20e9e622018-02-28 11:18:51 -0800729
730 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800731 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800732 FIELD_ID_CONFIG_STATS_ALERT_STATS);
733 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
734 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
735 proto->end(tmpToken);
736 }
737
738 proto->end(token);
739}
740
Yao Chen69f1baf2017-11-27 17:25:36 -0800741void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800742 lock_guard<std::mutex> lock(mLock);
743
Yao Chenb3561512017-11-21 18:07:17 -0800744 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800745 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800746 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800747
Yao Chenb3561512017-11-21 18:07:17 -0800748 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800749 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800750 }
751
752 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800753 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800754 }
755
Yao Chenb3561512017-11-21 18:07:17 -0800756 const size_t atomCounts = mPushedAtomStats.size();
757 for (size_t i = 2; i < atomCounts; i++) {
758 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800759 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800760 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
761 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
762 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
763 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800764 }
765 }
766
Chenjie Yub038b702017-12-18 15:15:34 -0800767 for (const auto& pair : mPulledAtomStats) {
768 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800769 }
770
Misha Wagner1eee2212019-01-22 11:47:11 +0000771 for (const auto& pair : mAtomMetricStats) {
772 android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
773 }
774
Bookatz1d0136d2017-12-01 11:13:32 -0800775 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800776 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800777 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
778 mAnomalyAlarmRegisteredStats);
779 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800780 }
781
Yangster-mac932ecec2018-02-01 10:23:52 -0800782 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700783 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
784 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800785 mPeriodicAlarmRegisteredStats);
786 proto.end(token);
787 }
788
Yi Jin5ee07872018-03-05 18:18:27 -0800789 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800790 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
791 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800792 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700793 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800794 proto.end(uidMapToken);
David Chenc136f45a2017-11-27 11:52:26 -0800795
Yao Chen0f5d6612018-08-22 14:11:51 -0700796 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800797 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800798 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800799 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
800 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
801 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen884c8c12018-01-26 10:36:25 -0800802 proto.end(token);
803 }
804
Yangster-mac892f3d32018-05-02 14:16:48 -0700805 for (const auto& restart : mSystemServerRestartSec) {
806 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
807 restart);
808 }
809
Yao Chenb3561512017-11-21 18:07:17 -0800810 output->clear();
811 size_t bufferSize = proto.size();
812 output->resize(bufferSize);
813
814 size_t pos = 0;
815 auto it = proto.data();
816 while (it.readBuffer() != NULL) {
817 size_t toRead = it.currentToRead();
818 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
819 pos += toRead;
820 it.rp()->move(toRead);
821 }
822
823 if (reset) {
824 resetInternalLocked();
825 }
826
827 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800828}
829
830} // namespace statsd
831} // namespace os
yro255f72e2018-02-26 15:15:17 -0800832} // namespace android