blob: c20c302942974dda4d57e9fc239d12f534527c3c [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 }
Yangster-macd1815dc2017-11-13 21:43:15 -0800100 VLOG("value metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen93fe3a32017-11-02 13:52:59 -0700101 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700102}
103
Chenjie Yu6736c892017-11-09 10:50:09 -0800104// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800105ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
106 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800107 const sp<ConditionWizard>& wizard, const int pullTagId,
108 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -0800109 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800110 make_shared<StatsPullerManager>()) {
111}
112
Chenjie Yub3dda412017-10-24 13:41:59 -0700113ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700114 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800115 if (mPullTagId != -1) {
116 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
117 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700118}
119
Yangsterf2bee6f2017-11-29 12:01:05 -0800120void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800121 VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
Chenjie Yub3dda412017-10-24 13:41:59 -0700122}
123
Yao Chen288c6002017-12-12 13:43:18 -0800124void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
125 ProtoOutputStream* protoOutput) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000126 VLOG("metric %s dump report now...", mMetric.name().c_str());
Yao Chen288c6002017-12-12 13:43:18 -0800127 flushIfNeededLocked(dumpTimeNs);
128 protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
129 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
130 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000131
Yao Chen93fe3a32017-11-02 13:52:59 -0700132 for (const auto& pair : mPastBuckets) {
133 const HashableDimensionKey& hashableKey = pair.first;
yro2b0f8862017-11-06 14:27:31 -0800134 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700135 auto it = mDimensionKeyMap.find(hashableKey);
136 if (it == mDimensionKeyMap.end()) {
137 ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str());
138 continue;
139 }
yrob0378b02017-11-09 20:36:25 -0800140 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800141 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700142
yro2b0f8862017-11-06 14:27:31 -0800143 // First fill dimension (KeyValuePairs).
144 for (const auto& kv : it->second) {
Yao Chen288c6002017-12-12 13:43:18 -0800145 long long dimensionToken = protoOutput->start(
146 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
147 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
yro2b0f8862017-11-06 14:27:31 -0800148 if (kv.has_value_str()) {
Yao Chen288c6002017-12-12 13:43:18 -0800149 protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro2b0f8862017-11-06 14:27:31 -0800150 } else if (kv.has_value_int()) {
Yao Chen288c6002017-12-12 13:43:18 -0800151 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
yro2b0f8862017-11-06 14:27:31 -0800152 } else if (kv.has_value_bool()) {
Yao Chen288c6002017-12-12 13:43:18 -0800153 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
yro2b0f8862017-11-06 14:27:31 -0800154 } else if (kv.has_value_float()) {
Yao Chen288c6002017-12-12 13:43:18 -0800155 protoOutput->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
yro2b0f8862017-11-06 14:27:31 -0800156 }
Yao Chen288c6002017-12-12 13:43:18 -0800157 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800158 }
159
160 // Then fill bucket_info (ValueBucketInfo).
161 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800162 long long bucketInfoToken = protoOutput->start(
163 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
164 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
165 (long long)bucket.mBucketStartNs);
166 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
167 (long long)bucket.mBucketEndNs);
168 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
169 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800170 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
171 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
172 }
Yao Chen288c6002017-12-12 13:43:18 -0800173 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700174 }
Yao Chen288c6002017-12-12 13:43:18 -0800175 protoOutput->end(protoToken);
176 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
yro2b0f8862017-11-06 14:27:31 -0800177
Yangster-macd1815dc2017-11-13 21:43:15 -0800178 VLOG("metric %s dump report now...", mMetric.name().c_str());
Yao Chen6a8c7992017-11-29 20:02:07 +0000179 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800180 mStartTimeNs = mCurrentBucketStartTimeNs;
yro2b0f8862017-11-06 14:27:31 -0800181 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700182}
183
Yangsterf2bee6f2017-11-29 12:01:05 -0800184void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000185 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700186
Yao Chen2794da22017-12-13 16:01:55 -0800187 if (eventTime < mCurrentBucketStartTimeNs) {
188 return;
189 }
190
Yao Chen6a8c7992017-11-29 20:02:07 +0000191 if (mPullTagId != -1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700192 if (mCondition == true) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800193 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
194 mMetric.bucket().bucket_size_millis());
195 } else if (mCondition == false) {
196 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700197 }
Yangster8de69392017-11-27 13:48:29 -0800198
Yao Chen6a8c7992017-11-29 20:02:07 +0000199 vector<shared_ptr<LogEvent>> allData;
200 if (mStatsPullerManager->Pull(mPullTagId, &allData)) {
201 if (allData.size() == 0) {
202 return;
203 }
204 for (const auto& data : allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800205 onMatchedLogEventLocked(0, *data, false);
Yao Chen6a8c7992017-11-29 20:02:07 +0000206 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800207 flushIfNeededLocked(eventTime);
Yao Chen6a8c7992017-11-29 20:02:07 +0000208 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700209 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700210 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700211}
212
213void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800214 std::lock_guard<std::mutex> lock(mMutex);
215
Yao Chen6a8c7992017-11-29 20:02:07 +0000216 if (mCondition == true || !mMetric.has_condition()) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700217 if (allData.size() == 0) {
218 return;
219 }
220 uint64_t eventTime = allData.at(0)->GetTimestampNs();
Chenjie Yu6736c892017-11-09 10:50:09 -0800221 // alarm is not accurate and might drift.
222 if (eventTime > mCurrentBucketStartTimeNs + mBucketSizeNs * 3 / 2) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800223 flushIfNeededLocked(eventTime);
Chenjie Yu6736c892017-11-09 10:50:09 -0800224 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700225 for (const auto& data : allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800226 onMatchedLogEventLocked(0, *data, true);
Chenjie Yub3dda412017-10-24 13:41:59 -0700227 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800228 flushIfNeededLocked(eventTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700229 }
230}
231
Yangsterf2bee6f2017-11-29 12:01:05 -0800232bool ValueMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800233 // ===========GuardRail==============
234 // 1. Report the tuple count if the tuple count > soft limit
235 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
236 return false;
237 }
238 if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
239 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
240 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
241 newTupleCount);
242 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
243 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
244 ALOGE("ValueMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
245 newKey.c_str());
246 return true;
247 }
248 }
249
250 return false;
251}
252
Yangsterf2bee6f2017-11-29 12:01:05 -0800253void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster8de69392017-11-27 13:48:29 -0800254 const size_t matcherIndex, const HashableDimensionKey& eventKey,
255 const map<string, HashableDimensionKey>& conditionKey, bool condition,
256 const LogEvent& event, bool scheduledPull) {
257 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000258 if (eventTimeNs < mCurrentBucketStartTimeNs) {
259 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
260 (long long)mCurrentBucketStartTimeNs);
261 return;
262 }
263
Yangsterf2bee6f2017-11-29 12:01:05 -0800264 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800265 return;
266 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000267 Interval& interval = mCurrentSlicedBucket[eventKey];
268
269 long value = get_value(event);
270
Yangster8de69392017-11-27 13:48:29 -0800271 if (mPullTagId != -1) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000272 if (scheduledPull) {
273 // scheduled pull always sets beginning of current bucket and end
274 // of next bucket
275 if (interval.raw.size() > 0) {
276 interval.raw.back().second = value;
277 } else {
278 interval.raw.push_back(make_pair(value, value));
279 }
280 Interval& nextInterval = mNextSlicedBucket[eventKey];
281 if (nextInterval.raw.size() == 0) {
282 nextInterval.raw.push_back(make_pair(value, 0));
283 } else {
284 nextInterval.raw.front().first = value;
285 }
286 } else {
287 if (mCondition == true) {
288 interval.raw.push_back(make_pair(value, 0));
289 } else {
290 if (interval.raw.size() != 0) {
291 interval.raw.back().second = value;
292 } else {
293 interval.tainted = true;
294 VLOG("Data on condition true missing!");
295 }
296 }
297 }
Yangster8de69392017-11-27 13:48:29 -0800298 } else {
Yangsterf2bee6f2017-11-29 12:01:05 -0800299 flushIfNeededLocked(eventTimeNs);
Yao Chen6a8c7992017-11-29 20:02:07 +0000300 interval.raw.push_back(make_pair(value, 0));
Yangster8de69392017-11-27 13:48:29 -0800301 }
302}
303
Yao Chen6a8c7992017-11-29 20:02:07 +0000304long ValueMetricProducer::get_value(const LogEvent& event) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700305 status_t err = NO_ERROR;
306 long val = event.GetLong(mMetric.value_field(), &err);
307 if (err == NO_ERROR) {
308 return val;
309 } else {
Chenjie Yu6736c892017-11-09 10:50:09 -0800310 VLOG("Can't find value in message. %s", event.ToString().c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700311 return 0;
312 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700313}
314
Yangsterf2bee6f2017-11-29 12:01:05 -0800315void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700316 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) {
317 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
318 (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs));
319 return;
320 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700321 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
322 (int)mCurrentSlicedBucket.size());
yro2b0f8862017-11-06 14:27:31 -0800323 ValueBucket info;
324 info.mBucketStartNs = mCurrentBucketStartTimeNs;
325 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800326 info.mBucketNum = mCurrentBucketNum;
Chenjie Yub3dda412017-10-24 13:41:59 -0700327
Chenjie Yu6736c892017-11-09 10:50:09 -0800328 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700329 for (const auto& slice : mCurrentSlicedBucket) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700330 long value = 0;
Chenjie Yu6736c892017-11-09 10:50:09 -0800331 if (mPullTagId != -1) {
332 for (const auto& pair : slice.second.raw) {
333 value += (pair.second - pair.first);
334 }
335 } else {
336 for (const auto& pair : slice.second.raw) {
337 value += pair.first;
338 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700339 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800340 tainted += slice.second.tainted;
yro2b0f8862017-11-06 14:27:31 -0800341 info.mValue = value;
Chenjie Yu6736c892017-11-09 10:50:09 -0800342 VLOG(" %s, %ld, %d", slice.first.c_str(), value, tainted);
Yao Chen93fe3a32017-11-02 13:52:59 -0700343 // it will auto create new vector of ValuebucketInfo if the key is not found.
344 auto& bucketList = mPastBuckets[slice.first];
345 bucketList.push_back(info);
Chenjie Yub3dda412017-10-24 13:41:59 -0700346 }
347
348 // Reset counters
349 mCurrentSlicedBucket.swap(mNextSlicedBucket);
350 mNextSlicedBucket.clear();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800351
Chenjie Yub3dda412017-10-24 13:41:59 -0700352 int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800353 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
354 mCurrentBucketNum += numBucketsForward;
355
Yao Chen93fe3a32017-11-02 13:52:59 -0700356 if (numBucketsForward > 1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700357 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
358 }
Yangster-macd1815dc2017-11-13 21:43:15 -0800359 VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
Chenjie Yub3dda412017-10-24 13:41:59 -0700360 (long long)mCurrentBucketStartTimeNs);
361}
362
Yangsterf2bee6f2017-11-29 12:01:05 -0800363size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800364 size_t totalSize = 0;
365 for (const auto& pair : mPastBuckets) {
366 totalSize += pair.second.size() * kBucketSize;
367 }
368 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800369}
370
Chenjie Yub3dda412017-10-24 13:41:59 -0700371} // namespace statsd
372} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700373} // namespace android