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