blob: 6b321e11edcf08f4f48b89a026d4e87252783c52 [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
54const int FIELD_ID_START_BUCKET_NANOS = 1;
55const int FIELD_ID_END_BUCKET_NANOS = 2;
56const 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()) {
75 mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000;
Yao Chen729093d2017-10-16 10:33:26 -070076 } else {
77 mBucketSizeNs = LLONG_MAX;
78 }
79
Yao Chen8a8d16c2018-02-08 14:50:40 -080080 if (metric.has_dimensions_in_what()) {
81 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
82 }
83
84 if (internalDimensions.has_field()) {
85 translateFieldMatcher(internalDimensions, &mInternalDimensions);
86 }
87
88 if (metric.has_dimensions_in_condition()) {
89 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
90 }
Yao Chen729093d2017-10-16 10:33:26 -070091
92 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080093 for (const auto& link : metric.links()) {
94 Metric2Condition mc;
95 mc.conditionId = link.condition();
96 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
97 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
98 mMetric2ConditionLinks.push_back(mc);
99 }
Yao Chen729093d2017-10-16 10:33:26 -0700100 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800101 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -0700102
Yangster-mac94e197c2018-01-02 16:03:03 -0800103 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -0700104 (long long)mBucketSizeNs, (long long)mStartTimeNs);
105}
106
107DurationMetricProducer::~DurationMetricProducer() {
108 VLOG("~DurationMetric() called");
109}
110
Bookatz857aaa52017-12-19 15:29:06 -0800111sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) {
112 std::lock_guard<std::mutex> lock(mMutex);
Yangster-maca7fb12d2018-01-03 17:17:20 -0800113 if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) {
114 ALOGW("invalid alert: threshold (%f) > possible recordable value (%d x %lld)",
115 alert.trigger_if_sum_gt(), alert.num_buckets(),
Bookatz450099d2017-11-30 17:09:30 -0800116 (long long)mBucketSizeNs);
117 return nullptr;
118 }
Bookatz857aaa52017-12-19 15:29:06 -0800119 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey);
120 if (anomalyTracker != nullptr) {
121 mAnomalyTrackers.push_back(anomalyTracker);
122 }
123 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800124}
125
Yao Chen5154a3792017-10-30 22:57:06 -0700126unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800127 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800128 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800129 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800130 return make_unique<OringDurationTracker>(
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);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800134 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800135 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800136 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800137 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
138 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a3792017-10-30 22:57:06 -0700139 }
140}
141
Yangsterf2bee6f2017-11-29 12:01:05 -0800142void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800143 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800144 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800145
Yao Chen729093d2017-10-16 10:33:26 -0700146 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac93694462018-01-22 20:49:31 -0800147 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a3792017-10-30 22:57:06 -0700148 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700149 }
Yangster-mac93694462018-01-22 20:49:31 -0800150
151
152 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
153 ConditionState conditionState = mWizard->getMetConditionDimension(
154 mConditionTrackerIndex, mDimensionsInCondition, &conditionDimensionsKeySet);
155
156 bool condition = (conditionState == ConditionState::kTrue);
157 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
158 conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition());
159 }
160 std::unordered_set<MetricDimensionKey> newKeys;
161 for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) {
162 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
163 auto newKey =
164 MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey);
165 if (newKeys.find(newKey) == newKeys.end()) {
166 mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime);
167 mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey);
168 mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime);
169 }
170 newKeys.insert(newKey);
171 }
172 }
Yao Chen729093d2017-10-16 10:33:26 -0700173}
174
Yangsterf2bee6f2017-11-29 12:01:05 -0800175void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
176 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800177 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700178 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800179 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700180 // TODO: need to populate the condition change time from the event which triggers the condition
181 // change, instead of using current time.
Yangster-mac93694462018-01-22 20:49:31 -0800182 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a3792017-10-30 22:57:06 -0700183 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700184 }
185}
186
Yao Chen288c6002017-12-12 13:43:18 -0800187void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
188 ProtoOutputStream* protoOutput) {
189 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800190 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800191 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800192 return;
193 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000194
Yangster-mac94e197c2018-01-02 16:03:03 -0800195 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800196 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
197
Yao Chen8a8d16c2018-02-08 14:50:40 -0800198 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000199
Yao Chen729093d2017-10-16 10:33:26 -0700200 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800201 const MetricDimensionKey& dimensionKey = pair.first;
202 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800203
yrob0378b02017-11-09 20:36:25 -0800204 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800205 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800206
Yangster-mac20877162017-12-22 17:19:39 -0800207 // First fill dimension.
208 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800209 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800210 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800211 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800212
Yangster-mac93694462018-01-22 20:49:31 -0800213 if (dimensionKey.hasDimensionKeyInCondition()) {
214 long long dimensionInConditionToken = protoOutput->start(
215 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800216 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800217 protoOutput->end(dimensionInConditionToken);
218 }
219
yro2b0f8862017-11-06 14:27:31 -0800220 // Then fill bucket_info (DurationBucketInfo).
221 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800222 long long bucketInfoToken = protoOutput->start(
223 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
224 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
225 (long long)bucket.mBucketStartNs);
226 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
227 (long long)bucket.mBucketEndNs);
228 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
229 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800230 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
231 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
232 }
233
Yao Chen288c6002017-12-12 13:43:18 -0800234 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700235 }
yro2b0f8862017-11-06 14:27:31 -0800236
Yao Chen288c6002017-12-12 13:43:18 -0800237 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800238 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800239}
Yao Chen729093d2017-10-16 10:33:26 -0700240
David Chen27785a82018-01-19 17:06:45 -0800241void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
242 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
243
244 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700245 return;
246 }
Yao Chen5154a3792017-10-30 22:57:06 -0700247 VLOG("flushing...........");
Yangster-mac93694462018-01-22 20:49:31 -0800248 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
249 it != mCurrentSlicedDurationTrackerMap.end();) {
David Chen27785a82018-01-19 17:06:45 -0800250 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
Yao Chen5154a3792017-10-30 22:57:06 -0700251 VLOG("erase bucket for key %s", it->first.c_str());
Yangster-mac93694462018-01-22 20:49:31 -0800252 it = mCurrentSlicedDurationTrackerMap.erase(it);
Yao Chend41c4222017-11-15 19:26:14 -0800253 } else {
254 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700255 }
256 }
Yao Chen5154a3792017-10-30 22:57:06 -0700257
David Chen27785a82018-01-19 17:06:45 -0800258 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
259 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800260 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a3792017-10-30 22:57:06 -0700261}
262
David Chen27785a82018-01-19 17:06:45 -0800263void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
264 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
265 it != mCurrentSlicedDurationTrackerMap.end();) {
266 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
267 VLOG("erase bucket for key %s", it->first.c_str());
268 it = mCurrentSlicedDurationTrackerMap.erase(it);
269 } else {
270 ++it;
271 }
272 }
273}
274
Yao Chen884c8c12018-01-26 10:36:25 -0800275void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800276 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800277 return;
278 }
279
280 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800281 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800282 if (verbose) {
Yangster-mac93694462018-01-22 20:49:31 -0800283 for (const auto& slice : mCurrentSlicedDurationTrackerMap) {
Yao Chen884c8c12018-01-26 10:36:25 -0800284 fprintf(out, "\t%s\n", slice.first.c_str());
285 slice.second->dumpStates(out, verbose);
286 }
287 }
288}
289
Yangster-mac93694462018-01-22 20:49:31 -0800290bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800291 // the key is not new, we are good.
Yangster-mac93694462018-01-22 20:49:31 -0800292 if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800293 return false;
294 }
295 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800296 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
297 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800298 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800299 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
300 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800301 ALOGE("DurationMetric %lld dropping data for dimension key %s",
302 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800303 return true;
304 }
305 }
306 return false;
307}
308
Yangsterf2bee6f2017-11-29 12:01:05 -0800309void DurationMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800310 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800311 const ConditionKey& conditionKeys, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800312 const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800313 flushIfNeededLocked(event.GetTimestampNs());
Yao Chen5154a3792017-10-30 22:57:06 -0700314
Yao Chen6a8c7992017-11-29 20:02:07 +0000315 if (matcherIndex == mStopAllIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800316 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000317 pair.second->noteStopAll(event.GetTimestampNs());
318 }
319 return;
320 }
321
Yangster-mac93694462018-01-22 20:49:31 -0800322 if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800323 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800324 return;
325 }
Yangster-mac93694462018-01-22 20:49:31 -0800326 mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000327 }
Yao Chen5154a3792017-10-30 22:57:06 -0700328
Yangster-mac93694462018-01-22 20:49:31 -0800329 auto it = mCurrentSlicedDurationTrackerMap.find(eventKey);
Yao Chen5154a3792017-10-30 22:57:06 -0700330
Yao Chen8a8d16c2018-02-08 14:50:40 -0800331 std::vector<HashableDimensionKey> values;
332 filterValues(mInternalDimensions, event.getValues(), &values);
Yangster-mac20877162017-12-22 17:19:39 -0800333 if (values.empty()) {
334 if (matcherIndex == mStartIndex) {
335 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
336 event.GetTimestampNs(), conditionKeys);
337 } else if (matcherIndex == mStopIndex) {
338 it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetTimestampNs(), false);
339 }
340 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800341 for (const auto& value : values) {
Yangster-mac20877162017-12-22 17:19:39 -0800342 if (matcherIndex == mStartIndex) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800343 it->second->noteStart(value, condition, event.GetTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800344 } else if (matcherIndex == mStopIndex) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800345 it->second->noteStop(value, event.GetTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800346 }
347 }
Yao Chen5154a3792017-10-30 22:57:06 -0700348 }
Yangster-mac20877162017-12-22 17:19:39 -0800349
Yao Chen729093d2017-10-16 10:33:26 -0700350}
351
Yangsterf2bee6f2017-11-29 12:01:05 -0800352size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800353 size_t totalSize = 0;
354 for (const auto& pair : mPastBuckets) {
355 totalSize += pair.second.size() * kBucketSize;
356 }
357 return totalSize;
yro69007c82017-10-26 20:42:57 -0700358}
359
Yao Chen729093d2017-10-16 10:33:26 -0700360} // namespace statsd
361} // namespace os
362} // namespace android