blob: efbdae15079217b6c818d07fd62dd42c611d467f [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_START_REPORT_NANOS = 2;
48const int FIELD_ID_END_REPORT_NANOS = 3;
49const int FIELD_ID_DURATION_METRICS = 6;
50// for DurationMetricDataWrapper
51const int FIELD_ID_DATA = 1;
52// for DurationMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080053const int FIELD_ID_DIMENSION_IN_WHAT = 1;
54const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
55const int FIELD_ID_BUCKET_INFO = 3;
yro2b0f8862017-11-06 14:27:31 -080056// for DurationBucketInfo
57const int FIELD_ID_START_BUCKET_NANOS = 1;
58const int FIELD_ID_END_BUCKET_NANOS = 2;
59const int FIELD_ID_DURATION = 3;
60
Yao Chenb3561512017-11-21 18:07:17 -080061DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric,
Yao Chen729093d2017-10-16 10:33:26 -070062 const int conditionIndex, const size_t startIndex,
63 const size_t stopIndex, const size_t stopAllIndex,
Yao Chen0ea19902017-11-15 15:44:45 -080064 const bool nesting,
Yao Chen5154a372017-10-30 22:57:06 -070065 const sp<ConditionWizard>& wizard,
Yangster-mac20877162017-12-22 17:19:39 -080066 const FieldMatcher& internalDimensions,
Yao Chen93fe3a32017-11-02 13:52:59 -070067 const uint64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080068 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080069 mAggregationType(metric.aggregation_type()),
Yao Chen729093d2017-10-16 10:33:26 -070070 mStartIndex(startIndex),
71 mStopIndex(stopIndex),
Yao Chen5154a372017-10-30 22:57:06 -070072 mStopAllIndex(stopAllIndex),
Yao Chen0ea19902017-11-15 15:44:45 -080073 mNested(nesting),
Yangster-mac20877162017-12-22 17:19:39 -080074 mInternalDimensions(internalDimensions) {
Yao Chen729093d2017-10-16 10:33:26 -070075 // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract
76 // them in the base class, because the proto generated CountMetric, and DurationMetric are
77 // not related. Maybe we should add a template in the future??
Yangster-macb8144812018-01-04 10:56:23 -080078 if (metric.has_bucket()) {
79 mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000;
Yao Chen729093d2017-10-16 10:33:26 -070080 } else {
81 mBucketSizeNs = LLONG_MAX;
82 }
83
84 // TODO: use UidMap if uid->pkg_name is required
Yangster-mac93694462018-01-22 20:49:31 -080085 mDimensionsInWhat = metric.dimensions_in_what();
86 mDimensionsInCondition = metric.dimensions_in_condition();
Yao Chen729093d2017-10-16 10:33:26 -070087
88 if (metric.links().size() > 0) {
89 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
90 metric.links().end());
Yao Chen729093d2017-10-16 10:33:26 -070091 }
Yangster-mac93694462018-01-22 20:49:31 -080092 mConditionSliced = (metric.links().size() > 0)||
93 (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -070094
Yangster-mac94e197c2018-01-02 16:03:03 -080095 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -070096 (long long)mBucketSizeNs, (long long)mStartTimeNs);
97}
98
99DurationMetricProducer::~DurationMetricProducer() {
100 VLOG("~DurationMetric() called");
101}
102
Bookatz857aaa52017-12-19 15:29:06 -0800103sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) {
104 std::lock_guard<std::mutex> lock(mMutex);
Yangster-maca7fb12d2018-01-03 17:17:20 -0800105 if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) {
106 ALOGW("invalid alert: threshold (%f) > possible recordable value (%d x %lld)",
107 alert.trigger_if_sum_gt(), alert.num_buckets(),
Bookatz450099d2017-11-30 17:09:30 -0800108 (long long)mBucketSizeNs);
109 return nullptr;
110 }
Bookatz857aaa52017-12-19 15:29:06 -0800111 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey);
112 if (anomalyTracker != nullptr) {
113 mAnomalyTrackers.push_back(anomalyTracker);
114 }
115 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800116}
117
Yao Chen5154a372017-10-30 22:57:06 -0700118unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800119 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800120 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800121 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800122 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800123 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
124 mDimensionsInCondition, mNested,
Yao Chend59a6582018-01-08 11:17:11 -0800125 mCurrentBucketStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800126 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800127 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800128 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
129 mDimensionsInCondition, mNested,
Yao Chend59a6582018-01-08 11:17:11 -0800130 mCurrentBucketStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a372017-10-30 22:57:06 -0700131 }
132}
133
Yangsterf2bee6f2017-11-29 12:01:05 -0800134void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800135 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800136 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800137
Yao Chen729093d2017-10-16 10:33:26 -0700138 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac93694462018-01-22 20:49:31 -0800139 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700140 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700141 }
Yangster-mac93694462018-01-22 20:49:31 -0800142
143
144 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
145 ConditionState conditionState = mWizard->getMetConditionDimension(
146 mConditionTrackerIndex, mDimensionsInCondition, &conditionDimensionsKeySet);
147
148 bool condition = (conditionState == ConditionState::kTrue);
149 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
150 conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition());
151 }
152 std::unordered_set<MetricDimensionKey> newKeys;
153 for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) {
154 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
155 auto newKey =
156 MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey);
157 if (newKeys.find(newKey) == newKeys.end()) {
158 mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime);
159 mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey);
160 mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime);
161 }
162 newKeys.insert(newKey);
163 }
164 }
Yao Chen729093d2017-10-16 10:33:26 -0700165}
166
Yangsterf2bee6f2017-11-29 12:01:05 -0800167void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
168 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800169 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700170 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800171 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700172 // TODO: need to populate the condition change time from the event which triggers the condition
173 // change, instead of using current time.
Yangster-mac93694462018-01-22 20:49:31 -0800174 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen5154a372017-10-30 22:57:06 -0700175 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700176 }
177}
178
Yangster-mac20877162017-12-22 17:19:39 -0800179void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
180 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800181 report->set_metric_id(mMetricId);
Yangster-mac20877162017-12-22 17:19:39 -0800182 report->set_start_report_nanos(mStartTimeNs);
183
184 auto duration_metrics = report->mutable_duration_metrics();
185 for (const auto& pair : mPastBuckets) {
186 DurationMetricData* metricData = duration_metrics->add_data();
Yangster-mac93694462018-01-22 20:49:31 -0800187 *metricData->mutable_dimensions_in_what() =
188 pair.first.getDimensionKeyInWhat().getDimensionsValue();
189 *metricData->mutable_dimensions_in_condition() =
190 pair.first.getDimensionKeyInCondition().getDimensionsValue();
Yangster-mac20877162017-12-22 17:19:39 -0800191 for (const auto& bucket : pair.second) {
192 auto bucketInfo = metricData->add_bucket_info();
193 bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs);
194 bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs);
195 bucketInfo->set_duration_nanos(bucket.mDuration);
196 }
197 }
198}
199
Yao Chen288c6002017-12-12 13:43:18 -0800200void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
201 ProtoOutputStream* protoOutput) {
202 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800203 if (mPastBuckets.empty()) {
204 return;
205 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000206
Yangster-mac94e197c2018-01-02 16:03:03 -0800207 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800208 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
209 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
210
Yangster-mac94e197c2018-01-02 16:03:03 -0800211 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000212
Yao Chen729093d2017-10-16 10:33:26 -0700213 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800214 const MetricDimensionKey& dimensionKey = pair.first;
215 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800216
yrob0378b02017-11-09 20:36:25 -0800217 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800218 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800219
Yangster-mac20877162017-12-22 17:19:39 -0800220 // First fill dimension.
221 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800222 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac93694462018-01-22 20:49:31 -0800223 writeDimensionsValueProtoToStream(
224 dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800225 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800226
Yangster-mac93694462018-01-22 20:49:31 -0800227 if (dimensionKey.hasDimensionKeyInCondition()) {
228 long long dimensionInConditionToken = protoOutput->start(
229 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
230 writeDimensionsValueProtoToStream(
231 dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput);
232 protoOutput->end(dimensionInConditionToken);
233 }
234
yro2b0f8862017-11-06 14:27:31 -0800235 // Then fill bucket_info (DurationBucketInfo).
236 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800237 long long bucketInfoToken = protoOutput->start(
238 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
239 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
240 (long long)bucket.mBucketStartNs);
241 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
242 (long long)bucket.mBucketEndNs);
243 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
244 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800245 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
246 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
247 }
248
Yao Chen288c6002017-12-12 13:43:18 -0800249 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700250 }
yro2b0f8862017-11-06 14:27:31 -0800251
Yao Chen288c6002017-12-12 13:43:18 -0800252 protoOutput->end(protoToken);
253 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800254 mPastBuckets.clear();
Yao Chen288c6002017-12-12 13:43:18 -0800255 mStartTimeNs = mCurrentBucketStartTimeNs;
yro2b0f8862017-11-06 14:27:31 -0800256}
Yao Chen729093d2017-10-16 10:33:26 -0700257
Yangsterf2bee6f2017-11-29 12:01:05 -0800258void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTime) {
Yao Chen729093d2017-10-16 10:33:26 -0700259 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) {
260 return;
261 }
Yao Chen5154a372017-10-30 22:57:06 -0700262 VLOG("flushing...........");
Yangster-mac93694462018-01-22 20:49:31 -0800263 for (auto it = mCurrentSlicedDurationTrackerMap.begin();
264 it != mCurrentSlicedDurationTrackerMap.end();) {
Yao Chenf60e0ba2017-11-29 15:06:41 -0800265 if (it->second->flushIfNeeded(eventTime, &mPastBuckets)) {
Yao Chen5154a372017-10-30 22:57:06 -0700266 VLOG("erase bucket for key %s", it->first.c_str());
Yangster-mac93694462018-01-22 20:49:31 -0800267 it = mCurrentSlicedDurationTrackerMap.erase(it);
Yao Chend41c4222017-11-15 19:26:14 -0800268 } else {
269 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700270 }
271 }
Yao Chen5154a372017-10-30 22:57:06 -0700272
273 int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs;
274 mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800275 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700276}
277
Yao Chen884c8c12018-01-26 10:36:25 -0800278void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800279 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800280 return;
281 }
282
283 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800284 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800285 if (verbose) {
Yangster-mac93694462018-01-22 20:49:31 -0800286 for (const auto& slice : mCurrentSlicedDurationTrackerMap) {
Yao Chen884c8c12018-01-26 10:36:25 -0800287 fprintf(out, "\t%s\n", slice.first.c_str());
288 slice.second->dumpStates(out, verbose);
289 }
290 }
291}
292
Yangster-mac93694462018-01-22 20:49:31 -0800293bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800294 // the key is not new, we are good.
Yangster-mac93694462018-01-22 20:49:31 -0800295 if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) {
Yao Chenb3561512017-11-21 18:07:17 -0800296 return false;
297 }
298 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800299 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
300 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800301 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800302 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
303 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800304 ALOGE("DurationMetric %lld dropping data for dimension key %s",
305 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800306 return true;
307 }
308 }
309 return false;
310}
311
Yangsterf2bee6f2017-11-29 12:01:05 -0800312void DurationMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800313 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800314 const ConditionKey& conditionKeys, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800315 const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800316 flushIfNeededLocked(event.GetTimestampNs());
Yao Chen5154a372017-10-30 22:57:06 -0700317
Yao Chen6a8c7992017-11-29 20:02:07 +0000318 if (matcherIndex == mStopAllIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800319 for (auto& pair : mCurrentSlicedDurationTrackerMap) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000320 pair.second->noteStopAll(event.GetTimestampNs());
321 }
322 return;
323 }
324
Yangster-mac93694462018-01-22 20:49:31 -0800325 if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800326 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800327 return;
328 }
Yangster-mac93694462018-01-22 20:49:31 -0800329 mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000330 }
Yao Chen5154a372017-10-30 22:57:06 -0700331
Yangster-mac93694462018-01-22 20:49:31 -0800332 auto it = mCurrentSlicedDurationTrackerMap.find(eventKey);
Yao Chen5154a372017-10-30 22:57:06 -0700333
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800334 std::vector<DimensionsValue> values;
335 getDimensionKeys(event, mInternalDimensions, &values);
Yangster-mac20877162017-12-22 17:19:39 -0800336 if (values.empty()) {
337 if (matcherIndex == mStartIndex) {
338 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
339 event.GetTimestampNs(), conditionKeys);
340 } else if (matcherIndex == mStopIndex) {
341 it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetTimestampNs(), false);
342 }
343 } else {
344 for (const DimensionsValue& value : values) {
345 if (matcherIndex == mStartIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800346 it->second->noteStart(
347 HashableDimensionKey(value), condition, event.GetTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800348 } else if (matcherIndex == mStopIndex) {
Yangster-mac93694462018-01-22 20:49:31 -0800349 it->second->noteStop(
350 HashableDimensionKey(value), event.GetTimestampNs(), false);
Yangster-mac20877162017-12-22 17:19:39 -0800351 }
352 }
Yao Chen5154a372017-10-30 22:57:06 -0700353 }
Yangster-mac20877162017-12-22 17:19:39 -0800354
Yao Chen729093d2017-10-16 10:33:26 -0700355}
356
Yangsterf2bee6f2017-11-29 12:01:05 -0800357size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800358 size_t totalSize = 0;
359 for (const auto& pair : mPastBuckets) {
360 totalSize += pair.second.size() * kBucketSize;
361 }
362 return totalSize;
yro69007c82017-10-26 20:42:57 -0700363}
364
Yao Chen729093d2017-10-16 10:33:26 -0700365} // namespace statsd
366} // namespace os
367} // namespace android