blob: b0d23397d7067177f3bde0a7e08ae9b1fd394d0e [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;
Yangster-mac932ecec2018-02-01 10:23:52 -080050const int FIELD_ID_SUBSCRIBER_ALARM_STATS = 12;
Yao Chenb3561512017-11-21 18:07:17 -080051
Yao Chenb3561512017-11-21 18:07:17 -080052const int FIELD_ID_ATOM_STATS_TAG = 1;
53const int FIELD_ID_ATOM_STATS_COUNT = 2;
54
Bookatz1d0136d2017-12-01 11:13:32 -080055const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
Yangster-mac932ecec2018-02-01 10:23:52 -080056const int FIELD_ID_SUBSCRIBER_ALARMS_REGISTERED = 1;
Bookatz1d0136d2017-12-01 11:13:32 -080057
Yao Chen884c8c12018-01-26 10:36:25 -080058const int FIELD_ID_LOGGER_STATS_TIME = 1;
59const int FIELD_ID_LOGGER_STATS_ERROR_CODE = 2;
60
Chenjie Yub038b702017-12-18 15:15:34 -080061std::map<int, long> StatsdStats::kPullerCooldownMap = {
62 {android::util::KERNEL_WAKELOCK, 1},
63 {android::util::WIFI_BYTES_TRANSFER, 1},
64 {android::util::MOBILE_BYTES_TRANSFER, 1},
65 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, 1},
66 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, 1},
Chenjie Yub038b702017-12-18 15:15:34 -080067 {android::util::SUBSYSTEM_SLEEP_STATE, 1},
68 {android::util::CPU_TIME_PER_FREQ, 1},
69 {android::util::CPU_TIME_PER_UID, 1},
70 {android::util::CPU_TIME_PER_UID_FREQ, 1},
71};
72
Yao Chenb3561512017-11-21 18:07:17 -080073// TODO: add stats for pulled atoms.
74StatsdStats::StatsdStats() {
75 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yangster-mac330af582018-02-08 15:24:38 -080076 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -080077}
78
79StatsdStats& StatsdStats::getInstance() {
80 static StatsdStats statsInstance;
81 return statsInstance;
82}
83
Yao Chenf6723df2018-01-08 15:11:58 -080084void StatsdStats::addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats) {
85 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
86 if (mIceBox.size() == kMaxIceBoxSize) {
87 mIceBox.pop_front();
88 }
89 mIceBox.push_back(stats);
90}
91
Yao Chenb3561512017-11-21 18:07:17 -080092void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
93 int matchersCount, int alertsCount, bool isValid) {
94 lock_guard<std::mutex> lock(mLock);
Yangster-mac330af582018-02-08 15:24:38 -080095 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -080096
97 // If there is an existing config for the same key, icebox the old config.
98 noteConfigRemovedInternalLocked(key);
99
100 StatsdStatsReport_ConfigStats configStats;
101 configStats.set_uid(key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800102 configStats.set_id(key.GetId());
Yao Chenb3561512017-11-21 18:07:17 -0800103 configStats.set_creation_time_sec(nowTimeSec);
104 configStats.set_metric_count(metricsCount);
105 configStats.set_condition_count(conditionsCount);
106 configStats.set_matcher_count(matchersCount);
107 configStats.set_alert_count(alertsCount);
108 configStats.set_is_valid(isValid);
109
110 if (isValid) {
111 mConfigStats[key] = configStats;
112 } else {
113 configStats.set_deletion_time_sec(nowTimeSec);
Yao Chenf6723df2018-01-08 15:11:58 -0800114 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800115 }
116}
117
118void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
119 auto it = mConfigStats.find(key);
120 if (it != mConfigStats.end()) {
Yangster-mac330af582018-02-08 15:24:38 -0800121 int32_t nowTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800122 it->second.set_deletion_time_sec(nowTimeSec);
Bookatz8f2f3d82017-12-07 13:53:21 -0800123 // Add condition stats, metrics stats, matcher stats, alert stats
124 addSubStatsToConfigLocked(key, it->second);
Yao Chenb3561512017-11-21 18:07:17 -0800125 // Remove them after they are added to the config stats.
126 mMatcherStats.erase(key);
127 mMetricsStats.erase(key);
Bookatz8f2f3d82017-12-07 13:53:21 -0800128 mAlertStats.erase(key);
Yao Chenb3561512017-11-21 18:07:17 -0800129 mConditionStats.erase(key);
Yao Chenf6723df2018-01-08 15:11:58 -0800130 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800131 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800132 }
133}
134
135void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
136 lock_guard<std::mutex> lock(mLock);
137 noteConfigRemovedInternalLocked(key);
138}
139
140void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800141 noteBroadcastSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800142}
143
144void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800145 lock_guard<std::mutex> lock(mLock);
146 auto it = mConfigStats.find(key);
147 if (it == mConfigStats.end()) {
148 ALOGE("Config key %s not found!", key.ToString().c_str());
149 return;
150 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800151 if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) {
152 auto timestampList = it->second.mutable_broadcast_sent_time_sec();
153 // This is O(N) operation. It shouldn't happen often, and N is only 20.
154 timestampList->erase(timestampList->begin());
155 }
156 it->second.add_broadcast_sent_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800157}
158
Yao Chen69f1baf2017-11-27 17:25:36 -0800159void StatsdStats::noteDataDropped(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800160 noteDataDropped(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800161}
162
163void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800164 lock_guard<std::mutex> lock(mLock);
165 auto it = mConfigStats.find(key);
166 if (it == mConfigStats.end()) {
167 ALOGE("Config key %s not found!", key.ToString().c_str());
168 return;
169 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800170 if (it->second.data_drop_time_sec_size() >= kMaxTimestampCount) {
171 auto timestampList = it->second.mutable_data_drop_time_sec();
172 // This is O(N) operation. It shouldn't happen often, and N is only 20.
173 timestampList->erase(timestampList->begin());
174 }
175 it->second.add_data_drop_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800176}
177
Yao Chen69f1baf2017-11-27 17:25:36 -0800178void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
Yangster-mac330af582018-02-08 15:24:38 -0800179 noteMetricsReportSent(key, getWallClockSec());
Yao Chen0fac5b12017-11-28 16:07:02 -0800180}
181
182void StatsdStats::noteMetricsReportSent(const ConfigKey& key, int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800183 lock_guard<std::mutex> lock(mLock);
184 auto it = mConfigStats.find(key);
185 if (it == mConfigStats.end()) {
186 ALOGE("Config key %s not found!", key.ToString().c_str());
187 return;
188 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800189 if (it->second.dump_report_time_sec_size() >= kMaxTimestampCount) {
190 auto timestampList = it->second.mutable_dump_report_time_sec();
191 // This is O(N) operation. It shouldn't happen often, and N is only 20.
192 timestampList->erase(timestampList->begin());
193 }
194 it->second.add_dump_report_time_sec(timeSec);
Yao Chen69f1baf2017-11-27 17:25:36 -0800195}
196
David Chenc136f452017-11-27 11:52:26 -0800197void StatsdStats::noteUidMapDropped(int snapshots, int deltas) {
198 lock_guard<std::mutex> lock(mLock);
199 mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots);
200 mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas);
201}
202
203void StatsdStats::setUidMapSnapshots(int snapshots) {
204 lock_guard<std::mutex> lock(mLock);
205 mUidMapStats.set_snapshots(snapshots);
206}
207
208void StatsdStats::setUidMapChanges(int changes) {
209 lock_guard<std::mutex> lock(mLock);
210 mUidMapStats.set_changes(changes);
211}
212
213void StatsdStats::setCurrentUidMapMemory(int bytes) {
214 lock_guard<std::mutex> lock(mLock);
215 mUidMapStats.set_bytes_used(bytes);
216}
217
Yangster-mac94e197c2018-01-02 16:03:03 -0800218void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800219 lock_guard<std::mutex> lock(mLock);
220 // if name doesn't exist before, it will create the key with count 0.
221 auto& conditionSizeMap = mConditionStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800222 if (size > conditionSizeMap[id]) {
223 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800224 }
225}
226
Yangster-mac94e197c2018-01-02 16:03:03 -0800227void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800228 lock_guard<std::mutex> lock(mLock);
229 // if name doesn't exist before, it will create the key with count 0.
230 auto& metricsDimensionMap = mMetricsStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800231 if (size > metricsDimensionMap[id]) {
232 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800233 }
234}
235
Yangster-mac94e197c2018-01-02 16:03:03 -0800236void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800237 lock_guard<std::mutex> lock(mLock);
238 auto& matcherStats = mMatcherStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800239 matcherStats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800240}
241
Yangster-mac94e197c2018-01-02 16:03:03 -0800242void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800243 lock_guard<std::mutex> lock(mLock);
244 auto& alertStats = mAlertStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800245 alertStats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800246}
247
Bookatz1d0136d2017-12-01 11:13:32 -0800248void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
249 lock_guard<std::mutex> lock(mLock);
250 mAnomalyAlarmRegisteredStats++;
251}
252
Yangster-mac932ecec2018-02-01 10:23:52 -0800253void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
254 lock_guard<std::mutex> lock(mLock);
255 mPeriodicAlarmRegisteredStats++;
256}
257
Chenjie Yub038b702017-12-18 15:15:34 -0800258void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
259 lock_guard<std::mutex> lock(mLock);
260 mPulledAtomStats[pullAtomId].minPullIntervalSec = intervalSec;
261}
262
263void StatsdStats::notePull(int pullAtomId) {
264 lock_guard<std::mutex> lock(mLock);
265 mPulledAtomStats[pullAtomId].totalPull++;
266}
267
268void StatsdStats::notePullFromCache(int pullAtomId) {
269 lock_guard<std::mutex> lock(mLock);
270 mPulledAtomStats[pullAtomId].totalPullFromCache++;
271}
272
Yao Chenb3561512017-11-21 18:07:17 -0800273void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
274 lock_guard<std::mutex> lock(mLock);
275
Yao Chenb3561512017-11-21 18:07:17 -0800276 if (atomId > android::util::kMaxPushedAtomId) {
277 ALOGW("not interested in atom %d", atomId);
278 return;
279 }
280
281 mPushedAtomStats[atomId]++;
282}
283
Yao Chen884c8c12018-01-26 10:36:25 -0800284void StatsdStats::noteLoggerError(int error) {
285 lock_guard<std::mutex> lock(mLock);
286 // grows strictly one at a time. so it won't > kMaxLoggerErrors
287 if (mLoggerErrors.size() == kMaxLoggerErrors) {
288 mLoggerErrors.pop_front();
289 }
Yangster-mac330af582018-02-08 15:24:38 -0800290 mLoggerErrors.push_back(std::make_pair(getWallClockSec(), error));
Yao Chen884c8c12018-01-26 10:36:25 -0800291}
292
Yao Chenb3561512017-11-21 18:07:17 -0800293void StatsdStats::reset() {
294 lock_guard<std::mutex> lock(mLock);
295 resetInternalLocked();
296}
297
298void StatsdStats::resetInternalLocked() {
299 // Reset the historical data, but keep the active ConfigStats
Yangster-mac330af582018-02-08 15:24:38 -0800300 mStartTimeSec = getWallClockSec();
Yao Chenb3561512017-11-21 18:07:17 -0800301 mIceBox.clear();
302 mConditionStats.clear();
303 mMetricsStats.clear();
304 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz8f2f3d82017-12-07 13:53:21 -0800305 mAlertStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800306 mAnomalyAlarmRegisteredStats = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800307 mPeriodicAlarmRegisteredStats = 0;
Yao Chenb3561512017-11-21 18:07:17 -0800308 mMatcherStats.clear();
Yao Chen884c8c12018-01-26 10:36:25 -0800309 mLoggerErrors.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800310 for (auto& config : mConfigStats) {
311 config.second.clear_broadcast_sent_time_sec();
312 config.second.clear_data_drop_time_sec();
313 config.second.clear_dump_report_time_sec();
314 config.second.clear_matcher_stats();
315 config.second.clear_condition_stats();
316 config.second.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800317 config.second.clear_alert_stats();
Yao Chen0fac5b12017-11-28 16:07:02 -0800318 }
Yao Chenb3561512017-11-21 18:07:17 -0800319}
320
Bookatz8f2f3d82017-12-07 13:53:21 -0800321void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key,
Yao Chenb3561512017-11-21 18:07:17 -0800322 StatsdStatsReport_ConfigStats& configStats) {
323 // Add matcher stats
324 if (mMatcherStats.find(key) != mMatcherStats.end()) {
325 const auto& matcherStats = mMatcherStats[key];
326 for (const auto& stats : matcherStats) {
327 auto output = configStats.add_matcher_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800328 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800329 output->set_matched_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800330 VLOG("matcher %lld matched %d times",
331 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800332 }
333 }
334 // Add condition stats
335 if (mConditionStats.find(key) != mConditionStats.end()) {
336 const auto& conditionStats = mConditionStats[key];
337 for (const auto& stats : conditionStats) {
338 auto output = configStats.add_condition_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800339 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800340 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800341 VLOG("condition %lld max output tuple size %d",
342 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800343 }
344 }
345 // Add metrics stats
346 if (mMetricsStats.find(key) != mMetricsStats.end()) {
347 const auto& conditionStats = mMetricsStats[key];
348 for (const auto& stats : conditionStats) {
349 auto output = configStats.add_metric_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800350 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800351 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800352 VLOG("metrics %lld max output tuple size %d",
353 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800354 }
355 }
Bookatz8f2f3d82017-12-07 13:53:21 -0800356 // Add anomaly detection alert stats
357 if (mAlertStats.find(key) != mAlertStats.end()) {
358 const auto& alertStats = mAlertStats[key];
359 for (const auto& stats : alertStats) {
360 auto output = configStats.add_alert_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800361 output->set_id(stats.first);
Bookatze1d143a2017-12-13 15:21:57 -0800362 output->set_alerted_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800363 VLOG("alert %lld declared %d times", (long long)stats.first, stats.second);
Bookatz8f2f3d82017-12-07 13:53:21 -0800364 }
365 }
Yao Chenb3561512017-11-21 18:07:17 -0800366}
367
Yao Chenf5acabe2018-01-17 14:10:34 -0800368void StatsdStats::dumpStats(FILE* out) const {
369 lock_guard<std::mutex> lock(mLock);
370 time_t t = mStartTimeSec;
371 struct tm* tm = localtime(&t);
372 char timeBuffer[80];
373 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
374 fprintf(out, "Stats collection start second: %s\n", timeBuffer);
375 fprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
376 for (const auto& configStats : mIceBox) {
377 fprintf(out,
378 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
379 "#matcher=%d, #alert=%d, valid=%d\n",
380 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
381 configStats.deletion_time_sec(), configStats.metric_count(),
382 configStats.condition_count(), configStats.matcher_count(),
383 configStats.alert_count(), configStats.is_valid());
384
385 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
386 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
387 }
388
389 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
390 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
391 }
392 }
393 fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
394 for (auto& pair : mConfigStats) {
395 auto& key = pair.first;
396 auto& configStats = pair.second;
397
398 fprintf(out,
399 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
400 "#matcher=%d, #alert=%d, valid=%d\n",
401 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
402 configStats.deletion_time_sec(), configStats.metric_count(),
403 configStats.condition_count(), configStats.matcher_count(),
404 configStats.alert_count(), configStats.is_valid());
405 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
406 fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
407 }
408
409 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
410 fprintf(out, "\tdata drop time: %d\n", dataDropTime);
411 }
412
413 for (const auto& dumpTime : configStats.dump_report_time_sec()) {
414 fprintf(out, "\tdump report time: %d\n", dumpTime);
415 }
416
417 // Add matcher stats
418 auto matcherIt = mMatcherStats.find(key);
419 if (matcherIt != mMatcherStats.end()) {
420 const auto& matcherStats = matcherIt->second;
421 for (const auto& stats : matcherStats) {
422 fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first,
423 stats.second);
424 }
425 }
426 // Add condition stats
427 auto conditionIt = mConditionStats.find(key);
428 if (conditionIt != mConditionStats.end()) {
429 const auto& conditionStats = conditionIt->second;
430 for (const auto& stats : conditionStats) {
431 fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
432 stats.second);
433 }
434 }
435 // Add metrics stats
436 auto metricIt = mMetricsStats.find(key);
437 if (metricIt != mMetricsStats.end()) {
438 const auto& conditionStats = metricIt->second;
439 for (const auto& stats : conditionStats) {
440 fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
441 stats.second);
442 }
443 }
444 // Add anomaly detection alert stats
445 auto alertIt = mAlertStats.find(key);
446 if (alertIt != mAlertStats.end()) {
447 const auto& alertStats = alertIt->second;
448 for (const auto& stats : alertStats) {
449 fprintf(out, "alert %lld declared %d times\n", (long long)stats.first,
450 stats.second);
451 }
452 }
453 }
454 fprintf(out, "********Pushed Atom stats***********\n");
455 const size_t atomCounts = mPushedAtomStats.size();
456 for (size_t i = 2; i < atomCounts; i++) {
457 if (mPushedAtomStats[i] > 0) {
458 fprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
459 }
460 }
461
462 fprintf(out, "********Pulled Atom stats***********\n");
463 for (const auto& pair : mPulledAtomStats) {
464 fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull,
465 (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
466 }
467
468 if (mAnomalyAlarmRegisteredStats > 0) {
469 fprintf(out, "********AnomalyAlarmStats stats***********\n");
470 fprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
471 }
472
Yangster-mac932ecec2018-02-01 10:23:52 -0800473 if (mPeriodicAlarmRegisteredStats > 0) {
474 fprintf(out, "********SubscriberAlarmStats stats***********\n");
475 fprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
476 }
477
Yao Chenf5acabe2018-01-17 14:10:34 -0800478 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);
Yangster-mac330af582018-02-08 15:24:38 -0800498 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
Yao Chenb3561512017-11-21 18:07:17 -0800499
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
Yangster-mac932ecec2018-02-01 10:23:52 -0800547 if (mPeriodicAlarmRegisteredStats > 0) {
548 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_SUBSCRIBER_ALARM_STATS);
549 proto.write(FIELD_TYPE_INT32 | FIELD_ID_SUBSCRIBER_ALARMS_REGISTERED,
550 mPeriodicAlarmRegisteredStats);
551 proto.end(token);
552 }
553
David Chenc136f452017-11-27 11:52:26 -0800554 const int numBytes = mUidMapStats.ByteSize();
555 vector<char> buffer(numBytes);
556 mUidMapStats.SerializeToArray(&buffer[0], numBytes);
557 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
David Chenc136f452017-11-27 11:52:26 -0800558
Yao Chen884c8c12018-01-26 10:36:25 -0800559 for (const auto& error : mLoggerErrors) {
560 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
561 FIELD_COUNT_REPEATED);
562 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_TIME, error.first);
563 proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_ERROR_CODE, error.second);
564 proto.end(token);
565 }
566
Yao Chenb3561512017-11-21 18:07:17 -0800567 output->clear();
568 size_t bufferSize = proto.size();
569 output->resize(bufferSize);
570
571 size_t pos = 0;
572 auto it = proto.data();
573 while (it.readBuffer() != NULL) {
574 size_t toRead = it.currentToRead();
575 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
576 pos += toRead;
577 it.rp()->move(toRead);
578 }
579
580 if (reset) {
581 resetInternalLocked();
582 }
583
584 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
Yao Chenb3561512017-11-21 18:07:17 -0800585}
586
587} // namespace statsd
588} // namespace os
589} // namespace android