Yangster | 1d4d686 | 2017-10-31 12:58:51 -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 |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
| 20 | #include "GaugeMetricProducer.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | #include "guardrail/StatsdStats.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 22 | #include "dimension.h" |
| 23 | #include "stats_log_util.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 24 | |
| 25 | #include <cutils/log.h> |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 26 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 27 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 28 | using android::util::FIELD_TYPE_BOOL; |
| 29 | using android::util::FIELD_TYPE_FLOAT; |
| 30 | using android::util::FIELD_TYPE_INT32; |
| 31 | using android::util::FIELD_TYPE_INT64; |
| 32 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 33 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 34 | using android::util::ProtoOutputStream; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 35 | using std::map; |
| 36 | using std::string; |
| 37 | using std::unordered_map; |
| 38 | using std::vector; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 39 | using std::make_shared; |
| 40 | using std::shared_ptr; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace os { |
| 44 | namespace statsd { |
| 45 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 46 | // for StatsLogReport |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 47 | const int FIELD_ID_NAME = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 48 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 49 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 50 | const int FIELD_ID_GAUGE_METRICS = 8; |
| 51 | // for GaugeMetricDataWrapper |
| 52 | const int FIELD_ID_DATA = 1; |
| 53 | // for GaugeMetricData |
| 54 | const int FIELD_ID_DIMENSION = 1; |
| 55 | const int FIELD_ID_BUCKET_INFO = 2; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 56 | // for GaugeBucketInfo |
| 57 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 58 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 59 | const int FIELD_ID_ATOM = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 61 | GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric, |
| 62 | const int conditionIndex, |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 63 | const sp<ConditionWizard>& wizard, const int atomTagId, |
| 64 | const int pullTagId, const uint64_t startTimeNs, |
| 65 | shared_ptr<StatsPullerManager> statsPullerManager) |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 66 | : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard), |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 67 | mStatsPullerManager(statsPullerManager), |
| 68 | mPullTagId(pullTagId), |
| 69 | mAtomTagId(atomTagId) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 70 | mCurrentSlicedBucket = std::make_shared<DimToGaugeFieldsMap>(); |
| 71 | mCurrentSlicedBucketForAnomaly = std::make_shared<DimToValMap>(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 72 | if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) { |
| 73 | mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000; |
| 74 | } else { |
| 75 | mBucketSizeNs = kDefaultGaugemBucketSizeNs; |
| 76 | } |
| 77 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 78 | mFieldFilter = metric.gauge_fields_filter(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 79 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 80 | // TODO: use UidMap if uid->pkg_name is required |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 81 | mDimensions = metric.dimensions(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 82 | |
| 83 | if (metric.links().size() > 0) { |
| 84 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 85 | metric.links().end()); |
| 86 | mConditionSliced = true; |
| 87 | } |
| 88 | |
| 89 | // Kicks off the puller immediately. |
| 90 | if (mPullTagId != -1) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 91 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 92 | metric.bucket().bucket_size_millis()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 95 | VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(), |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 96 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 97 | } |
| 98 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 99 | // for testing |
| 100 | GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric, |
| 101 | const int conditionIndex, |
| 102 | const sp<ConditionWizard>& wizard, const int pullTagId, |
| 103 | const int atomTagId, const int64_t startTimeNs) |
| 104 | : GaugeMetricProducer(key, metric, conditionIndex, wizard, pullTagId, atomTagId, startTimeNs, |
| 105 | make_shared<StatsPullerManager>()) { |
| 106 | } |
| 107 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 108 | GaugeMetricProducer::~GaugeMetricProducer() { |
| 109 | VLOG("~GaugeMetricProducer() called"); |
Chenjie Yu | 032fefc | 2017-12-01 23:30:59 -0800 | [diff] [blame] | 110 | if (mPullTagId != -1) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 111 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | 032fefc | 2017-12-01 23:30:59 -0800 | [diff] [blame] | 112 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 115 | void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) { |
| 116 | flushIfNeededLocked(dumpTimeNs); |
| 117 | } |
| 118 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 119 | void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 120 | ProtoOutputStream* protoOutput) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 121 | VLOG("gauge metric %s dump report now...", mName.c_str()); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 122 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 123 | flushIfNeededLocked(dumpTimeNs); |
| 124 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 125 | protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 126 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 127 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_GAUGE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 128 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 129 | for (const auto& pair : mPastBuckets) { |
| 130 | const HashableDimensionKey& hashableKey = pair.first; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 131 | |
| 132 | VLOG(" dimension key %s", hashableKey.c_str()); |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 133 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 134 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 135 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 136 | // First fill dimension. |
| 137 | long long dimensionToken = protoOutput->start( |
| 138 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION); |
| 139 | writeDimensionsValueProtoToStream(hashableKey.getDimensionsValue(), protoOutput); |
| 140 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 141 | |
| 142 | // Then fill bucket_info (GaugeBucketInfo). |
| 143 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 144 | long long bucketInfoToken = protoOutput->start( |
| 145 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 146 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 147 | (long long)bucket.mBucketStartNs); |
| 148 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 149 | (long long)bucket.mBucketEndNs); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 150 | long long atomToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 151 | writeFieldValueTreeToStream(*bucket.mGaugeFields, protoOutput); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 152 | protoOutput->end(atomToken); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 153 | protoOutput->end(bucketInfoToken); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 154 | VLOG("\t bucket [%lld - %lld] includes %d gauge fields.", (long long)bucket.mBucketStartNs, |
| 155 | (long long)bucket.mBucketEndNs, (int)bucket.mGaugeFields->size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 156 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 157 | protoOutput->end(wrapperToken); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 158 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 159 | protoOutput->end(protoToken); |
| 160 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 161 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 162 | mPastBuckets.clear(); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 163 | mStartTimeNs = mCurrentBucketStartTimeNs; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 164 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 167 | void GaugeMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 168 | const uint64_t eventTime) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 169 | VLOG("Metric %s onConditionChanged", mName.c_str()); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 170 | flushIfNeededLocked(eventTime); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 171 | mCondition = conditionMet; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 172 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 173 | // Push mode. No need to proactively pull the gauge data. |
| 174 | if (mPullTagId == -1) { |
| 175 | return; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 176 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 177 | // No need to pull again. Either scheduled pull or condition on true happened |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 178 | if (!mCondition) { |
| 179 | return; |
| 180 | } |
| 181 | // Already have gauge metric for the current bucket, do not do it again. |
| 182 | if (mCurrentSlicedBucket->size() > 0) { |
| 183 | return; |
| 184 | } |
| 185 | vector<std::shared_ptr<LogEvent>> allData; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 186 | if (!mStatsPullerManager->Pull(mPullTagId, &allData)) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 187 | ALOGE("Stats puller failed for tag: %d", mPullTagId); |
| 188 | return; |
| 189 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 190 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 191 | onMatchedLogEventLocked(0, *data); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 192 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 193 | flushIfNeededLocked(eventTime); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 196 | void GaugeMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 197 | VLOG("Metric %s onSlicedConditionMayChange", mName.c_str()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 200 | std::shared_ptr<FieldValueMap> GaugeMetricProducer::getGaugeFields(const LogEvent& event) { |
| 201 | std::shared_ptr<FieldValueMap> gaugeFields = |
| 202 | std::make_shared<FieldValueMap>(event.getFieldValueMap()); |
| 203 | if (!mFieldFilter.include_all()) { |
| 204 | filterFields(mFieldFilter.fields(), gaugeFields.get()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 205 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 206 | return gaugeFields; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void GaugeMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 210 | std::lock_guard<std::mutex> lock(mMutex); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 211 | if (allData.size() == 0) { |
| 212 | return; |
| 213 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 214 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 215 | onMatchedLogEventLocked(0, *data); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 216 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 219 | bool GaugeMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 220 | if (mCurrentSlicedBucket->find(newKey) != mCurrentSlicedBucket->end()) { |
| 221 | return false; |
| 222 | } |
| 223 | // 1. Report the tuple count if the tuple count > soft limit |
| 224 | if (mCurrentSlicedBucket->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 225 | size_t newTupleCount = mCurrentSlicedBucket->size() + 1; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 226 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 227 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 228 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 229 | ALOGE("GaugeMetric %s dropping data for dimension key %s", mName.c_str(), |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 230 | newKey.c_str()); |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 238 | void GaugeMetricProducer::onMatchedLogEventInternalLocked( |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 239 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 240 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 241 | const LogEvent& event) { |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 242 | if (condition == false) { |
| 243 | return; |
| 244 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 245 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 246 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 247 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 248 | (long long)mCurrentBucketStartTimeNs); |
| 249 | return; |
| 250 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 251 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 252 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 253 | // For gauge metric, we just simply use the first gauge in the given bucket. |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 254 | if (mCurrentSlicedBucket->find(eventKey) != mCurrentSlicedBucket->end()) { |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 255 | return; |
| 256 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 257 | std::shared_ptr<FieldValueMap> gaugeFields = getGaugeFields(event); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 258 | if (hitGuardRailLocked(eventKey)) { |
| 259 | return; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 260 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 261 | (*mCurrentSlicedBucket)[eventKey] = gaugeFields; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 262 | // Anomaly detection on gauge metric only works when there is one numeric |
| 263 | // field specified. |
| 264 | if (mAnomalyTrackers.size() > 0) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 265 | if (gaugeFields->size() == 1) { |
| 266 | const DimensionsValue& dimensionsValue = gaugeFields->begin()->second; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 267 | long gaugeVal = 0; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 268 | if (dimensionsValue.has_value_int()) { |
| 269 | gaugeVal = (long)dimensionsValue.value_int(); |
| 270 | } else if (dimensionsValue.has_value_long()) { |
| 271 | gaugeVal = dimensionsValue.value_long(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 272 | } |
| 273 | for (auto& tracker : mAnomalyTrackers) { |
| 274 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, |
| 275 | gaugeVal); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void GaugeMetricProducer::updateCurrentSlicedBucketForAnomaly() { |
| 282 | mCurrentSlicedBucketForAnomaly->clear(); |
| 283 | status_t err = NO_ERROR; |
| 284 | for (const auto& slice : *mCurrentSlicedBucket) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 285 | const DimensionsValue& dimensionsValue = slice.second->begin()->second; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 286 | long gaugeVal = 0; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 287 | if (dimensionsValue.has_value_int()) { |
| 288 | gaugeVal = (long)dimensionsValue.value_int(); |
| 289 | } else if (dimensionsValue.has_value_long()) { |
| 290 | gaugeVal = dimensionsValue.value_long(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 291 | } |
| 292 | (*mCurrentSlicedBucketForAnomaly)[slice.first] = gaugeVal; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | // When a new matched event comes in, we check if event falls into the current |
| 297 | // bucket. If not, flush the old counter to past buckets and initialize the new |
| 298 | // bucket. |
| 299 | // if data is pushed, onMatchedLogEvent will only be called through onConditionChanged() inside |
| 300 | // the GaugeMetricProducer while holding the lock. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 301 | void GaugeMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 302 | if (eventTimeNs < mCurrentBucketStartTimeNs + mBucketSizeNs) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 303 | VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs, |
| 304 | (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs)); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 308 | GaugeBucket info; |
| 309 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
| 310 | info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 311 | info.mBucketNum = mCurrentBucketNum; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 312 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 313 | for (const auto& slice : *mCurrentSlicedBucket) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 314 | info.mGaugeFields = slice.second; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 315 | auto& bucketList = mPastBuckets[slice.first]; |
| 316 | bucketList.push_back(info); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 317 | VLOG("gauge metric %s, dump key value: %s", mName.c_str(), slice.first.c_str()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 318 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 319 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 320 | // Reset counters |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 321 | if (mAnomalyTrackers.size() > 0) { |
| 322 | updateCurrentSlicedBucketForAnomaly(); |
| 323 | for (auto& tracker : mAnomalyTrackers) { |
| 324 | tracker->addPastBucket(mCurrentSlicedBucketForAnomaly, mCurrentBucketNum); |
| 325 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 328 | mCurrentSlicedBucket = std::make_shared<DimToGaugeFieldsMap>(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 329 | |
| 330 | // Adjusts the bucket start time |
| 331 | int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 332 | mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 333 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 334 | VLOG("metric %s: new bucket start time: %lld", mName.c_str(), |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 335 | (long long)mCurrentBucketStartTimeNs); |
| 336 | } |
| 337 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 338 | size_t GaugeMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 339 | size_t totalSize = 0; |
| 340 | for (const auto& pair : mPastBuckets) { |
| 341 | totalSize += pair.second.size() * kBucketSize; |
| 342 | } |
| 343 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 346 | } // namespace statsd |
| 347 | } // namespace os |
| 348 | } // namespace android |