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 | |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 20 | #include "../guardrail/StatsdStats.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 21 | #include "GaugeMetricProducer.h" |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 22 | #include "../stats_log_util.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 23 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 24 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 25 | using android::util::FIELD_TYPE_BOOL; |
| 26 | using android::util::FIELD_TYPE_FLOAT; |
| 27 | using android::util::FIELD_TYPE_INT32; |
| 28 | using android::util::FIELD_TYPE_INT64; |
| 29 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 30 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 31 | using android::util::ProtoOutputStream; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 32 | using std::map; |
| 33 | using std::string; |
| 34 | using std::unordered_map; |
| 35 | using std::vector; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 36 | using std::make_shared; |
| 37 | using std::shared_ptr; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace os { |
| 41 | namespace statsd { |
| 42 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 43 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 44 | const int FIELD_ID_ID = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 45 | const int FIELD_ID_GAUGE_METRICS = 8; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 46 | const int FIELD_ID_TIME_BASE = 9; |
| 47 | const int FIELD_ID_BUCKET_SIZE = 10; |
| 48 | const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11; |
Howard Ro | 9440e09 | 2018-12-16 19:15:21 -0800 | [diff] [blame] | 49 | const int FIELD_ID_IS_ACTIVE = 14; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 50 | // for GaugeMetricDataWrapper |
| 51 | const int FIELD_ID_DATA = 1; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 52 | const int FIELD_ID_SKIPPED = 2; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 53 | // for SkippedBuckets |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 54 | const int FIELD_ID_SKIPPED_START_MILLIS = 3; |
| 55 | const int FIELD_ID_SKIPPED_END_MILLIS = 4; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 56 | const int FIELD_ID_SKIPPED_DROP_EVENT = 5; |
| 57 | // for DumpEvent Proto |
| 58 | const int FIELD_ID_BUCKET_DROP_REASON = 1; |
| 59 | const int FIELD_ID_DROP_TIME = 2; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | // for GaugeMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 61 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 62 | const int FIELD_ID_BUCKET_INFO = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 63 | const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 64 | // for GaugeBucketInfo |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 65 | const int FIELD_ID_ATOM = 3; |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 66 | const int FIELD_ID_ELAPSED_ATOM_TIMESTAMP = 4; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 67 | const int FIELD_ID_BUCKET_NUM = 6; |
| 68 | const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 7; |
| 69 | const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 8; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 70 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 71 | GaugeMetricProducer::GaugeMetricProducer( |
| 72 | const ConfigKey& key, const GaugeMetric& metric, const int conditionIndex, |
| 73 | const sp<ConditionWizard>& wizard, const int whatMatcherIndex, |
| 74 | const sp<EventMatcherWizard>& matcherWizard, const int pullTagId, const int triggerAtomId, |
| 75 | const int atomId, const int64_t timeBaseNs, const int64_t startTimeNs, |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 76 | const sp<StatsPullerManager>& pullerManager, |
| 77 | const unordered_map<int, shared_ptr<Activation>>& eventActivationMap, |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 78 | const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap) |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 79 | : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard, eventActivationMap, |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 80 | eventDeactivationMap, /*slicedStateAtoms=*/{}, /*stateGroupMap=*/{}), |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 81 | mWhatMatcherIndex(whatMatcherIndex), |
| 82 | mEventMatcherWizard(matcherWizard), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 83 | mPullerManager(pullerManager), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 84 | mPullTagId(pullTagId), |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 85 | mTriggerAtomId(triggerAtomId), |
| 86 | mAtomId(atomId), |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 87 | mIsPulled(pullTagId != -1), |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 88 | mMinBucketSizeNs(metric.min_bucket_size_nanos()), |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 89 | mMaxPullDelayNs(metric.max_pull_delay_sec() > 0 ? metric.max_pull_delay_sec() * NS_PER_SEC |
| 90 | : StatsdStats::kPullMaxDelayNs), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 91 | mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 92 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 93 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first |
| 94 | : StatsdStats::kDimensionKeySizeSoftLimit), |
| 95 | mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 96 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 97 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second |
Yangster-mac | 50b0c9a | 2018-05-10 17:13:12 -0700 | [diff] [blame] | 98 | : StatsdStats::kDimensionKeySizeHardLimit), |
Chenjie Yu | cd1b797 | 2019-01-16 20:38:15 -0800 | [diff] [blame] | 99 | mGaugeAtomsPerDimensionLimit(metric.max_num_gauge_atoms_per_bucket()), |
| 100 | mSplitBucketForAppUpgrade(metric.split_bucket_for_app_upgrade()) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 101 | mCurrentSlicedBucket = std::make_shared<DimToGaugeAtomsMap>(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 102 | mCurrentSlicedBucketForAnomaly = std::make_shared<DimToValMap>(); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 103 | int64_t bucketSizeMills = 0; |
| 104 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 105 | bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 106 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 107 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 108 | } |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 109 | mBucketSizeNs = bucketSizeMills * 1000000; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 110 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 111 | mSamplingType = metric.sampling_type(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 112 | if (!metric.gauge_fields_filter().include_all()) { |
| 113 | translateFieldMatcher(metric.gauge_fields_filter().fields(), &mFieldMatchers); |
| 114 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 115 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 116 | if (metric.has_dimensions_in_what()) { |
| 117 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 118 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 121 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 122 | for (const auto& link : metric.links()) { |
| 123 | Metric2Condition mc; |
| 124 | mc.conditionId = link.condition(); |
| 125 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 126 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 127 | mMetric2ConditionLinks.push_back(mc); |
| 128 | } |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 129 | mConditionSliced = true; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 130 | } |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 131 | mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 132 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 133 | flushIfNeededLocked(startTimeNs); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 134 | // Kicks off the puller immediately. |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 135 | if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 136 | mPullerManager->RegisterReceiver(mPullTagId, mConfigKey, this, getCurrentBucketEndTimeNs(), |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 137 | mBucketSizeNs); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 140 | // Adjust start for partial first bucket and then pull if needed |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 141 | mCurrentBucketStartTimeNs = startTimeNs; |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 142 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 143 | VLOG("Gauge metric %lld created. bucket size %lld start_time: %lld sliced %d", |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 144 | (long long)metric.id(), (long long)mBucketSizeNs, (long long)mTimeBaseNs, |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 145 | mConditionSliced); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | GaugeMetricProducer::~GaugeMetricProducer() { |
| 149 | VLOG("~GaugeMetricProducer() called"); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 150 | if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 151 | mPullerManager->UnRegisterReceiver(mPullTagId, mConfigKey, this); |
Chenjie Yu | 032fefc | 2017-12-01 23:30:59 -0800 | [diff] [blame] | 152 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 155 | void GaugeMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 156 | if (mCurrentSlicedBucket == nullptr || |
| 157 | mCurrentSlicedBucket->size() == 0) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | fprintf(out, "GaugeMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 162 | (unsigned long)mCurrentSlicedBucket->size()); |
| 163 | if (verbose) { |
| 164 | for (const auto& it : *mCurrentSlicedBucket) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 165 | fprintf(out, "\t(what)%s\t(states)%s %d atoms\n", |
| 166 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 167 | it.first.getStateValuesKey().toString().c_str(), (int)it.second.size()); |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 172 | void GaugeMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 173 | flushIfNeededLocked(dumpTimeNs); |
| 174 | mPastBuckets.clear(); |
| 175 | mSkippedBuckets.clear(); |
| 176 | } |
| 177 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 178 | void GaugeMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 179 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 180 | const bool erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 181 | const DumpLatency dumpLatency, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 182 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 183 | ProtoOutputStream* protoOutput) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 184 | VLOG("Gauge metric %lld report now...", (long long)mMetricId); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 185 | if (include_current_partial_bucket) { |
| 186 | flushLocked(dumpTimeNs); |
| 187 | } else { |
| 188 | flushIfNeededLocked(dumpTimeNs); |
| 189 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 190 | |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 191 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Howard Ro | 07e23ff | 2018-12-17 17:28:07 -0800 | [diff] [blame] | 192 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked()); |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 193 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 194 | if (mPastBuckets.empty() && mSkippedBuckets.empty()) { |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 195 | return; |
| 196 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 197 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 198 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs); |
| 199 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs); |
| 200 | |
| 201 | // Fills the dimension path if not slicing by ALL. |
| 202 | if (!mSliceByPositionALL) { |
| 203 | if (!mDimensionsInWhat.empty()) { |
| 204 | uint64_t dimenPathToken = protoOutput->start( |
| 205 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT); |
| 206 | writeDimensionPathToProto(mDimensionsInWhat, protoOutput); |
| 207 | protoOutput->end(dimenPathToken); |
| 208 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 211 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_GAUGE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 212 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 213 | for (const auto& skippedBucket : mSkippedBuckets) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 214 | uint64_t wrapperToken = |
| 215 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 216 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS, |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 217 | (long long)(NanoToMillis(skippedBucket.bucketStartTimeNs))); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 218 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS, |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 219 | (long long)(NanoToMillis(skippedBucket.bucketEndTimeNs))); |
| 220 | |
| 221 | for (const auto& dropEvent : skippedBucket.dropEvents) { |
| 222 | uint64_t dropEventToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 223 | FIELD_ID_SKIPPED_DROP_EVENT); |
| 224 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_BUCKET_DROP_REASON, dropEvent.reason); |
| 225 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DROP_TIME, (long long) (NanoToMillis(dropEvent.dropTimeNs))); |
| 226 | protoOutput->end(dropEventToken); |
| 227 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 228 | protoOutput->end(wrapperToken); |
| 229 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 230 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 231 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 232 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 233 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 234 | VLOG("Gauge dimension key %s", dimensionKey.toString().c_str()); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 235 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 236 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 237 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 238 | // First fill dimension. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 239 | if (mSliceByPositionALL) { |
| 240 | uint64_t dimensionToken = protoOutput->start( |
| 241 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
| 242 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput); |
| 243 | protoOutput->end(dimensionToken); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 244 | } else { |
| 245 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(), |
| 246 | FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 247 | } |
| 248 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 249 | // Then fill bucket_info (GaugeBucketInfo). |
| 250 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 251 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 252 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 253 | |
| 254 | if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) { |
| 255 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS, |
| 256 | (long long)NanoToMillis(bucket.mBucketStartNs)); |
| 257 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS, |
| 258 | (long long)NanoToMillis(bucket.mBucketEndNs)); |
| 259 | } else { |
| 260 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM, |
| 261 | (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs))); |
| 262 | } |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 263 | |
| 264 | if (!bucket.mGaugeAtoms.empty()) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 265 | for (const auto& atom : bucket.mGaugeAtoms) { |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 266 | uint64_t atomsToken = |
| 267 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 268 | FIELD_ID_ATOM); |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 269 | writeFieldValueTreeToStream(mAtomId, *(atom.mFields), protoOutput); |
Yangster-mac | 3fa5d7f | 2018-03-10 21:50:27 -0800 | [diff] [blame] | 270 | protoOutput->end(atomsToken); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 271 | } |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 272 | for (const auto& atom : bucket.mGaugeAtoms) { |
Muhammad Qureshi | a7de000 | 2020-04-15 11:34:35 -0700 | [diff] [blame] | 273 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | |
| 274 | FIELD_ID_ELAPSED_ATOM_TIMESTAMP, |
| 275 | (long long)atom.mElapsedTimestampNs); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 278 | protoOutput->end(bucketInfoToken); |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 279 | VLOG("Gauge \t bucket [%lld - %lld] includes %d atoms.", |
| 280 | (long long)bucket.mBucketStartNs, (long long)bucket.mBucketEndNs, |
| 281 | (int)bucket.mGaugeAtoms.size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 282 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 283 | protoOutput->end(wrapperToken); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 284 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 285 | protoOutput->end(protoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 286 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 287 | |
| 288 | if (erase_data) { |
| 289 | mPastBuckets.clear(); |
| 290 | mSkippedBuckets.clear(); |
| 291 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 294 | void GaugeMetricProducer::prepareFirstBucketLocked() { |
| 295 | if (mIsActive && mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
| 296 | pullAndMatchEventsLocked(mCurrentBucketStartTimeNs); |
| 297 | } |
| 298 | } |
Tej Singh | 597c716 | 2019-04-17 16:41:45 -0700 | [diff] [blame] | 299 | |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 300 | void GaugeMetricProducer::pullAndMatchEventsLocked(const int64_t timestampNs) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 301 | bool triggerPuller = false; |
| 302 | switch(mSamplingType) { |
| 303 | // When the metric wants to do random sampling and there is already one gauge atom for the |
| 304 | // current bucket, do not do it again. |
| 305 | case GaugeMetric::RANDOM_ONE_SAMPLE: { |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 306 | triggerPuller = mCondition == ConditionState::kTrue && mCurrentSlicedBucket->empty(); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 307 | break; |
| 308 | } |
Chenjie Yu | e077fd2 | 2018-11-19 13:29:40 -0800 | [diff] [blame] | 309 | case GaugeMetric::CONDITION_CHANGE_TO_TRUE: { |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 310 | triggerPuller = mCondition == ConditionState::kTrue; |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 311 | break; |
| 312 | } |
Chenjie Yu | e077fd2 | 2018-11-19 13:29:40 -0800 | [diff] [blame] | 313 | case GaugeMetric::FIRST_N_SAMPLES: { |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 314 | triggerPuller = mCondition == ConditionState::kTrue; |
Yangster | ec3c7a3 | 2018-05-09 15:51:07 -0700 | [diff] [blame] | 315 | break; |
| 316 | } |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 317 | default: |
| 318 | break; |
| 319 | } |
| 320 | if (!triggerPuller) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 321 | return; |
| 322 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 323 | vector<std::shared_ptr<LogEvent>> allData; |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 324 | if (!mPullerManager->Pull(mPullTagId, mConfigKey, &allData)) { |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 325 | ALOGE("Gauge Stats puller failed for tag: %d at %lld", mPullTagId, (long long)timestampNs); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 326 | return; |
| 327 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 328 | const int64_t pullDelayNs = getElapsedRealtimeNs() - timestampNs; |
Tej Singh | 873e91a | 2020-04-28 00:33:11 -0700 | [diff] [blame] | 329 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 330 | if (pullDelayNs > mMaxPullDelayNs) { |
| 331 | ALOGE("Pull finish too late for atom %d", mPullTagId); |
| 332 | StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId); |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 333 | return; |
| 334 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 335 | for (const auto& data : allData) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 336 | LogEvent localCopy = data->makeCopy(); |
| 337 | localCopy.setElapsedTimestampNs(timestampNs); |
| 338 | if (mEventMatcherWizard->matchLogEvent(localCopy, mWhatMatcherIndex) == |
| 339 | MatchingState::kMatched) { |
| 340 | onMatchedLogEventLocked(mWhatMatcherIndex, localCopy); |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 341 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 342 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 345 | void GaugeMetricProducer::onActiveStateChangedLocked(const int64_t& eventTimeNs) { |
| 346 | MetricProducer::onActiveStateChangedLocked(eventTimeNs); |
| 347 | if (ConditionState::kTrue != mCondition || !mIsPulled) { |
| 348 | return; |
| 349 | } |
| 350 | if (mTriggerAtomId == -1 || (mIsActive && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE)) { |
| 351 | pullAndMatchEventsLocked(eventTimeNs); |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 356 | void GaugeMetricProducer::onConditionChangedLocked(const bool conditionMet, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 357 | const int64_t eventTimeNs) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 358 | VLOG("GaugeMetric %lld onConditionChanged", (long long)mMetricId); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 359 | |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 360 | mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse; |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 361 | if (!mIsActive) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | flushIfNeededLocked(eventTimeNs); |
Chenjie Yu | e077fd2 | 2018-11-19 13:29:40 -0800 | [diff] [blame] | 366 | if (mIsPulled && mTriggerAtomId == -1) { |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 367 | pullAndMatchEventsLocked(eventTimeNs); |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 368 | } // else: Push mode. No need to proactively pull the gauge data. |
| 369 | } |
| 370 | |
| 371 | void GaugeMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 372 | const int64_t eventTimeNs) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 373 | VLOG("GaugeMetric %lld onSlicedConditionMayChange overall condition %d", (long long)mMetricId, |
| 374 | overallCondition); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 375 | mCondition = overallCondition ? ConditionState::kTrue : ConditionState::kFalse; |
| 376 | if (!mIsActive) { |
| 377 | return; |
| 378 | } |
| 379 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 380 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 381 | // If the condition is sliced, mCondition is true if any of the dimensions is true. And we will |
| 382 | // pull for every dimension. |
Chenjie Yu | e077fd2 | 2018-11-19 13:29:40 -0800 | [diff] [blame] | 383 | if (mIsPulled && mTriggerAtomId == -1) { |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 384 | pullAndMatchEventsLocked(eventTimeNs); |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 385 | } // else: Push mode. No need to proactively pull the gauge data. |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 388 | std::shared_ptr<vector<FieldValue>> GaugeMetricProducer::getGaugeFields(const LogEvent& event) { |
Chenjie Yu | 4c31f67 | 2018-08-21 15:42:40 -0700 | [diff] [blame] | 389 | std::shared_ptr<vector<FieldValue>> gaugeFields; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 390 | if (mFieldMatchers.size() > 0) { |
Chenjie Yu | 4c31f67 | 2018-08-21 15:42:40 -0700 | [diff] [blame] | 391 | gaugeFields = std::make_shared<vector<FieldValue>>(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 392 | filterGaugeValues(mFieldMatchers, event.getValues(), gaugeFields.get()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 393 | } else { |
Chenjie Yu | 4c31f67 | 2018-08-21 15:42:40 -0700 | [diff] [blame] | 394 | gaugeFields = std::make_shared<vector<FieldValue>>(event.getValues()); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 395 | } |
Chenjie Yu | 4c31f67 | 2018-08-21 15:42:40 -0700 | [diff] [blame] | 396 | // Trim all dimension fields from output. Dimensions will appear in output report and will |
| 397 | // benefit from dictionary encoding. For large pulled atoms, this can give the benefit of |
| 398 | // optional repeated field. |
| 399 | for (const auto& field : mDimensionsInWhat) { |
| 400 | for (auto it = gaugeFields->begin(); it != gaugeFields->end();) { |
| 401 | if (it->mField.matches(field)) { |
| 402 | it = gaugeFields->erase(it); |
| 403 | } else { |
| 404 | it++; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | return gaugeFields; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 411 | void GaugeMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData, |
Olivier Gaillard | 11203df | 2019-02-06 13:18:09 +0000 | [diff] [blame] | 412 | bool pullSuccess, int64_t originalPullTimeNs) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 413 | std::lock_guard<std::mutex> lock(mMutex); |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame] | 414 | if (!pullSuccess || allData.size() == 0) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 415 | return; |
| 416 | } |
Tej Singh | 873e91a | 2020-04-28 00:33:11 -0700 | [diff] [blame] | 417 | const int64_t pullDelayNs = getElapsedRealtimeNs() - originalPullTimeNs; |
| 418 | StatsdStats::getInstance().notePullDelay(mPullTagId, pullDelayNs); |
| 419 | if (pullDelayNs > mMaxPullDelayNs) { |
| 420 | ALOGE("Pull finish too late for atom %d", mPullTagId); |
| 421 | StatsdStats::getInstance().notePullExceedMaxDelay(mPullTagId); |
| 422 | return; |
| 423 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 424 | for (const auto& data : allData) { |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 425 | if (mEventMatcherWizard->matchLogEvent( |
| 426 | *data, mWhatMatcherIndex) == MatchingState::kMatched) { |
| 427 | onMatchedLogEventLocked(mWhatMatcherIndex, *data); |
| 428 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 429 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 432 | bool GaugeMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 433 | if (mCurrentSlicedBucket->find(newKey) != mCurrentSlicedBucket->end()) { |
| 434 | return false; |
| 435 | } |
| 436 | // 1. Report the tuple count if the tuple count > soft limit |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 437 | if (mCurrentSlicedBucket->size() > mDimensionSoftLimit - 1) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 438 | size_t newTupleCount = mCurrentSlicedBucket->size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 439 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 440 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 441 | if (newTupleCount > mDimensionHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 442 | ALOGE("GaugeMetric %lld dropping data for dimension key %s", |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 443 | (long long)mMetricId, newKey.toString().c_str()); |
Muhammad Qureshi | 87348a6 | 2019-02-14 16:17:52 -0800 | [diff] [blame] | 444 | StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return false; |
| 450 | } |
| 451 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 452 | void GaugeMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 453 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 454 | const ConditionKey& conditionKey, bool condition, const LogEvent& event, |
| 455 | const map<int, HashableDimensionKey>& statePrimaryKeys) { |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 456 | if (condition == false) { |
| 457 | return; |
| 458 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 459 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 460 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 461 | VLOG("Gauge Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 462 | (long long)mCurrentBucketStartTimeNs); |
| 463 | return; |
| 464 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 465 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 466 | |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 467 | if (mTriggerAtomId == event.GetTagId()) { |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 468 | pullAndMatchEventsLocked(eventTimeNs); |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 472 | // When gauge metric wants to randomly sample the output atom, we just simply use the first |
| 473 | // gauge in the given bucket. |
| 474 | if (mCurrentSlicedBucket->find(eventKey) != mCurrentSlicedBucket->end() && |
| 475 | mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 476 | return; |
| 477 | } |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 478 | if (hitGuardRailLocked(eventKey)) { |
| 479 | return; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 480 | } |
Yangster-mac | 50b0c9a | 2018-05-10 17:13:12 -0700 | [diff] [blame] | 481 | if ((*mCurrentSlicedBucket)[eventKey].size() >= mGaugeAtomsPerDimensionLimit) { |
| 482 | return; |
| 483 | } |
Muhammad Qureshi | a7de000 | 2020-04-15 11:34:35 -0700 | [diff] [blame] | 484 | |
| 485 | const int64_t truncatedElapsedTimestampNs = truncateTimestampIfNecessary(event); |
| 486 | GaugeAtom gaugeAtom(getGaugeFields(event), truncatedElapsedTimestampNs); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 487 | (*mCurrentSlicedBucket)[eventKey].push_back(gaugeAtom); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 488 | // Anomaly detection on gauge metric only works when there is one numeric |
| 489 | // field specified. |
| 490 | if (mAnomalyTrackers.size() > 0) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 491 | if (gaugeAtom.mFields->size() == 1) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 492 | const Value& value = gaugeAtom.mFields->begin()->mValue; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 493 | long gaugeVal = 0; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 494 | if (value.getType() == INT) { |
| 495 | gaugeVal = (long)value.int_value; |
| 496 | } else if (value.getType() == LONG) { |
| 497 | gaugeVal = value.long_value; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 498 | } |
| 499 | for (auto& tracker : mAnomalyTrackers) { |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 500 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, |
| 501 | eventKey, gaugeVal); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void GaugeMetricProducer::updateCurrentSlicedBucketForAnomaly() { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 508 | for (const auto& slice : *mCurrentSlicedBucket) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 509 | if (slice.second.empty()) { |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 510 | continue; |
| 511 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 512 | const Value& value = slice.second.front().mFields->front().mValue; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 513 | long gaugeVal = 0; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 514 | if (value.getType() == INT) { |
| 515 | gaugeVal = (long)value.int_value; |
| 516 | } else if (value.getType() == LONG) { |
| 517 | gaugeVal = value.long_value; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 518 | } |
| 519 | (*mCurrentSlicedBucketForAnomaly)[slice.first] = gaugeVal; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 523 | void GaugeMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 524 | flushIfNeededLocked(dropTimeNs); |
Olivier Gaillard | 320952b | 2019-02-06 13:57:24 +0000 | [diff] [blame] | 525 | StatsdStats::getInstance().noteBucketDropped(mMetricId); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 526 | mPastBuckets.clear(); |
| 527 | } |
| 528 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 529 | // When a new matched event comes in, we check if event falls into the current |
| 530 | // bucket. If not, flush the old counter to past buckets and initialize the new |
| 531 | // bucket. |
| 532 | // if data is pushed, onMatchedLogEvent will only be called through onConditionChanged() inside |
| 533 | // the GaugeMetricProducer while holding the lock. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 534 | void GaugeMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 535 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 536 | |
| 537 | if (eventTimeNs < currentBucketEndTimeNs) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 538 | VLOG("Gauge eventTime is %lld, less than next bucket start time %lld", |
| 539 | (long long)eventTimeNs, (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs)); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 543 | // Adjusts the bucket start and end times. |
| 544 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
Muhammad Qureshi | 902529a | 2019-03-14 16:03:21 -0700 | [diff] [blame] | 545 | int64_t nextBucketNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 546 | flushCurrentBucketLocked(eventTimeNs, nextBucketNs); |
| 547 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 548 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 549 | VLOG("Gauge metric %lld: new bucket start time: %lld", (long long)mMetricId, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 550 | (long long)mCurrentBucketStartTimeNs); |
| 551 | } |
| 552 | |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 553 | void GaugeMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs, |
| 554 | const int64_t& nextBucketStartTimeNs) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 555 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
tsaichristine | 45f703b | 2020-02-03 10:44:39 -0800 | [diff] [blame] | 556 | int64_t bucketEndTime = eventTimeNs < fullBucketEndTimeNs ? eventTimeNs : fullBucketEndTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 557 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 558 | GaugeBucket info; |
| 559 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
tsaichristine | 45f703b | 2020-02-03 10:44:39 -0800 | [diff] [blame] | 560 | info.mBucketEndNs = bucketEndTime; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 561 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 562 | // Add bucket to mPastBuckets if bucket is large enough. |
| 563 | // Otherwise, drop the bucket data and add bucket metadata to mSkippedBuckets. |
| 564 | bool isBucketLargeEnough = info.mBucketEndNs - mCurrentBucketStartTimeNs >= mMinBucketSizeNs; |
| 565 | if (isBucketLargeEnough) { |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 566 | for (const auto& slice : *mCurrentSlicedBucket) { |
| 567 | info.mGaugeAtoms = slice.second; |
| 568 | auto& bucketList = mPastBuckets[slice.first]; |
| 569 | bucketList.push_back(info); |
| 570 | VLOG("Gauge gauge metric %lld, dump key value: %s", (long long)mMetricId, |
| 571 | slice.first.toString().c_str()); |
| 572 | } |
| 573 | } else { |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 574 | mCurrentSkippedBucket.bucketStartTimeNs = mCurrentBucketStartTimeNs; |
tsaichristine | 45f703b | 2020-02-03 10:44:39 -0800 | [diff] [blame] | 575 | mCurrentSkippedBucket.bucketEndTimeNs = bucketEndTime; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 576 | if (!maxDropEventsReached()) { |
| 577 | mCurrentSkippedBucket.dropEvents.emplace_back( |
| 578 | buildDropEvent(eventTimeNs, BucketDropReason::BUCKET_TOO_SMALL)); |
| 579 | } |
| 580 | mSkippedBuckets.emplace_back(mCurrentSkippedBucket); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 581 | } |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 582 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 583 | // 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] | 584 | if (mAnomalyTrackers.size() > 0) { |
| 585 | updateCurrentSlicedBucketForAnomaly(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 586 | |
| 587 | if (eventTimeNs > fullBucketEndTimeNs) { |
| 588 | // This is known to be a full bucket, so send this data to the anomaly tracker. |
| 589 | for (auto& tracker : mAnomalyTrackers) { |
| 590 | tracker->addPastBucket(mCurrentSlicedBucketForAnomaly, mCurrentBucketNum); |
| 591 | } |
| 592 | mCurrentSlicedBucketForAnomaly = std::make_shared<DimToValMap>(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 593 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Olivier Gaillard | f248c0d | 2019-02-21 15:56:58 +0000 | [diff] [blame] | 596 | StatsdStats::getInstance().noteBucketCount(mMetricId); |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 597 | mCurrentSlicedBucket = std::make_shared<DimToGaugeAtomsMap>(); |
Muhammad Qureshi | 902529a | 2019-03-14 16:03:21 -0700 | [diff] [blame] | 598 | mCurrentBucketStartTimeNs = nextBucketStartTimeNs; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 599 | mCurrentSkippedBucket.reset(); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 602 | size_t GaugeMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 603 | size_t totalSize = 0; |
| 604 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | b2532da | 2018-04-11 13:55:04 -0700 | [diff] [blame] | 605 | for (const auto& bucket : pair.second) { |
| 606 | totalSize += bucket.mGaugeAtoms.size() * sizeof(GaugeAtom); |
| 607 | for (const auto& atom : bucket.mGaugeAtoms) { |
| 608 | if (atom.mFields != nullptr) { |
| 609 | totalSize += atom.mFields->size() * sizeof(FieldValue); |
| 610 | } |
| 611 | } |
| 612 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 613 | } |
| 614 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 617 | } // namespace statsd |
| 618 | } // namespace os |
| 619 | } // namespace android |