blob: 9a8e3bd7b3eb1afbc29c29063e7ba67d701ccad3 [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()),
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800106 mHasGlobalBase(false),
107 mMaxPullDelayNs(metric.max_pull_delay_sec() > 0 ? metric.max_pull_delay_sec() * NS_PER_SEC
108 : StatsdStats::kPullMaxDelayNs) {
Yangster-macb8144812018-01-04 10:56:23 -0800109 int64_t bucketSizeMills = 0;
110 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -0800111 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -0800112 } else {
Yangster-macb8144812018-01-04 10:56:23 -0800113 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -0800114 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700115
Yangster-macb8144812018-01-04 10:56:23 -0800116 mBucketSizeNs = bucketSizeMills * 1000000;
Chenjie Yu32717c32018-10-20 23:54:48 -0700117
118 translateFieldMatcher(metric.value_field(), &mFieldMatchers);
119
Yao Chen8a8d16c2018-02-08 14:50:40 -0800120 if (metric.has_dimensions_in_what()) {
121 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -0800122 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800123 }
124
125 if (metric.has_dimensions_in_condition()) {
126 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
127 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700128
Yao Chen93fe3a32017-11-02 13:52:59 -0700129 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800130 for (const auto& link : metric.links()) {
131 Metric2Condition mc;
132 mc.conditionId = link.condition();
133 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
134 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
135 mMetric2ConditionLinks.push_back(mc);
136 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700137 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800138
Yao Chen8a8d16c2018-02-08 14:50:40 -0800139 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yangster-mac9def8e32018-04-17 13:55:51 -0700140 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700141 HasPositionALL(metric.dimensions_in_condition());
Chenjie Yub3dda412017-10-24 13:41:59 -0700142
Chenjie Yue1361ed2018-07-23 17:33:09 -0700143 flushIfNeededLocked(startTimeNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700144
Chenjie Yua0f02242018-07-06 16:14:34 -0700145 if (mIsPulled) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700146 mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(),
147 mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700148 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700149
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700150 // Only do this for partial buckets like first bucket. All other buckets should use
Chenjie Yue1361ed2018-07-23 17:33:09 -0700151 // flushIfNeeded to adjust start and end to bucket boundaries.
152 // Adjust start for partial bucket
153 mCurrentBucketStartTimeNs = startTimeNs;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700154 // Kicks off the puller immediately if condition is true and diff based.
155 if (mIsPulled && mCondition && mUseDiff) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800156 pullAndMatchEventsLocked(startTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700157 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700158 VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
159 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700160}
161
162ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700163 VLOG("~ValueMetricProducer() called");
Chenjie Yua0f02242018-07-06 16:14:34 -0700164 if (mIsPulled) {
Chenjie Yue2219202018-06-08 10:07:51 -0700165 mPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yu6736c892017-11-09 10:50:09 -0800166 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700167}
168
Yao Chen427d3722018-03-22 15:21:52 -0700169void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700170 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800171 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700172}
173
Yangster-macb142cc82018-03-30 15:22:08 -0700174void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800175 flushIfNeededLocked(dropTimeNs);
176 mPastBuckets.clear();
177}
178
Yangster-maca802d732018-04-24 07:50:38 -0700179void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
180 flushIfNeededLocked(dumpTimeNs);
181 mPastBuckets.clear();
182 mSkippedBuckets.clear();
183}
184
Yangster-macb142cc82018-03-30 15:22:08 -0700185void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700186 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700187 const bool erase_data,
Yangster-mac9def8e32018-04-17 13:55:51 -0700188 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800189 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800190 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700191 if (include_current_partial_bucket) {
192 flushLocked(dumpTimeNs);
193 } else {
194 flushIfNeededLocked(dumpTimeNs);
195 }
Yang Lub4722912018-11-15 11:02:03 -0800196 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800197 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800198
David Chen81245fd2018-04-12 14:33:37 -0700199 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800200 return;
201 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700202 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
203 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
204 // Fills the dimension path if not slicing by ALL.
205 if (!mSliceByPositionALL) {
206 if (!mDimensionsInWhat.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700207 uint64_t dimenPathToken =
208 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700209 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
210 protoOutput->end(dimenPathToken);
211 }
212 if (!mDimensionsInCondition.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700213 uint64_t dimenPathToken =
214 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
Yangster-mac9def8e32018-04-17 13:55:51 -0700215 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
216 protoOutput->end(dimenPathToken);
217 }
218 }
219
Yi Jin5ee07872018-03-05 18:18:27 -0800220 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000221
David Chen81245fd2018-04-12 14:33:37 -0700222 for (const auto& pair : mSkippedBuckets) {
223 uint64_t wrapperToken =
224 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED);
Yangster-mac9def8e32018-04-17 13:55:51 -0700225 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS,
226 (long long)(NanoToMillis(pair.first)));
227 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS,
228 (long long)(NanoToMillis(pair.second)));
David Chen81245fd2018-04-12 14:33:37 -0700229 protoOutput->end(wrapperToken);
230 }
David Chen81245fd2018-04-12 14:33:37 -0700231
Yao Chen93fe3a32017-11-02 13:52:59 -0700232 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800233 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800234 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800235 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800236 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700237
Yangster-mac20877162017-12-22 17:19:39 -0800238 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700239 if (mSliceByPositionALL) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700240 uint64_t dimensionToken =
241 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700242 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
243 protoOutput->end(dimensionToken);
244 if (dimensionKey.hasDimensionKeyInCondition()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700245 uint64_t dimensionInConditionToken =
246 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
247 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), str_set,
248 protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -0700249 protoOutput->end(dimensionInConditionToken);
250 }
251 } else {
252 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
253 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
254 if (dimensionKey.hasDimensionKeyInCondition()) {
255 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700256 FIELD_ID_DIMENSION_LEAF_IN_CONDITION, str_set,
257 protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -0700258 }
Yangster-mac93694462018-01-22 20:49:31 -0800259 }
yro2b0f8862017-11-06 14:27:31 -0800260
261 // Then fill bucket_info (ValueBucketInfo).
262 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800263 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800264 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700265
266 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
267 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
268 (long long)NanoToMillis(bucket.mBucketStartNs));
269 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
270 (long long)NanoToMillis(bucket.mBucketEndNs));
271 } else {
272 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
273 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
274 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700275 for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) {
276 int index = bucket.valueIndex[i];
277 const Value& value = bucket.values[i];
278 uint64_t valueToken = protoOutput->start(
279 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES);
280 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX,
281 index);
282 if (value.getType() == LONG) {
283 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG,
284 (long long)value.long_value);
285 VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs,
286 (long long)bucket.mBucketEndNs, index, (long long)value.long_value);
287 } else if (value.getType() == DOUBLE) {
288 protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE,
289 value.double_value);
290 VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs,
291 (long long)bucket.mBucketEndNs, index, value.double_value);
292 } else {
293 VLOG("Wrong value type for ValueMetric output: %d", value.getType());
294 }
295 protoOutput->end(valueToken);
Chenjie Yua0f02242018-07-06 16:14:34 -0700296 }
Yao Chen288c6002017-12-12 13:43:18 -0800297 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800298 }
Yao Chen288c6002017-12-12 13:43:18 -0800299 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700300 }
Yao Chen288c6002017-12-12 13:43:18 -0800301 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800302
Yangster-mac94e197c2018-01-02 16:03:03 -0800303 VLOG("metric %lld dump report now...", (long long)mMetricId);
Bookatzff71cad2018-09-20 17:17:49 -0700304 if (erase_data) {
305 mPastBuckets.clear();
306 mSkippedBuckets.clear();
307 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700308}
309
Chenjie Yuf275f612018-11-30 23:29:06 -0800310void ValueMetricProducer::resetBase() {
311 for (auto& slice : mCurrentSlicedBucket) {
312 for (auto& interval : slice.second) {
313 interval.hasBase = false;
314 }
315 }
316 mHasGlobalBase = false;
317}
318
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700319void ValueMetricProducer::onConditionChangedLocked(const bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700320 const int64_t eventTimeNs) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700321 if (eventTimeNs < mCurrentBucketStartTimeNs) {
322 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800323 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800324 return;
325 }
326
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700327 flushIfNeededLocked(eventTimeNs);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800328
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700329 // Pull on condition changes.
330 if (mIsPulled && (mCondition != condition)) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800331 pullAndMatchEventsLocked(eventTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700332 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700333
Chenjie Yuf275f612018-11-30 23:29:06 -0800334 // when condition change from true to false, clear diff base but don't
335 // reset other counters as we may accumulate more value in the bucket.
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700336 if (mUseDiff && mCondition && !condition) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800337 resetBase();
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700338 }
339
340 mCondition = condition;
Chenjie Yue1361ed2018-07-23 17:33:09 -0700341}
342
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800343void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700344 vector<std::shared_ptr<LogEvent>> allData;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800345 if (!mPullerManager->Pull(mPullTagId, &allData)) {
346 ALOGE("Gauge Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs);
Chenjie Yuf275f612018-11-30 23:29:06 -0800347 resetBase();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800348 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700349 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800350 const int64_t pullDelayNs = getElapsedRealtimeNs() - timestampNs;
351 if (pullDelayNs > mMaxPullDelayNs) {
352 ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId,
353 (long long)mMaxPullDelayNs);
354 StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId);
355 StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs);
356 resetBase();
357 return;
358 }
359 StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs);
360
361 for (const auto& data : allData) {
362 // make a copy before doing and changes
363 LogEvent localCopy = data->makeCopy();
364 localCopy.setElapsedTimestampNs(timestampNs);
365 if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) ==
366 MatchingState::kMatched) {
367 onMatchedLogEventLocked(mWhatMatcherIndex, localCopy);
368 }
369 }
370 mHasGlobalBase = true;
Chenjie Yub3dda412017-10-24 13:41:59 -0700371}
372
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700373int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) {
374 return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
375}
376
Chenjie Yub3dda412017-10-24 13:41:59 -0700377void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800378 std::lock_guard<std::mutex> lock(mMutex);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700379 if (mCondition) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700380 if (allData.size() == 0) {
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800381 VLOG("Data pulled is empty");
Chenjie Yub3dda412017-10-24 13:41:59 -0700382 return;
383 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800384 // For scheduled pulled data, the effective event time is snap to the nearest
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700385 // bucket end. In the case of waking up from a deep sleep state, we will
386 // attribute to the previous bucket end. If the sleep was long but not very long, we
387 // will be in the immediate next bucket. Previous bucket may get a larger number as
388 // we pull at a later time than real bucket end.
389 // If the sleep was very long, we skip more than one bucket before sleep. In this case,
390 // if the diff base will be cleared and this new data will serve as new diff base.
Yangster-macb142cc82018-03-30 15:22:08 -0700391 int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs();
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700392 int64_t bucketEndTime = calcPreviousBucketEndTime(realEventTime) - 1;
393 if (bucketEndTime < mCurrentBucketStartTimeNs) {
394 VLOG("Skip bucket end pull due to late arrival: %lld vs %lld", (long long)bucketEndTime,
395 (long long)mCurrentBucketStartTimeNs);
396 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700397 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800398 for (const auto& data : allData) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800399 LogEvent localCopy = data->makeCopy();
400 if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) ==
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800401 MatchingState::kMatched) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800402 localCopy.setElapsedTimestampNs(bucketEndTime);
403 onMatchedLogEventLocked(mWhatMatcherIndex, localCopy);
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800404 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800405 }
Chenjie Yuf275f612018-11-30 23:29:06 -0800406 mHasGlobalBase = true;
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800407 } else {
408 VLOG("No need to commit data on condition false.");
Chenjie Yub3dda412017-10-24 13:41:59 -0700409 }
410}
411
Yangster-maca78d0082018-03-12 12:02:56 -0700412void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
413 if (mCurrentSlicedBucket.size() == 0) {
414 return;
415 }
416
417 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
418 (unsigned long)mCurrentSlicedBucket.size());
419 if (verbose) {
420 for (const auto& it : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700421 for (const auto& interval : it.second) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700422 fprintf(out, "\t(what)%s\t(condition)%s (value)%s\n",
423 it.first.getDimensionKeyInWhat().toString().c_str(),
424 it.first.getDimensionKeyInCondition().toString().c_str(),
Chenjie Yu32717c32018-10-20 23:54:48 -0700425 interval.value.toString().c_str());
426 }
Yangster-maca78d0082018-03-12 12:02:56 -0700427 }
428 }
429}
430
Yangster-mac93694462018-01-22 20:49:31 -0800431bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800432 // ===========GuardRail==============
433 // 1. Report the tuple count if the tuple count > soft limit
434 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
435 return false;
436 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800437 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800438 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800439 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800440 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Chenjie Yuc5875052018-03-09 10:13:11 -0800441 if (newTupleCount > mDimensionHardLimit) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700442 ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId,
443 newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800444 return true;
445 }
446 }
447
448 return false;
449}
450
Chenjie Yudbe5c502018-11-30 23:15:57 -0800451bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) {
452 // ===========GuardRail==============
453 // 1. Report the tuple count if the tuple count > soft limit
454 if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) {
455 return false;
456 }
457 if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) {
458 size_t newTupleCount = mCurrentFullBucket.size() + 1;
459 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
460 if (newTupleCount > mDimensionHardLimit) {
461 ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s",
462 (long long)mMetricId,
463 newKey.toString().c_str());
464 return true;
465 }
466 }
467
468 return false;
469}
470
Chenjie Yu32717c32018-10-20 23:54:48 -0700471bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) {
472 for (const FieldValue& value : event.getValues()) {
473 if (value.mField.matches(matcher)) {
474 switch (value.mValue.type) {
475 case INT:
476 ret.setLong(value.mValue.int_value);
477 break;
478 case LONG:
479 ret.setLong(value.mValue.long_value);
480 break;
481 case FLOAT:
482 ret.setDouble(value.mValue.float_value);
483 break;
484 case DOUBLE:
485 ret.setDouble(value.mValue.double_value);
486 break;
487 default:
488 break;
489 }
490 return true;
491 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700492 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700493 return false;
Chenjie Yua0f02242018-07-06 16:14:34 -0700494}
495
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700496void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex,
497 const MetricDimensionKey& eventKey,
498 const ConditionKey& conditionKey,
499 bool condition, const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700500 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000501 if (eventTimeNs < mCurrentBucketStartTimeNs) {
502 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
503 (long long)mCurrentBucketStartTimeNs);
504 return;
505 }
506
Chenjie Yua7259ab2017-12-10 08:31:05 -0800507 flushIfNeededLocked(eventTimeNs);
508
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700509 // For pulled data, we already check condition when we decide to pull or
510 // in onDataPulled. So take all of them.
511 // For pushed data, just check condition.
512 if (!(mIsPulled || condition)) {
513 VLOG("ValueMetric skip event because condition is false");
514 return;
515 }
516
Yangsterf2bee6f2017-11-29 12:01:05 -0800517 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800518 return;
519 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700520 vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey];
521 if (multiIntervals.size() < mFieldMatchers.size()) {
522 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
523 multiIntervals.resize(mFieldMatchers.size());
Yangster-maca7fb12d2018-01-03 17:17:20 -0800524 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000525
Chenjie Yu32717c32018-10-20 23:54:48 -0700526 for (int i = 0; i < (int)mFieldMatchers.size(); i++) {
527 const Matcher& matcher = mFieldMatchers[i];
528 Interval& interval = multiIntervals[i];
529 interval.valueIndex = i;
530 Value value;
531 if (!getDoubleOrLong(event, matcher, value)) {
532 VLOG("Failed to get value %d from event %s", i, event.ToString().c_str());
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700533 return;
534 }
Chenjie Yudbe5c502018-11-30 23:15:57 -0800535 interval.seenNewData = true;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700536
Chenjie Yu32717c32018-10-20 23:54:48 -0700537 if (mUseDiff) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700538 if (!interval.hasBase) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800539 if (mHasGlobalBase && mUseZeroDefaultBase) {
540 // The bucket has global base. This key does not.
541 // Optionally use zero as base.
542 interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE);
543 interval.hasBase = true;
544 } else {
545 // no base. just update base and return.
546 interval.base = value;
547 interval.hasBase = true;
548 return;
549 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700550 }
551 Value diff;
552 switch (mValueDirection) {
553 case ValueMetric::INCREASING:
554 if (value >= interval.base) {
555 diff = value - interval.base;
556 } else if (mUseAbsoluteValueOnReset) {
557 diff = value;
558 } else {
559 VLOG("Unexpected decreasing value");
560 StatsdStats::getInstance().notePullDataError(mPullTagId);
561 interval.base = value;
562 return;
563 }
564 break;
565 case ValueMetric::DECREASING:
566 if (interval.base >= value) {
567 diff = interval.base - value;
568 } else if (mUseAbsoluteValueOnReset) {
569 diff = value;
570 } else {
571 VLOG("Unexpected increasing value");
572 StatsdStats::getInstance().notePullDataError(mPullTagId);
573 interval.base = value;
574 return;
575 }
576 break;
577 case ValueMetric::ANY:
578 diff = value - interval.base;
579 break;
580 default:
581 break;
582 }
583 interval.base = value;
584 value = diff;
Yao Chen6a8c7992017-11-29 20:02:07 +0000585 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700586
587 if (interval.hasValue) {
588 switch (mAggregationType) {
589 case ValueMetric::SUM:
590 // for AVG, we add up and take average when flushing the bucket
591 case ValueMetric::AVG:
592 interval.value += value;
593 break;
594 case ValueMetric::MIN:
595 interval.value = std::min(value, interval.value);
596 break;
597 case ValueMetric::MAX:
598 interval.value = std::max(value, interval.value);
599 break;
600 default:
601 break;
602 }
603 } else {
604 interval.value = value;
605 interval.hasValue = true;
606 }
607 interval.sampleSize += 1;
Yangster8de69392017-11-27 13:48:29 -0800608 }
Bookatzde1b55622017-12-14 18:38:27 -0800609
Chenjie Yua0f02242018-07-06 16:14:34 -0700610 // TODO: propgate proper values down stream when anomaly support doubles
Chenjie Yu32717c32018-10-20 23:54:48 -0700611 long wholeBucketVal = multiIntervals[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800612 auto prev = mCurrentFullBucket.find(eventKey);
613 if (prev != mCurrentFullBucket.end()) {
614 wholeBucketVal += prev->second;
615 }
Bookatzde1b55622017-12-14 18:38:27 -0800616 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800617 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal);
Bookatzde1b55622017-12-14 18:38:27 -0800618 }
Yangster8de69392017-11-27 13:48:29 -0800619}
620
Yangster-macb142cc82018-03-30 15:22:08 -0700621void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
622 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800623
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700624 if (eventTimeNs < currentBucketEndTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700625 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800626 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700627 return;
628 }
David Chen27785a82018-01-19 17:06:45 -0800629
630 flushCurrentBucketLocked(eventTimeNs);
631
632 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
633 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
634 mCurrentBucketNum += numBucketsForward;
635
636 if (numBucketsForward > 1) {
637 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700638 // take base again in future good bucket.
Chenjie Yuf275f612018-11-30 23:29:06 -0800639 resetBase();
David Chen27785a82018-01-19 17:06:45 -0800640 }
641 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
642 (long long)mCurrentBucketStartTimeNs);
643}
644
Yangster-macb142cc82018-03-30 15:22:08 -0700645void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700646 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
647 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700648 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800649
Chenjie Yu32717c32018-10-20 23:54:48 -0700650 int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs;
Chenjie Yub3dda412017-10-24 13:41:59 -0700651
Chenjie Yu32717c32018-10-20 23:54:48 -0700652 if (bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) {
David Chen81245fd2018-04-12 14:33:37 -0700653 // The current bucket is large enough to keep.
David Chen81245fd2018-04-12 14:33:37 -0700654 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700655 ValueBucket bucket;
656 bucket.mBucketStartNs = mCurrentBucketStartTimeNs;
657 bucket.mBucketEndNs = bucketEndTime;
658 for (const auto& interval : slice.second) {
659 if (interval.hasValue) {
660 // skip the output if the diff is zero
661 if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) {
662 continue;
663 }
664 bucket.valueIndex.push_back(interval.valueIndex);
665 if (mAggregationType != ValueMetric::AVG) {
666 bucket.values.push_back(interval.value);
667 } else {
668 double sum = interval.value.type == LONG ? (double)interval.value.long_value
669 : interval.value.double_value;
670 bucket.values.push_back(Value((double)sum / interval.sampleSize));
671 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700672 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700673 }
674 // it will auto create new vector of ValuebucketInfo if the key is not found.
675 if (bucket.valueIndex.size() > 0) {
David Chen81245fd2018-04-12 14:33:37 -0700676 auto& bucketList = mPastBuckets[slice.first];
Chenjie Yu32717c32018-10-20 23:54:48 -0700677 bucketList.push_back(bucket);
David Chen81245fd2018-04-12 14:33:37 -0700678 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700679 }
David Chen81245fd2018-04-12 14:33:37 -0700680 } else {
Chenjie Yu32717c32018-10-20 23:54:48 -0700681 mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700682 }
683
David Chen27785a82018-01-19 17:06:45 -0800684 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
685 // Accumulate partial buckets with current value and then send to anomaly tracker.
686 if (mCurrentFullBucket.size() > 0) {
687 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yudbe5c502018-11-30 23:15:57 -0800688 if (hitFullBucketGuardRailLocked(slice.first)) {
689 continue;
690 }
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 mCurrentFullBucket[slice.first] += slice.second[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800693 }
694 for (const auto& slice : mCurrentFullBucket) {
695 for (auto& tracker : mAnomalyTrackers) {
696 if (tracker != nullptr) {
697 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
698 }
699 }
700 }
701 mCurrentFullBucket.clear();
702 } else {
703 // Skip aggregating the partial buckets since there's no previous partial bucket.
704 for (const auto& slice : mCurrentSlicedBucket) {
705 for (auto& tracker : mAnomalyTrackers) {
706 if (tracker != nullptr) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700707 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700708 tracker->addPastBucket(slice.first, slice.second[0].value.long_value,
Chenjie Yua0f02242018-07-06 16:14:34 -0700709 mCurrentBucketNum);
David Chen27785a82018-01-19 17:06:45 -0800710 }
711 }
712 }
713 }
714 } else {
715 // Accumulate partial bucket.
716 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700717 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700718 mCurrentFullBucket[slice.first] += slice.second[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800719 }
720 }
721
Chenjie Yudbe5c502018-11-30 23:15:57 -0800722 for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) {
723 bool obsolete = true;
724 for (auto& interval : it->second) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700725 interval.hasValue = false;
726 interval.sampleSize = 0;
Chenjie Yudbe5c502018-11-30 23:15:57 -0800727 if (interval.seenNewData) {
728 obsolete = false;
729 }
730 interval.seenNewData = false;
731 }
732
733 if (obsolete) {
734 it = mCurrentSlicedBucket.erase(it);
735 } else {
736 it++;
Chenjie Yu32717c32018-10-20 23:54:48 -0700737 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700738 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700739}
740
Yangsterf2bee6f2017-11-29 12:01:05 -0800741size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800742 size_t totalSize = 0;
743 for (const auto& pair : mPastBuckets) {
744 totalSize += pair.second.size() * kBucketSize;
745 }
746 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800747}
748
Chenjie Yub3dda412017-10-24 13:41:59 -0700749} // namespace statsd
750} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700751} // namespace android