blob: ea90170a53e13b2a9be520dd8397fcefd978139e [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
Yao Chen3c0b95c2017-12-16 14:34:20 -080017#define DEBUG false // STOPSHIP if true
Chenjie Yub3dda412017-10-24 13:41:59 -070018#include "Log.h"
19
20#include "ValueMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Yangster-mac20877162017-12-22 17:19:39 -080022#include "stats_log_util.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070023
24#include <cutils/log.h>
25#include <limits.h>
26#include <stdlib.h>
27
yrob0378b02017-11-09 20:36:25 -080028using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080029using 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;
Yangster-macd1815dc2017-11-13 21:43:15 -080034using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080035using android::util::ProtoOutputStream;
Chenjie Yub3dda412017-10-24 13:41:59 -070036using std::list;
Chenjie Yu6736c892017-11-09 10:50:09 -080037using std::make_pair;
Chenjie Yub3dda412017-10-24 13:41:59 -070038using std::make_shared;
Yao Chen93fe3a32017-11-02 13:52:59 -070039using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070040using std::shared_ptr;
41using std::unique_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070042using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070043
44namespace android {
45namespace os {
46namespace statsd {
47
yro2b0f8862017-11-06 14:27:31 -080048// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080049const int FIELD_ID_ID = 1;
yro2b0f8862017-11-06 14:27:31 -080050const int FIELD_ID_START_REPORT_NANOS = 2;
51const int FIELD_ID_END_REPORT_NANOS = 3;
52const int FIELD_ID_VALUE_METRICS = 7;
53// for ValueMetricDataWrapper
54const int FIELD_ID_DATA = 1;
55// for ValueMetricData
56const int FIELD_ID_DIMENSION = 1;
57const int FIELD_ID_BUCKET_INFO = 2;
yro2b0f8862017-11-06 14:27:31 -080058// for ValueBucketInfo
59const int FIELD_ID_START_BUCKET_NANOS = 1;
60const int FIELD_ID_END_BUCKET_NANOS = 2;
61const int FIELD_ID_VALUE = 3;
62
Chenjie Yu6736c892017-11-09 10:50:09 -080063static const uint64_t kDefaultBucketSizeMillis = 60 * 60 * 1000L;
64
Chenjie Yub3dda412017-10-24 13:41:59 -070065// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Yao Chenb3561512017-11-21 18:07:17 -080066ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
67 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070068 const sp<ConditionWizard>& wizard, const int pullTagId,
Chenjie Yu6736c892017-11-09 10:50:09 -080069 const uint64_t startTimeNs,
70 shared_ptr<StatsPullerManager> statsPullerManager)
Yangster-mac94e197c2018-01-02 16:03:03 -080071 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080072 mValueField(metric.value_field()),
Chenjie Yu6736c892017-11-09 10:50:09 -080073 mStatsPullerManager(statsPullerManager),
74 mPullTagId(pullTagId) {
Yao Chen93fe3a32017-11-02 13:52:59 -070075 // TODO: valuemetric for pushed events may need unlimited bucket length
Chenjie Yu6736c892017-11-09 10:50:09 -080076 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
Yao Chenf09569f2017-12-13 17:00:51 -080077 mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
Chenjie Yu6736c892017-11-09 10:50:09 -080078 } else {
79 mBucketSizeNs = kDefaultBucketSizeMillis * 1000 * 1000;
80 }
Chenjie Yub3dda412017-10-24 13:41:59 -070081
Yangster-mac20877162017-12-22 17:19:39 -080082 mDimensions = metric.dimensions();
Chenjie Yub3dda412017-10-24 13:41:59 -070083
Yao Chen93fe3a32017-11-02 13:52:59 -070084 if (metric.links().size() > 0) {
85 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
86 metric.links().end());
87 mConditionSliced = true;
88 }
Chenjie Yub3dda412017-10-24 13:41:59 -070089
Yao Chen93fe3a32017-11-02 13:52:59 -070090 if (!metric.has_condition() && mPullTagId != -1) {
Chenjie Yu6736c892017-11-09 10:50:09 -080091 VLOG("Setting up periodic pulling for %d", mPullTagId);
92 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
93 metric.bucket().bucket_size_millis());
Yao Chen93fe3a32017-11-02 13:52:59 -070094 }
Yangster-mac94e197c2018-01-02 16:03:03 -080095 VLOG("value metric %lld created. bucket size %lld start_time: %lld",
96 (long long)metric.id(), (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -070097}
98
Chenjie Yu6736c892017-11-09 10:50:09 -080099// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800100ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
101 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800102 const sp<ConditionWizard>& wizard, const int pullTagId,
103 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -0800104 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800105 make_shared<StatsPullerManager>()) {
106}
107
Chenjie Yub3dda412017-10-24 13:41:59 -0700108ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700109 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800110 if (mPullTagId != -1) {
111 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
112 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700113}
114
Yangsterf2bee6f2017-11-29 12:01:05 -0800115void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800116 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700117}
118
Yangster-mac20877162017-12-22 17:19:39 -0800119void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
120 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800121 report->set_metric_id(mMetricId);
Yangster-mac20877162017-12-22 17:19:39 -0800122 report->set_start_report_nanos(mStartTimeNs);
123 auto value_metrics = report->mutable_value_metrics();
124 for (const auto& pair : mPastBuckets) {
125 ValueMetricData* metricData = value_metrics->add_data();
126 *metricData->mutable_dimension() = pair.first.getDimensionsValue();
127 for (const auto& bucket : pair.second) {
128 ValueBucketInfo* bucketInfo = metricData->add_bucket_info();
129 bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs);
130 bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs);
131 bucketInfo->set_value(bucket.mValue);
132 }
133 }
134}
135
Yao Chen288c6002017-12-12 13:43:18 -0800136void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
137 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800138 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800139 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800140 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800141 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
142 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000143
Yao Chen93fe3a32017-11-02 13:52:59 -0700144 for (const auto& pair : mPastBuckets) {
145 const HashableDimensionKey& hashableKey = pair.first;
yro2b0f8862017-11-06 14:27:31 -0800146 VLOG(" dimension key %s", hashableKey.c_str());
yrob0378b02017-11-09 20:36:25 -0800147 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800148 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700149
Yangster-mac20877162017-12-22 17:19:39 -0800150 // First fill dimension.
151 long long dimensionToken = protoOutput->start(
152 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
153 writeDimensionsValueProtoToStream(hashableKey.getDimensionsValue(), protoOutput);
154 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800155
156 // Then fill bucket_info (ValueBucketInfo).
157 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800158 long long bucketInfoToken = protoOutput->start(
159 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
160 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
161 (long long)bucket.mBucketStartNs);
162 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
163 (long long)bucket.mBucketEndNs);
164 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
165 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800166 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
167 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
168 }
Yao Chen288c6002017-12-12 13:43:18 -0800169 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700170 }
Yao Chen288c6002017-12-12 13:43:18 -0800171 protoOutput->end(protoToken);
172 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
yro2b0f8862017-11-06 14:27:31 -0800173
Yangster-mac94e197c2018-01-02 16:03:03 -0800174 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000175 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800176 mStartTimeNs = mCurrentBucketStartTimeNs;
yro2b0f8862017-11-06 14:27:31 -0800177 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700178}
179
Yangsterf2bee6f2017-11-29 12:01:05 -0800180void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000181 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700182
Yao Chen2794da22017-12-13 16:01:55 -0800183 if (eventTime < mCurrentBucketStartTimeNs) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800184 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTime,
185 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800186 return;
187 }
188
Chenjie Yua7259ab2017-12-10 08:31:05 -0800189 flushIfNeededLocked(eventTime);
190
Yao Chen6a8c7992017-11-29 20:02:07 +0000191 if (mPullTagId != -1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700192 if (mCondition == true) {
Yao Chenf09569f2017-12-13 17:00:51 -0800193 mStatsPullerManager->RegisterReceiver(mPullTagId, this, mBucketSizeNs / 1000 / 1000);
Chenjie Yu6736c892017-11-09 10:50:09 -0800194 } else if (mCondition == false) {
195 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700196 }
Yangster8de69392017-11-27 13:48:29 -0800197
Yao Chen6a8c7992017-11-29 20:02:07 +0000198 vector<shared_ptr<LogEvent>> allData;
199 if (mStatsPullerManager->Pull(mPullTagId, &allData)) {
200 if (allData.size() == 0) {
201 return;
202 }
203 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800204 onMatchedLogEventLocked(0, *data);
Yao Chen6a8c7992017-11-29 20:02:07 +0000205 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000206 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700207 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700208 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700209}
210
211void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800212 std::lock_guard<std::mutex> lock(mMutex);
213
Yao Chenf09569f2017-12-13 17:00:51 -0800214 if (mCondition == true || mConditionTrackerIndex < 0) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700215 if (allData.size() == 0) {
216 return;
217 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800218 // For scheduled pulled data, the effective event time is snap to the nearest
219 // bucket boundary to make bucket finalize.
220 uint64_t realEventTime = allData.at(0)->GetTimestampNs();
221 uint64_t eventTime = mStartTimeNs + ((realEventTime - mStartTimeNs)/mBucketSizeNs) * mBucketSizeNs;
222
223 mCondition = false;
Chenjie Yub3dda412017-10-24 13:41:59 -0700224 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800225 data->setTimestampNs(eventTime-1);
226 onMatchedLogEventLocked(0, *data);
Chenjie Yub3dda412017-10-24 13:41:59 -0700227 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800228
229 mCondition = true;
230 for (const auto& data : allData) {
231 data->setTimestampNs(eventTime);
232 onMatchedLogEventLocked(0, *data);
233 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700234 }
235}
236
Yangsterf2bee6f2017-11-29 12:01:05 -0800237bool ValueMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800238 // ===========GuardRail==============
239 // 1. Report the tuple count if the tuple count > soft limit
240 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
241 return false;
242 }
243 if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
244 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800245 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800246 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
247 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800248 ALOGE("ValueMetric %lld dropping data for dimension key %s",
249 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800250 return true;
251 }
252 }
253
254 return false;
255}
256
Yangsterf2bee6f2017-11-29 12:01:05 -0800257void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster8de69392017-11-27 13:48:29 -0800258 const size_t matcherIndex, const HashableDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800259 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800260 const LogEvent& event) {
Yangster8de69392017-11-27 13:48:29 -0800261 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000262 if (eventTimeNs < mCurrentBucketStartTimeNs) {
263 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
264 (long long)mCurrentBucketStartTimeNs);
265 return;
266 }
267
Chenjie Yua7259ab2017-12-10 08:31:05 -0800268 flushIfNeededLocked(eventTimeNs);
269
Yangsterf2bee6f2017-11-29 12:01:05 -0800270 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800271 return;
272 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000273 Interval& interval = mCurrentSlicedBucket[eventKey];
274
275 long value = get_value(event);
276
Chenjie Yua7259ab2017-12-10 08:31:05 -0800277 if (mPullTagId != -1) { // for pulled events
278 if (mCondition == true) {
279 interval.start = value;
280 interval.startUpdated = true;
Yao Chen6a8c7992017-11-29 20:02:07 +0000281 } else {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800282 if (interval.startUpdated) {
283 interval.sum += (value - interval.start);
284 interval.startUpdated = false;
Yao Chen6a8c7992017-11-29 20:02:07 +0000285 } else {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800286 VLOG("No start for matching end %ld", value);
287 interval.tainted += 1;
Yao Chen6a8c7992017-11-29 20:02:07 +0000288 }
289 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800290 } else { // for pushed events
291 interval.sum += value;
Yangster8de69392017-11-27 13:48:29 -0800292 }
Bookatzde1b55622017-12-14 18:38:27 -0800293
294 for (auto& tracker : mAnomalyTrackers) {
295 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, interval.sum);
296 }
Yangster8de69392017-11-27 13:48:29 -0800297}
298
Yao Chen6a8c7992017-11-29 20:02:07 +0000299long ValueMetricProducer::get_value(const LogEvent& event) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700300 status_t err = NO_ERROR;
Yao Chenf09569f2017-12-13 17:00:51 -0800301 long val = event.GetLong(mValueField, &err);
Yao Chen93fe3a32017-11-02 13:52:59 -0700302 if (err == NO_ERROR) {
303 return val;
304 } else {
Chenjie Yu6736c892017-11-09 10:50:09 -0800305 VLOG("Can't find value in message. %s", event.ToString().c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700306 return 0;
307 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700308}
309
Yangsterf2bee6f2017-11-29 12:01:05 -0800310void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700311 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) {
312 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
313 (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs));
314 return;
315 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700316 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
317 (int)mCurrentSlicedBucket.size());
yro2b0f8862017-11-06 14:27:31 -0800318 ValueBucket info;
319 info.mBucketStartNs = mCurrentBucketStartTimeNs;
320 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800321 info.mBucketNum = mCurrentBucketNum;
Chenjie Yub3dda412017-10-24 13:41:59 -0700322
Chenjie Yu6736c892017-11-09 10:50:09 -0800323 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700324 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800325 tainted += slice.second.tainted;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800326 info.mValue = slice.second.sum;
Yao Chen93fe3a32017-11-02 13:52:59 -0700327 // it will auto create new vector of ValuebucketInfo if the key is not found.
328 auto& bucketList = mPastBuckets[slice.first];
329 bucketList.push_back(info);
Bookatzde1b55622017-12-14 18:38:27 -0800330
331 for (auto& tracker : mAnomalyTrackers) {
332 if (tracker != nullptr) {
333 tracker->addPastBucket(slice.first, info.mValue, info.mBucketNum);
334 }
335 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700336 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800337 VLOG("%d tainted pairs in the bucket", tainted);
Chenjie Yub3dda412017-10-24 13:41:59 -0700338
339 // Reset counters
Chenjie Yua7259ab2017-12-10 08:31:05 -0800340 mCurrentSlicedBucket.clear();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800341
Chenjie Yub3dda412017-10-24 13:41:59 -0700342 int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800343 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
344 mCurrentBucketNum += numBucketsForward;
345
Yao Chen93fe3a32017-11-02 13:52:59 -0700346 if (numBucketsForward > 1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700347 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
348 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800349 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
Chenjie Yub3dda412017-10-24 13:41:59 -0700350 (long long)mCurrentBucketStartTimeNs);
351}
352
Yangsterf2bee6f2017-11-29 12:01:05 -0800353size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800354 size_t totalSize = 0;
355 for (const auto& pair : mPastBuckets) {
356 totalSize += pair.second.size() * kBucketSize;
357 }
358 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800359}
360
Chenjie Yub3dda412017-10-24 13:41:59 -0700361} // namespace statsd
362} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700363} // namespace android