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 <= kMaxPushedAtomId) {
463         mPushedAtomStats[atomId]++;
464     } else {
465         if (mNonPlatformPushedAtomStats.size() < kMaxNonPlatformPushedAtoms) {
466             mNonPlatformPushedAtomStats[atomId]++;
467         }
468     }
469 }
470 
noteSystemServerRestart(int32_t timeSec)471 void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
472     lock_guard<std::mutex> lock(mLock);
473 
474     if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
475         mSystemServerRestartSec.pop_front();
476     }
477     mSystemServerRestartSec.push_back(timeSec);
478 }
479 
notePullFailed(int atomId)480 void StatsdStats::notePullFailed(int atomId) {
481     lock_guard<std::mutex> lock(mLock);
482     mPulledAtomStats[atomId].pullFailed++;
483 }
484 
notePullUidProviderNotFound(int atomId)485 void StatsdStats::notePullUidProviderNotFound(int atomId) {
486     lock_guard<std::mutex> lock(mLock);
487     mPulledAtomStats[atomId].pullUidProviderNotFound++;
488 }
489 
notePullerNotFound(int atomId)490 void StatsdStats::notePullerNotFound(int atomId) {
491     lock_guard<std::mutex> lock(mLock);
492     mPulledAtomStats[atomId].pullerNotFound++;
493 }
494 
notePullBinderCallFailed(int atomId)495 void StatsdStats::notePullBinderCallFailed(int atomId) {
496     lock_guard<std::mutex> lock(mLock);
497     mPulledAtomStats[atomId].binderCallFailCount++;
498 }
499 
noteEmptyData(int atomId)500 void StatsdStats::noteEmptyData(int atomId) {
501     lock_guard<std::mutex> lock(mLock);
502     mPulledAtomStats[atomId].emptyData++;
503 }
504 
notePullerCallbackRegistrationChanged(int atomId,bool registered)505 void StatsdStats::notePullerCallbackRegistrationChanged(int atomId, bool registered) {
506     lock_guard<std::mutex> lock(mLock);
507     if (registered) {
508         mPulledAtomStats[atomId].registeredCount++;
509     } else {
510         mPulledAtomStats[atomId].unregisteredCount++;
511     }
512 }
513 
noteHardDimensionLimitReached(int64_t metricId)514 void StatsdStats::noteHardDimensionLimitReached(int64_t metricId) {
515     lock_guard<std::mutex> lock(mLock);
516     getAtomMetricStats(metricId).hardDimensionLimitReached++;
517 }
518 
noteLateLogEventSkipped(int64_t metricId)519 void StatsdStats::noteLateLogEventSkipped(int64_t metricId) {
520     lock_guard<std::mutex> lock(mLock);
521     getAtomMetricStats(metricId).lateLogEventSkipped++;
522 }
523 
noteSkippedForwardBuckets(int64_t metricId)524 void StatsdStats::noteSkippedForwardBuckets(int64_t metricId) {
525     lock_guard<std::mutex> lock(mLock);
526     getAtomMetricStats(metricId).skippedForwardBuckets++;
527 }
528 
noteBadValueType(int64_t metricId)529 void StatsdStats::noteBadValueType(int64_t metricId) {
530     lock_guard<std::mutex> lock(mLock);
531     getAtomMetricStats(metricId).badValueType++;
532 }
533 
noteBucketDropped(int64_t metricId)534 void StatsdStats::noteBucketDropped(int64_t metricId) {
535     lock_guard<std::mutex> lock(mLock);
536     getAtomMetricStats(metricId).bucketDropped++;
537 }
538 
noteBucketUnknownCondition(int64_t metricId)539 void StatsdStats::noteBucketUnknownCondition(int64_t metricId) {
540     lock_guard<std::mutex> lock(mLock);
541     getAtomMetricStats(metricId).bucketUnknownCondition++;
542 }
543 
noteConditionChangeInNextBucket(int64_t metricId)544 void StatsdStats::noteConditionChangeInNextBucket(int64_t metricId) {
545     lock_guard<std::mutex> lock(mLock);
546     getAtomMetricStats(metricId).conditionChangeInNextBucket++;
547 }
548 
noteInvalidatedBucket(int64_t metricId)549 void StatsdStats::noteInvalidatedBucket(int64_t metricId) {
550     lock_guard<std::mutex> lock(mLock);
551     getAtomMetricStats(metricId).invalidatedBucket++;
552 }
553 
noteBucketCount(int64_t metricId)554 void StatsdStats::noteBucketCount(int64_t metricId) {
555     lock_guard<std::mutex> lock(mLock);
556     getAtomMetricStats(metricId).bucketCount++;
557 }
558 
noteBucketBoundaryDelayNs(int64_t metricId,int64_t timeDelayNs)559 void StatsdStats::noteBucketBoundaryDelayNs(int64_t metricId, int64_t timeDelayNs) {
560     lock_guard<std::mutex> lock(mLock);
561     AtomMetricStats& pullStats = getAtomMetricStats(metricId);
562     pullStats.maxBucketBoundaryDelayNs =
563             std::max(pullStats.maxBucketBoundaryDelayNs, timeDelayNs);
564     pullStats.minBucketBoundaryDelayNs =
565             std::min(pullStats.minBucketBoundaryDelayNs, timeDelayNs);
566 }
567 
noteAtomError(int atomTag,bool pull)568 void StatsdStats::noteAtomError(int atomTag, bool pull) {
569     lock_guard<std::mutex> lock(mLock);
570     if (pull) {
571         mPulledAtomStats[atomTag].atomErrorCount++;
572         return;
573     }
574 
575     bool present = (mPushedAtomErrorStats.find(atomTag) != mPushedAtomErrorStats.end());
576     bool full = (mPushedAtomErrorStats.size() >= (size_t)kMaxPushedAtomErrorStatsSize);
577     if (!full || present) {
578         mPushedAtomErrorStats[atomTag]++;
579     }
580 }
581 
getAtomMetricStats(int64_t metricId)582 StatsdStats::AtomMetricStats& StatsdStats::getAtomMetricStats(int64_t metricId) {
583     auto atomMetricStatsIter = mAtomMetricStats.find(metricId);
584     if (atomMetricStatsIter != mAtomMetricStats.end()) {
585         return atomMetricStatsIter->second;
586     }
587     auto emplaceResult = mAtomMetricStats.emplace(metricId, AtomMetricStats());
588     return emplaceResult.first->second;
589 }
590 
reset()591 void StatsdStats::reset() {
592     lock_guard<std::mutex> lock(mLock);
593     resetInternalLocked();
594 }
595 
resetInternalLocked()596 void StatsdStats::resetInternalLocked() {
597     // Reset the historical data, but keep the active ConfigStats
598     mStartTimeSec = getWallClockSec();
599     mIceBox.clear();
600     std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
601     mNonPlatformPushedAtomStats.clear();
602     mAnomalyAlarmRegisteredStats = 0;
603     mPeriodicAlarmRegisteredStats = 0;
604     mSystemServerRestartSec.clear();
605     mLogLossStats.clear();
606     mOverflowCount = 0;
607     mMinQueueHistoryNs = kInt64Max;
608     mMaxQueueHistoryNs = 0;
609     for (auto& config : mConfigStats) {
610         config.second->broadcast_sent_time_sec.clear();
611         config.second->activation_time_sec.clear();
612         config.second->deactivation_time_sec.clear();
613         config.second->data_drop_time_sec.clear();
614         config.second->data_drop_bytes.clear();
615         config.second->dump_report_stats.clear();
616         config.second->annotations.clear();
617         config.second->matcher_stats.clear();
618         config.second->condition_stats.clear();
619         config.second->metric_stats.clear();
620         config.second->metric_dimension_in_condition_stats.clear();
621         config.second->alert_stats.clear();
622     }
623     for (auto& pullStats : mPulledAtomStats) {
624         pullStats.second.totalPull = 0;
625         pullStats.second.totalPullFromCache = 0;
626         pullStats.second.minPullIntervalSec = LONG_MAX;
627         pullStats.second.avgPullTimeNs = 0;
628         pullStats.second.maxPullTimeNs = 0;
629         pullStats.second.numPullTime = 0;
630         pullStats.second.avgPullDelayNs = 0;
631         pullStats.second.maxPullDelayNs = 0;
632         pullStats.second.numPullDelay = 0;
633         pullStats.second.dataError = 0;
634         pullStats.second.pullTimeout = 0;
635         pullStats.second.pullExceedMaxDelay = 0;
636         pullStats.second.pullFailed = 0;
637         pullStats.second.pullUidProviderNotFound = 0;
638         pullStats.second.pullerNotFound = 0;
639         pullStats.second.registeredCount = 0;
640         pullStats.second.unregisteredCount = 0;
641         pullStats.second.atomErrorCount = 0;
642         pullStats.second.binderCallFailCount = 0;
643         pullStats.second.pullTimeoutMetadata.clear();
644     }
645     mAtomMetricStats.clear();
646     mActivationBroadcastGuardrailStats.clear();
647     mPushedAtomErrorStats.clear();
648 }
649 
buildTimeString(int64_t timeSec)650 string buildTimeString(int64_t timeSec) {
651     time_t t = timeSec;
652     struct tm* tm = localtime(&t);
653     char timeBuffer[80];
654     strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
655     return string(timeBuffer);
656 }
657 
getPushedAtomErrors(int atomId) const658 int StatsdStats::getPushedAtomErrors(int atomId) const {
659     const auto& it = mPushedAtomErrorStats.find(atomId);
660     if (it != mPushedAtomErrorStats.end()) {
661         return it->second;
662     } else {
663         return 0;
664     }
665 }
666 
dumpStats(int out) const667 void StatsdStats::dumpStats(int out) const {
668     lock_guard<std::mutex> lock(mLock);
669     time_t t = mStartTimeSec;
670     struct tm* tm = localtime(&t);
671     char timeBuffer[80];
672     strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
673     dprintf(out, "Stats collection start second: %s\n", timeBuffer);
674     dprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
675     for (const auto& configStats : mIceBox) {
676         dprintf(out,
677                 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
678                 "#matcher=%d, #alert=%d,  valid=%d\n",
679                 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
680                 configStats->deletion_time_sec, configStats->reset_time_sec,
681                 configStats->metric_count, configStats->condition_count, configStats->matcher_count,
682                 configStats->alert_count, configStats->is_valid);
683 
684         for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
685             dprintf(out, "\tbroadcast time: %d\n", broadcastTime);
686         }
687 
688         for (const int& activationTime : configStats->activation_time_sec) {
689             dprintf(out, "\tactivation time: %d\n", activationTime);
690         }
691 
692         for (const int& deactivationTime : configStats->deactivation_time_sec) {
693             dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
694         }
695 
696         auto dropTimePtr = configStats->data_drop_time_sec.begin();
697         auto dropBytesPtr = configStats->data_drop_bytes.begin();
698         for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
699              i++, dropTimePtr++, dropBytesPtr++) {
700             dprintf(out, "\tdata drop time: %d with size %lld", *dropTimePtr,
701                     (long long)*dropBytesPtr);
702         }
703     }
704     dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
705     for (auto& pair : mConfigStats) {
706         auto& configStats = pair.second;
707         dprintf(out,
708                 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
709                 "#matcher=%d, #alert=%d,  valid=%d\n",
710                 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
711                 configStats->deletion_time_sec, configStats->metric_count,
712                 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
713                 configStats->is_valid);
714         for (const auto& annotation : configStats->annotations) {
715             dprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
716                     annotation.second);
717         }
718 
719         for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
720             dprintf(out, "\tbroadcast time: %s(%lld)\n", buildTimeString(broadcastTime).c_str(),
721                     (long long)broadcastTime);
722         }
723 
724         for (const int& activationTime : configStats->activation_time_sec) {
725             dprintf(out, "\tactivation time: %d\n", activationTime);
726         }
727 
728         for (const int& deactivationTime : configStats->deactivation_time_sec) {
729             dprintf(out, "\tdeactivation time: %d\n", deactivationTime);
730         }
731 
732         auto dropTimePtr = configStats->data_drop_time_sec.begin();
733         auto dropBytesPtr = configStats->data_drop_bytes.begin();
734         for (int i = 0; i < (int)configStats->data_drop_time_sec.size();
735              i++, dropTimePtr++, dropBytesPtr++) {
736             dprintf(out, "\tdata drop time: %s(%lld) with %lld bytes\n",
737                     buildTimeString(*dropTimePtr).c_str(), (long long)*dropTimePtr,
738                     (long long)*dropBytesPtr);
739         }
740 
741         for (const auto& dump : configStats->dump_report_stats) {
742             dprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
743                     buildTimeString(dump.first).c_str(), (long long)dump.first,
744                     (long long)dump.second);
745         }
746 
747         for (const auto& stats : pair.second->matcher_stats) {
748             dprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
749         }
750 
751         for (const auto& stats : pair.second->condition_stats) {
752             dprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
753                     stats.second);
754         }
755 
756         for (const auto& stats : pair.second->condition_stats) {
757             dprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
758                     stats.second);
759         }
760 
761         for (const auto& stats : pair.second->alert_stats) {
762             dprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
763         }
764     }
765     dprintf(out, "********Disk Usage stats***********\n");
766     StorageManager::printStats(out);
767     dprintf(out, "********Pushed Atom stats***********\n");
768     const size_t atomCounts = mPushedAtomStats.size();
769     for (size_t i = 2; i < atomCounts; i++) {
770         if (mPushedAtomStats[i] > 0) {
771             dprintf(out, "Atom %zu->(total count)%d, (error count)%d\n", i, mPushedAtomStats[i],
772                     getPushedAtomErrors((int)i));
773         }
774     }
775     for (const auto& pair : mNonPlatformPushedAtomStats) {
776         dprintf(out, "Atom %d->(total count)%d, (error count)%d\n", pair.first, pair.second,
777                 getPushedAtomErrors(pair.first));
778     }
779 
780     dprintf(out, "********Pulled Atom stats***********\n");
781     for (const auto& pair : mPulledAtomStats) {
782         dprintf(out,
783                 "Atom %d->(total pull)%ld, (pull from cache)%ld, "
784                 "(pull failed)%ld, (min pull interval)%ld \n"
785                 "  (average pull time nanos)%lld, (max pull time nanos)%lld, (average pull delay "
786                 "nanos)%lld, "
787                 "  (max pull delay nanos)%lld, (data error)%ld\n"
788                 "  (pull timeout)%ld, (pull exceed max delay)%ld"
789                 "  (no uid provider count)%ld, (no puller found count)%ld\n"
790                 "  (registered count) %ld, (unregistered count) %ld"
791                 "  (atom error count) %d\n",
792                 (int)pair.first, (long)pair.second.totalPull, (long)pair.second.totalPullFromCache,
793                 (long)pair.second.pullFailed, (long)pair.second.minPullIntervalSec,
794                 (long long)pair.second.avgPullTimeNs, (long long)pair.second.maxPullTimeNs,
795                 (long long)pair.second.avgPullDelayNs, (long long)pair.second.maxPullDelayNs,
796                 pair.second.dataError, pair.second.pullTimeout, pair.second.pullExceedMaxDelay,
797                 pair.second.pullUidProviderNotFound, pair.second.pullerNotFound,
798                 pair.second.registeredCount, pair.second.unregisteredCount,
799                 pair.second.atomErrorCount);
800         if (pair.second.pullTimeoutMetadata.size() > 0) {
801             string uptimeMillis = "(pull timeout system uptime millis) ";
802             string pullTimeoutMillis = "(pull timeout elapsed time millis) ";
803             for (const auto& stats : pair.second.pullTimeoutMetadata) {
804                 uptimeMillis.append(to_string(stats.pullTimeoutUptimeMillis)).append(",");;
805                 pullTimeoutMillis.append(to_string(stats.pullTimeoutElapsedMillis)).append(",");
806             }
807             uptimeMillis.pop_back();
808             uptimeMillis.push_back('\n');
809             pullTimeoutMillis.pop_back();
810             pullTimeoutMillis.push_back('\n');
811             dprintf(out, "%s", uptimeMillis.c_str());
812             dprintf(out, "%s", pullTimeoutMillis.c_str());
813         }
814     }
815 
816     if (mAnomalyAlarmRegisteredStats > 0) {
817         dprintf(out, "********AnomalyAlarmStats stats***********\n");
818         dprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
819     }
820 
821     if (mPeriodicAlarmRegisteredStats > 0) {
822         dprintf(out, "********SubscriberAlarmStats stats***********\n");
823         dprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
824     }
825 
826     dprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
827             mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
828             mUidMapStats.dropped_changes);
829 
830     for (const auto& restart : mSystemServerRestartSec) {
831         dprintf(out, "System server restarts at %s(%lld)\n", buildTimeString(restart).c_str(),
832                 (long long)restart);
833     }
834 
835     for (const auto& loss : mLogLossStats) {
836         dprintf(out,
837                 "Log loss: %lld (wall clock sec) - %d (count), %d (last error), %d (last tag), %d "
838                 "(uid), %d (pid)\n",
839                 (long long)loss.mWallClockSec, loss.mCount, loss.mLastError, loss.mLastTag,
840                 loss.mUid, loss.mPid);
841     }
842 
843     dprintf(out, "Event queue overflow: %d; MaxHistoryNs: %lld; MinHistoryNs: %lld\n",
844             mOverflowCount, (long long)mMaxQueueHistoryNs, (long long)mMinQueueHistoryNs);
845 
846     if (mActivationBroadcastGuardrailStats.size() > 0) {
847         dprintf(out, "********mActivationBroadcastGuardrail stats***********\n");
848         for (const auto& pair: mActivationBroadcastGuardrailStats) {
849             dprintf(out, "Uid %d: Times: ", pair.first);
850             for (const auto& guardrailHitTime : pair.second) {
851                 dprintf(out, "%d ", guardrailHitTime);
852             }
853         }
854         dprintf(out, "\n");
855     }
856 }
857 
addConfigStatsToProto(const ConfigStats & configStats,ProtoOutputStream * proto)858 void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
859     uint64_t token =
860             proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
861     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
862     proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
863     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
864     if (configStats.reset_time_sec != 0) {
865         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
866     }
867     if (configStats.deletion_time_sec != 0) {
868         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
869                      configStats.deletion_time_sec);
870     }
871     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
872     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
873                  configStats.condition_count);
874     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
875     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
876     proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
877 
878     for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
879         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
880                      broadcast);
881     }
882 
883     for (const auto& activation : configStats.activation_time_sec) {
884         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ACTIVATION | FIELD_COUNT_REPEATED,
885                      activation);
886     }
887 
888     for (const auto& deactivation : configStats.deactivation_time_sec) {
889         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DEACTIVATION | FIELD_COUNT_REPEATED,
890                      deactivation);
891     }
892 
893     for (const auto& drop_time : configStats.data_drop_time_sec) {
894         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP_TIME | FIELD_COUNT_REPEATED,
895                      drop_time);
896     }
897 
898     for (const auto& drop_bytes : configStats.data_drop_bytes) {
899         proto->write(
900                 FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DATA_DROP_BYTES | FIELD_COUNT_REPEATED,
901                 (long long)drop_bytes);
902     }
903 
904     for (const auto& dump : configStats.dump_report_stats) {
905         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
906                      FIELD_COUNT_REPEATED,
907                      dump.first);
908     }
909 
910     for (const auto& dump : configStats.dump_report_stats) {
911         proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
912                      FIELD_COUNT_REPEATED,
913                      (long long)dump.second);
914     }
915 
916     for (const auto& annotation : configStats.annotations) {
917         uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
918                                       FIELD_ID_CONFIG_STATS_ANNOTATION);
919         proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
920                      (long long)annotation.first);
921         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
922         proto->end(token);
923     }
924 
925     for (const auto& pair : configStats.matcher_stats) {
926         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
927                                           FIELD_ID_CONFIG_STATS_MATCHER_STATS);
928         proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
929         proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
930         proto->end(tmpToken);
931     }
932 
933     for (const auto& pair : configStats.condition_stats) {
934         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
935                                           FIELD_ID_CONFIG_STATS_CONDITION_STATS);
936         proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
937         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
938         proto->end(tmpToken);
939     }
940 
941     for (const auto& pair : configStats.metric_stats) {
942         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
943                                           FIELD_ID_CONFIG_STATS_METRIC_STATS);
944         proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
945         proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
946         proto->end(tmpToken);
947     }
948     for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
949         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
950                                          FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
951         proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
952         proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
953         proto->end(tmpToken);
954     }
955 
956     for (const auto& pair : configStats.alert_stats) {
957         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
958                                           FIELD_ID_CONFIG_STATS_ALERT_STATS);
959         proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
960         proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
961         proto->end(tmpToken);
962     }
963 
964     proto->end(token);
965 }
966 
dumpStats(std::vector<uint8_t> * output,bool reset)967 void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
968     lock_guard<std::mutex> lock(mLock);
969 
970     ProtoOutputStream proto;
971     proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
972     proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
973 
974     for (const auto& configStats : mIceBox) {
975         addConfigStatsToProto(*configStats, &proto);
976     }
977 
978     for (auto& pair : mConfigStats) {
979         addConfigStatsToProto(*(pair.second), &proto);
980     }
981 
982     const size_t atomCounts = mPushedAtomStats.size();
983     for (size_t i = 2; i < atomCounts; i++) {
984         if (mPushedAtomStats[i] > 0) {
985             uint64_t token =
986                     proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
987             proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
988             proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
989             int errors = getPushedAtomErrors(i);
990             if (errors > 0) {
991                 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_ERROR_COUNT, errors);
992             }
993             proto.end(token);
994         }
995     }
996 
997     for (const auto& pair : mNonPlatformPushedAtomStats) {
998         uint64_t token =
999                 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
1000         proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, pair.first);
1001         proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, pair.second);
1002         int errors = getPushedAtomErrors(pair.first);
1003         if (errors > 0) {
1004             proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_ERROR_COUNT, errors);
1005         }
1006         proto.end(token);
1007     }
1008 
1009     for (const auto& pair : mPulledAtomStats) {
1010         android::os::statsd::writePullerStatsToStream(pair, &proto);
1011     }
1012 
1013     for (const auto& pair : mAtomMetricStats) {
1014         android::os::statsd::writeAtomMetricStatsToStream(pair, &proto);
1015     }
1016 
1017     if (mAnomalyAlarmRegisteredStats > 0) {
1018         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
1019         proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
1020                     mAnomalyAlarmRegisteredStats);
1021         proto.end(token);
1022     }
1023 
1024     if (mPeriodicAlarmRegisteredStats > 0) {
1025         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
1026         proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
1027                     mPeriodicAlarmRegisteredStats);
1028         proto.end(token);
1029     }
1030 
1031     uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
1032     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
1033     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
1034     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
1035     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
1036     proto.end(uidMapToken);
1037 
1038     for (const auto& error : mLogLossStats) {
1039         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
1040                                       FIELD_COUNT_REPEATED);
1041         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TIME, error.mWallClockSec);
1042         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_COUNT, error.mCount);
1043         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_ERROR, error.mLastError);
1044         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_TAG, error.mLastTag);
1045         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_UID, error.mUid);
1046         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOG_LOSS_STATS_PID, error.mPid);
1047         proto.end(token);
1048     }
1049 
1050     if (mOverflowCount > 0) {
1051         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_OVERFLOW);
1052         proto.write(FIELD_TYPE_INT32 | FIELD_ID_OVERFLOW_COUNT, (int32_t)mOverflowCount);
1053         proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MAX_HISTORY,
1054                     (long long)mMaxQueueHistoryNs);
1055         proto.write(FIELD_TYPE_INT64 | FIELD_ID_OVERFLOW_MIN_HISTORY,
1056                     (long long)mMinQueueHistoryNs);
1057         proto.end(token);
1058     }
1059 
1060     for (const auto& restart : mSystemServerRestartSec) {
1061         proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
1062                     restart);
1063     }
1064 
1065     for (const auto& pair: mActivationBroadcastGuardrailStats) {
1066         uint64_t token = proto.start(FIELD_TYPE_MESSAGE |
1067                                      FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL |
1068                                      FIELD_COUNT_REPEATED);
1069         proto.write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL_UID,
1070                     (int32_t) pair.first);
1071         for (const auto& guardrailHitTime : pair.second) {
1072             proto.write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVATION_BROADCAST_GUARDRAIL_TIME |
1073                             FIELD_COUNT_REPEATED,
1074                         guardrailHitTime);
1075         }
1076         proto.end(token);
1077     }
1078 
1079     output->clear();
1080     size_t bufferSize = proto.size();
1081     output->resize(bufferSize);
1082 
1083     size_t pos = 0;
1084     sp<android::util::ProtoReader> reader = proto.data();
1085     while (reader->readBuffer() != NULL) {
1086         size_t toRead = reader->currentToRead();
1087         std::memcpy(&((*output)[pos]), reader->readBuffer(), toRead);
1088         pos += toRead;
1089         reader->move(toRead);
1090     }
1091 
1092     if (reset) {
1093         resetInternalLocked();
1094     }
1095 
1096     VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
1097 }
1098 
1099 }  // namespace statsd
1100 }  // namespace os
1101 }  // namespace android
1102