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