blob: 977aa88a14d285af658a8309a4b96c96ea8cb242 [file] [log] [blame]
Chenjie Yub3dda412017-10-24 13:41:59 -07001/*
2 * Copyright (C) 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
17#define DEBUG true // STOPSHIP if true
18#include "Log.h"
19
20#include "ValueMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070022
23#include <cutils/log.h>
24#include <limits.h>
25#include <stdlib.h>
26
yrob0378b02017-11-09 20:36:25 -080027using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080028using android::util::FIELD_TYPE_BOOL;
29using android::util::FIELD_TYPE_FLOAT;
30using android::util::FIELD_TYPE_INT32;
31using android::util::FIELD_TYPE_INT64;
32using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080033using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080034using android::util::ProtoOutputStream;
Chenjie Yub3dda412017-10-24 13:41:59 -070035using std::list;
Chenjie Yu6736c892017-11-09 10:50:09 -080036using std::make_pair;
Chenjie Yub3dda412017-10-24 13:41:59 -070037using std::make_shared;
Yao Chen93fe3a32017-11-02 13:52:59 -070038using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070039using std::shared_ptr;
40using std::unique_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070041using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070042
43namespace android {
44namespace os {
45namespace statsd {
46
yro2b0f8862017-11-06 14:27:31 -080047// for StatsLogReport
Yangster-macd1815dc2017-11-13 21:43:15 -080048const int FIELD_ID_NAME = 1;
yro2b0f8862017-11-06 14:27:31 -080049const int FIELD_ID_START_REPORT_NANOS = 2;
50const int FIELD_ID_END_REPORT_NANOS = 3;
51const int FIELD_ID_VALUE_METRICS = 7;
52// for ValueMetricDataWrapper
53const int FIELD_ID_DATA = 1;
54// for ValueMetricData
55const int FIELD_ID_DIMENSION = 1;
56const int FIELD_ID_BUCKET_INFO = 2;
57// for KeyValuePair
58const int FIELD_ID_KEY = 1;
59const int FIELD_ID_VALUE_STR = 2;
60const int FIELD_ID_VALUE_INT = 3;
61const int FIELD_ID_VALUE_BOOL = 4;
62const int FIELD_ID_VALUE_FLOAT = 5;
63// for ValueBucketInfo
64const int FIELD_ID_START_BUCKET_NANOS = 1;
65const int FIELD_ID_END_BUCKET_NANOS = 2;
66const int FIELD_ID_VALUE = 3;
67
Chenjie Yu6736c892017-11-09 10:50:09 -080068static const uint64_t kDefaultBucketSizeMillis = 60 * 60 * 1000L;
69
Chenjie Yub3dda412017-10-24 13:41:59 -070070// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Yao Chenb3561512017-11-21 18:07:17 -080071ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
72 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070073 const sp<ConditionWizard>& wizard, const int pullTagId,
Chenjie Yu6736c892017-11-09 10:50:09 -080074 const uint64_t startTimeNs,
75 shared_ptr<StatsPullerManager> statsPullerManager)
Yao Chenb3561512017-11-21 18:07:17 -080076 : MetricProducer(key, startTimeNs, conditionIndex, wizard),
Chenjie Yu6736c892017-11-09 10:50:09 -080077 mMetric(metric),
78 mStatsPullerManager(statsPullerManager),
79 mPullTagId(pullTagId) {
Yao Chen93fe3a32017-11-02 13:52:59 -070080 // TODO: valuemetric for pushed events may need unlimited bucket length
Chenjie Yu6736c892017-11-09 10:50:09 -080081 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
82 mBucketSizeNs = mMetric.bucket().bucket_size_millis() * 1000 * 1000;
83 } else {
84 mBucketSizeNs = kDefaultBucketSizeMillis * 1000 * 1000;
85 }
Chenjie Yub3dda412017-10-24 13:41:59 -070086
Yao Chen93fe3a32017-11-02 13:52:59 -070087 mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());
Chenjie Yub3dda412017-10-24 13:41:59 -070088
Yao Chen93fe3a32017-11-02 13:52:59 -070089 if (metric.links().size() > 0) {
90 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
91 metric.links().end());
92 mConditionSliced = true;
93 }
Chenjie Yub3dda412017-10-24 13:41:59 -070094
Yao Chen93fe3a32017-11-02 13:52:59 -070095 if (!metric.has_condition() && mPullTagId != -1) {
Chenjie Yu6736c892017-11-09 10:50:09 -080096 VLOG("Setting up periodic pulling for %d", mPullTagId);
97 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
98 metric.bucket().bucket_size_millis());
Yao Chen93fe3a32017-11-02 13:52:59 -070099 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700100
Yangsterf2bee6f2017-11-29 12:01:05 -0800101 startNewProtoOutputStreamLocked(mStartTimeNs);
yro2b0f8862017-11-06 14:27:31 -0800102
Yangster-macd1815dc2017-11-13 21:43:15 -0800103 VLOG("value metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen93fe3a32017-11-02 13:52:59 -0700104 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700105}
106
Chenjie Yu6736c892017-11-09 10:50:09 -0800107// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800108ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
109 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800110 const sp<ConditionWizard>& wizard, const int pullTagId,
111 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -0800112 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800113 make_shared<StatsPullerManager>()) {
114}
115
Chenjie Yub3dda412017-10-24 13:41:59 -0700116ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700117 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800118 if (mPullTagId != -1) {
119 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
120 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700121}
122
Yangsterf2bee6f2017-11-29 12:01:05 -0800123void ValueMetricProducer::startNewProtoOutputStreamLocked(long long startTime) {
yro2b0f8862017-11-06 14:27:31 -0800124 mProto = std::make_unique<ProtoOutputStream>();
Yangster-macd1815dc2017-11-13 21:43:15 -0800125 mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
yro2b0f8862017-11-06 14:27:31 -0800126 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
127 mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
128}
129
Chenjie Yub3dda412017-10-24 13:41:59 -0700130void ValueMetricProducer::finish() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700131 // TODO: write the StatsLogReport to dropbox using
132 // DropboxWriter.
Chenjie Yub3dda412017-10-24 13:41:59 -0700133}
134
Yangsterf2bee6f2017-11-29 12:01:05 -0800135void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800136 VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
Chenjie Yub3dda412017-10-24 13:41:59 -0700137}
138
Yangsterf2bee6f2017-11-29 12:01:05 -0800139std::unique_ptr<std::vector<uint8_t>> ValueMetricProducer::onDumpReportLocked() {
Yao Chen6a8c7992017-11-29 20:02:07 +0000140 VLOG("metric %s dump report now...", mMetric.name().c_str());
141
Yao Chen93fe3a32017-11-02 13:52:59 -0700142 for (const auto& pair : mPastBuckets) {
143 const HashableDimensionKey& hashableKey = pair.first;
yro2b0f8862017-11-06 14:27:31 -0800144 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700145 auto it = mDimensionKeyMap.find(hashableKey);
146 if (it == mDimensionKeyMap.end()) {
147 ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str());
148 continue;
149 }
yrob0378b02017-11-09 20:36:25 -0800150 long long wrapperToken =
151 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700152
yro2b0f8862017-11-06 14:27:31 -0800153 // First fill dimension (KeyValuePairs).
154 for (const auto& kv : it->second) {
yrob0378b02017-11-09 20:36:25 -0800155 long long dimensionToken =
156 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
yro2b0f8862017-11-06 14:27:31 -0800157 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
158 if (kv.has_value_str()) {
Yao Chen1ff4f432017-11-16 17:01:40 -0800159 mProto->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro2b0f8862017-11-06 14:27:31 -0800160 } else if (kv.has_value_int()) {
161 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
162 } else if (kv.has_value_bool()) {
163 mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
164 } else if (kv.has_value_float()) {
165 mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
166 }
167 mProto->end(dimensionToken);
168 }
169
170 // Then fill bucket_info (ValueBucketInfo).
171 for (const auto& bucket : pair.second) {
yrob0378b02017-11-09 20:36:25 -0800172 long long bucketInfoToken =
173 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
yro2b0f8862017-11-06 14:27:31 -0800174 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
175 (long long)bucket.mBucketStartNs);
176 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
177 (long long)bucket.mBucketEndNs);
178 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
179 mProto->end(bucketInfoToken);
180 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
181 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
182 }
183 mProto->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700184 }
yro2b0f8862017-11-06 14:27:31 -0800185 mProto->end(mProtoToken);
186 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS,
187 (long long)mCurrentBucketStartTimeNs);
188
Yangster-macd1815dc2017-11-13 21:43:15 -0800189 VLOG("metric %s dump report now...", mMetric.name().c_str());
Yangsterf2bee6f2017-11-29 12:01:05 -0800190 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked();
yro2b0f8862017-11-06 14:27:31 -0800191
Yangsterf2bee6f2017-11-29 12:01:05 -0800192 startNewProtoOutputStreamLocked(time(nullptr) * NS_PER_SEC);
Yao Chen6a8c7992017-11-29 20:02:07 +0000193 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800194
yro17adac92017-11-08 23:16:29 -0800195 return buffer;
Yao Chen6a8c7992017-11-29 20:02:07 +0000196
yro2b0f8862017-11-06 14:27:31 -0800197 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700198}
199
Yangsterf2bee6f2017-11-29 12:01:05 -0800200void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000201 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700202
Yao Chen6a8c7992017-11-29 20:02:07 +0000203 if (mPullTagId != -1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700204 if (mCondition == true) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800205 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
206 mMetric.bucket().bucket_size_millis());
207 } else if (mCondition == false) {
208 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700209 }
Yangster8de69392017-11-27 13:48:29 -0800210
Yao Chen6a8c7992017-11-29 20:02:07 +0000211 vector<shared_ptr<LogEvent>> allData;
212 if (mStatsPullerManager->Pull(mPullTagId, &allData)) {
213 if (allData.size() == 0) {
214 return;
215 }
216 for (const auto& data : allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800217 onMatchedLogEventLocked(0, *data, false);
Yao Chen6a8c7992017-11-29 20:02:07 +0000218 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800219 flushIfNeededLocked(eventTime);
Yao Chen6a8c7992017-11-29 20:02:07 +0000220 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700221 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700222 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700223}
224
225void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800226 std::lock_guard<std::mutex> lock(mMutex);
227
Yao Chen6a8c7992017-11-29 20:02:07 +0000228 if (mCondition == true || !mMetric.has_condition()) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700229 if (allData.size() == 0) {
230 return;
231 }
232 uint64_t eventTime = allData.at(0)->GetTimestampNs();
Chenjie Yu6736c892017-11-09 10:50:09 -0800233 // alarm is not accurate and might drift.
234 if (eventTime > mCurrentBucketStartTimeNs + mBucketSizeNs * 3 / 2) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800235 flushIfNeededLocked(eventTime);
Chenjie Yu6736c892017-11-09 10:50:09 -0800236 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700237 for (const auto& data : allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800238 onMatchedLogEventLocked(0, *data, true);
Chenjie Yub3dda412017-10-24 13:41:59 -0700239 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800240 flushIfNeededLocked(eventTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700241 }
242}
243
Yangsterf2bee6f2017-11-29 12:01:05 -0800244bool ValueMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800245 // ===========GuardRail==============
246 // 1. Report the tuple count if the tuple count > soft limit
247 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
248 return false;
249 }
250 if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
251 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
252 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
253 newTupleCount);
254 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
255 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
256 ALOGE("ValueMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
257 newKey.c_str());
258 return true;
259 }
260 }
261
262 return false;
263}
264
Yangsterf2bee6f2017-11-29 12:01:05 -0800265void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster8de69392017-11-27 13:48:29 -0800266 const size_t matcherIndex, const HashableDimensionKey& eventKey,
267 const map<string, HashableDimensionKey>& conditionKey, bool condition,
268 const LogEvent& event, bool scheduledPull) {
269 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000270 if (eventTimeNs < mCurrentBucketStartTimeNs) {
271 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
272 (long long)mCurrentBucketStartTimeNs);
273 return;
274 }
275
Yangsterf2bee6f2017-11-29 12:01:05 -0800276 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800277 return;
278 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000279 Interval& interval = mCurrentSlicedBucket[eventKey];
280
281 long value = get_value(event);
282
Yangster8de69392017-11-27 13:48:29 -0800283 if (mPullTagId != -1) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000284 if (scheduledPull) {
285 // scheduled pull always sets beginning of current bucket and end
286 // of next bucket
287 if (interval.raw.size() > 0) {
288 interval.raw.back().second = value;
289 } else {
290 interval.raw.push_back(make_pair(value, value));
291 }
292 Interval& nextInterval = mNextSlicedBucket[eventKey];
293 if (nextInterval.raw.size() == 0) {
294 nextInterval.raw.push_back(make_pair(value, 0));
295 } else {
296 nextInterval.raw.front().first = value;
297 }
298 } else {
299 if (mCondition == true) {
300 interval.raw.push_back(make_pair(value, 0));
301 } else {
302 if (interval.raw.size() != 0) {
303 interval.raw.back().second = value;
304 } else {
305 interval.tainted = true;
306 VLOG("Data on condition true missing!");
307 }
308 }
309 }
Yangster8de69392017-11-27 13:48:29 -0800310 } else {
Yangsterf2bee6f2017-11-29 12:01:05 -0800311 flushIfNeededLocked(eventTimeNs);
Yao Chen6a8c7992017-11-29 20:02:07 +0000312 interval.raw.push_back(make_pair(value, 0));
Yangster8de69392017-11-27 13:48:29 -0800313 }
314}
315
Yao Chen6a8c7992017-11-29 20:02:07 +0000316long ValueMetricProducer::get_value(const LogEvent& event) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700317 status_t err = NO_ERROR;
318 long val = event.GetLong(mMetric.value_field(), &err);
319 if (err == NO_ERROR) {
320 return val;
321 } else {
Chenjie Yu6736c892017-11-09 10:50:09 -0800322 VLOG("Can't find value in message. %s", event.ToString().c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700323 return 0;
324 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700325}
326
Yangsterf2bee6f2017-11-29 12:01:05 -0800327void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700328 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) {
329 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
330 (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs));
331 return;
332 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700333 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
334 (int)mCurrentSlicedBucket.size());
yro2b0f8862017-11-06 14:27:31 -0800335 ValueBucket info;
336 info.mBucketStartNs = mCurrentBucketStartTimeNs;
337 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800338 info.mBucketNum = mCurrentBucketNum;
Chenjie Yub3dda412017-10-24 13:41:59 -0700339
Chenjie Yu6736c892017-11-09 10:50:09 -0800340 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700341 for (const auto& slice : mCurrentSlicedBucket) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700342 long value = 0;
Chenjie Yu6736c892017-11-09 10:50:09 -0800343 if (mPullTagId != -1) {
344 for (const auto& pair : slice.second.raw) {
345 value += (pair.second - pair.first);
346 }
347 } else {
348 for (const auto& pair : slice.second.raw) {
349 value += pair.first;
350 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700351 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800352 tainted += slice.second.tainted;
yro2b0f8862017-11-06 14:27:31 -0800353 info.mValue = value;
Chenjie Yu6736c892017-11-09 10:50:09 -0800354 VLOG(" %s, %ld, %d", slice.first.c_str(), value, tainted);
Yao Chen93fe3a32017-11-02 13:52:59 -0700355 // it will auto create new vector of ValuebucketInfo if the key is not found.
356 auto& bucketList = mPastBuckets[slice.first];
357 bucketList.push_back(info);
Chenjie Yub3dda412017-10-24 13:41:59 -0700358 }
359
360 // Reset counters
361 mCurrentSlicedBucket.swap(mNextSlicedBucket);
362 mNextSlicedBucket.clear();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800363
Chenjie Yub3dda412017-10-24 13:41:59 -0700364 int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800365 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
366 mCurrentBucketNum += numBucketsForward;
367
Yao Chen93fe3a32017-11-02 13:52:59 -0700368 if (numBucketsForward > 1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700369 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
370 }
Yangster-macd1815dc2017-11-13 21:43:15 -0800371 VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
Chenjie Yub3dda412017-10-24 13:41:59 -0700372 (long long)mCurrentBucketStartTimeNs);
373}
374
Yangsterf2bee6f2017-11-29 12:01:05 -0800375size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800376 size_t totalSize = 0;
377 for (const auto& pair : mPastBuckets) {
378 totalSize += pair.second.size() * kBucketSize;
379 }
380 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800381}
382
Chenjie Yub3dda412017-10-24 13:41:59 -0700383} // namespace statsd
384} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700385} // namespace android