blob: 36dd6167663bb2cc47ab3ee976b6e450599180b4 [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 Chenb3561512017-11-21 18:07:17 -080048
49const int FIELD_ID_MATCHER_STATS_NAME = 1;
50const int FIELD_ID_MATCHER_STATS_COUNT = 2;
51
52const int FIELD_ID_CONDITION_STATS_NAME = 1;
53const int FIELD_ID_CONDITION_STATS_COUNT = 2;
54
55const int FIELD_ID_METRIC_STATS_NAME = 1;
56const int FIELD_ID_METRIC_STATS_COUNT = 2;
57
58const int FIELD_ID_ATOM_STATS_TAG = 1;
59const int FIELD_ID_ATOM_STATS_COUNT = 2;
60
Bookatz1d0136d2017-12-01 11:13:32 -080061const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
62
Chenjie Yub038b702017-12-18 15:15:34 -080063std::map<int, long> StatsdStats::kPullerCooldownMap = {
64 {android::util::KERNEL_WAKELOCK, 1},
65 {android::util::WIFI_BYTES_TRANSFER, 1},
66 {android::util::MOBILE_BYTES_TRANSFER, 1},
67 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, 1},
68 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, 1},
69 {android::util::PLATFORM_SLEEP_STATE, 1},
70 {android::util::SLEEP_STATE_VOTER, 1},
71 {android::util::SUBSYSTEM_SLEEP_STATE, 1},
72 {android::util::CPU_TIME_PER_FREQ, 1},
73 {android::util::CPU_TIME_PER_UID, 1},
74 {android::util::CPU_TIME_PER_UID_FREQ, 1},
75};
76
Yao Chenb3561512017-11-21 18:07:17 -080077// TODO: add stats for pulled atoms.
78StatsdStats::StatsdStats() {
79 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yao Chen69f1baf2017-11-27 17:25:36 -080080 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -080081}
82
83StatsdStats& StatsdStats::getInstance() {
84 static StatsdStats statsInstance;
85 return statsInstance;
86}
87
Yao Chenf6723df2018-01-08 15:11:58 -080088void StatsdStats::addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats) {
89 // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
90 if (mIceBox.size() == kMaxIceBoxSize) {
91 mIceBox.pop_front();
92 }
93 mIceBox.push_back(stats);
94}
95
Yao Chenb3561512017-11-21 18:07:17 -080096void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
97 int matchersCount, int alertsCount, bool isValid) {
98 lock_guard<std::mutex> lock(mLock);
99 int32_t nowTimeSec = time(nullptr);
100
101 // If there is an existing config for the same key, icebox the old config.
102 noteConfigRemovedInternalLocked(key);
103
104 StatsdStatsReport_ConfigStats configStats;
105 configStats.set_uid(key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -0800106 configStats.set_id(key.GetId());
Yao Chenb3561512017-11-21 18:07:17 -0800107 configStats.set_creation_time_sec(nowTimeSec);
108 configStats.set_metric_count(metricsCount);
109 configStats.set_condition_count(conditionsCount);
110 configStats.set_matcher_count(matchersCount);
111 configStats.set_alert_count(alertsCount);
112 configStats.set_is_valid(isValid);
113
114 if (isValid) {
115 mConfigStats[key] = configStats;
116 } else {
117 configStats.set_deletion_time_sec(nowTimeSec);
Yao Chenf6723df2018-01-08 15:11:58 -0800118 addToIceBoxLocked(configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800119 }
120}
121
122void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
123 auto it = mConfigStats.find(key);
124 if (it != mConfigStats.end()) {
125 int32_t nowTimeSec = time(nullptr);
126 it->second.set_deletion_time_sec(nowTimeSec);
Bookatz8f2f3d82017-12-07 13:53:21 -0800127 // Add condition stats, metrics stats, matcher stats, alert stats
128 addSubStatsToConfigLocked(key, it->second);
Yao Chenb3561512017-11-21 18:07:17 -0800129 // Remove them after they are added to the config stats.
130 mMatcherStats.erase(key);
131 mMetricsStats.erase(key);
Bookatz8f2f3d82017-12-07 13:53:21 -0800132 mAlertStats.erase(key);
Yao Chenb3561512017-11-21 18:07:17 -0800133 mConditionStats.erase(key);
Yao Chenf6723df2018-01-08 15:11:58 -0800134 addToIceBoxLocked(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800135 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800136 }
137}
138
139void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
140 lock_guard<std::mutex> lock(mLock);
141 noteConfigRemovedInternalLocked(key);
142}
143
144void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800145 noteBroadcastSent(key, time(nullptr));
146}
147
148void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800149 lock_guard<std::mutex> lock(mLock);
150 auto it = mConfigStats.find(key);
151 if (it == mConfigStats.end()) {
152 ALOGE("Config key %s not found!", key.ToString().c_str());
153 return;
154 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800155 if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) {
156 auto timestampList = it->second.mutable_broadcast_sent_time_sec();
157 // This is O(N) operation. It shouldn't happen often, and N is only 20.
158 timestampList->erase(timestampList->begin());
159 }
160 it->second.add_broadcast_sent_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800161}
162
Yao Chen69f1baf2017-11-27 17:25:36 -0800163void StatsdStats::noteDataDropped(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800164 noteDataDropped(key, time(nullptr));
165}
166
167void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800168 lock_guard<std::mutex> lock(mLock);
169 auto it = mConfigStats.find(key);
170 if (it == mConfigStats.end()) {
171 ALOGE("Config key %s not found!", key.ToString().c_str());
172 return;
173 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800174 if (it->second.data_drop_time_sec_size() >= kMaxTimestampCount) {
175 auto timestampList = it->second.mutable_data_drop_time_sec();
176 // This is O(N) operation. It shouldn't happen often, and N is only 20.
177 timestampList->erase(timestampList->begin());
178 }
179 it->second.add_data_drop_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800180}
181
Yao Chen69f1baf2017-11-27 17:25:36 -0800182void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800183 noteMetricsReportSent(key, time(nullptr));
184}
185
186void StatsdStats::noteMetricsReportSent(const ConfigKey& key, int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800187 lock_guard<std::mutex> lock(mLock);
188 auto it = mConfigStats.find(key);
189 if (it == mConfigStats.end()) {
190 ALOGE("Config key %s not found!", key.ToString().c_str());
191 return;
192 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800193 if (it->second.dump_report_time_sec_size() >= kMaxTimestampCount) {
194 auto timestampList = it->second.mutable_dump_report_time_sec();
195 // This is O(N) operation. It shouldn't happen often, and N is only 20.
196 timestampList->erase(timestampList->begin());
197 }
198 it->second.add_dump_report_time_sec(timeSec);
Yao Chen69f1baf2017-11-27 17:25:36 -0800199}
200
David Chenc136f452017-11-27 11:52:26 -0800201void StatsdStats::noteUidMapDropped(int snapshots, int deltas) {
202 lock_guard<std::mutex> lock(mLock);
203 mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots);
204 mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas);
205}
206
207void StatsdStats::setUidMapSnapshots(int snapshots) {
208 lock_guard<std::mutex> lock(mLock);
209 mUidMapStats.set_snapshots(snapshots);
210}
211
212void StatsdStats::setUidMapChanges(int changes) {
213 lock_guard<std::mutex> lock(mLock);
214 mUidMapStats.set_changes(changes);
215}
216
217void StatsdStats::setCurrentUidMapMemory(int bytes) {
218 lock_guard<std::mutex> lock(mLock);
219 mUidMapStats.set_bytes_used(bytes);
220}
221
Yangster-mac94e197c2018-01-02 16:03:03 -0800222void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800223 lock_guard<std::mutex> lock(mLock);
224 // if name doesn't exist before, it will create the key with count 0.
225 auto& conditionSizeMap = mConditionStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800226 if (size > conditionSizeMap[id]) {
227 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800228 }
229}
230
Yangster-mac94e197c2018-01-02 16:03:03 -0800231void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800232 lock_guard<std::mutex> lock(mLock);
233 // if name doesn't exist before, it will create the key with count 0.
234 auto& metricsDimensionMap = mMetricsStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800235 if (size > metricsDimensionMap[id]) {
236 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800237 }
238}
239
Yangster-mac94e197c2018-01-02 16:03:03 -0800240void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800241 lock_guard<std::mutex> lock(mLock);
242 auto& matcherStats = mMatcherStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800243 matcherStats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800244}
245
Yangster-mac94e197c2018-01-02 16:03:03 -0800246void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800247 lock_guard<std::mutex> lock(mLock);
248 auto& alertStats = mAlertStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800249 alertStats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800250}
251
Bookatz1d0136d2017-12-01 11:13:32 -0800252void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
253 lock_guard<std::mutex> lock(mLock);
254 mAnomalyAlarmRegisteredStats++;
255}
256
Chenjie Yub038b702017-12-18 15:15:34 -0800257void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
258 lock_guard<std::mutex> lock(mLock);
259 mPulledAtomStats[pullAtomId].minPullIntervalSec = intervalSec;
260}
261
262void StatsdStats::notePull(int pullAtomId) {
263 lock_guard<std::mutex> lock(mLock);
264 mPulledAtomStats[pullAtomId].totalPull++;
265}
266
267void StatsdStats::notePullFromCache(int pullAtomId) {
268 lock_guard<std::mutex> lock(mLock);
269 mPulledAtomStats[pullAtomId].totalPullFromCache++;
270}
271
Yao Chenb3561512017-11-21 18:07:17 -0800272void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
273 lock_guard<std::mutex> lock(mLock);
274
Yao Chen69f1baf2017-11-27 17:25:36 -0800275 if (timeSec < mStartTimeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800276 return;
277 }
278
279 if (atomId > android::util::kMaxPushedAtomId) {
280 ALOGW("not interested in atom %d", atomId);
281 return;
282 }
283
284 mPushedAtomStats[atomId]++;
285}
286
287void StatsdStats::reset() {
288 lock_guard<std::mutex> lock(mLock);
289 resetInternalLocked();
290}
291
292void StatsdStats::resetInternalLocked() {
293 // Reset the historical data, but keep the active ConfigStats
Yao Chen69f1baf2017-11-27 17:25:36 -0800294 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -0800295 mIceBox.clear();
296 mConditionStats.clear();
297 mMetricsStats.clear();
298 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz8f2f3d82017-12-07 13:53:21 -0800299 mAlertStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800300 mAnomalyAlarmRegisteredStats = 0;
Yao Chenb3561512017-11-21 18:07:17 -0800301 mMatcherStats.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 Chen69f1baf2017-11-27 17:25:36 -0800360void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800361 lock_guard<std::mutex> lock(mLock);
362
363 if (DEBUG) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800364 time_t t = mStartTimeSec;
Yao Chenb3561512017-11-21 18:07:17 -0800365 struct tm* tm = localtime(&t);
366 char timeBuffer[80];
367 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
368 VLOG("=================StatsdStats dump begins====================");
369 VLOG("Stats collection start second: %s", timeBuffer);
370 }
371 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800372 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800373 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr));
374
375 VLOG("%lu Config in icebox: ", (unsigned long)mIceBox.size());
376 for (const auto& configStats : mIceBox) {
377 const int numBytes = configStats.ByteSize();
378 vector<char> buffer(numBytes);
379 configStats.SerializeToArray(&buffer[0], numBytes);
380 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
381 buffer.size());
382
383 // surround the whole block with DEBUG, so that compiler can strip out the code
384 // in production.
385 if (DEBUG) {
386 VLOG("*****ICEBOX*****");
Yangster-mac94e197c2018-01-02 16:03:03 -0800387 VLOG("Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
Yao Chenb3561512017-11-21 18:07:17 -0800388 "#matcher=%d, #alert=%d, #valid=%d",
Yangster-mac94e197c2018-01-02 16:03:03 -0800389 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
Yao Chenb3561512017-11-21 18:07:17 -0800390 configStats.deletion_time_sec(), configStats.metric_count(),
391 configStats.condition_count(), configStats.matcher_count(),
392 configStats.alert_count(), configStats.is_valid());
393
394 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
395 VLOG("\tbroadcast time: %d", broadcastTime);
396 }
397
398 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
399 VLOG("\tdata drop time: %d", dataDropTime);
400 }
401 }
402 }
403
404 for (auto& pair : mConfigStats) {
405 auto& configStats = pair.second;
406 if (DEBUG) {
407 VLOG("********Active Configs***********");
Yangster-mac94e197c2018-01-02 16:03:03 -0800408 VLOG("Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
Yao Chenb3561512017-11-21 18:07:17 -0800409 "#matcher=%d, #alert=%d, #valid=%d",
Yangster-mac94e197c2018-01-02 16:03:03 -0800410 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
Yao Chenb3561512017-11-21 18:07:17 -0800411 configStats.deletion_time_sec(), configStats.metric_count(),
412 configStats.condition_count(), configStats.matcher_count(),
413 configStats.alert_count(), configStats.is_valid());
414 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
415 VLOG("\tbroadcast time: %d", broadcastTime);
416 }
417
418 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
419 VLOG("\tdata drop time: %d", dataDropTime);
420 }
Yao Chen69f1baf2017-11-27 17:25:36 -0800421
422 for (const auto& dumpTime : configStats.dump_report_time_sec()) {
423 VLOG("\tdump report time: %d", dumpTime);
424 }
Yao Chenb3561512017-11-21 18:07:17 -0800425 }
426
Bookatz8f2f3d82017-12-07 13:53:21 -0800427 addSubStatsToConfigLocked(pair.first, configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800428
429 const int numBytes = configStats.ByteSize();
430 vector<char> buffer(numBytes);
431 configStats.SerializeToArray(&buffer[0], numBytes);
432 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
433 buffer.size());
434 // reset the sub stats, the source of truth is in the individual map
435 // they will be repopulated when dumpStats() is called again.
436 configStats.clear_matcher_stats();
437 configStats.clear_condition_stats();
438 configStats.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800439 configStats.clear_alert_stats();
Yao Chenb3561512017-11-21 18:07:17 -0800440 }
441
Chenjie Yub038b702017-12-18 15:15:34 -0800442 VLOG("********Pushed Atom stats***********");
Yao Chenb3561512017-11-21 18:07:17 -0800443 const size_t atomCounts = mPushedAtomStats.size();
444 for (size_t i = 2; i < atomCounts; i++) {
445 if (mPushedAtomStats[i] > 0) {
446 long long token =
447 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
448 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
449 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
450 proto.end(token);
451
452 VLOG("Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
453 }
454 }
455
Chenjie Yub038b702017-12-18 15:15:34 -0800456 VLOG("********Pulled Atom stats***********");
457 for (const auto& pair : mPulledAtomStats) {
458 android::os::statsd::writePullerStatsToStream(pair, &proto);
459 VLOG("Atom %d->%ld, %ld, %ld\n", (int) pair.first, (long) pair.second.totalPull, (long) pair.second.totalPullFromCache, (long) pair.second.minPullIntervalSec);
460 }
461
Bookatz1d0136d2017-12-01 11:13:32 -0800462 if (mAnomalyAlarmRegisteredStats > 0) {
463 VLOG("********AnomalyAlarmStats stats***********");
464 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
465 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
466 mAnomalyAlarmRegisteredStats);
467 proto.end(token);
468 VLOG("Anomaly alarm registrations: %d", mAnomalyAlarmRegisteredStats);
469 }
470
David Chenc136f452017-11-27 11:52:26 -0800471 const int numBytes = mUidMapStats.ByteSize();
472 vector<char> buffer(numBytes);
473 mUidMapStats.SerializeToArray(&buffer[0], numBytes);
474 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
475 VLOG("UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes "
476 "lost=%d",
477 mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(),
478 mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes());
479
Yao Chenb3561512017-11-21 18:07:17 -0800480 output->clear();
481 size_t bufferSize = proto.size();
482 output->resize(bufferSize);
483
484 size_t pos = 0;
485 auto it = proto.data();
486 while (it.readBuffer() != NULL) {
487 size_t toRead = it.currentToRead();
488 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
489 pos += toRead;
490 it.rp()->move(toRead);
491 }
492
493 if (reset) {
494 resetInternalLocked();
495 }
496
497 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
498 VLOG("=================StatsdStats dump ends====================");
499}
500
501} // namespace statsd
502} // namespace os
503} // namespace android