blob: b433c41518ccda8a4024b468a778993af16ca8d1 [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;
Tej Singh6ede28b2019-01-29 17:06:54 -080085const int FIELD_ID_CONFIG_STATS_ACTIVATION = 22;
86const int FIELD_ID_CONFIG_STATS_DEACTIVATION = 23;
David Chenfaa1af52018-03-30 15:14:04 -070087const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
88const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
Yao Chen20e9e622018-02-28 11:18:51 -080089
90const int FIELD_ID_MATCHER_STATS_ID = 1;
91const int FIELD_ID_MATCHER_STATS_COUNT = 2;
92const int FIELD_ID_CONDITION_STATS_ID = 1;
93const int FIELD_ID_CONDITION_STATS_COUNT = 2;
94const int FIELD_ID_METRIC_STATS_ID = 1;
95const int FIELD_ID_METRIC_STATS_COUNT = 2;
96const int FIELD_ID_ALERT_STATS_ID = 1;
97const int FIELD_ID_ALERT_STATS_COUNT = 2;
98
David Chenbd125272018-04-04 19:02:50 -070099const int FIELD_ID_UID_MAP_CHANGES = 1;
100const int FIELD_ID_UID_MAP_BYTES_USED = 2;
101const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
102const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
Yao Chen20e9e622018-02-28 11:18:51 -0800103
Chenjie Yuc5875052018-03-09 10:13:11 -0800104const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100105 {android::util::BINDER_CALLS, {6000, 10000}},
Olivier Gaillardc840ed92018-11-30 13:06:18 +0000106 {android::util::LOOPER_STATS, {1500, 2500}},
Chenjie Yuc5875052018-03-09 10:13:11 -0800107 {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
108};
109
Yao Chenb3561512017-11-21 18:07:17 -0800110StatsdStats::StatsdStats() {
111 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -0800112 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800113}
114
115StatsdStats& StatsdStats::getInstance() {
116 static StatsdStats statsInstance;
117 return statsInstance;
118}
119
Yao Chen20e9e622018-02-28 11:18:51 -0800120void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
Yao Chenf6723df2018-01-08 15:11:58 -0800121 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
122 if (mIceBox.size() == kMaxIceBoxSize) {
123 mIceBox.pop_front();
124 }
125 mIceBox.push_back(stats);
126}
127
David Chenfaa1af52018-03-30 15:14:04 -0700128void StatsdStats::noteConfigReceived(
129 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
130 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
131 bool isValid) {
Yao Chenb3561512017-11-21 18:07:17 -0800132 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -0800133 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800134
135 // If there is an existing config for the same key, icebox the old config.
136 noteConfigRemovedInternalLocked(key);
137
Yao Chen20e9e622018-02-28 11:18:51 -0800138 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
139 configStats->uid = key.GetUid();
140 configStats->id = key.GetId();
141 configStats->creation_time_sec = nowTimeSec;
142 configStats->metric_count = metricsCount;
143 configStats->condition_count = conditionsCount;
144 configStats->matcher_count = matchersCount;
145 configStats->alert_count = alertsCount;
146 configStats->is_valid = isValid;
David Chenfaa1af52018-03-30 15:14:04 -0700147 for (auto& v : annotations) {
148 configStats->annotations.emplace_back(v);
149 }
Yao Chenb3561512017-11-21 18:07:17 -0800150
151 if (isValid) {
152 mConfigStats[key] = configStats;
153 } else {
Yao Chen20e9e622018-02-28 11:18:51 -0800154 configStats->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800155 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800156 }
157}
158
159void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
160 auto it = mConfigStats.find(key);
161 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800162 int32_t nowTimeSec = getWallClockSec();
Yao Chen20e9e622018-02-28 11:18:51 -0800163 it->second->deletion_time_sec = nowTimeSec;
Yao Chenf6723df2018-01-08 15:11:58 -0800164 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800165 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800166 }
167}
168
169void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
170 lock_guard<std::mutex> lock(mLock);
171 noteConfigRemovedInternalLocked(key);
172}
173
Yangster-macb142cc82018-03-30 15:22:08 -0700174void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
175 auto it = mConfigStats.find(key);
176 if (it != mConfigStats.end()) {
177 it->second->reset_time_sec = getWallClockSec();
178 }
179}
180
181void StatsdStats::noteConfigReset(const ConfigKey& key) {
182 lock_guard<std::mutex> lock(mLock);
183 noteConfigResetInternalLocked(key);
184}
185
Yao Chen39b67992018-11-08 15:32:17 -0800186void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError) {
Yao Chen163d2602018-04-10 10:39:53 -0700187 lock_guard<std::mutex> lock(mLock);
Yao Chen0f5d6612018-08-22 14:11:51 -0700188 if (mLogLossStats.size() == kMaxLoggerErrors) {
189 mLogLossStats.pop_front();
Yao Chen163d2602018-04-10 10:39:53 -0700190 }
Yao Chen39b67992018-11-08 15:32:17 -0800191 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError);
Yao Chen163d2602018-04-10 10:39:53 -0700192}
193
Yao Chenb3561512017-11-21 18:07:17 -0800194void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800195 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800196}
197
198void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800199 lock_guard<std::mutex> lock(mLock);
200 auto it = mConfigStats.find(key);
201 if (it == mConfigStats.end()) {
202 ALOGE("Config key %s not found!", key.ToString().c_str());
203 return;
204 }
Yao Chen20e9e622018-02-28 11:18:51 -0800205 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
206 it->second->broadcast_sent_time_sec.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800207 }
Yao Chen20e9e622018-02-28 11:18:51 -0800208 it->second->broadcast_sent_time_sec.push_back(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800209}
210
Tej Singh6ede28b2019-01-29 17:06:54 -0800211void StatsdStats::noteActiveStatusChanged(const ConfigKey& key, bool activated) {
212 noteActiveStatusChanged(key, activated, getWallClockSec());
213}
214
215void StatsdStats::noteActiveStatusChanged(const ConfigKey& key, bool activated, int32_t timeSec) {
216 lock_guard<std::mutex> lock(mLock);
217 auto it = mConfigStats.find(key);
218 if (it == mConfigStats.end()) {
219 ALOGE("Config key %s not found!", key.ToString().c_str());
220 return;
221 }
222 auto& vec = activated ? it->second->activation_time_sec
223 : it->second->deactivation_time_sec;
224 if (vec.size() == kMaxTimestampCount) {
225 vec.pop_front();
226 }
227 vec.push_back(timeSec);
228}
229
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700230void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
231 noteDataDropped(key, totalBytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800232}
233
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700234void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800235 lock_guard<std::mutex> lock(mLock);
236 auto it = mConfigStats.find(key);
237 if (it == mConfigStats.end()) {
238 ALOGE("Config key %s not found!", key.ToString().c_str());
239 return;
240 }
Yao Chen20e9e622018-02-28 11:18:51 -0800241 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
242 it->second->data_drop_time_sec.pop_front();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700243 it->second->data_drop_bytes.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800244 }
Yao Chen20e9e622018-02-28 11:18:51 -0800245 it->second->data_drop_time_sec.push_back(timeSec);
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700246 it->second->data_drop_bytes.push_back(totalBytes);
Yao Chenb3561512017-11-21 18:07:17 -0800247}
248
Yangster-mace68f3a52018-04-04 00:01:43 -0700249void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
250 noteMetricsReportSent(key, num_bytes, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800251}
252
Yangster-mace68f3a52018-04-04 00:01:43 -0700253void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
254 int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800255 lock_guard<std::mutex> lock(mLock);
256 auto it = mConfigStats.find(key);
257 if (it == mConfigStats.end()) {
258 ALOGE("Config key %s not found!", key.ToString().c_str());
259 return;
260 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700261 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
262 it->second->dump_report_stats.pop_front();
Yao Chen0fac5b12017-11-28 16:07:02 -0800263 }
Yangster-mace68f3a52018-04-04 00:01:43 -0700264 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
Yao Chen69f1baf2017-11-27 17:25:36 -0800265}
266
David Chenbd125272018-04-04 19:02:50 -0700267void StatsdStats::noteUidMapDropped(int deltas) {
David Chenc136f45a2017-11-27 11:52:26 -0800268 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800269 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
David Chenc136f45a2017-11-27 11:52:26 -0800270}
271
David Chenbd125272018-04-04 19:02:50 -0700272void StatsdStats::noteUidMapAppDeletionDropped() {
David Chenc136f45a2017-11-27 11:52:26 -0800273 lock_guard<std::mutex> lock(mLock);
David Chenbd125272018-04-04 19:02:50 -0700274 mUidMapStats.deleted_apps++;
David Chenc136f45a2017-11-27 11:52:26 -0800275}
276
277void StatsdStats::setUidMapChanges(int changes) {
278 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800279 mUidMapStats.changes = changes;
David Chenc136f45a2017-11-27 11:52:26 -0800280}
281
282void StatsdStats::setCurrentUidMapMemory(int bytes) {
283 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800284 mUidMapStats.bytes_used = bytes;
David Chenc136f45a2017-11-27 11:52:26 -0800285}
286
Yangster-mac94e197c2018-01-02 16:03:03 -0800287void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800288 lock_guard<std::mutex> lock(mLock);
289 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800290 auto statsIt = mConfigStats.find(key);
291 if (statsIt == mConfigStats.end()) {
292 return;
293 }
294
295 auto& conditionSizeMap = statsIt->second->condition_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800296 if (size > conditionSizeMap[id]) {
297 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800298 }
299}
300
Yangster-mac94e197c2018-01-02 16:03:03 -0800301void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800302 lock_guard<std::mutex> lock(mLock);
303 // if name doesn't exist before, it will create the key with count 0.
Yao Chen20e9e622018-02-28 11:18:51 -0800304 auto statsIt = mConfigStats.find(key);
305 if (statsIt == mConfigStats.end()) {
306 return;
307 }
308 auto& metricsDimensionMap = statsIt->second->metric_stats;
Yangster-mac94e197c2018-01-02 16:03:03 -0800309 if (size > metricsDimensionMap[id]) {
310 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800311 }
312}
313
Yangster-mac306ccc22018-03-24 15:03:40 -0700314void StatsdStats::noteMetricDimensionInConditionSize(
315 const ConfigKey& key, const int64_t& id, int size) {
316 lock_guard<std::mutex> lock(mLock);
317 // if name doesn't exist before, it will create the key with count 0.
318 auto statsIt = mConfigStats.find(key);
319 if (statsIt == mConfigStats.end()) {
320 return;
321 }
322 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
323 if (size > metricsDimensionMap[id]) {
324 metricsDimensionMap[id] = size;
325 }
326}
327
Yangster-mac94e197c2018-01-02 16:03:03 -0800328void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800329 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800330
331 auto statsIt = mConfigStats.find(key);
332 if (statsIt == mConfigStats.end()) {
333 return;
334 }
335 statsIt->second->matcher_stats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800336}
337
Yangster-mac94e197c2018-01-02 16:03:03 -0800338void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800339 lock_guard<std::mutex> lock(mLock);
Yao Chen20e9e622018-02-28 11:18:51 -0800340 auto statsIt = mConfigStats.find(key);
341 if (statsIt == mConfigStats.end()) {
342 return;
343 }
344 statsIt->second->alert_stats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800345}
346
Bookatz1d0136d2017-12-01 11:13:32 -0800347void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
348 lock_guard<std::mutex> lock(mLock);
349 mAnomalyAlarmRegisteredStats++;
350}
351
Yangster-mac932ecec2018-02-01 10:23:52 -0800352void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
353 lock_guard<std::mutex> lock(mLock);
354 mPeriodicAlarmRegisteredStats++;
355}
356
Chenjie Yub038b702017-12-18 15:15:34 -0800357void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
358 lock_guard<std::mutex> lock(mLock);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700359 mPulledAtomStats[pullAtomId].minPullIntervalSec =
360 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
Chenjie Yub038b702017-12-18 15:15:34 -0800361}
362
363void StatsdStats::notePull(int pullAtomId) {
364 lock_guard<std::mutex> lock(mLock);
365 mPulledAtomStats[pullAtomId].totalPull++;
366}
367
368void StatsdStats::notePullFromCache(int pullAtomId) {
369 lock_guard<std::mutex> lock(mLock);
370 mPulledAtomStats[pullAtomId].totalPullFromCache++;
371}
372
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700373void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
374 lock_guard<std::mutex> lock(mLock);
375 auto& pullStats = mPulledAtomStats[pullAtomId];
376 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
377 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
378 (pullStats.numPullTime + 1);
379 pullStats.numPullTime += 1;
380}
381
382void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
383 lock_guard<std::mutex> lock(mLock);
384 auto& pullStats = mPulledAtomStats[pullAtomId];
385 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700386 pullStats.avgPullDelayNs =
387 (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
388 (pullStats.numPullDelay + 1);
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700389 pullStats.numPullDelay += 1;
390}
391
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700392void StatsdStats::notePullDataError(int pullAtomId) {
393 lock_guard<std::mutex> lock(mLock);
394 mPulledAtomStats[pullAtomId].dataError++;
395}
396
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800397void StatsdStats::notePullTimeout(int pullAtomId) {
398 lock_guard<std::mutex> lock(mLock);
399 mPulledAtomStats[pullAtomId].pullTimeout++;
400}
401
402void StatsdStats::notePullExceedMaxDelay(int pullAtomId) {
403 lock_guard<std::mutex> lock(mLock);
404 mPulledAtomStats[pullAtomId].pullExceedMaxDelay++;
405}
406
Yao Chenb3561512017-11-21 18:07:17 -0800407void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
408 lock_guard<std::mutex> lock(mLock);
409
Yao Chenb3561512017-11-21 18:07:17 -0800410 if (atomId > android::util::kMaxPushedAtomId) {
411 ALOGW("not interested in atom %d", atomId);
412 return;
413 }
414
415 mPushedAtomStats[atomId]++;
416}
417
Yangster-mac892f3d32018-05-02 14:16:48 -0700418void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
419 lock_guard<std::mutex> lock(mLock);
420
421 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
422 mSystemServerRestartSec.pop_front();
423 }
424 mSystemServerRestartSec.push_back(timeSec);
425}
426
Misha Wagner1eee2212019-01-22 11:47:11 +0000427void StatsdStats::notePullFailed(int atomId) {
428 lock_guard<std::mutex> lock(mLock);
429 mPulledAtomStats[atomId].pullFailed++;
430}
431
432void StatsdStats::noteStatsCompanionPullFailed(int atomId) {
433 lock_guard<std::mutex> lock(mLock);
434 mPulledAtomStats[atomId].statsCompanionPullFailed++;
435}
436
437void StatsdStats::noteStatsCompanionPullBinderTransactionFailed(int atomId) {
438 lock_guard<std::mutex> lock(mLock);
439 mPulledAtomStats[atomId].statsCompanionPullBinderTransactionFailed++;
440}
441
442void StatsdStats::noteEmptyData(int atomId) {
443 lock_guard<std::mutex> lock(mLock);
444 mPulledAtomStats[atomId].emptyData++;
445}
446
Tej Singha0c89dd2019-01-25 16:39:18 -0800447void StatsdStats::notePullerCallbackRegistrationChanged(int atomId, bool registered) {
448 lock_guard<std::mutex> lock(mLock);
449 if (registered) {
450 mPulledAtomStats[atomId].registeredCount++;
451 } else {
452 mPulledAtomStats[atomId].unregisteredCount++;
453 }
454}
455
Olivier Gaillard2310b192019-02-08 10:23:05 +0000456void StatsdStats::noteHardDimensionLimitReached(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000457 lock_guard<std::mutex> lock(mLock);
458 getAtomMetricStats(metricId).hardDimensionLimitReached++;
459}
460
Olivier Gaillard2310b192019-02-08 10:23:05 +0000461void StatsdStats::noteLateLogEventSkipped(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000462 lock_guard<std::mutex> lock(mLock);
463 getAtomMetricStats(metricId).lateLogEventSkipped++;
464}
465
Olivier Gaillard2310b192019-02-08 10:23:05 +0000466void StatsdStats::noteSkippedForwardBuckets(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000467 lock_guard<std::mutex> lock(mLock);
468 getAtomMetricStats(metricId).skippedForwardBuckets++;
469}
470
Olivier Gaillard2310b192019-02-08 10:23:05 +0000471void StatsdStats::noteBadValueType(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000472 lock_guard<std::mutex> lock(mLock);
473 getAtomMetricStats(metricId).badValueType++;
474}
475
Olivier Gaillard2310b192019-02-08 10:23:05 +0000476void StatsdStats::noteBucketDropped(int64_t metricId) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000477 lock_guard<std::mutex> lock(mLock);
478 getAtomMetricStats(metricId).bucketDropped++;
479}
480
Olivier Gaillard2310b192019-02-08 10:23:05 +0000481void StatsdStats::noteConditionChangeInNextBucket(int64_t metricId) {
Misha Wagner1eee2212019-01-22 11:47:11 +0000482 lock_guard<std::mutex> lock(mLock);
483 getAtomMetricStats(metricId).conditionChangeInNextBucket++;
484}
485
Olivier Gaillard2310b192019-02-08 10:23:05 +0000486void StatsdStats::noteInvalidatedBucket(int64_t metricId) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000487 lock_guard<std::mutex> lock(mLock);
488 getAtomMetricStats(metricId).invalidatedBucket++;
489}
490
Olivier Gaillard3ba2e932019-02-08 14:25:35 +0000491void StatsdStats::noteBucketBoundaryDelayNs(int64_t metricId, int64_t timeDelayNs) {
492 lock_guard<std::mutex> lock(mLock);
493 AtomMetricStats& pullStats = getAtomMetricStats(metricId);
494 pullStats.maxBucketBoundaryDelayNs =
495 std::max(pullStats.maxBucketBoundaryDelayNs, timeDelayNs);
496 pullStats.minBucketBoundaryDelayNs =
497 std::min(pullStats.minBucketBoundaryDelayNs, timeDelayNs);
498}
499
Misha Wagner1eee2212019-01-22 11:47:11 +0000500StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int metricId) {
501 auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
502 if (atomMetricStatsIter != mAtomMetricStats.end()) {
503 return atomMetricStatsIter->second;
504 }
505 auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
506 return emplaceResult.first->second;
507}
508
Yao Chenb3561512017-11-21 18:07:17 -0800509void StatsdStats::reset() {
510 lock_guard<std::mutex> lock(mLock);
511 resetInternalLocked();
512}
513
514void StatsdStats::resetInternalLocked() {
515 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800516 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800517 mIceBox.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800518 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz1d0136d2017-12-01 11:13:32 -0800519 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800520 mPeriodicAlarmRegisteredStats = 0;
Yangster-mac892f3d32018-05-02 14:16:48 -0700521 mSystemServerRestartSec.clear();
Yao Chen0f5d6612018-08-22 14:11:51 -0700522 mLogLossStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800523 for (auto& config : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800524 config.second->broadcast_sent_time_sec.clear();
Tej Singh6ede28b2019-01-29 17:06:54 -0800525 config.second->activation_time_sec.clear();
526 config.second->deactivation_time_sec.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800527 config.second->data_drop_time_sec.clear();
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700528 config.second->data_drop_bytes.clear();
Yangster-mace68f3a52018-04-04 00:01:43 -0700529 config.second->dump_report_stats.clear();
David Chenfaa1af52018-03-30 15:14:04 -0700530 config.second->annotations.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800531 config.second->matcher_stats.clear();
532 config.second->condition_stats.clear();
533 config.second->metric_stats.clear();
Yangster-mac306ccc22018-03-24 15:03:40 -0700534 config.second->metric_dimension_in_condition_stats.clear();
Yao Chen20e9e622018-02-28 11:18:51 -0800535 config.second->alert_stats.clear();
Bookatz8f2f3d82017-12-07 13:53:21 -0800536 }
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700537 for (auto& pullStats : mPulledAtomStats) {
538 pullStats.second.totalPull = 0;
539 pullStats.second.totalPullFromCache = 0;
540 pullStats.second.avgPullTimeNs = 0;
541 pullStats.second.maxPullTimeNs = 0;
542 pullStats.second.numPullTime = 0;
543 pullStats.second.avgPullDelayNs = 0;
544 pullStats.second.maxPullDelayNs = 0;
545 pullStats.second.numPullDelay = 0;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700546 pullStats.second.dataError = 0;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800547 pullStats.second.pullTimeout = 0;
548 pullStats.second.pullExceedMaxDelay = 0;
Tej Singha0c89dd2019-01-25 16:39:18 -0800549 pullStats.second.registeredCount = 0;
550 pullStats.second.unregisteredCount = 0;
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700551 }
Misha Wagner1eee2212019-01-22 11:47:11 +0000552 mAtomMetricStats.clear();
Yao Chenb3561512017-11-21 18:07:17 -0800553}
554
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700555string buildTimeString(int64_t timeSec) {
556 time_t t = timeSec;
557 struct tm* tm = localtime(&t);
558 char timeBuffer[80];
Yangster-mac892f3d32018-05-02 14:16:48 -0700559 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700560 return string(timeBuffer);
561}
562
Yao Chena80e5c02018-09-04 13:55:29 -0700563void StatsdStats::dumpStats(int out) const {
Yao Chenf5acabe2018-01-17 14:10:34 -0800564 lock_guard<std::mutex> lock(mLock);
565 time_t t = mStartTimeSec;
566 struct tm* tm = localtime(&t);
567 char timeBuffer[80];
568 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
Yao Chena80e5c02018-09-04 13:55:29 -0700569 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
570 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800571 for (const auto& configStats : mIceBox) {
Yao Chena80e5c02018-09-04 13:55:29 -0700572 dprintf(out,
Yangster-macb142cc82018-03-30 15:22:08 -0700573 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800574 "#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,
Yangster-macb142cc82018-03-30 15:22:08 -0700576 configStats->deletion_time_sec, configStats->reset_time_sec,
Yao Chena80e5c02018-09-04 13:55:29 -0700577 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
578 configStats->alert_count, configStats->is_valid);
Yao Chenf5acabe2018-01-17 14:10:34 -0800579
Yao Chen20e9e622018-02-28 11:18:51 -0800580 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700581 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800582 }
583
Tej Singh6ede28b2019-01-29 17:06:54 -0800584 for (const int& activationTime : configStats->activation_time_sec) {
585 dprintf(out, "\tactivation time: %d\n", activationTime);
586 }
587
588 for (const int& deactivationTime : configStats->deactivation_time_sec) {
589 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
590 }
591
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700592 auto dropTimePtr = configStats->data_drop_time_sec.begin();
593 auto dropBytesPtr = configStats->data_drop_bytes.begin();
594 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
595 i++, dropTimePtr++, dropBytesPtr++) {
596 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
597 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800598 }
599 }
Yao Chena80e5c02018-09-04 13:55:29 -0700600 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
Yao Chenf5acabe2018-01-17 14:10:34 -0800601 for (auto& pair : mConfigStats) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800602 auto& configStats = pair.second;
Yao Chena80e5c02018-09-04 13:55:29 -0700603 dprintf(out,
Yao Chenf5acabe2018-01-17 14:10:34 -0800604 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
605 "#matcher=%d, #alert=%d, valid=%d\n",
Yao Chen20e9e622018-02-28 11:18:51 -0800606 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
607 configStats->deletion_time_sec, configStats->metric_count,
608 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
609 configStats->is_valid);
David Chenfaa1af52018-03-30 15:14:04 -0700610 for (const auto& annotation : configStats->annotations) {
Yao Chena80e5c02018-09-04 13:55:29 -0700611 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
David Chenfaa1af52018-03-30 15:14:04 -0700612 annotation.second);
613 }
614
Yao Chen20e9e622018-02-28 11:18:51 -0800615 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700616 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
617 (long long)broadcastTime);
Yao Chenf5acabe2018-01-17 14:10:34 -0800618 }
619
Tej Singh6ede28b2019-01-29 17:06:54 -0800620 for (const int& activationTime : configStats->activation_time_sec) {
621 dprintf(out, "\tactivation time: %d\n", activationTime);
622 }
623
624 for (const int& deactivationTime : configStats->deactivation_time_sec) {
625 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
626 }
627
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700628 auto dropTimePtr = configStats->data_drop_time_sec.begin();
629 auto dropBytesPtr = configStats->data_drop_bytes.begin();
630 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
631 i++, dropTimePtr++, dropBytesPtr++) {
632 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
633 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
634 (long long)*dropBytesPtr);
Yao Chenf5acabe2018-01-17 14:10:34 -0800635 }
636
Yangster-mace68f3a52018-04-04 00:01:43 -0700637 for (const auto& dump : configStats->dump_report_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700638 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700639 buildTimeString(dump.first).c_str(), (long long)dump.first,
640 (long long)dump.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800641 }
642
Yao Chen20e9e622018-02-28 11:18:51 -0800643 for (const auto& stats : pair.second->matcher_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700644 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800645 }
Yao Chen20e9e622018-02-28 11:18:51 -0800646
647 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700648 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800649 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800650 }
Yao Chen20e9e622018-02-28 11:18:51 -0800651
652 for (const auto& stats : pair.second->condition_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700653 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
Yao Chen20e9e622018-02-28 11:18:51 -0800654 stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800655 }
Yao Chen20e9e622018-02-28 11:18:51 -0800656
657 for (const auto& stats : pair.second->alert_stats) {
Yao Chena80e5c02018-09-04 13:55:29 -0700658 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
Yao Chenf5acabe2018-01-17 14:10:34 -0800659 }
660 }
Yao Chena80e5c02018-09-04 13:55:29 -0700661 dprintf(out, "********Disk Usage stats***********\n");
yro665208d2018-03-13 18:08:09 -0700662 StorageManager::printStats(out);
Yao Chena80e5c02018-09-04 13:55:29 -0700663 dprintf(out, "********Pushed Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800664 const size_t atomCounts = mPushedAtomStats.size();
665 for (size_t i = 2; i < atomCounts; i++) {
666 if (mPushedAtomStats[i] > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700667 dprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
Yao Chenf5acabe2018-01-17 14:10:34 -0800668 }
669 }
670
Yao Chena80e5c02018-09-04 13:55:29 -0700671 dprintf(out, "********Pulled Atom stats***********\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800672 for (const auto& pair : mPulledAtomStats) {
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700673 dprintf(out,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800674 "Atom %d->(total pull)%ld, (pull from cache)%ld, (min pull interval)%ld \n"
675 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
676 "nanos)%lld, "
677 " (max pull delay nanos)%lld, (data error)%ld\n"
Tej Singha0c89dd2019-01-25 16:39:18 -0800678 " (pull timeout)%ld, (pull exceed max delay)%ld\n"
679 " (registered count) %ld, (unregistered count) %ld\n",
Chenjie Yu48ed1cc2018-10-31 17:36:38 -0700680 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
681 (long)pair.second.minPullIntervalSec, (long long)pair.second.avgPullTimeNs,
682 (long long)pair.second.maxPullTimeNs, (long long)pair.second.avgPullDelayNs,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800683 (long long)pair.second.maxPullDelayNs, pair.second.dataError,
Tej Singha0c89dd2019-01-25 16:39:18 -0800684 pair.second.pullTimeout, pair.second.pullExceedMaxDelay,
685 pair.second.registeredCount, pair.second.unregisteredCount);
Yao Chenf5acabe2018-01-17 14:10:34 -0800686 }
687
688 if (mAnomalyAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700689 dprintf(out, "********AnomalyAlarmStats stats***********\n");
690 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
Yao Chenf5acabe2018-01-17 14:10:34 -0800691 }
692
Yangster-mac932ecec2018-02-01 10:23:52 -0800693 if (mPeriodicAlarmRegisteredStats > 0) {
Yao Chena80e5c02018-09-04 13:55:29 -0700694 dprintf(out, "********SubscriberAlarmStats stats***********\n");
695 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
Yangster-mac932ecec2018-02-01 10:23:52 -0800696 }
697
Yao Chena80e5c02018-09-04 13:55:29 -0700698 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
David Chenbd125272018-04-04 19:02:50 -0700699 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
700 mUidMapStats.dropped_changes);
Yao Chen884c8c12018-01-26 10:36:25 -0800701
Yangster-mac892f3d32018-05-02 14:16:48 -0700702 for (const auto& restart : mSystemServerRestartSec) {
Yao Chena80e5c02018-09-04 13:55:29 -0700703 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
704 (long long)restart);
Yangster-mac892f3d32018-05-02 14:16:48 -0700705 }
706
Yao Chen0f5d6612018-08-22 14:11:51 -0700707 for (const auto& loss : mLogLossStats) {
Yao Chen39b67992018-11-08 15:32:17 -0800708 dprintf(out, "Log loss: %lld (wall clock sec) - %d (count) %d (last error)\n",
709 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError);
Yangster-macb8382a12018-04-04 10:39:12 -0700710 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800711}
712
Yao Chen20e9e622018-02-28 11:18:51 -0800713void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
Yi Jin5ee07872018-03-05 18:18:27 -0800714 uint64_t token =
Yao Chen20e9e622018-02-28 11:18:51 -0800715 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
716 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
717 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
718 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
Yangster-macb142cc82018-03-30 15:22:08 -0700719 if (configStats.reset_time_sec != 0) {
720 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
721 }
Yao Chen20e9e622018-02-28 11:18:51 -0800722 if (configStats.deletion_time_sec != 0) {
723 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
724 configStats.deletion_time_sec);
725 }
726 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
727 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
728 configStats.condition_count);
729 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
730 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
731 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
732
733 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
734 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
735 broadcast);
736 }
737
Tej Singh6ede28b2019-01-29 17:06:54 -0800738 for (const auto& activation : configStats.activation_time_sec) {
739 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ACTIVATION | FIELD_COUNT_REPEATED,
740 activation);
741 }
742
743 for (const auto& deactivation : configStats.deactivation_time_sec) {
744 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DEACTIVATION | FIELD_COUNT_REPEATED,
745 deactivation);
746 }
747
Chenjie Yuc3c30c02018-10-26 09:48:07 -0700748 for (const auto& drop_time : configStats.data_drop_time_sec) {
749 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
750 drop_time);
751 }
752
753 for (const auto& drop_bytes : configStats.data_drop_bytes) {
754 proto->write(
755 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
756 (long long)drop_bytes);
Yao Chen20e9e622018-02-28 11:18:51 -0800757 }
758
Yangster-mace68f3a52018-04-04 00:01:43 -0700759 for (const auto& dump : configStats.dump_report_stats) {
760 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
761 FIELD_COUNT_REPEATED,
762 dump.first);
763 }
764
765 for (const auto& dump : configStats.dump_report_stats) {
766 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
767 FIELD_COUNT_REPEATED,
768 (long long)dump.second);
Yao Chen20e9e622018-02-28 11:18:51 -0800769 }
770
David Chenfaa1af52018-03-30 15:14:04 -0700771 for (const auto& annotation : configStats.annotations) {
772 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
773 FIELD_ID_CONFIG_STATS_ANNOTATION);
774 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
775 (long long)annotation.first);
776 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
777 proto->end(token);
778 }
779
Yao Chen20e9e622018-02-28 11:18:51 -0800780 for (const auto& pair : configStats.matcher_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800781 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800782 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
783 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
784 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
785 proto->end(tmpToken);
786 }
787
788 for (const auto& pair : configStats.condition_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800789 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800790 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
791 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
792 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
793 proto->end(tmpToken);
794 }
795
796 for (const auto& pair : configStats.metric_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800797 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800798 FIELD_ID_CONFIG_STATS_METRIC_STATS);
799 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
800 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
801 proto->end(tmpToken);
802 }
Yangster-mac306ccc22018-03-24 15:03:40 -0700803 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
804 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
805 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
806 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
807 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
808 proto->end(tmpToken);
809 }
Yao Chen20e9e622018-02-28 11:18:51 -0800810
811 for (const auto& pair : configStats.alert_stats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800812 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
Yao Chen20e9e622018-02-28 11:18:51 -0800813 FIELD_ID_CONFIG_STATS_ALERT_STATS);
814 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
815 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
816 proto->end(tmpToken);
817 }
818
819 proto->end(token);
820}
821
Yao Chen69f1baf2017-11-27 17:25:36 -0800822void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800823 lock_guard<std::mutex> lock(mLock);
824
Yao Chenb3561512017-11-21 18:07:17 -0800825 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800826 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800827 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800828
Yao Chenb3561512017-11-21 18:07:17 -0800829 for (const auto& configStats : mIceBox) {
Yao Chen20e9e622018-02-28 11:18:51 -0800830 addConfigStatsToProto(*configStats, &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800831 }
832
833 for (auto& pair : mConfigStats) {
Yao Chen20e9e622018-02-28 11:18:51 -0800834 addConfigStatsToProto(*(pair.second), &proto);
Yao Chenb3561512017-11-21 18:07:17 -0800835 }
836
Yao Chenb3561512017-11-21 18:07:17 -0800837 const size_t atomCounts = mPushedAtomStats.size();
838 for (size_t i = 2; i < atomCounts; i++) {
839 if (mPushedAtomStats[i] > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800840 uint64_t token =
Yao Chenb3561512017-11-21 18:07:17 -0800841 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
842 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
843 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
844 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800845 }
846 }
847
Chenjie Yub038b702017-12-18 15:15:34 -0800848 for (const auto& pair : mPulledAtomStats) {
849 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800850 }
851
Misha Wagner1eee2212019-01-22 11:47:11 +0000852 for (const auto& pair : mAtomMetricStats) {
853 android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
854 }
855
Bookatz1d0136d2017-12-01 11:13:32 -0800856 if (mAnomalyAlarmRegisteredStats > 0) {
Yi Jin5ee07872018-03-05 18:18:27 -0800857 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
Bookatz1d0136d2017-12-01 11:13:32 -0800858 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
859 mAnomalyAlarmRegisteredStats);
860 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800861 }
862
Yangster-mac932ecec2018-02-01 10:23:52 -0800863 if (mPeriodicAlarmRegisteredStats > 0) {
Yangster-macb8382a12018-04-04 10:39:12 -0700864 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
865 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
Yangster-mac932ecec2018-02-01 10:23:52 -0800866 mPeriodicAlarmRegisteredStats);
867 proto.end(token);
868 }
869
Yi Jin5ee07872018-03-05 18:18:27 -0800870 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
Yao Chen20e9e622018-02-28 11:18:51 -0800871 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
872 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
Yao Chen20e9e622018-02-28 11:18:51 -0800873 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
David Chenbd125272018-04-04 19:02:50 -0700874 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
Yao Chen20e9e622018-02-28 11:18:51 -0800875 proto.end(uidMapToken);
David Chenc136f45a2017-11-27 11:52:26 -0800876
Yao Chen0f5d6612018-08-22 14:11:51 -0700877 for (const auto& error : mLogLossStats) {
Yi Jin5ee07872018-03-05 18:18:27 -0800878 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
Yao Chen884c8c12018-01-26 10:36:25 -0800879 FIELD_COUNT_REPEATED);
Yao Chen39b67992018-11-08 15:32:17 -0800880 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
881 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
882 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
Yao Chen884c8c12018-01-26 10:36:25 -0800883 proto.end(token);
884 }
885
Yangster-mac892f3d32018-05-02 14:16:48 -0700886 for (const auto& restart : mSystemServerRestartSec) {
887 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
888 restart);
889 }
890
Yao Chenb3561512017-11-21 18:07:17 -0800891 output->clear();
892 size_t bufferSize = proto.size();
893 output->resize(bufferSize);
894
895 size_t pos = 0;
896 auto it = proto.data();
897 while (it.readBuffer() != NULL) {
898 size_t toRead = it.currentToRead();
899 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
900 pos += toRead;
901 it.rp()->move(toRead);
902 }
903
904 if (reset) {
905 resetInternalLocked();
906 }
907
908 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800909}
910
911} // namespace statsd
912} // namespace os
yro255f72e2018-02-26 15:15:17 -0800913} // namespace android