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 | |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 20 | #include "dimension.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 21 | #include "ValueMetricProducer.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 22 | #include "guardrail/StatsdStats.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 23 | #include "stats_log_util.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 24 | |
| 25 | #include <cutils/log.h> |
| 26 | #include <limits.h> |
| 27 | #include <stdlib.h> |
| 28 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 29 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 30 | using android::util::FIELD_TYPE_BOOL; |
| 31 | using android::util::FIELD_TYPE_FLOAT; |
| 32 | using android::util::FIELD_TYPE_INT32; |
| 33 | using android::util::FIELD_TYPE_INT64; |
| 34 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 35 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 36 | using android::util::ProtoOutputStream; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 37 | using std::list; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 38 | using std::make_pair; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 39 | using std::make_shared; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 40 | using std::map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 41 | using std::shared_ptr; |
| 42 | using std::unique_ptr; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 43 | using std::unordered_map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | namespace os { |
| 47 | namespace statsd { |
| 48 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 49 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 50 | const int FIELD_ID_ID = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 51 | const int FIELD_ID_VALUE_METRICS = 7; |
| 52 | // for ValueMetricDataWrapper |
| 53 | const int FIELD_ID_DATA = 1; |
| 54 | // for ValueMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 55 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 56 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 57 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 58 | // for ValueBucketInfo |
| 59 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 60 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 61 | const int FIELD_ID_VALUE = 3; |
| 62 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 63 | // 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] | 64 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 65 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 66 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 67 | const uint64_t startTimeNs, |
| 68 | shared_ptr<StatsPullerManager> statsPullerManager) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 69 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 70 | mValueField(metric.value_field()), |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 71 | mStatsPullerManager(statsPullerManager), |
| 72 | mPullTagId(pullTagId) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 73 | // TODO: valuemetric for pushed events may need unlimited bucket length |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 74 | int64_t bucketSizeMills = 0; |
| 75 | if (metric.has_bucket()) { |
| 76 | bucketSizeMills = TimeUnitToBucketSizeInMillis(metric.bucket()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 77 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 78 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 79 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 80 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 81 | mBucketSizeNs = bucketSizeMills * 1000000; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 82 | mDimensionsInWhat = metric.dimensions_in_what(); |
| 83 | mDimensionsInCondition = metric.dimensions_in_condition(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 84 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 85 | if (metric.links().size() > 0) { |
| 86 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 87 | metric.links().end()); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 88 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 89 | mConditionSliced = (metric.links().size() > 0)|| |
| 90 | (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 91 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 92 | if (!metric.has_condition() && mPullTagId != -1) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 93 | VLOG("Setting up periodic pulling for %d", mPullTagId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 94 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, bucketSizeMills); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 95 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 96 | VLOG("value metric %lld created. bucket size %lld start_time: %lld", |
| 97 | (long long)metric.id(), (long long)mBucketSizeNs, (long long)mStartTimeNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 100 | // for testing |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 101 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 102 | const int conditionIndex, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 103 | const sp<ConditionWizard>& wizard, const int pullTagId, |
| 104 | const uint64_t startTimeNs) |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 105 | : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 106 | make_shared<StatsPullerManager>()) { |
| 107 | } |
| 108 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 109 | ValueMetricProducer::~ValueMetricProducer() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 110 | VLOG("~ValueMetricProducer() called"); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 111 | if (mPullTagId != -1) { |
| 112 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
| 113 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 116 | void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 117 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 120 | void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) { |
| 121 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 122 | report->set_metric_id(mMetricId); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 123 | auto value_metrics = report->mutable_value_metrics(); |
| 124 | for (const auto& pair : mPastBuckets) { |
| 125 | ValueMetricData* metricData = value_metrics->add_data(); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 126 | *metricData->mutable_dimensions_in_what() = |
| 127 | pair.first.getDimensionKeyInWhat().getDimensionsValue(); |
| 128 | *metricData->mutable_dimensions_in_condition() = |
| 129 | pair.first.getDimensionKeyInCondition().getDimensionsValue(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 130 | for (const auto& bucket : pair.second) { |
| 131 | ValueBucketInfo* bucketInfo = metricData->add_bucket_info(); |
| 132 | bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs); |
| 133 | bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs); |
| 134 | bucketInfo->set_value(bucket.mValue); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 139 | void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 140 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 141 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 142 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 143 | if (mPastBuckets.empty()) { |
| 144 | return; |
| 145 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 146 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 147 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 148 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 149 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 150 | const MetricDimensionKey& dimensionKey = pair.first; |
| 151 | VLOG(" dimension key %s", dimensionKey.c_str()); |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 152 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 153 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 154 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 155 | // First fill dimension. |
| 156 | long long dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 157 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 158 | writeDimensionsValueProtoToStream( |
| 159 | dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 160 | protoOutput->end(dimensionToken); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 161 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 162 | long long dimensionInConditionToken = protoOutput->start( |
| 163 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
| 164 | writeDimensionsValueProtoToStream( |
| 165 | dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput); |
| 166 | protoOutput->end(dimensionInConditionToken); |
| 167 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 168 | |
| 169 | // Then fill bucket_info (ValueBucketInfo). |
| 170 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 171 | long long bucketInfoToken = protoOutput->start( |
| 172 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 173 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 174 | (long long)bucket.mBucketStartNs); |
| 175 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 176 | (long long)bucket.mBucketEndNs); |
| 177 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue); |
| 178 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 179 | VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs, |
| 180 | (long long)bucket.mBucketEndNs, (long long)bucket.mValue); |
| 181 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 182 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 183 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 184 | protoOutput->end(protoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 185 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 186 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 187 | mPastBuckets.clear(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 188 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 191 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 192 | mCondition = condition; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 193 | |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 194 | if (eventTime < mCurrentBucketStartTimeNs) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 195 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTime, |
| 196 | (long long)mCurrentBucketStartTimeNs); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 200 | flushIfNeededLocked(eventTime); |
| 201 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 202 | if (mPullTagId != -1) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 203 | if (mCondition == true) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 204 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, mBucketSizeNs / 1000 / 1000); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 205 | } else if (mCondition == false) { |
| 206 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 207 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 208 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 209 | vector<shared_ptr<LogEvent>> allData; |
| 210 | if (mStatsPullerManager->Pull(mPullTagId, &allData)) { |
| 211 | if (allData.size() == 0) { |
| 212 | return; |
| 213 | } |
| 214 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 215 | onMatchedLogEventLocked(0, *data); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 216 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 217 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 218 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 219 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 223 | std::lock_guard<std::mutex> lock(mMutex); |
| 224 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 225 | if (mCondition == true || mConditionTrackerIndex < 0) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 226 | if (allData.size() == 0) { |
| 227 | return; |
| 228 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 229 | // For scheduled pulled data, the effective event time is snap to the nearest |
| 230 | // bucket boundary to make bucket finalize. |
| 231 | uint64_t realEventTime = allData.at(0)->GetTimestampNs(); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 232 | uint64_t eventTime = mStartTimeNs + |
| 233 | ((realEventTime - mStartTimeNs)/mBucketSizeNs) * mBucketSizeNs; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 234 | |
| 235 | mCondition = false; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 236 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 237 | data->setTimestampNs(eventTime-1); |
| 238 | onMatchedLogEventLocked(0, *data); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 239 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 240 | |
| 241 | mCondition = true; |
| 242 | for (const auto& data : allData) { |
| 243 | data->setTimestampNs(eventTime); |
| 244 | onMatchedLogEventLocked(0, *data); |
| 245 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 249 | bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 250 | // ===========GuardRail============== |
| 251 | // 1. Report the tuple count if the tuple count > soft limit |
| 252 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 253 | return false; |
| 254 | } |
| 255 | if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 256 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 257 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 258 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 259 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 260 | ALOGE("ValueMetric %lld dropping data for dimension key %s", |
| 261 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return false; |
| 267 | } |
| 268 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 269 | void ValueMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 270 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 271 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 272 | const LogEvent& event) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 273 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 274 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 275 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 276 | (long long)mCurrentBucketStartTimeNs); |
| 277 | return; |
| 278 | } |
| 279 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 280 | flushIfNeededLocked(eventTimeNs); |
| 281 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 282 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 283 | return; |
| 284 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 285 | Interval& interval = mCurrentSlicedBucket[eventKey]; |
| 286 | |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 287 | std::shared_ptr<FieldValueMap> valueFieldMap = getValueFields(event); |
| 288 | if (valueFieldMap->empty() || valueFieldMap->size() > 1) { |
| 289 | return; |
| 290 | } |
| 291 | const long value = getLongFromDimenValue(valueFieldMap->begin()->second); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 292 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 293 | if (mPullTagId != -1) { // for pulled events |
| 294 | if (mCondition == true) { |
| 295 | interval.start = value; |
| 296 | interval.startUpdated = true; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 297 | } else { |
Chenjie Yu | 0ed268b | 2018-01-10 09:37:10 -0800 | [diff] [blame] | 298 | // Generally we expect value to be monotonically increasing. |
| 299 | // If not, there was a reset event. We take the absolute value as |
| 300 | // diff in this case. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 301 | if (interval.startUpdated) { |
Chenjie Yu | 0ed268b | 2018-01-10 09:37:10 -0800 | [diff] [blame] | 302 | if (value > interval.start) { |
| 303 | interval.sum += (value - interval.start); |
| 304 | } else { |
| 305 | interval.sum += value; |
| 306 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 307 | interval.startUpdated = false; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 308 | } else { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 309 | VLOG("No start for matching end %ld", value); |
| 310 | interval.tainted += 1; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 313 | } else { // for pushed events |
| 314 | interval.sum += value; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 315 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 316 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 317 | long wholeBucketVal = interval.sum; |
| 318 | auto prev = mCurrentFullBucket.find(eventKey); |
| 319 | if (prev != mCurrentFullBucket.end()) { |
| 320 | wholeBucketVal += prev->second; |
| 321 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 322 | for (auto& tracker : mAnomalyTrackers) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 323 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 324 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 327 | std::shared_ptr<FieldValueMap> ValueMetricProducer::getValueFields(const LogEvent& event) { |
| 328 | std::shared_ptr<FieldValueMap> valueFields = |
| 329 | std::make_shared<FieldValueMap>(event.getFieldValueMap()); |
| 330 | filterFields(mValueField, valueFields.get()); |
| 331 | return valueFields; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 334 | void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 335 | uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 336 | |
| 337 | if (currentBucketEndTimeNs > eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 338 | 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] | 339 | (long long)(currentBucketEndTimeNs)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 342 | |
| 343 | flushCurrentBucketLocked(eventTimeNs); |
| 344 | |
| 345 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 346 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 347 | mCurrentBucketNum += numBucketsForward; |
| 348 | |
| 349 | if (numBucketsForward > 1) { |
| 350 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
| 351 | } |
| 352 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 353 | (long long)mCurrentBucketStartTimeNs); |
| 354 | } |
| 355 | |
| 356 | void ValueMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 357 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 358 | (int)mCurrentSlicedBucket.size()); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 359 | uint64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 360 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 361 | ValueBucket info; |
| 362 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 363 | if (eventTimeNs < fullBucketEndTimeNs) { |
| 364 | info.mBucketEndNs = eventTimeNs; |
| 365 | } else { |
| 366 | info.mBucketEndNs = fullBucketEndTimeNs; |
| 367 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 368 | info.mBucketNum = mCurrentBucketNum; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 369 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 370 | int tainted = 0; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 371 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 372 | tainted += slice.second.tainted; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 373 | info.mValue = slice.second.sum; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 374 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 375 | auto& bucketList = mPastBuckets[slice.first]; |
| 376 | bucketList.push_back(info); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 377 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 378 | VLOG("%d tainted pairs in the bucket", tainted); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 379 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 380 | if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker. |
| 381 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 382 | if (mCurrentFullBucket.size() > 0) { |
| 383 | for (const auto& slice : mCurrentSlicedBucket) { |
| 384 | mCurrentFullBucket[slice.first] += slice.second.sum; |
| 385 | } |
| 386 | for (const auto& slice : mCurrentFullBucket) { |
| 387 | for (auto& tracker : mAnomalyTrackers) { |
| 388 | if (tracker != nullptr) { |
| 389 | tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | mCurrentFullBucket.clear(); |
| 394 | } else { |
| 395 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 396 | for (const auto& slice : mCurrentSlicedBucket) { |
| 397 | for (auto& tracker : mAnomalyTrackers) { |
| 398 | if (tracker != nullptr) { |
| 399 | tracker->addPastBucket(slice.first, slice.second.sum, mCurrentBucketNum); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } else { |
| 405 | // Accumulate partial bucket. |
| 406 | for (const auto& slice : mCurrentSlicedBucket) { |
| 407 | mCurrentFullBucket[slice.first] += slice.second.sum; |
| 408 | } |
| 409 | } |
| 410 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 411 | // Reset counters |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 412 | mCurrentSlicedBucket.clear(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 415 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 416 | size_t totalSize = 0; |
| 417 | for (const auto& pair : mPastBuckets) { |
| 418 | totalSize += pair.second.size() * kBucketSize; |
| 419 | } |
| 420 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 423 | } // namespace statsd |
| 424 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 425 | } // namespace android |