blob: eb78ebc521e1cee85d9babd0533b262de8104461 [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) {
162 pullAndMatchEventsLocked(mCurrentBucketStartTimeNs, mCondition);
163 }
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);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000186 // We are going to flush the data without doing a pull first so we need to invalidte the data.
187 bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue;
188 if (pullNeeded) {
189 invalidateCurrentBucket();
190 }
191 flushIfNeededLocked(dropTimeNs);
192 clearPastBucketsLocked(dropTimeNs);
Yao Chen06dba5d2018-01-26 13:38:16 -0800193}
194
Yangster-maca802d732018-04-24 07:50:38 -0700195void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
Yangster-maca802d732018-04-24 07:50:38 -0700196 mPastBuckets.clear();
197 mSkippedBuckets.clear();
198}
199
Yangster-macb142cc82018-03-30 15:22:08 -0700200void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700201 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700202 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000203 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700204 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800205 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800206 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700207 if (include_current_partial_bucket) {
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000208 // For pull metrics, we need to do a pull at bucket boundaries. If we do not do that the
209 // current bucket will have incomplete data and the next will have the wrong snapshot to do
210 // a diff against. If the condition is false, we are fine since the base data is reset and
211 // we are not tracking anything.
212 bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue;
213 if (pullNeeded) {
214 switch (dumpLatency) {
215 case FAST:
216 invalidateCurrentBucket();
217 break;
218 case NO_TIME_CONSTRAINTS:
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000219 pullAndMatchEventsLocked(dumpTimeNs, mCondition);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000220 break;
221 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000222 }
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000223 flushCurrentBucketLocked(dumpTimeNs, dumpTimeNs);
Yangster-mace68f3a52018-04-04 00:01:43 -0700224 }
Yang Lub4722912018-11-15 11:02:03 -0800225 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800226 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800227
David Chen81245fd2018-04-12 14:33:37 -0700228 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800229 return;
230 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700231 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
232 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
233 // Fills the dimension path if not slicing by ALL.
234 if (!mSliceByPositionALL) {
235 if (!mDimensionsInWhat.empty()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700236 uint64_t dimenPathToken =
237 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700238 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
239 protoOutput->end(dimenPathToken);
240 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700241 }
242
Yi Jin5ee07872018-03-05 18:18:27 -0800243 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000244
David Chen81245fd2018-04-12 14:33:37 -0700245 for (const auto& pair : mSkippedBuckets) {
246 uint64_t wrapperToken =
247 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED);
Yangster-mac9def8e32018-04-17 13:55:51 -0700248 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS,
249 (long long)(NanoToMillis(pair.first)));
250 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS,
251 (long long)(NanoToMillis(pair.second)));
David Chen81245fd2018-04-12 14:33:37 -0700252 protoOutput->end(wrapperToken);
253 }
David Chen81245fd2018-04-12 14:33:37 -0700254
Yao Chen93fe3a32017-11-02 13:52:59 -0700255 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800256 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800257 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800258 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800259 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700260
Yangster-mac20877162017-12-22 17:19:39 -0800261 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700262 if (mSliceByPositionALL) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700263 uint64_t dimensionToken =
264 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac9def8e32018-04-17 13:55:51 -0700265 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
266 protoOutput->end(dimensionToken);
Yangster-mac9def8e32018-04-17 13:55:51 -0700267 } else {
268 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
269 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800270 }
yro2b0f8862017-11-06 14:27:31 -0800271
272 // Then fill bucket_info (ValueBucketInfo).
273 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800274 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800275 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700276
277 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
278 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
279 (long long)NanoToMillis(bucket.mBucketStartNs));
280 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
281 (long long)NanoToMillis(bucket.mBucketEndNs));
282 } else {
283 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
284 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
285 }
Yao Chene6cfb142019-04-08 12:00:01 -0700286 // only write the condition timer value if the metric has a condition.
287 if (mConditionTrackerIndex >= 0) {
288 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_TRUE_NS,
289 (long long)bucket.mConditionTrueNs);
290 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700291 for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) {
292 int index = bucket.valueIndex[i];
293 const Value& value = bucket.values[i];
294 uint64_t valueToken = protoOutput->start(
295 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES);
296 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX,
297 index);
298 if (value.getType() == LONG) {
299 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG,
300 (long long)value.long_value);
301 VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs,
302 (long long)bucket.mBucketEndNs, index, (long long)value.long_value);
303 } else if (value.getType() == DOUBLE) {
304 protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE,
305 value.double_value);
306 VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs,
307 (long long)bucket.mBucketEndNs, index, value.double_value);
308 } else {
309 VLOG("Wrong value type for ValueMetric output: %d", value.getType());
310 }
311 protoOutput->end(valueToken);
Chenjie Yua0f02242018-07-06 16:14:34 -0700312 }
Yao Chen288c6002017-12-12 13:43:18 -0800313 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800314 }
Yao Chen288c6002017-12-12 13:43:18 -0800315 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700316 }
Yao Chen288c6002017-12-12 13:43:18 -0800317 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800318
Yangster-mac94e197c2018-01-02 16:03:03 -0800319 VLOG("metric %lld dump report now...", (long long)mMetricId);
Bookatzff71cad2018-09-20 17:17:49 -0700320 if (erase_data) {
321 mPastBuckets.clear();
322 mSkippedBuckets.clear();
323 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700324}
325
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000326void ValueMetricProducer::invalidateCurrentBucketWithoutResetBase() {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000327 if (!mCurrentBucketIsInvalid) {
328 // Only report once per invalid bucket.
329 StatsdStats::getInstance().noteInvalidatedBucket(mMetricId);
330 }
331 mCurrentBucketIsInvalid = true;
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000332}
333
334void ValueMetricProducer::invalidateCurrentBucket() {
335 invalidateCurrentBucketWithoutResetBase();
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000336 resetBase();
337}
338
Chenjie Yuf275f612018-11-30 23:29:06 -0800339void ValueMetricProducer::resetBase() {
340 for (auto& slice : mCurrentSlicedBucket) {
341 for (auto& interval : slice.second) {
342 interval.hasBase = false;
343 }
344 }
345 mHasGlobalBase = false;
346}
347
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700348// Handle active state change. Active state change is treated like a condition change:
349// - drop bucket if active state change event arrives too late
350// - if condition is true, pull data on active state changes
351// - ConditionTimer tracks changes based on AND of condition and active state.
352void ValueMetricProducer::onActiveStateChangedLocked(const int64_t& eventTimeNs) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000353 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
tsaichristine90f95bb2019-11-06 17:06:53 -0800354 if (isEventTooLate) {
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700355 // Drop bucket because event arrived too late, ie. we are missing data for this bucket.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000356 invalidateCurrentBucket();
Yao Chen2794da22017-12-13 16:01:55 -0800357 }
358
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700359 // Call parent method once we've verified the validity of current bucket.
360 MetricProducer::onActiveStateChangedLocked(eventTimeNs);
361
362 if (ConditionState::kTrue != mCondition) {
363 return;
364 }
365
366 // Pull on active state changes.
367 if (!isEventTooLate) {
368 if (mIsPulled) {
369 pullAndMatchEventsLocked(eventTimeNs, mCondition);
370 }
371 // When active state changes from true to false, clear diff base but don't
372 // reset other counters as we may accumulate more value in the bucket.
373 if (mUseDiff && !mIsActive) {
374 resetBase();
375 }
376 }
377
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700378 flushIfNeededLocked(eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700379
380 // Let condition timer know of new active state.
381 mConditionTimer.onConditionChanged(mIsActive, eventTimeNs);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700382}
383
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700384void ValueMetricProducer::onConditionChangedLocked(const bool condition,
385 const int64_t eventTimeNs) {
386 ConditionState newCondition = condition ? ConditionState::kTrue : ConditionState::kFalse;
387 bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs;
388
tsaichristine90f95bb2019-11-06 17:06:53 -0800389 // If the config is not active, skip the event.
390 if (!mIsActive) {
391 mCondition = isEventTooLate ? ConditionState::kUnknown : newCondition;
392 return;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700393 }
394
tsaichristine90f95bb2019-11-06 17:06:53 -0800395 // If the event arrived late, mark the bucket as invalid and skip the event.
396 if (isEventTooLate) {
397 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
398 (long long)mCurrentBucketStartTimeNs);
399 StatsdStats::getInstance().noteConditionChangeInNextBucket(mMetricId);
400 invalidateCurrentBucket();
401 mCondition = ConditionState::kUnknown;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700402 mConditionTimer.onConditionChanged(mCondition, eventTimeNs);
tsaichristine90f95bb2019-11-06 17:06:53 -0800403 return;
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700404 }
tsaichristine90f95bb2019-11-06 17:06:53 -0800405
406 // If the previous condition was unknown, mark the bucket as invalid
407 // because the bucket will contain partial data. For example, the condition
408 // change might happen close to the end of the bucket and we might miss a
409 // lot of data.
410 //
411 // We still want to pull to set the base.
412 if (mCondition == ConditionState::kUnknown) {
413 invalidateCurrentBucket();
414 }
415
416 // Pull and match for the following condition change cases:
417 // unknown/false -> true - condition changed
418 // true -> false - condition changed
419 // true -> true - old condition was true so we can flush the bucket at the
420 // end if needed.
421 //
422 // We don’t need to pull for unknown -> false or false -> false.
423 //
424 // onConditionChangedLocked might happen on bucket boundaries if this is
425 // called before #onDataPulled.
426 if (mIsPulled &&
427 (newCondition == ConditionState::kTrue || mCondition == ConditionState::kTrue)) {
428 pullAndMatchEventsLocked(eventTimeNs, newCondition);
429 }
430
431 // For metrics that use diff, when condition changes from true to false,
432 // clear diff base but don't reset other counts because we may accumulate
433 // more value in the bucket.
434 if (mUseDiff &&
435 (mCondition == ConditionState::kTrue && newCondition == ConditionState::kFalse)) {
436 resetBase();
437 }
438
439 // Update condition state after pulling.
440 mCondition = newCondition;
441
442 flushIfNeededLocked(eventTimeNs);
443 mConditionTimer.onConditionChanged(mCondition, eventTimeNs);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700444}
445
446void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs,
447 ConditionState condition) {
Chenjie Yue1361ed2018-07-23 17:33:09 -0700448 vector<std::shared_ptr<LogEvent>> allData;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800449 if (!mPullerManager->Pull(mPullTagId, &allData)) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000450 ALOGE("Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000451 invalidateCurrentBucket();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800452 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700453 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000454
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000455 accumulateEvents(allData, timestampNs, timestampNs, condition);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000456}
457
458int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) {
459 return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
460}
461
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000462// By design, statsd pulls data at bucket boundaries using AlarmManager. These pulls are likely
463// to be delayed. Other events like condition changes or app upgrade which are not based on
464// AlarmManager might have arrived earlier and close the bucket.
Olivier Gaillard11203df2019-02-06 13:18:09 +0000465void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData,
466 bool pullSuccess, int64_t originalPullTimeNs) {
467 std::lock_guard<std::mutex> lock(mMutex);
tsaichristine90f95bb2019-11-06 17:06:53 -0800468 if (mCondition == ConditionState::kTrue) {
469 // If the pull failed, we won't be able to compute a diff.
470 if (!pullSuccess) {
471 invalidateCurrentBucket();
472 } else {
473 bool isEventLate = originalPullTimeNs < getCurrentBucketEndTimeNs();
474 if (isEventLate) {
475 // If the event is late, we are in the middle of a bucket. Just
476 // process the data without trying to snap the data to the nearest bucket.
477 accumulateEvents(allData, originalPullTimeNs, originalPullTimeNs, mCondition);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000478 } else {
tsaichristine90f95bb2019-11-06 17:06:53 -0800479 // For scheduled pulled data, the effective event time is snap to the nearest
480 // bucket end. In the case of waking up from a deep sleep state, we will
481 // attribute to the previous bucket end. If the sleep was long but not very
482 // long, we will be in the immediate next bucket. Previous bucket may get a
483 // larger number as we pull at a later time than real bucket end.
484 //
485 // If the sleep was very long, we skip more than one bucket before sleep. In
486 // this case, if the diff base will be cleared and this new data will serve as
487 // new diff base.
488 int64_t bucketEndTime = calcPreviousBucketEndTime(originalPullTimeNs) - 1;
489 StatsdStats::getInstance().noteBucketBoundaryDelayNs(
490 mMetricId, originalPullTimeNs - bucketEndTime);
491 accumulateEvents(allData, originalPullTimeNs, bucketEndTime, mCondition);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000492 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000493 }
tsaichristine90f95bb2019-11-06 17:06:53 -0800494 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000495
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000496 // We can probably flush the bucket. Since we used bucketEndTime when calling
497 // #onMatchedLogEventInternalLocked, the current bucket will not have been flushed.
498 flushIfNeededLocked(originalPullTimeNs);
Olivier Gaillard11203df2019-02-06 13:18:09 +0000499}
500
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000501void ValueMetricProducer::accumulateEvents(const std::vector<std::shared_ptr<LogEvent>>& allData,
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000502 int64_t originalPullTimeNs, int64_t eventElapsedTimeNs,
503 ConditionState condition) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000504 bool isEventLate = eventElapsedTimeNs < mCurrentBucketStartTimeNs;
505 if (isEventLate) {
506 VLOG("Skip bucket end pull due to late arrival: %lld vs %lld",
507 (long long)eventElapsedTimeNs, (long long)mCurrentBucketStartTimeNs);
508 StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId);
509 invalidateCurrentBucket();
510 return;
511 }
512
513 const int64_t pullDelayNs = getElapsedRealtimeNs() - originalPullTimeNs;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000514 StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800515 if (pullDelayNs > mMaxPullDelayNs) {
516 ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId,
517 (long long)mMaxPullDelayNs);
518 StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000519 // We are missing one pull from the bucket which means we will not have a complete view of
520 // what's going on.
521 invalidateCurrentBucket();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800522 return;
523 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800524
Olivier Gaillard11203df2019-02-06 13:18:09 +0000525 if (allData.size() == 0) {
526 VLOG("Data pulled is empty");
527 StatsdStats::getInstance().noteEmptyData(mPullTagId);
Misha Wagner1eee2212019-01-22 11:47:11 +0000528 }
529
Olivier Gaillard11203df2019-02-06 13:18:09 +0000530 mMatchedMetricDimensionKeys.clear();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800531 for (const auto& data : allData) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800532 LogEvent localCopy = data->makeCopy();
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800533 if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) ==
534 MatchingState::kMatched) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000535 localCopy.setElapsedTimestampNs(eventElapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800536 onMatchedLogEventLocked(mWhatMatcherIndex, localCopy);
537 }
538 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000539 // If the new pulled data does not contains some keys we track in our intervals, we need to
540 // reset the base.
541 for (auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000542 bool presentInPulledData = mMatchedMetricDimensionKeys.find(slice.first)
Olivier Gaillard11203df2019-02-06 13:18:09 +0000543 != mMatchedMetricDimensionKeys.end();
544 if (!presentInPulledData) {
545 for (auto& interval : slice.second) {
546 interval.hasBase = false;
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800547 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800548 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700549 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000550 mMatchedMetricDimensionKeys.clear();
551 mHasGlobalBase = true;
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000552
553 // If we reach the guardrail, we might have dropped some data which means the bucket is
554 // incomplete.
555 //
556 // The base also needs to be reset. If we do not have the full data, we might
557 // incorrectly compute the diff when mUseZeroDefaultBase is true since an existing key
558 // might be missing from mCurrentSlicedBucket.
559 if (hasReachedGuardRailLimit()) {
560 invalidateCurrentBucket();
561 mCurrentSlicedBucket.clear();
562 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700563}
564
Yangster-maca78d0082018-03-12 12:02:56 -0700565void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
566 if (mCurrentSlicedBucket.size() == 0) {
567 return;
568 }
569
570 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
571 (unsigned long)mCurrentSlicedBucket.size());
572 if (verbose) {
573 for (const auto& it : mCurrentSlicedBucket) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700574 for (const auto& interval : it.second) {
tsaichristine69000e62019-10-18 17:34:52 -0700575 fprintf(out, "\t(what)%s\t(states)%s (value)%s\n",
576 it.first.getDimensionKeyInWhat().toString().c_str(),
577 it.first.getStateValuesKey().toString().c_str(),
578 interval.value.toString().c_str());
Chenjie Yu32717c32018-10-20 23:54:48 -0700579 }
Yangster-maca78d0082018-03-12 12:02:56 -0700580 }
581 }
582}
583
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000584bool ValueMetricProducer::hasReachedGuardRailLimit() const {
585 return mCurrentSlicedBucket.size() >= mDimensionHardLimit;
586}
587
Yangster-mac93694462018-01-22 20:49:31 -0800588bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800589 // ===========GuardRail==============
590 // 1. Report the tuple count if the tuple count > soft limit
591 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
592 return false;
593 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800594 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800595 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800596 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800597 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Olivier Gaillard1e0d8fc2019-02-11 18:08:43 +0000598 if (hasReachedGuardRailLimit()) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700599 ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId,
600 newKey.toString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000601 StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId);
Yao Chenb3561512017-11-21 18:07:17 -0800602 return true;
603 }
604 }
605
606 return false;
607}
608
Chenjie Yudbe5c502018-11-30 23:15:57 -0800609bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) {
610 // ===========GuardRail==============
611 // 1. Report the tuple count if the tuple count > soft limit
612 if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) {
613 return false;
614 }
615 if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) {
616 size_t newTupleCount = mCurrentFullBucket.size() + 1;
617 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
618 if (newTupleCount > mDimensionHardLimit) {
619 ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s",
620 (long long)mMetricId,
621 newKey.toString().c_str());
622 return true;
623 }
624 }
625
626 return false;
627}
628
Chenjie Yu32717c32018-10-20 23:54:48 -0700629bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) {
630 for (const FieldValue& value : event.getValues()) {
631 if (value.mField.matches(matcher)) {
632 switch (value.mValue.type) {
633 case INT:
634 ret.setLong(value.mValue.int_value);
635 break;
636 case LONG:
637 ret.setLong(value.mValue.long_value);
638 break;
639 case FLOAT:
640 ret.setDouble(value.mValue.float_value);
641 break;
642 case DOUBLE:
643 ret.setDouble(value.mValue.double_value);
644 break;
645 default:
tsaichristine409468d2019-10-28 11:32:31 -0700646 return false;
Chenjie Yu32717c32018-10-20 23:54:48 -0700647 break;
648 }
649 return true;
650 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700651 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700652 return false;
Chenjie Yua0f02242018-07-06 16:14:34 -0700653}
654
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700655void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex,
656 const MetricDimensionKey& eventKey,
657 const ConditionKey& conditionKey,
658 bool condition, const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700659 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000660 if (eventTimeNs < mCurrentBucketStartTimeNs) {
661 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
662 (long long)mCurrentBucketStartTimeNs);
663 return;
664 }
Olivier Gaillard11203df2019-02-06 13:18:09 +0000665 mMatchedMetricDimensionKeys.insert(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000666
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000667 if (!mIsPulled) {
668 // We cannot flush without doing a pull first.
669 flushIfNeededLocked(eventTimeNs);
670 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800671
Olivier Gaillardfbee9162019-04-11 11:48:01 +0100672 // We should not accumulate the data for pushed metrics when the condition is false.
673 bool shouldSkipForPushMetric = !mIsPulled && !condition;
674 // For pulled metrics, there are two cases:
675 // - to compute diffs, we need to process all the state changes
676 // - for non-diffs metrics, we should ignore the data if the condition wasn't true. If we have a
677 // state change from
678 // + True -> True: we should process the data, it might be a bucket boundary
679 // + True -> False: we als need to process the data.
680 bool shouldSkipForPulledMetric = mIsPulled && !mUseDiff
681 && mCondition != ConditionState::kTrue;
682 if (shouldSkipForPushMetric || shouldSkipForPulledMetric) {
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700683 VLOG("ValueMetric skip event because condition is false");
684 return;
685 }
686
Yangsterf2bee6f2017-11-29 12:01:05 -0800687 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800688 return;
689 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700690 vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey];
691 if (multiIntervals.size() < mFieldMatchers.size()) {
692 VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size());
693 multiIntervals.resize(mFieldMatchers.size());
Yangster-maca7fb12d2018-01-03 17:17:20 -0800694 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000695
Misha Wagner26531762019-01-21 14:18:51 +0000696 // We only use anomaly detection under certain cases.
697 // N.B.: The anomaly detection cases were modified in order to fix an issue with value metrics
698 // containing multiple values. We tried to retain all previous behaviour, but we are unsure the
699 // previous behaviour was correct. At the time of the fix, anomaly detection had no owner.
700 // Whoever next works on it should look into the cases where it is triggered in this function.
701 // Discussion here: http://ag/6124370.
702 bool useAnomalyDetection = true;
703
Chenjie Yu32717c32018-10-20 23:54:48 -0700704 for (int i = 0; i < (int)mFieldMatchers.size(); i++) {
705 const Matcher& matcher = mFieldMatchers[i];
706 Interval& interval = multiIntervals[i];
707 interval.valueIndex = i;
708 Value value;
709 if (!getDoubleOrLong(event, matcher, value)) {
710 VLOG("Failed to get value %d from event %s", i, event.ToString().c_str());
Misha Wagner1eee2212019-01-22 11:47:11 +0000711 StatsdStats::getInstance().noteBadValueType(mMetricId);
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700712 return;
713 }
Chenjie Yudbe5c502018-11-30 23:15:57 -0800714 interval.seenNewData = true;
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700715
Chenjie Yu32717c32018-10-20 23:54:48 -0700716 if (mUseDiff) {
Chenjie Yu32717c32018-10-20 23:54:48 -0700717 if (!interval.hasBase) {
Chenjie Yuf275f612018-11-30 23:29:06 -0800718 if (mHasGlobalBase && mUseZeroDefaultBase) {
719 // The bucket has global base. This key does not.
720 // Optionally use zero as base.
721 interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE);
722 interval.hasBase = true;
723 } else {
724 // no base. just update base and return.
725 interval.base = value;
726 interval.hasBase = true;
Misha Wagner26531762019-01-21 14:18:51 +0000727 // If we're missing a base, do not use anomaly detection on incomplete data
728 useAnomalyDetection = false;
729 // Continue (instead of return) here in order to set interval.base and
730 // interval.hasBase for other intervals
731 continue;
Chenjie Yuf275f612018-11-30 23:29:06 -0800732 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700733 }
734 Value diff;
735 switch (mValueDirection) {
736 case ValueMetric::INCREASING:
737 if (value >= interval.base) {
738 diff = value - interval.base;
739 } else if (mUseAbsoluteValueOnReset) {
740 diff = value;
741 } else {
742 VLOG("Unexpected decreasing value");
743 StatsdStats::getInstance().notePullDataError(mPullTagId);
744 interval.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000745 // If we've got bad data, do not use anomaly detection
746 useAnomalyDetection = false;
747 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700748 }
749 break;
750 case ValueMetric::DECREASING:
751 if (interval.base >= value) {
752 diff = interval.base - value;
753 } else if (mUseAbsoluteValueOnReset) {
754 diff = value;
755 } else {
756 VLOG("Unexpected increasing value");
757 StatsdStats::getInstance().notePullDataError(mPullTagId);
758 interval.base = value;
Misha Wagner26531762019-01-21 14:18:51 +0000759 // If we've got bad data, do not use anomaly detection
760 useAnomalyDetection = false;
761 continue;
Chenjie Yu32717c32018-10-20 23:54:48 -0700762 }
763 break;
764 case ValueMetric::ANY:
765 diff = value - interval.base;
766 break;
767 default:
768 break;
769 }
770 interval.base = value;
771 value = diff;
Yao Chen6a8c7992017-11-29 20:02:07 +0000772 }
Chenjie Yu32717c32018-10-20 23:54:48 -0700773
774 if (interval.hasValue) {
775 switch (mAggregationType) {
776 case ValueMetric::SUM:
777 // for AVG, we add up and take average when flushing the bucket
778 case ValueMetric::AVG:
779 interval.value += value;
780 break;
781 case ValueMetric::MIN:
782 interval.value = std::min(value, interval.value);
783 break;
784 case ValueMetric::MAX:
785 interval.value = std::max(value, interval.value);
786 break;
787 default:
788 break;
789 }
790 } else {
791 interval.value = value;
792 interval.hasValue = true;
793 }
794 interval.sampleSize += 1;
Yangster8de69392017-11-27 13:48:29 -0800795 }
Bookatzde1b55622017-12-14 18:38:27 -0800796
Misha Wagner26531762019-01-21 14:18:51 +0000797 // Only trigger the tracker if all intervals are correct
798 if (useAnomalyDetection) {
799 // TODO: propgate proper values down stream when anomaly support doubles
800 long wholeBucketVal = multiIntervals[0].value.long_value;
801 auto prev = mCurrentFullBucket.find(eventKey);
802 if (prev != mCurrentFullBucket.end()) {
803 wholeBucketVal += prev->second;
804 }
805 for (auto& tracker : mAnomalyTrackers) {
Yao Chen4ce07292019-02-13 13:06:36 -0800806 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey,
807 wholeBucketVal);
Misha Wagner26531762019-01-21 14:18:51 +0000808 }
Bookatzde1b55622017-12-14 18:38:27 -0800809 }
Yangster8de69392017-11-27 13:48:29 -0800810}
811
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000812// For pulled metrics, we always need to make sure we do a pull before flushing the bucket
813// if mCondition is true!
Yangster-macb142cc82018-03-30 15:22:08 -0700814void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
815 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700816 if (eventTimeNs < currentBucketEndTimeNs) {
tsaichristine90f95bb2019-11-06 17:06:53 -0800817 VLOG("eventTime is %lld, less than current bucket end time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800818 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700819 return;
820 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000821 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000822 int64_t nextBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
823 flushCurrentBucketLocked(eventTimeNs, nextBucketStartTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000824}
David Chen27785a82018-01-19 17:06:45 -0800825
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000826int64_t ValueMetricProducer::calcBucketsForwardCount(const int64_t& eventTimeNs) const {
827 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
828 if (eventTimeNs < currentBucketEndTimeNs) {
829 return 0;
David Chen27785a82018-01-19 17:06:45 -0800830 }
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000831 return 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800832}
833
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000834void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs,
835 const int64_t& nextBucketStartTimeNs) {
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000836 if (mCondition == ConditionState::kUnknown) {
837 StatsdStats::getInstance().noteBucketUnknownCondition(mMetricId);
838 }
839
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000840 int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs);
Olivier Gaillard47a9efc2019-02-22 15:43:31 +0000841 if (numBucketsForward > 1) {
842 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
843 StatsdStats::getInstance().noteSkippedForwardBuckets(mMetricId);
844 // Something went wrong. Maybe the device was sleeping for a long time. It is better
845 // to mark the current bucket as invalid. The last pull might have been successful through.
846 invalidateCurrentBucketWithoutResetBase();
847 }
848
Chenjie Yub3dda412017-10-24 13:41:59 -0700849 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
850 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700851 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
Chenjie Yu32717c32018-10-20 23:54:48 -0700852 int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs;
Yao Chene6cfb142019-04-08 12:00:01 -0700853 // Close the current bucket.
854 int64_t conditionTrueDuration = mConditionTimer.newBucketStart(bucketEndTime);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000855 bool isBucketLargeEnough = bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs;
856 if (isBucketLargeEnough && !mCurrentBucketIsInvalid) {
David Chen81245fd2018-04-12 14:33:37 -0700857 // The current bucket is large enough to keep.
David Chen81245fd2018-04-12 14:33:37 -0700858 for (const auto& slice : mCurrentSlicedBucket) {
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000859 ValueBucket bucket = buildPartialBucket(bucketEndTime, slice.second);
Yao Chene6cfb142019-04-08 12:00:01 -0700860 bucket.mConditionTrueNs = conditionTrueDuration;
Chenjie Yu32717c32018-10-20 23:54:48 -0700861 // it will auto create new vector of ValuebucketInfo if the key is not found.
862 if (bucket.valueIndex.size() > 0) {
David Chen81245fd2018-04-12 14:33:37 -0700863 auto& bucketList = mPastBuckets[slice.first];
Chenjie Yu32717c32018-10-20 23:54:48 -0700864 bucketList.push_back(bucket);
David Chen81245fd2018-04-12 14:33:37 -0700865 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700866 }
David Chen81245fd2018-04-12 14:33:37 -0700867 } else {
Chenjie Yu32717c32018-10-20 23:54:48 -0700868 mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700869 }
870
Olivier Gaillardc3719912019-03-15 17:33:40 +0000871 appendToFullBucket(eventTimeNs, fullBucketEndTimeNs);
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000872 initCurrentSlicedBucket(nextBucketStartTimeNs);
Yao Chene6cfb142019-04-08 12:00:01 -0700873 // Update the condition timer again, in case we skipped buckets.
874 mConditionTimer.newBucketStart(nextBucketStartTimeNs);
Olivier Gaillardc3719912019-03-15 17:33:40 +0000875 mCurrentBucketNum += numBucketsForward;
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000876}
877
878ValueBucket ValueMetricProducer::buildPartialBucket(int64_t bucketEndTime,
879 const std::vector<Interval>& intervals) {
880 ValueBucket bucket;
881 bucket.mBucketStartNs = mCurrentBucketStartTimeNs;
882 bucket.mBucketEndNs = bucketEndTime;
883 for (const auto& interval : intervals) {
884 if (interval.hasValue) {
885 // skip the output if the diff is zero
886 if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) {
887 continue;
888 }
889 bucket.valueIndex.push_back(interval.valueIndex);
890 if (mAggregationType != ValueMetric::AVG) {
891 bucket.values.push_back(interval.value);
892 } else {
893 double sum = interval.value.type == LONG ? (double)interval.value.long_value
894 : interval.value.double_value;
895 bucket.values.push_back(Value((double)sum / interval.sampleSize));
896 }
897 }
898 }
899 return bucket;
900}
901
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000902void ValueMetricProducer::initCurrentSlicedBucket(int64_t nextBucketStartTimeNs) {
903 StatsdStats::getInstance().noteBucketCount(mMetricId);
904 // Cleanup data structure to aggregate values.
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000905 for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) {
906 bool obsolete = true;
907 for (auto& interval : it->second) {
908 interval.hasValue = false;
909 interval.sampleSize = 0;
910 if (interval.seenNewData) {
911 obsolete = false;
912 }
913 interval.seenNewData = false;
914 }
915
916 if (obsolete) {
917 it = mCurrentSlicedBucket.erase(it);
918 } else {
919 it++;
920 }
921 }
Olivier Gaillarda8b70112019-02-25 11:24:23 +0000922
923 mCurrentBucketIsInvalid = false;
924 // If we do not have a global base when the condition is true,
925 // we will have incomplete bucket for the next bucket.
926 if (mUseDiff && !mHasGlobalBase && mCondition) {
927 mCurrentBucketIsInvalid = false;
928 }
929 mCurrentBucketStartTimeNs = nextBucketStartTimeNs;
930 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
931 (long long)mCurrentBucketStartTimeNs);
Olivier Gaillard9a5d3592019-02-05 15:12:39 +0000932}
933
934void ValueMetricProducer::appendToFullBucket(int64_t eventTimeNs, int64_t fullBucketEndTimeNs) {
Olivier Gaillardc3719912019-03-15 17:33:40 +0000935 bool isFullBucketReached = eventTimeNs > fullBucketEndTimeNs;
936 if (mCurrentBucketIsInvalid) {
937 if (isFullBucketReached) {
938 // If the bucket is invalid, we ignore the full bucket since it contains invalid data.
939 mCurrentFullBucket.clear();
940 }
941 // Current bucket is invalid, we do not add it to the full bucket.
942 return;
943 }
944
945 if (isFullBucketReached) { // If full bucket, send to anomaly tracker.
David Chen27785a82018-01-19 17:06:45 -0800946 // Accumulate partial buckets with current value and then send to anomaly tracker.
947 if (mCurrentFullBucket.size() > 0) {
948 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yudbe5c502018-11-30 23:15:57 -0800949 if (hitFullBucketGuardRailLocked(slice.first)) {
950 continue;
951 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700952 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +0000953 auto& interval = slice.second[0];
954 if (interval.hasValue) {
955 mCurrentFullBucket[slice.first] += interval.value.long_value;
956 }
David Chen27785a82018-01-19 17:06:45 -0800957 }
958 for (const auto& slice : mCurrentFullBucket) {
959 for (auto& tracker : mAnomalyTrackers) {
960 if (tracker != nullptr) {
961 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
962 }
963 }
964 }
965 mCurrentFullBucket.clear();
966 } else {
967 // Skip aggregating the partial buckets since there's no previous partial bucket.
968 for (const auto& slice : mCurrentSlicedBucket) {
969 for (auto& tracker : mAnomalyTrackers) {
970 if (tracker != nullptr) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700971 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +0000972 auto& interval = slice.second[0];
973 if (interval.hasValue) {
974 tracker->addPastBucket(slice.first, interval.value.long_value,
975 mCurrentBucketNum);
976 }
David Chen27785a82018-01-19 17:06:45 -0800977 }
978 }
979 }
980 }
981 } else {
982 // Accumulate partial bucket.
983 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yua0f02242018-07-06 16:14:34 -0700984 // TODO: fix this when anomaly can accept double values
Olivier Gaillardc3719912019-03-15 17:33:40 +0000985 auto& interval = slice.second[0];
986 if (interval.hasValue) {
987 mCurrentFullBucket[slice.first] += interval.value.long_value;
988 }
David Chen27785a82018-01-19 17:06:45 -0800989 }
990 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700991}
992
Yangsterf2bee6f2017-11-29 12:01:05 -0800993size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800994 size_t totalSize = 0;
995 for (const auto& pair : mPastBuckets) {
996 totalSize += pair.second.size() * kBucketSize;
997 }
998 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800999}
1000
Chenjie Yub3dda412017-10-24 13:41:59 -07001001} // namespace statsd
1002} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -07001003} // namespace android