blob: 16cac99f1fb98bd6345b3066630002c32dba8cf0 [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
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 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
Yangster-mac932ecec2018-02-01 10:23:52 -0800112sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(
113 const Alert &alert, const sp<AlarmMonitor>& anomalyAlarmMonitor) {
Bookatz857aaa52017-12-19 15:29:06 -0800114 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac932ecec2018-02-01 10:23:52 -0800115 sp<DurationAnomalyTracker> anomalyTracker =
116 new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor);
Bookatz857aaa52017-12-19 15:29:06 -0800117 if (anomalyTracker != nullptr) {
118 mAnomalyTrackers.push_back(anomalyTracker);
119 }
120 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800121}
122
Yao Chen5154a372017-10-30 22:57:06 -0700123unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800124 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800125 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800126 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800127 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800128 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800129 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
130 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800131 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800132 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800133 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800134 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
135 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a372017-10-30 22:57:06 -0700136 }
137}
138
Yangsterf2bee6f2017-11-29 12:01:05 -0800139void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800140 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800141 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800142
Yao Chen729093d2017-10-16 10:33:26 -0700143 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac93694462018-01-22 20:49:31 -0800144 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700145 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700146 }
Yangster-mac93694462018-01-22 20:49:31 -0800147
148
149 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
Yao Chen4c959cb2018-02-13 13:27:48 -0800150 mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition,
151 &conditionDimensionsKeySet);
Yangster-mac93694462018-01-22 20:49:31 -0800152
Yangster-mac93694462018-01-22 20:49:31 -0800153 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
154 conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition());
155 }
156 std::unordered_set<MetricDimensionKey> newKeys;
157 for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) {
158 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
159 auto newKey =
160 MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey);
161 if (newKeys.find(newKey) == newKeys.end()) {
162 mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime);
163 mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey);
164 mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime);
165 }
166 newKeys.insert(newKey);
167 }
168 }
Yao Chen729093d2017-10-16 10:33:26 -0700169}
170
Yangsterf2bee6f2017-11-29 12:01:05 -0800171void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
172 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800173 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700174 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800175 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700176 // TODO: need to populate the condition change time from the event which triggers the condition
177 // change, instead of using current time.
Yangster-mac93694462018-01-22 20:49:31 -0800178 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700179 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700180 }
181}
182
Yao Chen288c6002017-12-12 13:43:18 -0800183void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
184 ProtoOutputStream* protoOutput) {
185 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800186 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800187 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800188 return;
189 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000190
Yangster-mac94e197c2018-01-02 16:03:03 -0800191 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800192 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
193
Yao Chen8a8d16c2018-02-08 14:50:40 -0800194 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000195
Yao Chen729093d2017-10-16 10:33:26 -0700196 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800197 const MetricDimensionKey& dimensionKey = pair.first;
198 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800199
yrob0378b02017-11-09 20:36:25 -0800200 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800201 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800202
Yangster-mac20877162017-12-22 17:19:39 -0800203 // First fill dimension.
204 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800205 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800206 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800207 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800208
Yangster-mac93694462018-01-22 20:49:31 -0800209 if (dimensionKey.hasDimensionKeyInCondition()) {
210 long long dimensionInConditionToken = protoOutput->start(
211 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800212 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800213 protoOutput->end(dimensionInConditionToken);
214 }
215
yro2b0f8862017-11-06 14:27:31 -0800216 // Then fill bucket_info (DurationBucketInfo).
217 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800218 long long bucketInfoToken = protoOutput->start(
219 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac330af582018-02-08 15:24:38 -0800220 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800221 (long long)bucket.mBucketStartNs);
Yangster-mac330af582018-02-08 15:24:38 -0800222 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800223 (long long)bucket.mBucketEndNs);
224 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
225 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800226 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
227 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
228 }
229
Yao Chen288c6002017-12-12 13:43:18 -0800230 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700231 }
yro2b0f8862017-11-06 14:27:31 -0800232
Yao Chen288c6002017-12-12 13:43:18 -0800233 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800234 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800235}
Yao Chen729093d2017-10-16 10:33:26 -0700236
David Chen27785a82018-01-19 17:06:45 -0800237void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
238 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
239
240 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700241 return;
242 }
Yao Chen5154a372017-10-30 22:57:06 -0700243 VLOG("flushing...........");
Yangster-mac93694462018-01-22 20:49:31 -0800244 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
245 it != mCurrentSlicedDurationTrackerMap.end();) {
David Chen27785a82018-01-19 17:06:45 -0800246 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
Yao Chen5154a372017-10-30 22:57:06 -0700247 VLOG("erase bucket for key %s", it->first.c_str());
Yangster-mac93694462018-01-22 20:49:31 -0800248 it = mCurrentSlicedDurationTrackerMap.erase(it);
Yao Chend41c4222017-11-15 19:26:14 -0800249 } else {
250 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700251 }
252 }
Yao Chen5154a372017-10-30 22:57:06 -0700253
David Chen27785a82018-01-19 17:06:45 -0800254 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
255 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800256 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700257}
258
David Chen27785a82018-01-19 17:06:45 -0800259void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
260 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
261 it != mCurrentSlicedDurationTrackerMap.end();) {
262 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
263 VLOG("erase bucket for key %s", it->first.c_str());
264 it = mCurrentSlicedDurationTrackerMap.erase(it);
265 } else {
266 ++it;
267 }
268 }
269}
270
Yao Chen884c8c12018-01-26 10:36:25 -0800271void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800272 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800273 return;
274 }
275
276 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800277 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800278 if (verbose) {
Yangster-mac93694462018-01-22 20:49:31 -0800279 for (const auto& slice : mCurrentSlicedDurationTrackerMap) {
Yao Chen884c8c12018-01-26 10:36:25 -0800280 fprintf(out, "\t%s\n", slice.first.c_str());
281 slice.second->dumpStates(out, verbose);
282 }
283 }
284}
285
Yangster-mac93694462018-01-22 20:49:31 -0800286bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800287 // the key is not new, we are good.
Yangster-mac93694462018-01-22 20:49:31 -0800288 if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800289 return false;
290 }
291 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800292 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
293 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800294 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800295 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
296 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800297 ALOGE("DurationMetric %lld dropping data for dimension key %s",
298 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800299 return true;
300 }
301 }
302 return false;
303}
304
Yangsterf2bee6f2017-11-29 12:01:05 -0800305void DurationMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800306 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800307 const ConditionKey& conditionKeys, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800308 const LogEvent& event) {
Yangster-mac330af582018-02-08 15:24:38 -0800309 flushIfNeededLocked(event.GetElapsedTimestampNs());
Yao Chen5154a372017-10-30 22:57:06 -0700310
Yao Chen6a8c7992017-11-29 20:02:07 +0000311 if (matcherIndex == mStopAllIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800312 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yangster-mac330af582018-02-08 15:24:38 -0800313 pair.second->noteStopAll(event.GetElapsedTimestampNs());
Yao Chen6a8c7992017-11-29 20:02:07 +0000314 }
315 return;
316 }
317
Yangster-mac93694462018-01-22 20:49:31 -0800318 if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800319 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800320 return;
321 }
Yangster-mac93694462018-01-22 20:49:31 -0800322 mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000323 }
Yao Chen5154a372017-10-30 22:57:06 -0700324
Yangster-mac93694462018-01-22 20:49:31 -0800325 auto it = mCurrentSlicedDurationTrackerMap.find(eventKey);
Yao Chen5154a372017-10-30 22:57:06 -0700326
Yao Chen8a8d16c2018-02-08 14:50:40 -0800327 std::vector<HashableDimensionKey> values;
328 filterValues(mInternalDimensions, event.getValues(), &values);
Yangster-mac20877162017-12-22 17:19:39 -0800329 if (values.empty()) {
330 if (matcherIndex == mStartIndex) {
331 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
Yangster-mac330af582018-02-08 15:24:38 -0800332 event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800333 } else if (matcherIndex == mStopIndex) {
Yangster-mac330af582018-02-08 15:24:38 -0800334 it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetElapsedTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800335 }
336 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800337 for (const auto& value : values) {
Yangster-mac20877162017-12-22 17:19:39 -0800338 if (matcherIndex == mStartIndex) {
Yangster-mac330af582018-02-08 15:24:38 -0800339 it->second->noteStart(value, condition, event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800340 } else if (matcherIndex == mStopIndex) {
Yangster-mac330af582018-02-08 15:24:38 -0800341 it->second->noteStop(value, event.GetElapsedTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800342 }
343 }
Yao Chen5154a372017-10-30 22:57:06 -0700344 }
Yangster-mac20877162017-12-22 17:19:39 -0800345
Yao Chen729093d2017-10-16 10:33:26 -0700346}
347
Yangsterf2bee6f2017-11-29 12:01:05 -0800348size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800349 size_t totalSize = 0;
350 for (const auto& pair : mPastBuckets) {
351 totalSize += pair.second.size() * kBucketSize;
352 }
353 return totalSize;
yro69007c82017-10-26 20:42:57 -0700354}
355
Yao Chen729093d2017-10-16 10:33:26 -0700356} // namespace statsd
357} // namespace os
358} // namespace android