Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
| 20 | #include "ValueMetricProducer.h" |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 21 | #include "../guardrail/StatsdStats.h" |
| 22 | #include "../stats_log_util.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 23 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 27 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 28 | using android::util::FIELD_TYPE_BOOL; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 29 | using android::util::FIELD_TYPE_DOUBLE; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 30 | using android::util::FIELD_TYPE_INT32; |
| 31 | using android::util::FIELD_TYPE_INT64; |
| 32 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 33 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 34 | using android::util::ProtoOutputStream; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 35 | using std::map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 36 | using std::shared_ptr; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 37 | using std::unordered_map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace os { |
| 41 | namespace statsd { |
| 42 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 43 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 44 | const int FIELD_ID_ID = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 45 | const int FIELD_ID_VALUE_METRICS = 7; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 46 | const int FIELD_ID_TIME_BASE = 9; |
| 47 | const int FIELD_ID_BUCKET_SIZE = 10; |
| 48 | const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11; |
Howard Ro | 9440e09 | 2018-12-16 19:15:21 -0800 | [diff] [blame] | 49 | const int FIELD_ID_IS_ACTIVE = 14; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 50 | // for ValueMetricDataWrapper |
| 51 | const int FIELD_ID_DATA = 1; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 52 | const int FIELD_ID_SKIPPED = 2; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 53 | // for SkippedBuckets |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 54 | const int FIELD_ID_SKIPPED_START_MILLIS = 3; |
| 55 | const int FIELD_ID_SKIPPED_END_MILLIS = 4; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 56 | const int FIELD_ID_SKIPPED_DROP_EVENT = 5; |
| 57 | // for DumpEvent Proto |
| 58 | const int FIELD_ID_BUCKET_DROP_REASON = 1; |
| 59 | const int FIELD_ID_DROP_TIME = 2; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | // for ValueMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 61 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 62 | const int FIELD_ID_BUCKET_INFO = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 63 | const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4; |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 64 | const int FIELD_ID_SLICE_BY_STATE = 6; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 65 | // for ValueBucketInfo |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 66 | const int FIELD_ID_VALUE_INDEX = 1; |
| 67 | const int FIELD_ID_VALUE_LONG = 2; |
| 68 | const int FIELD_ID_VALUE_DOUBLE = 3; |
| 69 | const int FIELD_ID_VALUES = 9; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 70 | const int FIELD_ID_BUCKET_NUM = 4; |
| 71 | const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5; |
| 72 | const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6; |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 73 | const int FIELD_ID_CONDITION_TRUE_NS = 10; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 74 | |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 75 | const Value ZERO_LONG((int64_t)0); |
| 76 | const Value ZERO_DOUBLE((int64_t)0); |
| 77 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 78 | // ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 79 | ValueMetricProducer::ValueMetricProducer( |
| 80 | const ConfigKey& key, const ValueMetric& metric, const int conditionIndex, |
| 81 | const sp<ConditionWizard>& conditionWizard, const int whatMatcherIndex, |
| 82 | const sp<EventMatcherWizard>& matcherWizard, const int pullTagId, const int64_t timeBaseNs, |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 83 | const int64_t startTimeNs, const sp<StatsPullerManager>& pullerManager, |
| 84 | const unordered_map<int, shared_ptr<Activation>>& eventActivationMap, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 85 | const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap, |
| 86 | const vector<int>& slicedStateAtoms, |
| 87 | const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap) |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 88 | : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, conditionWizard, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 89 | eventActivationMap, eventDeactivationMap, slicedStateAtoms, stateGroupMap), |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 90 | mWhatMatcherIndex(whatMatcherIndex), |
| 91 | mEventMatcherWizard(matcherWizard), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 92 | mPullerManager(pullerManager), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 93 | mPullTagId(pullTagId), |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 94 | mIsPulled(pullTagId != -1), |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 95 | mMinBucketSizeNs(metric.min_bucket_size_nanos()), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 96 | mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 97 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 98 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first |
| 99 | : StatsdStats::kDimensionKeySizeSoftLimit), |
| 100 | mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 101 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 102 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second |
Chenjie Yu | 4723464 | 2018-05-14 10:14:16 -0700 | [diff] [blame] | 103 | : StatsdStats::kDimensionKeySizeHardLimit), |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 104 | mUseAbsoluteValueOnReset(metric.use_absolute_value_on_reset()), |
| 105 | mAggregationType(metric.aggregation_type()), |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 106 | mUseDiff(metric.has_use_diff() ? metric.use_diff() : (mIsPulled ? true : false)), |
| 107 | mValueDirection(metric.value_direction()), |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 108 | mSkipZeroDiffOutput(metric.skip_zero_diff_output()), |
| 109 | mUseZeroDefaultBase(metric.use_zero_default_base()), |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 110 | mHasGlobalBase(false), |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 111 | mCurrentBucketIsSkipped(false), |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 112 | mMaxPullDelayNs(metric.max_pull_delay_sec() > 0 ? metric.max_pull_delay_sec() * NS_PER_SEC |
Chenjie Yu | cd1b797 | 2019-01-16 20:38:15 -0800 | [diff] [blame] | 113 | : StatsdStats::kPullMaxDelayNs), |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 114 | mSplitBucketForAppUpgrade(metric.split_bucket_for_app_upgrade()), |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 115 | // Condition timer will be set later within the constructor after pulling events |
Tej Singh | ee4495e | 2019-06-03 18:37:35 -0700 | [diff] [blame] | 116 | mConditionTimer(false, timeBaseNs) { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 117 | int64_t bucketSizeMills = 0; |
| 118 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 119 | bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 120 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 121 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 122 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 123 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 124 | mBucketSizeNs = bucketSizeMills * 1000000; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 125 | |
| 126 | translateFieldMatcher(metric.value_field(), &mFieldMatchers); |
| 127 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 128 | if (metric.has_dimensions_in_what()) { |
| 129 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 130 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 131 | mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 134 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 135 | for (const auto& link : metric.links()) { |
| 136 | Metric2Condition mc; |
| 137 | mc.conditionId = link.condition(); |
| 138 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 139 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 140 | mMetric2ConditionLinks.push_back(mc); |
| 141 | } |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 142 | mConditionSliced = true; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 143 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 144 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 145 | for (const auto& stateLink : metric.state_link()) { |
| 146 | Metric2State ms; |
| 147 | ms.stateAtomId = stateLink.state_atom_id(); |
| 148 | translateFieldMatcher(stateLink.fields_in_what(), &ms.metricFields); |
| 149 | translateFieldMatcher(stateLink.fields_in_state(), &ms.stateFields); |
| 150 | mMetric2StateLinks.push_back(ms); |
| 151 | } |
| 152 | |
Tej Singh | 597c716 | 2019-04-17 16:41:45 -0700 | [diff] [blame] | 153 | int64_t numBucketsForward = calcBucketsForwardCount(startTimeNs); |
| 154 | mCurrentBucketNum += numBucketsForward; |
| 155 | |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 156 | flushIfNeededLocked(startTimeNs); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 157 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 158 | if (mIsPulled) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 159 | mPullerManager->RegisterReceiver(mPullTagId, mConfigKey, this, getCurrentBucketEndTimeNs(), |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 160 | mBucketSizeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 161 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 162 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 163 | // Only do this for partial buckets like first bucket. All other buckets should use |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 164 | // flushIfNeeded to adjust start and end to bucket boundaries. |
| 165 | // Adjust start for partial bucket |
| 166 | mCurrentBucketStartTimeNs = startTimeNs; |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 167 | mConditionTimer.newBucketStart(mCurrentBucketStartTimeNs); |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 168 | |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 169 | // Now that activations are processed, start the condition timer if needed. |
| 170 | mConditionTimer.onConditionChanged(mIsActive && mCondition == ConditionState::kTrue, |
| 171 | mCurrentBucketStartTimeNs); |
| 172 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 173 | VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
| 174 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | ValueMetricProducer::~ValueMetricProducer() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 178 | VLOG("~ValueMetricProducer() called"); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 179 | if (mIsPulled) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 180 | mPullerManager->UnRegisterReceiver(mPullTagId, mConfigKey, this); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 181 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 182 | } |
| 183 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 184 | void ValueMetricProducer::onStateChanged(int64_t eventTimeNs, int32_t atomId, |
tsaichristine | 9f95105 | 2020-05-13 14:32:37 -0700 | [diff] [blame] | 185 | const HashableDimensionKey& primaryKey, |
| 186 | const FieldValue& oldState, const FieldValue& newState) { |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 187 | VLOG("ValueMetric %lld onStateChanged time %lld, State %d, key %s, %d -> %d", |
| 188 | (long long)mMetricId, (long long)eventTimeNs, atomId, primaryKey.toString().c_str(), |
tsaichristine | 9f95105 | 2020-05-13 14:32:37 -0700 | [diff] [blame] | 189 | oldState.mValue.int_value, newState.mValue.int_value); |
tsaichristine | 0f6c1bc | 2020-04-28 10:42:25 -0700 | [diff] [blame] | 190 | // If condition is not true or metric is not active, we do not need to pull |
| 191 | // for this state change. |
| 192 | if (mCondition != ConditionState::kTrue || !mIsActive) { |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 193 | return; |
| 194 | } |
tsaichristine | 9f95105 | 2020-05-13 14:32:37 -0700 | [diff] [blame] | 195 | |
| 196 | // If old and new states are in the same StateGroup, then we do not need to |
| 197 | // pull for this state change. |
| 198 | FieldValue oldStateCopy = oldState; |
| 199 | FieldValue newStateCopy = newState; |
| 200 | mapStateValue(atomId, &oldStateCopy); |
| 201 | mapStateValue(atomId, &newStateCopy); |
| 202 | if (oldStateCopy == newStateCopy) { |
| 203 | return; |
| 204 | } |
| 205 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 206 | bool isEventLate = eventTimeNs < mCurrentBucketStartTimeNs; |
| 207 | if (isEventLate) { |
| 208 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 209 | (long long)mCurrentBucketStartTimeNs); |
| 210 | invalidateCurrentBucket(eventTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET); |
| 211 | return; |
| 212 | } |
| 213 | mStateChangePrimaryKey.first = atomId; |
| 214 | mStateChangePrimaryKey.second = primaryKey; |
| 215 | if (mIsPulled) { |
| 216 | pullAndMatchEventsLocked(eventTimeNs); |
| 217 | } |
| 218 | mStateChangePrimaryKey.first = 0; |
| 219 | mStateChangePrimaryKey.second = DEFAULT_DIMENSION_KEY; |
| 220 | flushIfNeededLocked(eventTimeNs); |
| 221 | } |
| 222 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 223 | void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 224 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 225 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 228 | void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Olivier Gaillard | 320952b | 2019-02-06 13:57:24 +0000 | [diff] [blame] | 229 | StatsdStats::getInstance().noteBucketDropped(mMetricId); |
tsaichristine | b7fcf00 | 2019-12-06 18:40:47 -0800 | [diff] [blame] | 230 | |
| 231 | // The current partial bucket is not flushed and does not require a pull, |
| 232 | // so the data is still valid. |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 233 | flushIfNeededLocked(dropTimeNs); |
| 234 | clearPastBucketsLocked(dropTimeNs); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 237 | void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 238 | mPastBuckets.clear(); |
| 239 | mSkippedBuckets.clear(); |
| 240 | } |
| 241 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 242 | void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 243 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 244 | const bool erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 245 | const DumpLatency dumpLatency, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 246 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 247 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 248 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 249 | if (include_current_partial_bucket) { |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 250 | // For pull metrics, we need to do a pull at bucket boundaries. If we do not do that the |
| 251 | // current bucket will have incomplete data and the next will have the wrong snapshot to do |
| 252 | // a diff against. If the condition is false, we are fine since the base data is reset and |
| 253 | // we are not tracking anything. |
| 254 | bool pullNeeded = mIsPulled && mCondition == ConditionState::kTrue; |
| 255 | if (pullNeeded) { |
| 256 | switch (dumpLatency) { |
| 257 | case FAST: |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 258 | invalidateCurrentBucket(dumpTimeNs, BucketDropReason::DUMP_REPORT_REQUESTED); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 259 | break; |
| 260 | case NO_TIME_CONSTRAINTS: |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 261 | pullAndMatchEventsLocked(dumpTimeNs); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 262 | break; |
| 263 | } |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 264 | } |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 265 | flushCurrentBucketLocked(dumpTimeNs, dumpTimeNs); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 266 | } |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 267 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Howard Ro | 07e23ff | 2018-12-17 17:28:07 -0800 | [diff] [blame] | 268 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked()); |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 269 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 270 | if (mPastBuckets.empty() && mSkippedBuckets.empty()) { |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 271 | return; |
| 272 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 273 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs); |
| 274 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs); |
| 275 | // Fills the dimension path if not slicing by ALL. |
| 276 | if (!mSliceByPositionALL) { |
| 277 | if (!mDimensionsInWhat.empty()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 278 | uint64_t dimenPathToken = |
| 279 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 280 | writeDimensionPathToProto(mDimensionsInWhat, protoOutput); |
| 281 | protoOutput->end(dimenPathToken); |
| 282 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 285 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 286 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 287 | for (const auto& skippedBucket : mSkippedBuckets) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 288 | uint64_t wrapperToken = |
| 289 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 290 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS, |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 291 | (long long)(NanoToMillis(skippedBucket.bucketStartTimeNs))); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 292 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS, |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 293 | (long long)(NanoToMillis(skippedBucket.bucketEndTimeNs))); |
| 294 | for (const auto& dropEvent : skippedBucket.dropEvents) { |
| 295 | uint64_t dropEventToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 296 | FIELD_ID_SKIPPED_DROP_EVENT); |
| 297 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_BUCKET_DROP_REASON, dropEvent.reason); |
| 298 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DROP_TIME, |
| 299 | (long long)(NanoToMillis(dropEvent.dropTimeNs))); |
| 300 | ; |
| 301 | protoOutput->end(dropEventToken); |
| 302 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 303 | protoOutput->end(wrapperToken); |
| 304 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 305 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 306 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 307 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 308 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 309 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 310 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 311 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 312 | // First fill dimension. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 313 | if (mSliceByPositionALL) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 314 | uint64_t dimensionToken = |
| 315 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 316 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput); |
| 317 | protoOutput->end(dimensionToken); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 318 | } else { |
| 319 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(), |
| 320 | FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 321 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 322 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 323 | // Then fill slice_by_state. |
| 324 | for (auto state : dimensionKey.getStateValuesKey().getValues()) { |
| 325 | uint64_t stateToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 326 | FIELD_ID_SLICE_BY_STATE); |
| 327 | writeStateToProto(state, protoOutput); |
| 328 | protoOutput->end(stateToken); |
| 329 | } |
| 330 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 331 | // Then fill bucket_info (ValueBucketInfo). |
| 332 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 333 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 334 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 335 | |
| 336 | if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) { |
| 337 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS, |
| 338 | (long long)NanoToMillis(bucket.mBucketStartNs)); |
| 339 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS, |
| 340 | (long long)NanoToMillis(bucket.mBucketEndNs)); |
| 341 | } else { |
| 342 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM, |
| 343 | (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs))); |
| 344 | } |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 345 | // only write the condition timer value if the metric has a condition. |
| 346 | if (mConditionTrackerIndex >= 0) { |
| 347 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_TRUE_NS, |
| 348 | (long long)bucket.mConditionTrueNs); |
| 349 | } |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 350 | for (int i = 0; i < (int)bucket.valueIndex.size(); i++) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 351 | int index = bucket.valueIndex[i]; |
| 352 | const Value& value = bucket.values[i]; |
| 353 | uint64_t valueToken = protoOutput->start( |
| 354 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES); |
| 355 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX, |
| 356 | index); |
| 357 | if (value.getType() == LONG) { |
| 358 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG, |
| 359 | (long long)value.long_value); |
| 360 | VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs, |
| 361 | (long long)bucket.mBucketEndNs, index, (long long)value.long_value); |
| 362 | } else if (value.getType() == DOUBLE) { |
| 363 | protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE, |
| 364 | value.double_value); |
| 365 | VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs, |
| 366 | (long long)bucket.mBucketEndNs, index, value.double_value); |
| 367 | } else { |
| 368 | VLOG("Wrong value type for ValueMetric output: %d", value.getType()); |
| 369 | } |
| 370 | protoOutput->end(valueToken); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 371 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 372 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 373 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 374 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 375 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 376 | protoOutput->end(protoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 377 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 378 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 379 | if (erase_data) { |
| 380 | mPastBuckets.clear(); |
| 381 | mSkippedBuckets.clear(); |
| 382 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 383 | } |
| 384 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 385 | void ValueMetricProducer::invalidateCurrentBucketWithoutResetBase(const int64_t dropTimeNs, |
| 386 | const BucketDropReason reason) { |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 387 | if (!mCurrentBucketIsSkipped) { |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 388 | // Only report to StatsdStats once per invalid bucket. |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 389 | StatsdStats::getInstance().noteInvalidatedBucket(mMetricId); |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 392 | skipCurrentBucket(dropTimeNs, reason); |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 393 | } |
| 394 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 395 | void ValueMetricProducer::invalidateCurrentBucket(const int64_t dropTimeNs, |
| 396 | const BucketDropReason reason) { |
| 397 | invalidateCurrentBucketWithoutResetBase(dropTimeNs, reason); |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 398 | resetBase(); |
| 399 | } |
| 400 | |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 401 | void ValueMetricProducer::skipCurrentBucket(const int64_t dropTimeNs, |
| 402 | const BucketDropReason reason) { |
| 403 | if (!maxDropEventsReached()) { |
| 404 | mCurrentSkippedBucket.dropEvents.emplace_back(buildDropEvent(dropTimeNs, reason)); |
| 405 | } |
| 406 | mCurrentBucketIsSkipped = true; |
| 407 | } |
| 408 | |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 409 | void ValueMetricProducer::resetBase() { |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 410 | for (auto& slice : mCurrentBaseInfo) { |
| 411 | for (auto& baseInfo : slice.second) { |
| 412 | baseInfo.hasBase = false; |
| 413 | baseInfo.hasCurrentState = false; |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | mHasGlobalBase = false; |
| 417 | } |
| 418 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 419 | // Handle active state change. Active state change is treated like a condition change: |
| 420 | // - drop bucket if active state change event arrives too late |
| 421 | // - if condition is true, pull data on active state changes |
| 422 | // - ConditionTimer tracks changes based on AND of condition and active state. |
| 423 | void ValueMetricProducer::onActiveStateChangedLocked(const int64_t& eventTimeNs) { |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 424 | bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs; |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 425 | if (isEventTooLate) { |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 426 | // Drop bucket because event arrived too late, ie. we are missing data for this bucket. |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 427 | StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId); |
| 428 | invalidateCurrentBucket(eventTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 431 | // Call parent method once we've verified the validity of current bucket. |
| 432 | MetricProducer::onActiveStateChangedLocked(eventTimeNs); |
| 433 | |
| 434 | if (ConditionState::kTrue != mCondition) { |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | // Pull on active state changes. |
| 439 | if (!isEventTooLate) { |
| 440 | if (mIsPulled) { |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 441 | pullAndMatchEventsLocked(eventTimeNs); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 442 | } |
| 443 | // When active state changes from true to false, clear diff base but don't |
| 444 | // reset other counters as we may accumulate more value in the bucket. |
| 445 | if (mUseDiff && !mIsActive) { |
| 446 | resetBase(); |
| 447 | } |
| 448 | } |
| 449 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 450 | flushIfNeededLocked(eventTimeNs); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 451 | |
| 452 | // Let condition timer know of new active state. |
| 453 | mConditionTimer.onConditionChanged(mIsActive, eventTimeNs); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 456 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, |
| 457 | const int64_t eventTimeNs) { |
| 458 | ConditionState newCondition = condition ? ConditionState::kTrue : ConditionState::kFalse; |
| 459 | bool isEventTooLate = eventTimeNs < mCurrentBucketStartTimeNs; |
| 460 | |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 461 | // If the config is not active, skip the event. |
| 462 | if (!mIsActive) { |
| 463 | mCondition = isEventTooLate ? ConditionState::kUnknown : newCondition; |
| 464 | return; |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 465 | } |
| 466 | |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 467 | // If the event arrived late, mark the bucket as invalid and skip the event. |
| 468 | if (isEventTooLate) { |
| 469 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 470 | (long long)mCurrentBucketStartTimeNs); |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 471 | StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 472 | StatsdStats::getInstance().noteConditionChangeInNextBucket(mMetricId); |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 473 | invalidateCurrentBucket(eventTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 474 | mCondition = ConditionState::kUnknown; |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 475 | mConditionTimer.onConditionChanged(mCondition, eventTimeNs); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 476 | return; |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 477 | } |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 478 | |
| 479 | // If the previous condition was unknown, mark the bucket as invalid |
| 480 | // because the bucket will contain partial data. For example, the condition |
| 481 | // change might happen close to the end of the bucket and we might miss a |
| 482 | // lot of data. |
| 483 | // |
| 484 | // We still want to pull to set the base. |
| 485 | if (mCondition == ConditionState::kUnknown) { |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 486 | invalidateCurrentBucket(eventTimeNs, BucketDropReason::CONDITION_UNKNOWN); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Pull and match for the following condition change cases: |
| 490 | // unknown/false -> true - condition changed |
| 491 | // true -> false - condition changed |
| 492 | // true -> true - old condition was true so we can flush the bucket at the |
| 493 | // end if needed. |
| 494 | // |
| 495 | // We don’t need to pull for unknown -> false or false -> false. |
| 496 | // |
| 497 | // onConditionChangedLocked might happen on bucket boundaries if this is |
| 498 | // called before #onDataPulled. |
| 499 | if (mIsPulled && |
| 500 | (newCondition == ConditionState::kTrue || mCondition == ConditionState::kTrue)) { |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 501 | pullAndMatchEventsLocked(eventTimeNs); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // For metrics that use diff, when condition changes from true to false, |
| 505 | // clear diff base but don't reset other counts because we may accumulate |
| 506 | // more value in the bucket. |
| 507 | if (mUseDiff && |
| 508 | (mCondition == ConditionState::kTrue && newCondition == ConditionState::kFalse)) { |
| 509 | resetBase(); |
| 510 | } |
| 511 | |
| 512 | // Update condition state after pulling. |
| 513 | mCondition = newCondition; |
| 514 | |
| 515 | flushIfNeededLocked(eventTimeNs); |
| 516 | mConditionTimer.onConditionChanged(mCondition, eventTimeNs); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 519 | void ValueMetricProducer::prepareFirstBucketLocked() { |
| 520 | // Kicks off the puller immediately if condition is true and diff based. |
| 521 | if (mIsActive && mIsPulled && mCondition == ConditionState::kTrue && mUseDiff) { |
| 522 | pullAndMatchEventsLocked(mCurrentBucketStartTimeNs); |
| 523 | } |
| 524 | } |
| 525 | |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 526 | void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 527 | vector<std::shared_ptr<LogEvent>> allData; |
Tej Singh | 7b975a8 | 2020-05-11 11:05:08 -0700 | [diff] [blame] | 528 | if (!mPullerManager->Pull(mPullTagId, mConfigKey, timestampNs, &allData)) { |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 529 | ALOGE("Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs); |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 530 | invalidateCurrentBucket(timestampNs, BucketDropReason::PULL_FAILED); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 531 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 532 | } |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 533 | |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 534 | accumulateEvents(allData, timestampNs, timestampNs); |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) { |
| 538 | return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs; |
| 539 | } |
| 540 | |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 541 | // By design, statsd pulls data at bucket boundaries using AlarmManager. These pulls are likely |
| 542 | // to be delayed. Other events like condition changes or app upgrade which are not based on |
| 543 | // AlarmManager might have arrived earlier and close the bucket. |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 544 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData, |
| 545 | bool pullSuccess, int64_t originalPullTimeNs) { |
| 546 | std::lock_guard<std::mutex> lock(mMutex); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 547 | if (mCondition == ConditionState::kTrue) { |
| 548 | // If the pull failed, we won't be able to compute a diff. |
| 549 | if (!pullSuccess) { |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 550 | invalidateCurrentBucket(originalPullTimeNs, BucketDropReason::PULL_FAILED); |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 551 | } else { |
| 552 | bool isEventLate = originalPullTimeNs < getCurrentBucketEndTimeNs(); |
| 553 | if (isEventLate) { |
| 554 | // If the event is late, we are in the middle of a bucket. Just |
| 555 | // process the data without trying to snap the data to the nearest bucket. |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 556 | accumulateEvents(allData, originalPullTimeNs, originalPullTimeNs); |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 557 | } else { |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 558 | // For scheduled pulled data, the effective event time is snap to the nearest |
| 559 | // bucket end. In the case of waking up from a deep sleep state, we will |
| 560 | // attribute to the previous bucket end. If the sleep was long but not very |
| 561 | // long, we will be in the immediate next bucket. Previous bucket may get a |
| 562 | // larger number as we pull at a later time than real bucket end. |
| 563 | // |
| 564 | // If the sleep was very long, we skip more than one bucket before sleep. In |
| 565 | // this case, if the diff base will be cleared and this new data will serve as |
| 566 | // new diff base. |
| 567 | int64_t bucketEndTime = calcPreviousBucketEndTime(originalPullTimeNs) - 1; |
| 568 | StatsdStats::getInstance().noteBucketBoundaryDelayNs( |
| 569 | mMetricId, originalPullTimeNs - bucketEndTime); |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 570 | accumulateEvents(allData, originalPullTimeNs, bucketEndTime); |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 571 | } |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 572 | } |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 573 | } |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 574 | |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 575 | // We can probably flush the bucket. Since we used bucketEndTime when calling |
| 576 | // #onMatchedLogEventInternalLocked, the current bucket will not have been flushed. |
| 577 | flushIfNeededLocked(originalPullTimeNs); |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 580 | void ValueMetricProducer::accumulateEvents(const std::vector<std::shared_ptr<LogEvent>>& allData, |
tsaichristine | 78b8570 | 2019-12-06 12:20:30 -0800 | [diff] [blame] | 581 | int64_t originalPullTimeNs, int64_t eventElapsedTimeNs) { |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 582 | bool isEventLate = eventElapsedTimeNs < mCurrentBucketStartTimeNs; |
| 583 | if (isEventLate) { |
| 584 | VLOG("Skip bucket end pull due to late arrival: %lld vs %lld", |
| 585 | (long long)eventElapsedTimeNs, (long long)mCurrentBucketStartTimeNs); |
| 586 | StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId); |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 587 | invalidateCurrentBucket(eventElapsedTimeNs, BucketDropReason::EVENT_IN_WRONG_BUCKET); |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 591 | const int64_t elapsedRealtimeNs = getElapsedRealtimeNs(); |
| 592 | const int64_t pullDelayNs = elapsedRealtimeNs - originalPullTimeNs; |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 593 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 594 | if (pullDelayNs > mMaxPullDelayNs) { |
| 595 | ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId, |
| 596 | (long long)mMaxPullDelayNs); |
| 597 | StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId); |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 598 | // We are missing one pull from the bucket which means we will not have a complete view of |
| 599 | // what's going on. |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 600 | invalidateCurrentBucket(eventElapsedTimeNs, BucketDropReason::PULL_DELAYED); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 601 | return; |
| 602 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 603 | |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 604 | mMatchedMetricDimensionKeys.clear(); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 605 | for (const auto& data : allData) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 606 | LogEvent localCopy = data->makeCopy(); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 607 | if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) == |
| 608 | MatchingState::kMatched) { |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 609 | localCopy.setElapsedTimestampNs(eventElapsedTimeNs); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 610 | onMatchedLogEventLocked(mWhatMatcherIndex, localCopy); |
| 611 | } |
| 612 | } |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 613 | // If a key that is: |
| 614 | // 1. Tracked in mCurrentSlicedBucket and |
| 615 | // 2. A superset of the current mStateChangePrimaryKey |
| 616 | // was not found in the new pulled data (i.e. not in mMatchedDimensionInWhatKeys) |
| 617 | // then we need to reset the base. |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 618 | for (auto& slice : mCurrentSlicedBucket) { |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 619 | const auto& whatKey = slice.first.getDimensionKeyInWhat(); |
| 620 | bool presentInPulledData = |
| 621 | mMatchedMetricDimensionKeys.find(whatKey) != mMatchedMetricDimensionKeys.end(); |
| 622 | if (!presentInPulledData && whatKey.contains(mStateChangePrimaryKey.second)) { |
| 623 | auto it = mCurrentBaseInfo.find(whatKey); |
| 624 | for (auto& baseInfo : it->second) { |
| 625 | baseInfo.hasBase = false; |
| 626 | baseInfo.hasCurrentState = false; |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 627 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 628 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 629 | } |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 630 | mMatchedMetricDimensionKeys.clear(); |
| 631 | mHasGlobalBase = true; |
Olivier Gaillard | 1e0d8fc | 2019-02-11 18:08:43 +0000 | [diff] [blame] | 632 | |
| 633 | // If we reach the guardrail, we might have dropped some data which means the bucket is |
| 634 | // incomplete. |
| 635 | // |
| 636 | // The base also needs to be reset. If we do not have the full data, we might |
| 637 | // incorrectly compute the diff when mUseZeroDefaultBase is true since an existing key |
| 638 | // might be missing from mCurrentSlicedBucket. |
| 639 | if (hasReachedGuardRailLimit()) { |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 640 | invalidateCurrentBucket(eventElapsedTimeNs, BucketDropReason::DIMENSION_GUARDRAIL_REACHED); |
Olivier Gaillard | 1e0d8fc | 2019-02-11 18:08:43 +0000 | [diff] [blame] | 641 | mCurrentSlicedBucket.clear(); |
| 642 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 645 | void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 646 | if (mCurrentSlicedBucket.size() == 0) { |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 651 | (unsigned long)mCurrentSlicedBucket.size()); |
| 652 | if (verbose) { |
| 653 | for (const auto& it : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 654 | for (const auto& interval : it.second) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 655 | fprintf(out, "\t(what)%s\t(states)%s (value)%s\n", |
| 656 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 657 | it.first.getStateValuesKey().toString().c_str(), |
| 658 | interval.value.toString().c_str()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 659 | } |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
Olivier Gaillard | 1e0d8fc | 2019-02-11 18:08:43 +0000 | [diff] [blame] | 664 | bool ValueMetricProducer::hasReachedGuardRailLimit() const { |
| 665 | return mCurrentSlicedBucket.size() >= mDimensionHardLimit; |
| 666 | } |
| 667 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 668 | bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 669 | // ===========GuardRail============== |
| 670 | // 1. Report the tuple count if the tuple count > soft limit |
| 671 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 672 | return false; |
| 673 | } |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 674 | if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 675 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 676 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 677 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
Olivier Gaillard | 1e0d8fc | 2019-02-11 18:08:43 +0000 | [diff] [blame] | 678 | if (hasReachedGuardRailLimit()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 679 | ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId, |
| 680 | newKey.toString().c_str()); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 681 | StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 682 | return true; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return false; |
| 687 | } |
| 688 | |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 689 | bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) { |
| 690 | // ===========GuardRail============== |
| 691 | // 1. Report the tuple count if the tuple count > soft limit |
| 692 | if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) { |
| 693 | return false; |
| 694 | } |
| 695 | if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) { |
| 696 | size_t newTupleCount = mCurrentFullBucket.size() + 1; |
| 697 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 698 | if (newTupleCount > mDimensionHardLimit) { |
| 699 | ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s", |
| 700 | (long long)mMetricId, |
| 701 | newKey.toString().c_str()); |
| 702 | return true; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return false; |
| 707 | } |
| 708 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 709 | bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) { |
| 710 | for (const FieldValue& value : event.getValues()) { |
| 711 | if (value.mField.matches(matcher)) { |
| 712 | switch (value.mValue.type) { |
| 713 | case INT: |
| 714 | ret.setLong(value.mValue.int_value); |
| 715 | break; |
| 716 | case LONG: |
| 717 | ret.setLong(value.mValue.long_value); |
| 718 | break; |
| 719 | case FLOAT: |
| 720 | ret.setDouble(value.mValue.float_value); |
| 721 | break; |
| 722 | case DOUBLE: |
| 723 | ret.setDouble(value.mValue.double_value); |
| 724 | break; |
| 725 | default: |
tsaichristine | 409468d | 2019-10-28 11:32:31 -0700 | [diff] [blame] | 726 | return false; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 727 | break; |
| 728 | } |
| 729 | return true; |
| 730 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 731 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 732 | return false; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 733 | } |
| 734 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 735 | void ValueMetricProducer::onMatchedLogEventInternalLocked( |
| 736 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
| 737 | const ConditionKey& conditionKey, bool condition, const LogEvent& event, |
| 738 | const map<int, HashableDimensionKey>& statePrimaryKeys) { |
| 739 | auto whatKey = eventKey.getDimensionKeyInWhat(); |
| 740 | auto stateKey = eventKey.getStateValuesKey(); |
| 741 | |
| 742 | // Skip this event if a state changed occurred for a different primary key. |
| 743 | auto it = statePrimaryKeys.find(mStateChangePrimaryKey.first); |
| 744 | // Check that both the atom id and the primary key are equal. |
| 745 | if (it != statePrimaryKeys.end() && it->second != mStateChangePrimaryKey.second) { |
| 746 | VLOG("ValueMetric skip event with primary key %s because state change primary key " |
| 747 | "is %s", |
| 748 | it->second.toString().c_str(), mStateChangePrimaryKey.second.toString().c_str()); |
| 749 | return; |
| 750 | } |
| 751 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 752 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 753 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 754 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 755 | (long long)mCurrentBucketStartTimeNs); |
| 756 | return; |
| 757 | } |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 758 | mMatchedMetricDimensionKeys.insert(whatKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 759 | |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 760 | if (!mIsPulled) { |
| 761 | // We cannot flush without doing a pull first. |
| 762 | flushIfNeededLocked(eventTimeNs); |
| 763 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 764 | |
Olivier Gaillard | fbee916 | 2019-04-11 11:48:01 +0100 | [diff] [blame] | 765 | // We should not accumulate the data for pushed metrics when the condition is false. |
| 766 | bool shouldSkipForPushMetric = !mIsPulled && !condition; |
| 767 | // For pulled metrics, there are two cases: |
| 768 | // - to compute diffs, we need to process all the state changes |
| 769 | // - for non-diffs metrics, we should ignore the data if the condition wasn't true. If we have a |
| 770 | // state change from |
| 771 | // + True -> True: we should process the data, it might be a bucket boundary |
| 772 | // + True -> False: we als need to process the data. |
| 773 | bool shouldSkipForPulledMetric = mIsPulled && !mUseDiff |
| 774 | && mCondition != ConditionState::kTrue; |
| 775 | if (shouldSkipForPushMetric || shouldSkipForPulledMetric) { |
tsaichristine | 5088e72 | 2020-05-28 10:06:41 -0700 | [diff] [blame] | 776 | VLOG("ValueMetric skip event because condition is false and we are not using diff (for " |
| 777 | "pulled metric)"); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 778 | return; |
| 779 | } |
| 780 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 781 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 782 | return; |
| 783 | } |
tsaichristine | 5088e72 | 2020-05-28 10:06:41 -0700 | [diff] [blame] | 784 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 785 | vector<BaseInfo>& baseInfos = mCurrentBaseInfo[whatKey]; |
| 786 | if (baseInfos.size() < mFieldMatchers.size()) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 787 | VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size()); |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 788 | baseInfos.resize(mFieldMatchers.size()); |
| 789 | } |
| 790 | |
tsaichristine | 5088e72 | 2020-05-28 10:06:41 -0700 | [diff] [blame] | 791 | for (BaseInfo& baseInfo : baseInfos) { |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 792 | if (!baseInfo.hasCurrentState) { |
tsaichristine | 5088e72 | 2020-05-28 10:06:41 -0700 | [diff] [blame] | 793 | baseInfo.currentState = getUnknownStateKey(); |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 794 | baseInfo.hasCurrentState = true; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // We need to get the intervals stored with the previous state key so we can |
| 799 | // close these value intervals. |
| 800 | const auto oldStateKey = baseInfos[0].currentState; |
| 801 | vector<Interval>& intervals = mCurrentSlicedBucket[MetricDimensionKey(whatKey, oldStateKey)]; |
| 802 | if (intervals.size() < mFieldMatchers.size()) { |
| 803 | VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size()); |
| 804 | intervals.resize(mFieldMatchers.size()); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 805 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 806 | |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 807 | // We only use anomaly detection under certain cases. |
| 808 | // N.B.: The anomaly detection cases were modified in order to fix an issue with value metrics |
| 809 | // containing multiple values. We tried to retain all previous behaviour, but we are unsure the |
| 810 | // previous behaviour was correct. At the time of the fix, anomaly detection had no owner. |
| 811 | // Whoever next works on it should look into the cases where it is triggered in this function. |
| 812 | // Discussion here: http://ag/6124370. |
| 813 | bool useAnomalyDetection = true; |
| 814 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 815 | for (int i = 0; i < (int)mFieldMatchers.size(); i++) { |
| 816 | const Matcher& matcher = mFieldMatchers[i]; |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 817 | BaseInfo& baseInfo = baseInfos[i]; |
| 818 | Interval& interval = intervals[i]; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 819 | interval.valueIndex = i; |
| 820 | Value value; |
| 821 | if (!getDoubleOrLong(event, matcher, value)) { |
| 822 | VLOG("Failed to get value %d from event %s", i, event.ToString().c_str()); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 823 | StatsdStats::getInstance().noteBadValueType(mMetricId); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 824 | return; |
| 825 | } |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 826 | interval.seenNewData = true; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 827 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 828 | if (mUseDiff) { |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 829 | if (!baseInfo.hasBase) { |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 830 | if (mHasGlobalBase && mUseZeroDefaultBase) { |
| 831 | // The bucket has global base. This key does not. |
| 832 | // Optionally use zero as base. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 833 | baseInfo.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE); |
| 834 | baseInfo.hasBase = true; |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 835 | } else { |
| 836 | // no base. just update base and return. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 837 | baseInfo.base = value; |
| 838 | baseInfo.hasBase = true; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 839 | // If we're missing a base, do not use anomaly detection on incomplete data |
| 840 | useAnomalyDetection = false; |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 841 | // Continue (instead of return) here in order to set baseInfo.base and |
| 842 | // baseInfo.hasBase for other baseInfos |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 843 | continue; |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 844 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 845 | } |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 846 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 847 | Value diff; |
| 848 | switch (mValueDirection) { |
| 849 | case ValueMetric::INCREASING: |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 850 | if (value >= baseInfo.base) { |
| 851 | diff = value - baseInfo.base; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 852 | } else if (mUseAbsoluteValueOnReset) { |
| 853 | diff = value; |
| 854 | } else { |
| 855 | VLOG("Unexpected decreasing value"); |
| 856 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 857 | baseInfo.base = value; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 858 | // If we've got bad data, do not use anomaly detection |
| 859 | useAnomalyDetection = false; |
| 860 | continue; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 861 | } |
| 862 | break; |
| 863 | case ValueMetric::DECREASING: |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 864 | if (baseInfo.base >= value) { |
| 865 | diff = baseInfo.base - value; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 866 | } else if (mUseAbsoluteValueOnReset) { |
| 867 | diff = value; |
| 868 | } else { |
| 869 | VLOG("Unexpected increasing value"); |
| 870 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 871 | baseInfo.base = value; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 872 | // If we've got bad data, do not use anomaly detection |
| 873 | useAnomalyDetection = false; |
| 874 | continue; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 875 | } |
| 876 | break; |
| 877 | case ValueMetric::ANY: |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 878 | diff = value - baseInfo.base; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 879 | break; |
| 880 | default: |
| 881 | break; |
| 882 | } |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 883 | baseInfo.base = value; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 884 | value = diff; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 885 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 886 | |
| 887 | if (interval.hasValue) { |
| 888 | switch (mAggregationType) { |
| 889 | case ValueMetric::SUM: |
| 890 | // for AVG, we add up and take average when flushing the bucket |
| 891 | case ValueMetric::AVG: |
| 892 | interval.value += value; |
| 893 | break; |
| 894 | case ValueMetric::MIN: |
| 895 | interval.value = std::min(value, interval.value); |
| 896 | break; |
| 897 | case ValueMetric::MAX: |
| 898 | interval.value = std::max(value, interval.value); |
| 899 | break; |
| 900 | default: |
| 901 | break; |
| 902 | } |
| 903 | } else { |
| 904 | interval.value = value; |
| 905 | interval.hasValue = true; |
| 906 | } |
| 907 | interval.sampleSize += 1; |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 908 | baseInfo.currentState = stateKey; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 909 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 910 | |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 911 | // Only trigger the tracker if all intervals are correct |
| 912 | if (useAnomalyDetection) { |
| 913 | // TODO: propgate proper values down stream when anomaly support doubles |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 914 | long wholeBucketVal = intervals[0].value.long_value; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 915 | auto prev = mCurrentFullBucket.find(eventKey); |
| 916 | if (prev != mCurrentFullBucket.end()) { |
| 917 | wholeBucketVal += prev->second; |
| 918 | } |
| 919 | for (auto& tracker : mAnomalyTrackers) { |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 920 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey, |
| 921 | wholeBucketVal); |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 922 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 923 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 926 | // For pulled metrics, we always need to make sure we do a pull before flushing the bucket |
| 927 | // if mCondition is true! |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 928 | void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 929 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 930 | if (eventTimeNs < currentBucketEndTimeNs) { |
tsaichristine | 90f95bb | 2019-11-06 17:06:53 -0800 | [diff] [blame] | 931 | VLOG("eventTime is %lld, less than current bucket end time %lld", (long long)eventTimeNs, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 932 | (long long)(currentBucketEndTimeNs)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 933 | return; |
| 934 | } |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 935 | int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs); |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 936 | int64_t nextBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 937 | flushCurrentBucketLocked(eventTimeNs, nextBucketStartTimeNs); |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 938 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 939 | |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 940 | int64_t ValueMetricProducer::calcBucketsForwardCount(const int64_t& eventTimeNs) const { |
| 941 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 942 | if (eventTimeNs < currentBucketEndTimeNs) { |
| 943 | return 0; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 944 | } |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 945 | return 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 948 | void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs, |
| 949 | const int64_t& nextBucketStartTimeNs) { |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 950 | if (mCondition == ConditionState::kUnknown) { |
| 951 | StatsdStats::getInstance().noteBucketUnknownCondition(mMetricId); |
| 952 | } |
| 953 | |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 954 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 955 | (int)mCurrentSlicedBucket.size()); |
| 956 | |
| 957 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 958 | int64_t bucketEndTime = fullBucketEndTimeNs; |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 959 | int64_t numBucketsForward = calcBucketsForwardCount(eventTimeNs); |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 960 | if (numBucketsForward > 1) { |
| 961 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
| 962 | StatsdStats::getInstance().noteSkippedForwardBuckets(mMetricId); |
| 963 | // Something went wrong. Maybe the device was sleeping for a long time. It is better |
| 964 | // to mark the current bucket as invalid. The last pull might have been successful through. |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 965 | invalidateCurrentBucketWithoutResetBase(eventTimeNs, |
| 966 | BucketDropReason::MULTIPLE_BUCKETS_SKIPPED); |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 967 | // End the bucket at the next bucket start time so the entire interval is skipped. |
| 968 | bucketEndTime = nextBucketStartTimeNs; |
| 969 | } else if (eventTimeNs < fullBucketEndTimeNs) { |
| 970 | bucketEndTime = eventTimeNs; |
Olivier Gaillard | 47a9efc | 2019-02-22 15:43:31 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 973 | // Close the current bucket. |
| 974 | int64_t conditionTrueDuration = mConditionTimer.newBucketStart(bucketEndTime); |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 975 | bool isBucketLargeEnough = bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 976 | if (!isBucketLargeEnough) { |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 977 | skipCurrentBucket(eventTimeNs, BucketDropReason::BUCKET_TOO_SMALL); |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 978 | } |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 979 | if (!mCurrentBucketIsSkipped) { |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 980 | bool bucketHasData = false; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 981 | // The current bucket is large enough to keep. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 982 | for (const auto& slice : mCurrentSlicedBucket) { |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 983 | ValueBucket bucket = buildPartialBucket(bucketEndTime, slice.second); |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 984 | bucket.mConditionTrueNs = conditionTrueDuration; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 985 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 986 | if (bucket.valueIndex.size() > 0) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 987 | auto& bucketList = mPastBuckets[slice.first]; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 988 | bucketList.push_back(bucket); |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 989 | bucketHasData = true; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 990 | } |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 991 | } |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 992 | if (!bucketHasData) { |
| 993 | skipCurrentBucket(eventTimeNs, BucketDropReason::NO_DATA); |
| 994 | } |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | if (mCurrentBucketIsSkipped) { |
tsaichristine | 45f703b | 2020-02-03 10:44:39 -0800 | [diff] [blame] | 998 | mCurrentSkippedBucket.bucketStartTimeNs = mCurrentBucketStartTimeNs; |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 999 | mCurrentSkippedBucket.bucketEndTimeNs = bucketEndTime; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 1000 | mSkippedBuckets.emplace_back(mCurrentSkippedBucket); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Jeffrey Huang | f108912 | 2020-05-21 12:16:25 -0700 | [diff] [blame] | 1003 | // This means that the current bucket was not flushed before a forced bucket split. |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 1004 | // This can happen if an app update or a dump report with include_current_partial_bucket is |
| 1005 | // requested before we get a chance to flush the bucket due to receiving new data, either from |
| 1006 | // the statsd socket or the StatsPullerManager. |
| 1007 | if (bucketEndTime < nextBucketStartTimeNs) { |
Jeffrey Huang | f108912 | 2020-05-21 12:16:25 -0700 | [diff] [blame] | 1008 | SkippedBucket bucketInGap; |
| 1009 | bucketInGap.bucketStartTimeNs = bucketEndTime; |
| 1010 | bucketInGap.bucketEndTimeNs = nextBucketStartTimeNs; |
| 1011 | bucketInGap.dropEvents.emplace_back( |
| 1012 | buildDropEvent(eventTimeNs, BucketDropReason::NO_DATA)); |
| 1013 | mSkippedBuckets.emplace_back(bucketInGap); |
| 1014 | } |
| 1015 | |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 1016 | appendToFullBucket(eventTimeNs > fullBucketEndTimeNs); |
Olivier Gaillard | a8b7011 | 2019-02-25 11:24:23 +0000 | [diff] [blame] | 1017 | initCurrentSlicedBucket(nextBucketStartTimeNs); |
Yao Chen | e6cfb14 | 2019-04-08 12:00:01 -0700 | [diff] [blame] | 1018 | // Update the condition timer again, in case we skipped buckets. |
| 1019 | mConditionTimer.newBucketStart(nextBucketStartTimeNs); |
Olivier Gaillard | c371991 | 2019-03-15 17:33:40 +0000 | [diff] [blame] | 1020 | mCurrentBucketNum += numBucketsForward; |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | ValueBucket ValueMetricProducer::buildPartialBucket(int64_t bucketEndTime, |
| 1024 | const std::vector<Interval>& intervals) { |
| 1025 | ValueBucket bucket; |
| 1026 | bucket.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 1027 | bucket.mBucketEndNs = bucketEndTime; |
| 1028 | for (const auto& interval : intervals) { |
| 1029 | if (interval.hasValue) { |
| 1030 | // skip the output if the diff is zero |
| 1031 | if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) { |
| 1032 | continue; |
| 1033 | } |
| 1034 | bucket.valueIndex.push_back(interval.valueIndex); |
| 1035 | if (mAggregationType != ValueMetric::AVG) { |
| 1036 | bucket.values.push_back(interval.value); |
| 1037 | } else { |
| 1038 | double sum = interval.value.type == LONG ? (double)interval.value.long_value |
| 1039 | : interval.value.double_value; |
| 1040 | bucket.values.push_back(Value((double)sum / interval.sampleSize)); |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | return bucket; |
| 1045 | } |
| 1046 | |
Olivier Gaillard | a8b7011 | 2019-02-25 11:24:23 +0000 | [diff] [blame] | 1047 | void ValueMetricProducer::initCurrentSlicedBucket(int64_t nextBucketStartTimeNs) { |
| 1048 | StatsdStats::getInstance().noteBucketCount(mMetricId); |
| 1049 | // Cleanup data structure to aggregate values. |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 1050 | for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) { |
| 1051 | bool obsolete = true; |
| 1052 | for (auto& interval : it->second) { |
| 1053 | interval.hasValue = false; |
| 1054 | interval.sampleSize = 0; |
| 1055 | if (interval.seenNewData) { |
| 1056 | obsolete = false; |
| 1057 | } |
| 1058 | interval.seenNewData = false; |
| 1059 | } |
| 1060 | |
| 1061 | if (obsolete) { |
| 1062 | it = mCurrentSlicedBucket.erase(it); |
| 1063 | } else { |
| 1064 | it++; |
| 1065 | } |
tsaichristine | 5088e72 | 2020-05-28 10:06:41 -0700 | [diff] [blame] | 1066 | // TODO(b/157655103): remove mCurrentBaseInfo entries when obsolete |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 1067 | } |
Olivier Gaillard | a8b7011 | 2019-02-25 11:24:23 +0000 | [diff] [blame] | 1068 | |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 1069 | mCurrentBucketIsSkipped = false; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 1070 | mCurrentSkippedBucket.reset(); |
| 1071 | |
Olivier Gaillard | a8b7011 | 2019-02-25 11:24:23 +0000 | [diff] [blame] | 1072 | // If we do not have a global base when the condition is true, |
| 1073 | // we will have incomplete bucket for the next bucket. |
| 1074 | if (mUseDiff && !mHasGlobalBase && mCondition) { |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 1075 | mCurrentBucketIsSkipped = false; |
Olivier Gaillard | a8b7011 | 2019-02-25 11:24:23 +0000 | [diff] [blame] | 1076 | } |
| 1077 | mCurrentBucketStartTimeNs = nextBucketStartTimeNs; |
| 1078 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 1079 | (long long)mCurrentBucketStartTimeNs); |
Olivier Gaillard | 9a5d359 | 2019-02-05 15:12:39 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Jeffrey Huang | 56ff8f3 | 2020-05-26 15:56:53 -0700 | [diff] [blame] | 1082 | void ValueMetricProducer::appendToFullBucket(const bool isFullBucketReached) { |
Jeffrey Huang | de3348a | 2020-05-20 16:06:13 -0700 | [diff] [blame] | 1083 | if (mCurrentBucketIsSkipped) { |
Olivier Gaillard | c371991 | 2019-03-15 17:33:40 +0000 | [diff] [blame] | 1084 | if (isFullBucketReached) { |
| 1085 | // If the bucket is invalid, we ignore the full bucket since it contains invalid data. |
| 1086 | mCurrentFullBucket.clear(); |
| 1087 | } |
| 1088 | // Current bucket is invalid, we do not add it to the full bucket. |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | if (isFullBucketReached) { // If full bucket, send to anomaly tracker. |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 1093 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 1094 | if (mCurrentFullBucket.size() > 0) { |
| 1095 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 1096 | if (hitFullBucketGuardRailLocked(slice.first)) { |
| 1097 | continue; |
| 1098 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1099 | // TODO: fix this when anomaly can accept double values |
Olivier Gaillard | c371991 | 2019-03-15 17:33:40 +0000 | [diff] [blame] | 1100 | auto& interval = slice.second[0]; |
| 1101 | if (interval.hasValue) { |
| 1102 | mCurrentFullBucket[slice.first] += interval.value.long_value; |
| 1103 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 1104 | } |
| 1105 | for (const auto& slice : mCurrentFullBucket) { |
| 1106 | for (auto& tracker : mAnomalyTrackers) { |
| 1107 | if (tracker != nullptr) { |
| 1108 | tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum); |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | mCurrentFullBucket.clear(); |
| 1113 | } else { |
| 1114 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 1115 | for (const auto& slice : mCurrentSlicedBucket) { |
| 1116 | for (auto& tracker : mAnomalyTrackers) { |
| 1117 | if (tracker != nullptr) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1118 | // TODO: fix this when anomaly can accept double values |
Olivier Gaillard | c371991 | 2019-03-15 17:33:40 +0000 | [diff] [blame] | 1119 | auto& interval = slice.second[0]; |
| 1120 | if (interval.hasValue) { |
| 1121 | tracker->addPastBucket(slice.first, interval.value.long_value, |
| 1122 | mCurrentBucketNum); |
| 1123 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | } else { |
| 1129 | // Accumulate partial bucket. |
| 1130 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1131 | // TODO: fix this when anomaly can accept double values |
Olivier Gaillard | c371991 | 2019-03-15 17:33:40 +0000 | [diff] [blame] | 1132 | auto& interval = slice.second[0]; |
| 1133 | if (interval.hasValue) { |
| 1134 | mCurrentFullBucket[slice.first] += interval.value.long_value; |
| 1135 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 1136 | } |
| 1137 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 1140 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 1141 | size_t totalSize = 0; |
| 1142 | for (const auto& pair : mPastBuckets) { |
| 1143 | totalSize += pair.second.size() * kBucketSize; |
| 1144 | } |
| 1145 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 1148 | } // namespace statsd |
| 1149 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 1150 | } // namespace android |