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