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