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