blob: 27ee57013fdaa2158e717ab90d4431286b07a023 [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),
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000107 mCurrentBucketIsInvalid(false),
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800108 mMaxPullDelayNs(metric.max_pull_delay_sec() > 0 ? metric.max_pull_delay_sec() * NS_PER_SEC
Chenjie Yucd1b7972019-01-16 20:38:15 -0800109 : StatsdStats::kPullMaxDelayNs),
110 mSplitBucketForAppUpgrade(metric.split_bucket_for_app_upgrade()) {
Yangster-macb8144812018-01-04 10:56:23 -0800111 int64_t bucketSizeMills = 0;
112 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -0800113 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -0800114 } else {
Yangster-macb8144812018-01-04 10:56:23 -0800115 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -0800116 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700117
Yangster-macb8144812018-01-04 10:56:23 -0800118 mBucketSizeNs = bucketSizeMills * 1000000;
Chenjie Yu32717c32018-10-20 23:54:48 -0700119
120 translateFieldMatcher(metric.value_field(), &mFieldMatchers);
121
Yao Chen8a8d16c2018-02-08 14:50:40 -0800122 if (metric.has_dimensions_in_what()) {
123 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -0800124 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800125 }
126
127 if (metric.has_dimensions_in_condition()) {
128 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
129 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700130
Yao Chen93fe3a32017-11-02 13:52:59 -0700131 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800132 for (const auto& link : metric.links()) {
133 Metric2Condition mc;
134 mc.conditionId = link.condition();
135 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
136 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
137 mMetric2ConditionLinks.push_back(mc);
138 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700139 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800140
Yao Chen8a8d16c2018-02-08 14:50:40 -0800141 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yangster-mac9def8e32018-04-17 13:55:51 -0700142 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700143 HasPositionALL(metric.dimensions_in_condition());
Chenjie Yub3dda412017-10-24 13:41:59 -0700144
Chenjie Yue1361ed2018-07-23 17:33:09 -0700145 flushIfNeededLocked(startTimeNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700146
Chenjie Yua0f02242018-07-06 16:14:34 -0700147 if (mIsPulled) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700148 mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(),
149 mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700150 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700151
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700152 // Only do this for partial buckets like first bucket. All other buckets should use
Chenjie Yue1361ed2018-07-23 17:33:09 -0700153 // flushIfNeeded to adjust start and end to bucket boundaries.
154 // Adjust start for partial bucket
155 mCurrentBucketStartTimeNs = startTimeNs;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700156 // Kicks off the puller immediately if condition is true and diff based.
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000157 if (mIsPulled && mCondition == ConditionState::kTrue && mUseDiff) {
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000158 pullAndMatchEventsLocked(startTimeNs, mCondition);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700159 }
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700160 VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
161 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700162}
163
164ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700165 VLOG("~ValueMetricProducer() called");
Chenjie Yua0f02242018-07-06 16:14:34 -0700166 if (mIsPulled) {
Chenjie Yue2219202018-06-08 10:07:51 -0700167 mPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yu6736c892017-11-09 10:50:09 -0800168 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700169}
170
Yao Chen427d3722018-03-22 15:21:52 -0700171void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700172 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800173 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700174}
175
Yangster-macb142cc82018-03-30 15:22:08 -0700176void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000177 StatsdStats::getInstance().noteBucketDropped(mMetricId);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000178 // We are going to flush the data without doing a pull first so we need to invalidte the data.
179 bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue;
180 if (pullNeeded) {
181 invalidateCurrentBucket();
182 }
183 flushIfNeededLocked(dropTimeNs);
184 clearPastBucketsLocked(dropTimeNs);
Yao Chen06dba5d2018-01-26 13:38:16 -0800185}
186
Yangster-maca802d732018-04-24 07:50:38 -0700187void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
Yangster-maca802d732018-04-24 07:50:38 -0700188 mPastBuckets.clear();
189 mSkippedBuckets.clear();
190}
191
Yangster-macb142cc82018-03-30 15:22:08 -0700192void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700193 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700194 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000195 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700196 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800197 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800198 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700199 if (include_current_partial_bucket) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000200 // For pull metrics, we need to do a pull at bucket boundaries. If we do not do that the
201 // current bucket will have incomplete data and the next will have the wrong snapshot to do
202 // a diff against. If the condition is false, we are fine since the base data is reset and
203 // we are not tracking anything.
204 bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue;
205 if (pullNeeded) {
206 switch (dumpLatency) {
207 case FAST:
208 invalidateCurrentBucket();
209 break;
210 case NO_TIME_CONSTRAINTS:
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000211 pullAndMatchEventsLocked(dumpTimeNs, mCondition);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000212 break;
213 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000214 }
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000215 flushCurrentBucketLocked(dumpTimeNs, dumpTimeNs);
Yangster-mace68f3a52018-04-04 00:01:43 -0700216 }
Yang Lub4722912018-11-15 11:02:03 -0800217 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800218 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800219
David Chen81245fd2018-04-12 14:33:37 -0700220 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800221 return;
222 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700223 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
224 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
225 // Fills the dimension path if not slicing by ALL.
226 if (!mSliceByPositionALL) {
227 if (!mDimensionsInWhat.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700228 uint64_t dimenPathToken =
229 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700230 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
231 protoOutput->end(dimenPathToken);
232 }
233 if (!mDimensionsInCondition.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700234 uint64_t dimenPathToken =
235 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
Yangster-mac9def8e32018-04-17 13:55:51 -0700236 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
237 protoOutput->end(dimenPathToken);
238 }
239 }
240
Yi Jin5ee07872018-03-05 18:18:27 -0800241 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000242
David Chen81245fd2018-04-12 14:33:37 -0700243 for (const auto& pair : mSkippedBuckets) {
244 uint64_t wrapperToken =
245 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED);
Yangster-mac9def8e32018-04-17 13:55:51 -0700246 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS,
247 (long long)(NanoToMillis(pair.first)));
248 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS,
249 (long long)(NanoToMillis(pair.second)));
David Chen81245fd2018-04-12 14:33:37 -0700250 protoOutput->end(wrapperToken);
251 }
David Chen81245fd2018-04-12 14:33:37 -0700252
Yao Chen93fe3a32017-11-02 13:52:59 -0700253 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800254 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800255 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800256 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800257 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700258
Yangster-mac20877162017-12-22 17:19:39 -0800259 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700260 if (mSliceByPositionALL) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700261 uint64_t dimensionToken =
262 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700263 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
264 protoOutput->end(dimensionToken);
265 if (dimensionKey.hasDimensionKeyInCondition()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700266 uint64_t dimensionInConditionToken =
267 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
268 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), str_set,
269 protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -0700270 protoOutput->end(dimensionInConditionToken);
271 }
272 } else {
273 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
274 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
275 if (dimensionKey.hasDimensionKeyInCondition()) {
276 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700277 FIELD_ID_DIMENSION_LEAF_IN_CONDITION, str_set,
278 protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -0700279 }
Yangster-mac93694462018-01-22 20:49:31 -0800280 }
yro2b0f8862017-11-06 14:27:31 -0800281
282 // Then fill bucket_info (ValueBucketInfo).
283 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800284 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800285 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700286
287 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
288 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
289 (long long)NanoToMillis(bucket.mBucketStartNs));
290 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
291 (long long)NanoToMillis(bucket.mBucketEndNs));
292 } else {
293 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
294 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
295 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700296 for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) {
297 int index = bucket.valueIndex[i];
298 const Value& value = bucket.values[i];
299 uint64_t valueToken = protoOutput->start(
300 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES);
301 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX,
302 index);
303 if (value.getType() == LONG) {
304 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG,
305 (long long)value.long_value);
306 VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs,
307 (long long)bucket.mBucketEndNs, index, (long long)value.long_value);
308 } else if (value.getType() == DOUBLE) {
309 protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE,
310 value.double_value);
311 VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs,
312 (long long)bucket.mBucketEndNs, index, value.double_value);
313 } else {
314 VLOG("Wrong value type for ValueMetric output: %d", value.getType());
315 }
316 protoOutput->end(valueToken);
Chenjie Yua0f02242018-07-06 16:14:34 -0700317 }
Yao Chen288c6002017-12-12 13:43:18 -0800318 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800319 }
Yao Chen288c6002017-12-12 13:43:18 -0800320 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700321 }
Yao Chen288c6002017-12-12 13:43:18 -0800322 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800323
Yangster-mac94e197c2018-01-02 16:03:03 -0800324 VLOG("metric %lld dump report now...", (long long)mMetricId);
Bookatzff71cad2018-09-20 17:17:49 -0700325 if (erase_data) {
326 mPastBuckets.clear();
327 mSkippedBuckets.clear();
328 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700329}
330
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000331void ValueMetricProducer::invalidateCurrentBucketWithoutResetBase() {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000332 if (!mCurrentBucketIsInvalid) {
333 // Only report once per invalid bucket.
334 StatsdStats::getInstance().noteInvalidatedBucket(mMetricId);
335 }
336 mCurrentBucketIsInvalid = true;
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000337}
338
339void ValueMetricProducer::invalidateCurrentBucket() {
340 invalidateCurrentBucketWithoutResetBase();
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000341 resetBase();
342}
343
Chenjie Yuf275f612018-11-30 23:29:06 -0800344void ValueMetricProducer::resetBase() {
345 for (auto& slice : mCurrentSlicedBucket) {
346 for (auto& interval : slice.second) {
347 interval.hasBase = false;
348 }
349 }
350 mHasGlobalBase = false;
351}
352
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700353void ValueMetricProducer::onConditionChangedLocked(const bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700354 const int64_t eventTimeNs) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000355 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
356 if (!isEventTooLate) {
357 if (mCondition == ConditionState::kUnknown) {
358 // If the condition was unknown, we mark the bucket as invalid since the bucket will
359 // contain partial data. For instance, the condition change might happen close to the
360 // end of the bucket and we might miss lots of data.
361 //
362 // We still want to pull to set the base.
363 invalidateCurrentBucket();
364 }
365
366 // Pull on condition changes.
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000367 ConditionState newCondition = condition ? ConditionState::kTrue : ConditionState::kFalse;
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000368 bool conditionChanged =
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000369 (mCondition == ConditionState::kTrue && newCondition == ConditionState::kFalse)
370 || (mCondition == ConditionState::kFalse && newCondition == ConditionState::kTrue);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000371 // We do not need to pull when we go from unknown to false.
372 //
373 // We also pull if the condition was already true in order to be able to flush the bucket at
374 // the end if needed.
375 //
376 // onConditionChangedLocked might happen on bucket boundaries if this is called before
377 // #onDataPulled.
378 if (mIsPulled && (conditionChanged || condition)) {
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000379 pullAndMatchEventsLocked(eventTimeNs, newCondition);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000380 }
381
382 // When condition change from true to false, clear diff base but don't
383 // reset other counters as we may accumulate more value in the bucket.
384 if (mUseDiff && mCondition == ConditionState::kTrue
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000385 && newCondition == ConditionState::kFalse) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000386 resetBase();
387 }
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000388 mCondition = newCondition;
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000389
390 } else {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700391 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800392 (long long)mCurrentBucketStartTimeNs);
Misha Wagner1eee2212019-01-22 11:47:11 +0000393 StatsdStats::getInstance().noteConditionChangeInNextBucket(mMetricId);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000394 invalidateCurrentBucket();
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000395 // Something weird happened. If we received another event if the future, the condition might
396 // be wrong.
Olivier Gaillarde35b2822019-02-27 17:09:40 +0000397 mCondition = initialCondition(mConditionTrackerIndex);
Yao Chen2794da22017-12-13 16:01:55 -0800398 }
399
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000400 // This part should alway be called.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700401 flushIfNeededLocked(eventTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700402}
403
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000404void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs, ConditionState condition) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700405 vector<std::shared_ptr<LogEvent>> allData;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800406 if (!mPullerManager->Pull(mPullTagId, &allData)) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000407 ALOGE("Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000408 invalidateCurrentBucket();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800409 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700410 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000411
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000412 accumulateEvents(allData, timestampNs, timestampNs, condition);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000413}
414
415int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) {
416 return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
417}
418
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000419// By design, statsd pulls data at bucket boundaries using AlarmManager. These pulls are likely
420// to be delayed. Other events like condition changes or app upgrade which are not based on
421// AlarmManager might have arrived earlier and close the bucket.
Olivier Gaillard11203df2019-02-06 13:18:09 +0000422void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData,
423 bool pullSuccess, int64_t originalPullTimeNs) {
424 std::lock_guard<std::mutex> lock(mMutex);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000425 if (mCondition == ConditionState::kTrue) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000426 // If the pull failed, we won't be able to compute a diff.
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000427 if (!pullSuccess) {
428 invalidateCurrentBucket();
429 } else {
430 bool isEventLate = originalPullTimeNs < getCurrentBucketEndTimeNs();
431 if (isEventLate) {
432 // If the event is late, we are in the middle of a bucket. Just
433 // process the data without trying to snap the data to the nearest bucket.
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000434 accumulateEvents(allData, originalPullTimeNs, originalPullTimeNs, mCondition);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000435 } else {
436 // For scheduled pulled data, the effective event time is snap to the nearest
437 // bucket end. In the case of waking up from a deep sleep state, we will
438 // attribute to the previous bucket end. If the sleep was long but not very
439 // long, we will be in the immediate next bucket. Previous bucket may get a
440 // larger number as we pull at a later time than real bucket end.
441 //
442 // If the sleep was very long, we skip more than one bucket before sleep. In
443 // this case, if the diff base will be cleared and this new data will serve as
444 // new diff base.
445 int64_t bucketEndTime = calcPreviousBucketEndTime(originalPullTimeNs) - 1;
446 StatsdStats::getInstance().noteBucketBoundaryDelayNs(
447 mMetricId, originalPullTimeNs - bucketEndTime);
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000448 accumulateEvents(allData, originalPullTimeNs, bucketEndTime, mCondition);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000449 }
450 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000451 }
452
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000453 // We can probably flush the bucket. Since we used bucketEndTime when calling
454 // #onMatchedLogEventInternalLocked, the current bucket will not have been flushed.
455 flushIfNeededLocked(originalPullTimeNs);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000456}
457
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000458void ValueMetricProducer::accumulateEvents(const std::vector<std::shared_ptr<LogEvent>>& allData,
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000459 int64_t originalPullTimeNs, int64_t eventElapsedTimeNs,
460 ConditionState condition) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000461 bool isEventLate = eventElapsedTimeNs < mCurrentBucketStartTimeNs;
462 if (isEventLate) {
463 VLOG("Skip bucket end pull due to late arrival: %lld vs %lld",
464 (long long)eventElapsedTimeNs, (long long)mCurrentBucketStartTimeNs);
465 StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId);
466 invalidateCurrentBucket();
467 return;
468 }
469
470 const int64_t pullDelayNs = getElapsedRealtimeNs() - originalPullTimeNs;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000471 StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800472 if (pullDelayNs > mMaxPullDelayNs) {
473 ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId,
474 (long long)mMaxPullDelayNs);
475 StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000476 // We are missing one pull from the bucket which means we will not have a complete view of
477 // what's going on.
478 invalidateCurrentBucket();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800479 return;
480 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800481
Olivier Gaillard11203df2019-02-06 13:18:09 +0000482 if (allData.size() == 0) {
483 VLOG("Data pulled is empty");
484 StatsdStats::getInstance().noteEmptyData(mPullTagId);
Misha Wagner1eee2212019-01-22 11:47:11 +0000485 }
486
Olivier Gaillard11203df2019-02-06 13:18:09 +0000487 mMatchedMetricDimensionKeys.clear();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800488 for (const auto& data : allData) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800489 LogEvent localCopy = data->makeCopy();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800490 if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) ==
491 MatchingState::kMatched) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000492 localCopy.setElapsedTimestampNs(eventElapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800493 onMatchedLogEventLocked(mWhatMatcherIndex, localCopy);
494 }
495 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000496 // If the new pulled data does not contains some keys we track in our intervals, we need to
497 // reset the base.
498 for (auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000499 bool presentInPulledData = mMatchedMetricDimensionKeys.find(slice.first)
Olivier Gaillard11203df2019-02-06 13:18:09 +0000500 != mMatchedMetricDimensionKeys.end();
501 if (!presentInPulledData) {
502 for (auto& interval : slice.second) {
503 interval.hasBase = false;
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800504 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800505 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700506 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000507 mMatchedMetricDimensionKeys.clear();
508 mHasGlobalBase = true;
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000509
510 // If we reach the guardrail, we might have dropped some data which means the bucket is
511 // incomplete.
512 //
513 // The base also needs to be reset. If we do not have the full data, we might
514 // incorrectly compute the diff when mUseZeroDefaultBase is true since an existing key
515 // might be missing from mCurrentSlicedBucket.
516 if (hasReachedGuardRailLimit()) {
517 invalidateCurrentBucket();
518 mCurrentSlicedBucket.clear();
519 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700520}
521
Yangster-maca78d0082018-03-12 12:02:56 -0700522void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
523 if (mCurrentSlicedBucket.size() == 0) {
524 return;
525 }
526
527 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
528 (unsigned long)mCurrentSlicedBucket.size());
529 if (verbose) {
530 for (const auto& it : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700531 for (const auto& interval : it.second) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700532 fprintf(out, "\t(what)%s\t(condition)%s (value)%s\n",
533 it.first.getDimensionKeyInWhat().toString().c_str(),
534 it.first.getDimensionKeyInCondition().toString().c_str(),
Chenjie Yu32717c32018-10-20 23:54:48 -0700535 interval.value.toString().c_str());
536 }
Yangster-maca78d0082018-03-12 12:02:56 -0700537 }
538 }
539}
540
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000541bool ValueMetricProducer::hasReachedGuardRailLimit() const {
542 return mCurrentSlicedBucket.size() >= mDimensionHardLimit;
543}
544
Yangster-mac93694462018-01-22 20:49:31 -0800545bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800546 // ===========GuardRail==============
547 // 1. Report the tuple count if the tuple count > soft limit
548 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
549 return false;
550 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800551 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800552 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800553 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800554 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000555 if (hasReachedGuardRailLimit()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700556 ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId,
557 newKey.toString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000558 StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId);
Yao Chenb3561512017-11-21 18:07:17 -0800559 return true;
560 }
561 }
562
563 return false;
564}
565
Chenjie Yudbe5c502018-11-30 23:15:57 -0800566bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) {
567 // ===========GuardRail==============
568 // 1. Report the tuple count if the tuple count > soft limit
569 if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) {
570 return false;
571 }
572 if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) {
573 size_t newTupleCount = mCurrentFullBucket.size() + 1;
574 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
575 if (newTupleCount > mDimensionHardLimit) {
576 ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s",
577 (long long)mMetricId,
578 newKey.toString().c_str());
579 return true;
580 }
581 }
582
583 return false;
584}
585
Chenjie Yu32717c32018-10-20 23:54:48 -0700586bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) {
587 for (const FieldValue& value : event.getValues()) {
588 if (value.mField.matches(matcher)) {
589 switch (value.mValue.type) {
590 case INT:
591 ret.setLong(value.mValue.int_value);
592 break;
593 case LONG:
594 ret.setLong(value.mValue.long_value);
595 break;
596 case FLOAT:
597 ret.setDouble(value.mValue.float_value);
598 break;
599 case DOUBLE:
600 ret.setDouble(value.mValue.double_value);
601 break;
602 default:
603 break;
604 }
605 return true;
606 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700607 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700608 return false;
Chenjie Yua0f02242018-07-06 16:14:34 -0700609}
610
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700611void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex,
612 const MetricDimensionKey& eventKey,
613 const ConditionKey& conditionKey,
614 bool condition, const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700615 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000616 if (eventTimeNs < mCurrentBucketStartTimeNs) {
617 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
618 (long long)mCurrentBucketStartTimeNs);
619 return;
620 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000621 mMatchedMetricDimensionKeys.insert(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000622
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000623 if (!mIsPulled) {
624 // We cannot flush without doing a pull first.
625 flushIfNeededLocked(eventTimeNs);
626 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800627
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700628 // For pulled data, we already check condition when we decide to pull or
629 // in onDataPulled. So take all of them.
630 // For pushed data, just check condition.
631 if (!(mIsPulled || condition)) {
632 VLOG("ValueMetric skip event because condition is false");
633 return;
634 }
635
Yangsterf2bee6f2017-11-29 12:01:05 -0800636 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800637 return;
638 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700639 vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey];
640 if (multiIntervals.size() < mFieldMatchers.size()) {
641 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
642 multiIntervals.resize(mFieldMatchers.size());
Yangster-maca7fb12d2018-01-03 17:17:20 -0800643 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000644
Misha Wagner26531762019-01-21 14:18:51 +0000645 // We only use anomaly detection under certain cases.
646 // N.B.: The anomaly detection cases were modified in order to fix an issue with value metrics
647 // containing multiple values. We tried to retain all previous behaviour, but we are unsure the
648 // previous behaviour was correct. At the time of the fix, anomaly detection had no owner.
649 // Whoever next works on it should look into the cases where it is triggered in this function.
650 // Discussion here: http://ag/6124370.
651 bool useAnomalyDetection = true;
652
Chenjie Yu32717c32018-10-20 23:54:48 -0700653 for (int i = 0; i < (int)mFieldMatchers.size(); i++) {
654 const Matcher& matcher = mFieldMatchers[i];
655 Interval& interval = multiIntervals[i];
656 interval.valueIndex = i;
657 Value value;
658 if (!getDoubleOrLong(event, matcher, value)) {
659 VLOG("Failed to get value %d from event %s", i, event.ToString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000660 StatsdStats::getInstance().noteBadValueType(mMetricId);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700661 return;
662 }
Chenjie Yudbe5c502018-11-30 23:15:57 -0800663 interval.seenNewData = true;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700664
Chenjie Yu32717c32018-10-20 23:54:48 -0700665 if (mUseDiff) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700666 if (!interval.hasBase) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800667 if (mHasGlobalBase && mUseZeroDefaultBase) {
668 // The bucket has global base. This key does not.
669 // Optionally use zero as base.
670 interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE);
671 interval.hasBase = true;
672 } else {
673 // no base. just update base and return.
674 interval.base = value;
675 interval.hasBase = true;
Misha Wagner26531762019-01-21 14:18:51 +0000676 // If we're missing a base, do not use anomaly detection on incomplete data
677 useAnomalyDetection = false;
678 // Continue (instead of return) here in order to set interval.base and
679 // interval.hasBase for other intervals
680 continue;
Chenjie Yuf275f612018-11-30 23:29:06 -0800681 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700682 }
683 Value diff;
684 switch (mValueDirection) {
685 case ValueMetric::INCREASING:
686 if (value >= interval.base) {
687 diff = value - interval.base;
688 } else if (mUseAbsoluteValueOnReset) {
689 diff = value;
690 } else {
691 VLOG("Unexpected decreasing value");
692 StatsdStats::getInstance().notePullDataError(mPullTagId);
693 interval.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000694 // If we've got bad data, do not use anomaly detection
695 useAnomalyDetection = false;
696 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700697 }
698 break;
699 case ValueMetric::DECREASING:
700 if (interval.base >= value) {
701 diff = interval.base - value;
702 } else if (mUseAbsoluteValueOnReset) {
703 diff = value;
704 } else {
705 VLOG("Unexpected increasing value");
706 StatsdStats::getInstance().notePullDataError(mPullTagId);
707 interval.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000708 // If we've got bad data, do not use anomaly detection
709 useAnomalyDetection = false;
710 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700711 }
712 break;
713 case ValueMetric::ANY:
714 diff = value - interval.base;
715 break;
716 default:
717 break;
718 }
719 interval.base = value;
720 value = diff;
Yao Chen6a8c7992017-11-29 20:02:07 +0000721 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700722
723 if (interval.hasValue) {
724 switch (mAggregationType) {
725 case ValueMetric::SUM:
726 // for AVG, we add up and take average when flushing the bucket
727 case ValueMetric::AVG:
728 interval.value += value;
729 break;
730 case ValueMetric::MIN:
731 interval.value = std::min(value, interval.value);
732 break;
733 case ValueMetric::MAX:
734 interval.value = std::max(value, interval.value);
735 break;
736 default:
737 break;
738 }
739 } else {
740 interval.value = value;
741 interval.hasValue = true;
742 }
743 interval.sampleSize += 1;
Yangster8de69392017-11-27 13:48:29 -0800744 }
Bookatzde1b55622017-12-14 18:38:27 -0800745
Misha Wagner26531762019-01-21 14:18:51 +0000746 // Only trigger the tracker if all intervals are correct
747 if (useAnomalyDetection) {
748 // TODO: propgate proper values down stream when anomaly support doubles
749 long wholeBucketVal = multiIntervals[0].value.long_value;
750 auto prev = mCurrentFullBucket.find(eventKey);
751 if (prev != mCurrentFullBucket.end()) {
752 wholeBucketVal += prev->second;
753 }
754 for (auto& tracker : mAnomalyTrackers) {
Yao Chen4ce07292019-02-13 13:06:36 -0800755 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey,
756 wholeBucketVal);
Misha Wagner26531762019-01-21 14:18:51 +0000757 }
Bookatzde1b55622017-12-14 18:38:27 -0800758 }
Yangster8de69392017-11-27 13:48:29 -0800759}
760
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000761// For pulled metrics, we always need to make sure we do a pull before flushing the bucket
762// if mCondition is true!
Yangster-macb142cc82018-03-30 15:22:08 -0700763void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
764 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700765 if (eventTimeNs < currentBucketEndTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700766 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800767 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700768 return;
769 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000770 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000771 int64_t nextBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
772 flushCurrentBucketLocked(eventTimeNs, nextBucketStartTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000773}
David Chen27785a82018-01-19 17:06:45 -0800774
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000775int64_t ValueMetricProducer::calcBucketsForwardCount(const int64_t& eventTimeNs) const {
776 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
777 if (eventTimeNs < currentBucketEndTimeNs) {
778 return 0;
David Chen27785a82018-01-19 17:06:45 -0800779 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000780 return 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800781}
782
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000783void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs,
784 const int64_t& nextBucketStartTimeNs) {
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000785 if (mCondition == ConditionState::kUnknown) {
786 StatsdStats::getInstance().noteBucketUnknownCondition(mMetricId);
787 }
788
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000789 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
790 mCurrentBucketNum += numBucketsForward;
791 if (numBucketsForward > 1) {
792 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
793 StatsdStats::getInstance().noteSkippedForwardBuckets(mMetricId);
794 // Something went wrong. Maybe the device was sleeping for a long time. It is better
795 // to mark the current bucket as invalid. The last pull might have been successful through.
796 invalidateCurrentBucketWithoutResetBase();
797 }
798
Chenjie Yub3dda412017-10-24 13:41:59 -0700799 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
800 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700801 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
Chenjie Yu32717c32018-10-20 23:54:48 -0700802 int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs;
Chenjie Yub3dda412017-10-24 13:41:59 -0700803
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000804 bool isBucketLargeEnough = bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs;
805 if (isBucketLargeEnough && !mCurrentBucketIsInvalid) {
David Chen81245fd2018-04-12 14:33:37 -0700806 // The current bucket is large enough to keep.
David Chen81245fd2018-04-12 14:33:37 -0700807 for (const auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000808 ValueBucket bucket = buildPartialBucket(bucketEndTime, slice.second);
Chenjie Yu32717c32018-10-20 23:54:48 -0700809 // it will auto create new vector of ValuebucketInfo if the key is not found.
810 if (bucket.valueIndex.size() > 0) {
David Chen81245fd2018-04-12 14:33:37 -0700811 auto& bucketList = mPastBuckets[slice.first];
Chenjie Yu32717c32018-10-20 23:54:48 -0700812 bucketList.push_back(bucket);
David Chen81245fd2018-04-12 14:33:37 -0700813 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700814 }
David Chen81245fd2018-04-12 14:33:37 -0700815 } else {
Chenjie Yu32717c32018-10-20 23:54:48 -0700816 mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700817 }
818
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000819 if (!mCurrentBucketIsInvalid) {
820 appendToFullBucket(eventTimeNs, fullBucketEndTimeNs);
821 }
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000822 initCurrentSlicedBucket(nextBucketStartTimeNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000823}
824
825ValueBucket ValueMetricProducer::buildPartialBucket(int64_t bucketEndTime,
826 const std::vector<Interval>& intervals) {
827 ValueBucket bucket;
828 bucket.mBucketStartNs = mCurrentBucketStartTimeNs;
829 bucket.mBucketEndNs = bucketEndTime;
830 for (const auto& interval : intervals) {
831 if (interval.hasValue) {
832 // skip the output if the diff is zero
833 if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) {
834 continue;
835 }
836 bucket.valueIndex.push_back(interval.valueIndex);
837 if (mAggregationType != ValueMetric::AVG) {
838 bucket.values.push_back(interval.value);
839 } else {
840 double sum = interval.value.type == LONG ? (double)interval.value.long_value
841 : interval.value.double_value;
842 bucket.values.push_back(Value((double)sum / interval.sampleSize));
843 }
844 }
845 }
846 return bucket;
847}
848
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000849void ValueMetricProducer::initCurrentSlicedBucket(int64_t nextBucketStartTimeNs) {
850 StatsdStats::getInstance().noteBucketCount(mMetricId);
851 // Cleanup data structure to aggregate values.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000852 for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) {
853 bool obsolete = true;
854 for (auto& interval : it->second) {
855 interval.hasValue = false;
856 interval.sampleSize = 0;
857 if (interval.seenNewData) {
858 obsolete = false;
859 }
860 interval.seenNewData = false;
861 }
862
863 if (obsolete) {
864 it = mCurrentSlicedBucket.erase(it);
865 } else {
866 it++;
867 }
868 }
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000869
870 mCurrentBucketIsInvalid = false;
871 // If we do not have a global base when the condition is true,
872 // we will have incomplete bucket for the next bucket.
873 if (mUseDiff && !mHasGlobalBase && mCondition) {
874 mCurrentBucketIsInvalid = false;
875 }
876 mCurrentBucketStartTimeNs = nextBucketStartTimeNs;
877 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
878 (long long)mCurrentBucketStartTimeNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000879}
880
881void ValueMetricProducer::appendToFullBucket(int64_t eventTimeNs, int64_t fullBucketEndTimeNs) {
David Chen27785a82018-01-19 17:06:45 -0800882 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
883 // Accumulate partial buckets with current value and then send to anomaly tracker.
884 if (mCurrentFullBucket.size() > 0) {
885 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yudbe5c502018-11-30 23:15:57 -0800886 if (hitFullBucketGuardRailLocked(slice.first)) {
887 continue;
888 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700889 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700890 mCurrentFullBucket[slice.first] += slice.second[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800891 }
892 for (const auto& slice : mCurrentFullBucket) {
893 for (auto& tracker : mAnomalyTrackers) {
894 if (tracker != nullptr) {
895 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
896 }
897 }
898 }
899 mCurrentFullBucket.clear();
900 } else {
901 // Skip aggregating the partial buckets since there's no previous partial bucket.
902 for (const auto& slice : mCurrentSlicedBucket) {
903 for (auto& tracker : mAnomalyTrackers) {
904 if (tracker != nullptr) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700905 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700906 tracker->addPastBucket(slice.first, slice.second[0].value.long_value,
Chenjie Yua0f02242018-07-06 16:14:34 -0700907 mCurrentBucketNum);
David Chen27785a82018-01-19 17:06:45 -0800908 }
909 }
910 }
911 }
912 } else {
913 // Accumulate partial bucket.
914 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700915 // TODO: fix this when anomaly can accept double values
Chenjie Yu32717c32018-10-20 23:54:48 -0700916 mCurrentFullBucket[slice.first] += slice.second[0].value.long_value;
David Chen27785a82018-01-19 17:06:45 -0800917 }
918 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700919}
920
Yangsterf2bee6f2017-11-29 12:01:05 -0800921size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800922 size_t totalSize = 0;
923 for (const auto& pair : mPastBuckets) {
924 totalSize += pair.second.size() * kBucketSize;
925 }
926 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800927}
928
Chenjie Yub3dda412017-10-24 13:41:59 -0700929} // namespace statsd
930} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700931} // namespace android