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); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 328 | flushIfNeededLocked(eventTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 329 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 330 | // Pull on condition changes. |
| 331 | if (mIsPulled && (mCondition != condition)) { |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 332 | pullAndMatchEventsLocked(eventTimeNs); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 333 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 334 | |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 335 | // when condition change from true to false, clear diff base but don't |
| 336 | // reset other counters as we may accumulate more value in the bucket. |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 337 | if (mUseDiff && mCondition && !condition) { |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 338 | resetBase(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | mCondition = condition; |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 344 | void ValueMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 345 | vector<std::shared_ptr<LogEvent>> allData; |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 346 | if (!mPullerManager->Pull(mPullTagId, &allData)) { |
| 347 | 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] | 348 | resetBase(); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 349 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 350 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 351 | const int64_t pullDelayNs = getElapsedRealtimeNs() - timestampNs; |
| 352 | if (pullDelayNs > mMaxPullDelayNs) { |
| 353 | ALOGE("Pull finish too late for atom %d, longer than %lld", mPullTagId, |
| 354 | (long long)mMaxPullDelayNs); |
| 355 | StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId); |
| 356 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
| 357 | resetBase(); |
| 358 | return; |
| 359 | } |
| 360 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
| 361 | |
| 362 | for (const auto& data : allData) { |
| 363 | // make a copy before doing and changes |
| 364 | LogEvent localCopy = data->makeCopy(); |
| 365 | localCopy.setElapsedTimestampNs(timestampNs); |
| 366 | if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) == |
| 367 | MatchingState::kMatched) { |
| 368 | onMatchedLogEventLocked(mWhatMatcherIndex, localCopy); |
| 369 | } |
| 370 | } |
| 371 | mHasGlobalBase = true; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 374 | int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) { |
| 375 | return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs; |
| 376 | } |
| 377 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 378 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 379 | std::lock_guard<std::mutex> lock(mMutex); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 380 | if (mCondition) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 381 | if (allData.size() == 0) { |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 382 | VLOG("Data pulled is empty"); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 385 | // 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] | 386 | // bucket end. In the case of waking up from a deep sleep state, we will |
| 387 | // attribute to the previous bucket end. If the sleep was long but not very long, we |
| 388 | // will be in the immediate next bucket. Previous bucket may get a larger number as |
| 389 | // we pull at a later time than real bucket end. |
| 390 | // If the sleep was very long, we skip more than one bucket before sleep. In this case, |
| 391 | // 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] | 392 | int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 393 | int64_t bucketEndTime = calcPreviousBucketEndTime(realEventTime) - 1; |
| 394 | if (bucketEndTime < mCurrentBucketStartTimeNs) { |
| 395 | VLOG("Skip bucket end pull due to late arrival: %lld vs %lld", (long long)bucketEndTime, |
| 396 | (long long)mCurrentBucketStartTimeNs); |
| 397 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 398 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 399 | for (const auto& data : allData) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 400 | LogEvent localCopy = data->makeCopy(); |
| 401 | if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) == |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 402 | MatchingState::kMatched) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 403 | localCopy.setElapsedTimestampNs(bucketEndTime); |
| 404 | onMatchedLogEventLocked(mWhatMatcherIndex, localCopy); |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 405 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 406 | } |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 407 | mHasGlobalBase = true; |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame] | 408 | } else { |
| 409 | VLOG("No need to commit data on condition false."); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 413 | void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 414 | if (mCurrentSlicedBucket.size() == 0) { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 419 | (unsigned long)mCurrentSlicedBucket.size()); |
| 420 | if (verbose) { |
| 421 | for (const auto& it : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 422 | for (const auto& interval : it.second) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 423 | fprintf(out, "\t(what)%s\t(condition)%s (value)%s\n", |
| 424 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 425 | it.first.getDimensionKeyInCondition().toString().c_str(), |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 426 | interval.value.toString().c_str()); |
| 427 | } |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 432 | bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 433 | // ===========GuardRail============== |
| 434 | // 1. Report the tuple count if the tuple count > soft limit |
| 435 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 436 | return false; |
| 437 | } |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 438 | if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 439 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 440 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 441 | // 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] | 442 | if (newTupleCount > mDimensionHardLimit) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 443 | ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId, |
| 444 | newKey.toString().c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return false; |
| 450 | } |
| 451 | |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 452 | bool ValueMetricProducer::hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey) { |
| 453 | // ===========GuardRail============== |
| 454 | // 1. Report the tuple count if the tuple count > soft limit |
| 455 | if (mCurrentFullBucket.find(newKey) != mCurrentFullBucket.end()) { |
| 456 | return false; |
| 457 | } |
| 458 | if (mCurrentFullBucket.size() > mDimensionSoftLimit - 1) { |
| 459 | size_t newTupleCount = mCurrentFullBucket.size() + 1; |
| 460 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 461 | if (newTupleCount > mDimensionHardLimit) { |
| 462 | ALOGE("ValueMetric %lld dropping data for full bucket dimension key %s", |
| 463 | (long long)mMetricId, |
| 464 | newKey.toString().c_str()); |
| 465 | return true; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 472 | bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) { |
| 473 | for (const FieldValue& value : event.getValues()) { |
| 474 | if (value.mField.matches(matcher)) { |
| 475 | switch (value.mValue.type) { |
| 476 | case INT: |
| 477 | ret.setLong(value.mValue.int_value); |
| 478 | break; |
| 479 | case LONG: |
| 480 | ret.setLong(value.mValue.long_value); |
| 481 | break; |
| 482 | case FLOAT: |
| 483 | ret.setDouble(value.mValue.float_value); |
| 484 | break; |
| 485 | case DOUBLE: |
| 486 | ret.setDouble(value.mValue.double_value); |
| 487 | break; |
| 488 | default: |
| 489 | break; |
| 490 | } |
| 491 | return true; |
| 492 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 493 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 494 | return false; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 497 | void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex, |
| 498 | const MetricDimensionKey& eventKey, |
| 499 | const ConditionKey& conditionKey, |
| 500 | bool condition, const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 501 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 502 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 503 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 504 | (long long)mCurrentBucketStartTimeNs); |
| 505 | return; |
| 506 | } |
| 507 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 508 | flushIfNeededLocked(eventTimeNs); |
| 509 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 510 | // For pulled data, we already check condition when we decide to pull or |
| 511 | // in onDataPulled. So take all of them. |
| 512 | // For pushed data, just check condition. |
| 513 | if (!(mIsPulled || condition)) { |
| 514 | VLOG("ValueMetric skip event because condition is false"); |
| 515 | return; |
| 516 | } |
| 517 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 518 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 519 | return; |
| 520 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 521 | vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey]; |
| 522 | if (multiIntervals.size() < mFieldMatchers.size()) { |
| 523 | VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size()); |
| 524 | multiIntervals.resize(mFieldMatchers.size()); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 525 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 526 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 527 | for (int i = 0; i < (int)mFieldMatchers.size(); i++) { |
| 528 | const Matcher& matcher = mFieldMatchers[i]; |
| 529 | Interval& interval = multiIntervals[i]; |
| 530 | interval.valueIndex = i; |
| 531 | Value value; |
| 532 | if (!getDoubleOrLong(event, matcher, value)) { |
| 533 | VLOG("Failed to get value %d from event %s", i, event.ToString().c_str()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 534 | return; |
| 535 | } |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 536 | interval.seenNewData = true; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 537 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 538 | if (mUseDiff) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 539 | if (!interval.hasBase) { |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 540 | if (mHasGlobalBase && mUseZeroDefaultBase) { |
| 541 | // The bucket has global base. This key does not. |
| 542 | // Optionally use zero as base. |
| 543 | interval.base = (value.type == LONG ? ZERO_LONG : ZERO_DOUBLE); |
| 544 | interval.hasBase = true; |
| 545 | } else { |
| 546 | // no base. just update base and return. |
| 547 | interval.base = value; |
| 548 | interval.hasBase = true; |
| 549 | return; |
| 550 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 551 | } |
| 552 | Value diff; |
| 553 | switch (mValueDirection) { |
| 554 | case ValueMetric::INCREASING: |
| 555 | if (value >= interval.base) { |
| 556 | diff = value - interval.base; |
| 557 | } else if (mUseAbsoluteValueOnReset) { |
| 558 | diff = value; |
| 559 | } else { |
| 560 | VLOG("Unexpected decreasing value"); |
| 561 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
| 562 | interval.base = value; |
| 563 | return; |
| 564 | } |
| 565 | break; |
| 566 | case ValueMetric::DECREASING: |
| 567 | if (interval.base >= value) { |
| 568 | diff = interval.base - value; |
| 569 | } else if (mUseAbsoluteValueOnReset) { |
| 570 | diff = value; |
| 571 | } else { |
| 572 | VLOG("Unexpected increasing value"); |
| 573 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
| 574 | interval.base = value; |
| 575 | return; |
| 576 | } |
| 577 | break; |
| 578 | case ValueMetric::ANY: |
| 579 | diff = value - interval.base; |
| 580 | break; |
| 581 | default: |
| 582 | break; |
| 583 | } |
| 584 | interval.base = value; |
| 585 | value = diff; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 586 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 587 | |
| 588 | if (interval.hasValue) { |
| 589 | switch (mAggregationType) { |
| 590 | case ValueMetric::SUM: |
| 591 | // for AVG, we add up and take average when flushing the bucket |
| 592 | case ValueMetric::AVG: |
| 593 | interval.value += value; |
| 594 | break; |
| 595 | case ValueMetric::MIN: |
| 596 | interval.value = std::min(value, interval.value); |
| 597 | break; |
| 598 | case ValueMetric::MAX: |
| 599 | interval.value = std::max(value, interval.value); |
| 600 | break; |
| 601 | default: |
| 602 | break; |
| 603 | } |
| 604 | } else { |
| 605 | interval.value = value; |
| 606 | interval.hasValue = true; |
| 607 | } |
| 608 | interval.sampleSize += 1; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 609 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 610 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 611 | // TODO: propgate proper values down stream when anomaly support doubles |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 612 | long wholeBucketVal = multiIntervals[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 613 | auto prev = mCurrentFullBucket.find(eventKey); |
| 614 | if (prev != mCurrentFullBucket.end()) { |
| 615 | wholeBucketVal += prev->second; |
| 616 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 617 | for (auto& tracker : mAnomalyTrackers) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 618 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 619 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 622 | void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 623 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 624 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 625 | if (eventTimeNs < currentBucketEndTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 626 | 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] | 627 | (long long)(currentBucketEndTimeNs)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 630 | |
| 631 | flushCurrentBucketLocked(eventTimeNs); |
| 632 | |
| 633 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 634 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 635 | mCurrentBucketNum += numBucketsForward; |
| 636 | |
| 637 | if (numBucketsForward > 1) { |
| 638 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 639 | // take base again in future good bucket. |
Chenjie Yu | f275f61 | 2018-11-30 23:29:06 -0800 | [diff] [blame] | 640 | resetBase(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 641 | } |
| 642 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 643 | (long long)mCurrentBucketStartTimeNs); |
| 644 | } |
| 645 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 646 | void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 647 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 648 | (int)mCurrentSlicedBucket.size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 649 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 650 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 651 | int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 652 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 653 | if (bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 654 | // The current bucket is large enough to keep. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 655 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 656 | ValueBucket bucket; |
| 657 | bucket.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 658 | bucket.mBucketEndNs = bucketEndTime; |
| 659 | for (const auto& interval : slice.second) { |
| 660 | if (interval.hasValue) { |
| 661 | // skip the output if the diff is zero |
| 662 | if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) { |
| 663 | continue; |
| 664 | } |
| 665 | bucket.valueIndex.push_back(interval.valueIndex); |
| 666 | if (mAggregationType != ValueMetric::AVG) { |
| 667 | bucket.values.push_back(interval.value); |
| 668 | } else { |
| 669 | double sum = interval.value.type == LONG ? (double)interval.value.long_value |
| 670 | : interval.value.double_value; |
| 671 | bucket.values.push_back(Value((double)sum / interval.sampleSize)); |
| 672 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 673 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 674 | } |
| 675 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 676 | if (bucket.valueIndex.size() > 0) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 677 | auto& bucketList = mPastBuckets[slice.first]; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 678 | bucketList.push_back(bucket); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 679 | } |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 680 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 681 | } else { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 682 | mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 683 | } |
| 684 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 685 | if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker. |
| 686 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 687 | if (mCurrentFullBucket.size() > 0) { |
| 688 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 689 | if (hitFullBucketGuardRailLocked(slice.first)) { |
| 690 | continue; |
| 691 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 692 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 693 | mCurrentFullBucket[slice.first] += slice.second[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 694 | } |
| 695 | for (const auto& slice : mCurrentFullBucket) { |
| 696 | for (auto& tracker : mAnomalyTrackers) { |
| 697 | if (tracker != nullptr) { |
| 698 | tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | mCurrentFullBucket.clear(); |
| 703 | } else { |
| 704 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 705 | for (const auto& slice : mCurrentSlicedBucket) { |
| 706 | for (auto& tracker : mAnomalyTrackers) { |
| 707 | if (tracker != nullptr) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 708 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 709 | tracker->addPastBucket(slice.first, slice.second[0].value.long_value, |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 710 | mCurrentBucketNum); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } else { |
| 716 | // Accumulate partial bucket. |
| 717 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 718 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 719 | mCurrentFullBucket[slice.first] += slice.second[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 723 | for (auto it = mCurrentSlicedBucket.begin(); it != mCurrentSlicedBucket.end();) { |
| 724 | bool obsolete = true; |
| 725 | for (auto& interval : it->second) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 726 | interval.hasValue = false; |
| 727 | interval.sampleSize = 0; |
Chenjie Yu | dbe5c50 | 2018-11-30 23:15:57 -0800 | [diff] [blame] | 728 | if (interval.seenNewData) { |
| 729 | obsolete = false; |
| 730 | } |
| 731 | interval.seenNewData = false; |
| 732 | } |
| 733 | |
| 734 | if (obsolete) { |
| 735 | it = mCurrentSlicedBucket.erase(it); |
| 736 | } else { |
| 737 | it++; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 738 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 739 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 742 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 743 | size_t totalSize = 0; |
| 744 | for (const auto& pair : mPastBuckets) { |
| 745 | totalSize += pair.second.size() * kBucketSize; |
| 746 | } |
| 747 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 750 | } // namespace statsd |
| 751 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 752 | } // namespace android |