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; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 55 | // for ValueMetricDataWrapper |
| 56 | const int FIELD_ID_DATA = 1; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 57 | const int FIELD_ID_SKIPPED = 2; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 58 | const int FIELD_ID_SKIPPED_START_MILLIS = 3; |
| 59 | const int FIELD_ID_SKIPPED_END_MILLIS = 4; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | // for ValueMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 61 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 62 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 63 | const int FIELD_ID_BUCKET_INFO = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 64 | const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4; |
| 65 | const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 66 | // for ValueBucketInfo |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 67 | const int FIELD_ID_VALUE_INDEX = 1; |
| 68 | const int FIELD_ID_VALUE_LONG = 2; |
| 69 | const int FIELD_ID_VALUE_DOUBLE = 3; |
| 70 | const int FIELD_ID_VALUES = 9; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 71 | const int FIELD_ID_BUCKET_NUM = 4; |
| 72 | const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5; |
| 73 | const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 74 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 75 | // ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 76 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 77 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 78 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 79 | const int64_t timeBaseNs, const int64_t startTimeNs, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 80 | const sp<StatsPullerManager>& pullerManager) |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 81 | : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 82 | mPullerManager(pullerManager), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 83 | mPullTagId(pullTagId), |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 84 | mIsPulled(pullTagId != -1), |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 85 | mMinBucketSizeNs(metric.min_bucket_size_nanos()), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 86 | mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 87 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 88 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first |
| 89 | : StatsdStats::kDimensionKeySizeSoftLimit), |
| 90 | mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 91 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 92 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second |
Chenjie Yu | 4723464 | 2018-05-14 10:14:16 -0700 | [diff] [blame] | 93 | : StatsdStats::kDimensionKeySizeHardLimit), |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 94 | mUseAbsoluteValueOnReset(metric.use_absolute_value_on_reset()), |
| 95 | mAggregationType(metric.aggregation_type()), |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 96 | mUseDiff(metric.has_use_diff() ? metric.use_diff() : (mIsPulled ? true : false)), |
| 97 | mValueDirection(metric.value_direction()), |
| 98 | mSkipZeroDiffOutput(metric.skip_zero_diff_output()) { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 99 | int64_t bucketSizeMills = 0; |
| 100 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 101 | bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 102 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 103 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 104 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 105 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 106 | mBucketSizeNs = bucketSizeMills * 1000000; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 107 | |
| 108 | translateFieldMatcher(metric.value_field(), &mFieldMatchers); |
| 109 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 110 | if (metric.has_dimensions_in_what()) { |
| 111 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 112 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (metric.has_dimensions_in_condition()) { |
| 116 | translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition); |
| 117 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 118 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 119 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 120 | for (const auto& link : metric.links()) { |
| 121 | Metric2Condition mc; |
| 122 | mc.conditionId = link.condition(); |
| 123 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 124 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 125 | mMetric2ConditionLinks.push_back(mc); |
| 126 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 127 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 128 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 129 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 130 | mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) || |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 131 | HasPositionALL(metric.dimensions_in_condition()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 132 | |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 133 | flushIfNeededLocked(startTimeNs); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 134 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 135 | if (mIsPulled) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 136 | mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(), |
| 137 | mBucketSizeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 138 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 139 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 140 | // 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] | 141 | // flushIfNeeded to adjust start and end to bucket boundaries. |
| 142 | // Adjust start for partial bucket |
| 143 | mCurrentBucketStartTimeNs = startTimeNs; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 144 | // Kicks off the puller immediately if condition is true and diff based. |
| 145 | if (mIsPulled && mCondition && mUseDiff) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 146 | pullLocked(startTimeNs); |
| 147 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 148 | VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
| 149 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | ValueMetricProducer::~ValueMetricProducer() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 153 | VLOG("~ValueMetricProducer() called"); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 154 | if (mIsPulled) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 155 | mPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 156 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 159 | void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 160 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 161 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 164 | void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 165 | flushIfNeededLocked(dropTimeNs); |
| 166 | mPastBuckets.clear(); |
| 167 | } |
| 168 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 169 | void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 170 | flushIfNeededLocked(dumpTimeNs); |
| 171 | mPastBuckets.clear(); |
| 172 | mSkippedBuckets.clear(); |
| 173 | } |
| 174 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 175 | void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 176 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 177 | const bool erase_data, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 178 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 179 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 180 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 181 | if (include_current_partial_bucket) { |
| 182 | flushLocked(dumpTimeNs); |
| 183 | } else { |
| 184 | flushIfNeededLocked(dumpTimeNs); |
| 185 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 186 | if (mPastBuckets.empty() && mSkippedBuckets.empty()) { |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 187 | return; |
| 188 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 189 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 190 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs); |
| 191 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs); |
| 192 | // Fills the dimension path if not slicing by ALL. |
| 193 | if (!mSliceByPositionALL) { |
| 194 | if (!mDimensionsInWhat.empty()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 195 | uint64_t dimenPathToken = |
| 196 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 197 | writeDimensionPathToProto(mDimensionsInWhat, protoOutput); |
| 198 | protoOutput->end(dimenPathToken); |
| 199 | } |
| 200 | if (!mDimensionsInCondition.empty()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 201 | uint64_t dimenPathToken = |
| 202 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 203 | writeDimensionPathToProto(mDimensionsInCondition, protoOutput); |
| 204 | protoOutput->end(dimenPathToken); |
| 205 | } |
| 206 | } |
| 207 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 208 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 209 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 210 | for (const auto& pair : mSkippedBuckets) { |
| 211 | uint64_t wrapperToken = |
| 212 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 213 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS, |
| 214 | (long long)(NanoToMillis(pair.first))); |
| 215 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS, |
| 216 | (long long)(NanoToMillis(pair.second))); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 217 | protoOutput->end(wrapperToken); |
| 218 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 219 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 220 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 221 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 222 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 223 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 224 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 225 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 226 | // First fill dimension. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 227 | if (mSliceByPositionALL) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 228 | uint64_t dimensionToken = |
| 229 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 230 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput); |
| 231 | protoOutput->end(dimensionToken); |
| 232 | if (dimensionKey.hasDimensionKeyInCondition()) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 233 | uint64_t dimensionInConditionToken = |
| 234 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
| 235 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), str_set, |
| 236 | protoOutput); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 237 | protoOutput->end(dimensionInConditionToken); |
| 238 | } |
| 239 | } else { |
| 240 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(), |
| 241 | FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput); |
| 242 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 243 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(), |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 244 | FIELD_ID_DIMENSION_LEAF_IN_CONDITION, str_set, |
| 245 | protoOutput); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 246 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 247 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 248 | |
| 249 | // Then fill bucket_info (ValueBucketInfo). |
| 250 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 251 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 252 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 253 | |
| 254 | if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) { |
| 255 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS, |
| 256 | (long long)NanoToMillis(bucket.mBucketStartNs)); |
| 257 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS, |
| 258 | (long long)NanoToMillis(bucket.mBucketEndNs)); |
| 259 | } else { |
| 260 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM, |
| 261 | (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs))); |
| 262 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 263 | for (int i = 0; i < (int)bucket.valueIndex.size(); i ++) { |
| 264 | int index = bucket.valueIndex[i]; |
| 265 | const Value& value = bucket.values[i]; |
| 266 | uint64_t valueToken = protoOutput->start( |
| 267 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_VALUES); |
| 268 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_INDEX, |
| 269 | index); |
| 270 | if (value.getType() == LONG) { |
| 271 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_LONG, |
| 272 | (long long)value.long_value); |
| 273 | VLOG("\t bucket [%lld - %lld] value %d: %lld", (long long)bucket.mBucketStartNs, |
| 274 | (long long)bucket.mBucketEndNs, index, (long long)value.long_value); |
| 275 | } else if (value.getType() == DOUBLE) { |
| 276 | protoOutput->write(FIELD_TYPE_DOUBLE | FIELD_ID_VALUE_DOUBLE, |
| 277 | value.double_value); |
| 278 | VLOG("\t bucket [%lld - %lld] value %d: %.2f", (long long)bucket.mBucketStartNs, |
| 279 | (long long)bucket.mBucketEndNs, index, value.double_value); |
| 280 | } else { |
| 281 | VLOG("Wrong value type for ValueMetric output: %d", value.getType()); |
| 282 | } |
| 283 | protoOutput->end(valueToken); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 284 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 285 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 286 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 287 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 288 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 289 | protoOutput->end(protoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 290 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 291 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 292 | if (erase_data) { |
| 293 | mPastBuckets.clear(); |
| 294 | mSkippedBuckets.clear(); |
| 295 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 298 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 299 | const int64_t eventTimeNs) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 300 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 301 | 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] | 302 | (long long)mCurrentBucketStartTimeNs); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 306 | flushIfNeededLocked(eventTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 307 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 308 | // Pull on condition changes. |
| 309 | if (mIsPulled && (mCondition != condition)) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 310 | pullLocked(eventTimeNs); |
| 311 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 312 | |
| 313 | // when condition change from true to false, clear diff base |
| 314 | if (mUseDiff && mCondition && !condition) { |
| 315 | for (auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 316 | for (auto& interval : slice.second) { |
| 317 | interval.hasBase = false; |
| 318 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | mCondition = condition; |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void ValueMetricProducer::pullLocked(const int64_t timestampNs) { |
| 326 | vector<std::shared_ptr<LogEvent>> allData; |
| 327 | if (mPullerManager->Pull(mPullTagId, timestampNs, &allData)) { |
| 328 | if (allData.size() == 0) { |
| 329 | return; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 330 | } |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 331 | for (const auto& data : allData) { |
| 332 | onMatchedLogEventLocked(0, *data); |
| 333 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 334 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 337 | int64_t ValueMetricProducer::calcPreviousBucketEndTime(const int64_t currentTimeNs) { |
| 338 | return mTimeBaseNs + ((currentTimeNs - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs; |
| 339 | } |
| 340 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 341 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 342 | std::lock_guard<std::mutex> lock(mMutex); |
| 343 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 344 | if (mCondition) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 345 | if (allData.size() == 0) { |
| 346 | return; |
| 347 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 348 | // 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] | 349 | // bucket end. In the case of waking up from a deep sleep state, we will |
| 350 | // attribute to the previous bucket end. If the sleep was long but not very long, we |
| 351 | // will be in the immediate next bucket. Previous bucket may get a larger number as |
| 352 | // we pull at a later time than real bucket end. |
| 353 | // If the sleep was very long, we skip more than one bucket before sleep. In this case, |
| 354 | // 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] | 355 | int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 356 | int64_t bucketEndTime = calcPreviousBucketEndTime(realEventTime) - 1; |
| 357 | if (bucketEndTime < mCurrentBucketStartTimeNs) { |
| 358 | VLOG("Skip bucket end pull due to late arrival: %lld vs %lld", (long long)bucketEndTime, |
| 359 | (long long)mCurrentBucketStartTimeNs); |
| 360 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 361 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 362 | for (const auto& data : allData) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 363 | data->setElapsedTimestampNs(bucketEndTime); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 364 | onMatchedLogEventLocked(0, *data); |
| 365 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 369 | void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 370 | if (mCurrentSlicedBucket.size() == 0) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 375 | (unsigned long)mCurrentSlicedBucket.size()); |
| 376 | if (verbose) { |
| 377 | for (const auto& it : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 378 | for (const auto& interval : it.second) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 379 | fprintf(out, "\t(what)%s\t(condition)%s (value)%s\n", |
| 380 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 381 | it.first.getDimensionKeyInCondition().toString().c_str(), |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 382 | interval.value.toString().c_str()); |
| 383 | } |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 388 | bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 389 | // ===========GuardRail============== |
| 390 | // 1. Report the tuple count if the tuple count > soft limit |
| 391 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 392 | return false; |
| 393 | } |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 394 | if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 395 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 396 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 397 | // 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] | 398 | if (newTupleCount > mDimensionHardLimit) { |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 399 | ALOGE("ValueMetric %lld dropping data for dimension key %s", (long long)mMetricId, |
| 400 | newKey.toString().c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return false; |
| 406 | } |
| 407 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 408 | bool getDoubleOrLong(const LogEvent& event, const Matcher& matcher, Value& ret) { |
| 409 | for (const FieldValue& value : event.getValues()) { |
| 410 | if (value.mField.matches(matcher)) { |
| 411 | switch (value.mValue.type) { |
| 412 | case INT: |
| 413 | ret.setLong(value.mValue.int_value); |
| 414 | break; |
| 415 | case LONG: |
| 416 | ret.setLong(value.mValue.long_value); |
| 417 | break; |
| 418 | case FLOAT: |
| 419 | ret.setDouble(value.mValue.float_value); |
| 420 | break; |
| 421 | case DOUBLE: |
| 422 | ret.setDouble(value.mValue.double_value); |
| 423 | break; |
| 424 | default: |
| 425 | break; |
| 426 | } |
| 427 | return true; |
| 428 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 429 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 430 | return false; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 433 | void ValueMetricProducer::onMatchedLogEventInternalLocked(const size_t matcherIndex, |
| 434 | const MetricDimensionKey& eventKey, |
| 435 | const ConditionKey& conditionKey, |
| 436 | bool condition, const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 437 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 438 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 439 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 440 | (long long)mCurrentBucketStartTimeNs); |
| 441 | return; |
| 442 | } |
| 443 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 444 | flushIfNeededLocked(eventTimeNs); |
| 445 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 446 | // For pulled data, we already check condition when we decide to pull or |
| 447 | // in onDataPulled. So take all of them. |
| 448 | // For pushed data, just check condition. |
| 449 | if (!(mIsPulled || condition)) { |
| 450 | VLOG("ValueMetric skip event because condition is false"); |
| 451 | return; |
| 452 | } |
| 453 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 454 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 455 | return; |
| 456 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 457 | vector<Interval>& multiIntervals = mCurrentSlicedBucket[eventKey]; |
| 458 | if (multiIntervals.size() < mFieldMatchers.size()) { |
| 459 | VLOG("Resizing number of intervals to %d", (int)mFieldMatchers.size()); |
| 460 | multiIntervals.resize(mFieldMatchers.size()); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 461 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 462 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 463 | for (int i = 0; i < (int)mFieldMatchers.size(); i++) { |
| 464 | const Matcher& matcher = mFieldMatchers[i]; |
| 465 | Interval& interval = multiIntervals[i]; |
| 466 | interval.valueIndex = i; |
| 467 | Value value; |
| 468 | if (!getDoubleOrLong(event, matcher, value)) { |
| 469 | 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] | 470 | return; |
| 471 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 472 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 473 | if (mUseDiff) { |
| 474 | // no base. just update base and return. |
| 475 | if (!interval.hasBase) { |
| 476 | interval.base = value; |
| 477 | interval.hasBase = true; |
| 478 | return; |
| 479 | } |
| 480 | Value diff; |
| 481 | switch (mValueDirection) { |
| 482 | case ValueMetric::INCREASING: |
| 483 | if (value >= interval.base) { |
| 484 | diff = value - interval.base; |
| 485 | } else if (mUseAbsoluteValueOnReset) { |
| 486 | diff = value; |
| 487 | } else { |
| 488 | VLOG("Unexpected decreasing value"); |
| 489 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
| 490 | interval.base = value; |
| 491 | return; |
| 492 | } |
| 493 | break; |
| 494 | case ValueMetric::DECREASING: |
| 495 | if (interval.base >= value) { |
| 496 | diff = interval.base - value; |
| 497 | } else if (mUseAbsoluteValueOnReset) { |
| 498 | diff = value; |
| 499 | } else { |
| 500 | VLOG("Unexpected increasing value"); |
| 501 | StatsdStats::getInstance().notePullDataError(mPullTagId); |
| 502 | interval.base = value; |
| 503 | return; |
| 504 | } |
| 505 | break; |
| 506 | case ValueMetric::ANY: |
| 507 | diff = value - interval.base; |
| 508 | break; |
| 509 | default: |
| 510 | break; |
| 511 | } |
| 512 | interval.base = value; |
| 513 | value = diff; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 514 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 515 | |
| 516 | if (interval.hasValue) { |
| 517 | switch (mAggregationType) { |
| 518 | case ValueMetric::SUM: |
| 519 | // for AVG, we add up and take average when flushing the bucket |
| 520 | case ValueMetric::AVG: |
| 521 | interval.value += value; |
| 522 | break; |
| 523 | case ValueMetric::MIN: |
| 524 | interval.value = std::min(value, interval.value); |
| 525 | break; |
| 526 | case ValueMetric::MAX: |
| 527 | interval.value = std::max(value, interval.value); |
| 528 | break; |
| 529 | default: |
| 530 | break; |
| 531 | } |
| 532 | } else { |
| 533 | interval.value = value; |
| 534 | interval.hasValue = true; |
| 535 | } |
| 536 | interval.sampleSize += 1; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 537 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 538 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 539 | // TODO: propgate proper values down stream when anomaly support doubles |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 540 | long wholeBucketVal = multiIntervals[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 541 | auto prev = mCurrentFullBucket.find(eventKey); |
| 542 | if (prev != mCurrentFullBucket.end()) { |
| 543 | wholeBucketVal += prev->second; |
| 544 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 545 | for (auto& tracker : mAnomalyTrackers) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 546 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 547 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 550 | void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 551 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 552 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 553 | if (eventTimeNs < currentBucketEndTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 554 | 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] | 555 | (long long)(currentBucketEndTimeNs)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 558 | |
| 559 | flushCurrentBucketLocked(eventTimeNs); |
| 560 | |
| 561 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 562 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 563 | mCurrentBucketNum += numBucketsForward; |
| 564 | |
| 565 | if (numBucketsForward > 1) { |
| 566 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 567 | // take base again in future good bucket. |
| 568 | for (auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 569 | for (auto& interval : slice.second) { |
| 570 | interval.hasBase = false; |
| 571 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 572 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 573 | } |
| 574 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 575 | (long long)mCurrentBucketStartTimeNs); |
| 576 | } |
| 577 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 578 | void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 579 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 580 | (int)mCurrentSlicedBucket.size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 581 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 582 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 583 | int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 584 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 585 | if (bucketEndTime - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 586 | // The current bucket is large enough to keep. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 587 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 588 | ValueBucket bucket; |
| 589 | bucket.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 590 | bucket.mBucketEndNs = bucketEndTime; |
| 591 | for (const auto& interval : slice.second) { |
| 592 | if (interval.hasValue) { |
| 593 | // skip the output if the diff is zero |
| 594 | if (mSkipZeroDiffOutput && mUseDiff && interval.value.isZero()) { |
| 595 | continue; |
| 596 | } |
| 597 | bucket.valueIndex.push_back(interval.valueIndex); |
| 598 | if (mAggregationType != ValueMetric::AVG) { |
| 599 | bucket.values.push_back(interval.value); |
| 600 | } else { |
| 601 | double sum = interval.value.type == LONG ? (double)interval.value.long_value |
| 602 | : interval.value.double_value; |
| 603 | bucket.values.push_back(Value((double)sum / interval.sampleSize)); |
| 604 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 605 | } |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 606 | } |
| 607 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 608 | if (bucket.valueIndex.size() > 0) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 609 | auto& bucketList = mPastBuckets[slice.first]; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 610 | bucketList.push_back(bucket); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 611 | } |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 612 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 613 | } else { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 614 | mSkippedBuckets.emplace_back(mCurrentBucketStartTimeNs, bucketEndTime); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 615 | } |
| 616 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 617 | if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker. |
| 618 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 619 | if (mCurrentFullBucket.size() > 0) { |
| 620 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 621 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 622 | mCurrentFullBucket[slice.first] += slice.second[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 623 | } |
| 624 | for (const auto& slice : mCurrentFullBucket) { |
| 625 | for (auto& tracker : mAnomalyTrackers) { |
| 626 | if (tracker != nullptr) { |
| 627 | tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | mCurrentFullBucket.clear(); |
| 632 | } else { |
| 633 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 634 | for (const auto& slice : mCurrentSlicedBucket) { |
| 635 | for (auto& tracker : mAnomalyTrackers) { |
| 636 | if (tracker != nullptr) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 637 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 638 | tracker->addPastBucket(slice.first, slice.second[0].value.long_value, |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 639 | mCurrentBucketNum); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | } else { |
| 645 | // Accumulate partial bucket. |
| 646 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 647 | // TODO: fix this when anomaly can accept double values |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 648 | mCurrentFullBucket[slice.first] += slice.second[0].value.long_value; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 652 | // Reset counters |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 653 | for (auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame^] | 654 | for (auto& interval : slice.second) { |
| 655 | interval.hasValue = false; |
| 656 | interval.sampleSize = 0; |
| 657 | } |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 658 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 661 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 662 | size_t totalSize = 0; |
| 663 | for (const auto& pair : mPastBuckets) { |
| 664 | totalSize += pair.second.size() * kBucketSize; |
| 665 | } |
| 666 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 669 | } // namespace statsd |
| 670 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 671 | } // namespace android |