blob: 33927aa9b44c281777c911a83caa2ddd9767e7ea [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>
22#include "statslog.h"
23
24namespace android {
25namespace os {
26namespace statsd {
27
28using android::util::FIELD_COUNT_REPEATED;
29using android::util::FIELD_TYPE_BOOL;
30using android::util::FIELD_TYPE_FLOAT;
31using android::util::FIELD_TYPE_INT32;
32using android::util::FIELD_TYPE_INT64;
33using android::util::FIELD_TYPE_MESSAGE;
34using android::util::FIELD_TYPE_STRING;
35using android::util::ProtoOutputStream;
36using std::lock_guard;
37using std::map;
38using std::string;
39using std::vector;
40
41const int FIELD_ID_BEGIN_TIME = 1;
42const int FIELD_ID_END_TIME = 2;
43const int FIELD_ID_CONFIG_STATS = 3;
Yao Chenb3561512017-11-21 18:07:17 -080044const int FIELD_ID_ATOM_STATS = 7;
David Chenc136f452017-11-27 11:52:26 -080045const int FIELD_ID_UIDMAP_STATS = 8;
Bookatz1d0136d2017-12-01 11:13:32 -080046const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
Yao Chenb3561512017-11-21 18:07:17 -080047
48const int FIELD_ID_MATCHER_STATS_NAME = 1;
49const int FIELD_ID_MATCHER_STATS_COUNT = 2;
50
51const int FIELD_ID_CONDITION_STATS_NAME = 1;
52const int FIELD_ID_CONDITION_STATS_COUNT = 2;
53
54const int FIELD_ID_METRIC_STATS_NAME = 1;
55const int FIELD_ID_METRIC_STATS_COUNT = 2;
56
57const int FIELD_ID_ATOM_STATS_TAG = 1;
58const int FIELD_ID_ATOM_STATS_COUNT = 2;
59
Bookatz1d0136d2017-12-01 11:13:32 -080060const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
61
Yao Chenb3561512017-11-21 18:07:17 -080062// TODO: add stats for pulled atoms.
63StatsdStats::StatsdStats() {
64 mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
Yao Chen69f1baf2017-11-27 17:25:36 -080065 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -080066}
67
68StatsdStats& StatsdStats::getInstance() {
69 static StatsdStats statsInstance;
70 return statsInstance;
71}
72
73void StatsdStats::noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
74 int matchersCount, int alertsCount, bool isValid) {
75 lock_guard<std::mutex> lock(mLock);
76 int32_t nowTimeSec = time(nullptr);
77
78 // If there is an existing config for the same key, icebox the old config.
79 noteConfigRemovedInternalLocked(key);
80
81 StatsdStatsReport_ConfigStats configStats;
82 configStats.set_uid(key.GetUid());
Yangster-mac94e197c2018-01-02 16:03:03 -080083 configStats.set_id(key.GetId());
Yao Chenb3561512017-11-21 18:07:17 -080084 configStats.set_creation_time_sec(nowTimeSec);
85 configStats.set_metric_count(metricsCount);
86 configStats.set_condition_count(conditionsCount);
87 configStats.set_matcher_count(matchersCount);
88 configStats.set_alert_count(alertsCount);
89 configStats.set_is_valid(isValid);
90
91 if (isValid) {
92 mConfigStats[key] = configStats;
93 } else {
94 configStats.set_deletion_time_sec(nowTimeSec);
95 mIceBox.push_back(configStats);
96 }
97}
98
99void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
100 auto it = mConfigStats.find(key);
101 if (it != mConfigStats.end()) {
102 int32_t nowTimeSec = time(nullptr);
103 it->second.set_deletion_time_sec(nowTimeSec);
Bookatz8f2f3d82017-12-07 13:53:21 -0800104 // Add condition stats, metrics stats, matcher stats, alert stats
105 addSubStatsToConfigLocked(key, it->second);
Yao Chenb3561512017-11-21 18:07:17 -0800106 // Remove them after they are added to the config stats.
107 mMatcherStats.erase(key);
108 mMetricsStats.erase(key);
Bookatz8f2f3d82017-12-07 13:53:21 -0800109 mAlertStats.erase(key);
Yao Chenb3561512017-11-21 18:07:17 -0800110 mConditionStats.erase(key);
111 mIceBox.push_back(it->second);
Yao Chen69f1baf2017-11-27 17:25:36 -0800112 mConfigStats.erase(it);
Yao Chenb3561512017-11-21 18:07:17 -0800113 }
114}
115
116void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
117 lock_guard<std::mutex> lock(mLock);
118 noteConfigRemovedInternalLocked(key);
119}
120
121void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800122 noteBroadcastSent(key, time(nullptr));
123}
124
125void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800126 lock_guard<std::mutex> lock(mLock);
127 auto it = mConfigStats.find(key);
128 if (it == mConfigStats.end()) {
129 ALOGE("Config key %s not found!", key.ToString().c_str());
130 return;
131 }
Yao Chen0fac5b12017-11-28 16:07:02 -0800132 if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) {
133 auto timestampList = it->second.mutable_broadcast_sent_time_sec();
134 // This is O(N) operation. It shouldn't happen often, and N is only 20.
135 timestampList->erase(timestampList->begin());
136 }
137 it->second.add_broadcast_sent_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800138}
139
Yao Chen69f1baf2017-11-27 17:25:36 -0800140void StatsdStats::noteDataDropped(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800141 noteDataDropped(key, time(nullptr));
142}
143
144void StatsdStats::noteDataDropped(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.data_drop_time_sec_size() >= kMaxTimestampCount) {
152 auto timestampList = it->second.mutable_data_drop_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_data_drop_time_sec(timeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800157}
158
Yao Chen69f1baf2017-11-27 17:25:36 -0800159void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
Yao Chen0fac5b12017-11-28 16:07:02 -0800160 noteMetricsReportSent(key, time(nullptr));
161}
162
163void StatsdStats::noteMetricsReportSent(const ConfigKey& key, int32_t timeSec) {
Yao Chen69f1baf2017-11-27 17:25:36 -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.dump_report_time_sec_size() >= kMaxTimestampCount) {
171 auto timestampList = it->second.mutable_dump_report_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_dump_report_time_sec(timeSec);
Yao Chen69f1baf2017-11-27 17:25:36 -0800176}
177
David Chenc136f452017-11-27 11:52:26 -0800178void StatsdStats::noteUidMapDropped(int snapshots, int deltas) {
179 lock_guard<std::mutex> lock(mLock);
180 mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots);
181 mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas);
182}
183
184void StatsdStats::setUidMapSnapshots(int snapshots) {
185 lock_guard<std::mutex> lock(mLock);
186 mUidMapStats.set_snapshots(snapshots);
187}
188
189void StatsdStats::setUidMapChanges(int changes) {
190 lock_guard<std::mutex> lock(mLock);
191 mUidMapStats.set_changes(changes);
192}
193
194void StatsdStats::setCurrentUidMapMemory(int bytes) {
195 lock_guard<std::mutex> lock(mLock);
196 mUidMapStats.set_bytes_used(bytes);
197}
198
Yangster-mac94e197c2018-01-02 16:03:03 -0800199void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800200 lock_guard<std::mutex> lock(mLock);
201 // if name doesn't exist before, it will create the key with count 0.
202 auto& conditionSizeMap = mConditionStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800203 if (size > conditionSizeMap[id]) {
204 conditionSizeMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800205 }
206}
207
Yangster-mac94e197c2018-01-02 16:03:03 -0800208void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
Yao Chenb3561512017-11-21 18:07:17 -0800209 lock_guard<std::mutex> lock(mLock);
210 // if name doesn't exist before, it will create the key with count 0.
211 auto& metricsDimensionMap = mMetricsStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800212 if (size > metricsDimensionMap[id]) {
213 metricsDimensionMap[id] = size;
Yao Chenb3561512017-11-21 18:07:17 -0800214 }
215}
216
Yangster-mac94e197c2018-01-02 16:03:03 -0800217void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
Yao Chenb3561512017-11-21 18:07:17 -0800218 lock_guard<std::mutex> lock(mLock);
219 auto& matcherStats = mMatcherStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800220 matcherStats[id]++;
Yao Chenb3561512017-11-21 18:07:17 -0800221}
222
Yangster-mac94e197c2018-01-02 16:03:03 -0800223void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800224 lock_guard<std::mutex> lock(mLock);
225 auto& alertStats = mAlertStats[key];
Yangster-mac94e197c2018-01-02 16:03:03 -0800226 alertStats[id]++;
Bookatz8f2f3d82017-12-07 13:53:21 -0800227}
228
Bookatz1d0136d2017-12-01 11:13:32 -0800229void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
230 lock_guard<std::mutex> lock(mLock);
231 mAnomalyAlarmRegisteredStats++;
232}
233
Yao Chenb3561512017-11-21 18:07:17 -0800234void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
235 lock_guard<std::mutex> lock(mLock);
236
Yao Chen69f1baf2017-11-27 17:25:36 -0800237 if (timeSec < mStartTimeSec) {
Yao Chenb3561512017-11-21 18:07:17 -0800238 return;
239 }
240
241 if (atomId > android::util::kMaxPushedAtomId) {
242 ALOGW("not interested in atom %d", atomId);
243 return;
244 }
245
246 mPushedAtomStats[atomId]++;
247}
248
249void StatsdStats::reset() {
250 lock_guard<std::mutex> lock(mLock);
251 resetInternalLocked();
252}
253
254void StatsdStats::resetInternalLocked() {
255 // Reset the historical data, but keep the active ConfigStats
Yao Chen69f1baf2017-11-27 17:25:36 -0800256 mStartTimeSec = time(nullptr);
Yao Chenb3561512017-11-21 18:07:17 -0800257 mIceBox.clear();
258 mConditionStats.clear();
259 mMetricsStats.clear();
260 std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
Bookatz8f2f3d82017-12-07 13:53:21 -0800261 mAlertStats.clear();
Bookatz1d0136d2017-12-01 11:13:32 -0800262 mAnomalyAlarmRegisteredStats = 0;
Yao Chenb3561512017-11-21 18:07:17 -0800263 mMatcherStats.clear();
Yao Chen0fac5b12017-11-28 16:07:02 -0800264 for (auto& config : mConfigStats) {
265 config.second.clear_broadcast_sent_time_sec();
266 config.second.clear_data_drop_time_sec();
267 config.second.clear_dump_report_time_sec();
268 config.second.clear_matcher_stats();
269 config.second.clear_condition_stats();
270 config.second.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800271 config.second.clear_alert_stats();
Yao Chen0fac5b12017-11-28 16:07:02 -0800272 }
Yao Chenb3561512017-11-21 18:07:17 -0800273}
274
Bookatz8f2f3d82017-12-07 13:53:21 -0800275void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key,
Yao Chenb3561512017-11-21 18:07:17 -0800276 StatsdStatsReport_ConfigStats& configStats) {
277 // Add matcher stats
278 if (mMatcherStats.find(key) != mMatcherStats.end()) {
279 const auto& matcherStats = mMatcherStats[key];
280 for (const auto& stats : matcherStats) {
281 auto output = configStats.add_matcher_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800282 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800283 output->set_matched_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800284 VLOG("matcher %lld matched %d times",
285 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800286 }
287 }
288 // Add condition stats
289 if (mConditionStats.find(key) != mConditionStats.end()) {
290 const auto& conditionStats = mConditionStats[key];
291 for (const auto& stats : conditionStats) {
292 auto output = configStats.add_condition_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800293 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800294 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800295 VLOG("condition %lld max output tuple size %d",
296 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800297 }
298 }
299 // Add metrics stats
300 if (mMetricsStats.find(key) != mMetricsStats.end()) {
301 const auto& conditionStats = mMetricsStats[key];
302 for (const auto& stats : conditionStats) {
303 auto output = configStats.add_metric_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800304 output->set_id(stats.first);
Yao Chenb3561512017-11-21 18:07:17 -0800305 output->set_max_tuple_counts(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800306 VLOG("metrics %lld max output tuple size %d",
307 (long long)stats.first, stats.second);
Yao Chenb3561512017-11-21 18:07:17 -0800308 }
309 }
Bookatz8f2f3d82017-12-07 13:53:21 -0800310 // Add anomaly detection alert stats
311 if (mAlertStats.find(key) != mAlertStats.end()) {
312 const auto& alertStats = mAlertStats[key];
313 for (const auto& stats : alertStats) {
314 auto output = configStats.add_alert_stats();
Yangster-mac94e197c2018-01-02 16:03:03 -0800315 output->set_id(stats.first);
Bookatze1d143a2017-12-13 15:21:57 -0800316 output->set_alerted_times(stats.second);
Yangster-mac94e197c2018-01-02 16:03:03 -0800317 VLOG("alert %lld declared %d times", (long long)stats.first, stats.second);
Bookatz8f2f3d82017-12-07 13:53:21 -0800318 }
319 }
Yao Chenb3561512017-11-21 18:07:17 -0800320}
321
Yao Chen69f1baf2017-11-27 17:25:36 -0800322void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
Yao Chenb3561512017-11-21 18:07:17 -0800323 lock_guard<std::mutex> lock(mLock);
324
325 if (DEBUG) {
Yao Chen69f1baf2017-11-27 17:25:36 -0800326 time_t t = mStartTimeSec;
Yao Chenb3561512017-11-21 18:07:17 -0800327 struct tm* tm = localtime(&t);
328 char timeBuffer[80];
329 strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
330 VLOG("=================StatsdStats dump begins====================");
331 VLOG("Stats collection start second: %s", timeBuffer);
332 }
333 ProtoOutputStream proto;
Yao Chen69f1baf2017-11-27 17:25:36 -0800334 proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
Yao Chenb3561512017-11-21 18:07:17 -0800335 proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr));
336
337 VLOG("%lu Config in icebox: ", (unsigned long)mIceBox.size());
338 for (const auto& configStats : mIceBox) {
339 const int numBytes = configStats.ByteSize();
340 vector<char> buffer(numBytes);
341 configStats.SerializeToArray(&buffer[0], numBytes);
342 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
343 buffer.size());
344
345 // surround the whole block with DEBUG, so that compiler can strip out the code
346 // in production.
347 if (DEBUG) {
348 VLOG("*****ICEBOX*****");
Yangster-mac94e197c2018-01-02 16:03:03 -0800349 VLOG("Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
Yao Chenb3561512017-11-21 18:07:17 -0800350 "#matcher=%d, #alert=%d, #valid=%d",
Yangster-mac94e197c2018-01-02 16:03:03 -0800351 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
Yao Chenb3561512017-11-21 18:07:17 -0800352 configStats.deletion_time_sec(), configStats.metric_count(),
353 configStats.condition_count(), configStats.matcher_count(),
354 configStats.alert_count(), configStats.is_valid());
355
356 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
357 VLOG("\tbroadcast time: %d", broadcastTime);
358 }
359
360 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
361 VLOG("\tdata drop time: %d", dataDropTime);
362 }
363 }
364 }
365
366 for (auto& pair : mConfigStats) {
367 auto& configStats = pair.second;
368 if (DEBUG) {
369 VLOG("********Active Configs***********");
Yangster-mac94e197c2018-01-02 16:03:03 -0800370 VLOG("Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
Yao Chenb3561512017-11-21 18:07:17 -0800371 "#matcher=%d, #alert=%d, #valid=%d",
Yangster-mac94e197c2018-01-02 16:03:03 -0800372 configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
Yao Chenb3561512017-11-21 18:07:17 -0800373 configStats.deletion_time_sec(), configStats.metric_count(),
374 configStats.condition_count(), configStats.matcher_count(),
375 configStats.alert_count(), configStats.is_valid());
376 for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
377 VLOG("\tbroadcast time: %d", broadcastTime);
378 }
379
380 for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
381 VLOG("\tdata drop time: %d", dataDropTime);
382 }
Yao Chen69f1baf2017-11-27 17:25:36 -0800383
384 for (const auto& dumpTime : configStats.dump_report_time_sec()) {
385 VLOG("\tdump report time: %d", dumpTime);
386 }
Yao Chenb3561512017-11-21 18:07:17 -0800387 }
388
Bookatz8f2f3d82017-12-07 13:53:21 -0800389 addSubStatsToConfigLocked(pair.first, configStats);
Yao Chenb3561512017-11-21 18:07:17 -0800390
391 const int numBytes = configStats.ByteSize();
392 vector<char> buffer(numBytes);
393 configStats.SerializeToArray(&buffer[0], numBytes);
394 proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
395 buffer.size());
396 // reset the sub stats, the source of truth is in the individual map
397 // they will be repopulated when dumpStats() is called again.
398 configStats.clear_matcher_stats();
399 configStats.clear_condition_stats();
400 configStats.clear_metric_stats();
Bookatz8f2f3d82017-12-07 13:53:21 -0800401 configStats.clear_alert_stats();
Yao Chenb3561512017-11-21 18:07:17 -0800402 }
403
404 VLOG("********Atom stats***********");
405 const size_t atomCounts = mPushedAtomStats.size();
406 for (size_t i = 2; i < atomCounts; i++) {
407 if (mPushedAtomStats[i] > 0) {
408 long long token =
409 proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
410 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
411 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
412 proto.end(token);
413
414 VLOG("Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
415 }
416 }
417
Bookatz1d0136d2017-12-01 11:13:32 -0800418 if (mAnomalyAlarmRegisteredStats > 0) {
419 VLOG("********AnomalyAlarmStats stats***********");
420 long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
421 proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
422 mAnomalyAlarmRegisteredStats);
423 proto.end(token);
424 VLOG("Anomaly alarm registrations: %d", mAnomalyAlarmRegisteredStats);
425 }
426
David Chenc136f452017-11-27 11:52:26 -0800427 const int numBytes = mUidMapStats.ByteSize();
428 vector<char> buffer(numBytes);
429 mUidMapStats.SerializeToArray(&buffer[0], numBytes);
430 proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
431 VLOG("UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes "
432 "lost=%d",
433 mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(),
434 mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes());
435
Yao Chenb3561512017-11-21 18:07:17 -0800436 output->clear();
437 size_t bufferSize = proto.size();
438 output->resize(bufferSize);
439
440 size_t pos = 0;
441 auto it = proto.data();
442 while (it.readBuffer() != NULL) {
443 size_t toRead = it.currentToRead();
444 std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
445 pos += toRead;
446 it.rp()->move(toRead);
447 }
448
449 if (reset) {
450 resetInternalLocked();
451 }
452
453 VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
454 VLOG("=================StatsdStats dump ends====================");
455}
456
457} // namespace statsd
458} // namespace os
459} // namespace android