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