blob: 9c65371ba958b08c7244aa941ff53e98da3659af [file] [log] [blame]
Yao Chen729093d2017-10-16 10:33:26 -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
Yao Chen3c0b95c2017-12-16 14:34:20 -080017#define DEBUG false
Yao Chen5154a3792017-10-30 22:57:06 -070018
Yao Chen729093d2017-10-16 10:33:26 -070019#include "Log.h"
Yao Chen5154a3792017-10-30 22:57:06 -070020#include "DurationMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Yao Chen729093d2017-10-16 10:33:26 -070022#include "stats_util.h"
Yangster-mac20877162017-12-22 17:19:39 -080023#include "stats_log_util.h"
Yao Chen729093d2017-10-16 10:33:26 -070024
Yao Chen729093d2017-10-16 10:33:26 -070025#include <limits.h>
26#include <stdlib.h>
27
yrob0378b02017-11-09 20:36:25 -080028using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080029using android::util::FIELD_TYPE_BOOL;
30using android::util::FIELD_TYPE_FLOAT;
31using android::util::FIELD_TYPE_INT32;
32using android::util::FIELD_TYPE_INT64;
33using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080034using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080035using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070036using std::string;
37using std::unordered_map;
38using std::vector;
39
40namespace android {
41namespace os {
42namespace statsd {
43
yro2b0f8862017-11-06 14:27:31 -080044// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080045const int FIELD_ID_ID = 1;
yro2b0f8862017-11-06 14:27:31 -080046const int FIELD_ID_DURATION_METRICS = 6;
47// for DurationMetricDataWrapper
48const int FIELD_ID_DATA = 1;
49// for DurationMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080050const int FIELD_ID_DIMENSION_IN_WHAT = 1;
51const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
52const int FIELD_ID_BUCKET_INFO = 3;
yro2b0f8862017-11-06 14:27:31 -080053// for DurationBucketInfo
Yangster-mac330af582018-02-08 15:24:38 -080054const int FIELD_ID_START_BUCKET_ELAPSED_NANOS = 1;
55const int FIELD_ID_END_BUCKET_ELAPSED_NANOS = 2;
yro2b0f8862017-11-06 14:27:31 -080056const int FIELD_ID_DURATION = 3;
57
Yao Chenb3561512017-11-21 18:07:17 -080058DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric,
Yao Chen729093d2017-10-16 10:33:26 -070059 const int conditionIndex, const size_t startIndex,
60 const size_t stopIndex, const size_t stopAllIndex,
Yao Chen0ea19902017-11-15 15:44:45 -080061 const bool nesting,
Yao Chen5154a3792017-10-30 22:57:06 -070062 const sp<ConditionWizard>& wizard,
Yangster-mac20877162017-12-22 17:19:39 -080063 const FieldMatcher& internalDimensions,
Yao Chen93fe3a32017-11-02 13:52:59 -070064 const uint64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080065 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080066 mAggregationType(metric.aggregation_type()),
Yao Chen729093d2017-10-16 10:33:26 -070067 mStartIndex(startIndex),
68 mStopIndex(stopIndex),
Yao Chen5154a3792017-10-30 22:57:06 -070069 mStopAllIndex(stopAllIndex),
Yao Chen8a8d16c2018-02-08 14:50:40 -080070 mNested(nesting) {
Yao Chen729093d2017-10-16 10:33:26 -070071 // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract
72 // them in the base class, because the proto generated CountMetric, and DurationMetric are
73 // not related. Maybe we should add a template in the future??
Yangster-macb8144812018-01-04 10:56:23 -080074 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080075 mBucketSizeNs =
76 TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000;
Yao Chen729093d2017-10-16 10:33:26 -070077 } else {
78 mBucketSizeNs = LLONG_MAX;
79 }
80
Yao Chen8a8d16c2018-02-08 14:50:40 -080081 if (metric.has_dimensions_in_what()) {
82 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
83 }
84
85 if (internalDimensions.has_field()) {
86 translateFieldMatcher(internalDimensions, &mInternalDimensions);
87 }
88
89 if (metric.has_dimensions_in_condition()) {
90 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
91 }
Yao Chen729093d2017-10-16 10:33:26 -070092
93 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080094 for (const auto& link : metric.links()) {
95 Metric2Condition mc;
96 mc.conditionId = link.condition();
97 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
98 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
99 mMetric2ConditionLinks.push_back(mc);
100 }
Yao Chen729093d2017-10-16 10:33:26 -0700101 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800102 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -0700103
Yangster-mac94e197c2018-01-02 16:03:03 -0800104 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -0700105 (long long)mBucketSizeNs, (long long)mStartTimeNs);
106}
107
108DurationMetricProducer::~DurationMetricProducer() {
109 VLOG("~DurationMetric() called");
110}
111
Bookatz857aaa52017-12-19 15:29:06 -0800112sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) {
113 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800114 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey);
115 if (anomalyTracker != nullptr) {
116 mAnomalyTrackers.push_back(anomalyTracker);
117 }
118 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800119}
120
Yao Chen5154a3792017-10-30 22:57:06 -0700121unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800122 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800123 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800124 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800125 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800126 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800127 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
128 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800129 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800130 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800131 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800132 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
133 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a3792017-10-30 22:57:06 -0700134 }
135}
136
Yangsterf2bee6f2017-11-29 12:01:05 -0800137void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800138 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800139 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800140
Yao Chen729093d2017-10-16 10:33:26 -0700141 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac93694462018-01-22 20:49:31 -0800142 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a3792017-10-30 22:57:06 -0700143 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700144 }
Yangster-mac93694462018-01-22 20:49:31 -0800145
146
147 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
Yao Chen4c959cb2018-02-13 13:27:48 -0800148 mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition,
149 &conditionDimensionsKeySet);
Yangster-mac93694462018-01-22 20:49:31 -0800150
Yangster-mac93694462018-01-22 20:49:31 -0800151 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
152 conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition());
153 }
154 std::unordered_set<MetricDimensionKey> newKeys;
155 for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) {
156 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
157 auto newKey =
158 MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey);
159 if (newKeys.find(newKey) == newKeys.end()) {
160 mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime);
161 mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey);
162 mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime);
163 }
164 newKeys.insert(newKey);
165 }
166 }
Yao Chen729093d2017-10-16 10:33:26 -0700167}
168
Yangsterf2bee6f2017-11-29 12:01:05 -0800169void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
170 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800171 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700172 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800173 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700174 // TODO: need to populate the condition change time from the event which triggers the condition
175 // change, instead of using current time.
Yangster-mac93694462018-01-22 20:49:31 -0800176 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a3792017-10-30 22:57:06 -0700177 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700178 }
179}
180
Yao Chen288c6002017-12-12 13:43:18 -0800181void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
182 ProtoOutputStream* protoOutput) {
183 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800184 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800185 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800186 return;
187 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000188
Yangster-mac94e197c2018-01-02 16:03:03 -0800189 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800190 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
191
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000193
Yao Chen729093d2017-10-16 10:33:26 -0700194 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800195 const MetricDimensionKey& dimensionKey = pair.first;
196 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800197
yrob0378b02017-11-09 20:36:25 -0800198 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800199 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800200
Yangster-mac20877162017-12-22 17:19:39 -0800201 // First fill dimension.
202 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800203 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800204 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800205 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800206
Yangster-mac93694462018-01-22 20:49:31 -0800207 if (dimensionKey.hasDimensionKeyInCondition()) {
208 long long dimensionInConditionToken = protoOutput->start(
209 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800210 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800211 protoOutput->end(dimensionInConditionToken);
212 }
213
yro2b0f8862017-11-06 14:27:31 -0800214 // Then fill bucket_info (DurationBucketInfo).
215 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800216 long long bucketInfoToken = protoOutput->start(
217 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac330af582018-02-08 15:24:38 -0800218 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800219 (long long)bucket.mBucketStartNs);
Yangster-mac330af582018-02-08 15:24:38 -0800220 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800221 (long long)bucket.mBucketEndNs);
222 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
223 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800224 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
225 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
226 }
227
Yao Chen288c6002017-12-12 13:43:18 -0800228 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700229 }
yro2b0f8862017-11-06 14:27:31 -0800230
Yao Chen288c6002017-12-12 13:43:18 -0800231 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800232 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800233}
Yao Chen729093d2017-10-16 10:33:26 -0700234
David Chen27785a82018-01-19 17:06:45 -0800235void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
236 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
237
238 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700239 return;
240 }
Yao Chen5154a3792017-10-30 22:57:06 -0700241 VLOG("flushing...........");
Yangster-mac93694462018-01-22 20:49:31 -0800242 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
243 it != mCurrentSlicedDurationTrackerMap.end();) {
David Chen27785a82018-01-19 17:06:45 -0800244 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
Yao Chen5154a3792017-10-30 22:57:06 -0700245 VLOG("erase bucket for key %s", it->first.c_str());
Yangster-mac93694462018-01-22 20:49:31 -0800246 it = mCurrentSlicedDurationTrackerMap.erase(it);
Yao Chend41c4222017-11-15 19:26:14 -0800247 } else {
248 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700249 }
250 }
Yao Chen5154a3792017-10-30 22:57:06 -0700251
David Chen27785a82018-01-19 17:06:45 -0800252 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
253 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800254 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a3792017-10-30 22:57:06 -0700255}
256
David Chen27785a82018-01-19 17:06:45 -0800257void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
258 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
259 it != mCurrentSlicedDurationTrackerMap.end();) {
260 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
261 VLOG("erase bucket for key %s", it->first.c_str());
262 it = mCurrentSlicedDurationTrackerMap.erase(it);
263 } else {
264 ++it;
265 }
266 }
267}
268
Yao Chen884c8c12018-01-26 10:36:25 -0800269void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800270 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800271 return;
272 }
273
274 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800275 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800276 if (verbose) {
Yangster-mac93694462018-01-22 20:49:31 -0800277 for (const auto& slice : mCurrentSlicedDurationTrackerMap) {
Yao Chen884c8c12018-01-26 10:36:25 -0800278 fprintf(out, "\t%s\n", slice.first.c_str());
279 slice.second->dumpStates(out, verbose);
280 }
281 }
282}
283
Yangster-mac93694462018-01-22 20:49:31 -0800284bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800285 // the key is not new, we are good.
Yangster-mac93694462018-01-22 20:49:31 -0800286 if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800287 return false;
288 }
289 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800290 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
291 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800292 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800293 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
294 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800295 ALOGE("DurationMetric %lld dropping data for dimension key %s",
296 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800297 return true;
298 }
299 }
300 return false;
301}
302
Yangsterf2bee6f2017-11-29 12:01:05 -0800303void DurationMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800304 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800305 const ConditionKey& conditionKeys, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800306 const LogEvent& event) {
Yangster-mac330af582018-02-08 15:24:38 -0800307 flushIfNeededLocked(event.GetElapsedTimestampNs());
Yao Chen5154a3792017-10-30 22:57:06 -0700308
Yao Chen6a8c7992017-11-29 20:02:07 +0000309 if (matcherIndex == mStopAllIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800310 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yangster-mac330af582018-02-08 15:24:38 -0800311 pair.second->noteStopAll(event.GetElapsedTimestampNs());
Yao Chen6a8c7992017-11-29 20:02:07 +0000312 }
313 return;
314 }
315
Yangster-mac93694462018-01-22 20:49:31 -0800316 if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800317 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800318 return;
319 }
Yangster-mac93694462018-01-22 20:49:31 -0800320 mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000321 }
Yao Chen5154a3792017-10-30 22:57:06 -0700322
Yangster-mac93694462018-01-22 20:49:31 -0800323 auto it = mCurrentSlicedDurationTrackerMap.find(eventKey);
Yao Chen5154a3792017-10-30 22:57:06 -0700324
Yao Chen8a8d16c2018-02-08 14:50:40 -0800325 std::vector<HashableDimensionKey> values;
326 filterValues(mInternalDimensions, event.getValues(), &values);
Yangster-mac20877162017-12-22 17:19:39 -0800327 if (values.empty()) {
328 if (matcherIndex == mStartIndex) {
329 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
Yangster-mac330af582018-02-08 15:24:38 -0800330 event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800331 } else if (matcherIndex == mStopIndex) {
Yangster-mac330af582018-02-08 15:24:38 -0800332 it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetElapsedTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800333 }
334 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800335 for (const auto& value : values) {
Yangster-mac20877162017-12-22 17:19:39 -0800336 if (matcherIndex == mStartIndex) {
Yangster-mac330af582018-02-08 15:24:38 -0800337 it->second->noteStart(value, condition, event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800338 } else if (matcherIndex == mStopIndex) {
Yangster-mac330af582018-02-08 15:24:38 -0800339 it->second->noteStop(value, event.GetElapsedTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800340 }
341 }
Yao Chen5154a3792017-10-30 22:57:06 -0700342 }
Yangster-mac20877162017-12-22 17:19:39 -0800343
Yao Chen729093d2017-10-16 10:33:26 -0700344}
345
Yangsterf2bee6f2017-11-29 12:01:05 -0800346size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800347 size_t totalSize = 0;
348 for (const auto& pair : mPastBuckets) {
349 totalSize += pair.second.size() * kBucketSize;
350 }
351 return totalSize;
yro69007c82017-10-26 20:42:57 -0700352}
353
Yao Chen729093d2017-10-16 10:33:26 -0700354} // namespace statsd
355} // namespace os
356} // namespace android