blob: 77f54569cb4778cffad58646a502718cda0aaa00 [file] [log] [blame]
Yao Chenb3561512017-11-21 18:07:17 -08001/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#define DEBUG true // STOPSHIP if true
17#include "Log.h"
18
19#include "StatsdStats.h"
20
21#include <android/util/ProtoOutputStream.h>
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "../stats_log_util.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "statslog.h"
24
25namespace android {
26namespace os {
27namespace statsd {
28
29using android::util::FIELD_COUNT_REPEATED;
30using android::util::FIELD_TYPE_BOOL;
31using android::util::FIELD_TYPE_FLOAT;
32using android::util::FIELD_TYPE_INT32;
33using android::util::FIELD_TYPE_INT64;
34using android::util::FIELD_TYPE_MESSAGE;
35using android::util::FIELD_TYPE_STRING;
36using android::util::ProtoOutputStream;
37using std::lock_guard;
38using std::map;
39using std::string;
40using std::vector;
41
42const int FIELD_ID_BEGIN_TIME = 1;
43const int FIELD_ID_END_TIME = 2;
44const int FIELD_ID_CONFIG_STATS = 3;
Yao Chenb3561512017-11-21 18:07:17 -080045const int FIELD_ID_ATOM_STATS = 7;
David Chenc136f452017-11-27 11:52:26 -080046const int FIELD_ID_UIDMAP_STATS = 8;
Bookatz1d0136d2017-12-01 11:13:32 -080047const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
Yao Chen884c8c12018-01-26 10:36:25 -080048const int FIELD_ID_PULLED_ATOM_STATS = 10;
49const int FIELD_ID_LOGGER_ERROR_STATS = 11;
Yao Chenb3561512017-11-21 18:07:17 -080050
51const int FIELD_ID_MATCHER_STATS_NAME = 1;
52const int FIELD_ID_MATCHER_STATS_COUNT = 2;
53
54const int FIELD_ID_CONDITION_STATS_NAME = 1;
55const int FIELD_ID_CONDITION_STATS_COUNT = 2;
56
57const int FIELD_ID_METRIC_STATS_NAME = 1;
58const int FIELD_ID_METRIC_STATS_COUNT = 2;
59
60const int FIELD_ID_ATOM_STATS_TAG = 1;
61const int FIELD_ID_ATOM_STATS_COUNT = 2;
62
Bookatz1d0136d2017-12-01 11:13:32 -080063const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
64
Yao Chen884c8c12018-01-26 10:36:25 -080065const int FIELD_ID_LOGGER_STATS_TIME = 1;
66const int FIELD_ID_LOGGER_STATS_ERROR_CODE = 2;
67
Chenjie Yub038b702017-12-18 15:15:34 -080068std::map<int, long> StatsdStats::kPullerCooldownMap = {
69 {android::util::KERNEL_WAKELOCK, 1},
70 {android::util::WIFI_BYTES_TRANSFER, 1},
71 {android::util::MOBILE_BYTES_TRANSFER, 1},
72 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, 1},
73 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, 1},
Chenjie Yub038b702017-12-18 15:15:34 -080074 {android::util::SUBSYSTEM_SLEEP_STATE, 1},
75 {android::util::CPU_TIME_PER_FREQ, 1},
76 {android::util::CPU_TIME_PER_UID, 1},
77 {android::util::CPU_TIME_PER_UID_FREQ, 1},
78};
79
Yao Chenb3561512017-11-21 18:07:17 -080080// TODO: add stats for pulled atoms.
81StatsdStats::StatsdStats() {
82 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yao Chen69f1baf2017-11-27 17:25:36 -080083 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -080084}
85
86StatsdStats& StatsdStats::getInstance() {
87 static StatsdStats statsInstance;
88 return statsInstance;
89}
90
Yao Chenf6723df2018-01-08 15:11:58 -080091void StatsdStats::addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats) {
92 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
93 if (mIceBox.size() == kMaxIceBoxSize) {
94 mIceBox.pop_front();
95 }
96 mIceBox.push_back(stats);
97}
98
Yao Chenb3561512017-11-21 18:07:17 -080099void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
100 int matchersCount, int alertsCount, bool isValid) {
101 lock_guard<std::mutex> lock(mLock);
102 int32_t nowTimeSec = time(nullptr);
103
104 // If there is an existing config for the same key, icebox the old config.
105 noteConfigRemovedInternalLocked(key);
106
107 StatsdStatsReport_ConfigStats configStats;
108 configStats.set_uid(key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800109 configStats.set_id(key.GetId());
Yao Chenb3561512017-11-21 18:07:17 -0800110 configStats.set_creation_time_sec(nowTimeSec);
111 configStats.set_metric_count(metricsCount);
112 configStats.set_condition_count(conditionsCount);
113 configStats.set_matcher_count(matchersCount);
114 configStats.set_alert_count(alertsCount);
115 configStats.set_is_valid(isValid);
116
117 if (isValid) {
118 mConfigStats[key] = configStats;
119 } else {
120 configStats.set_deletion_time_sec(nowTimeSec);
Yao Chenf6723df2018-01-08 15:11:58 -0800121 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800122 }
123}
124
125void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
126 auto it = mConfigStats.find(key);
127 if (it != mConfigStats.end()) {
128 int32_t nowTimeSec = time(nullptr);
129 it->second.set_deletion_time_sec(nowTimeSec);
Bookatz8f2f3d82017-12-07 13:53:21 -0800130 // Add condition stats, metrics stats, matcher stats, alert stats
131 addSubStatsToConfigLocked(key, it->second);
Yao Chenb3561512017-11-21 18:07:17 -0800132 // Remove them after they are added to the config stats.
133 mMatcherStats.erase(key);
134 mMetricsStats.erase(key);
Bookatz8f2f3d82017-12-07 13:53:21 -0800135 mAlertStats.erase(key);
Yao Chenb3561512017-11-21 18:07:17 -0800136 mConditionStats.erase(key);
Yao Chenf6723df2018-01-08 15:11:58 -0800137 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800138 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800139 }
140}
141
142void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
143 lock_guard<std::mutex> lock(mLock);
144 noteConfigRemovedInternalLocked(key);
145}
146
147void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800148 noteBroadcastSent(key, time(nullptr));
149}
150
151void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800152 lock_guard<std::mutex> lock(mLock);
153 auto it = mConfigStats.find(key);
154 if (it == mConfigStats.end()) {
155 ALOGE("Config key %s not found!", key.ToString().c_str());
156 return;
157 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800158 if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) {
159 auto timestampList = it->second.mutable_broadcast_sent_time_sec();
160 // This is O(N) operation. It shouldn't happen often, and N is only 20.
161 timestampList->erase(timestampList->begin());
162 }
163 it->second.add_broadcast_sent_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800164}
165
Yao Chen69f1baf2017-11-27 17:25:36 -0800166void StatsdStats::noteDataDropped(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800167 noteDataDropped(key, time(nullptr));
168}
169
170void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800171 lock_guard<std::mutex> lock(mLock);
172 auto it = mConfigStats.find(key);
173 if (it == mConfigStats.end()) {
174 ALOGE("Config key %s not found!", key.ToString().c_str());
175 return;
176 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800177 if (it->second.data_drop_time_sec_size() >= kMaxTimestampCount) {
178 auto timestampList = it->second.mutable_data_drop_time_sec();
179 // This is O(N) operation. It shouldn't happen often, and N is only 20.
180 timestampList->erase(timestampList->begin());
181 }
182 it->second.add_data_drop_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800183}
184
Yao Chen69f1baf2017-11-27 17:25:36 -0800185void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800186 noteMetricsReportSent(key, time(nullptr));
187}
188
189void StatsdStats::noteMetricsReportSent(const ConfigKey& key, int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800190 lock_guard<std::mutex> lock(mLock);
191 auto it = mConfigStats.find(key);
192 if (it == mConfigStats.end()) {
193 ALOGE("Config key %s not found!", key.ToString().c_str());
194 return;
195 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800196 if (it->second.dump_report_time_sec_size() >= kMaxTimestampCount) {
197 auto timestampList = it->second.mutable_dump_report_time_sec();
198 // This is O(N) operation. It shouldn't happen often, and N is only 20.
199 timestampList->erase(timestampList->begin());
200 }
201 it->second.add_dump_report_time_sec(timeSec);
Yao Chen69f1baf2017-11-27 17:25:36 -0800202}
203
David Chenc136f452017-11-27 11:52:26 -0800204void StatsdStats::noteUidMapDropped(int snapshots, int deltas) {
205 lock_guard<std::mutex> lock(mLock);
206 mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots);
207 mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas);
208}
209
210void StatsdStats::setUidMapSnapshots(int snapshots) {
211 lock_guard<std::mutex> lock(mLock);
212 mUidMapStats.set_snapshots(snapshots);
213}
214
215void StatsdStats::setUidMapChanges(int changes) {
216 lock_guard<std::mutex> lock(mLock);
217 mUidMapStats.set_changes(changes);
218}
219
220void StatsdStats::setCurrentUidMapMemory(int bytes) {
221 lock_guard<std::mutex> lock(mLock);
222 mUidMapStats.set_bytes_used(bytes);
223}
224
Yangster-mac94e197c2018-01-02 16:03:03 -0800225void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800226 lock_guard<std::mutex> lock(mLock);
227 // if name doesn't exist before, it will create the key with count 0.
228 auto& conditionSizeMap = mConditionStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800229 if (size > conditionSizeMap[id]) {
230 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800231 }
232}
233
Yangster-mac94e197c2018-01-02 16:03:03 -0800234void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800235 lock_guard<std::mutex> lock(mLock);
236 // if name doesn't exist before, it will create the key with count 0.
237 auto& metricsDimensionMap = mMetricsStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800238 if (size > metricsDimensionMap[id]) {
239 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800240 }
241}
242
Yangster-mac94e197c2018-01-02 16:03:03 -0800243void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800244 lock_guard<std::mutex> lock(mLock);
245 auto& matcherStats = mMatcherStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800246 matcherStats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800247}
248
Yangster-mac94e197c2018-01-02 16:03:03 -0800249void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800250 lock_guard<std::mutex> lock(mLock);
251 auto& alertStats = mAlertStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800252 alertStats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800253}
254
Bookatz1d0136d2017-12-01 11:13:32 -0800255void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
256 lock_guard<std::mutex> lock(mLock);
257 mAnomalyAlarmRegisteredStats++;
258}
259
Chenjie Yub038b702017-12-18 15:15:34 -0800260void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
261 lock_guard<std::mutex> lock(mLock);
262 mPulledAtomStats[pullAtomId].minPullIntervalSec = intervalSec;
263}
264
265void StatsdStats::notePull(int pullAtomId) {
266 lock_guard<std::mutex> lock(mLock);
267 mPulledAtomStats[pullAtomId].totalPull++;
268}
269
270void StatsdStats::notePullFromCache(int pullAtomId) {
271 lock_guard<std::mutex> lock(mLock);
272 mPulledAtomStats[pullAtomId].totalPullFromCache++;
273}
274
Yao Chenb3561512017-11-21 18:07:17 -0800275void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
276 lock_guard<std::mutex> lock(mLock);
277
Yao Chen69f1baf2017-11-27 17:25:36 -0800278 if (timeSec < mStartTimeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800279 return;
280 }
281
282 if (atomId > android::util::kMaxPushedAtomId) {
283 ALOGW("not interested in atom %d", atomId);
284 return;
285 }
286
287 mPushedAtomStats[atomId]++;
288}
289
Yao Chen884c8c12018-01-26 10:36:25 -0800290void StatsdStats::noteLoggerError(int error) {
291 lock_guard<std::mutex> lock(mLock);
292 // grows strictly one at a time. so it won't > kMaxLoggerErrors
293 if (mLoggerErrors.size() == kMaxLoggerErrors) {
294 mLoggerErrors.pop_front();
295 }
296 mLoggerErrors.push_back(std::make_pair(time(nullptr), error));
297}
298
Yao Chenb3561512017-11-21 18:07:17 -0800299void StatsdStats::reset() {
300 lock_guard<std::mutex> lock(mLock);
301 resetInternalLocked();
302}
303
304void StatsdStats::resetInternalLocked() {
305 // Reset the historical data, but keep the active ConfigStats
Yao Chen69f1baf2017-11-27 17:25:36 -0800306 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -0800307 mIceBox.clear();
308 mConditionStats.clear();
309 mMetricsStats.clear();
310 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz8f2f3d82017-12-07 13:53:21 -0800311 mAlertStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800312 mAnomalyAlarmRegisteredStats = 0;
Yao Chenb3561512017-11-21 18:07:17 -0800313 mMatcherStats.clear();
Yao Chen884c8c12018-01-26 10:36:25 -0800314 mLoggerErrors.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800315 for (auto& config : mConfigStats) {
316 config.second.clear_broadcast_sent_time_sec();
317 config.second.clear_data_drop_time_sec();
318 config.second.clear_dump_report_time_sec();
319 config.second.clear_matcher_stats();
320 config.second.clear_condition_stats();
321 config.second.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800322 config.second.clear_alert_stats();
Yao Chen0fac5b12017-11-28 16:07:02 -0800323 }
Yao Chenb3561512017-11-21 18:07:17 -0800324}
325
Bookatz8f2f3d82017-12-07 13:53:21 -0800326void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key,
Yao Chenb3561512017-11-21 18:07:17 -0800327 StatsdStatsReport_ConfigStats& configStats) {
328 // Add matcher stats
329 if (mMatcherStats.find(key) != mMatcherStats.end()) {
330 const auto& matcherStats = mMatcherStats[key];
331 for (const auto& stats : matcherStats) {
332 auto output = configStats.add_matcher_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800333 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800334 output->set_matched_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800335 VLOG("matcher %lld matched %d times",
336 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800337 }
338 }
339 // Add condition stats
340 if (mConditionStats.find(key) != mConditionStats.end()) {
341 const auto& conditionStats = mConditionStats[key];
342 for (const auto& stats : conditionStats) {
343 auto output = configStats.add_condition_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800344 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800345 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800346 VLOG("condition %lld max output tuple size %d",
347 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800348 }
349 }
350 // Add metrics stats
351 if (mMetricsStats.find(key) != mMetricsStats.end()) {
352 const auto& conditionStats = mMetricsStats[key];
353 for (const auto& stats : conditionStats) {
354 auto output = configStats.add_metric_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800355 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800356 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800357 VLOG("metrics %lld max output tuple size %d",
358 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800359 }
360 }
Bookatz8f2f3d82017-12-07 13:53:21 -0800361 // Add anomaly detection alert stats
362 if (mAlertStats.find(key) != mAlertStats.end()) {
363 const auto& alertStats = mAlertStats[key];
364 for (const auto& stats : alertStats) {
365 auto output = configStats.add_alert_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800366 output->set_id(stats.first);
Bookatze1d143a2017-12-13 15:21:57 -0800367 output->set_alerted_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800368 VLOG("alert %lld declared %d times", (long long)stats.first, stats.second);
Bookatz8f2f3d82017-12-07 13:53:21 -0800369 }
370 }
Yao Chenb3561512017-11-21 18:07:17 -0800371}
372
Yao Chenf5acabe2018-01-17 14:10:34 -0800373void StatsdStats::dumpStats(FILE* out) const {
374 lock_guard<std::mutex> lock(mLock);
375 time_t t = mStartTimeSec;
376 struct tm* tm = localtime(&t);
377 char timeBuffer[80];
378 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
379 fprintf(out, "Stats collection start second: %s\n", timeBuffer);
380 fprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
381 for (const auto& configStats : mIceBox) {
382 fprintf(out,
383 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
384 "#matcher=%d, #alert=%d, valid=%d\n",
385 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
386 configStats.deletion_time_sec(), configStats.metric_count(),
387 configStats.condition_count(), configStats.matcher_count(),
388 configStats.alert_count(), configStats.is_valid());
389
390 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
391 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
392 }
393
394 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
395 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
396 }
397 }
398 fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
399 for (auto& pair : mConfigStats) {
400 auto& key = pair.first;
401 auto& configStats = pair.second;
402
403 fprintf(out,
404 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
405 "#matcher=%d, #alert=%d, valid=%d\n",
406 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
407 configStats.deletion_time_sec(), configStats.metric_count(),
408 configStats.condition_count(), configStats.matcher_count(),
409 configStats.alert_count(), configStats.is_valid());
410 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
411 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
412 }
413
414 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
415 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
416 }
417
418 for (const auto& dumpTime : configStats.dump_report_time_sec()) {
419 fprintf(out, "\tdump report time: %d\n", dumpTime);
420 }
421
422 // Add matcher stats
423 auto matcherIt = mMatcherStats.find(key);
424 if (matcherIt != mMatcherStats.end()) {
425 const auto& matcherStats = matcherIt->second;
426 for (const auto& stats : matcherStats) {
427 fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first,
428 stats.second);
429 }
430 }
431 // Add condition stats
432 auto conditionIt = mConditionStats.find(key);
433 if (conditionIt != mConditionStats.end()) {
434 const auto& conditionStats = conditionIt->second;
435 for (const auto& stats : conditionStats) {
436 fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
437 stats.second);
438 }
439 }
440 // Add metrics stats
441 auto metricIt = mMetricsStats.find(key);
442 if (metricIt != mMetricsStats.end()) {
443 const auto& conditionStats = metricIt->second;
444 for (const auto& stats : conditionStats) {
445 fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
446 stats.second);
447 }
448 }
449 // Add anomaly detection alert stats
450 auto alertIt = mAlertStats.find(key);
451 if (alertIt != mAlertStats.end()) {
452 const auto& alertStats = alertIt->second;
453 for (const auto& stats : alertStats) {
454 fprintf(out, "alert %lld declared %d times\n", (long long)stats.first,
455 stats.second);
456 }
457 }
458 }
459 fprintf(out, "********Pushed Atom stats***********\n");
460 const size_t atomCounts = mPushedAtomStats.size();
461 for (size_t i = 2; i < atomCounts; i++) {
462 if (mPushedAtomStats[i] > 0) {
463 fprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
464 }
465 }
466
467 fprintf(out, "********Pulled Atom stats***********\n");
468 for (const auto& pair : mPulledAtomStats) {
469 fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull,
470 (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
471 }
472
473 if (mAnomalyAlarmRegisteredStats > 0) {
474 fprintf(out, "********AnomalyAlarmStats stats***********\n");
475 fprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
476 }
477
478 fprintf(out,
479 "UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes "
480 "lost=%d\n",
481 mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(),
482 mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes());
Yao Chen884c8c12018-01-26 10:36:25 -0800483
484 for (const auto& error : mLoggerErrors) {
485 time_t error_time = error.first;
486 struct tm* error_tm = localtime(&error_time);
487 char buffer[80];
488 strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M%p\n", error_tm);
489 fprintf(out, "Logger error %d at %s\n", error.second, buffer);
490 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800491}
492
Yao Chen69f1baf2017-11-27 17:25:36 -0800493void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800494 lock_guard<std::mutex> lock(mLock);
495
Yao Chenb3561512017-11-21 18:07:17 -0800496 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800497 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800498 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr));
499
Yao Chenb3561512017-11-21 18:07:17 -0800500 for (const auto& configStats : mIceBox) {
501 const int numBytes = configStats.ByteSize();
502 vector<char> buffer(numBytes);
503 configStats.SerializeToArray(&buffer[0], numBytes);
504 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
505 buffer.size());
Yao Chenb3561512017-11-21 18:07:17 -0800506 }
507
508 for (auto& pair : mConfigStats) {
509 auto& configStats = pair.second;
Bookatz8f2f3d82017-12-07 13:53:21 -0800510 addSubStatsToConfigLocked(pair.first, configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800511
512 const int numBytes = configStats.ByteSize();
513 vector<char> buffer(numBytes);
514 configStats.SerializeToArray(&buffer[0], numBytes);
515 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
516 buffer.size());
517 // reset the sub stats, the source of truth is in the individual map
518 // they will be repopulated when dumpStats() is called again.
519 configStats.clear_matcher_stats();
520 configStats.clear_condition_stats();
521 configStats.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800522 configStats.clear_alert_stats();
Yao Chenb3561512017-11-21 18:07:17 -0800523 }
524
Yao Chenb3561512017-11-21 18:07:17 -0800525 const size_t atomCounts = mPushedAtomStats.size();
526 for (size_t i = 2; i < atomCounts; i++) {
527 if (mPushedAtomStats[i] > 0) {
528 long long token =
529 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
530 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
531 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
532 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800533 }
534 }
535
Chenjie Yub038b702017-12-18 15:15:34 -0800536 for (const auto& pair : mPulledAtomStats) {
537 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800538 }
539
Bookatz1d0136d2017-12-01 11:13:32 -0800540 if (mAnomalyAlarmRegisteredStats > 0) {
Bookatz1d0136d2017-12-01 11:13:32 -0800541 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
542 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
543 mAnomalyAlarmRegisteredStats);
544 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800545 }
546
David Chenc136f452017-11-27 11:52:26 -0800547 const int numBytes = mUidMapStats.ByteSize();
548 vector<char> buffer(numBytes);
549 mUidMapStats.SerializeToArray(&buffer[0], numBytes);
550 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
David Chenc136f452017-11-27 11:52:26 -0800551
Yao Chen884c8c12018-01-26 10:36:25 -0800552 for (const auto& error : mLoggerErrors) {
553 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
554 FIELD_COUNT_REPEATED);
555 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_TIME, error.first);
556 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_ERROR_CODE, error.second);
557 proto.end(token);
558 }
559
Yao Chenb3561512017-11-21 18:07:17 -0800560 output->clear();
561 size_t bufferSize = proto.size();
562 output->resize(bufferSize);
563
564 size_t pos = 0;
565 auto it = proto.data();
566 while (it.readBuffer() != NULL) {
567 size_t toRead = it.currentToRead();
568 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
569 pos += toRead;
570 it.rp()->move(toRead);
571 }
572
573 if (reset) {
574 resetInternalLocked();
575 }
576
577 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800578}
579
580} // namespace statsd
581} // namespace os
582} // namespace android