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), |
| 107 | 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] | 108 | : StatsdStats::kPullMaxDelayNs), |
| 109 | mSplitBucketForAppUpgrade(metric.split_bucket_for_app_upgrade()) { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 110 | int64_t bucketSizeMills = 0; |
| 111 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 112 | bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 113 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 114 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 115 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 116 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 117 | mBucketSizeNs = bucketSizeMills * 1000000; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 118 | |
| 119 | translateFieldMatcher(metric.value_field(), &mFieldMatchers); |
| 120 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 121 | if (metric.has_dimensions_in_what()) { |
| 122 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 123 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (metric.has_dimensions_in_condition()) { |
| 127 | translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition); |
| 128 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 129 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 130 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 131 | for (const auto& link : metric.links()) { |
| 132 | Metric2Condition mc; |
| 133 | mc.conditionId = link.condition(); |
| 134 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 135 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 136 | mMetric2ConditionLinks.push_back(mc); |
| 137 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 138 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 139 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 140 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 141 | mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) || |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 142 | HasPositionALL(metric.dimensions_in_condition()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 143 | |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 144 | flushIfNeededLocked(startTimeNs); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 145 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 146 | if (mIsPulled) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 147 | mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(), |
| 148 | mBucketSizeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 149 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 150 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 151 | // 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] | 152 | // flushIfNeeded to adjust start and end to bucket boundaries. |
| 153 | // Adjust start for partial bucket |
| 154 | mCurrentBucketStartTimeNs = startTimeNs; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 155 | // Kicks off the puller immediately if condition is true and diff based. |
| 156 | if (mIsPulled && mCondition && mUseDiff) { |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 157 | pullAndMatchEventsLocked(startTimeNs); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 158 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 159 | VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
| 160 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ValueMetricProducer::~ValueMetricProducer() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 164 | VLOG("~ValueMetricProducer() called"); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 165 | if (mIsPulled) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 166 | mPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 167 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 170 | void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 171 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 172 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 175 | void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 176 | flushIfNeededLocked(dropTimeNs); |
| 177 | mPastBuckets.clear(); |
| 178 | } |
| 179 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 180 | void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 181 | flushIfNeededLocked(dumpTimeNs); |
| 182 | mPastBuckets.clear(); |
| 183 | mSkippedBuckets.clear(); |
| 184 | } |
| 185 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 186 | void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 187 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 188 | const bool erase_data, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 189 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 190 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 191 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 192 | if (include_current_partial_bucket) { |
| 193 | flushLocked(dumpTimeNs); |
| 194 | } else { |
| 195 | flushIfNeededLocked(dumpTimeNs); |
| 196 | } |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 197 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Howard Ro | 07e23ff | 2018-12-17 17:28:07 -0800 | [diff] [blame] | 198 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked()); |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 199 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 200 | if (mPastBuckets.empty() && mSkippedBuckets.empty()) { |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 203 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs); |
| 204 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs); |
| 205 | // Fills the dimension path if not slicing by ALL. |
| 206 | if (!mSliceByPositionALL) { |
| 207 | if (!mDimensionsInWhat.empty()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 208 | uint64_t dimenPathToken = |
| 209 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 210 | writeDimensionPathToProto(mDimensionsInWhat, protoOutput); |
| 211 | protoOutput->end(dimenPathToken); |
| 212 | } |
| 213 | if (!mDimensionsInCondition.empty()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 214 | uint64_t dimenPathToken = |
| 215 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 216 | writeDimensionPathToProto(mDimensionsInCondition, protoOutput); |
| 217 | protoOutput->end(dimenPathToken); |
| 218 | } |
| 219 | } |
| 220 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 221 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 222 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 223 | for (const auto& pair : mSkippedBuckets) { |
| 224 | uint64_t wrapperToken = |
| 225 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 226 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS, |
| 227 | (long long)(NanoToMillis(pair.first))); |
| 228 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS, |
| 229 | (long long)(NanoToMillis(pair.second))); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 230 | protoOutput->end(wrapperToken); |
| 231 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 232 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 233 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 234 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 235 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 236 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 237 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 238 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 239 | // First fill dimension. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 240 | if (mSliceByPositionALL) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 241 | uint64_t dimensionToken = |
| 242 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 243 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput); |
| 244 | protoOutput->end(dimensionToken); |
| 245 | if (dimensionKey.hasDimensionKeyInCondition()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 246 | uint64_t dimensionInConditionToken = |
| 247 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
| 248 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), str_set, |
| 249 | protoOutput); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 250 | protoOutput->end(dimensionInConditionToken); |
| 251 | } |
| 252 | } else { |
| 253 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(), |
| 254 | FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput); |
| 255 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 256 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(), |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 257 | FIELD_ID_DIMENSION_LEAF_IN_CONDITION, str_set, |
| 258 | protoOutput); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 259 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 260 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 261 | |
| 262 | // Then fill bucket_info (ValueBucketInfo). |
| 263 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 264 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 265 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 266 | |
| 267 | if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) { |
| 268 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS, |
| 269 | (long long)NanoToMillis(bucket.mBucketStartNs)); |
| 270 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS, |
| 271 | (long long)NanoToMillis(bucket.mBucketEndNs)); |
| 272 | } else { |
| 273 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM, |
| 274 | (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs))); |
| 275 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 276 | for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) { |
| 277 | int index = bucket.valueIndex[i]; |
| 278 | const Value& value = bucket.values[i]; |
| 279 | uint64_t valueToken = protoOutput->start( |
| 280 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES); |
| 281 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX, |
| 282 | index); |
| 283 | if (value.getType() == LONG) { |
| 284 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG, |
| 285 | (long long)value.long_value); |
| 286 | VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs, |
| 287 | (long long)bucket.mBucketEndNs, index, (long long)value.long_value); |
| 288 | } else if (value.getType() == DOUBLE) { |
| 289 | protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE, |
| 290 | value.double_value); |
| 291 | VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs, |
| 292 | (long long)bucket.mBucketEndNs, index, value.double_value); |
| 293 | } else { |
| 294 | VLOG("Wrong value type for ValueMetric output: %d", value.getType()); |
| 295 | } |
| 296 | protoOutput->end(valueToken); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 297 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 298 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 299 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 300 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 301 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 302 | protoOutput->end(protoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 303 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 304 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 305 | if (erase_data) { |
| 306 | mPastBuckets.clear(); |
| 307 | mSkippedBuckets.clear(); |
| 308 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 311 | void ValueMetricProducer::resetBase() { |
| 312 | for (auto& slice : mCurrentSlicedBucket) { |
| 313 | for (auto& interval : slice.second) { |
| 314 | interval.hasBase = false; |
| 315 | } |
| 316 | } |
| 317 | mHasGlobalBase = false; |
| 318 | } |
| 319 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 320 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 321 | const int64_t eventTimeNs) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 322 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 323 | 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] | 324 | (long long)mCurrentBucketStartTimeNs); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 325 | StatsdStats::getInstance().noteConditionChangeInNextBucket(mMetricId); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 329 | flushIfNeededLocked(eventTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 330 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 331 | // Pull on condition changes. |
| 332 | if (mIsPulled && (mCondition != condition)) { |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 333 | pullAndMatchEventsLocked(eventTimeNs); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 334 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 335 | |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 336 | // when condition change from true to false, clear diff base but don't |
| 337 | // reset other counters as we may accumulate more value in the bucket. |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 338 | if (mUseDiff && mCondition && !condition) { |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 339 | resetBase(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | mCondition = condition; |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 345 | void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 346 | vector<std::shared_ptr<LogEvent>> allData; |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 347 | if (!mPullerManager->Pull(mPullTagId, &allData)) { |
| 348 | ALOGE("Gauge Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs); |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 349 | resetBase(); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 350 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 351 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 352 | const int64_t pullDelayNs = getElapsedRealtimeNs() - timestampNs; |
| 353 | if (pullDelayNs > mMaxPullDelayNs) { |
| 354 | ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId, |
| 355 | (long long)mMaxPullDelayNs); |
| 356 | StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId); |
| 357 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
| 358 | resetBase(); |
| 359 | return; |
| 360 | } |
| 361 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
| 362 | |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 363 | if (timestampNs < mCurrentBucketStartTimeNs) { |
| 364 | // The data will be skipped in onMatchedLogEventInternalLocked, but we don't want to report |
| 365 | // for every event, just the pull |
| 366 | StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId); |
| 367 | } |
| 368 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 369 | for (const auto& data : allData) { |
| 370 | // make a copy before doing and changes |
| 371 | LogEvent localCopy = data->makeCopy(); |
| 372 | localCopy.setElapsedTimestampNs(timestampNs); |
| 373 | if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) == |
| 374 | MatchingState::kMatched) { |
| 375 | onMatchedLogEventLocked(mWhatMatcherIndex, localCopy); |
| 376 | } |
| 377 | } |
| 378 | mHasGlobalBase = true; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 381 | int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) { |
| 382 | return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs; |
| 383 | } |
| 384 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 385 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 386 | std::lock_guard<std::mutex> lock(mMutex); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 387 | if (mCondition) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 388 | if (allData.size() == 0) { |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 389 | VLOG("Data pulled is empty"); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 390 | StatsdStats::getInstance().noteEmptyData(mPullTagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 391 | return; |
| 392 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 393 | // For scheduled pulled data, the effective event time is snap to the nearest |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 394 | // bucket end. In the case of waking up from a deep sleep state, we will |
| 395 | // attribute to the previous bucket end. If the sleep was long but not very long, we |
| 396 | // will be in the immediate next bucket. Previous bucket may get a larger number as |
| 397 | // we pull at a later time than real bucket end. |
| 398 | // If the sleep was very long, we skip more than one bucket before sleep. In this case, |
| 399 | // if the diff base will be cleared and this new data will serve as new diff base. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 400 | int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 401 | int64_t bucketEndTime = calcPreviousBucketEndTime(realEventTime) - 1; |
| 402 | if (bucketEndTime < mCurrentBucketStartTimeNs) { |
| 403 | VLOG("Skip bucket end pull due to late arrival: %lld vs %lld", (long long)bucketEndTime, |
| 404 | (long long)mCurrentBucketStartTimeNs); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 405 | StatsdStats::getInstance().noteLateLogEventSkipped(mMetricId); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 406 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 407 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 408 | for (const auto& data : allData) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 409 | LogEvent localCopy = data->makeCopy(); |
| 410 | if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) == |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 411 | MatchingState::kMatched) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 412 | localCopy.setElapsedTimestampNs(bucketEndTime); |
| 413 | onMatchedLogEventLocked(mWhatMatcherIndex, localCopy); |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 414 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 415 | } |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 416 | mHasGlobalBase = true; |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 417 | } else { |
| 418 | VLOG("No need to commit data on condition false."); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 422 | void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 423 | if (mCurrentSlicedBucket.size() == 0) { |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 428 | (unsigned long)mCurrentSlicedBucket.size()); |
| 429 | if (verbose) { |
| 430 | for (const auto& it : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 431 | for (const auto& interval : it.second) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 432 | fprintf(out, "\t(what)%s\t(condition)%s (value)%s\n", |
| 433 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 434 | it.first.getDimensionKeyInCondition().toString().c_str(), |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 435 | interval.value.toString().c_str()); |
| 436 | } |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 441 | bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 442 | // ===========GuardRail============== |
| 443 | // 1. Report the tuple count if the tuple count > soft limit |
| 444 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 445 | return false; |
| 446 | } |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 447 | if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 448 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 449 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 450 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 451 | if (newTupleCount > mDimensionHardLimit) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 452 | ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId, |
| 453 | newKey.toString().c_str()); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 454 | StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return false; |
| 460 | } |
| 461 | |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 462 | bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) { |
| 463 | // ===========GuardRail============== |
| 464 | // 1. Report the tuple count if the tuple count > soft limit |
| 465 | if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) { |
| 466 | return false; |
| 467 | } |
| 468 | if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) { |
| 469 | size_t newTupleCount = mCurrentFullBucket.size() + 1; |
| 470 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 471 | if (newTupleCount > mDimensionHardLimit) { |
| 472 | ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s", |
| 473 | (long long)mMetricId, |
| 474 | newKey.toString().c_str()); |
| 475 | return true; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return false; |
| 480 | } |
| 481 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 482 | bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) { |
| 483 | for (const FieldValue& value : event.getValues()) { |
| 484 | if (value.mField.matches(matcher)) { |
| 485 | switch (value.mValue.type) { |
| 486 | case INT: |
| 487 | ret.setLong(value.mValue.int_value); |
| 488 | break; |
| 489 | case LONG: |
| 490 | ret.setLong(value.mValue.long_value); |
| 491 | break; |
| 492 | case FLOAT: |
| 493 | ret.setDouble(value.mValue.float_value); |
| 494 | break; |
| 495 | case DOUBLE: |
| 496 | ret.setDouble(value.mValue.double_value); |
| 497 | break; |
| 498 | default: |
| 499 | break; |
| 500 | } |
| 501 | return true; |
| 502 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 503 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 504 | return false; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 507 | void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex, |
| 508 | const MetricDimensionKey& eventKey, |
| 509 | const ConditionKey& conditionKey, |
| 510 | bool condition, const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 511 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 512 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 513 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 514 | (long long)mCurrentBucketStartTimeNs); |
| 515 | return; |
| 516 | } |
| 517 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 518 | flushIfNeededLocked(eventTimeNs); |
| 519 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 520 | // For pulled data, we already check condition when we decide to pull or |
| 521 | // in onDataPulled. So take all of them. |
| 522 | // For pushed data, just check condition. |
| 523 | if (!(mIsPulled || condition)) { |
| 524 | VLOG("ValueMetric skip event because condition is false"); |
| 525 | return; |
| 526 | } |
| 527 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 528 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 529 | return; |
| 530 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 531 | vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey]; |
| 532 | if (multiIntervals.size() < mFieldMatchers.size()) { |
| 533 | VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size()); |
| 534 | multiIntervals.resize(mFieldMatchers.size()); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 535 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 536 | |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 537 | // We only use anomaly detection under certain cases. |
| 538 | // N.B.: The anomaly detection cases were modified in order to fix an issue with value metrics |
| 539 | // containing multiple values. We tried to retain all previous behaviour, but we are unsure the |
| 540 | // previous behaviour was correct. At the time of the fix, anomaly detection had no owner. |
| 541 | // Whoever next works on it should look into the cases where it is triggered in this function. |
| 542 | // Discussion here: http://ag/6124370. |
| 543 | bool useAnomalyDetection = true; |
| 544 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 545 | for (int i = 0; i < (int)mFieldMatchers.size(); i++) { |
| 546 | const Matcher& matcher = mFieldMatchers[i]; |
| 547 | Interval& interval = multiIntervals[i]; |
| 548 | interval.valueIndex = i; |
| 549 | Value value; |
| 550 | if (!getDoubleOrLong(event, matcher, value)) { |
| 551 | 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] | 552 | StatsdStats::getInstance().noteBadValueType(mMetricId); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 553 | return; |
| 554 | } |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 555 | interval.seenNewData = true; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 556 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 557 | if (mUseDiff) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 558 | if (!interval.hasBase) { |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 559 | if (mHasGlobalBase && mUseZeroDefaultBase) { |
| 560 | // The bucket has global base. This key does not. |
| 561 | // Optionally use zero as base. |
| 562 | interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE); |
| 563 | interval.hasBase = true; |
| 564 | } else { |
| 565 | // no base. just update base and return. |
| 566 | interval.base = value; |
| 567 | interval.hasBase = true; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 568 | // If we're missing a base, do not use anomaly detection on incomplete data |
| 569 | useAnomalyDetection = false; |
| 570 | // Continue (instead of return) here in order to set interval.base and |
| 571 | // interval.hasBase for other intervals |
| 572 | continue; |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 573 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 574 | } |
| 575 | Value diff; |
| 576 | switch (mValueDirection) { |
| 577 | case ValueMetric::INCREASING: |
| 578 | if (value >= interval.base) { |
| 579 | diff = value - interval.base; |
| 580 | } else if (mUseAbsoluteValueOnReset) { |
| 581 | diff = value; |
| 582 | } else { |
| 583 | VLOG("Unexpected decreasing value"); |
| 584 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
| 585 | interval.base = value; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 586 | // If we've got bad data, do not use anomaly detection |
| 587 | useAnomalyDetection = false; |
| 588 | continue; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 589 | } |
| 590 | break; |
| 591 | case ValueMetric::DECREASING: |
| 592 | if (interval.base >= value) { |
| 593 | diff = interval.base - value; |
| 594 | } else if (mUseAbsoluteValueOnReset) { |
| 595 | diff = value; |
| 596 | } else { |
| 597 | VLOG("Unexpected increasing value"); |
| 598 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
| 599 | interval.base = value; |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 600 | // If we've got bad data, do not use anomaly detection |
| 601 | useAnomalyDetection = false; |
| 602 | continue; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 603 | } |
| 604 | break; |
| 605 | case ValueMetric::ANY: |
| 606 | diff = value - interval.base; |
| 607 | break; |
| 608 | default: |
| 609 | break; |
| 610 | } |
| 611 | interval.base = value; |
| 612 | value = diff; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 613 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 614 | |
| 615 | if (interval.hasValue) { |
| 616 | switch (mAggregationType) { |
| 617 | case ValueMetric::SUM: |
| 618 | // for AVG, we add up and take average when flushing the bucket |
| 619 | case ValueMetric::AVG: |
| 620 | interval.value += value; |
| 621 | break; |
| 622 | case ValueMetric::MIN: |
| 623 | interval.value = std::min(value, interval.value); |
| 624 | break; |
| 625 | case ValueMetric::MAX: |
| 626 | interval.value = std::max(value, interval.value); |
| 627 | break; |
| 628 | default: |
| 629 | break; |
| 630 | } |
| 631 | } else { |
| 632 | interval.value = value; |
| 633 | interval.hasValue = true; |
| 634 | } |
| 635 | interval.sampleSize += 1; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 636 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 637 | |
Misha Wagner | 2653176 | 2019-01-21 14:18:51 +0000 | [diff] [blame] | 638 | // Only trigger the tracker if all intervals are correct |
| 639 | if (useAnomalyDetection) { |
| 640 | // TODO: propgate proper values down stream when anomaly support doubles |
| 641 | long wholeBucketVal = multiIntervals[0].value.long_value; |
| 642 | auto prev = mCurrentFullBucket.find(eventKey); |
| 643 | if (prev != mCurrentFullBucket.end()) { |
| 644 | wholeBucketVal += prev->second; |
| 645 | } |
| 646 | for (auto& tracker : mAnomalyTrackers) { |
| 647 | tracker->detectAndDeclareAnomaly( |
| 648 | eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal); |
| 649 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 650 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 653 | void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 654 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 655 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 656 | if (eventTimeNs < currentBucketEndTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 657 | 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] | 658 | (long long)(currentBucketEndTimeNs)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 659 | return; |
| 660 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 661 | |
| 662 | flushCurrentBucketLocked(eventTimeNs); |
| 663 | |
| 664 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 665 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 666 | mCurrentBucketNum += numBucketsForward; |
| 667 | |
| 668 | if (numBucketsForward > 1) { |
| 669 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 670 | StatsdStats::getInstance().noteSkippedForwardBuckets(mMetricId); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 671 | // take base again in future good bucket. |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 672 | resetBase(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 673 | } |
| 674 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 675 | (long long)mCurrentBucketStartTimeNs); |
| 676 | } |
| 677 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 678 | void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 679 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 680 | (int)mCurrentSlicedBucket.size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 681 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 682 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 683 | int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 684 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 685 | if (bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 686 | // The current bucket is large enough to keep. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 687 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 688 | ValueBucket bucket; |
| 689 | bucket.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 690 | bucket.mBucketEndNs = bucketEndTime; |
| 691 | for (const auto& interval : slice.second) { |
| 692 | if (interval.hasValue) { |
| 693 | // skip the output if the diff is zero |
| 694 | if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) { |
| 695 | continue; |
| 696 | } |
| 697 | bucket.valueIndex.push_back(interval.valueIndex); |
| 698 | if (mAggregationType != ValueMetric::AVG) { |
| 699 | bucket.values.push_back(interval.value); |
| 700 | } else { |
| 701 | double sum = interval.value.type == LONG ? (double)interval.value.long_value |
| 702 | : interval.value.double_value; |
| 703 | bucket.values.push_back(Value((double)sum / interval.sampleSize)); |
| 704 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 705 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 706 | } |
| 707 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 708 | if (bucket.valueIndex.size() > 0) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 709 | auto& bucketList = mPastBuckets[slice.first]; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 710 | bucketList.push_back(bucket); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 711 | } |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 712 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 713 | } else { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 714 | mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 715 | } |
| 716 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 717 | if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker. |
| 718 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 719 | if (mCurrentFullBucket.size() > 0) { |
| 720 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 721 | if (hitFullBucketGuardRailLocked(slice.first)) { |
| 722 | continue; |
| 723 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 724 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 725 | mCurrentFullBucket[slice.first] += slice.second[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 726 | } |
| 727 | for (const auto& slice : mCurrentFullBucket) { |
| 728 | for (auto& tracker : mAnomalyTrackers) { |
| 729 | if (tracker != nullptr) { |
| 730 | tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | mCurrentFullBucket.clear(); |
| 735 | } else { |
| 736 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 737 | for (const auto& slice : mCurrentSlicedBucket) { |
| 738 | for (auto& tracker : mAnomalyTrackers) { |
| 739 | if (tracker != nullptr) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 740 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 741 | tracker->addPastBucket(slice.first, slice.second[0].value.long_value, |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 742 | mCurrentBucketNum); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } else { |
| 748 | // Accumulate partial bucket. |
| 749 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 750 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 751 | mCurrentFullBucket[slice.first] += slice.second[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 755 | for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) { |
| 756 | bool obsolete = true; |
| 757 | for (auto& interval : it->second) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 758 | interval.hasValue = false; |
| 759 | interval.sampleSize = 0; |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 760 | if (interval.seenNewData) { |
| 761 | obsolete = false; |
| 762 | } |
| 763 | interval.seenNewData = false; |
| 764 | } |
| 765 | |
| 766 | if (obsolete) { |
| 767 | it = mCurrentSlicedBucket.erase(it); |
| 768 | } else { |
| 769 | it++; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 770 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 771 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 774 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 775 | size_t totalSize = 0; |
| 776 | for (const auto& pair : mPastBuckets) { |
| 777 | totalSize += pair.second.size() * kBucketSize; |
| 778 | } |
| 779 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 782 | } // namespace statsd |
| 783 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 784 | } // namespace android |