1 /*
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 */
16 #define DEBUG false // STOPSHIP if true
17 #include "Log.h"
18
19 #include "StatsdStats.h"
20
21 #include <android/util/ProtoOutputStream.h>
22 #include "../stats_log_util.h"
23 #include "statslog_statsd.h"
24 #include "storage/StorageManager.h"
25
26 namespace android {
27 namespace os {
28 namespace statsd {
29
30 using android::util::FIELD_COUNT_REPEATED;
31 using android::util::FIELD_TYPE_BOOL;
32 using android::util::FIELD_TYPE_FLOAT;
33 using android::util::FIELD_TYPE_INT32;
34 using android::util::FIELD_TYPE_INT64;
35 using android::util::FIELD_TYPE_MESSAGE;
36 using android::util::FIELD_TYPE_STRING;
37 using android::util::ProtoOutputStream;
38 using std::lock_guard;
39 using std::shared_ptr;
40 using std::string;
41 using std::to_string;
42 using std::vector;
43
44 const int FIELD_ID_BEGIN_TIME = 1;
45 const int FIELD_ID_END_TIME = 2;
46 const int FIELD_ID_CONFIG_STATS = 3;
47 const int FIELD_ID_ATOM_STATS = 7;
48 const int FIELD_ID_UIDMAP_STATS = 8;
49 const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
50 const int FIELD_ID_PERIODIC_ALARM_STATS = 12;
51 const int FIELD_ID_SYSTEM_SERVER_RESTART = 15;
52 const int FIELD_ID_LOGGER_ERROR_STATS = 16;
53 const int FIELD_ID_OVERFLOW = 18;
54 const int FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL = 19;
55
56 const int FIELD_ID_ATOM_STATS_TAG = 1;
57 const int FIELD_ID_ATOM_STATS_COUNT = 2;
58 const int FIELD_ID_ATOM_STATS_ERROR_COUNT = 3;
59
60 const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
61 const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1;
62
63 const int FIELD_ID_LOG_LOSS_STATS_TIME = 1;
64 const int FIELD_ID_LOG_LOSS_STATS_COUNT = 2;
65 const int FIELD_ID_LOG_LOSS_STATS_ERROR = 3;
66 const int FIELD_ID_LOG_LOSS_STATS_TAG = 4;
67 const int FIELD_ID_LOG_LOSS_STATS_UID = 5;
68 const int FIELD_ID_LOG_LOSS_STATS_PID = 6;
69
70 const int FIELD_ID_OVERFLOW_COUNT = 1;
71 const int FIELD_ID_OVERFLOW_MAX_HISTORY = 2;
72 const int FIELD_ID_OVERFLOW_MIN_HISTORY = 3;
73
74 const int FIELD_ID_CONFIG_STATS_UID = 1;
75 const int FIELD_ID_CONFIG_STATS_ID = 2;
76 const int FIELD_ID_CONFIG_STATS_CREATION = 3;
77 const int FIELD_ID_CONFIG_STATS_RESET = 19;
78 const int FIELD_ID_CONFIG_STATS_DELETION = 4;
79 const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
80 const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
81 const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
82 const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
83 const int FIELD_ID_CONFIG_STATS_VALID = 9;
84 const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
85 const int FIELD_ID_CONFIG_STATS_DATA_DROP_TIME = 11;
86 const int FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES = 21;
87 const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12;
88 const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20;
89 const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
90 const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
91 const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
92 const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
93 const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17;
94 const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18;
95 const int FIELD_ID_CONFIG_STATS_ACTIVATION = 22;
96 const int FIELD_ID_CONFIG_STATS_DEACTIVATION = 23;
97 const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
98 const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
99
100 const int FIELD_ID_MATCHER_STATS_ID = 1;
101 const int FIELD_ID_MATCHER_STATS_COUNT = 2;
102 const int FIELD_ID_CONDITION_STATS_ID = 1;
103 const int FIELD_ID_CONDITION_STATS_COUNT = 2;
104 const int FIELD_ID_METRIC_STATS_ID = 1;
105 const int FIELD_ID_METRIC_STATS_COUNT = 2;
106 const int FIELD_ID_ALERT_STATS_ID = 1;
107 const int FIELD_ID_ALERT_STATS_COUNT = 2;
108
109 const int FIELD_ID_UID_MAP_CHANGES = 1;
110 const int FIELD_ID_UID_MAP_BYTES_USED = 2;
111 const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
112 const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
113
114 const int FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL_UID = 1;
115 const int FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL_TIME = 2;
116
117 const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
118 {util::BINDER_CALLS, {6000, 10000}},
119 {util::LOOPER_STATS, {1500, 2500}},
120 {util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
121 };
122
StatsdStats()123 StatsdStats::StatsdStats() {
124 mPushedAtomStats.resize(kMaxPushedAtomId + 1);
125 mStartTimeSec = getWallClockSec();
126 }
127
getInstance()128 StatsdStats& StatsdStats::getInstance() {
129 static StatsdStats statsInstance;
130 return statsInstance;
131 }
132
addToIceBoxLocked(shared_ptr<ConfigStats> & stats)133 void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
134 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
135 if (mIceBox.size() == kMaxIceBoxSize) {
136 mIceBox.pop_front();
137 }
138 mIceBox.push_back(stats);
139 }
140
noteConfigReceived(const ConfigKey & key,int metricsCount,int conditionsCount,int matchersCount,int alertsCount,const std::list<std::pair<const int64_t,const int32_t>> & annotations,bool isValid)141 void StatsdStats::noteConfigReceived(
142 const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
143 int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
144 bool isValid) {
145 lock_guard<std::mutex> lock(mLock);
146 int32_t nowTimeSec = getWallClockSec();
147
148 // If there is an existing config for the same key, icebox the old config.
149 noteConfigRemovedInternalLocked(key);
150
151 shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
152 configStats->uid = key.GetUid();
153 configStats->id = key.GetId();
154 configStats->creation_time_sec = nowTimeSec;
155 configStats->metric_count = metricsCount;
156 configStats->condition_count = conditionsCount;
157 configStats->matcher_count = matchersCount;
158 configStats->alert_count = alertsCount;
159 configStats->is_valid = isValid;
160 for (auto& v : annotations) {
161 configStats->annotations.emplace_back(v);
162 }
163
164 if (isValid) {
165 mConfigStats[key] = configStats;
166 } else {
167 configStats->deletion_time_sec = nowTimeSec;
168 addToIceBoxLocked(configStats);
169 }
170 }
171
noteConfigRemovedInternalLocked(const ConfigKey & key)172 void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
173 auto it = mConfigStats.find(key);
174 if (it != mConfigStats.end()) {
175 int32_t nowTimeSec = getWallClockSec();
176 it->second->deletion_time_sec = nowTimeSec;
177 addToIceBoxLocked(it->second);
178 mConfigStats.erase(it);
179 }
180 }
181
noteConfigRemoved(const ConfigKey & key)182 void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
183 lock_guard<std::mutex> lock(mLock);
184 noteConfigRemovedInternalLocked(key);
185 }
186
noteConfigResetInternalLocked(const ConfigKey & key)187 void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
188 auto it = mConfigStats.find(key);
189 if (it != mConfigStats.end()) {
190 it->second->reset_time_sec = getWallClockSec();
191 }
192 }
193
noteConfigReset(const ConfigKey & key)194 void StatsdStats::noteConfigReset(const ConfigKey& key) {
195 lock_guard<std::mutex> lock(mLock);
196 noteConfigResetInternalLocked(key);
197 }
198
noteLogLost(int32_t wallClockTimeSec,int32_t count,int32_t lastError,int32_t lastTag,int32_t uid,int32_t pid)199 void StatsdStats::noteLogLost(int32_t wallClockTimeSec, int32_t count, int32_t lastError,
200 int32_t lastTag, int32_t uid, int32_t pid) {
201 lock_guard<std::mutex> lock(mLock);
202 if (mLogLossStats.size() == kMaxLoggerErrors) {
203 mLogLossStats.pop_front();
204 }
205 mLogLossStats.emplace_back(wallClockTimeSec, count, lastError, lastTag, uid, pid);
206 }
207
noteBroadcastSent(const ConfigKey & key)208 void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
209 noteBroadcastSent(key, getWallClockSec());
210 }
211
noteBroadcastSent(const ConfigKey & key,int32_t timeSec)212 void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
213 lock_guard<std::mutex> lock(mLock);
214 auto it = mConfigStats.find(key);
215 if (it == mConfigStats.end()) {
216 ALOGE("Config key %s not found!", key.ToString().c_str());
217 return;
218 }
219 if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
220 it->second->broadcast_sent_time_sec.pop_front();
221 }
222 it->second->broadcast_sent_time_sec.push_back(timeSec);
223 }
224
noteActiveStatusChanged(const ConfigKey & key,bool activated)225 void StatsdStats::noteActiveStatusChanged(const ConfigKey& key, bool activated) {
226 noteActiveStatusChanged(key, activated, getWallClockSec());
227 }
228
noteActiveStatusChanged(const ConfigKey & key,bool activated,int32_t timeSec)229 void StatsdStats::noteActiveStatusChanged(const ConfigKey& key, bool activated, int32_t timeSec) {
230 lock_guard<std::mutex> lock(mLock);
231 auto it = mConfigStats.find(key);
232 if (it == mConfigStats.end()) {
233 ALOGE("Config key %s not found!", key.ToString().c_str());
234 return;
235 }
236 auto& vec = activated ? it->second->activation_time_sec
237 : it->second->deactivation_time_sec;
238 if (vec.size() == kMaxTimestampCount) {
239 vec.pop_front();
240 }
241 vec.push_back(timeSec);
242 }
243
noteActivationBroadcastGuardrailHit(const int uid)244 void StatsdStats::noteActivationBroadcastGuardrailHit(const int uid) {
245 noteActivationBroadcastGuardrailHit(uid, getWallClockSec());
246 }
247
noteActivationBroadcastGuardrailHit(const int uid,const int32_t timeSec)248 void StatsdStats::noteActivationBroadcastGuardrailHit(const int uid, const int32_t timeSec) {
249 lock_guard<std::mutex> lock(mLock);
250 auto& guardrailTimes = mActivationBroadcastGuardrailStats[uid];
251 if (guardrailTimes.size() == kMaxTimestampCount) {
252 guardrailTimes.pop_front();
253 }
254 guardrailTimes.push_back(timeSec);
255 }
256
noteDataDropped(const ConfigKey & key,const size_t totalBytes)257 void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes) {
258 noteDataDropped(key, totalBytes, getWallClockSec());
259 }
260
noteEventQueueOverflow(int64_t oldestEventTimestampNs)261 void StatsdStats::noteEventQueueOverflow(int64_t oldestEventTimestampNs) {
262 lock_guard<std::mutex> lock(mLock);
263
264 mOverflowCount++;
265
266 int64_t history = getElapsedRealtimeNs() - oldestEventTimestampNs;
267
268 if (history > mMaxQueueHistoryNs) {
269 mMaxQueueHistoryNs = history;
270 }
271
272 if (history < mMinQueueHistoryNs) {
273 mMinQueueHistoryNs = history;
274 }
275 }
276
noteDataDropped(const ConfigKey & key,const size_t totalBytes,int32_t timeSec)277 void StatsdStats::noteDataDropped(const ConfigKey& key, const size_t totalBytes, int32_t timeSec) {
278 lock_guard<std::mutex> lock(mLock);
279 auto it = mConfigStats.find(key);
280 if (it == mConfigStats.end()) {
281 ALOGE("Config key %s not found!", key.ToString().c_str());
282 return;
283 }
284 if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
285 it->second->data_drop_time_sec.pop_front();
286 it->second->data_drop_bytes.pop_front();
287 }
288 it->second->data_drop_time_sec.push_back(timeSec);
289 it->second->data_drop_bytes.push_back(totalBytes);
290 }
291
noteMetricsReportSent(const ConfigKey & key,const size_t num_bytes)292 void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
293 noteMetricsReportSent(key, num_bytes, getWallClockSec());
294 }
295
noteMetricsReportSent(const ConfigKey & key,const size_t num_bytes,int32_t timeSec)296 void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
297 int32_t timeSec) {
298 lock_guard<std::mutex> lock(mLock);
299 auto it = mConfigStats.find(key);
300 if (it == mConfigStats.end()) {
301 ALOGE("Config key %s not found!", key.ToString().c_str());
302 return;
303 }
304 if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
305 it->second->dump_report_stats.pop_front();
306 }
307 it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
308 }
309
noteUidMapDropped(int deltas)310 void StatsdStats::noteUidMapDropped(int deltas) {
311 lock_guard<std::mutex> lock(mLock);
312 mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
313 }
314
noteUidMapAppDeletionDropped()315 void StatsdStats::noteUidMapAppDeletionDropped() {
316 lock_guard<std::mutex> lock(mLock);
317 mUidMapStats.deleted_apps++;
318 }
319
setUidMapChanges(int changes)320 void StatsdStats::setUidMapChanges(int changes) {
321 lock_guard<std::mutex> lock(mLock);
322 mUidMapStats.changes = changes;
323 }
324
setCurrentUidMapMemory(int bytes)325 void StatsdStats::setCurrentUidMapMemory(int bytes) {
326 lock_guard<std::mutex> lock(mLock);
327 mUidMapStats.bytes_used = bytes;
328 }
329
noteConditionDimensionSize(const ConfigKey & key,const int64_t & id,int size)330 void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
331 lock_guard<std::mutex> lock(mLock);
332 // if name doesn't exist before, it will create the key with count 0.
333 auto statsIt = mConfigStats.find(key);
334 if (statsIt == mConfigStats.end()) {
335 return;
336 }
337
338 auto& conditionSizeMap = statsIt->second->condition_stats;
339 if (size > conditionSizeMap[id]) {
340 conditionSizeMap[id] = size;
341 }
342 }
343
noteMetricDimensionSize(const ConfigKey & key,const int64_t & id,int size)344 void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
345 lock_guard<std::mutex> lock(mLock);
346 // if name doesn't exist before, it will create the key with count 0.
347 auto statsIt = mConfigStats.find(key);
348 if (statsIt == mConfigStats.end()) {
349 return;
350 }
351 auto& metricsDimensionMap = statsIt->second->metric_stats;
352 if (size > metricsDimensionMap[id]) {
353 metricsDimensionMap[id] = size;
354 }
355 }
356
noteMetricDimensionInConditionSize(const ConfigKey & key,const int64_t & id,int size)357 void StatsdStats::noteMetricDimensionInConditionSize(
358 const ConfigKey& key, const int64_t& id, int size) {
359 lock_guard<std::mutex> lock(mLock);
360 // if name doesn't exist before, it will create the key with count 0.
361 auto statsIt = mConfigStats.find(key);
362 if (statsIt == mConfigStats.end()) {
363 return;
364 }
365 auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
366 if (size > metricsDimensionMap[id]) {
367 metricsDimensionMap[id] = size;
368 }
369 }
370
noteMatcherMatched(const ConfigKey & key,const int64_t & id)371 void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
372 lock_guard<std::mutex> lock(mLock);
373
374 auto statsIt = mConfigStats.find(key);
375 if (statsIt == mConfigStats.end()) {
376 return;
377 }
378 statsIt->second->matcher_stats[id]++;
379 }
380
noteAnomalyDeclared(const ConfigKey & key,const int64_t & id)381 void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
382 lock_guard<std::mutex> lock(mLock);
383 auto statsIt = mConfigStats.find(key);
384 if (statsIt == mConfigStats.end()) {
385 return;
386 }
387 statsIt->second->alert_stats[id]++;
388 }
389
noteRegisteredAnomalyAlarmChanged()390 void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
391 lock_guard<std::mutex> lock(mLock);
392 mAnomalyAlarmRegisteredStats++;
393 }
394
noteRegisteredPeriodicAlarmChanged()395 void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
396 lock_guard<std::mutex> lock(mLock);
397 mPeriodicAlarmRegisteredStats++;
398 }
399
updateMinPullIntervalSec(int pullAtomId,long intervalSec)400 void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
401 lock_guard<std::mutex> lock(mLock);
402 mPulledAtomStats[pullAtomId].minPullIntervalSec =
403 std::min(mPulledAtomStats[pullAtomId].minPullIntervalSec, intervalSec);
404 }
405
notePull(int pullAtomId)406 void StatsdStats::notePull(int pullAtomId) {
407 lock_guard<std::mutex> lock(mLock);
408 mPulledAtomStats[pullAtomId].totalPull++;
409 }
410
notePullFromCache(int pullAtomId)411 void StatsdStats::notePullFromCache(int pullAtomId) {
412 lock_guard<std::mutex> lock(mLock);
413 mPulledAtomStats[pullAtomId].totalPullFromCache++;
414 }
415
notePullTime(int pullAtomId,int64_t pullTimeNs)416 void StatsdStats::notePullTime(int pullAtomId, int64_t pullTimeNs) {
417 lock_guard<std::mutex> lock(mLock);
418 auto& pullStats = mPulledAtomStats[pullAtomId];
419 pullStats.maxPullTimeNs = std::max(pullStats.maxPullTimeNs, pullTimeNs);
420 pullStats.avgPullTimeNs = (pullStats.avgPullTimeNs * pullStats.numPullTime + pullTimeNs) /
421 (pullStats.numPullTime + 1);
422 pullStats.numPullTime += 1;
423 }
424
notePullDelay(int pullAtomId,int64_t pullDelayNs)425 void StatsdStats::notePullDelay(int pullAtomId, int64_t pullDelayNs) {
426 lock_guard<std::mutex> lock(mLock);
427 auto& pullStats = mPulledAtomStats[pullAtomId];
428 pullStats.maxPullDelayNs = std::max(pullStats.maxPullDelayNs, pullDelayNs);
429 pullStats.avgPullDelayNs =
430 (pullStats.avgPullDelayNs * pullStats.numPullDelay + pullDelayNs) /
431 (pullStats.numPullDelay + 1);
432 pullStats.numPullDelay += 1;
433 }
434
notePullDataError(int pullAtomId)435 void StatsdStats::notePullDataError(int pullAtomId) {
436 lock_guard<std::mutex> lock(mLock);
437 mPulledAtomStats[pullAtomId].dataError++;
438 }
439
notePullTimeout(int pullAtomId,int64_t pullUptimeMillis,int64_t pullElapsedMillis)440 void StatsdStats::notePullTimeout(int pullAtomId,
441 int64_t pullUptimeMillis,
442 int64_t pullElapsedMillis) {
443 lock_guard<std::mutex> lock(mLock);
444 PulledAtomStats& pulledAtomStats = mPulledAtomStats[pullAtomId];
445 pulledAtomStats.pullTimeout++;
446
447 if (pulledAtomStats.pullTimeoutMetadata.size() == kMaxTimestampCount) {
448 pulledAtomStats.pullTimeoutMetadata.pop_front();
449 }
450
451 pulledAtomStats.pullTimeoutMetadata.emplace_back(pullUptimeMillis, pullElapsedMillis);
452 }
453
notePullExceedMaxDelay(int pullAtomId)454 void StatsdStats::notePullExceedMaxDelay(int pullAtomId) {
455 lock_guard<std::mutex> lock(mLock);
456 mPulledAtomStats[pullAtomId].pullExceedMaxDelay++;
457 }
458
noteAtomLogged(int atomId,int32_t timeSec)459 void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
460 lock_guard<std::mutex> lock(mLock);
461
462 if (atomId >= 0 && atomId <= kMaxPushedAtomId) {
463 mPushedAtomStats[atomId]++;
464 } else {
465 if (atomId < 0) {
466 android_errorWriteLog(0x534e4554, "187957589");
467 }
468 if (mNonPlatformPushedAtomStats.size() < kMaxNonPlatformPushedAtoms) {
469 mNonPlatformPushedAtomStats[atomId]++;
470 }
471 }
472 }
473
noteSystemServerRestart(int32_t timeSec)474 void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
475 lock_guard<std::mutex> lock(mLock);
476
477 if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
478 mSystemServerRestartSec.pop_front();
479 }
480 mSystemServerRestartSec.push_back(timeSec);
481 }
482
notePullFailed(int atomId)483 void StatsdStats::notePullFailed(int atomId) {
484 lock_guard<std::mutex> lock(mLock);
485 mPulledAtomStats[atomId].pullFailed++;
486 }
487
notePullUidProviderNotFound(int atomId)488 void StatsdStats::notePullUidProviderNotFound(int atomId) {
489 lock_guard<std::mutex> lock(mLock);
490 mPulledAtomStats[atomId].pullUidProviderNotFound++;
491 }
492
notePullerNotFound(int atomId)493 void StatsdStats::notePullerNotFound(int atomId) {
494 lock_guard<std::mutex> lock(mLock);
495 mPulledAtomStats[atomId].pullerNotFound++;
496 }
497
notePullBinderCallFailed(int atomId)498 void StatsdStats::notePullBinderCallFailed(int atomId) {
499 lock_guard<std::mutex> lock(mLock);
500 mPulledAtomStats[atomId].binderCallFailCount++;
501 }
502
noteEmptyData(int atomId)503 void StatsdStats::noteEmptyData(int atomId) {
504 lock_guard<std::mutex> lock(mLock);
505 mPulledAtomStats[atomId].emptyData++;
506 }
507
notePullerCallbackRegistrationChanged(int atomId,bool registered)508 void StatsdStats::notePullerCallbackRegistrationChanged(int atomId, bool registered) {
509 lock_guard<std::mutex> lock(mLock);
510 if (registered) {
511 mPulledAtomStats[atomId].registeredCount++;
512 } else {
513 mPulledAtomStats[atomId].unregisteredCount++;
514 }
515 }
516
noteHardDimensionLimitReached(int64_t metricId)517 void StatsdStats::noteHardDimensionLimitReached(int64_t metricId) {
518 lock_guard<std::mutex> lock(mLock);
519 getAtomMetricStats(metricId).hardDimensionLimitReached++;
520 }
521
noteLateLogEventSkipped(int64_t metricId)522 void StatsdStats::noteLateLogEventSkipped(int64_t metricId) {
523 lock_guard<std::mutex> lock(mLock);
524 getAtomMetricStats(metricId).lateLogEventSkipped++;
525 }
526
noteSkippedForwardBuckets(int64_t metricId)527 void StatsdStats::noteSkippedForwardBuckets(int64_t metricId) {
528 lock_guard<std::mutex> lock(mLock);
529 getAtomMetricStats(metricId).skippedForwardBuckets++;
530 }
531
noteBadValueType(int64_t metricId)532 void StatsdStats::noteBadValueType(int64_t metricId) {
533 lock_guard<std::mutex> lock(mLock);
534 getAtomMetricStats(metricId).badValueType++;
535 }
536
noteBucketDropped(int64_t metricId)537 void StatsdStats::noteBucketDropped(int64_t metricId) {
538 lock_guard<std::mutex> lock(mLock);
539 getAtomMetricStats(metricId).bucketDropped++;
540 }
541
noteBucketUnknownCondition(int64_t metricId)542 void StatsdStats::noteBucketUnknownCondition(int64_t metricId) {
543 lock_guard<std::mutex> lock(mLock);
544 getAtomMetricStats(metricId).bucketUnknownCondition++;
545 }
546
noteConditionChangeInNextBucket(int64_t metricId)547 void StatsdStats::noteConditionChangeInNextBucket(int64_t metricId) {
548 lock_guard<std::mutex> lock(mLock);
549 getAtomMetricStats(metricId).conditionChangeInNextBucket++;
550 }
551
noteInvalidatedBucket(int64_t metricId)552 void StatsdStats::noteInvalidatedBucket(int64_t metricId) {
553 lock_guard<std::mutex> lock(mLock);
554 getAtomMetricStats(metricId).invalidatedBucket++;
555 }
556
noteBucketCount(int64_t metricId)557 void StatsdStats::noteBucketCount(int64_t metricId) {
558 lock_guard<std::mutex> lock(mLock);
559 getAtomMetricStats(metricId).bucketCount++;
560 }
561
noteBucketBoundaryDelayNs(int64_t metricId,int64_t timeDelayNs)562 void StatsdStats::noteBucketBoundaryDelayNs(int64_t metricId, int64_t timeDelayNs) {
563 lock_guard<std::mutex> lock(mLock);
564 AtomMetricStats& metricStats = getAtomMetricStats(metricId);
565 metricStats.maxBucketBoundaryDelayNs =
566 std::max(metricStats.maxBucketBoundaryDelayNs, timeDelayNs);
567 metricStats.minBucketBoundaryDelayNs =
568 std::min(metricStats.minBucketBoundaryDelayNs, timeDelayNs);
569 }
570
noteAtomError(int atomTag,bool pull)571 void StatsdStats::noteAtomError(int atomTag, bool pull) {
572 lock_guard<std::mutex> lock(mLock);
573 if (pull) {
574 mPulledAtomStats[atomTag].atomErrorCount++;
575 return;
576 }
577
578 bool present = (mPushedAtomErrorStats.find(atomTag) != mPushedAtomErrorStats.end());
579 bool full = (mPushedAtomErrorStats.size() >= (size_t)kMaxPushedAtomErrorStatsSize);
580 if (!full || present) {
581 mPushedAtomErrorStats[atomTag]++;
582 }
583 }
584
getAtomMetricStats(int64_t metricId)585 StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int64_t metricId) {
586 auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
587 if (atomMetricStatsIter != mAtomMetricStats.end()) {
588 return atomMetricStatsIter->second;
589 }
590 auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
591 return emplaceResult.first->second;
592 }
593
reset()594 void StatsdStats::reset() {
595 lock_guard<std::mutex> lock(mLock);
596 resetInternalLocked();
597 }
598
resetInternalLocked()599 void StatsdStats::resetInternalLocked() {
600 // Reset the historical data, but keep the active ConfigStats
601 mStartTimeSec = getWallClockSec();
602 mIceBox.clear();
603 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
604 mNonPlatformPushedAtomStats.clear();
605 mAnomalyAlarmRegisteredStats = 0;
606 mPeriodicAlarmRegisteredStats = 0;
607 mSystemServerRestartSec.clear();
608 mLogLossStats.clear();
609 mOverflowCount = 0;
610 mMinQueueHistoryNs = kInt64Max;
611 mMaxQueueHistoryNs = 0;
612 for (auto& config : mConfigStats) {
613 config.second->broadcast_sent_time_sec.clear();
614 config.second->activation_time_sec.clear();
615 config.second->deactivation_time_sec.clear();
616 config.second->data_drop_time_sec.clear();
617 config.second->data_drop_bytes.clear();
618 config.second->dump_report_stats.clear();
619 config.second->annotations.clear();
620 config.second->matcher_stats.clear();
621 config.second->condition_stats.clear();
622 config.second->metric_stats.clear();
623 config.second->metric_dimension_in_condition_stats.clear();
624 config.second->alert_stats.clear();
625 }
626 for (auto& pullStats : mPulledAtomStats) {
627 pullStats.second.totalPull = 0;
628 pullStats.second.totalPullFromCache = 0;
629 pullStats.second.minPullIntervalSec = LONG_MAX;
630 pullStats.second.avgPullTimeNs = 0;
631 pullStats.second.maxPullTimeNs = 0;
632 pullStats.second.numPullTime = 0;
633 pullStats.second.avgPullDelayNs = 0;
634 pullStats.second.maxPullDelayNs = 0;
635 pullStats.second.numPullDelay = 0;
636 pullStats.second.dataError = 0;
637 pullStats.second.pullTimeout = 0;
638 pullStats.second.pullExceedMaxDelay = 0;
639 pullStats.second.pullFailed = 0;
640 pullStats.second.pullUidProviderNotFound = 0;
641 pullStats.second.pullerNotFound = 0;
642 pullStats.second.registeredCount = 0;
643 pullStats.second.unregisteredCount = 0;
644 pullStats.second.atomErrorCount = 0;
645 pullStats.second.binderCallFailCount = 0;
646 pullStats.second.pullTimeoutMetadata.clear();
647 }
648 mAtomMetricStats.clear();
649 mActivationBroadcastGuardrailStats.clear();
650 mPushedAtomErrorStats.clear();
651 }
652
buildTimeString(int64_t timeSec)653 string buildTimeString(int64_t timeSec) {
654 time_t t = timeSec;
655 struct tm* tm = localtime(&t);
656 char timeBuffer[80];
657 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
658 return string(timeBuffer);
659 }
660
getPushedAtomErrors(int atomId) const661 int StatsdStats::getPushedAtomErrors(int atomId) const {
662 const auto& it = mPushedAtomErrorStats.find(atomId);
663 if (it != mPushedAtomErrorStats.end()) {
664 return it->second;
665 } else {
666 return 0;
667 }
668 }
669
dumpStats(int out) const670 void StatsdStats::dumpStats(int out) const {
671 lock_guard<std::mutex> lock(mLock);
672 time_t t = mStartTimeSec;
673 struct tm* tm = localtime(&t);
674 char timeBuffer[80];
675 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
676 dprintf(out, "Stats collection start second: %s\n", timeBuffer);
677 dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
678 for (const auto& configStats : mIceBox) {
679 dprintf(out,
680 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
681 "#matcher=%d, #alert=%d, valid=%d\n",
682 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
683 configStats->deletion_time_sec, configStats->reset_time_sec,
684 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
685 configStats->alert_count, configStats->is_valid);
686
687 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
688 dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
689 }
690
691 for (const int& activationTime : configStats->activation_time_sec) {
692 dprintf(out, "\tactivation time: %d\n", activationTime);
693 }
694
695 for (const int& deactivationTime : configStats->deactivation_time_sec) {
696 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
697 }
698
699 auto dropTimePtr = configStats->data_drop_time_sec.begin();
700 auto dropBytesPtr = configStats->data_drop_bytes.begin();
701 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
702 i++, dropTimePtr++, dropBytesPtr++) {
703 dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
704 (long long)*dropBytesPtr);
705 }
706 }
707 dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
708 for (auto& pair : mConfigStats) {
709 auto& configStats = pair.second;
710 dprintf(out,
711 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
712 "#matcher=%d, #alert=%d, valid=%d\n",
713 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
714 configStats->deletion_time_sec, configStats->metric_count,
715 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
716 configStats->is_valid);
717 for (const auto& annotation : configStats->annotations) {
718 dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
719 annotation.second);
720 }
721
722 for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
723 dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
724 (long long)broadcastTime);
725 }
726
727 for (const int& activationTime : configStats->activation_time_sec) {
728 dprintf(out, "\tactivation time: %d\n", activationTime);
729 }
730
731 for (const int& deactivationTime : configStats->deactivation_time_sec) {
732 dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
733 }
734
735 auto dropTimePtr = configStats->data_drop_time_sec.begin();
736 auto dropBytesPtr = configStats->data_drop_bytes.begin();
737 for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
738 i++, dropTimePtr++, dropBytesPtr++) {
739 dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
740 buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
741 (long long)*dropBytesPtr);
742 }
743
744 for (const auto& dump : configStats->dump_report_stats) {
745 dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
746 buildTimeString(dump.first).c_str(), (long long)dump.first,
747 (long long)dump.second);
748 }
749
750 for (const auto& stats : pair.second->matcher_stats) {
751 dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
752 }
753
754 for (const auto& stats : pair.second->condition_stats) {
755 dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
756 stats.second);
757 }
758
759 for (const auto& stats : pair.second->condition_stats) {
760 dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
761 stats.second);
762 }
763
764 for (const auto& stats : pair.second->alert_stats) {
765 dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
766 }
767 }
768 dprintf(out, "********Disk Usage stats***********\n");
769 StorageManager::printStats(out);
770 dprintf(out, "********Pushed Atom stats***********\n");
771 const size_t atomCounts = mPushedAtomStats.size();
772 for (size_t i = 2; i < atomCounts; i++) {
773 if (mPushedAtomStats[i] > 0) {
774 dprintf(out, "Atom %zu->(total count)%d, (error count)%d\n", i, mPushedAtomStats[i],
775 getPushedAtomErrors((int)i));
776 }
777 }
778 for (const auto& pair : mNonPlatformPushedAtomStats) {
779 dprintf(out, "Atom %d->(total count)%d, (error count)%d\n", pair.first, pair.second,
780 getPushedAtomErrors(pair.first));
781 }
782
783 dprintf(out, "********Pulled Atom stats***********\n");
784 for (const auto& pair : mPulledAtomStats) {
785 dprintf(out,
786 "Atom %d->(total pull)%ld, (pull from cache)%ld, "
787 "(pull failed)%ld, (min pull interval)%ld \n"
788 " (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
789 "nanos)%lld, "
790 " (max pull delay nanos)%lld, (data error)%ld\n"
791 " (pull timeout)%ld, (pull exceed max delay)%ld"
792 " (no uid provider count)%ld, (no puller found count)%ld\n"
793 " (registered count) %ld, (unregistered count) %ld"
794 " (atom error count) %d\n",
795 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
796 (long)pair.second.pullFailed, (long)pair.second.minPullIntervalSec,
797 (long long)pair.second.avgPullTimeNs, (long long)pair.second.maxPullTimeNs,
798 (long long)pair.second.avgPullDelayNs, (long long)pair.second.maxPullDelayNs,
799 pair.second.dataError, pair.second.pullTimeout, pair.second.pullExceedMaxDelay,
800 pair.second.pullUidProviderNotFound, pair.second.pullerNotFound,
801 pair.second.registeredCount, pair.second.unregisteredCount,
802 pair.second.atomErrorCount);
803 if (pair.second.pullTimeoutMetadata.size() > 0) {
804 string uptimeMillis = "(pull timeout system uptime millis) ";
805 string pullTimeoutMillis = "(pull timeout elapsed time millis) ";
806 for (const auto& stats : pair.second.pullTimeoutMetadata) {
807 uptimeMillis.append(to_string(stats.pullTimeoutUptimeMillis)).append(",");;
808 pullTimeoutMillis.append(to_string(stats.pullTimeoutElapsedMillis)).append(",");
809 }
810 uptimeMillis.pop_back();
811 uptimeMillis.push_back('\n');
812 pullTimeoutMillis.pop_back();
813 pullTimeoutMillis.push_back('\n');
814 dprintf(out, "%s", uptimeMillis.c_str());
815 dprintf(out, "%s", pullTimeoutMillis.c_str());
816 }
817 }
818
819 if (mAnomalyAlarmRegisteredStats > 0) {
820 dprintf(out, "********AnomalyAlarmStats stats***********\n");
821 dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
822 }
823
824 if (mPeriodicAlarmRegisteredStats > 0) {
825 dprintf(out, "********SubscriberAlarmStats stats***********\n");
826 dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
827 }
828
829 dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
830 mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
831 mUidMapStats.dropped_changes);
832
833 for (const auto& restart : mSystemServerRestartSec) {
834 dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
835 (long long)restart);
836 }
837
838 for (const auto& loss : mLogLossStats) {
839 dprintf(out,
840 "Log loss: %lld (wall clock sec) - %d (count), %d (last error), %d (last tag), %d "
841 "(uid), %d (pid)\n",
842 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError, loss.mLastTag,
843 loss.mUid, loss.mPid);
844 }
845
846 dprintf(out, "Event queue overflow: %d; MaxHistoryNs: %lld; MinHistoryNs: %lld\n",
847 mOverflowCount, (long long)mMaxQueueHistoryNs, (long long)mMinQueueHistoryNs);
848
849 if (mActivationBroadcastGuardrailStats.size() > 0) {
850 dprintf(out, "********mActivationBroadcastGuardrail stats***********\n");
851 for (const auto& pair: mActivationBroadcastGuardrailStats) {
852 dprintf(out, "Uid %d: Times: ", pair.first);
853 for (const auto& guardrailHitTime : pair.second) {
854 dprintf(out, "%d ", guardrailHitTime);
855 }
856 }
857 dprintf(out, "\n");
858 }
859 }
860
addConfigStatsToProto(const ConfigStats & configStats,ProtoOutputStream * proto)861 void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
862 uint64_t token =
863 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
864 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
865 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
866 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
867 if (configStats.reset_time_sec != 0) {
868 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
869 }
870 if (configStats.deletion_time_sec != 0) {
871 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
872 configStats.deletion_time_sec);
873 }
874 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
875 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
876 configStats.condition_count);
877 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
878 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
879 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
880
881 for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
882 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
883 broadcast);
884 }
885
886 for (const auto& activation : configStats.activation_time_sec) {
887 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ACTIVATION | FIELD_COUNT_REPEATED,
888 activation);
889 }
890
891 for (const auto& deactivation : configStats.deactivation_time_sec) {
892 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DEACTIVATION | FIELD_COUNT_REPEATED,
893 deactivation);
894 }
895
896 for (const auto& drop_time : configStats.data_drop_time_sec) {
897 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
898 drop_time);
899 }
900
901 for (const auto& drop_bytes : configStats.data_drop_bytes) {
902 proto->write(
903 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
904 (long long)drop_bytes);
905 }
906
907 for (const auto& dump : configStats.dump_report_stats) {
908 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
909 FIELD_COUNT_REPEATED,
910 dump.first);
911 }
912
913 for (const auto& dump : configStats.dump_report_stats) {
914 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
915 FIELD_COUNT_REPEATED,
916 (long long)dump.second);
917 }
918
919 for (const auto& annotation : configStats.annotations) {
920 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
921 FIELD_ID_CONFIG_STATS_ANNOTATION);
922 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
923 (long long)annotation.first);
924 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
925 proto->end(token);
926 }
927
928 for (const auto& pair : configStats.matcher_stats) {
929 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
930 FIELD_ID_CONFIG_STATS_MATCHER_STATS);
931 proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
932 proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
933 proto->end(tmpToken);
934 }
935
936 for (const auto& pair : configStats.condition_stats) {
937 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
938 FIELD_ID_CONFIG_STATS_CONDITION_STATS);
939 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
940 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
941 proto->end(tmpToken);
942 }
943
944 for (const auto& pair : configStats.metric_stats) {
945 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
946 FIELD_ID_CONFIG_STATS_METRIC_STATS);
947 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
948 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
949 proto->end(tmpToken);
950 }
951 for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
952 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
953 FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
954 proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
955 proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
956 proto->end(tmpToken);
957 }
958
959 for (const auto& pair : configStats.alert_stats) {
960 uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
961 FIELD_ID_CONFIG_STATS_ALERT_STATS);
962 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
963 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
964 proto->end(tmpToken);
965 }
966
967 proto->end(token);
968 }
969
dumpStats(std::vector<uint8_t> * output,bool reset)970 void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
971 lock_guard<std::mutex> lock(mLock);
972
973 ProtoOutputStream proto;
974 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
975 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
976
977 for (const auto& configStats : mIceBox) {
978 addConfigStatsToProto(*configStats, &proto);
979 }
980
981 for (auto& pair : mConfigStats) {
982 addConfigStatsToProto(*(pair.second), &proto);
983 }
984
985 const size_t atomCounts = mPushedAtomStats.size();
986 for (size_t i = 2; i < atomCounts; i++) {
987 if (mPushedAtomStats[i] > 0) {
988 uint64_t token =
989 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
990 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
991 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
992 int errors = getPushedAtomErrors(i);
993 if (errors > 0) {
994 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_ERROR_COUNT, errors);
995 }
996 proto.end(token);
997 }
998 }
999
1000 for (const auto& pair : mNonPlatformPushedAtomStats) {
1001 uint64_t token =
1002 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
1003 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, pair.first);
1004 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, pair.second);
1005 int errors = getPushedAtomErrors(pair.first);
1006 if (errors > 0) {
1007 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_ERROR_COUNT, errors);
1008 }
1009 proto.end(token);
1010 }
1011
1012 for (const auto& pair : mPulledAtomStats) {
1013 android::os::statsd::writePullerStatsToStream(pair, &proto);
1014 }
1015
1016 for (const auto& pair : mAtomMetricStats) {
1017 android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
1018 }
1019
1020 if (mAnomalyAlarmRegisteredStats > 0) {
1021 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
1022 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
1023 mAnomalyAlarmRegisteredStats);
1024 proto.end(token);
1025 }
1026
1027 if (mPeriodicAlarmRegisteredStats > 0) {
1028 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
1029 proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
1030 mPeriodicAlarmRegisteredStats);
1031 proto.end(token);
1032 }
1033
1034 uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
1035 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
1036 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
1037 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
1038 proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
1039 proto.end(uidMapToken);
1040
1041 for (const auto& error : mLogLossStats) {
1042 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
1043 FIELD_COUNT_REPEATED);
1044 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
1045 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
1046 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
1047 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TAG, error.mLastTag);
1048 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_UID, error.mUid);
1049 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_PID, error.mPid);
1050 proto.end(token);
1051 }
1052
1053 if (mOverflowCount > 0) {
1054 uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_OVERFLOW);
1055 proto.write(FIELD_TYPE_INT32 | FIELD_ID_OVERFLOW_COUNT, (int32_t)mOverflowCount);
1056 proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MAX_HISTORY,
1057 (long long)mMaxQueueHistoryNs);
1058 proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MIN_HISTORY,
1059 (long long)mMinQueueHistoryNs);
1060 proto.end(token);
1061 }
1062
1063 for (const auto& restart : mSystemServerRestartSec) {
1064 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
1065 restart);
1066 }
1067
1068 for (const auto& pair: mActivationBroadcastGuardrailStats) {
1069 uint64_t token = proto.start(FIELD_TYPE_MESSAGE |
1070 FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL |
1071 FIELD_COUNT_REPEATED);
1072 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL_UID,
1073 (int32_t) pair.first);
1074 for (const auto& guardrailHitTime : pair.second) {
1075 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL_TIME |
1076 FIELD_COUNT_REPEATED,
1077 guardrailHitTime);
1078 }
1079 proto.end(token);
1080 }
1081
1082 output->clear();
1083 size_t bufferSize = proto.size();
1084 output->resize(bufferSize);
1085
1086 size_t pos = 0;
1087 sp<android::util::ProtoReader> reader = proto.data();
1088 while (reader->readBuffer() != NULL) {
1089 size_t toRead = reader->currentToRead();
1090 std::memcpy(&((*output)[pos]), reader->readBuffer(), toRead);
1091 pos += toRead;
1092 reader->move(toRead);
1093 }
1094
1095 if (reset) {
1096 resetInternalLocked();
1097 }
1098
1099 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
1100 }
1101
1102 } // namespace statsd
1103 } // namespace os
1104 } // namespace android
1105