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