blob: 078c15222076f9d100359e01f8b2102272dac8e3 [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 Chen5154a372017-10-30 22:57:06 -070018
Yao Chen729093d2017-10-16 10:33:26 -070019#include "Log.h"
Yao Chen5154a372017-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 Chen5154a372017-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 Chen5154a372017-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);
Yangster-maca7fb12d2018-01-03 17:17:20 -0800114 if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) {
115 ALOGW("invalid alert: threshold (%f) > possible recordable value (%d x %lld)",
116 alert.trigger_if_sum_gt(), alert.num_buckets(),
Bookatz450099d2017-11-30 17:09:30 -0800117 (long long)mBucketSizeNs);
118 return nullptr;
119 }
Bookatz857aaa52017-12-19 15:29:06 -0800120 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey);
121 if (anomalyTracker != nullptr) {
122 mAnomalyTrackers.push_back(anomalyTracker);
123 }
124 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800125}
126
Yao Chen5154a372017-10-30 22:57:06 -0700127unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800128 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800129 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800130 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800131 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800132 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800133 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
134 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800135 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800136 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800137 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800138 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
139 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a372017-10-30 22:57:06 -0700140 }
141}
142
Yangsterf2bee6f2017-11-29 12:01:05 -0800143void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800144 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800145 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800146
Yao Chen729093d2017-10-16 10:33:26 -0700147 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac93694462018-01-22 20:49:31 -0800148 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700149 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700150 }
Yangster-mac93694462018-01-22 20:49:31 -0800151
152
153 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
154 ConditionState conditionState = mWizard->getMetConditionDimension(
155 mConditionTrackerIndex, mDimensionsInCondition, &conditionDimensionsKeySet);
156
157 bool condition = (conditionState == ConditionState::kTrue);
158 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
159 conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition());
160 }
161 std::unordered_set<MetricDimensionKey> newKeys;
162 for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) {
163 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
164 auto newKey =
165 MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey);
166 if (newKeys.find(newKey) == newKeys.end()) {
167 mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime);
168 mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey);
169 mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime);
170 }
171 newKeys.insert(newKey);
172 }
173 }
Yao Chen729093d2017-10-16 10:33:26 -0700174}
175
Yangsterf2bee6f2017-11-29 12:01:05 -0800176void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
177 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800178 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700179 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800180 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700181 // TODO: need to populate the condition change time from the event which triggers the condition
182 // change, instead of using current time.
Yangster-mac93694462018-01-22 20:49:31 -0800183 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700184 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700185 }
186}
187
Yao Chen288c6002017-12-12 13:43:18 -0800188void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
189 ProtoOutputStream* protoOutput) {
190 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800191 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800193 return;
194 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000195
Yangster-mac94e197c2018-01-02 16:03:03 -0800196 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800197 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
198
Yao Chen8a8d16c2018-02-08 14:50:40 -0800199 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000200
Yao Chen729093d2017-10-16 10:33:26 -0700201 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800202 const MetricDimensionKey& dimensionKey = pair.first;
203 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800204
yrob0378b02017-11-09 20:36:25 -0800205 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800206 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800207
Yangster-mac20877162017-12-22 17:19:39 -0800208 // First fill dimension.
209 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800210 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800211 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800212 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800213
Yangster-mac93694462018-01-22 20:49:31 -0800214 if (dimensionKey.hasDimensionKeyInCondition()) {
215 long long dimensionInConditionToken = protoOutput->start(
216 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800217 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800218 protoOutput->end(dimensionInConditionToken);
219 }
220
yro2b0f8862017-11-06 14:27:31 -0800221 // Then fill bucket_info (DurationBucketInfo).
222 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800223 long long bucketInfoToken = protoOutput->start(
224 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
225 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
226 (long long)bucket.mBucketStartNs);
227 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
228 (long long)bucket.mBucketEndNs);
229 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
230 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800231 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
232 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
233 }
234
Yao Chen288c6002017-12-12 13:43:18 -0800235 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700236 }
yro2b0f8862017-11-06 14:27:31 -0800237
Yao Chen288c6002017-12-12 13:43:18 -0800238 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800239 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800240}
Yao Chen729093d2017-10-16 10:33:26 -0700241
David Chen27785a82018-01-19 17:06:45 -0800242void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
243 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
244
245 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700246 return;
247 }
Yao Chen5154a372017-10-30 22:57:06 -0700248 VLOG("flushing...........");
Yangster-mac93694462018-01-22 20:49:31 -0800249 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
250 it != mCurrentSlicedDurationTrackerMap.end();) {
David Chen27785a82018-01-19 17:06:45 -0800251 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
Yao Chen5154a372017-10-30 22:57:06 -0700252 VLOG("erase bucket for key %s", it->first.c_str());
Yangster-mac93694462018-01-22 20:49:31 -0800253 it = mCurrentSlicedDurationTrackerMap.erase(it);
Yao Chend41c4222017-11-15 19:26:14 -0800254 } else {
255 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700256 }
257 }
Yao Chen5154a372017-10-30 22:57:06 -0700258
David Chen27785a82018-01-19 17:06:45 -0800259 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
260 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800261 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700262}
263
David Chen27785a82018-01-19 17:06:45 -0800264void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
265 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
266 it != mCurrentSlicedDurationTrackerMap.end();) {
267 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
268 VLOG("erase bucket for key %s", it->first.c_str());
269 it = mCurrentSlicedDurationTrackerMap.erase(it);
270 } else {
271 ++it;
272 }
273 }
274}
275
Yao Chen884c8c12018-01-26 10:36:25 -0800276void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800277 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800278 return;
279 }
280
281 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800282 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800283 if (verbose) {
Yangster-mac93694462018-01-22 20:49:31 -0800284 for (const auto& slice : mCurrentSlicedDurationTrackerMap) {
Yao Chen884c8c12018-01-26 10:36:25 -0800285 fprintf(out, "\t%s\n", slice.first.c_str());
286 slice.second->dumpStates(out, verbose);
287 }
288 }
289}
290
Yangster-mac93694462018-01-22 20:49:31 -0800291bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800292 // the key is not new, we are good.
Yangster-mac93694462018-01-22 20:49:31 -0800293 if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800294 return false;
295 }
296 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800297 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
298 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800299 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800300 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
301 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800302 ALOGE("DurationMetric %lld dropping data for dimension key %s",
303 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800304 return true;
305 }
306 }
307 return false;
308}
309
Yangsterf2bee6f2017-11-29 12:01:05 -0800310void DurationMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800311 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800312 const ConditionKey& conditionKeys, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800313 const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800314 flushIfNeededLocked(event.GetTimestampNs());
Yao Chen5154a372017-10-30 22:57:06 -0700315
Yao Chen6a8c7992017-11-29 20:02:07 +0000316 if (matcherIndex == mStopAllIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800317 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000318 pair.second->noteStopAll(event.GetTimestampNs());
319 }
320 return;
321 }
322
Yangster-mac93694462018-01-22 20:49:31 -0800323 if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800324 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800325 return;
326 }
Yangster-mac93694462018-01-22 20:49:31 -0800327 mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000328 }
Yao Chen5154a372017-10-30 22:57:06 -0700329
Yangster-mac93694462018-01-22 20:49:31 -0800330 auto it = mCurrentSlicedDurationTrackerMap.find(eventKey);
Yao Chen5154a372017-10-30 22:57:06 -0700331
Yao Chen8a8d16c2018-02-08 14:50:40 -0800332 std::vector<HashableDimensionKey> values;
333 filterValues(mInternalDimensions, event.getValues(), &values);
Yangster-mac20877162017-12-22 17:19:39 -0800334 if (values.empty()) {
335 if (matcherIndex == mStartIndex) {
336 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
337 event.GetTimestampNs(), conditionKeys);
338 } else if (matcherIndex == mStopIndex) {
339 it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetTimestampNs(), false);
340 }
341 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800342 for (const auto& value : values) {
Yangster-mac20877162017-12-22 17:19:39 -0800343 if (matcherIndex == mStartIndex) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800344 it->second->noteStart(value, condition, event.GetTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800345 } else if (matcherIndex == mStopIndex) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800346 it->second->noteStop(value, event.GetTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800347 }
348 }
Yao Chen5154a372017-10-30 22:57:06 -0700349 }
Yangster-mac20877162017-12-22 17:19:39 -0800350
Yao Chen729093d2017-10-16 10:33:26 -0700351}
352
Yangsterf2bee6f2017-11-29 12:01:05 -0800353size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800354 size_t totalSize = 0;
355 for (const auto& pair : mPastBuckets) {
356 totalSize += pair.second.size() * kBucketSize;
357 }
358 return totalSize;
yro69007c82017-10-26 20:42:57 -0700359}
360
Yao Chen729093d2017-10-16 10:33:26 -0700361} // namespace statsd
362} // namespace os
363} // namespace android