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 | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 47 | const int FIELD_ID_ID = 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 |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 54 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 55 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 56 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 57 | // for GaugeBucketInfo |
| 58 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 59 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 60 | const int FIELD_ID_ATOM = 3; |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 61 | const int FIELD_ID_TIMESTAMP = 4; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 62 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 63 | GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric, |
| 64 | const int conditionIndex, |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 65 | const sp<ConditionWizard>& wizard, |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 66 | const int pullTagId, const uint64_t startTimeNs, |
| 67 | shared_ptr<StatsPullerManager> statsPullerManager) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 68 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard), |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 69 | mStatsPullerManager(statsPullerManager), |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 70 | mPullTagId(pullTagId) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 71 | mCurrentSlicedBucket = std::make_shared<DimToGaugeAtomsMap>(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 72 | mCurrentSlicedBucketForAnomaly = std::make_shared<DimToValMap>(); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 73 | int64_t bucketSizeMills = 0; |
| 74 | if (metric.has_bucket()) { |
| 75 | bucketSizeMills = TimeUnitToBucketSizeInMillis(metric.bucket()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 76 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 77 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 78 | } |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 79 | mBucketSizeNs = bucketSizeMills * 1000000; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 80 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 81 | mSamplingType = metric.sampling_type(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 82 | mFieldFilter = metric.gauge_fields_filter(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 83 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 84 | // TODO: use UidMap if uid->pkg_name is required |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 85 | mDimensionsInWhat = metric.dimensions_in_what(); |
| 86 | mDimensionsInCondition = metric.dimensions_in_condition(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 87 | |
| 88 | if (metric.links().size() > 0) { |
| 89 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 90 | metric.links().end()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 91 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 92 | mConditionSliced = (metric.links().size() > 0)|| |
| 93 | (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 94 | |
| 95 | // Kicks off the puller immediately. |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 96 | if (mPullTagId != -1 && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 97 | mStatsPullerManager->RegisterReceiver(mPullTagId, this, bucketSizeMills); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 100 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 101 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 102 | } |
| 103 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 104 | // for testing |
| 105 | GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric, |
| 106 | const int conditionIndex, |
| 107 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 108 | const int64_t startTimeNs) |
| 109 | : GaugeMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs, |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 110 | make_shared<StatsPullerManager>()) { |
| 111 | } |
| 112 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 113 | GaugeMetricProducer::~GaugeMetricProducer() { |
| 114 | VLOG("~GaugeMetricProducer() called"); |
Chenjie Yu | 032fefc | 2017-12-01 23:30:59 -0800 | [diff] [blame] | 115 | if (mPullTagId != -1) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 116 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
Chenjie Yu | 032fefc | 2017-12-01 23:30:59 -0800 | [diff] [blame] | 117 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 120 | void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) { |
| 121 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 122 | ProtoOutputStream pbOutput; |
| 123 | onDumpReportLocked(dumpTimeNs, &pbOutput); |
| 124 | parseProtoOutputStream(pbOutput, report); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 127 | void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 128 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 129 | VLOG("gauge metric %lld report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 130 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 131 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 132 | if (mPastBuckets.empty()) { |
| 133 | return; |
| 134 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 135 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 136 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 137 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 138 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_GAUGE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 139 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 140 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 141 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 142 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 143 | VLOG(" dimension key %s", dimensionKey.c_str()); |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 144 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 145 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 146 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 147 | // First fill dimension. |
| 148 | long long dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 149 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 150 | writeDimensionsValueProtoToStream( |
| 151 | dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 152 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 153 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 154 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 155 | long long dimensionInConditionToken = protoOutput->start( |
| 156 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
| 157 | writeDimensionsValueProtoToStream( |
| 158 | dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput); |
| 159 | protoOutput->end(dimensionInConditionToken); |
| 160 | } |
| 161 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 162 | // Then fill bucket_info (GaugeBucketInfo). |
| 163 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 164 | long long bucketInfoToken = protoOutput->start( |
| 165 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 166 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 167 | (long long)bucket.mBucketStartNs); |
| 168 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 169 | (long long)bucket.mBucketEndNs); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 170 | |
| 171 | if (!bucket.mGaugeAtoms.empty()) { |
| 172 | long long atomsToken = |
| 173 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_ATOM); |
| 174 | for (const auto& atom : bucket.mGaugeAtoms) { |
| 175 | writeFieldValueTreeToStream(*atom.mFields, protoOutput); |
| 176 | } |
| 177 | protoOutput->end(atomsToken); |
| 178 | |
| 179 | for (const auto& atom : bucket.mGaugeAtoms) { |
| 180 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_TIMESTAMP, |
| 181 | (long long)atom.mTimestamps); |
| 182 | } |
| 183 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 184 | protoOutput->end(bucketInfoToken); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 185 | VLOG("\t bucket [%lld - %lld] includes %d atoms.", (long long)bucket.mBucketStartNs, |
| 186 | (long long)bucket.mBucketEndNs, (int)bucket.mGaugeAtoms.size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 187 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 188 | protoOutput->end(wrapperToken); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 189 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 190 | protoOutput->end(protoToken); |
| 191 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 192 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 193 | mPastBuckets.clear(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 194 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 195 | } |
| 196 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 197 | void GaugeMetricProducer::pullLocked() { |
| 198 | vector<std::shared_ptr<LogEvent>> allData; |
| 199 | if (!mStatsPullerManager->Pull(mPullTagId, &allData)) { |
| 200 | ALOGE("Stats puller failed for tag: %d", mPullTagId); |
| 201 | return; |
| 202 | } |
| 203 | for (const auto& data : allData) { |
| 204 | onMatchedLogEventLocked(0, *data); |
| 205 | } |
| 206 | } |
| 207 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 208 | void GaugeMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 209 | const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 210 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 211 | flushIfNeededLocked(eventTime); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 212 | mCondition = conditionMet; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 213 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 214 | // Push mode. No need to proactively pull the gauge data. |
| 215 | if (mPullTagId == -1) { |
| 216 | return; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 217 | } |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 218 | |
| 219 | bool triggerPuller = false; |
| 220 | switch(mSamplingType) { |
| 221 | // When the metric wants to do random sampling and there is already one gauge atom for the |
| 222 | // current bucket, do not do it again. |
| 223 | case GaugeMetric::RANDOM_ONE_SAMPLE: { |
| 224 | triggerPuller = mCondition && mCurrentSlicedBucket->empty(); |
| 225 | break; |
| 226 | } |
| 227 | case GaugeMetric::ALL_CONDITION_CHANGES: { |
| 228 | triggerPuller = true; |
| 229 | break; |
| 230 | } |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | if (!triggerPuller) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 235 | return; |
| 236 | } |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 237 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 238 | vector<std::shared_ptr<LogEvent>> allData; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 239 | if (!mStatsPullerManager->Pull(mPullTagId, &allData)) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 240 | ALOGE("Stats puller failed for tag: %d", mPullTagId); |
| 241 | return; |
| 242 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 243 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 244 | onMatchedLogEventLocked(0, *data); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 245 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 246 | flushIfNeededLocked(eventTime); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 249 | void GaugeMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 250 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 253 | std::shared_ptr<FieldValueMap> GaugeMetricProducer::getGaugeFields(const LogEvent& event) { |
| 254 | std::shared_ptr<FieldValueMap> gaugeFields = |
| 255 | std::make_shared<FieldValueMap>(event.getFieldValueMap()); |
| 256 | if (!mFieldFilter.include_all()) { |
| 257 | filterFields(mFieldFilter.fields(), gaugeFields.get()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 258 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 259 | return gaugeFields; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void GaugeMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 263 | std::lock_guard<std::mutex> lock(mMutex); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 264 | if (allData.size() == 0) { |
| 265 | return; |
| 266 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 267 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 268 | onMatchedLogEventLocked(0, *data); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 269 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 272 | bool GaugeMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 273 | if (mCurrentSlicedBucket->find(newKey) != mCurrentSlicedBucket->end()) { |
| 274 | return false; |
| 275 | } |
| 276 | // 1. Report the tuple count if the tuple count > soft limit |
| 277 | if (mCurrentSlicedBucket->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 278 | size_t newTupleCount = mCurrentSlicedBucket->size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 279 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 280 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 281 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 282 | ALOGE("GaugeMetric %lld dropping data for dimension key %s", |
| 283 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 291 | void GaugeMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 292 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 293 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 294 | const LogEvent& event) { |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 295 | if (condition == false) { |
| 296 | return; |
| 297 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 298 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 299 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 300 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 301 | (long long)mCurrentBucketStartTimeNs); |
| 302 | return; |
| 303 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 304 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 305 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 306 | // When gauge metric wants to randomly sample the output atom, we just simply use the first |
| 307 | // gauge in the given bucket. |
| 308 | if (mCurrentSlicedBucket->find(eventKey) != mCurrentSlicedBucket->end() && |
| 309 | mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 310 | return; |
| 311 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 312 | if (hitGuardRailLocked(eventKey)) { |
| 313 | return; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 314 | } |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 315 | GaugeAtom gaugeAtom; |
| 316 | gaugeAtom.mFields = getGaugeFields(event); |
| 317 | gaugeAtom.mTimestamps = eventTimeNs; |
| 318 | (*mCurrentSlicedBucket)[eventKey].push_back(gaugeAtom); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 319 | // Anomaly detection on gauge metric only works when there is one numeric |
| 320 | // field specified. |
| 321 | if (mAnomalyTrackers.size() > 0) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 322 | if (gaugeAtom.mFields->size() == 1) { |
| 323 | const DimensionsValue& dimensionsValue = gaugeAtom.mFields->begin()->second; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 324 | long gaugeVal = 0; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 325 | if (dimensionsValue.has_value_int()) { |
| 326 | gaugeVal = (long)dimensionsValue.value_int(); |
| 327 | } else if (dimensionsValue.has_value_long()) { |
| 328 | gaugeVal = dimensionsValue.value_long(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 329 | } |
| 330 | for (auto& tracker : mAnomalyTrackers) { |
| 331 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, |
| 332 | gaugeVal); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void GaugeMetricProducer::updateCurrentSlicedBucketForAnomaly() { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 339 | status_t err = NO_ERROR; |
| 340 | for (const auto& slice : *mCurrentSlicedBucket) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 341 | if (slice.second.empty() || slice.second.front().mFields->empty()) { |
| 342 | continue; |
| 343 | } |
| 344 | const DimensionsValue& dimensionsValue = slice.second.front().mFields->begin()->second; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 345 | long gaugeVal = 0; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 346 | if (dimensionsValue.has_value_int()) { |
| 347 | gaugeVal = (long)dimensionsValue.value_int(); |
| 348 | } else if (dimensionsValue.has_value_long()) { |
| 349 | gaugeVal = dimensionsValue.value_long(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 350 | } |
| 351 | (*mCurrentSlicedBucketForAnomaly)[slice.first] = gaugeVal; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | // When a new matched event comes in, we check if event falls into the current |
| 356 | // bucket. If not, flush the old counter to past buckets and initialize the new |
| 357 | // bucket. |
| 358 | // if data is pushed, onMatchedLogEvent will only be called through onConditionChanged() inside |
| 359 | // the GaugeMetricProducer while holding the lock. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 360 | void GaugeMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 361 | uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 362 | |
| 363 | if (eventTimeNs < currentBucketEndTimeNs) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 364 | VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs, |
| 365 | (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs)); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 369 | flushCurrentBucketLocked(eventTimeNs); |
| 370 | |
| 371 | // Adjusts the bucket start and end times. |
| 372 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 373 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 374 | mCurrentBucketNum += numBucketsForward; |
| 375 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 376 | (long long)mCurrentBucketStartTimeNs); |
| 377 | } |
| 378 | |
| 379 | void GaugeMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) { |
| 380 | uint64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 381 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 382 | GaugeBucket info; |
| 383 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 384 | if (eventTimeNs < fullBucketEndTimeNs) { |
| 385 | info.mBucketEndNs = eventTimeNs; |
| 386 | } else { |
| 387 | info.mBucketEndNs = fullBucketEndTimeNs; |
| 388 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 389 | info.mBucketNum = mCurrentBucketNum; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 390 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 391 | for (const auto& slice : *mCurrentSlicedBucket) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 392 | info.mGaugeAtoms = slice.second; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 393 | auto& bucketList = mPastBuckets[slice.first]; |
| 394 | bucketList.push_back(info); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 395 | VLOG("gauge metric %lld, dump key value: %s", (long long)mMetricId, slice.first.c_str()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 396 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 397 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 398 | // If we have anomaly trackers, we need to update the partial bucket values. |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 399 | if (mAnomalyTrackers.size() > 0) { |
| 400 | updateCurrentSlicedBucketForAnomaly(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 401 | |
| 402 | if (eventTimeNs > fullBucketEndTimeNs) { |
| 403 | // This is known to be a full bucket, so send this data to the anomaly tracker. |
| 404 | for (auto& tracker : mAnomalyTrackers) { |
| 405 | tracker->addPastBucket(mCurrentSlicedBucketForAnomaly, mCurrentBucketNum); |
| 406 | } |
| 407 | mCurrentSlicedBucketForAnomaly = std::make_shared<DimToValMap>(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 408 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 411 | mCurrentSlicedBucket = std::make_shared<DimToGaugeAtomsMap>(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 414 | size_t GaugeMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 415 | size_t totalSize = 0; |
| 416 | for (const auto& pair : mPastBuckets) { |
| 417 | totalSize += pair.second.size() * kBucketSize; |
| 418 | } |
| 419 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 422 | } // namespace statsd |
| 423 | } // namespace os |
| 424 | } // namespace android |