blob: f03ce4550bc45155cf53293e861d6a0600c2314e [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
Chenjie Yub3dda412017-10-24 13:41:59 -070024#include <limits.h>
25#include <stdlib.h>
26
yrob0378b02017-11-09 20:36:25 -080027using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080028using android::util::FIELD_TYPE_BOOL;
Chenjie Yua0f02242018-07-06 16:14:34 -070029using android::util::FIELD_TYPE_DOUBLE;
yro2b0f8862017-11-06 14:27:31 -080030using android::util::FIELD_TYPE_INT32;
31using android::util::FIELD_TYPE_INT64;
32using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080033using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080034using android::util::ProtoOutputStream;
Yao Chen93fe3a32017-11-02 13:52:59 -070035using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070036using std::shared_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070037using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070038
39namespace android {
40namespace os {
41namespace statsd {
42
yro2b0f8862017-11-06 14:27:31 -080043// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080044const int FIELD_ID_ID = 1;
yro2b0f8862017-11-06 14:27:31 -080045const int FIELD_ID_VALUE_METRICS = 7;
Yangster-mac9def8e32018-04-17 13:55:51 -070046const int FIELD_ID_TIME_BASE = 9;
47const int FIELD_ID_BUCKET_SIZE = 10;
48const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11;
Howard Ro9440e092018-12-16 19:15:21 -080049const int FIELD_ID_IS_ACTIVE = 14;
yro2b0f8862017-11-06 14:27:31 -080050// for ValueMetricDataWrapper
51const int FIELD_ID_DATA = 1;
David Chen81245fd2018-04-12 14:33:37 -070052const int FIELD_ID_SKIPPED = 2;
tsaichristineb87ca152019-12-09 15:19:41 -080053// for SkippedBuckets
Yangster-mac9def8e32018-04-17 13:55:51 -070054const int FIELD_ID_SKIPPED_START_MILLIS = 3;
55const int FIELD_ID_SKIPPED_END_MILLIS = 4;
tsaichristineb87ca152019-12-09 15:19:41 -080056const int FIELD_ID_SKIPPED_DROP_EVENT = 5;
57// for DumpEvent Proto
58const int FIELD_ID_BUCKET_DROP_REASON = 1;
59const int FIELD_ID_DROP_TIME = 2;
yro2b0f8862017-11-06 14:27:31 -080060// for ValueMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080061const int FIELD_ID_DIMENSION_IN_WHAT = 1;
Yangster-mac468ff042018-01-17 12:26:34 -080062const int FIELD_ID_BUCKET_INFO = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070063const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
tsaichristinec876b492019-12-10 13:47:05 -080064const int FIELD_ID_SLICE_BY_STATE = 6;
yro2b0f8862017-11-06 14:27:31 -080065// for ValueBucketInfo
Chenjie Yu32717c32018-10-20 23:54:48 -070066const int FIELD_ID_VALUE_INDEX = 1;
67const int FIELD_ID_VALUE_LONG = 2;
68const int FIELD_ID_VALUE_DOUBLE = 3;
69const int FIELD_ID_VALUES = 9;
Yangster-mac9def8e32018-04-17 13:55:51 -070070const int FIELD_ID_BUCKET_NUM = 4;
71const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
72const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
Yao Chene6cfb142019-04-08 12:00:01 -070073const int FIELD_ID_CONDITION_TRUE_NS = 10;
yro2b0f8862017-11-06 14:27:31 -080074
Chenjie Yuf275f612018-11-30 23:29:06 -080075const Value ZERO_LONG((int64_t)0);
76const Value ZERO_DOUBLE((int64_t)0);
77
Chenjie Yub3dda412017-10-24 13:41:59 -070078// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Chenjie Yuf275f612018-11-30 23:29:06 -080079ValueMetricProducer::ValueMetricProducer(
80 const ConfigKey& key, const ValueMetric& metric, const int conditionIndex,
81 const sp<ConditionWizard>& conditionWizard, const int whatMatcherIndex,
82 const sp<EventMatcherWizard>& matcherWizard, const int pullTagId, const int64_t timeBaseNs,
Ruchir Rastogi21a287b2019-10-02 12:04:33 -070083 const int64_t startTimeNs, const sp<StatsPullerManager>& pullerManager,
84 const unordered_map<int, shared_ptr<Activation>>& eventActivationMap,
tsaichristined21aacf2019-10-07 14:47:38 -070085 const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap,
86 const vector<int>& slicedStateAtoms,
87 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap)
Ruchir Rastogi21a287b2019-10-02 12:04:33 -070088 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, conditionWizard,
tsaichristined21aacf2019-10-07 14:47:38 -070089 eventActivationMap, eventDeactivationMap, slicedStateAtoms, stateGroupMap),
Chenjie Yu054ce9c2018-11-12 15:27:29 -080090 mWhatMatcherIndex(whatMatcherIndex),
91 mEventMatcherWizard(matcherWizard),
Chenjie Yue2219202018-06-08 10:07:51 -070092 mPullerManager(pullerManager),
Chenjie Yuc5875052018-03-09 10:13:11 -080093 mPullTagId(pullTagId),
Chenjie Yua0f02242018-07-06 16:14:34 -070094 mIsPulled(pullTagId != -1),
David Chen81245fd2018-04-12 14:33:37 -070095 mMinBucketSizeNs(metric.min_bucket_size_nanos()),
Chenjie Yuc5875052018-03-09 10:13:11 -080096 mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
97 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
98 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first
99 : StatsdStats::kDimensionKeySizeSoftLimit),
100 mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
101 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
102 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second
Chenjie Yu47234642018-05-14 10:14:16 -0700103 : StatsdStats::kDimensionKeySizeHardLimit),
Chenjie Yua0f02242018-07-06 16:14:34 -0700104 mUseAbsoluteValueOnReset(metric.use_absolute_value_on_reset()),
105 mAggregationType(metric.aggregation_type()),
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700106 mUseDiff(metric.has_use_diff() ? metric.use_diff() : (mIsPulled ? true : false)),
107 mValueDirection(metric.value_direction()),
Chenjie Yuf275f612018-11-30 23:29:06 -0800108 mSkipZeroDiffOutput(metric.skip_zero_diff_output()),
109 mUseZeroDefaultBase(metric.use_zero_default_base()),
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800110 mHasGlobalBase(false),
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000111 mCurrentBucketIsInvalid(false),
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800112 mMaxPullDelayNs(metric.max_pull_delay_sec() > 0 ? metric.max_pull_delay_sec() * NS_PER_SEC
Chenjie Yucd1b7972019-01-16 20:38:15 -0800113 : StatsdStats::kPullMaxDelayNs),
Yao Chene6cfb142019-04-08 12:00:01 -0700114 mSplitBucketForAppUpgrade(metric.split_bucket_for_app_upgrade()),
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700115 // Condition timer will be set later within the constructor after pulling events
Tej Singhee4495e2019-06-03 18:37:35 -0700116 mConditionTimer(false, timeBaseNs) {
Yangster-macb8144812018-01-04 10:56:23 -0800117 int64_t bucketSizeMills = 0;
118 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -0800119 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -0800120 } else {
Yangster-macb8144812018-01-04 10:56:23 -0800121 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -0800122 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700123
Yangster-macb8144812018-01-04 10:56:23 -0800124 mBucketSizeNs = bucketSizeMills * 1000000;
Chenjie Yu32717c32018-10-20 23:54:48 -0700125
126 translateFieldMatcher(metric.value_field(), &mFieldMatchers);
127
Yao Chen8a8d16c2018-02-08 14:50:40 -0800128 if (metric.has_dimensions_in_what()) {
129 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -0800130 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
tsaichristine90f95bb2019-11-06 17:06:53 -0800131 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800132 }
133
Yao Chen93fe3a32017-11-02 13:52:59 -0700134 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800135 for (const auto& link : metric.links()) {
136 Metric2Condition mc;
137 mc.conditionId = link.condition();
138 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
139 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
140 mMetric2ConditionLinks.push_back(mc);
141 }
tsaichristine76853372019-08-06 17:17:03 -0700142 mConditionSliced = true;
Yao Chen93fe3a32017-11-02 13:52:59 -0700143 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800144
tsaichristinec876b492019-12-10 13:47:05 -0800145 for (const auto& stateLink : metric.state_link()) {
146 Metric2State ms;
147 ms.stateAtomId = stateLink.state_atom_id();
148 translateFieldMatcher(stateLink.fields_in_what(), &ms.metricFields);
149 translateFieldMatcher(stateLink.fields_in_state(), &ms.stateFields);
150 mMetric2StateLinks.push_back(ms);
151 }
152
Tej Singh597c7162019-04-17 16:41:45 -0700153 int64_t numBucketsForward = calcBucketsForwardCount(startTimeNs);
154 mCurrentBucketNum += numBucketsForward;
155
Chenjie Yue1361ed2018-07-23 17:33:09 -0700156 flushIfNeededLocked(startTimeNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700157
Chenjie Yua0f02242018-07-06 16:14:34 -0700158 if (mIsPulled) {
Tej Singh3be093b2020-03-04 20:08:38 -0800159 mPullerManager->RegisterReceiver(mPullTagId, mConfigKey, this, getCurrentBucketEndTimeNs(),
Chenjie Yue1361ed2018-07-23 17:33:09 -0700160 mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700161 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700162
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700163 // Only do this for partial buckets like first bucket. All other buckets should use
Chenjie Yue1361ed2018-07-23 17:33:09 -0700164 // flushIfNeeded to adjust start and end to bucket boundaries.
165 // Adjust start for partial bucket
166 mCurrentBucketStartTimeNs = startTimeNs;
Yao Chene6cfb142019-04-08 12:00:01 -0700167 mConditionTimer.newBucketStart(mCurrentBucketStartTimeNs);
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700168
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700169 // Now that activations are processed, start the condition timer if needed.
170 mConditionTimer.onConditionChanged(mIsActive && mCondition == ConditionState::kTrue,
171 mCurrentBucketStartTimeNs);
172
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700173 VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
174 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700175}
176
177ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700178 VLOG("~ValueMetricProducer() called");
Chenjie Yua0f02242018-07-06 16:14:34 -0700179 if (mIsPulled) {
Tej Singh3be093b2020-03-04 20:08:38 -0800180 mPullerManager->UnRegisterReceiver(mPullTagId, mConfigKey, this);
Chenjie Yu6736c892017-11-09 10:50:09 -0800181 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700182}
183
tsaichristinec876b492019-12-10 13:47:05 -0800184void ValueMetricProducer::onStateChanged(int64_t eventTimeNs, int32_t atomId,
tsaichristine9f951052020-05-13 14:32:37 -0700185 const HashableDimensionKey& primaryKey,
186 const FieldValue& oldState, const FieldValue& newState) {
tsaichristinec876b492019-12-10 13:47:05 -0800187 VLOG("ValueMetric %lld onStateChanged time %lld, State %d, key %s, %d -> %d",
188 (long long)mMetricId, (long long)eventTimeNs, atomId, primaryKey.toString().c_str(),
tsaichristine9f951052020-05-13 14:32:37 -0700189 oldState.mValue.int_value, newState.mValue.int_value);
tsaichristinec876b492019-12-10 13:47:05 -0800190 // If condition is not true, we do not need to pull for this state change.
191 if (mCondition != ConditionState::kTrue) {
192 return;
193 }
tsaichristine9f951052020-05-13 14:32:37 -0700194
195 // If old and new states are in the same StateGroup, then we do not need to
196 // pull for this state change.
197 FieldValue oldStateCopy = oldState;
198 FieldValue newStateCopy = newState;
199 mapStateValue(atomId, &oldStateCopy);
200 mapStateValue(atomId, &newStateCopy);
201 if (oldStateCopy == newStateCopy) {
202 return;
203 }
204
tsaichristinec876b492019-12-10 13:47:05 -0800205 bool isEventLate = eventTimeNs < mCurrentBucketStartTimeNs;
206 if (isEventLate) {
207 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
208 (long long)mCurrentBucketStartTimeNs);
209 invalidateCurrentBucket(eventTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET);
210 return;
211 }
212 mStateChangePrimaryKey.first = atomId;
213 mStateChangePrimaryKey.second = primaryKey;
214 if (mIsPulled) {
215 pullAndMatchEventsLocked(eventTimeNs);
216 }
217 mStateChangePrimaryKey.first = 0;
218 mStateChangePrimaryKey.second = DEFAULT_DIMENSION_KEY;
219 flushIfNeededLocked(eventTimeNs);
220}
221
Yao Chen427d3722018-03-22 15:21:52 -0700222void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700223 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800224 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700225}
226
Yangster-macb142cc82018-03-30 15:22:08 -0700227void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000228 StatsdStats::getInstance().noteBucketDropped(mMetricId);
tsaichristineb7fcf002019-12-06 18:40:47 -0800229
230 // The current partial bucket is not flushed and does not require a pull,
231 // so the data is still valid.
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000232 flushIfNeededLocked(dropTimeNs);
233 clearPastBucketsLocked(dropTimeNs);
Yao Chen06dba5d2018-01-26 13:38:16 -0800234}
235
Yangster-maca802d732018-04-24 07:50:38 -0700236void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
Yangster-maca802d732018-04-24 07:50:38 -0700237 mPastBuckets.clear();
238 mSkippedBuckets.clear();
239}
240
Yangster-macb142cc82018-03-30 15:22:08 -0700241void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700242 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700243 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000244 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700245 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800246 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800247 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700248 if (include_current_partial_bucket) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000249 // For pull metrics, we need to do a pull at bucket boundaries. If we do not do that the
250 // current bucket will have incomplete data and the next will have the wrong snapshot to do
251 // a diff against. If the condition is false, we are fine since the base data is reset and
252 // we are not tracking anything.
253 bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue;
254 if (pullNeeded) {
255 switch (dumpLatency) {
256 case FAST:
tsaichristineb87ca152019-12-09 15:19:41 -0800257 invalidateCurrentBucket(dumpTimeNs, BucketDropReason::DUMP_REPORT_REQUESTED);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000258 break;
259 case NO_TIME_CONSTRAINTS:
tsaichristine78b85702019-12-06 12:20:30 -0800260 pullAndMatchEventsLocked(dumpTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000261 break;
262 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000263 }
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000264 flushCurrentBucketLocked(dumpTimeNs, dumpTimeNs);
Yangster-mace68f3a52018-04-04 00:01:43 -0700265 }
Yang Lub4722912018-11-15 11:02:03 -0800266 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800267 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800268
David Chen81245fd2018-04-12 14:33:37 -0700269 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800270 return;
271 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700272 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
273 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
274 // Fills the dimension path if not slicing by ALL.
275 if (!mSliceByPositionALL) {
276 if (!mDimensionsInWhat.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700277 uint64_t dimenPathToken =
278 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700279 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
280 protoOutput->end(dimenPathToken);
281 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700282 }
283
Yi Jin5ee07872018-03-05 18:18:27 -0800284 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000285
tsaichristineb87ca152019-12-09 15:19:41 -0800286 for (const auto& skippedBucket : mSkippedBuckets) {
David Chen81245fd2018-04-12 14:33:37 -0700287 uint64_t wrapperToken =
288 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED);
Yangster-mac9def8e32018-04-17 13:55:51 -0700289 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS,
tsaichristineb87ca152019-12-09 15:19:41 -0800290 (long long)(NanoToMillis(skippedBucket.bucketStartTimeNs)));
Yangster-mac9def8e32018-04-17 13:55:51 -0700291 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS,
tsaichristineb87ca152019-12-09 15:19:41 -0800292 (long long)(NanoToMillis(skippedBucket.bucketEndTimeNs)));
293 for (const auto& dropEvent : skippedBucket.dropEvents) {
294 uint64_t dropEventToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
295 FIELD_ID_SKIPPED_DROP_EVENT);
296 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_BUCKET_DROP_REASON, dropEvent.reason);
297 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DROP_TIME,
298 (long long)(NanoToMillis(dropEvent.dropTimeNs)));
299 ;
300 protoOutput->end(dropEventToken);
301 }
David Chen81245fd2018-04-12 14:33:37 -0700302 protoOutput->end(wrapperToken);
303 }
David Chen81245fd2018-04-12 14:33:37 -0700304
Yao Chen93fe3a32017-11-02 13:52:59 -0700305 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800306 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800307 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800308 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800309 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700310
Yangster-mac20877162017-12-22 17:19:39 -0800311 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700312 if (mSliceByPositionALL) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700313 uint64_t dimensionToken =
314 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700315 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
316 protoOutput->end(dimensionToken);
Yangster-mac9def8e32018-04-17 13:55:51 -0700317 } else {
318 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
319 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800320 }
yro2b0f8862017-11-06 14:27:31 -0800321
tsaichristinec876b492019-12-10 13:47:05 -0800322 // Then fill slice_by_state.
323 for (auto state : dimensionKey.getStateValuesKey().getValues()) {
324 uint64_t stateToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
325 FIELD_ID_SLICE_BY_STATE);
326 writeStateToProto(state, protoOutput);
327 protoOutput->end(stateToken);
328 }
329
yro2b0f8862017-11-06 14:27:31 -0800330 // Then fill bucket_info (ValueBucketInfo).
331 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800332 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800333 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700334
335 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
336 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
337 (long long)NanoToMillis(bucket.mBucketStartNs));
338 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
339 (long long)NanoToMillis(bucket.mBucketEndNs));
340 } else {
341 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
342 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
343 }
Yao Chene6cfb142019-04-08 12:00:01 -0700344 // only write the condition timer value if the metric has a condition.
345 if (mConditionTrackerIndex >= 0) {
346 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_TRUE_NS,
347 (long long)bucket.mConditionTrueNs);
348 }
tsaichristinec876b492019-12-10 13:47:05 -0800349 for (int i = 0; i < (int)bucket.valueIndex.size(); i++) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700350 int index = bucket.valueIndex[i];
351 const Value& value = bucket.values[i];
352 uint64_t valueToken = protoOutput->start(
353 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES);
354 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX,
355 index);
356 if (value.getType() == LONG) {
357 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG,
358 (long long)value.long_value);
359 VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs,
360 (long long)bucket.mBucketEndNs, index, (long long)value.long_value);
361 } else if (value.getType() == DOUBLE) {
362 protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE,
363 value.double_value);
364 VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs,
365 (long long)bucket.mBucketEndNs, index, value.double_value);
366 } else {
367 VLOG("Wrong value type for ValueMetric output: %d", value.getType());
368 }
369 protoOutput->end(valueToken);
Chenjie Yua0f02242018-07-06 16:14:34 -0700370 }
Yao Chen288c6002017-12-12 13:43:18 -0800371 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800372 }
Yao Chen288c6002017-12-12 13:43:18 -0800373 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700374 }
Yao Chen288c6002017-12-12 13:43:18 -0800375 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800376
Yangster-mac94e197c2018-01-02 16:03:03 -0800377 VLOG("metric %lld dump report now...", (long long)mMetricId);
Bookatzff71cad2018-09-20 17:17:49 -0700378 if (erase_data) {
379 mPastBuckets.clear();
380 mSkippedBuckets.clear();
381 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700382}
383
tsaichristineb87ca152019-12-09 15:19:41 -0800384void ValueMetricProducer::invalidateCurrentBucketWithoutResetBase(const int64_t dropTimeNs,
385 const BucketDropReason reason) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000386 if (!mCurrentBucketIsInvalid) {
tsaichristineb87ca152019-12-09 15:19:41 -0800387 // Only report to StatsdStats once per invalid bucket.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000388 StatsdStats::getInstance().noteInvalidatedBucket(mMetricId);
tsaichristineb87ca152019-12-09 15:19:41 -0800389 }
390
391 if (!maxDropEventsReached()) {
392 mCurrentSkippedBucket.dropEvents.emplace_back(buildDropEvent(dropTimeNs, reason));
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000393 }
394 mCurrentBucketIsInvalid = true;
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000395}
396
tsaichristineb87ca152019-12-09 15:19:41 -0800397void ValueMetricProducer::invalidateCurrentBucket(const int64_t dropTimeNs,
398 const BucketDropReason reason) {
399 invalidateCurrentBucketWithoutResetBase(dropTimeNs, reason);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000400 resetBase();
401}
402
Chenjie Yuf275f612018-11-30 23:29:06 -0800403void ValueMetricProducer::resetBase() {
tsaichristinec876b492019-12-10 13:47:05 -0800404 for (auto& slice : mCurrentBaseInfo) {
405 for (auto& baseInfo : slice.second) {
406 baseInfo.hasBase = false;
407 baseInfo.hasCurrentState = false;
Chenjie Yuf275f612018-11-30 23:29:06 -0800408 }
409 }
410 mHasGlobalBase = false;
411}
412
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700413// Handle active state change. Active state change is treated like a condition change:
414// - drop bucket if active state change event arrives too late
415// - if condition is true, pull data on active state changes
416// - ConditionTimer tracks changes based on AND of condition and active state.
417void ValueMetricProducer::onActiveStateChangedLocked(const int64_t& eventTimeNs) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000418 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
tsaichristine90f95bb2019-11-06 17:06:53 -0800419 if (isEventTooLate) {
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700420 // Drop bucket because event arrived too late, ie. we are missing data for this bucket.
tsaichristineb87ca152019-12-09 15:19:41 -0800421 StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId);
422 invalidateCurrentBucket(eventTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET);
Yao Chen2794da22017-12-13 16:01:55 -0800423 }
424
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700425 // Call parent method once we've verified the validity of current bucket.
426 MetricProducer::onActiveStateChangedLocked(eventTimeNs);
427
428 if (ConditionState::kTrue != mCondition) {
429 return;
430 }
431
432 // Pull on active state changes.
433 if (!isEventTooLate) {
434 if (mIsPulled) {
tsaichristine78b85702019-12-06 12:20:30 -0800435 pullAndMatchEventsLocked(eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700436 }
437 // When active state changes from true to false, clear diff base but don't
438 // reset other counters as we may accumulate more value in the bucket.
439 if (mUseDiff && !mIsActive) {
440 resetBase();
441 }
442 }
443
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700444 flushIfNeededLocked(eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700445
446 // Let condition timer know of new active state.
447 mConditionTimer.onConditionChanged(mIsActive, eventTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700448}
449
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700450void ValueMetricProducer::onConditionChangedLocked(const bool condition,
451 const int64_t eventTimeNs) {
452 ConditionState newCondition = condition ? ConditionState::kTrue : ConditionState::kFalse;
453 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
454
tsaichristine90f95bb2019-11-06 17:06:53 -0800455 // If the config is not active, skip the event.
456 if (!mIsActive) {
457 mCondition = isEventTooLate ? ConditionState::kUnknown : newCondition;
458 return;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700459 }
460
tsaichristine90f95bb2019-11-06 17:06:53 -0800461 // If the event arrived late, mark the bucket as invalid and skip the event.
462 if (isEventTooLate) {
463 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
464 (long long)mCurrentBucketStartTimeNs);
tsaichristineb87ca152019-12-09 15:19:41 -0800465 StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId);
tsaichristine90f95bb2019-11-06 17:06:53 -0800466 StatsdStats::getInstance().noteConditionChangeInNextBucket(mMetricId);
tsaichristineb87ca152019-12-09 15:19:41 -0800467 invalidateCurrentBucket(eventTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET);
tsaichristine90f95bb2019-11-06 17:06:53 -0800468 mCondition = ConditionState::kUnknown;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700469 mConditionTimer.onConditionChanged(mCondition, eventTimeNs);
tsaichristine90f95bb2019-11-06 17:06:53 -0800470 return;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700471 }
tsaichristine90f95bb2019-11-06 17:06:53 -0800472
473 // If the previous condition was unknown, mark the bucket as invalid
474 // because the bucket will contain partial data. For example, the condition
475 // change might happen close to the end of the bucket and we might miss a
476 // lot of data.
477 //
478 // We still want to pull to set the base.
479 if (mCondition == ConditionState::kUnknown) {
tsaichristineb87ca152019-12-09 15:19:41 -0800480 invalidateCurrentBucket(eventTimeNs, BucketDropReason::CONDITION_UNKNOWN);
tsaichristine90f95bb2019-11-06 17:06:53 -0800481 }
482
483 // Pull and match for the following condition change cases:
484 // unknown/false -> true - condition changed
485 // true -> false - condition changed
486 // true -> true - old condition was true so we can flush the bucket at the
487 // end if needed.
488 //
489 // We don’t need to pull for unknown -> false or false -> false.
490 //
491 // onConditionChangedLocked might happen on bucket boundaries if this is
492 // called before #onDataPulled.
493 if (mIsPulled &&
494 (newCondition == ConditionState::kTrue || mCondition == ConditionState::kTrue)) {
tsaichristine78b85702019-12-06 12:20:30 -0800495 pullAndMatchEventsLocked(eventTimeNs);
tsaichristine90f95bb2019-11-06 17:06:53 -0800496 }
497
498 // For metrics that use diff, when condition changes from true to false,
499 // clear diff base but don't reset other counts because we may accumulate
500 // more value in the bucket.
501 if (mUseDiff &&
502 (mCondition == ConditionState::kTrue && newCondition == ConditionState::kFalse)) {
503 resetBase();
504 }
505
506 // Update condition state after pulling.
507 mCondition = newCondition;
508
509 flushIfNeededLocked(eventTimeNs);
510 mConditionTimer.onConditionChanged(mCondition, eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700511}
512
Tej Singh3be093b2020-03-04 20:08:38 -0800513void ValueMetricProducer::prepareFirstBucketLocked() {
514 // Kicks off the puller immediately if condition is true and diff based.
515 if (mIsActive && mIsPulled && mCondition == ConditionState::kTrue && mUseDiff) {
516 pullAndMatchEventsLocked(mCurrentBucketStartTimeNs);
517 }
518}
519
tsaichristine78b85702019-12-06 12:20:30 -0800520void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700521 vector<std::shared_ptr<LogEvent>> allData;
Tej Singh7b975a82020-05-11 11:05:08 -0700522 if (!mPullerManager->Pull(mPullTagId, mConfigKey, timestampNs, &allData)) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000523 ALOGE("Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs);
tsaichristineb87ca152019-12-09 15:19:41 -0800524 invalidateCurrentBucket(timestampNs, BucketDropReason::PULL_FAILED);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800525 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700526 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000527
tsaichristine78b85702019-12-06 12:20:30 -0800528 accumulateEvents(allData, timestampNs, timestampNs);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000529}
530
531int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) {
532 return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
533}
534
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000535// By design, statsd pulls data at bucket boundaries using AlarmManager. These pulls are likely
536// to be delayed. Other events like condition changes or app upgrade which are not based on
537// AlarmManager might have arrived earlier and close the bucket.
Olivier Gaillard11203df2019-02-06 13:18:09 +0000538void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData,
539 bool pullSuccess, int64_t originalPullTimeNs) {
540 std::lock_guard<std::mutex> lock(mMutex);
tsaichristine90f95bb2019-11-06 17:06:53 -0800541 if (mCondition == ConditionState::kTrue) {
542 // If the pull failed, we won't be able to compute a diff.
543 if (!pullSuccess) {
tsaichristineb87ca152019-12-09 15:19:41 -0800544 invalidateCurrentBucket(originalPullTimeNs, BucketDropReason::PULL_FAILED);
tsaichristine90f95bb2019-11-06 17:06:53 -0800545 } else {
546 bool isEventLate = originalPullTimeNs < getCurrentBucketEndTimeNs();
547 if (isEventLate) {
548 // If the event is late, we are in the middle of a bucket. Just
549 // process the data without trying to snap the data to the nearest bucket.
tsaichristine78b85702019-12-06 12:20:30 -0800550 accumulateEvents(allData, originalPullTimeNs, originalPullTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000551 } else {
tsaichristine90f95bb2019-11-06 17:06:53 -0800552 // For scheduled pulled data, the effective event time is snap to the nearest
553 // bucket end. In the case of waking up from a deep sleep state, we will
554 // attribute to the previous bucket end. If the sleep was long but not very
555 // long, we will be in the immediate next bucket. Previous bucket may get a
556 // larger number as we pull at a later time than real bucket end.
557 //
558 // If the sleep was very long, we skip more than one bucket before sleep. In
559 // this case, if the diff base will be cleared and this new data will serve as
560 // new diff base.
561 int64_t bucketEndTime = calcPreviousBucketEndTime(originalPullTimeNs) - 1;
562 StatsdStats::getInstance().noteBucketBoundaryDelayNs(
563 mMetricId, originalPullTimeNs - bucketEndTime);
tsaichristine78b85702019-12-06 12:20:30 -0800564 accumulateEvents(allData, originalPullTimeNs, bucketEndTime);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000565 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000566 }
tsaichristine90f95bb2019-11-06 17:06:53 -0800567 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000568
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000569 // We can probably flush the bucket. Since we used bucketEndTime when calling
570 // #onMatchedLogEventInternalLocked, the current bucket will not have been flushed.
571 flushIfNeededLocked(originalPullTimeNs);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000572}
573
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000574void ValueMetricProducer::accumulateEvents(const std::vector<std::shared_ptr<LogEvent>>& allData,
tsaichristine78b85702019-12-06 12:20:30 -0800575 int64_t originalPullTimeNs, int64_t eventElapsedTimeNs) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000576 bool isEventLate = eventElapsedTimeNs < mCurrentBucketStartTimeNs;
577 if (isEventLate) {
578 VLOG("Skip bucket end pull due to late arrival: %lld vs %lld",
579 (long long)eventElapsedTimeNs, (long long)mCurrentBucketStartTimeNs);
580 StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId);
tsaichristineb87ca152019-12-09 15:19:41 -0800581 invalidateCurrentBucket(eventElapsedTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000582 return;
583 }
584
tsaichristineb87ca152019-12-09 15:19:41 -0800585 const int64_t elapsedRealtimeNs = getElapsedRealtimeNs();
586 const int64_t pullDelayNs = elapsedRealtimeNs - originalPullTimeNs;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000587 StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800588 if (pullDelayNs > mMaxPullDelayNs) {
589 ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId,
590 (long long)mMaxPullDelayNs);
591 StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000592 // We are missing one pull from the bucket which means we will not have a complete view of
593 // what's going on.
tsaichristineb87ca152019-12-09 15:19:41 -0800594 invalidateCurrentBucket(eventElapsedTimeNs, BucketDropReason::PULL_DELAYED);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800595 return;
596 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800597
Olivier Gaillard11203df2019-02-06 13:18:09 +0000598 mMatchedMetricDimensionKeys.clear();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800599 for (const auto& data : allData) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800600 LogEvent localCopy = data->makeCopy();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800601 if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) ==
602 MatchingState::kMatched) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000603 localCopy.setElapsedTimestampNs(eventElapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800604 onMatchedLogEventLocked(mWhatMatcherIndex, localCopy);
605 }
606 }
tsaichristinec876b492019-12-10 13:47:05 -0800607 // If a key that is:
608 // 1. Tracked in mCurrentSlicedBucket and
609 // 2. A superset of the current mStateChangePrimaryKey
610 // was not found in the new pulled data (i.e. not in mMatchedDimensionInWhatKeys)
611 // then we need to reset the base.
Olivier Gaillard11203df2019-02-06 13:18:09 +0000612 for (auto& slice : mCurrentSlicedBucket) {
tsaichristinec876b492019-12-10 13:47:05 -0800613 const auto& whatKey = slice.first.getDimensionKeyInWhat();
614 bool presentInPulledData =
615 mMatchedMetricDimensionKeys.find(whatKey) != mMatchedMetricDimensionKeys.end();
616 if (!presentInPulledData && whatKey.contains(mStateChangePrimaryKey.second)) {
617 auto it = mCurrentBaseInfo.find(whatKey);
618 for (auto& baseInfo : it->second) {
619 baseInfo.hasBase = false;
620 baseInfo.hasCurrentState = false;
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800621 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800622 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700623 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000624 mMatchedMetricDimensionKeys.clear();
625 mHasGlobalBase = true;
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000626
627 // If we reach the guardrail, we might have dropped some data which means the bucket is
628 // incomplete.
629 //
630 // The base also needs to be reset. If we do not have the full data, we might
631 // incorrectly compute the diff when mUseZeroDefaultBase is true since an existing key
632 // might be missing from mCurrentSlicedBucket.
633 if (hasReachedGuardRailLimit()) {
tsaichristineb87ca152019-12-09 15:19:41 -0800634 invalidateCurrentBucket(eventElapsedTimeNs, BucketDropReason::DIMENSION_GUARDRAIL_REACHED);
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000635 mCurrentSlicedBucket.clear();
636 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700637}
638
Yangster-maca78d0082018-03-12 12:02:56 -0700639void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
640 if (mCurrentSlicedBucket.size() == 0) {
641 return;
642 }
643
644 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
645 (unsigned long)mCurrentSlicedBucket.size());
646 if (verbose) {
647 for (const auto& it : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700648 for (const auto& interval : it.second) {
tsaichristine69000e62019-10-18 17:34:52 -0700649 fprintf(out, "\t(what)%s\t(states)%s (value)%s\n",
650 it.first.getDimensionKeyInWhat().toString().c_str(),
651 it.first.getStateValuesKey().toString().c_str(),
652 interval.value.toString().c_str());
Chenjie Yu32717c32018-10-20 23:54:48 -0700653 }
Yangster-maca78d0082018-03-12 12:02:56 -0700654 }
655 }
656}
657
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000658bool ValueMetricProducer::hasReachedGuardRailLimit() const {
659 return mCurrentSlicedBucket.size() >= mDimensionHardLimit;
660}
661
Yangster-mac93694462018-01-22 20:49:31 -0800662bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800663 // ===========GuardRail==============
664 // 1. Report the tuple count if the tuple count > soft limit
665 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
666 return false;
667 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800668 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800669 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800670 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800671 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000672 if (hasReachedGuardRailLimit()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700673 ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId,
674 newKey.toString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000675 StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId);
Yao Chenb3561512017-11-21 18:07:17 -0800676 return true;
677 }
678 }
679
680 return false;
681}
682
Chenjie Yudbe5c502018-11-30 23:15:57 -0800683bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) {
684 // ===========GuardRail==============
685 // 1. Report the tuple count if the tuple count > soft limit
686 if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) {
687 return false;
688 }
689 if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) {
690 size_t newTupleCount = mCurrentFullBucket.size() + 1;
691 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
692 if (newTupleCount > mDimensionHardLimit) {
693 ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s",
694 (long long)mMetricId,
695 newKey.toString().c_str());
696 return true;
697 }
698 }
699
700 return false;
701}
702
Chenjie Yu32717c32018-10-20 23:54:48 -0700703bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) {
704 for (const FieldValue& value : event.getValues()) {
705 if (value.mField.matches(matcher)) {
706 switch (value.mValue.type) {
707 case INT:
708 ret.setLong(value.mValue.int_value);
709 break;
710 case LONG:
711 ret.setLong(value.mValue.long_value);
712 break;
713 case FLOAT:
714 ret.setDouble(value.mValue.float_value);
715 break;
716 case DOUBLE:
717 ret.setDouble(value.mValue.double_value);
718 break;
719 default:
tsaichristine409468d2019-10-28 11:32:31 -0700720 return false;
Chenjie Yu32717c32018-10-20 23:54:48 -0700721 break;
722 }
723 return true;
724 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700725 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700726 return false;
Chenjie Yua0f02242018-07-06 16:14:34 -0700727}
728
tsaichristinec876b492019-12-10 13:47:05 -0800729void ValueMetricProducer::onMatchedLogEventInternalLocked(
730 const size_t matcherIndex, const MetricDimensionKey& eventKey,
731 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
732 const map<int, HashableDimensionKey>& statePrimaryKeys) {
733 auto whatKey = eventKey.getDimensionKeyInWhat();
734 auto stateKey = eventKey.getStateValuesKey();
735
736 // Skip this event if a state changed occurred for a different primary key.
737 auto it = statePrimaryKeys.find(mStateChangePrimaryKey.first);
738 // Check that both the atom id and the primary key are equal.
739 if (it != statePrimaryKeys.end() && it->second != mStateChangePrimaryKey.second) {
740 VLOG("ValueMetric skip event with primary key %s because state change primary key "
741 "is %s",
742 it->second.toString().c_str(), mStateChangePrimaryKey.second.toString().c_str());
743 return;
744 }
745
Yangster-macb142cc82018-03-30 15:22:08 -0700746 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000747 if (eventTimeNs < mCurrentBucketStartTimeNs) {
748 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
749 (long long)mCurrentBucketStartTimeNs);
750 return;
751 }
tsaichristinec876b492019-12-10 13:47:05 -0800752 mMatchedMetricDimensionKeys.insert(whatKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000753
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000754 if (!mIsPulled) {
755 // We cannot flush without doing a pull first.
756 flushIfNeededLocked(eventTimeNs);
757 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800758
Olivier Gaillardfbee9162019-04-11 11:48:01 +0100759 // We should not accumulate the data for pushed metrics when the condition is false.
760 bool shouldSkipForPushMetric = !mIsPulled && !condition;
761 // For pulled metrics, there are two cases:
762 // - to compute diffs, we need to process all the state changes
763 // - for non-diffs metrics, we should ignore the data if the condition wasn't true. If we have a
764 // state change from
765 // + True -> True: we should process the data, it might be a bucket boundary
766 // + True -> False: we als need to process the data.
767 bool shouldSkipForPulledMetric = mIsPulled && !mUseDiff
768 && mCondition != ConditionState::kTrue;
769 if (shouldSkipForPushMetric || shouldSkipForPulledMetric) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700770 VLOG("ValueMetric skip event because condition is false");
771 return;
772 }
773
Yangsterf2bee6f2017-11-29 12:01:05 -0800774 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800775 return;
776 }
tsaichristinec876b492019-12-10 13:47:05 -0800777 vector<BaseInfo>& baseInfos = mCurrentBaseInfo[whatKey];
778 if (baseInfos.size() < mFieldMatchers.size()) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700779 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
tsaichristinec876b492019-12-10 13:47:05 -0800780 baseInfos.resize(mFieldMatchers.size());
781 }
782
783 for (auto baseInfo : baseInfos) {
784 if (!baseInfo.hasCurrentState) {
785 baseInfo.currentState = DEFAULT_DIMENSION_KEY;
786 baseInfo.hasCurrentState = true;
787 }
788 }
789
790 // We need to get the intervals stored with the previous state key so we can
791 // close these value intervals.
792 const auto oldStateKey = baseInfos[0].currentState;
793 vector<Interval>& intervals = mCurrentSlicedBucket[MetricDimensionKey(whatKey, oldStateKey)];
794 if (intervals.size() < mFieldMatchers.size()) {
795 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
796 intervals.resize(mFieldMatchers.size());
Yangster-maca7fb12d2018-01-03 17:17:20 -0800797 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000798
Misha Wagner26531762019-01-21 14:18:51 +0000799 // We only use anomaly detection under certain cases.
800 // N.B.: The anomaly detection cases were modified in order to fix an issue with value metrics
801 // containing multiple values. We tried to retain all previous behaviour, but we are unsure the
802 // previous behaviour was correct. At the time of the fix, anomaly detection had no owner.
803 // Whoever next works on it should look into the cases where it is triggered in this function.
804 // Discussion here: http://ag/6124370.
805 bool useAnomalyDetection = true;
806
Chenjie Yu32717c32018-10-20 23:54:48 -0700807 for (int i = 0; i < (int)mFieldMatchers.size(); i++) {
808 const Matcher& matcher = mFieldMatchers[i];
tsaichristinec876b492019-12-10 13:47:05 -0800809 BaseInfo& baseInfo = baseInfos[i];
810 Interval& interval = intervals[i];
Chenjie Yu32717c32018-10-20 23:54:48 -0700811 interval.valueIndex = i;
812 Value value;
813 if (!getDoubleOrLong(event, matcher, value)) {
814 VLOG("Failed to get value %d from event %s", i, event.ToString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000815 StatsdStats::getInstance().noteBadValueType(mMetricId);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700816 return;
817 }
Chenjie Yudbe5c502018-11-30 23:15:57 -0800818 interval.seenNewData = true;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700819
Chenjie Yu32717c32018-10-20 23:54:48 -0700820 if (mUseDiff) {
tsaichristinec876b492019-12-10 13:47:05 -0800821 if (!baseInfo.hasBase) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800822 if (mHasGlobalBase && mUseZeroDefaultBase) {
823 // The bucket has global base. This key does not.
824 // Optionally use zero as base.
tsaichristinec876b492019-12-10 13:47:05 -0800825 baseInfo.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE);
826 baseInfo.hasBase = true;
Chenjie Yuf275f612018-11-30 23:29:06 -0800827 } else {
828 // no base. just update base and return.
tsaichristinec876b492019-12-10 13:47:05 -0800829 baseInfo.base = value;
830 baseInfo.hasBase = true;
Misha Wagner26531762019-01-21 14:18:51 +0000831 // If we're missing a base, do not use anomaly detection on incomplete data
832 useAnomalyDetection = false;
tsaichristinec876b492019-12-10 13:47:05 -0800833 // Continue (instead of return) here in order to set baseInfo.base and
834 // baseInfo.hasBase for other baseInfos
Misha Wagner26531762019-01-21 14:18:51 +0000835 continue;
Chenjie Yuf275f612018-11-30 23:29:06 -0800836 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700837 }
tsaichristinec876b492019-12-10 13:47:05 -0800838
Chenjie Yu32717c32018-10-20 23:54:48 -0700839 Value diff;
840 switch (mValueDirection) {
841 case ValueMetric::INCREASING:
tsaichristinec876b492019-12-10 13:47:05 -0800842 if (value >= baseInfo.base) {
843 diff = value - baseInfo.base;
Chenjie Yu32717c32018-10-20 23:54:48 -0700844 } else if (mUseAbsoluteValueOnReset) {
845 diff = value;
846 } else {
847 VLOG("Unexpected decreasing value");
848 StatsdStats::getInstance().notePullDataError(mPullTagId);
tsaichristinec876b492019-12-10 13:47:05 -0800849 baseInfo.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000850 // If we've got bad data, do not use anomaly detection
851 useAnomalyDetection = false;
852 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700853 }
854 break;
855 case ValueMetric::DECREASING:
tsaichristinec876b492019-12-10 13:47:05 -0800856 if (baseInfo.base >= value) {
857 diff = baseInfo.base - value;
Chenjie Yu32717c32018-10-20 23:54:48 -0700858 } else if (mUseAbsoluteValueOnReset) {
859 diff = value;
860 } else {
861 VLOG("Unexpected increasing value");
862 StatsdStats::getInstance().notePullDataError(mPullTagId);
tsaichristinec876b492019-12-10 13:47:05 -0800863 baseInfo.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000864 // If we've got bad data, do not use anomaly detection
865 useAnomalyDetection = false;
866 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700867 }
868 break;
869 case ValueMetric::ANY:
tsaichristinec876b492019-12-10 13:47:05 -0800870 diff = value - baseInfo.base;
Chenjie Yu32717c32018-10-20 23:54:48 -0700871 break;
872 default:
873 break;
874 }
tsaichristinec876b492019-12-10 13:47:05 -0800875 baseInfo.base = value;
Chenjie Yu32717c32018-10-20 23:54:48 -0700876 value = diff;
Yao Chen6a8c7992017-11-29 20:02:07 +0000877 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700878
879 if (interval.hasValue) {
880 switch (mAggregationType) {
881 case ValueMetric::SUM:
882 // for AVG, we add up and take average when flushing the bucket
883 case ValueMetric::AVG:
884 interval.value += value;
885 break;
886 case ValueMetric::MIN:
887 interval.value = std::min(value, interval.value);
888 break;
889 case ValueMetric::MAX:
890 interval.value = std::max(value, interval.value);
891 break;
892 default:
893 break;
894 }
895 } else {
896 interval.value = value;
897 interval.hasValue = true;
898 }
899 interval.sampleSize += 1;
tsaichristinec876b492019-12-10 13:47:05 -0800900 baseInfo.currentState = stateKey;
Yangster8de69392017-11-27 13:48:29 -0800901 }
Bookatzde1b55622017-12-14 18:38:27 -0800902
Misha Wagner26531762019-01-21 14:18:51 +0000903 // Only trigger the tracker if all intervals are correct
904 if (useAnomalyDetection) {
905 // TODO: propgate proper values down stream when anomaly support doubles
tsaichristinec876b492019-12-10 13:47:05 -0800906 long wholeBucketVal = intervals[0].value.long_value;
Misha Wagner26531762019-01-21 14:18:51 +0000907 auto prev = mCurrentFullBucket.find(eventKey);
908 if (prev != mCurrentFullBucket.end()) {
909 wholeBucketVal += prev->second;
910 }
911 for (auto& tracker : mAnomalyTrackers) {
Yao Chen4ce07292019-02-13 13:06:36 -0800912 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey,
913 wholeBucketVal);
Misha Wagner26531762019-01-21 14:18:51 +0000914 }
Bookatzde1b55622017-12-14 18:38:27 -0800915 }
Yangster8de69392017-11-27 13:48:29 -0800916}
917
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000918// For pulled metrics, we always need to make sure we do a pull before flushing the bucket
919// if mCondition is true!
Yangster-macb142cc82018-03-30 15:22:08 -0700920void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
921 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700922 if (eventTimeNs < currentBucketEndTimeNs) {
tsaichristine90f95bb2019-11-06 17:06:53 -0800923 VLOG("eventTime is %lld, less than current bucket end time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800924 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700925 return;
926 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000927 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000928 int64_t nextBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
929 flushCurrentBucketLocked(eventTimeNs, nextBucketStartTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000930}
David Chen27785a82018-01-19 17:06:45 -0800931
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000932int64_t ValueMetricProducer::calcBucketsForwardCount(const int64_t& eventTimeNs) const {
933 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
934 if (eventTimeNs < currentBucketEndTimeNs) {
935 return 0;
David Chen27785a82018-01-19 17:06:45 -0800936 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000937 return 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800938}
939
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000940void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs,
941 const int64_t& nextBucketStartTimeNs) {
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000942 if (mCondition == ConditionState::kUnknown) {
943 StatsdStats::getInstance().noteBucketUnknownCondition(mMetricId);
944 }
945
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000946 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000947 if (numBucketsForward > 1) {
948 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
949 StatsdStats::getInstance().noteSkippedForwardBuckets(mMetricId);
950 // Something went wrong. Maybe the device was sleeping for a long time. It is better
951 // to mark the current bucket as invalid. The last pull might have been successful through.
tsaichristineb87ca152019-12-09 15:19:41 -0800952 invalidateCurrentBucketWithoutResetBase(eventTimeNs,
953 BucketDropReason::MULTIPLE_BUCKETS_SKIPPED);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000954 }
955
Chenjie Yub3dda412017-10-24 13:41:59 -0700956 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
957 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700958 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
Chenjie Yu32717c32018-10-20 23:54:48 -0700959 int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs;
Yao Chene6cfb142019-04-08 12:00:01 -0700960 // Close the current bucket.
961 int64_t conditionTrueDuration = mConditionTimer.newBucketStart(bucketEndTime);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000962 bool isBucketLargeEnough = bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs;
tsaichristineb87ca152019-12-09 15:19:41 -0800963 if (!isBucketLargeEnough) {
tsaichristineb87ca152019-12-09 15:19:41 -0800964 if (!maxDropEventsReached()) {
965 mCurrentSkippedBucket.dropEvents.emplace_back(
966 buildDropEvent(eventTimeNs, BucketDropReason::BUCKET_TOO_SMALL));
967 }
968 }
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000969 if (isBucketLargeEnough && !mCurrentBucketIsInvalid) {
David Chen81245fd2018-04-12 14:33:37 -0700970 // The current bucket is large enough to keep.
David Chen81245fd2018-04-12 14:33:37 -0700971 for (const auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000972 ValueBucket bucket = buildPartialBucket(bucketEndTime, slice.second);
Yao Chene6cfb142019-04-08 12:00:01 -0700973 bucket.mConditionTrueNs = conditionTrueDuration;
Chenjie Yu32717c32018-10-20 23:54:48 -0700974 // it will auto create new vector of ValuebucketInfo if the key is not found.
975 if (bucket.valueIndex.size() > 0) {
David Chen81245fd2018-04-12 14:33:37 -0700976 auto& bucketList = mPastBuckets[slice.first];
Chenjie Yu32717c32018-10-20 23:54:48 -0700977 bucketList.push_back(bucket);
David Chen81245fd2018-04-12 14:33:37 -0700978 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700979 }
David Chen81245fd2018-04-12 14:33:37 -0700980 } else {
tsaichristine45f703b2020-02-03 10:44:39 -0800981 mCurrentSkippedBucket.bucketStartTimeNs = mCurrentBucketStartTimeNs;
982 mCurrentSkippedBucket.bucketEndTimeNs = bucketEndTime;
tsaichristineb87ca152019-12-09 15:19:41 -0800983 mSkippedBuckets.emplace_back(mCurrentSkippedBucket);
Chenjie Yub3dda412017-10-24 13:41:59 -0700984 }
985
Olivier Gaillardc3719912019-03-15 17:33:40 +0000986 appendToFullBucket(eventTimeNs, fullBucketEndTimeNs);
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000987 initCurrentSlicedBucket(nextBucketStartTimeNs);
Yao Chene6cfb142019-04-08 12:00:01 -0700988 // Update the condition timer again, in case we skipped buckets.
989 mConditionTimer.newBucketStart(nextBucketStartTimeNs);
Olivier Gaillardc3719912019-03-15 17:33:40 +0000990 mCurrentBucketNum += numBucketsForward;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000991}
992
993ValueBucket ValueMetricProducer::buildPartialBucket(int64_t bucketEndTime,
994 const std::vector<Interval>& intervals) {
995 ValueBucket bucket;
996 bucket.mBucketStartNs = mCurrentBucketStartTimeNs;
997 bucket.mBucketEndNs = bucketEndTime;
998 for (const auto& interval : intervals) {
999 if (interval.hasValue) {
1000 // skip the output if the diff is zero
1001 if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) {
1002 continue;
1003 }
1004 bucket.valueIndex.push_back(interval.valueIndex);
1005 if (mAggregationType != ValueMetric::AVG) {
1006 bucket.values.push_back(interval.value);
1007 } else {
1008 double sum = interval.value.type == LONG ? (double)interval.value.long_value
1009 : interval.value.double_value;
1010 bucket.values.push_back(Value((double)sum / interval.sampleSize));
1011 }
1012 }
1013 }
1014 return bucket;
1015}
1016
Olivier Gaillarda8b70112019-02-25 11:24:23 +00001017void ValueMetricProducer::initCurrentSlicedBucket(int64_t nextBucketStartTimeNs) {
1018 StatsdStats::getInstance().noteBucketCount(mMetricId);
1019 // Cleanup data structure to aggregate values.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +00001020 for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) {
1021 bool obsolete = true;
1022 for (auto& interval : it->second) {
1023 interval.hasValue = false;
1024 interval.sampleSize = 0;
1025 if (interval.seenNewData) {
1026 obsolete = false;
1027 }
1028 interval.seenNewData = false;
1029 }
1030
1031 if (obsolete) {
1032 it = mCurrentSlicedBucket.erase(it);
1033 } else {
1034 it++;
1035 }
tsaichristinec876b492019-12-10 13:47:05 -08001036 // TODO: remove mCurrentBaseInfo entries when obsolete
Olivier Gaillard9a5d3592019-02-05 15:12:39 +00001037 }
Olivier Gaillarda8b70112019-02-25 11:24:23 +00001038
1039 mCurrentBucketIsInvalid = false;
tsaichristineb87ca152019-12-09 15:19:41 -08001040 mCurrentSkippedBucket.reset();
1041
Olivier Gaillarda8b70112019-02-25 11:24:23 +00001042 // If we do not have a global base when the condition is true,
1043 // we will have incomplete bucket for the next bucket.
1044 if (mUseDiff && !mHasGlobalBase && mCondition) {
1045 mCurrentBucketIsInvalid = false;
1046 }
1047 mCurrentBucketStartTimeNs = nextBucketStartTimeNs;
1048 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
1049 (long long)mCurrentBucketStartTimeNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +00001050}
1051
1052void ValueMetricProducer::appendToFullBucket(int64_t eventTimeNs, int64_t fullBucketEndTimeNs) {
Olivier Gaillardc3719912019-03-15 17:33:40 +00001053 bool isFullBucketReached = eventTimeNs > fullBucketEndTimeNs;
1054 if (mCurrentBucketIsInvalid) {
1055 if (isFullBucketReached) {
1056 // If the bucket is invalid, we ignore the full bucket since it contains invalid data.
1057 mCurrentFullBucket.clear();
1058 }
1059 // Current bucket is invalid, we do not add it to the full bucket.
1060 return;
1061 }
1062
1063 if (isFullBucketReached) { // If full bucket, send to anomaly tracker.
David Chen27785a82018-01-19 17:06:45 -08001064 // Accumulate partial buckets with current value and then send to anomaly tracker.
1065 if (mCurrentFullBucket.size() > 0) {
1066 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yudbe5c502018-11-30 23:15:57 -08001067 if (hitFullBucketGuardRailLocked(slice.first)) {
1068 continue;
1069 }
Chenjie Yua0f02242018-07-06 16:14:34 -07001070 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +00001071 auto& interval = slice.second[0];
1072 if (interval.hasValue) {
1073 mCurrentFullBucket[slice.first] += interval.value.long_value;
1074 }
David Chen27785a82018-01-19 17:06:45 -08001075 }
1076 for (const auto& slice : mCurrentFullBucket) {
1077 for (auto& tracker : mAnomalyTrackers) {
1078 if (tracker != nullptr) {
1079 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
1080 }
1081 }
1082 }
1083 mCurrentFullBucket.clear();
1084 } else {
1085 // Skip aggregating the partial buckets since there's no previous partial bucket.
1086 for (const auto& slice : mCurrentSlicedBucket) {
1087 for (auto& tracker : mAnomalyTrackers) {
1088 if (tracker != nullptr) {
Chenjie Yua0f02242018-07-06 16:14:34 -07001089 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +00001090 auto& interval = slice.second[0];
1091 if (interval.hasValue) {
1092 tracker->addPastBucket(slice.first, interval.value.long_value,
1093 mCurrentBucketNum);
1094 }
David Chen27785a82018-01-19 17:06:45 -08001095 }
1096 }
1097 }
1098 }
1099 } else {
1100 // Accumulate partial bucket.
1101 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yua0f02242018-07-06 16:14:34 -07001102 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +00001103 auto& interval = slice.second[0];
1104 if (interval.hasValue) {
1105 mCurrentFullBucket[slice.first] += interval.value.long_value;
1106 }
David Chen27785a82018-01-19 17:06:45 -08001107 }
1108 }
Chenjie Yub3dda412017-10-24 13:41:59 -07001109}
1110
Yangsterf2bee6f2017-11-29 12:01:05 -08001111size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -08001112 size_t totalSize = 0;
1113 for (const auto& pair : mPastBuckets) {
1114 totalSize += pair.second.size() * kBucketSize;
1115 }
1116 return totalSize;
yro2b0f8862017-11-06 14:27:31 -08001117}
1118
Chenjie Yub3dda412017-10-24 13:41:59 -07001119} // namespace statsd
1120} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -07001121} // namespace android