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