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