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