blob: 6e4512835b7ae16de91af1f72f1a08f4aef8a2c5 [file] [log] [blame]
Yao Chenb3561512017-11-21 18:07:17 -08001/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Tej Singh484524a2018-02-01 15:10:05 -080016#define DEBUG false // STOPSHIP if true
Yao Chenb3561512017-11-21 18:07:17 -080017#include "Log.h"
18
19#include "StatsdStats.h"
20
21#include <android/util/ProtoOutputStream.h>
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "../stats_log_util.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "statslog.h"
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 Chen4c959cb2018-02-13 13:27:48 -080048// const int FIELD_ID_PULLED_ATOM_STATS = 10; // The proto is written in stats_log_util.cpp
Yao Chen884c8c12018-01-26 10:36:25 -080049const int FIELD_ID_LOGGER_ERROR_STATS = 11;
Yao Chenb3561512017-11-21 18:07:17 -080050
Yao Chenb3561512017-11-21 18:07:17 -080051const int FIELD_ID_ATOM_STATS_TAG = 1;
52const int FIELD_ID_ATOM_STATS_COUNT = 2;
53
Bookatz1d0136d2017-12-01 11:13:32 -080054const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
55
Yao Chen884c8c12018-01-26 10:36:25 -080056const int FIELD_ID_LOGGER_STATS_TIME = 1;
57const int FIELD_ID_LOGGER_STATS_ERROR_CODE = 2;
58
Chenjie Yub038b702017-12-18 15:15:34 -080059std::map<int, long> StatsdStats::kPullerCooldownMap = {
60 {android::util::KERNEL_WAKELOCK, 1},
61 {android::util::WIFI_BYTES_TRANSFER, 1},
62 {android::util::MOBILE_BYTES_TRANSFER, 1},
63 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, 1},
64 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, 1},
Chenjie Yub038b702017-12-18 15:15:34 -080065 {android::util::SUBSYSTEM_SLEEP_STATE, 1},
66 {android::util::CPU_TIME_PER_FREQ, 1},
67 {android::util::CPU_TIME_PER_UID, 1},
68 {android::util::CPU_TIME_PER_UID_FREQ, 1},
69};
70
Yao Chenb3561512017-11-21 18:07:17 -080071// TODO: add stats for pulled atoms.
72StatsdStats::StatsdStats() {
73 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -080074 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -080075}
76
77StatsdStats& StatsdStats::getInstance() {
78 static StatsdStats statsInstance;
79 return statsInstance;
80}
81
Yao Chenf6723df2018-01-08 15:11:58 -080082void StatsdStats::addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats) {
83 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
84 if (mIceBox.size() == kMaxIceBoxSize) {
85 mIceBox.pop_front();
86 }
87 mIceBox.push_back(stats);
88}
89
Yao Chenb3561512017-11-21 18:07:17 -080090void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
91 int matchersCount, int alertsCount, bool isValid) {
92 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -080093 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -080094
95 // If there is an existing config for the same key, icebox the old config.
96 noteConfigRemovedInternalLocked(key);
97
98 StatsdStatsReport_ConfigStats configStats;
99 configStats.set_uid(key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800100 configStats.set_id(key.GetId());
Yao Chenb3561512017-11-21 18:07:17 -0800101 configStats.set_creation_time_sec(nowTimeSec);
102 configStats.set_metric_count(metricsCount);
103 configStats.set_condition_count(conditionsCount);
104 configStats.set_matcher_count(matchersCount);
105 configStats.set_alert_count(alertsCount);
106 configStats.set_is_valid(isValid);
107
108 if (isValid) {
109 mConfigStats[key] = configStats;
110 } else {
111 configStats.set_deletion_time_sec(nowTimeSec);
Yao Chenf6723df2018-01-08 15:11:58 -0800112 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800113 }
114}
115
116void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
117 auto it = mConfigStats.find(key);
118 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800119 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800120 it->second.set_deletion_time_sec(nowTimeSec);
Bookatz8f2f3d82017-12-07 13:53:21 -0800121 // Add condition stats, metrics stats, matcher stats, alert stats
122 addSubStatsToConfigLocked(key, it->second);
Yao Chenb3561512017-11-21 18:07:17 -0800123 // Remove them after they are added to the config stats.
124 mMatcherStats.erase(key);
125 mMetricsStats.erase(key);
Bookatz8f2f3d82017-12-07 13:53:21 -0800126 mAlertStats.erase(key);
Yao Chenb3561512017-11-21 18:07:17 -0800127 mConditionStats.erase(key);
Yao Chenf6723df2018-01-08 15:11:58 -0800128 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800129 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800130 }
131}
132
133void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
134 lock_guard<std::mutex> lock(mLock);
135 noteConfigRemovedInternalLocked(key);
136}
137
138void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800139 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800140}
141
142void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800143 lock_guard<std::mutex> lock(mLock);
144 auto it = mConfigStats.find(key);
145 if (it == mConfigStats.end()) {
146 ALOGE("Config key %s not found!", key.ToString().c_str());
147 return;
148 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800149 if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) {
150 auto timestampList = it->second.mutable_broadcast_sent_time_sec();
151 // This is O(N) operation. It shouldn't happen often, and N is only 20.
152 timestampList->erase(timestampList->begin());
153 }
154 it->second.add_broadcast_sent_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800155}
156
Yao Chen69f1baf2017-11-27 17:25:36 -0800157void StatsdStats::noteDataDropped(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800158 noteDataDropped(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800159}
160
161void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800162 lock_guard<std::mutex> lock(mLock);
163 auto it = mConfigStats.find(key);
164 if (it == mConfigStats.end()) {
165 ALOGE("Config key %s not found!", key.ToString().c_str());
166 return;
167 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800168 if (it->second.data_drop_time_sec_size() >= kMaxTimestampCount) {
169 auto timestampList = it->second.mutable_data_drop_time_sec();
170 // This is O(N) operation. It shouldn't happen often, and N is only 20.
171 timestampList->erase(timestampList->begin());
172 }
173 it->second.add_data_drop_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800174}
175
Yao Chen69f1baf2017-11-27 17:25:36 -0800176void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800177 noteMetricsReportSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800178}
179
180void StatsdStats::noteMetricsReportSent(const ConfigKey& key, int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800181 lock_guard<std::mutex> lock(mLock);
182 auto it = mConfigStats.find(key);
183 if (it == mConfigStats.end()) {
184 ALOGE("Config key %s not found!", key.ToString().c_str());
185 return;
186 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800187 if (it->second.dump_report_time_sec_size() >= kMaxTimestampCount) {
188 auto timestampList = it->second.mutable_dump_report_time_sec();
189 // This is O(N) operation. It shouldn't happen often, and N is only 20.
190 timestampList->erase(timestampList->begin());
191 }
192 it->second.add_dump_report_time_sec(timeSec);
Yao Chen69f1baf2017-11-27 17:25:36 -0800193}
194
David Chenc136f452017-11-27 11:52:26 -0800195void StatsdStats::noteUidMapDropped(int snapshots, int deltas) {
196 lock_guard<std::mutex> lock(mLock);
197 mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots);
198 mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas);
199}
200
201void StatsdStats::setUidMapSnapshots(int snapshots) {
202 lock_guard<std::mutex> lock(mLock);
203 mUidMapStats.set_snapshots(snapshots);
204}
205
206void StatsdStats::setUidMapChanges(int changes) {
207 lock_guard<std::mutex> lock(mLock);
208 mUidMapStats.set_changes(changes);
209}
210
211void StatsdStats::setCurrentUidMapMemory(int bytes) {
212 lock_guard<std::mutex> lock(mLock);
213 mUidMapStats.set_bytes_used(bytes);
214}
215
Yangster-mac94e197c2018-01-02 16:03:03 -0800216void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800217 lock_guard<std::mutex> lock(mLock);
218 // if name doesn't exist before, it will create the key with count 0.
219 auto& conditionSizeMap = mConditionStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800220 if (size > conditionSizeMap[id]) {
221 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800222 }
223}
224
Yangster-mac94e197c2018-01-02 16:03:03 -0800225void StatsdStats::noteMetricDimensionSize(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& metricsDimensionMap = mMetricsStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800229 if (size > metricsDimensionMap[id]) {
230 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800231 }
232}
233
Yangster-mac94e197c2018-01-02 16:03:03 -0800234void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800235 lock_guard<std::mutex> lock(mLock);
236 auto& matcherStats = mMatcherStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800237 matcherStats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800238}
239
Yangster-mac94e197c2018-01-02 16:03:03 -0800240void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800241 lock_guard<std::mutex> lock(mLock);
242 auto& alertStats = mAlertStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800243 alertStats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800244}
245
Bookatz1d0136d2017-12-01 11:13:32 -0800246void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
247 lock_guard<std::mutex> lock(mLock);
248 mAnomalyAlarmRegisteredStats++;
249}
250
Chenjie Yub038b702017-12-18 15:15:34 -0800251void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
252 lock_guard<std::mutex> lock(mLock);
253 mPulledAtomStats[pullAtomId].minPullIntervalSec = intervalSec;
254}
255
256void StatsdStats::notePull(int pullAtomId) {
257 lock_guard<std::mutex> lock(mLock);
258 mPulledAtomStats[pullAtomId].totalPull++;
259}
260
261void StatsdStats::notePullFromCache(int pullAtomId) {
262 lock_guard<std::mutex> lock(mLock);
263 mPulledAtomStats[pullAtomId].totalPullFromCache++;
264}
265
Yao Chenb3561512017-11-21 18:07:17 -0800266void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
267 lock_guard<std::mutex> lock(mLock);
268
Yao Chenb3561512017-11-21 18:07:17 -0800269 if (atomId > android::util::kMaxPushedAtomId) {
270 ALOGW("not interested in atom %d", atomId);
271 return;
272 }
273
274 mPushedAtomStats[atomId]++;
275}
276
Yao Chen884c8c12018-01-26 10:36:25 -0800277void StatsdStats::noteLoggerError(int error) {
278 lock_guard<std::mutex> lock(mLock);
279 // grows strictly one at a time. so it won't > kMaxLoggerErrors
280 if (mLoggerErrors.size() == kMaxLoggerErrors) {
281 mLoggerErrors.pop_front();
282 }
Yangster-mac330af582018-02-08 15:24:38 -0800283 mLoggerErrors.push_back(std::make_pair(getWallClockSec(), error));
Yao Chen884c8c12018-01-26 10:36:25 -0800284}
285
Yao Chenb3561512017-11-21 18:07:17 -0800286void StatsdStats::reset() {
287 lock_guard<std::mutex> lock(mLock);
288 resetInternalLocked();
289}
290
291void StatsdStats::resetInternalLocked() {
292 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800293 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800294 mIceBox.clear();
295 mConditionStats.clear();
296 mMetricsStats.clear();
297 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz8f2f3d82017-12-07 13:53:21 -0800298 mAlertStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800299 mAnomalyAlarmRegisteredStats = 0;
Yao Chenb3561512017-11-21 18:07:17 -0800300 mMatcherStats.clear();
Yao Chen884c8c12018-01-26 10:36:25 -0800301 mLoggerErrors.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800302 for (auto& config : mConfigStats) {
303 config.second.clear_broadcast_sent_time_sec();
304 config.second.clear_data_drop_time_sec();
305 config.second.clear_dump_report_time_sec();
306 config.second.clear_matcher_stats();
307 config.second.clear_condition_stats();
308 config.second.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800309 config.second.clear_alert_stats();
Yao Chen0fac5b12017-11-28 16:07:02 -0800310 }
Yao Chenb3561512017-11-21 18:07:17 -0800311}
312
Bookatz8f2f3d82017-12-07 13:53:21 -0800313void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key,
Yao Chenb3561512017-11-21 18:07:17 -0800314 StatsdStatsReport_ConfigStats& configStats) {
315 // Add matcher stats
316 if (mMatcherStats.find(key) != mMatcherStats.end()) {
317 const auto& matcherStats = mMatcherStats[key];
318 for (const auto& stats : matcherStats) {
319 auto output = configStats.add_matcher_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800320 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800321 output->set_matched_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800322 VLOG("matcher %lld matched %d times",
323 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800324 }
325 }
326 // Add condition stats
327 if (mConditionStats.find(key) != mConditionStats.end()) {
328 const auto& conditionStats = mConditionStats[key];
329 for (const auto& stats : conditionStats) {
330 auto output = configStats.add_condition_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800331 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800332 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800333 VLOG("condition %lld max output tuple size %d",
334 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800335 }
336 }
337 // Add metrics stats
338 if (mMetricsStats.find(key) != mMetricsStats.end()) {
339 const auto& conditionStats = mMetricsStats[key];
340 for (const auto& stats : conditionStats) {
341 auto output = configStats.add_metric_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800342 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800343 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800344 VLOG("metrics %lld max output tuple size %d",
345 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800346 }
347 }
Bookatz8f2f3d82017-12-07 13:53:21 -0800348 // Add anomaly detection alert stats
349 if (mAlertStats.find(key) != mAlertStats.end()) {
350 const auto& alertStats = mAlertStats[key];
351 for (const auto& stats : alertStats) {
352 auto output = configStats.add_alert_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800353 output->set_id(stats.first);
Bookatze1d143a2017-12-13 15:21:57 -0800354 output->set_alerted_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800355 VLOG("alert %lld declared %d times", (long long)stats.first, stats.second);
Bookatz8f2f3d82017-12-07 13:53:21 -0800356 }
357 }
Yao Chenb3561512017-11-21 18:07:17 -0800358}
359
Yao Chenf5acabe2018-01-17 14:10:34 -0800360void StatsdStats::dumpStats(FILE* out) const {
361 lock_guard<std::mutex> lock(mLock);
362 time_t t = mStartTimeSec;
363 struct tm* tm = localtime(&t);
364 char timeBuffer[80];
365 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
366 fprintf(out, "Stats collection start second: %s\n", timeBuffer);
367 fprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
368 for (const auto& configStats : mIceBox) {
369 fprintf(out,
yro255f72e2018-02-26 15:15:17 -0800370 "Config {%d_%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
Yao Chenf5acabe2018-01-17 14:10:34 -0800371 "#matcher=%d, #alert=%d, valid=%d\n",
372 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
373 configStats.deletion_time_sec(), configStats.metric_count(),
374 configStats.condition_count(), configStats.matcher_count(),
375 configStats.alert_count(), configStats.is_valid());
376
377 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
378 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
379 }
380
381 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
382 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
383 }
384 }
385 fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
386 for (auto& pair : mConfigStats) {
387 auto& key = pair.first;
388 auto& configStats = pair.second;
389
390 fprintf(out,
391 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
392 "#matcher=%d, #alert=%d, valid=%d\n",
393 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
394 configStats.deletion_time_sec(), configStats.metric_count(),
395 configStats.condition_count(), configStats.matcher_count(),
396 configStats.alert_count(), configStats.is_valid());
397 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
398 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
399 }
400
401 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
402 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
403 }
404
405 for (const auto& dumpTime : configStats.dump_report_time_sec()) {
406 fprintf(out, "\tdump report time: %d\n", dumpTime);
407 }
408
409 // Add matcher stats
410 auto matcherIt = mMatcherStats.find(key);
411 if (matcherIt != mMatcherStats.end()) {
412 const auto& matcherStats = matcherIt->second;
413 for (const auto& stats : matcherStats) {
414 fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first,
415 stats.second);
416 }
417 }
418 // Add condition stats
419 auto conditionIt = mConditionStats.find(key);
420 if (conditionIt != mConditionStats.end()) {
421 const auto& conditionStats = conditionIt->second;
422 for (const auto& stats : conditionStats) {
423 fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
424 stats.second);
425 }
426 }
427 // Add metrics stats
428 auto metricIt = mMetricsStats.find(key);
429 if (metricIt != mMetricsStats.end()) {
430 const auto& conditionStats = metricIt->second;
431 for (const auto& stats : conditionStats) {
432 fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
433 stats.second);
434 }
435 }
436 // Add anomaly detection alert stats
437 auto alertIt = mAlertStats.find(key);
438 if (alertIt != mAlertStats.end()) {
439 const auto& alertStats = alertIt->second;
440 for (const auto& stats : alertStats) {
441 fprintf(out, "alert %lld declared %d times\n", (long long)stats.first,
442 stats.second);
443 }
444 }
445 }
446 fprintf(out, "********Pushed Atom stats***********\n");
447 const size_t atomCounts = mPushedAtomStats.size();
448 for (size_t i = 2; i < atomCounts; i++) {
449 if (mPushedAtomStats[i] > 0) {
450 fprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
451 }
452 }
453
454 fprintf(out, "********Pulled Atom stats***********\n");
455 for (const auto& pair : mPulledAtomStats) {
456 fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull,
457 (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
458 }
459
460 if (mAnomalyAlarmRegisteredStats > 0) {
461 fprintf(out, "********AnomalyAlarmStats stats***********\n");
462 fprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
463 }
464
465 fprintf(out,
466 "UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes "
467 "lost=%d\n",
468 mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(),
469 mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes());
Yao Chen884c8c12018-01-26 10:36:25 -0800470
471 for (const auto& error : mLoggerErrors) {
472 time_t error_time = error.first;
473 struct tm* error_tm = localtime(&error_time);
474 char buffer[80];
475 strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M%p\n", error_tm);
476 fprintf(out, "Logger error %d at %s\n", error.second, buffer);
477 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800478}
479
Yao Chen69f1baf2017-11-27 17:25:36 -0800480void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800481 lock_guard<std::mutex> lock(mLock);
482
Yao Chenb3561512017-11-21 18:07:17 -0800483 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800484 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yangster-mac330af582018-02-08 15:24:38 -0800485 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800486
Yao Chenb3561512017-11-21 18:07:17 -0800487 for (const auto& configStats : mIceBox) {
488 const int numBytes = configStats.ByteSize();
489 vector<char> buffer(numBytes);
490 configStats.SerializeToArray(&buffer[0], numBytes);
491 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
492 buffer.size());
Yao Chenb3561512017-11-21 18:07:17 -0800493 }
494
495 for (auto& pair : mConfigStats) {
496 auto& configStats = pair.second;
Bookatz8f2f3d82017-12-07 13:53:21 -0800497 addSubStatsToConfigLocked(pair.first, configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800498
499 const int numBytes = configStats.ByteSize();
500 vector<char> buffer(numBytes);
501 configStats.SerializeToArray(&buffer[0], numBytes);
502 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
503 buffer.size());
504 // reset the sub stats, the source of truth is in the individual map
505 // they will be repopulated when dumpStats() is called again.
506 configStats.clear_matcher_stats();
507 configStats.clear_condition_stats();
508 configStats.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800509 configStats.clear_alert_stats();
Yao Chenb3561512017-11-21 18:07:17 -0800510 }
511
Yao Chenb3561512017-11-21 18:07:17 -0800512 const size_t atomCounts = mPushedAtomStats.size();
513 for (size_t i = 2; i < atomCounts; i++) {
514 if (mPushedAtomStats[i] > 0) {
515 long long token =
516 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
517 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
518 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
519 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800520 }
521 }
522
Chenjie Yub038b702017-12-18 15:15:34 -0800523 for (const auto& pair : mPulledAtomStats) {
524 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800525 }
526
Bookatz1d0136d2017-12-01 11:13:32 -0800527 if (mAnomalyAlarmRegisteredStats > 0) {
Bookatz1d0136d2017-12-01 11:13:32 -0800528 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
529 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
530 mAnomalyAlarmRegisteredStats);
531 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800532 }
533
David Chenc136f452017-11-27 11:52:26 -0800534 const int numBytes = mUidMapStats.ByteSize();
535 vector<char> buffer(numBytes);
536 mUidMapStats.SerializeToArray(&buffer[0], numBytes);
537 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
David Chenc136f452017-11-27 11:52:26 -0800538
Yao Chen884c8c12018-01-26 10:36:25 -0800539 for (const auto& error : mLoggerErrors) {
540 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
541 FIELD_COUNT_REPEATED);
542 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_TIME, error.first);
543 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_ERROR_CODE, error.second);
544 proto.end(token);
545 }
546
Yao Chenb3561512017-11-21 18:07:17 -0800547 output->clear();
548 size_t bufferSize = proto.size();
549 output->resize(bufferSize);
550
551 size_t pos = 0;
552 auto it = proto.data();
553 while (it.readBuffer() != NULL) {
554 size_t toRead = it.currentToRead();
555 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
556 pos += toRead;
557 it.rp()->move(toRead);
558 }
559
560 if (reset) {
561 resetInternalLocked();
562 }
563
564 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800565}
566
567} // namespace statsd
568} // namespace os
yro255f72e2018-02-26 15:15:17 -0800569} // namespace android