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_START_REPORT_NANOS = 2; |
| 52 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 53 | const int FIELD_ID_VALUE_METRICS = 7; |
| 54 | // for ValueMetricDataWrapper |
| 55 | const int FIELD_ID_DATA = 1; |
| 56 | // for ValueMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 57 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 58 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 59 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | // for ValueBucketInfo |
| 61 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 62 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 63 | const int FIELD_ID_VALUE = 3; |
| 64 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 65 | // 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] | 66 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 67 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 68 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 69 | const uint64_t startTimeNs, |
| 70 | shared_ptr<StatsPullerManager> statsPullerManager) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 71 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 72 | mValueField(metric.value_field()), |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 73 | mStatsPullerManager(statsPullerManager), |
| 74 | mPullTagId(pullTagId) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 75 | // TODO: valuemetric for pushed events may need unlimited bucket length |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 76 | int64_t bucketSizeMills = 0; |
| 77 | if (metric.has_bucket()) { |
| 78 | bucketSizeMills = TimeUnitToBucketSizeInMillis(metric.bucket()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 79 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 80 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 81 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 82 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 83 | mBucketSizeNs = bucketSizeMills * 1000000; |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 84 | mDimensions = metric.dimensions_in_what(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 85 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 86 | if (metric.links().size() > 0) { |
| 87 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 88 | metric.links().end()); |
| 89 | mConditionSliced = true; |
| 90 | } |
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 | report->set_start_report_nanos(mStartTimeNs); |
| 124 | auto value_metrics = report->mutable_value_metrics(); |
| 125 | for (const auto& pair : mPastBuckets) { |
| 126 | ValueMetricData* metricData = value_metrics->add_data(); |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 127 | *metricData->mutable_dimensions_in_what() = pair.first.getDimensionsValue(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 128 | for (const auto& bucket : pair.second) { |
| 129 | ValueBucketInfo* bucketInfo = metricData->add_bucket_info(); |
| 130 | bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs); |
| 131 | bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs); |
| 132 | bucketInfo->set_value(bucket.mValue); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 137 | void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 138 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 139 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 140 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame^] | 141 | if (mPastBuckets.empty()) { |
| 142 | return; |
| 143 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 144 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 145 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 146 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 147 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 148 | for (const auto& pair : mPastBuckets) { |
| 149 | const HashableDimensionKey& hashableKey = pair.first; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 150 | VLOG(" dimension key %s", hashableKey.c_str()); |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 151 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 152 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 153 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 154 | // First fill dimension. |
| 155 | long long dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 156 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 157 | writeDimensionsValueProtoToStream(hashableKey.getDimensionsValue(), protoOutput); |
| 158 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 159 | |
| 160 | // Then fill bucket_info (ValueBucketInfo). |
| 161 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 162 | long long bucketInfoToken = protoOutput->start( |
| 163 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 164 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 165 | (long long)bucket.mBucketStartNs); |
| 166 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 167 | (long long)bucket.mBucketEndNs); |
| 168 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue); |
| 169 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 170 | VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs, |
| 171 | (long long)bucket.mBucketEndNs, (long long)bucket.mValue); |
| 172 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 173 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 174 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 175 | protoOutput->end(protoToken); |
| 176 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 177 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 178 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 179 | mPastBuckets.clear(); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 180 | mStartTimeNs = mCurrentBucketStartTimeNs; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 181 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 184 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 185 | mCondition = condition; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 186 | |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 187 | if (eventTime < mCurrentBucketStartTimeNs) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 188 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTime, |
| 189 | (long long)mCurrentBucketStartTimeNs); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 193 | flushIfNeededLocked(eventTime); |
| 194 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 195 | if (mPullTagId != -1) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 196 | if (mCondition == true) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 197 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, mBucketSizeNs / 1000 / 1000); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 198 | } else if (mCondition == false) { |
| 199 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 200 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 201 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 202 | vector<shared_ptr<LogEvent>> allData; |
| 203 | if (mStatsPullerManager->Pull(mPullTagId, &allData)) { |
| 204 | if (allData.size() == 0) { |
| 205 | return; |
| 206 | } |
| 207 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 208 | onMatchedLogEventLocked(0, *data); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 209 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 210 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 211 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 212 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 216 | std::lock_guard<std::mutex> lock(mMutex); |
| 217 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 218 | if (mCondition == true || mConditionTrackerIndex < 0) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 219 | if (allData.size() == 0) { |
| 220 | return; |
| 221 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 222 | // For scheduled pulled data, the effective event time is snap to the nearest |
| 223 | // bucket boundary to make bucket finalize. |
| 224 | uint64_t realEventTime = allData.at(0)->GetTimestampNs(); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 225 | uint64_t eventTime = mStartTimeNs + |
| 226 | ((realEventTime - mStartTimeNs)/mBucketSizeNs) * mBucketSizeNs; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 227 | |
| 228 | mCondition = false; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 229 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 230 | data->setTimestampNs(eventTime-1); |
| 231 | onMatchedLogEventLocked(0, *data); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 232 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 233 | |
| 234 | mCondition = true; |
| 235 | for (const auto& data : allData) { |
| 236 | data->setTimestampNs(eventTime); |
| 237 | onMatchedLogEventLocked(0, *data); |
| 238 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 242 | bool ValueMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 243 | // ===========GuardRail============== |
| 244 | // 1. Report the tuple count if the tuple count > soft limit |
| 245 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 246 | return false; |
| 247 | } |
| 248 | if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 249 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 250 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 251 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 252 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 253 | ALOGE("ValueMetric %lld dropping data for dimension key %s", |
| 254 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 262 | void ValueMetricProducer::onMatchedLogEventInternalLocked( |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 263 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 264 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 265 | const LogEvent& event) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 266 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 267 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 268 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 269 | (long long)mCurrentBucketStartTimeNs); |
| 270 | return; |
| 271 | } |
| 272 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 273 | flushIfNeededLocked(eventTimeNs); |
| 274 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 275 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 276 | return; |
| 277 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 278 | Interval& interval = mCurrentSlicedBucket[eventKey]; |
| 279 | |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 280 | std::shared_ptr<FieldValueMap> valueFieldMap = getValueFields(event); |
| 281 | if (valueFieldMap->empty() || valueFieldMap->size() > 1) { |
| 282 | return; |
| 283 | } |
| 284 | const long value = getLongFromDimenValue(valueFieldMap->begin()->second); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 285 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 286 | if (mPullTagId != -1) { // for pulled events |
| 287 | if (mCondition == true) { |
| 288 | interval.start = value; |
| 289 | interval.startUpdated = true; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 290 | } else { |
Chenjie Yu | 0ed268b | 2018-01-10 09:37:10 -0800 | [diff] [blame] | 291 | // Generally we expect value to be monotonically increasing. |
| 292 | // If not, there was a reset event. We take the absolute value as |
| 293 | // diff in this case. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 294 | if (interval.startUpdated) { |
Chenjie Yu | 0ed268b | 2018-01-10 09:37:10 -0800 | [diff] [blame] | 295 | if (value > interval.start) { |
| 296 | interval.sum += (value - interval.start); |
| 297 | } else { |
| 298 | interval.sum += value; |
| 299 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 300 | interval.startUpdated = false; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 301 | } else { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 302 | VLOG("No start for matching end %ld", value); |
| 303 | interval.tainted += 1; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 306 | } else { // for pushed events |
| 307 | interval.sum += value; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 308 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 309 | |
| 310 | for (auto& tracker : mAnomalyTrackers) { |
| 311 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, interval.sum); |
| 312 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 315 | std::shared_ptr<FieldValueMap> ValueMetricProducer::getValueFields(const LogEvent& event) { |
| 316 | std::shared_ptr<FieldValueMap> valueFields = |
| 317 | std::make_shared<FieldValueMap>(event.getFieldValueMap()); |
| 318 | filterFields(mValueField, valueFields.get()); |
| 319 | return valueFields; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 322 | void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 323 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) { |
| 324 | VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs, |
| 325 | (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs)); |
| 326 | return; |
| 327 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 328 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 329 | (int)mCurrentSlicedBucket.size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 330 | ValueBucket info; |
| 331 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 332 | info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 333 | info.mBucketNum = mCurrentBucketNum; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 334 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 335 | int tainted = 0; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 336 | for (const auto& slice : mCurrentSlicedBucket) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 337 | tainted += slice.second.tainted; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 338 | info.mValue = slice.second.sum; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 339 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 340 | auto& bucketList = mPastBuckets[slice.first]; |
| 341 | bucketList.push_back(info); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 342 | |
| 343 | for (auto& tracker : mAnomalyTrackers) { |
| 344 | if (tracker != nullptr) { |
| 345 | tracker->addPastBucket(slice.first, info.mValue, info.mBucketNum); |
| 346 | } |
| 347 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 348 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 349 | VLOG("%d tainted pairs in the bucket", tainted); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 350 | |
| 351 | // Reset counters |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 352 | mCurrentSlicedBucket.clear(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 353 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 354 | int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 355 | mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs; |
| 356 | mCurrentBucketNum += numBucketsForward; |
| 357 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 358 | if (numBucketsForward > 1) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 359 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
| 360 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 361 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 362 | (long long)mCurrentBucketStartTimeNs); |
| 363 | } |
| 364 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 365 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 366 | size_t totalSize = 0; |
| 367 | for (const auto& pair : mPastBuckets) { |
| 368 | totalSize += pair.second.size() * kBucketSize; |
| 369 | } |
| 370 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 373 | } // namespace statsd |
| 374 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 375 | } // namespace android |