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