blob: cf56e2d4338583415d9dfee1c1ce8a4f052257bf [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
Chenjie Yu88588972018-08-03 09:49:22 -070017#define DEBUG false // STOPSHIP if true
Chenjie Yub3dda412017-10-24 13:41:59 -070018#include "Log.h"
19
20#include "ValueMetricProducer.h"
Chenjie Yuc5875052018-03-09 10:13:11 -080021#include "../guardrail/StatsdStats.h"
22#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;
Chenjie Yua0f02242018-07-06 16:14:34 -070030using android::util::FIELD_TYPE_DOUBLE;
yro2b0f8862017-11-06 14:27:31 -080031using 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_VALUE_METRICS = 7;
Yangster-mac9def8e32018-04-17 13:55:51 -070051const int FIELD_ID_TIME_BASE = 9;
52const int FIELD_ID_BUCKET_SIZE = 10;
53const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11;
54const int FIELD_ID_DIMENSION_PATH_IN_CONDITION = 12;
Howard Ro9440e092018-12-16 19:15:21 -080055const int FIELD_ID_IS_ACTIVE = 14;
yro2b0f8862017-11-06 14:27:31 -080056// for ValueMetricDataWrapper
57const int FIELD_ID_DATA = 1;
David Chen81245fd2018-04-12 14:33:37 -070058const int FIELD_ID_SKIPPED = 2;
Yangster-mac9def8e32018-04-17 13:55:51 -070059const int FIELD_ID_SKIPPED_START_MILLIS = 3;
60const int FIELD_ID_SKIPPED_END_MILLIS = 4;
yro2b0f8862017-11-06 14:27:31 -080061// for ValueMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080062const int FIELD_ID_DIMENSION_IN_WHAT = 1;
63const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
64const int FIELD_ID_BUCKET_INFO = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070065const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
66const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5;
yro2b0f8862017-11-06 14:27:31 -080067// for ValueBucketInfo
Chenjie Yu32717c32018-10-20 23:54:48 -070068const int FIELD_ID_VALUE_INDEX = 1;
69const int FIELD_ID_VALUE_LONG = 2;
70const int FIELD_ID_VALUE_DOUBLE = 3;
71const int FIELD_ID_VALUES = 9;
Yangster-mac9def8e32018-04-17 13:55:51 -070072const int FIELD_ID_BUCKET_NUM = 4;
73const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
74const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
yro2b0f8862017-11-06 14:27:31 -080075
Chenjie Yuf275f612018-11-30 23:29:06 -080076const Value ZERO_LONG((int64_t)0);
77const Value ZERO_DOUBLE((int64_t)0);
78
Chenjie Yub3dda412017-10-24 13:41:59 -070079// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Chenjie Yuf275f612018-11-30 23:29:06 -080080ValueMetricProducer::ValueMetricProducer(
81 const ConfigKey& key, const ValueMetric& metric, const int conditionIndex,
82 const sp<ConditionWizard>& conditionWizard, const int whatMatcherIndex,
83 const sp<EventMatcherWizard>& matcherWizard, const int pullTagId, const int64_t timeBaseNs,
84 const int64_t startTimeNs, const sp<StatsPullerManager>& pullerManager)
Chenjie Yu054ce9c2018-11-12 15:27:29 -080085 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, conditionWizard),
86 mWhatMatcherIndex(whatMatcherIndex),
87 mEventMatcherWizard(matcherWizard),
Chenjie Yue2219202018-06-08 10:07:51 -070088 mPullerManager(pullerManager),
Chenjie Yuc5875052018-03-09 10:13:11 -080089 mPullTagId(pullTagId),
Chenjie Yua0f02242018-07-06 16:14:34 -070090 mIsPulled(pullTagId != -1),
David Chen81245fd2018-04-12 14:33:37 -070091 mMinBucketSizeNs(metric.min_bucket_size_nanos()),
Chenjie Yuc5875052018-03-09 10:13:11 -080092 mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
93 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
94 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first
95 : StatsdStats::kDimensionKeySizeSoftLimit),
96 mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
97 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
98 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second
Chenjie Yu47234642018-05-14 10:14:16 -070099 : StatsdStats::kDimensionKeySizeHardLimit),
Chenjie Yua0f02242018-07-06 16:14:34 -0700100 mUseAbsoluteValueOnReset(metric.use_absolute_value_on_reset()),
101 mAggregationType(metric.aggregation_type()),
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700102 mUseDiff(metric.has_use_diff() ? metric.use_diff() : (mIsPulled ? true : false)),
103 mValueDirection(metric.value_direction()),
Chenjie Yuf275f612018-11-30 23:29:06 -0800104 mSkipZeroDiffOutput(metric.skip_zero_diff_output()),
105 mUseZeroDefaultBase(metric.use_zero_default_base()),
106 mHasGlobalBase(false) {
Yangster-macb8144812018-01-04 10:56:23 -0800107 int64_t bucketSizeMills = 0;
108 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -0800109 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -0800110 } else {
Yangster-macb8144812018-01-04 10:56:23 -0800111 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -0800112 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700113
Yangster-macb8144812018-01-04 10:56:23 -0800114 mBucketSizeNs = bucketSizeMills * 1000000;
Chenjie Yu32717c32018-10-20 23:54:48 -0700115
116 translateFieldMatcher(metric.value_field(), &mFieldMatchers);
117
Yao Chen8a8d16c2018-02-08 14:50:40 -0800118 if (metric.has_dimensions_in_what()) {
119 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -0800120 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800121 }
122
123 if (metric.has_dimensions_in_condition()) {
124 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
125 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700126
Yao Chen93fe3a32017-11-02 13:52:59 -0700127 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800128 for (const auto& link : metric.links()) {
129 Metric2Condition mc;
130 mc.conditionId = link.condition();
131 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
132 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
133 mMetric2ConditionLinks.push_back(mc);
134 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700135 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800136
Yao Chen8a8d16c2018-02-08 14:50:40 -0800137 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yangster-mac9def8e32018-04-17 13:55:51 -0700138 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700139 HasPositionALL(metric.dimensions_in_condition());
Chenjie Yub3dda412017-10-24 13:41:59 -0700140
Chenjie Yue1361ed2018-07-23 17:33:09 -0700141 flushIfNeededLocked(startTimeNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700142
Chenjie Yua0f02242018-07-06 16:14:34 -0700143 if (mIsPulled) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700144 mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(),
145 mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700146 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700147
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700148 // Only do this for partial buckets like first bucket. All other buckets should use
Chenjie Yue1361ed2018-07-23 17:33:09 -0700149 // flushIfNeeded to adjust start and end to bucket boundaries.
150 // Adjust start for partial bucket
151 mCurrentBucketStartTimeNs = startTimeNs;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700152 // Kicks off the puller immediately if condition is true and diff based.
153 if (mIsPulled && mCondition && mUseDiff) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800154 pullAndMatchEventsLocked(startTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700155 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700156 VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
157 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700158}
159
160ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700161 VLOG("~ValueMetricProducer() called");
Chenjie Yua0f02242018-07-06 16:14:34 -0700162 if (mIsPulled) {
Chenjie Yue2219202018-06-08 10:07:51 -0700163 mPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yu6736c892017-11-09 10:50:09 -0800164 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700165}
166
Yao Chen427d3722018-03-22 15:21:52 -0700167void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700168 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800169 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700170}
171
Yangster-macb142cc82018-03-30 15:22:08 -0700172void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800173 flushIfNeededLocked(dropTimeNs);
174 mPastBuckets.clear();
175}
176
Yangster-maca802d732018-04-24 07:50:38 -0700177void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
178 flushIfNeededLocked(dumpTimeNs);
179 mPastBuckets.clear();
180 mSkippedBuckets.clear();
181}
182
Yangster-macb142cc82018-03-30 15:22:08 -0700183void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700184 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700185 const bool erase_data,
Yangster-mac9def8e32018-04-17 13:55:51 -0700186 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800187 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800188 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700189 if (include_current_partial_bucket) {
190 flushLocked(dumpTimeNs);
191 } else {
192 flushIfNeededLocked(dumpTimeNs);
193 }
Yang Lub4722912018-11-15 11:02:03 -0800194 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800195 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800196
David Chen81245fd2018-04-12 14:33:37 -0700197 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800198 return;
199 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700200 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
201 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
202 // Fills the dimension path if not slicing by ALL.
203 if (!mSliceByPositionALL) {
204 if (!mDimensionsInWhat.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700205 uint64_t dimenPathToken =
206 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700207 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
208 protoOutput->end(dimenPathToken);
209 }
210 if (!mDimensionsInCondition.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700211 uint64_t dimenPathToken =
212 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
Yangster-mac9def8e32018-04-17 13:55:51 -0700213 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
214 protoOutput->end(dimenPathToken);
215 }
216 }
217
Yi Jin5ee07872018-03-05 18:18:27 -0800218 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000219
David Chen81245fd2018-04-12 14:33:37 -0700220 for (const auto& pair : mSkippedBuckets) {
221 uint64_t wrapperToken =
222 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED);
Yangster-mac9def8e32018-04-17 13:55:51 -0700223 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS,
224 (long long)(NanoToMillis(pair.first)));
225 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS,
226 (long long)(NanoToMillis(pair.second)));
David Chen81245fd2018-04-12 14:33:37 -0700227 protoOutput->end(wrapperToken);
228 }
David Chen81245fd2018-04-12 14:33:37 -0700229
Yao Chen93fe3a32017-11-02 13:52:59 -0700230 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800231 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800232 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800233 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800234 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700235
Yangster-mac20877162017-12-22 17:19:39 -0800236 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700237 if (mSliceByPositionALL) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700238 uint64_t dimensionToken =
239 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700240 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
241 protoOutput->end(dimensionToken);
242 if (dimensionKey.hasDimensionKeyInCondition()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700243 uint64_t dimensionInConditionToken =
244 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
245 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), str_set,
246 protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -0700247 protoOutput->end(dimensionInConditionToken);
248 }
249 } else {
250 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
251 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
252 if (dimensionKey.hasDimensionKeyInCondition()) {
253 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700254 FIELD_ID_DIMENSION_LEAF_IN_CONDITION, str_set,
255 protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -0700256 }
Yangster-mac93694462018-01-22 20:49:31 -0800257 }
yro2b0f8862017-11-06 14:27:31 -0800258
259 // Then fill bucket_info (ValueBucketInfo).
260 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800261 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800262 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700263
264 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
265 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
266 (long long)NanoToMillis(bucket.mBucketStartNs));
267 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
268 (long long)NanoToMillis(bucket.mBucketEndNs));
269 } else {
270 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
271 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
272 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700273 for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) {
274 int index = bucket.valueIndex[i];
275 const Value& value = bucket.values[i];
276 uint64_t valueToken = protoOutput->start(
277 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES);
278 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX,
279 index);
280 if (value.getType() == LONG) {
281 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG,
282 (long long)value.long_value);
283 VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs,
284 (long long)bucket.mBucketEndNs, index, (long long)value.long_value);
285 } else if (value.getType() == DOUBLE) {
286 protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE,
287 value.double_value);
288 VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs,
289 (long long)bucket.mBucketEndNs, index, value.double_value);
290 } else {
291 VLOG("Wrong value type for ValueMetric output: %d", value.getType());
292 }
293 protoOutput->end(valueToken);
Chenjie Yua0f02242018-07-06 16:14:34 -0700294 }
Yao Chen288c6002017-12-12 13:43:18 -0800295 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800296 }
Yao Chen288c6002017-12-12 13:43:18 -0800297 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700298 }
Yao Chen288c6002017-12-12 13:43:18 -0800299 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800300
Yangster-mac94e197c2018-01-02 16:03:03 -0800301 VLOG("metric %lld dump report now...", (long long)mMetricId);
Bookatzff71cad2018-09-20 17:17:49 -0700302 if (erase_data) {
303 mPastBuckets.clear();
304 mSkippedBuckets.clear();
305 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700306}
307
Chenjie Yuf275f612018-11-30 23:29:06 -0800308void ValueMetricProducer::resetBase() {
309 for (auto& slice : mCurrentSlicedBucket) {
310 for (auto& interval : slice.second) {
311 interval.hasBase = false;
312 }
313 }
314 mHasGlobalBase = false;
315}
316
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700317void ValueMetricProducer::onConditionChangedLocked(const bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700318 const int64_t eventTimeNs) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700319 if (eventTimeNs < mCurrentBucketStartTimeNs) {
320 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800321 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800322 return;
323 }
324
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700325 flushIfNeededLocked(eventTimeNs);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800326
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700327 // Pull on condition changes.
328 if (mIsPulled && (mCondition != condition)) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800329 pullAndMatchEventsLocked(eventTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700330 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700331
Chenjie Yuf275f612018-11-30 23:29:06 -0800332 // when condition change from true to false, clear diff base but don't
333 // reset other counters as we may accumulate more value in the bucket.
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700334 if (mUseDiff && mCondition && !condition) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800335 resetBase();
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700336 }
337
338 mCondition = condition;
Chenjie Yue1361ed2018-07-23 17:33:09 -0700339}
340
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800341void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700342 vector<std::shared_ptr<LogEvent>> allData;
343 if (mPullerManager->Pull(mPullTagId, timestampNs, &allData)) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700344 for (const auto& data : allData) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800345 if (mEventMatcherWizard->matchLogEvent(
346 *data, mWhatMatcherIndex) == MatchingState::kMatched) {
347 onMatchedLogEventLocked(mWhatMatcherIndex, *data);
348 }
Chenjie Yue1361ed2018-07-23 17:33:09 -0700349 }
Chenjie Yuf275f612018-11-30 23:29:06 -0800350 mHasGlobalBase = true;
351 } else {
352 // for pulled data, every pull is needed. So we reset the base if any
353 // pull fails.
354 resetBase();
Chenjie Yub3dda412017-10-24 13:41:59 -0700355 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700356}
357
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700358int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) {
359 return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
360}
361
Chenjie Yub3dda412017-10-24 13:41:59 -0700362void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800363 std::lock_guard<std::mutex> lock(mMutex);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700364 if (mCondition) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700365 if (allData.size() == 0) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800366 VLOG("Data pulled is empty");
Chenjie Yub3dda412017-10-24 13:41:59 -0700367 return;
368 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800369 // For scheduled pulled data, the effective event time is snap to the nearest
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700370 // bucket end. In the case of waking up from a deep sleep state, we will
371 // attribute to the previous bucket end. If the sleep was long but not very long, we
372 // will be in the immediate next bucket. Previous bucket may get a larger number as
373 // we pull at a later time than real bucket end.
374 // If the sleep was very long, we skip more than one bucket before sleep. In this case,
375 // if the diff base will be cleared and this new data will serve as new diff base.
Yangster-macb142cc82018-03-30 15:22:08 -0700376 int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs();
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700377 int64_t bucketEndTime = calcPreviousBucketEndTime(realEventTime) - 1;
378 if (bucketEndTime < mCurrentBucketStartTimeNs) {
379 VLOG("Skip bucket end pull due to late arrival: %lld vs %lld", (long long)bucketEndTime,
380 (long long)mCurrentBucketStartTimeNs);
381 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700382 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800383 for (const auto& data : allData) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800384 if (mEventMatcherWizard->matchLogEvent(*data, mWhatMatcherIndex) ==
385 MatchingState::kMatched) {
386 data->setElapsedTimestampNs(bucketEndTime);
387 onMatchedLogEventLocked(mWhatMatcherIndex, *data);
388 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800389 }
Chenjie Yuf275f612018-11-30 23:29:06 -0800390 mHasGlobalBase = true;
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800391 } else {
392 VLOG("No need to commit data on condition false.");
Chenjie Yub3dda412017-10-24 13:41:59 -0700393 }
394}
395
Yangster-maca78d0082018-03-12 12:02:56 -0700396void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
397 if (mCurrentSlicedBucket.size() == 0) {
398 return;
399 }
400
401 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
402 (unsigned long)mCurrentSlicedBucket.size());
403 if (verbose) {
404 for (const auto& it : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700405 for (const auto& interval : it.second) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700406 fprintf(out, "\t(what)%s\t(condition)%s (value)%s\n",
407 it.first.getDimensionKeyInWhat().toString().c_str(),
408 it.first.getDimensionKeyInCondition().toString().c_str(),
Chenjie Yu32717c32018-10-20 23:54:48 -0700409 interval.value.toString().c_str());
410 }
Yangster-maca78d0082018-03-12 12:02:56 -0700411 }
412 }
413}
414
Yangster-mac93694462018-01-22 20:49:31 -0800415bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800416 // ===========GuardRail==============
417 // 1. Report the tuple count if the tuple count > soft limit
418 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
419 return false;
420 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800421 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800422 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800423 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800424 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Chenjie Yuc5875052018-03-09 10:13:11 -0800425 if (newTupleCount > mDimensionHardLimit) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700426 ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId,
427 newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800428 return true;
429 }
430 }
431
432 return false;
433}
434
Chenjie Yudbe5c502018-11-30 23:15:57 -0800435bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) {
436 // ===========GuardRail==============
437 // 1. Report the tuple count if the tuple count > soft limit
438 if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) {
439 return false;
440 }
441 if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) {
442 size_t newTupleCount = mCurrentFullBucket.size() + 1;
443 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
444 if (newTupleCount > mDimensionHardLimit) {
445 ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s",
446 (long long)mMetricId,
447 newKey.toString().c_str());
448 return true;
449 }
450 }
451
452 return false;
453}
454
Chenjie Yu32717c32018-10-20 23:54:48 -0700455bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) {
456 for (const FieldValue& value : event.getValues()) {
457 if (value.mField.matches(matcher)) {
458 switch (value.mValue.type) {
459 case INT:
460 ret.setLong(value.mValue.int_value);
461 break;
462 case LONG:
463 ret.setLong(value.mValue.long_value);
464 break;
465 case FLOAT:
466 ret.setDouble(value.mValue.float_value);
467 break;
468 case DOUBLE:
469 ret.setDouble(value.mValue.double_value);
470 break;
471 default:
472 break;
473 }
474 return true;
475 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700476 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700477 return false;
Chenjie Yua0f02242018-07-06 16:14:34 -0700478}
479
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700480void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex,
481 const MetricDimensionKey& eventKey,
482 const ConditionKey& conditionKey,
483 bool condition, const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700484 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000485 if (eventTimeNs < mCurrentBucketStartTimeNs) {
486 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
487 (long long)mCurrentBucketStartTimeNs);
488 return;
489 }
490
Chenjie Yua7259ab2017-12-10 08:31:05 -0800491 flushIfNeededLocked(eventTimeNs);
492
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700493 // For pulled data, we already check condition when we decide to pull or
494 // in onDataPulled. So take all of them.
495 // For pushed data, just check condition.
496 if (!(mIsPulled || condition)) {
497 VLOG("ValueMetric skip event because condition is false");
498 return;
499 }
500
Yangsterf2bee6f2017-11-29 12:01:05 -0800501 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800502 return;
503 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700504 vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey];
505 if (multiIntervals.size() < mFieldMatchers.size()) {
506 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
507 multiIntervals.resize(mFieldMatchers.size());
Yangster-maca7fb12d2018-01-03 17:17:20 -0800508 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000509
Chenjie Yu32717c32018-10-20 23:54:48 -0700510 for (int i = 0; i < (int)mFieldMatchers.size(); i++) {
511 const Matcher& matcher = mFieldMatchers[i];
512 Interval& interval = multiIntervals[i];
513 interval.valueIndex = i;
514 Value value;
515 if (!getDoubleOrLong(event, matcher, value)) {
516 VLOG("Failed to get value %d from event %s", i, event.ToString().c_str());
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700517 return;
518 }
Chenjie Yudbe5c502018-11-30 23:15:57 -0800519 interval.seenNewData = true;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700520
Chenjie Yu32717c32018-10-20 23:54:48 -0700521 if (mUseDiff) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700522 if (!interval.hasBase) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800523 if (mHasGlobalBase && mUseZeroDefaultBase) {
524 // The bucket has global base. This key does not.
525 // Optionally use zero as base.
526 interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE);
527 interval.hasBase = true;
528 } else {
529 // no base. just update base and return.
530 interval.base = value;
531 interval.hasBase = true;
532 return;
533 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700534 }
535 Value diff;
536 switch (mValueDirection) {
537 case ValueMetric::INCREASING:
538 if (value >= interval.base) {
539 diff = value - interval.base;
540 } else if (mUseAbsoluteValueOnReset) {
541 diff = value;
542 } else {
543 VLOG("Unexpected decreasing value");
544 StatsdStats::getInstance().notePullDataError(mPullTagId);
545 interval.base = value;
546 return;
547 }
548 break;
549 case ValueMetric::DECREASING:
550 if (interval.base >= value) {
551 diff = interval.base - value;
552 } else if (mUseAbsoluteValueOnReset) {
553 diff = value;
554 } else {
555 VLOG("Unexpected increasing value");
556 StatsdStats::getInstance().notePullDataError(mPullTagId);
557 interval.base = value;
558 return;
559 }
560 break;
561 case ValueMetric::ANY:
562 diff = value - interval.base;
563 break;
564 default:
565 break;
566 }
567 interval.base = value;
568 value = diff;
Yao Chen6a8c7992017-11-29 20:02:07 +0000569 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700570
571 if (interval.hasValue) {
572 switch (mAggregationType) {
573 case ValueMetric::SUM:
574 // for AVG, we add up and take average when flushing the bucket
575 case ValueMetric::AVG:
576 interval.value += value;
577 break;
578 case ValueMetric::MIN:
579 interval.value = std::min(value, interval.value);
580 break;
581 case ValueMetric::MAX:
582 interval.value = std::max(value, interval.value);
583 break;
584 default:
585 break;
586 }
587 } else {
588 interval.value = value;
589 interval.hasValue = true;
590 }
591 interval.sampleSize += 1;
Yangster8de69392017-11-27 13:48:29 -0800592 }
Bookatzde1b55622017-12-14 18:38:27 -0800593
Chenjie Yua0f02242018-07-06 16:14:34 -0700594 // TODO: propgate proper values down stream when anomaly support doubles
Chenjie Yu32717c32018-10-20 23:54:48 -0700595 long wholeBucketVal = multiIntervals[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800596 auto prev = mCurrentFullBucket.find(eventKey);
597 if (prev != mCurrentFullBucket.end()) {
598 wholeBucketVal += prev->second;
599 }
Bookatzde1b55622017-12-14 18:38:27 -0800600 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800601 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal);
Bookatzde1b55622017-12-14 18:38:27 -0800602 }
Yangster8de69392017-11-27 13:48:29 -0800603}
604
Yangster-macb142cc82018-03-30 15:22:08 -0700605void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
606 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800607
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700608 if (eventTimeNs < currentBucketEndTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700609 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800610 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700611 return;
612 }
David Chen27785a82018-01-19 17:06:45 -0800613
614 flushCurrentBucketLocked(eventTimeNs);
615
616 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
617 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
618 mCurrentBucketNum += numBucketsForward;
619
620 if (numBucketsForward > 1) {
621 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700622 // take base again in future good bucket.
Chenjie Yuf275f612018-11-30 23:29:06 -0800623 resetBase();
David Chen27785a82018-01-19 17:06:45 -0800624 }
625 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
626 (long long)mCurrentBucketStartTimeNs);
627}
628
Yangster-macb142cc82018-03-30 15:22:08 -0700629void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700630 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
631 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700632 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800633
Chenjie Yu32717c32018-10-20 23:54:48 -0700634 int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs;
Chenjie Yub3dda412017-10-24 13:41:59 -0700635
Chenjie Yu32717c32018-10-20 23:54:48 -0700636 if (bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) {
David Chen81245fd2018-04-12 14:33:37 -0700637 // The current bucket is large enough to keep.
David Chen81245fd2018-04-12 14:33:37 -0700638 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700639 ValueBucket bucket;
640 bucket.mBucketStartNs = mCurrentBucketStartTimeNs;
641 bucket.mBucketEndNs = bucketEndTime;
642 for (const auto& interval : slice.second) {
643 if (interval.hasValue) {
644 // skip the output if the diff is zero
645 if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) {
646 continue;
647 }
648 bucket.valueIndex.push_back(interval.valueIndex);
649 if (mAggregationType != ValueMetric::AVG) {
650 bucket.values.push_back(interval.value);
651 } else {
652 double sum = interval.value.type == LONG ? (double)interval.value.long_value
653 : interval.value.double_value;
654 bucket.values.push_back(Value((double)sum / interval.sampleSize));
655 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700656 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700657 }
658 // it will auto create new vector of ValuebucketInfo if the key is not found.
659 if (bucket.valueIndex.size() > 0) {
David Chen81245fd2018-04-12 14:33:37 -0700660 auto& bucketList = mPastBuckets[slice.first];
Chenjie Yu32717c32018-10-20 23:54:48 -0700661 bucketList.push_back(bucket);
David Chen81245fd2018-04-12 14:33:37 -0700662 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700663 }
David Chen81245fd2018-04-12 14:33:37 -0700664 } else {
Chenjie Yu32717c32018-10-20 23:54:48 -0700665 mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700666 }
667
David Chen27785a82018-01-19 17:06:45 -0800668 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
669 // Accumulate partial buckets with current value and then send to anomaly tracker.
670 if (mCurrentFullBucket.size() > 0) {
671 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yudbe5c502018-11-30 23:15:57 -0800672 if (hitFullBucketGuardRailLocked(slice.first)) {
673 continue;
674 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700675 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700676 mCurrentFullBucket[slice.first] += slice.second[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800677 }
678 for (const auto& slice : mCurrentFullBucket) {
679 for (auto& tracker : mAnomalyTrackers) {
680 if (tracker != nullptr) {
681 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
682 }
683 }
684 }
685 mCurrentFullBucket.clear();
686 } else {
687 // Skip aggregating the partial buckets since there's no previous partial bucket.
688 for (const auto& slice : mCurrentSlicedBucket) {
689 for (auto& tracker : mAnomalyTrackers) {
690 if (tracker != nullptr) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700691 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700692 tracker->addPastBucket(slice.first, slice.second[0].value.long_value,
Chenjie Yua0f02242018-07-06 16:14:34 -0700693 mCurrentBucketNum);
David Chen27785a82018-01-19 17:06:45 -0800694 }
695 }
696 }
697 }
698 } else {
699 // Accumulate partial bucket.
700 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700701 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700702 mCurrentFullBucket[slice.first] += slice.second[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800703 }
704 }
705
Chenjie Yudbe5c502018-11-30 23:15:57 -0800706 for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) {
707 bool obsolete = true;
708 for (auto& interval : it->second) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700709 interval.hasValue = false;
710 interval.sampleSize = 0;
Chenjie Yudbe5c502018-11-30 23:15:57 -0800711 if (interval.seenNewData) {
712 obsolete = false;
713 }
714 interval.seenNewData = false;
715 }
716
717 if (obsolete) {
718 it = mCurrentSlicedBucket.erase(it);
719 } else {
720 it++;
Chenjie Yu32717c32018-10-20 23:54:48 -0700721 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700722 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700723}
724
Yangsterf2bee6f2017-11-29 12:01:05 -0800725size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800726 size_t totalSize = 0;
727 for (const auto& pair : mPastBuckets) {
728 totalSize += pair.second.size() * kBucketSize;
729 }
730 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800731}
732
Chenjie Yub3dda412017-10-24 13:41:59 -0700733} // namespace statsd
734} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700735} // namespace android