blob: a38f98fa1e35764261955387a030c91b16ed7bad [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);
Yao Chen69f1baf2017-11-27 17:25:36 -080074 mStartTimeSec = time(nullptr);
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);
93 int32_t nowTimeSec = time(nullptr);
94
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()) {
119 int32_t nowTimeSec = time(nullptr);
120 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) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800139 noteBroadcastSent(key, time(nullptr));
140}
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) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800158 noteDataDropped(key, time(nullptr));
159}
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) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800177 noteMetricsReportSent(key, time(nullptr));
178}
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 Chen69f1baf2017-11-27 17:25:36 -0800269 if (timeSec < mStartTimeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800270 return;
271 }
272
273 if (atomId > android::util::kMaxPushedAtomId) {
274 ALOGW("not interested in atom %d", atomId);
275 return;
276 }
277
278 mPushedAtomStats[atomId]++;
279}
280
Yao Chen884c8c12018-01-26 10:36:25 -0800281void StatsdStats::noteLoggerError(int error) {
282 lock_guard<std::mutex> lock(mLock);
283 // grows strictly one at a time. so it won't > kMaxLoggerErrors
284 if (mLoggerErrors.size() == kMaxLoggerErrors) {
285 mLoggerErrors.pop_front();
286 }
287 mLoggerErrors.push_back(std::make_pair(time(nullptr), error));
288}
289
Yao Chenb3561512017-11-21 18:07:17 -0800290void StatsdStats::reset() {
291 lock_guard<std::mutex> lock(mLock);
292 resetInternalLocked();
293}
294
295void StatsdStats::resetInternalLocked() {
296 // Reset the historical data, but keep the active ConfigStats
Yao Chen69f1baf2017-11-27 17:25:36 -0800297 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -0800298 mIceBox.clear();
299 mConditionStats.clear();
300 mMetricsStats.clear();
301 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz8f2f3d82017-12-07 13:53:21 -0800302 mAlertStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800303 mAnomalyAlarmRegisteredStats = 0;
Yao Chenb3561512017-11-21 18:07:17 -0800304 mMatcherStats.clear();
Yao Chen884c8c12018-01-26 10:36:25 -0800305 mLoggerErrors.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800306 for (auto& config : mConfigStats) {
307 config.second.clear_broadcast_sent_time_sec();
308 config.second.clear_data_drop_time_sec();
309 config.second.clear_dump_report_time_sec();
310 config.second.clear_matcher_stats();
311 config.second.clear_condition_stats();
312 config.second.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800313 config.second.clear_alert_stats();
Yao Chen0fac5b12017-11-28 16:07:02 -0800314 }
Yao Chenb3561512017-11-21 18:07:17 -0800315}
316
Bookatz8f2f3d82017-12-07 13:53:21 -0800317void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key,
Yao Chenb3561512017-11-21 18:07:17 -0800318 StatsdStatsReport_ConfigStats& configStats) {
319 // Add matcher stats
320 if (mMatcherStats.find(key) != mMatcherStats.end()) {
321 const auto& matcherStats = mMatcherStats[key];
322 for (const auto& stats : matcherStats) {
323 auto output = configStats.add_matcher_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800324 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800325 output->set_matched_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800326 VLOG("matcher %lld matched %d times",
327 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800328 }
329 }
330 // Add condition stats
331 if (mConditionStats.find(key) != mConditionStats.end()) {
332 const auto& conditionStats = mConditionStats[key];
333 for (const auto& stats : conditionStats) {
334 auto output = configStats.add_condition_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800335 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800336 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800337 VLOG("condition %lld max output tuple size %d",
338 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800339 }
340 }
341 // Add metrics stats
342 if (mMetricsStats.find(key) != mMetricsStats.end()) {
343 const auto& conditionStats = mMetricsStats[key];
344 for (const auto& stats : conditionStats) {
345 auto output = configStats.add_metric_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800346 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800347 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800348 VLOG("metrics %lld max output tuple size %d",
349 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800350 }
351 }
Bookatz8f2f3d82017-12-07 13:53:21 -0800352 // Add anomaly detection alert stats
353 if (mAlertStats.find(key) != mAlertStats.end()) {
354 const auto& alertStats = mAlertStats[key];
355 for (const auto& stats : alertStats) {
356 auto output = configStats.add_alert_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800357 output->set_id(stats.first);
Bookatze1d143a2017-12-13 15:21:57 -0800358 output->set_alerted_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800359 VLOG("alert %lld declared %d times", (long long)stats.first, stats.second);
Bookatz8f2f3d82017-12-07 13:53:21 -0800360 }
361 }
Yao Chenb3561512017-11-21 18:07:17 -0800362}
363
Yao Chenf5acabe2018-01-17 14:10:34 -0800364void StatsdStats::dumpStats(FILE* out) const {
365 lock_guard<std::mutex> lock(mLock);
366 time_t t = mStartTimeSec;
367 struct tm* tm = localtime(&t);
368 char timeBuffer[80];
369 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
370 fprintf(out, "Stats collection start second: %s\n", timeBuffer);
371 fprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
372 for (const auto& configStats : mIceBox) {
373 fprintf(out,
374 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
375 "#matcher=%d, #alert=%d, valid=%d\n",
376 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
377 configStats.deletion_time_sec(), configStats.metric_count(),
378 configStats.condition_count(), configStats.matcher_count(),
379 configStats.alert_count(), configStats.is_valid());
380
381 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
382 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
383 }
384
385 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
386 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
387 }
388 }
389 fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
390 for (auto& pair : mConfigStats) {
391 auto& key = pair.first;
392 auto& configStats = pair.second;
393
394 fprintf(out,
395 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
396 "#matcher=%d, #alert=%d, valid=%d\n",
397 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
398 configStats.deletion_time_sec(), configStats.metric_count(),
399 configStats.condition_count(), configStats.matcher_count(),
400 configStats.alert_count(), configStats.is_valid());
401 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
402 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
403 }
404
405 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
406 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
407 }
408
409 for (const auto& dumpTime : configStats.dump_report_time_sec()) {
410 fprintf(out, "\tdump report time: %d\n", dumpTime);
411 }
412
413 // Add matcher stats
414 auto matcherIt = mMatcherStats.find(key);
415 if (matcherIt != mMatcherStats.end()) {
416 const auto& matcherStats = matcherIt->second;
417 for (const auto& stats : matcherStats) {
418 fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first,
419 stats.second);
420 }
421 }
422 // Add condition stats
423 auto conditionIt = mConditionStats.find(key);
424 if (conditionIt != mConditionStats.end()) {
425 const auto& conditionStats = conditionIt->second;
426 for (const auto& stats : conditionStats) {
427 fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
428 stats.second);
429 }
430 }
431 // Add metrics stats
432 auto metricIt = mMetricsStats.find(key);
433 if (metricIt != mMetricsStats.end()) {
434 const auto& conditionStats = metricIt->second;
435 for (const auto& stats : conditionStats) {
436 fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
437 stats.second);
438 }
439 }
440 // Add anomaly detection alert stats
441 auto alertIt = mAlertStats.find(key);
442 if (alertIt != mAlertStats.end()) {
443 const auto& alertStats = alertIt->second;
444 for (const auto& stats : alertStats) {
445 fprintf(out, "alert %lld declared %d times\n", (long long)stats.first,
446 stats.second);
447 }
448 }
449 }
450 fprintf(out, "********Pushed Atom stats***********\n");
451 const size_t atomCounts = mPushedAtomStats.size();
452 for (size_t i = 2; i < atomCounts; i++) {
453 if (mPushedAtomStats[i] > 0) {
454 fprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
455 }
456 }
457
458 fprintf(out, "********Pulled Atom stats***********\n");
459 for (const auto& pair : mPulledAtomStats) {
460 fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull,
461 (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
462 }
463
464 if (mAnomalyAlarmRegisteredStats > 0) {
465 fprintf(out, "********AnomalyAlarmStats stats***********\n");
466 fprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
467 }
468
469 fprintf(out,
470 "UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes "
471 "lost=%d\n",
472 mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(),
473 mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes());
Yao Chen884c8c12018-01-26 10:36:25 -0800474
475 for (const auto& error : mLoggerErrors) {
476 time_t error_time = error.first;
477 struct tm* error_tm = localtime(&error_time);
478 char buffer[80];
479 strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M%p\n", error_tm);
480 fprintf(out, "Logger error %d at %s\n", error.second, buffer);
481 }
Yao Chenf5acabe2018-01-17 14:10:34 -0800482}
483
Yao Chen69f1baf2017-11-27 17:25:36 -0800484void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800485 lock_guard<std::mutex> lock(mLock);
486
Yao Chenb3561512017-11-21 18:07:17 -0800487 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800488 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800489 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr));
490
Yao Chenb3561512017-11-21 18:07:17 -0800491 for (const auto& configStats : mIceBox) {
492 const int numBytes = configStats.ByteSize();
493 vector<char> buffer(numBytes);
494 configStats.SerializeToArray(&buffer[0], numBytes);
495 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
496 buffer.size());
Yao Chenb3561512017-11-21 18:07:17 -0800497 }
498
499 for (auto& pair : mConfigStats) {
500 auto& configStats = pair.second;
Bookatz8f2f3d82017-12-07 13:53:21 -0800501 addSubStatsToConfigLocked(pair.first, configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800502
503 const int numBytes = configStats.ByteSize();
504 vector<char> buffer(numBytes);
505 configStats.SerializeToArray(&buffer[0], numBytes);
506 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
507 buffer.size());
508 // reset the sub stats, the source of truth is in the individual map
509 // they will be repopulated when dumpStats() is called again.
510 configStats.clear_matcher_stats();
511 configStats.clear_condition_stats();
512 configStats.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800513 configStats.clear_alert_stats();
Yao Chenb3561512017-11-21 18:07:17 -0800514 }
515
Yao Chenb3561512017-11-21 18:07:17 -0800516 const size_t atomCounts = mPushedAtomStats.size();
517 for (size_t i = 2; i < atomCounts; i++) {
518 if (mPushedAtomStats[i] > 0) {
519 long long token =
520 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
521 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
522 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
523 proto.end(token);
Yao Chenb3561512017-11-21 18:07:17 -0800524 }
525 }
526
Chenjie Yub038b702017-12-18 15:15:34 -0800527 for (const auto& pair : mPulledAtomStats) {
528 android::os::statsd::writePullerStatsToStream(pair, &proto);
Chenjie Yub038b702017-12-18 15:15:34 -0800529 }
530
Bookatz1d0136d2017-12-01 11:13:32 -0800531 if (mAnomalyAlarmRegisteredStats > 0) {
Bookatz1d0136d2017-12-01 11:13:32 -0800532 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
533 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
534 mAnomalyAlarmRegisteredStats);
535 proto.end(token);
Bookatz1d0136d2017-12-01 11:13:32 -0800536 }
537
David Chenc136f452017-11-27 11:52:26 -0800538 const int numBytes = mUidMapStats.ByteSize();
539 vector<char> buffer(numBytes);
540 mUidMapStats.SerializeToArray(&buffer[0], numBytes);
541 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
David Chenc136f452017-11-27 11:52:26 -0800542
Yao Chen884c8c12018-01-26 10:36:25 -0800543 for (const auto& error : mLoggerErrors) {
544 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
545 FIELD_COUNT_REPEATED);
546 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_TIME, error.first);
547 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_ERROR_CODE, error.second);
548 proto.end(token);
549 }
550
Yao Chenb3561512017-11-21 18:07:17 -0800551 output->clear();
552 size_t bufferSize = proto.size();
553 output->resize(bufferSize);
554
555 size_t pos = 0;
556 auto it = proto.data();
557 while (it.readBuffer() != NULL) {
558 size_t toRead = it.currentToRead();
559 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
560 pos += toRead;
561 it.rp()->move(toRead);
562 }
563
564 if (reset) {
565 resetInternalLocked();
566 }
567
568 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800569}
570
571} // namespace statsd
572} // namespace os
573} // namespace android