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