blob: 7ec57dcaee8823b2ebb5421458ea6785a97a83a5 [file] [log] [blame]
Yangster1d4d6862017-10-31 12:58:51 -07001/*
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
17#define DEBUG true // STOPSHIP if true
18#include "Log.h"
19
20#include "GaugeMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Yangster1d4d6862017-10-31 12:58:51 -070022
23#include <cutils/log.h>
Yangster1d4d6862017-10-31 12:58:51 -070024
yrob0378b02017-11-09 20:36:25 -080025using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080026using android::util::FIELD_TYPE_BOOL;
27using android::util::FIELD_TYPE_FLOAT;
28using android::util::FIELD_TYPE_INT32;
29using android::util::FIELD_TYPE_INT64;
30using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080031using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080032using android::util::ProtoOutputStream;
Yangster1d4d6862017-10-31 12:58:51 -070033using std::map;
34using std::string;
35using std::unordered_map;
36using std::vector;
Chenjie Yud9dfda72017-12-11 17:41:20 -080037using std::make_shared;
38using std::shared_ptr;
Yangster1d4d6862017-10-31 12:58:51 -070039
40namespace android {
41namespace os {
42namespace statsd {
43
yro2b0f8862017-11-06 14:27:31 -080044// for StatsLogReport
Yangster-macd1815dc2017-11-13 21:43:15 -080045const int FIELD_ID_NAME = 1;
yro2b0f8862017-11-06 14:27:31 -080046const int FIELD_ID_START_REPORT_NANOS = 2;
47const int FIELD_ID_END_REPORT_NANOS = 3;
48const int FIELD_ID_GAUGE_METRICS = 8;
49// for GaugeMetricDataWrapper
50const int FIELD_ID_DATA = 1;
51// for GaugeMetricData
52const int FIELD_ID_DIMENSION = 1;
53const int FIELD_ID_BUCKET_INFO = 2;
54// for KeyValuePair
55const int FIELD_ID_KEY = 1;
56const int FIELD_ID_VALUE_STR = 2;
57const int FIELD_ID_VALUE_INT = 3;
58const int FIELD_ID_VALUE_BOOL = 4;
59const int FIELD_ID_VALUE_FLOAT = 5;
60// for GaugeBucketInfo
61const int FIELD_ID_START_BUCKET_NANOS = 1;
62const int FIELD_ID_END_BUCKET_NANOS = 2;
Chenjie Yud9dfda72017-12-11 17:41:20 -080063const int FIELD_ID_ATOM = 3;
yro2b0f8862017-11-06 14:27:31 -080064
Yao Chenb3561512017-11-21 18:07:17 -080065GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric,
66 const int conditionIndex,
Chenjie Yud9dfda72017-12-11 17:41:20 -080067 const sp<ConditionWizard>& wizard, const int atomTagId,
68 const int pullTagId, const uint64_t startTimeNs,
69 shared_ptr<StatsPullerManager> statsPullerManager)
Yao Chenf09569f2017-12-13 17:00:51 -080070 : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard),
Chenjie Yud9dfda72017-12-11 17:41:20 -080071 mStatsPullerManager(statsPullerManager),
72 mPullTagId(pullTagId),
73 mAtomTagId(atomTagId) {
Yangster1d4d6862017-10-31 12:58:51 -070074 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
75 mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
76 } else {
77 mBucketSizeNs = kDefaultGaugemBucketSizeNs;
78 }
79
Chenjie Yud9dfda72017-12-11 17:41:20 -080080 for (int i = 0; i < metric.gauge_fields().field_num_size(); i++) {
81 mGaugeFields.push_back(metric.gauge_fields().field_num(i));
82 }
83
Yangster1d4d6862017-10-31 12:58:51 -070084 // TODO: use UidMap if uid->pkg_name is required
85 mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());
86
87 if (metric.links().size() > 0) {
88 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
89 metric.links().end());
90 mConditionSliced = true;
91 }
92
93 // Kicks off the puller immediately.
94 if (mPullTagId != -1) {
Chenjie Yud9dfda72017-12-11 17:41:20 -080095 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
Yangster1d4d6862017-10-31 12:58:51 -070096 metric.bucket().bucket_size_millis());
97 }
98
Yangster-macd1815dc2017-11-13 21:43:15 -080099 VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yangster1d4d6862017-10-31 12:58:51 -0700100 (long long)mBucketSizeNs, (long long)mStartTimeNs);
101}
102
Chenjie Yud9dfda72017-12-11 17:41:20 -0800103// for testing
104GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric,
105 const int conditionIndex,
106 const sp<ConditionWizard>& wizard, const int pullTagId,
107 const int atomTagId, const int64_t startTimeNs)
108 : GaugeMetricProducer(key, metric, conditionIndex, wizard, pullTagId, atomTagId, startTimeNs,
109 make_shared<StatsPullerManager>()) {
110}
111
Yangster1d4d6862017-10-31 12:58:51 -0700112GaugeMetricProducer::~GaugeMetricProducer() {
113 VLOG("~GaugeMetricProducer() called");
Chenjie Yu032fefc2017-12-01 23:30:59 -0800114 if (mPullTagId != -1) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800115 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yu032fefc2017-12-01 23:30:59 -0800116 }
Yangster1d4d6862017-10-31 12:58:51 -0700117}
118
Yao Chen288c6002017-12-12 13:43:18 -0800119void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
120 ProtoOutputStream* protoOutput) {
Yao Chenf09569f2017-12-13 17:00:51 -0800121 VLOG("gauge metric %s dump report now...", mName.c_str());
Yao Chen6a8c7992017-11-29 20:02:07 +0000122
Yao Chen288c6002017-12-12 13:43:18 -0800123 flushIfNeededLocked(dumpTimeNs);
124
Yao Chenf09569f2017-12-13 17:00:51 -0800125 protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
Yao Chen288c6002017-12-12 13:43:18 -0800126 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
127 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_GAUGE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000128
Yangster1d4d6862017-10-31 12:58:51 -0700129 for (const auto& pair : mPastBuckets) {
130 const HashableDimensionKey& hashableKey = pair.first;
131 auto it = mDimensionKeyMap.find(hashableKey);
132 if (it == mDimensionKeyMap.end()) {
133 ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str());
134 continue;
135 }
136
137 VLOG(" dimension key %s", hashableKey.c_str());
yrob0378b02017-11-09 20:36:25 -0800138 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800139 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800140
141 // First fill dimension (KeyValuePairs).
142 for (const auto& kv : it->second) {
Yao Chen288c6002017-12-12 13:43:18 -0800143 long long dimensionToken = protoOutput->start(
144 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
145 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
yro2b0f8862017-11-06 14:27:31 -0800146 if (kv.has_value_str()) {
Yao Chen288c6002017-12-12 13:43:18 -0800147 protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro2b0f8862017-11-06 14:27:31 -0800148 } else if (kv.has_value_int()) {
Yao Chen288c6002017-12-12 13:43:18 -0800149 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
yro2b0f8862017-11-06 14:27:31 -0800150 } else if (kv.has_value_bool()) {
Yao Chen288c6002017-12-12 13:43:18 -0800151 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
yro2b0f8862017-11-06 14:27:31 -0800152 } else if (kv.has_value_float()) {
Yao Chen288c6002017-12-12 13:43:18 -0800153 protoOutput->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
yro2b0f8862017-11-06 14:27:31 -0800154 }
Yao Chen288c6002017-12-12 13:43:18 -0800155 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800156 }
157
158 // Then fill bucket_info (GaugeBucketInfo).
159 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800160 long long bucketInfoToken = protoOutput->start(
161 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
162 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
163 (long long)bucket.mBucketStartNs);
164 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
165 (long long)bucket.mBucketEndNs);
Chenjie Yud9dfda72017-12-11 17:41:20 -0800166 long long atomToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM);
167 long long eventToken = protoOutput->start(FIELD_TYPE_MESSAGE | mAtomTagId);
168 for (const auto& pair : bucket.mEvent->kv) {
169 if (pair.has_value_int()) {
170 protoOutput->write(FIELD_TYPE_INT32 | pair.key(), pair.value_int());
171 } else if (pair.has_value_long()) {
172 protoOutput->write(FIELD_TYPE_INT64 | pair.key(), pair.value_long());
173 } else if (pair.has_value_str()) {
174 protoOutput->write(FIELD_TYPE_STRING | pair.key(), pair.value_str());
175 } else if (pair.has_value_long()) {
176 protoOutput->write(FIELD_TYPE_FLOAT | pair.key(), pair.value_float());
177 } else if (pair.has_value_bool()) {
178 protoOutput->write(FIELD_TYPE_BOOL | pair.key(), pair.value_bool());
179 }
180 }
181 protoOutput->end(eventToken);
182 protoOutput->end(atomToken);
Yao Chen288c6002017-12-12 13:43:18 -0800183 protoOutput->end(bucketInfoToken);
Chenjie Yud9dfda72017-12-11 17:41:20 -0800184 VLOG("\t bucket [%lld - %lld] content: %s", (long long)bucket.mBucketStartNs,
185 (long long)bucket.mBucketEndNs, bucket.mEvent->ToString().c_str());
yro2b0f8862017-11-06 14:27:31 -0800186 }
Yao Chen288c6002017-12-12 13:43:18 -0800187 protoOutput->end(wrapperToken);
Yangster1d4d6862017-10-31 12:58:51 -0700188 }
Yao Chen288c6002017-12-12 13:43:18 -0800189 protoOutput->end(protoToken);
190 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
yro2b0f8862017-11-06 14:27:31 -0800191
Yao Chen6a8c7992017-11-29 20:02:07 +0000192 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800193 mStartTimeNs = mCurrentBucketStartTimeNs;
yro2b0f8862017-11-06 14:27:31 -0800194 // TODO: Clear mDimensionKeyMap once the report is dumped.
Yangster1d4d6862017-10-31 12:58:51 -0700195}
196
Yangsterf2bee6f2017-11-29 12:01:05 -0800197void GaugeMetricProducer::onConditionChangedLocked(const bool conditionMet,
198 const uint64_t eventTime) {
Yao Chenf09569f2017-12-13 17:00:51 -0800199 VLOG("Metric %s onConditionChanged", mName.c_str());
Yangsterf2bee6f2017-11-29 12:01:05 -0800200 flushIfNeededLocked(eventTime);
Yao Chen6a8c7992017-11-29 20:02:07 +0000201 mCondition = conditionMet;
Yangster8de69392017-11-27 13:48:29 -0800202
Yao Chen6a8c7992017-11-29 20:02:07 +0000203 // Push mode. No need to proactively pull the gauge data.
204 if (mPullTagId == -1) {
205 return;
Yangster1d4d6862017-10-31 12:58:51 -0700206 }
Chenjie Yud9dfda72017-12-11 17:41:20 -0800207 // No need to pull again. Either scheduled pull or condition on true happened
Yao Chen6a8c7992017-11-29 20:02:07 +0000208 if (!mCondition) {
209 return;
210 }
211 // Already have gauge metric for the current bucket, do not do it again.
212 if (mCurrentSlicedBucket->size() > 0) {
213 return;
214 }
215 vector<std::shared_ptr<LogEvent>> allData;
Chenjie Yud9dfda72017-12-11 17:41:20 -0800216 if (!mStatsPullerManager->Pull(mPullTagId, &allData)) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000217 ALOGE("Stats puller failed for tag: %d", mPullTagId);
218 return;
219 }
Yangster1d4d6862017-10-31 12:58:51 -0700220 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800221 onMatchedLogEventLocked(0, *data);
Yangster1d4d6862017-10-31 12:58:51 -0700222 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800223 flushIfNeededLocked(eventTime);
Yangster1d4d6862017-10-31 12:58:51 -0700224}
225
Yangsterf2bee6f2017-11-29 12:01:05 -0800226void GaugeMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yao Chenf09569f2017-12-13 17:00:51 -0800227 VLOG("Metric %s onSlicedConditionMayChange", mName.c_str());
Yangster1d4d6862017-10-31 12:58:51 -0700228}
229
Chenjie Yud9dfda72017-12-11 17:41:20 -0800230shared_ptr<EventKV> GaugeMetricProducer::getGauge(const LogEvent& event) {
231 shared_ptr<EventKV> ret = make_shared<EventKV>();
232 if (mGaugeFields.size() == 0) {
233 for (int i = 1; i <= event.size(); i++) {
234 ret->kv.push_back(event.GetKeyValueProto(i));
235 }
Yangster1d4d6862017-10-31 12:58:51 -0700236 } else {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800237 for (int i = 0; i < (int)mGaugeFields.size(); i++) {
238 ret->kv.push_back(event.GetKeyValueProto(mGaugeFields[i]));
239 }
Yangster1d4d6862017-10-31 12:58:51 -0700240 }
Chenjie Yud9dfda72017-12-11 17:41:20 -0800241 return ret;
Yangster1d4d6862017-10-31 12:58:51 -0700242}
243
244void GaugeMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800245 std::lock_guard<std::mutex> lock(mMutex);
Chenjie Yud9dfda72017-12-11 17:41:20 -0800246 if (allData.size() == 0) {
247 return;
248 }
Yangster1d4d6862017-10-31 12:58:51 -0700249 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800250 onMatchedLogEventLocked(0, *data);
Yangster1d4d6862017-10-31 12:58:51 -0700251 }
Yangster1d4d6862017-10-31 12:58:51 -0700252}
253
Yangsterf2bee6f2017-11-29 12:01:05 -0800254bool GaugeMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800255 if (mCurrentSlicedBucket->find(newKey) != mCurrentSlicedBucket->end()) {
256 return false;
257 }
258 // 1. Report the tuple count if the tuple count > soft limit
259 if (mCurrentSlicedBucket->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
260 size_t newTupleCount = mCurrentSlicedBucket->size() + 1;
Yao Chenf09569f2017-12-13 17:00:51 -0800261 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800262 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
263 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yao Chenf09569f2017-12-13 17:00:51 -0800264 ALOGE("GaugeMetric %s dropping data for dimension key %s", mName.c_str(),
Yao Chenb3561512017-11-21 18:07:17 -0800265 newKey.c_str());
266 return true;
267 }
268 }
269
270 return false;
271}
272
Yangsterf2bee6f2017-11-29 12:01:05 -0800273void GaugeMetricProducer::onMatchedLogEventInternalLocked(
Yangster1d4d6862017-10-31 12:58:51 -0700274 const size_t matcherIndex, const HashableDimensionKey& eventKey,
275 const map<string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800276 const LogEvent& event) {
Yangster1d4d6862017-10-31 12:58:51 -0700277 if (condition == false) {
278 return;
279 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000280 uint64_t eventTimeNs = event.GetTimestampNs();
Yangster1d4d6862017-10-31 12:58:51 -0700281 if (eventTimeNs < mCurrentBucketStartTimeNs) {
282 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
283 (long long)mCurrentBucketStartTimeNs);
284 return;
285 }
Chenjie Yud9dfda72017-12-11 17:41:20 -0800286 flushIfNeededLocked(eventTimeNs);
Yao Chen6a8c7992017-11-29 20:02:07 +0000287
Yao Chenb3561512017-11-21 18:07:17 -0800288 // For gauge metric, we just simply use the first gauge in the given bucket.
Chenjie Yud9dfda72017-12-11 17:41:20 -0800289 if (mCurrentSlicedBucket->find(eventKey) != mCurrentSlicedBucket->end()) {
Yangster1d4d6862017-10-31 12:58:51 -0700290 return;
291 }
Chenjie Yud9dfda72017-12-11 17:41:20 -0800292 shared_ptr<EventKV> gauge = getGauge(event);
293 if (hitGuardRailLocked(eventKey)) {
294 return;
Yao Chen6a8c7992017-11-29 20:02:07 +0000295 }
Chenjie Yud9dfda72017-12-11 17:41:20 -0800296 (*mCurrentSlicedBucket)[eventKey] = gauge;
297 // Anomaly detection on gauge metric only works when there is one numeric
298 // field specified.
299 if (mAnomalyTrackers.size() > 0) {
300 if (gauge->kv.size() == 1) {
301 KeyValuePair pair = gauge->kv[0];
302 long gaugeVal = 0;
303 if (pair.has_value_int()) {
304 gaugeVal = (long)pair.value_int();
305 } else if (pair.has_value_long()) {
306 gaugeVal = pair.value_long();
307 }
308 for (auto& tracker : mAnomalyTrackers) {
309 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey,
310 gaugeVal);
311 }
312 }
313 }
314}
315
316void GaugeMetricProducer::updateCurrentSlicedBucketForAnomaly() {
317 mCurrentSlicedBucketForAnomaly->clear();
318 status_t err = NO_ERROR;
319 for (const auto& slice : *mCurrentSlicedBucket) {
320 KeyValuePair pair = slice.second->kv[0];
321 long gaugeVal = 0;
322 if (pair.has_value_int()) {
323 gaugeVal = (long)pair.value_int();
324 } else if (pair.has_value_long()) {
325 gaugeVal = pair.value_long();
326 }
327 (*mCurrentSlicedBucketForAnomaly)[slice.first] = gaugeVal;
Yangster1d4d6862017-10-31 12:58:51 -0700328 }
329}
330
331// When a new matched event comes in, we check if event falls into the current
332// bucket. If not, flush the old counter to past buckets and initialize the new
333// bucket.
334// if data is pushed, onMatchedLogEvent will only be called through onConditionChanged() inside
335// the GaugeMetricProducer while holding the lock.
Yangsterf2bee6f2017-11-29 12:01:05 -0800336void GaugeMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800337 if (eventTimeNs < mCurrentBucketStartTimeNs + mBucketSizeNs) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800338 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
339 (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs));
Yangster1d4d6862017-10-31 12:58:51 -0700340 return;
341 }
342
yro2b0f8862017-11-06 14:27:31 -0800343 GaugeBucket info;
344 info.mBucketStartNs = mCurrentBucketStartTimeNs;
345 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800346 info.mBucketNum = mCurrentBucketNum;
Yangster1d4d6862017-10-31 12:58:51 -0700347
Yangster-mace2cd6d52017-11-09 20:38:30 -0800348 for (const auto& slice : *mCurrentSlicedBucket) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800349 info.mEvent = slice.second;
Yangster1d4d6862017-10-31 12:58:51 -0700350 auto& bucketList = mPastBuckets[slice.first];
351 bucketList.push_back(info);
Chenjie Yud9dfda72017-12-11 17:41:20 -0800352 VLOG("gauge metric %s, dump key value: %s -> %s", mName.c_str(),
353 slice.first.c_str(), slice.second->ToString().c_str());
Yangster1d4d6862017-10-31 12:58:51 -0700354 }
Yangster1d4d6862017-10-31 12:58:51 -0700355
Yangster-mace2cd6d52017-11-09 20:38:30 -0800356 // Reset counters
Chenjie Yud9dfda72017-12-11 17:41:20 -0800357 if (mAnomalyTrackers.size() > 0) {
358 updateCurrentSlicedBucketForAnomaly();
359 for (auto& tracker : mAnomalyTrackers) {
360 tracker->addPastBucket(mCurrentSlicedBucketForAnomaly, mCurrentBucketNum);
361 }
Yangster-mace2cd6d52017-11-09 20:38:30 -0800362 }
363
Chenjie Yud9dfda72017-12-11 17:41:20 -0800364 mCurrentSlicedBucket = std::make_shared<DimToEventKVMap>();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800365
366 // Adjusts the bucket start time
367 int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yangster1d4d6862017-10-31 12:58:51 -0700368 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800369 mCurrentBucketNum += numBucketsForward;
Yao Chenf09569f2017-12-13 17:00:51 -0800370 VLOG("metric %s: new bucket start time: %lld", mName.c_str(),
Yangster1d4d6862017-10-31 12:58:51 -0700371 (long long)mCurrentBucketStartTimeNs);
372}
373
Yangsterf2bee6f2017-11-29 12:01:05 -0800374size_t GaugeMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800375 size_t totalSize = 0;
376 for (const auto& pair : mPastBuckets) {
377 totalSize += pair.second.size() * kBucketSize;
378 }
379 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800380}
381
Yangster1d4d6862017-10-31 12:58:51 -0700382} // namespace statsd
383} // namespace os
384} // namespace android