blob: 2c9991125d89ca41018b9539ed0ed054c6416fa6 [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;
Howard Ro9440e092018-12-16 19:15:21 -080054const int FIELD_ID_IS_ACTIVE = 14;
yro2b0f8862017-11-06 14:27:31 -080055// for ValueMetricDataWrapper
56const int FIELD_ID_DATA = 1;
David Chen81245fd2018-04-12 14:33:37 -070057const int FIELD_ID_SKIPPED = 2;
Yangster-mac9def8e32018-04-17 13:55:51 -070058const int FIELD_ID_SKIPPED_START_MILLIS = 3;
59const int FIELD_ID_SKIPPED_END_MILLIS = 4;
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;
yro2b0f8862017-11-06 14:27:31 -080064// for ValueBucketInfo
Chenjie Yu32717c32018-10-20 23:54:48 -070065const int FIELD_ID_VALUE_INDEX = 1;
66const int FIELD_ID_VALUE_LONG = 2;
67const int FIELD_ID_VALUE_DOUBLE = 3;
68const int FIELD_ID_VALUES = 9;
Yangster-mac9def8e32018-04-17 13:55:51 -070069const int FIELD_ID_BUCKET_NUM = 4;
70const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
71const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
Yao Chene6cfb142019-04-08 12:00:01 -070072const int FIELD_ID_CONDITION_TRUE_NS = 10;
yro2b0f8862017-11-06 14:27:31 -080073
Chenjie Yuf275f612018-11-30 23:29:06 -080074const Value ZERO_LONG((int64_t)0);
75const Value ZERO_DOUBLE((int64_t)0);
76
Chenjie Yub3dda412017-10-24 13:41:59 -070077// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Chenjie Yuf275f612018-11-30 23:29:06 -080078ValueMetricProducer::ValueMetricProducer(
79 const ConfigKey& key, const ValueMetric& metric, const int conditionIndex,
80 const sp<ConditionWizard>& conditionWizard, const int whatMatcherIndex,
81 const sp<EventMatcherWizard>& matcherWizard, const int pullTagId, const int64_t timeBaseNs,
Ruchir Rastogi21a287b2019-10-02 12:04:33 -070082 const int64_t startTimeNs, const sp<StatsPullerManager>& pullerManager,
83 const unordered_map<int, shared_ptr<Activation>>& eventActivationMap,
tsaichristined21aacf2019-10-07 14:47:38 -070084 const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap,
85 const vector<int>& slicedStateAtoms,
86 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap)
Ruchir Rastogi21a287b2019-10-02 12:04:33 -070087 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, conditionWizard,
tsaichristined21aacf2019-10-07 14:47:38 -070088 eventActivationMap, eventDeactivationMap, slicedStateAtoms, stateGroupMap),
Chenjie Yu054ce9c2018-11-12 15:27:29 -080089 mWhatMatcherIndex(whatMatcherIndex),
90 mEventMatcherWizard(matcherWizard),
Chenjie Yue2219202018-06-08 10:07:51 -070091 mPullerManager(pullerManager),
Chenjie Yuc5875052018-03-09 10:13:11 -080092 mPullTagId(pullTagId),
Chenjie Yua0f02242018-07-06 16:14:34 -070093 mIsPulled(pullTagId != -1),
David Chen81245fd2018-04-12 14:33:37 -070094 mMinBucketSizeNs(metric.min_bucket_size_nanos()),
Chenjie Yuc5875052018-03-09 10:13:11 -080095 mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
96 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
97 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first
98 : StatsdStats::kDimensionKeySizeSoftLimit),
99 mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
100 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
101 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second
Chenjie Yu47234642018-05-14 10:14:16 -0700102 : StatsdStats::kDimensionKeySizeHardLimit),
Chenjie Yua0f02242018-07-06 16:14:34 -0700103 mUseAbsoluteValueOnReset(metric.use_absolute_value_on_reset()),
104 mAggregationType(metric.aggregation_type()),
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700105 mUseDiff(metric.has_use_diff() ? metric.use_diff() : (mIsPulled ? true : false)),
106 mValueDirection(metric.value_direction()),
Chenjie Yuf275f612018-11-30 23:29:06 -0800107 mSkipZeroDiffOutput(metric.skip_zero_diff_output()),
108 mUseZeroDefaultBase(metric.use_zero_default_base()),
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800109 mHasGlobalBase(false),
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000110 mCurrentBucketIsInvalid(false),
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800111 mMaxPullDelayNs(metric.max_pull_delay_sec() > 0 ? metric.max_pull_delay_sec() * NS_PER_SEC
Chenjie Yucd1b7972019-01-16 20:38:15 -0800112 : StatsdStats::kPullMaxDelayNs),
Yao Chene6cfb142019-04-08 12:00:01 -0700113 mSplitBucketForAppUpgrade(metric.split_bucket_for_app_upgrade()),
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700114 // Condition timer will be set later within the constructor after pulling events
Tej Singhee4495e2019-06-03 18:37:35 -0700115 mConditionTimer(false, timeBaseNs) {
Yangster-macb8144812018-01-04 10:56:23 -0800116 int64_t bucketSizeMills = 0;
117 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -0800118 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -0800119 } else {
Yangster-macb8144812018-01-04 10:56:23 -0800120 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -0800121 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700122
Yangster-macb8144812018-01-04 10:56:23 -0800123 mBucketSizeNs = bucketSizeMills * 1000000;
Chenjie Yu32717c32018-10-20 23:54:48 -0700124
125 translateFieldMatcher(metric.value_field(), &mFieldMatchers);
126
Yao Chen8a8d16c2018-02-08 14:50:40 -0800127 if (metric.has_dimensions_in_what()) {
128 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -0800129 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
tsaichristine90f95bb2019-11-06 17:06:53 -0800130 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800131 }
132
Yao Chen93fe3a32017-11-02 13:52:59 -0700133 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800134 for (const auto& link : metric.links()) {
135 Metric2Condition mc;
136 mc.conditionId = link.condition();
137 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
138 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
139 mMetric2ConditionLinks.push_back(mc);
140 }
tsaichristine76853372019-08-06 17:17:03 -0700141 mConditionSliced = true;
Yao Chen93fe3a32017-11-02 13:52:59 -0700142 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800143
Tej Singh597c7162019-04-17 16:41:45 -0700144 int64_t numBucketsForward = calcBucketsForwardCount(startTimeNs);
145 mCurrentBucketNum += numBucketsForward;
146
Chenjie Yue1361ed2018-07-23 17:33:09 -0700147 flushIfNeededLocked(startTimeNs);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700148
Chenjie Yua0f02242018-07-06 16:14:34 -0700149 if (mIsPulled) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700150 mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(),
151 mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700152 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700153
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700154 // Only do this for partial buckets like first bucket. All other buckets should use
Chenjie Yue1361ed2018-07-23 17:33:09 -0700155 // flushIfNeeded to adjust start and end to bucket boundaries.
156 // Adjust start for partial bucket
157 mCurrentBucketStartTimeNs = startTimeNs;
Yao Chene6cfb142019-04-08 12:00:01 -0700158 mConditionTimer.newBucketStart(mCurrentBucketStartTimeNs);
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700159
160 // Kicks off the puller immediately if condition is true and diff based.
161 if (mIsActive && mIsPulled && mCondition == ConditionState::kTrue && mUseDiff) {
tsaichristine78b85702019-12-06 12:20:30 -0800162 pullAndMatchEventsLocked(mCurrentBucketStartTimeNs);
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700163 }
164 // Now that activations are processed, start the condition timer if needed.
165 mConditionTimer.onConditionChanged(mIsActive && mCondition == ConditionState::kTrue,
166 mCurrentBucketStartTimeNs);
167
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700168 VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
169 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700170}
171
172ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700173 VLOG("~ValueMetricProducer() called");
Chenjie Yua0f02242018-07-06 16:14:34 -0700174 if (mIsPulled) {
Chenjie Yue2219202018-06-08 10:07:51 -0700175 mPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yu6736c892017-11-09 10:50:09 -0800176 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700177}
178
Yao Chen427d3722018-03-22 15:21:52 -0700179void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700180 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800181 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700182}
183
Yangster-macb142cc82018-03-30 15:22:08 -0700184void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Olivier Gaillard320952b2019-02-06 13:57:24 +0000185 StatsdStats::getInstance().noteBucketDropped(mMetricId);
tsaichristineb7fcf002019-12-06 18:40:47 -0800186
187 // The current partial bucket is not flushed and does not require a pull,
188 // so the data is still valid.
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000189 flushIfNeededLocked(dropTimeNs);
190 clearPastBucketsLocked(dropTimeNs);
Yao Chen06dba5d2018-01-26 13:38:16 -0800191}
192
Yangster-maca802d732018-04-24 07:50:38 -0700193void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
Yangster-maca802d732018-04-24 07:50:38 -0700194 mPastBuckets.clear();
195 mSkippedBuckets.clear();
196}
197
Yangster-macb142cc82018-03-30 15:22:08 -0700198void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700199 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700200 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000201 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700202 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800203 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800204 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700205 if (include_current_partial_bucket) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000206 // For pull metrics, we need to do a pull at bucket boundaries. If we do not do that the
207 // current bucket will have incomplete data and the next will have the wrong snapshot to do
208 // a diff against. If the condition is false, we are fine since the base data is reset and
209 // we are not tracking anything.
210 bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue;
211 if (pullNeeded) {
212 switch (dumpLatency) {
213 case FAST:
214 invalidateCurrentBucket();
215 break;
216 case NO_TIME_CONSTRAINTS:
tsaichristine78b85702019-12-06 12:20:30 -0800217 pullAndMatchEventsLocked(dumpTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000218 break;
219 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000220 }
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000221 flushCurrentBucketLocked(dumpTimeNs, dumpTimeNs);
Yangster-mace68f3a52018-04-04 00:01:43 -0700222 }
Yang Lub4722912018-11-15 11:02:03 -0800223 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800224 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800225
David Chen81245fd2018-04-12 14:33:37 -0700226 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800227 return;
228 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700229 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
230 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
231 // Fills the dimension path if not slicing by ALL.
232 if (!mSliceByPositionALL) {
233 if (!mDimensionsInWhat.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700234 uint64_t dimenPathToken =
235 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700236 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
237 protoOutput->end(dimenPathToken);
238 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700239 }
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);
Yangster-mac9def8e32018-04-17 13:55:51 -0700265 } else {
266 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
267 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800268 }
yro2b0f8862017-11-06 14:27:31 -0800269
270 // Then fill bucket_info (ValueBucketInfo).
271 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800272 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800273 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700274
275 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
276 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
277 (long long)NanoToMillis(bucket.mBucketStartNs));
278 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
279 (long long)NanoToMillis(bucket.mBucketEndNs));
280 } else {
281 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
282 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
283 }
Yao Chene6cfb142019-04-08 12:00:01 -0700284 // only write the condition timer value if the metric has a condition.
285 if (mConditionTrackerIndex >= 0) {
286 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_TRUE_NS,
287 (long long)bucket.mConditionTrueNs);
288 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700289 for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) {
290 int index = bucket.valueIndex[i];
291 const Value& value = bucket.values[i];
292 uint64_t valueToken = protoOutput->start(
293 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES);
294 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX,
295 index);
296 if (value.getType() == LONG) {
297 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG,
298 (long long)value.long_value);
299 VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs,
300 (long long)bucket.mBucketEndNs, index, (long long)value.long_value);
301 } else if (value.getType() == DOUBLE) {
302 protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE,
303 value.double_value);
304 VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs,
305 (long long)bucket.mBucketEndNs, index, value.double_value);
306 } else {
307 VLOG("Wrong value type for ValueMetric output: %d", value.getType());
308 }
309 protoOutput->end(valueToken);
Chenjie Yua0f02242018-07-06 16:14:34 -0700310 }
Yao Chen288c6002017-12-12 13:43:18 -0800311 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800312 }
Yao Chen288c6002017-12-12 13:43:18 -0800313 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700314 }
Yao Chen288c6002017-12-12 13:43:18 -0800315 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800316
Yangster-mac94e197c2018-01-02 16:03:03 -0800317 VLOG("metric %lld dump report now...", (long long)mMetricId);
Bookatzff71cad2018-09-20 17:17:49 -0700318 if (erase_data) {
319 mPastBuckets.clear();
320 mSkippedBuckets.clear();
321 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700322}
323
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000324void ValueMetricProducer::invalidateCurrentBucketWithoutResetBase() {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000325 if (!mCurrentBucketIsInvalid) {
326 // Only report once per invalid bucket.
327 StatsdStats::getInstance().noteInvalidatedBucket(mMetricId);
328 }
329 mCurrentBucketIsInvalid = true;
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000330}
331
332void ValueMetricProducer::invalidateCurrentBucket() {
333 invalidateCurrentBucketWithoutResetBase();
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000334 resetBase();
335}
336
Chenjie Yuf275f612018-11-30 23:29:06 -0800337void ValueMetricProducer::resetBase() {
338 for (auto& slice : mCurrentSlicedBucket) {
339 for (auto& interval : slice.second) {
340 interval.hasBase = false;
341 }
342 }
343 mHasGlobalBase = false;
344}
345
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700346// Handle active state change. Active state change is treated like a condition change:
347// - drop bucket if active state change event arrives too late
348// - if condition is true, pull data on active state changes
349// - ConditionTimer tracks changes based on AND of condition and active state.
350void ValueMetricProducer::onActiveStateChangedLocked(const int64_t& eventTimeNs) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000351 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
tsaichristine90f95bb2019-11-06 17:06:53 -0800352 if (isEventTooLate) {
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700353 // Drop bucket because event arrived too late, ie. we are missing data for this bucket.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000354 invalidateCurrentBucket();
Yao Chen2794da22017-12-13 16:01:55 -0800355 }
356
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700357 // Call parent method once we've verified the validity of current bucket.
358 MetricProducer::onActiveStateChangedLocked(eventTimeNs);
359
360 if (ConditionState::kTrue != mCondition) {
361 return;
362 }
363
364 // Pull on active state changes.
365 if (!isEventTooLate) {
366 if (mIsPulled) {
tsaichristine78b85702019-12-06 12:20:30 -0800367 pullAndMatchEventsLocked(eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700368 }
369 // When active state changes from true to false, clear diff base but don't
370 // reset other counters as we may accumulate more value in the bucket.
371 if (mUseDiff && !mIsActive) {
372 resetBase();
373 }
374 }
375
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700376 flushIfNeededLocked(eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700377
378 // Let condition timer know of new active state.
379 mConditionTimer.onConditionChanged(mIsActive, eventTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700380}
381
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700382void ValueMetricProducer::onConditionChangedLocked(const bool condition,
383 const int64_t eventTimeNs) {
384 ConditionState newCondition = condition ? ConditionState::kTrue : ConditionState::kFalse;
385 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
386
tsaichristine90f95bb2019-11-06 17:06:53 -0800387 // If the config is not active, skip the event.
388 if (!mIsActive) {
389 mCondition = isEventTooLate ? ConditionState::kUnknown : newCondition;
390 return;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700391 }
392
tsaichristine90f95bb2019-11-06 17:06:53 -0800393 // If the event arrived late, mark the bucket as invalid and skip the event.
394 if (isEventTooLate) {
395 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
396 (long long)mCurrentBucketStartTimeNs);
397 StatsdStats::getInstance().noteConditionChangeInNextBucket(mMetricId);
398 invalidateCurrentBucket();
399 mCondition = ConditionState::kUnknown;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700400 mConditionTimer.onConditionChanged(mCondition, eventTimeNs);
tsaichristine90f95bb2019-11-06 17:06:53 -0800401 return;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700402 }
tsaichristine90f95bb2019-11-06 17:06:53 -0800403
404 // If the previous condition was unknown, mark the bucket as invalid
405 // because the bucket will contain partial data. For example, the condition
406 // change might happen close to the end of the bucket and we might miss a
407 // lot of data.
408 //
409 // We still want to pull to set the base.
410 if (mCondition == ConditionState::kUnknown) {
411 invalidateCurrentBucket();
412 }
413
414 // Pull and match for the following condition change cases:
415 // unknown/false -> true - condition changed
416 // true -> false - condition changed
417 // true -> true - old condition was true so we can flush the bucket at the
418 // end if needed.
419 //
420 // We don’t need to pull for unknown -> false or false -> false.
421 //
422 // onConditionChangedLocked might happen on bucket boundaries if this is
423 // called before #onDataPulled.
424 if (mIsPulled &&
425 (newCondition == ConditionState::kTrue || mCondition == ConditionState::kTrue)) {
tsaichristine78b85702019-12-06 12:20:30 -0800426 pullAndMatchEventsLocked(eventTimeNs);
tsaichristine90f95bb2019-11-06 17:06:53 -0800427 }
428
429 // For metrics that use diff, when condition changes from true to false,
430 // clear diff base but don't reset other counts because we may accumulate
431 // more value in the bucket.
432 if (mUseDiff &&
433 (mCondition == ConditionState::kTrue && newCondition == ConditionState::kFalse)) {
434 resetBase();
435 }
436
437 // Update condition state after pulling.
438 mCondition = newCondition;
439
440 flushIfNeededLocked(eventTimeNs);
441 mConditionTimer.onConditionChanged(mCondition, eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700442}
443
tsaichristine78b85702019-12-06 12:20:30 -0800444void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700445 vector<std::shared_ptr<LogEvent>> allData;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800446 if (!mPullerManager->Pull(mPullTagId, &allData)) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000447 ALOGE("Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000448 invalidateCurrentBucket();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800449 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700450 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000451
tsaichristine78b85702019-12-06 12:20:30 -0800452 accumulateEvents(allData, timestampNs, timestampNs);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000453}
454
455int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) {
456 return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
457}
458
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000459// By design, statsd pulls data at bucket boundaries using AlarmManager. These pulls are likely
460// to be delayed. Other events like condition changes or app upgrade which are not based on
461// AlarmManager might have arrived earlier and close the bucket.
Olivier Gaillard11203df2019-02-06 13:18:09 +0000462void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData,
463 bool pullSuccess, int64_t originalPullTimeNs) {
464 std::lock_guard<std::mutex> lock(mMutex);
tsaichristine90f95bb2019-11-06 17:06:53 -0800465 if (mCondition == ConditionState::kTrue) {
466 // If the pull failed, we won't be able to compute a diff.
467 if (!pullSuccess) {
468 invalidateCurrentBucket();
469 } else {
470 bool isEventLate = originalPullTimeNs < getCurrentBucketEndTimeNs();
471 if (isEventLate) {
472 // If the event is late, we are in the middle of a bucket. Just
473 // process the data without trying to snap the data to the nearest bucket.
tsaichristine78b85702019-12-06 12:20:30 -0800474 accumulateEvents(allData, originalPullTimeNs, originalPullTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000475 } else {
tsaichristine90f95bb2019-11-06 17:06:53 -0800476 // For scheduled pulled data, the effective event time is snap to the nearest
477 // bucket end. In the case of waking up from a deep sleep state, we will
478 // attribute to the previous bucket end. If the sleep was long but not very
479 // long, we will be in the immediate next bucket. Previous bucket may get a
480 // larger number as we pull at a later time than real bucket end.
481 //
482 // If the sleep was very long, we skip more than one bucket before sleep. In
483 // this case, if the diff base will be cleared and this new data will serve as
484 // new diff base.
485 int64_t bucketEndTime = calcPreviousBucketEndTime(originalPullTimeNs) - 1;
486 StatsdStats::getInstance().noteBucketBoundaryDelayNs(
487 mMetricId, originalPullTimeNs - bucketEndTime);
tsaichristine78b85702019-12-06 12:20:30 -0800488 accumulateEvents(allData, originalPullTimeNs, bucketEndTime);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000489 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000490 }
tsaichristine90f95bb2019-11-06 17:06:53 -0800491 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000492
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000493 // We can probably flush the bucket. Since we used bucketEndTime when calling
494 // #onMatchedLogEventInternalLocked, the current bucket will not have been flushed.
495 flushIfNeededLocked(originalPullTimeNs);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000496}
497
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000498void ValueMetricProducer::accumulateEvents(const std::vector<std::shared_ptr<LogEvent>>& allData,
tsaichristine78b85702019-12-06 12:20:30 -0800499 int64_t originalPullTimeNs, int64_t eventElapsedTimeNs) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000500 bool isEventLate = eventElapsedTimeNs < mCurrentBucketStartTimeNs;
501 if (isEventLate) {
502 VLOG("Skip bucket end pull due to late arrival: %lld vs %lld",
503 (long long)eventElapsedTimeNs, (long long)mCurrentBucketStartTimeNs);
504 StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId);
505 invalidateCurrentBucket();
506 return;
507 }
508
509 const int64_t pullDelayNs = getElapsedRealtimeNs() - originalPullTimeNs;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000510 StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800511 if (pullDelayNs > mMaxPullDelayNs) {
512 ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId,
513 (long long)mMaxPullDelayNs);
514 StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000515 // We are missing one pull from the bucket which means we will not have a complete view of
516 // what's going on.
517 invalidateCurrentBucket();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800518 return;
519 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800520
Olivier Gaillard11203df2019-02-06 13:18:09 +0000521 if (allData.size() == 0) {
522 VLOG("Data pulled is empty");
523 StatsdStats::getInstance().noteEmptyData(mPullTagId);
Misha Wagner1eee2212019-01-22 11:47:11 +0000524 }
525
Olivier Gaillard11203df2019-02-06 13:18:09 +0000526 mMatchedMetricDimensionKeys.clear();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800527 for (const auto& data : allData) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800528 LogEvent localCopy = data->makeCopy();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800529 if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) ==
530 MatchingState::kMatched) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000531 localCopy.setElapsedTimestampNs(eventElapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800532 onMatchedLogEventLocked(mWhatMatcherIndex, localCopy);
533 }
534 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000535 // If the new pulled data does not contains some keys we track in our intervals, we need to
536 // reset the base.
537 for (auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000538 bool presentInPulledData = mMatchedMetricDimensionKeys.find(slice.first)
Olivier Gaillard11203df2019-02-06 13:18:09 +0000539 != mMatchedMetricDimensionKeys.end();
540 if (!presentInPulledData) {
541 for (auto& interval : slice.second) {
542 interval.hasBase = false;
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800543 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800544 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700545 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000546 mMatchedMetricDimensionKeys.clear();
547 mHasGlobalBase = true;
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000548
549 // If we reach the guardrail, we might have dropped some data which means the bucket is
550 // incomplete.
551 //
552 // The base also needs to be reset. If we do not have the full data, we might
553 // incorrectly compute the diff when mUseZeroDefaultBase is true since an existing key
554 // might be missing from mCurrentSlicedBucket.
555 if (hasReachedGuardRailLimit()) {
556 invalidateCurrentBucket();
557 mCurrentSlicedBucket.clear();
558 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700559}
560
Yangster-maca78d0082018-03-12 12:02:56 -0700561void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
562 if (mCurrentSlicedBucket.size() == 0) {
563 return;
564 }
565
566 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
567 (unsigned long)mCurrentSlicedBucket.size());
568 if (verbose) {
569 for (const auto& it : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700570 for (const auto& interval : it.second) {
tsaichristine69000e62019-10-18 17:34:52 -0700571 fprintf(out, "\t(what)%s\t(states)%s (value)%s\n",
572 it.first.getDimensionKeyInWhat().toString().c_str(),
573 it.first.getStateValuesKey().toString().c_str(),
574 interval.value.toString().c_str());
Chenjie Yu32717c32018-10-20 23:54:48 -0700575 }
Yangster-maca78d0082018-03-12 12:02:56 -0700576 }
577 }
578}
579
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000580bool ValueMetricProducer::hasReachedGuardRailLimit() const {
581 return mCurrentSlicedBucket.size() >= mDimensionHardLimit;
582}
583
Yangster-mac93694462018-01-22 20:49:31 -0800584bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800585 // ===========GuardRail==============
586 // 1. Report the tuple count if the tuple count > soft limit
587 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
588 return false;
589 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800590 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800591 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800592 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800593 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000594 if (hasReachedGuardRailLimit()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700595 ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId,
596 newKey.toString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000597 StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId);
Yao Chenb3561512017-11-21 18:07:17 -0800598 return true;
599 }
600 }
601
602 return false;
603}
604
Chenjie Yudbe5c502018-11-30 23:15:57 -0800605bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) {
606 // ===========GuardRail==============
607 // 1. Report the tuple count if the tuple count > soft limit
608 if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) {
609 return false;
610 }
611 if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) {
612 size_t newTupleCount = mCurrentFullBucket.size() + 1;
613 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
614 if (newTupleCount > mDimensionHardLimit) {
615 ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s",
616 (long long)mMetricId,
617 newKey.toString().c_str());
618 return true;
619 }
620 }
621
622 return false;
623}
624
Chenjie Yu32717c32018-10-20 23:54:48 -0700625bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) {
626 for (const FieldValue& value : event.getValues()) {
627 if (value.mField.matches(matcher)) {
628 switch (value.mValue.type) {
629 case INT:
630 ret.setLong(value.mValue.int_value);
631 break;
632 case LONG:
633 ret.setLong(value.mValue.long_value);
634 break;
635 case FLOAT:
636 ret.setDouble(value.mValue.float_value);
637 break;
638 case DOUBLE:
639 ret.setDouble(value.mValue.double_value);
640 break;
641 default:
tsaichristine409468d2019-10-28 11:32:31 -0700642 return false;
Chenjie Yu32717c32018-10-20 23:54:48 -0700643 break;
644 }
645 return true;
646 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700647 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700648 return false;
Chenjie Yua0f02242018-07-06 16:14:34 -0700649}
650
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700651void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex,
652 const MetricDimensionKey& eventKey,
653 const ConditionKey& conditionKey,
654 bool condition, const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700655 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000656 if (eventTimeNs < mCurrentBucketStartTimeNs) {
657 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
658 (long long)mCurrentBucketStartTimeNs);
659 return;
660 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000661 mMatchedMetricDimensionKeys.insert(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000662
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000663 if (!mIsPulled) {
664 // We cannot flush without doing a pull first.
665 flushIfNeededLocked(eventTimeNs);
666 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800667
Olivier Gaillardfbee9162019-04-11 11:48:01 +0100668 // We should not accumulate the data for pushed metrics when the condition is false.
669 bool shouldSkipForPushMetric = !mIsPulled && !condition;
670 // For pulled metrics, there are two cases:
671 // - to compute diffs, we need to process all the state changes
672 // - for non-diffs metrics, we should ignore the data if the condition wasn't true. If we have a
673 // state change from
674 // + True -> True: we should process the data, it might be a bucket boundary
675 // + True -> False: we als need to process the data.
676 bool shouldSkipForPulledMetric = mIsPulled && !mUseDiff
677 && mCondition != ConditionState::kTrue;
678 if (shouldSkipForPushMetric || shouldSkipForPulledMetric) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700679 VLOG("ValueMetric skip event because condition is false");
680 return;
681 }
682
Yangsterf2bee6f2017-11-29 12:01:05 -0800683 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800684 return;
685 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700686 vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey];
687 if (multiIntervals.size() < mFieldMatchers.size()) {
688 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
689 multiIntervals.resize(mFieldMatchers.size());
Yangster-maca7fb12d2018-01-03 17:17:20 -0800690 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000691
Misha Wagner26531762019-01-21 14:18:51 +0000692 // We only use anomaly detection under certain cases.
693 // N.B.: The anomaly detection cases were modified in order to fix an issue with value metrics
694 // containing multiple values. We tried to retain all previous behaviour, but we are unsure the
695 // previous behaviour was correct. At the time of the fix, anomaly detection had no owner.
696 // Whoever next works on it should look into the cases where it is triggered in this function.
697 // Discussion here: http://ag/6124370.
698 bool useAnomalyDetection = true;
699
Chenjie Yu32717c32018-10-20 23:54:48 -0700700 for (int i = 0; i < (int)mFieldMatchers.size(); i++) {
701 const Matcher& matcher = mFieldMatchers[i];
702 Interval& interval = multiIntervals[i];
703 interval.valueIndex = i;
704 Value value;
705 if (!getDoubleOrLong(event, matcher, value)) {
706 VLOG("Failed to get value %d from event %s", i, event.ToString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000707 StatsdStats::getInstance().noteBadValueType(mMetricId);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700708 return;
709 }
Chenjie Yudbe5c502018-11-30 23:15:57 -0800710 interval.seenNewData = true;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700711
Chenjie Yu32717c32018-10-20 23:54:48 -0700712 if (mUseDiff) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700713 if (!interval.hasBase) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800714 if (mHasGlobalBase && mUseZeroDefaultBase) {
715 // The bucket has global base. This key does not.
716 // Optionally use zero as base.
717 interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE);
718 interval.hasBase = true;
719 } else {
720 // no base. just update base and return.
721 interval.base = value;
722 interval.hasBase = true;
Misha Wagner26531762019-01-21 14:18:51 +0000723 // If we're missing a base, do not use anomaly detection on incomplete data
724 useAnomalyDetection = false;
725 // Continue (instead of return) here in order to set interval.base and
726 // interval.hasBase for other intervals
727 continue;
Chenjie Yuf275f612018-11-30 23:29:06 -0800728 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700729 }
730 Value diff;
731 switch (mValueDirection) {
732 case ValueMetric::INCREASING:
733 if (value >= interval.base) {
734 diff = value - interval.base;
735 } else if (mUseAbsoluteValueOnReset) {
736 diff = value;
737 } else {
738 VLOG("Unexpected decreasing value");
739 StatsdStats::getInstance().notePullDataError(mPullTagId);
740 interval.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000741 // If we've got bad data, do not use anomaly detection
742 useAnomalyDetection = false;
743 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700744 }
745 break;
746 case ValueMetric::DECREASING:
747 if (interval.base >= value) {
748 diff = interval.base - value;
749 } else if (mUseAbsoluteValueOnReset) {
750 diff = value;
751 } else {
752 VLOG("Unexpected increasing value");
753 StatsdStats::getInstance().notePullDataError(mPullTagId);
754 interval.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000755 // If we've got bad data, do not use anomaly detection
756 useAnomalyDetection = false;
757 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700758 }
759 break;
760 case ValueMetric::ANY:
761 diff = value - interval.base;
762 break;
763 default:
764 break;
765 }
766 interval.base = value;
767 value = diff;
Yao Chen6a8c7992017-11-29 20:02:07 +0000768 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700769
770 if (interval.hasValue) {
771 switch (mAggregationType) {
772 case ValueMetric::SUM:
773 // for AVG, we add up and take average when flushing the bucket
774 case ValueMetric::AVG:
775 interval.value += value;
776 break;
777 case ValueMetric::MIN:
778 interval.value = std::min(value, interval.value);
779 break;
780 case ValueMetric::MAX:
781 interval.value = std::max(value, interval.value);
782 break;
783 default:
784 break;
785 }
786 } else {
787 interval.value = value;
788 interval.hasValue = true;
789 }
790 interval.sampleSize += 1;
Yangster8de69392017-11-27 13:48:29 -0800791 }
Bookatzde1b55622017-12-14 18:38:27 -0800792
Misha Wagner26531762019-01-21 14:18:51 +0000793 // Only trigger the tracker if all intervals are correct
794 if (useAnomalyDetection) {
795 // TODO: propgate proper values down stream when anomaly support doubles
796 long wholeBucketVal = multiIntervals[0].value.long_value;
797 auto prev = mCurrentFullBucket.find(eventKey);
798 if (prev != mCurrentFullBucket.end()) {
799 wholeBucketVal += prev->second;
800 }
801 for (auto& tracker : mAnomalyTrackers) {
Yao Chen4ce07292019-02-13 13:06:36 -0800802 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey,
803 wholeBucketVal);
Misha Wagner26531762019-01-21 14:18:51 +0000804 }
Bookatzde1b55622017-12-14 18:38:27 -0800805 }
Yangster8de69392017-11-27 13:48:29 -0800806}
807
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000808// For pulled metrics, we always need to make sure we do a pull before flushing the bucket
809// if mCondition is true!
Yangster-macb142cc82018-03-30 15:22:08 -0700810void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
811 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700812 if (eventTimeNs < currentBucketEndTimeNs) {
tsaichristine90f95bb2019-11-06 17:06:53 -0800813 VLOG("eventTime is %lld, less than current bucket end time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800814 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700815 return;
816 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000817 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000818 int64_t nextBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
819 flushCurrentBucketLocked(eventTimeNs, nextBucketStartTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000820}
David Chen27785a82018-01-19 17:06:45 -0800821
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000822int64_t ValueMetricProducer::calcBucketsForwardCount(const int64_t& eventTimeNs) const {
823 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
824 if (eventTimeNs < currentBucketEndTimeNs) {
825 return 0;
David Chen27785a82018-01-19 17:06:45 -0800826 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000827 return 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800828}
829
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000830void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs,
831 const int64_t& nextBucketStartTimeNs) {
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000832 if (mCondition == ConditionState::kUnknown) {
833 StatsdStats::getInstance().noteBucketUnknownCondition(mMetricId);
834 }
835
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000836 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000837 if (numBucketsForward > 1) {
838 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
839 StatsdStats::getInstance().noteSkippedForwardBuckets(mMetricId);
840 // Something went wrong. Maybe the device was sleeping for a long time. It is better
841 // to mark the current bucket as invalid. The last pull might have been successful through.
842 invalidateCurrentBucketWithoutResetBase();
843 }
844
Chenjie Yub3dda412017-10-24 13:41:59 -0700845 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
846 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700847 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
Chenjie Yu32717c32018-10-20 23:54:48 -0700848 int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs;
Yao Chene6cfb142019-04-08 12:00:01 -0700849 // Close the current bucket.
850 int64_t conditionTrueDuration = mConditionTimer.newBucketStart(bucketEndTime);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000851 bool isBucketLargeEnough = bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs;
852 if (isBucketLargeEnough && !mCurrentBucketIsInvalid) {
David Chen81245fd2018-04-12 14:33:37 -0700853 // The current bucket is large enough to keep.
David Chen81245fd2018-04-12 14:33:37 -0700854 for (const auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000855 ValueBucket bucket = buildPartialBucket(bucketEndTime, slice.second);
Yao Chene6cfb142019-04-08 12:00:01 -0700856 bucket.mConditionTrueNs = conditionTrueDuration;
Chenjie Yu32717c32018-10-20 23:54:48 -0700857 // it will auto create new vector of ValuebucketInfo if the key is not found.
858 if (bucket.valueIndex.size() > 0) {
David Chen81245fd2018-04-12 14:33:37 -0700859 auto& bucketList = mPastBuckets[slice.first];
Chenjie Yu32717c32018-10-20 23:54:48 -0700860 bucketList.push_back(bucket);
David Chen81245fd2018-04-12 14:33:37 -0700861 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700862 }
David Chen81245fd2018-04-12 14:33:37 -0700863 } else {
Chenjie Yu32717c32018-10-20 23:54:48 -0700864 mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700865 }
866
Olivier Gaillardc3719912019-03-15 17:33:40 +0000867 appendToFullBucket(eventTimeNs, fullBucketEndTimeNs);
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000868 initCurrentSlicedBucket(nextBucketStartTimeNs);
Yao Chene6cfb142019-04-08 12:00:01 -0700869 // Update the condition timer again, in case we skipped buckets.
870 mConditionTimer.newBucketStart(nextBucketStartTimeNs);
Olivier Gaillardc3719912019-03-15 17:33:40 +0000871 mCurrentBucketNum += numBucketsForward;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000872}
873
874ValueBucket ValueMetricProducer::buildPartialBucket(int64_t bucketEndTime,
875 const std::vector<Interval>& intervals) {
876 ValueBucket bucket;
877 bucket.mBucketStartNs = mCurrentBucketStartTimeNs;
878 bucket.mBucketEndNs = bucketEndTime;
879 for (const auto& interval : intervals) {
880 if (interval.hasValue) {
881 // skip the output if the diff is zero
882 if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) {
883 continue;
884 }
885 bucket.valueIndex.push_back(interval.valueIndex);
886 if (mAggregationType != ValueMetric::AVG) {
887 bucket.values.push_back(interval.value);
888 } else {
889 double sum = interval.value.type == LONG ? (double)interval.value.long_value
890 : interval.value.double_value;
891 bucket.values.push_back(Value((double)sum / interval.sampleSize));
892 }
893 }
894 }
895 return bucket;
896}
897
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000898void ValueMetricProducer::initCurrentSlicedBucket(int64_t nextBucketStartTimeNs) {
899 StatsdStats::getInstance().noteBucketCount(mMetricId);
900 // Cleanup data structure to aggregate values.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000901 for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) {
902 bool obsolete = true;
903 for (auto& interval : it->second) {
904 interval.hasValue = false;
905 interval.sampleSize = 0;
906 if (interval.seenNewData) {
907 obsolete = false;
908 }
909 interval.seenNewData = false;
910 }
911
912 if (obsolete) {
913 it = mCurrentSlicedBucket.erase(it);
914 } else {
915 it++;
916 }
917 }
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000918
919 mCurrentBucketIsInvalid = false;
920 // If we do not have a global base when the condition is true,
921 // we will have incomplete bucket for the next bucket.
922 if (mUseDiff && !mHasGlobalBase && mCondition) {
923 mCurrentBucketIsInvalid = false;
924 }
925 mCurrentBucketStartTimeNs = nextBucketStartTimeNs;
926 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
927 (long long)mCurrentBucketStartTimeNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000928}
929
930void ValueMetricProducer::appendToFullBucket(int64_t eventTimeNs, int64_t fullBucketEndTimeNs) {
Olivier Gaillardc3719912019-03-15 17:33:40 +0000931 bool isFullBucketReached = eventTimeNs > fullBucketEndTimeNs;
932 if (mCurrentBucketIsInvalid) {
933 if (isFullBucketReached) {
934 // If the bucket is invalid, we ignore the full bucket since it contains invalid data.
935 mCurrentFullBucket.clear();
936 }
937 // Current bucket is invalid, we do not add it to the full bucket.
938 return;
939 }
940
941 if (isFullBucketReached) { // If full bucket, send to anomaly tracker.
David Chen27785a82018-01-19 17:06:45 -0800942 // Accumulate partial buckets with current value and then send to anomaly tracker.
943 if (mCurrentFullBucket.size() > 0) {
944 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yudbe5c502018-11-30 23:15:57 -0800945 if (hitFullBucketGuardRailLocked(slice.first)) {
946 continue;
947 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700948 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +0000949 auto& interval = slice.second[0];
950 if (interval.hasValue) {
951 mCurrentFullBucket[slice.first] += interval.value.long_value;
952 }
David Chen27785a82018-01-19 17:06:45 -0800953 }
954 for (const auto& slice : mCurrentFullBucket) {
955 for (auto& tracker : mAnomalyTrackers) {
956 if (tracker != nullptr) {
957 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
958 }
959 }
960 }
961 mCurrentFullBucket.clear();
962 } else {
963 // Skip aggregating the partial buckets since there's no previous partial bucket.
964 for (const auto& slice : mCurrentSlicedBucket) {
965 for (auto& tracker : mAnomalyTrackers) {
966 if (tracker != nullptr) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700967 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +0000968 auto& interval = slice.second[0];
969 if (interval.hasValue) {
970 tracker->addPastBucket(slice.first, interval.value.long_value,
971 mCurrentBucketNum);
972 }
David Chen27785a82018-01-19 17:06:45 -0800973 }
974 }
975 }
976 }
977 } else {
978 // Accumulate partial bucket.
979 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700980 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +0000981 auto& interval = slice.second[0];
982 if (interval.hasValue) {
983 mCurrentFullBucket[slice.first] += interval.value.long_value;
984 }
David Chen27785a82018-01-19 17:06:45 -0800985 }
986 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700987}
988
Yangsterf2bee6f2017-11-29 12:01:05 -0800989size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800990 size_t totalSize = 0;
991 for (const auto& pair : mPastBuckets) {
992 totalSize += pair.second.size() * kBucketSize;
993 }
994 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800995}
996
Chenjie Yub3dda412017-10-24 13:41:59 -0700997} // namespace statsd
998} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700999} // namespace android