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