blob: 65cbc4afdad4cb359bf1805d816b47e1279c82d3 [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"
Yangster-mac93694462018-01-22 20:49:31 -080024#include "dimension.h"
Yao Chen729093d2017-10-16 10:33:26 -070025
Yao Chen729093d2017-10-16 10:33:26 -070026#include <limits.h>
27#include <stdlib.h>
28
yrob0378b02017-11-09 20:36:25 -080029using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080030using android::util::FIELD_TYPE_BOOL;
31using android::util::FIELD_TYPE_FLOAT;
32using android::util::FIELD_TYPE_INT32;
33using android::util::FIELD_TYPE_INT64;
34using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080035using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080036using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070037using std::string;
38using std::unordered_map;
39using std::vector;
40
41namespace android {
42namespace os {
43namespace statsd {
44
yro2b0f8862017-11-06 14:27:31 -080045// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080046const int FIELD_ID_ID = 1;
yro2b0f8862017-11-06 14:27:31 -080047const int FIELD_ID_DURATION_METRICS = 6;
48// for DurationMetricDataWrapper
49const int FIELD_ID_DATA = 1;
50// for DurationMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080051const int FIELD_ID_DIMENSION_IN_WHAT = 1;
52const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
53const int FIELD_ID_BUCKET_INFO = 3;
yro2b0f8862017-11-06 14:27:31 -080054// for DurationBucketInfo
55const int FIELD_ID_START_BUCKET_NANOS = 1;
56const int FIELD_ID_END_BUCKET_NANOS = 2;
57const int FIELD_ID_DURATION = 3;
58
Yao Chenb3561512017-11-21 18:07:17 -080059DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric,
Yao Chen729093d2017-10-16 10:33:26 -070060 const int conditionIndex, const size_t startIndex,
61 const size_t stopIndex, const size_t stopAllIndex,
Yao Chen0ea19902017-11-15 15:44:45 -080062 const bool nesting,
Yao Chen5154a372017-10-30 22:57:06 -070063 const sp<ConditionWizard>& wizard,
Yangster-mac20877162017-12-22 17:19:39 -080064 const FieldMatcher& internalDimensions,
Yao Chen93fe3a32017-11-02 13:52:59 -070065 const uint64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080066 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080067 mAggregationType(metric.aggregation_type()),
Yao Chen729093d2017-10-16 10:33:26 -070068 mStartIndex(startIndex),
69 mStopIndex(stopIndex),
Yao Chen5154a372017-10-30 22:57:06 -070070 mStopAllIndex(stopAllIndex),
Yao Chen0ea19902017-11-15 15:44:45 -080071 mNested(nesting),
Yangster-mac20877162017-12-22 17:19:39 -080072 mInternalDimensions(internalDimensions) {
Yao Chen729093d2017-10-16 10:33:26 -070073 // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract
74 // them in the base class, because the proto generated CountMetric, and DurationMetric are
75 // not related. Maybe we should add a template in the future??
Yangster-macb8144812018-01-04 10:56:23 -080076 if (metric.has_bucket()) {
77 mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000;
Yao Chen729093d2017-10-16 10:33:26 -070078 } else {
79 mBucketSizeNs = LLONG_MAX;
80 }
81
82 // TODO: use UidMap if uid->pkg_name is required
Yangster-mac93694462018-01-22 20:49:31 -080083 mDimensionsInWhat = metric.dimensions_in_what();
84 mDimensionsInCondition = metric.dimensions_in_condition();
Yao Chen729093d2017-10-16 10:33:26 -070085
86 if (metric.links().size() > 0) {
87 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
88 metric.links().end());
Yao Chen729093d2017-10-16 10:33:26 -070089 }
Yangster-mac93694462018-01-22 20:49:31 -080090 mConditionSliced = (metric.links().size() > 0)||
91 (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -070092
Yangster-mac94e197c2018-01-02 16:03:03 -080093 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -070094 (long long)mBucketSizeNs, (long long)mStartTimeNs);
95}
96
97DurationMetricProducer::~DurationMetricProducer() {
98 VLOG("~DurationMetric() called");
99}
100
Bookatz857aaa52017-12-19 15:29:06 -0800101sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) {
102 std::lock_guard<std::mutex> lock(mMutex);
Yangster-maca7fb12d2018-01-03 17:17:20 -0800103 if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) {
104 ALOGW("invalid alert: threshold (%f) > possible recordable value (%d x %lld)",
105 alert.trigger_if_sum_gt(), alert.num_buckets(),
Bookatz450099d2017-11-30 17:09:30 -0800106 (long long)mBucketSizeNs);
107 return nullptr;
108 }
Bookatz857aaa52017-12-19 15:29:06 -0800109 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey);
110 if (anomalyTracker != nullptr) {
111 mAnomalyTrackers.push_back(anomalyTracker);
112 }
113 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800114}
115
Yao Chen5154a372017-10-30 22:57:06 -0700116unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800117 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800118 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800119 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800120 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800121 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800122 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
123 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800124 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800125 return make_unique<MaxDurationTracker>(
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);
Yao Chen5154a372017-10-30 22:57:06 -0700129 }
130}
131
Yangsterf2bee6f2017-11-29 12:01:05 -0800132void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800133 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800134 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800135
Yao Chen729093d2017-10-16 10:33:26 -0700136 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac93694462018-01-22 20:49:31 -0800137 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700138 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700139 }
Yangster-mac93694462018-01-22 20:49:31 -0800140
141
142 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
143 ConditionState conditionState = mWizard->getMetConditionDimension(
144 mConditionTrackerIndex, mDimensionsInCondition, &conditionDimensionsKeySet);
145
146 bool condition = (conditionState == ConditionState::kTrue);
147 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
148 conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition());
149 }
150 std::unordered_set<MetricDimensionKey> newKeys;
151 for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) {
152 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
153 auto newKey =
154 MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey);
155 if (newKeys.find(newKey) == newKeys.end()) {
156 mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime);
157 mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey);
158 mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime);
159 }
160 newKeys.insert(newKey);
161 }
162 }
Yao Chen729093d2017-10-16 10:33:26 -0700163}
164
Yangsterf2bee6f2017-11-29 12:01:05 -0800165void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
166 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800167 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700168 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800169 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700170 // TODO: need to populate the condition change time from the event which triggers the condition
171 // change, instead of using current time.
Yangster-mac93694462018-01-22 20:49:31 -0800172 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700173 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700174 }
175}
176
Yangster-mac20877162017-12-22 17:19:39 -0800177void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
178 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800179 report->set_metric_id(mMetricId);
Yangster-mac20877162017-12-22 17:19:39 -0800180
181 auto duration_metrics = report->mutable_duration_metrics();
182 for (const auto& pair : mPastBuckets) {
183 DurationMetricData* metricData = duration_metrics->add_data();
Yangster-mac93694462018-01-22 20:49:31 -0800184 *metricData->mutable_dimensions_in_what() =
185 pair.first.getDimensionKeyInWhat().getDimensionsValue();
186 *metricData->mutable_dimensions_in_condition() =
187 pair.first.getDimensionKeyInCondition().getDimensionsValue();
Yangster-mac20877162017-12-22 17:19:39 -0800188 for (const auto& bucket : pair.second) {
189 auto bucketInfo = metricData->add_bucket_info();
190 bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs);
191 bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs);
192 bucketInfo->set_duration_nanos(bucket.mDuration);
193 }
194 }
195}
196
Yao Chen288c6002017-12-12 13:43:18 -0800197void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
198 ProtoOutputStream* protoOutput) {
199 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800200 if (mPastBuckets.empty()) {
201 return;
202 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000203
Yangster-mac94e197c2018-01-02 16:03:03 -0800204 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800205 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
206
Yangster-mac94e197c2018-01-02 16:03:03 -0800207 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000208
Yao Chen729093d2017-10-16 10:33:26 -0700209 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800210 const MetricDimensionKey& dimensionKey = pair.first;
211 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800212
yrob0378b02017-11-09 20:36:25 -0800213 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800214 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800215
Yangster-mac20877162017-12-22 17:19:39 -0800216 // First fill dimension.
217 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800218 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac93694462018-01-22 20:49:31 -0800219 writeDimensionsValueProtoToStream(
220 dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800221 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800222
Yangster-mac93694462018-01-22 20:49:31 -0800223 if (dimensionKey.hasDimensionKeyInCondition()) {
224 long long dimensionInConditionToken = protoOutput->start(
225 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
226 writeDimensionsValueProtoToStream(
227 dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput);
228 protoOutput->end(dimensionInConditionToken);
229 }
230
yro2b0f8862017-11-06 14:27:31 -0800231 // Then fill bucket_info (DurationBucketInfo).
232 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800233 long long bucketInfoToken = protoOutput->start(
234 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
235 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
236 (long long)bucket.mBucketStartNs);
237 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
238 (long long)bucket.mBucketEndNs);
239 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
240 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800241 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
242 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
243 }
244
Yao Chen288c6002017-12-12 13:43:18 -0800245 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700246 }
yro2b0f8862017-11-06 14:27:31 -0800247
Yao Chen288c6002017-12-12 13:43:18 -0800248 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800249 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800250}
Yao Chen729093d2017-10-16 10:33:26 -0700251
David Chen27785a82018-01-19 17:06:45 -0800252void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
253 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
254
255 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700256 return;
257 }
Yao Chen5154a372017-10-30 22:57:06 -0700258 VLOG("flushing...........");
Yangster-mac93694462018-01-22 20:49:31 -0800259 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
260 it != mCurrentSlicedDurationTrackerMap.end();) {
David Chen27785a82018-01-19 17:06:45 -0800261 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
Yao Chen5154a372017-10-30 22:57:06 -0700262 VLOG("erase bucket for key %s", it->first.c_str());
Yangster-mac93694462018-01-22 20:49:31 -0800263 it = mCurrentSlicedDurationTrackerMap.erase(it);
Yao Chend41c4222017-11-15 19:26:14 -0800264 } else {
265 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700266 }
267 }
Yao Chen5154a372017-10-30 22:57:06 -0700268
David Chen27785a82018-01-19 17:06:45 -0800269 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
270 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800271 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700272}
273
David Chen27785a82018-01-19 17:06:45 -0800274void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
275 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
276 it != mCurrentSlicedDurationTrackerMap.end();) {
277 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
278 VLOG("erase bucket for key %s", it->first.c_str());
279 it = mCurrentSlicedDurationTrackerMap.erase(it);
280 } else {
281 ++it;
282 }
283 }
284}
285
Yao Chen884c8c12018-01-26 10:36:25 -0800286void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800287 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800288 return;
289 }
290
291 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800292 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800293 if (verbose) {
Yangster-mac93694462018-01-22 20:49:31 -0800294 for (const auto& slice : mCurrentSlicedDurationTrackerMap) {
Yao Chen884c8c12018-01-26 10:36:25 -0800295 fprintf(out, "\t%s\n", slice.first.c_str());
296 slice.second->dumpStates(out, verbose);
297 }
298 }
299}
300
Yangster-mac93694462018-01-22 20:49:31 -0800301bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800302 // the key is not new, we are good.
Yangster-mac93694462018-01-22 20:49:31 -0800303 if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800304 return false;
305 }
306 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800307 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
308 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800309 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800310 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
311 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800312 ALOGE("DurationMetric %lld dropping data for dimension key %s",
313 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800314 return true;
315 }
316 }
317 return false;
318}
319
Yangsterf2bee6f2017-11-29 12:01:05 -0800320void DurationMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800321 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800322 const ConditionKey& conditionKeys, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800323 const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800324 flushIfNeededLocked(event.GetTimestampNs());
Yao Chen5154a372017-10-30 22:57:06 -0700325
Yao Chen6a8c7992017-11-29 20:02:07 +0000326 if (matcherIndex == mStopAllIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800327 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000328 pair.second->noteStopAll(event.GetTimestampNs());
329 }
330 return;
331 }
332
Yangster-mac93694462018-01-22 20:49:31 -0800333 if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800334 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800335 return;
336 }
Yangster-mac93694462018-01-22 20:49:31 -0800337 mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000338 }
Yao Chen5154a372017-10-30 22:57:06 -0700339
Yangster-mac93694462018-01-22 20:49:31 -0800340 auto it = mCurrentSlicedDurationTrackerMap.find(eventKey);
Yao Chen5154a372017-10-30 22:57:06 -0700341
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800342 std::vector<DimensionsValue> values;
343 getDimensionKeys(event, mInternalDimensions, &values);
Yangster-mac20877162017-12-22 17:19:39 -0800344 if (values.empty()) {
345 if (matcherIndex == mStartIndex) {
346 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
347 event.GetTimestampNs(), conditionKeys);
348 } else if (matcherIndex == mStopIndex) {
349 it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetTimestampNs(), false);
350 }
351 } else {
352 for (const DimensionsValue& value : values) {
353 if (matcherIndex == mStartIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800354 it->second->noteStart(
355 HashableDimensionKey(value), condition, event.GetTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800356 } else if (matcherIndex == mStopIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800357 it->second->noteStop(
358 HashableDimensionKey(value), event.GetTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800359 }
360 }
Yao Chen5154a372017-10-30 22:57:06 -0700361 }
Yangster-mac20877162017-12-22 17:19:39 -0800362
Yao Chen729093d2017-10-16 10:33:26 -0700363}
364
Yangsterf2bee6f2017-11-29 12:01:05 -0800365size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800366 size_t totalSize = 0;
367 for (const auto& pair : mPastBuckets) {
368 totalSize += pair.second.size() * kBucketSize;
369 }
370 return totalSize;
yro69007c82017-10-26 20:42:57 -0700371}
372
Yao Chen729093d2017-10-16 10:33:26 -0700373} // namespace statsd
374} // namespace os
375} // namespace android