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