blob: da6b97cc4e590c703226624ca0e7cd5f2c65cf1d [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;
Yangster-mac9def8e32018-04-17 13:55:51 -070047const int FIELD_ID_TIME_BASE = 9;
48const int FIELD_ID_BUCKET_SIZE = 10;
49const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11;
50const int FIELD_ID_DIMENSION_PATH_IN_CONDITION = 12;
Howard Ro9440e092018-12-16 19:15:21 -080051const int FIELD_ID_IS_ACTIVE = 14;
yro2b0f8862017-11-06 14:27:31 -080052// for DurationMetricDataWrapper
53const int FIELD_ID_DATA = 1;
54// for DurationMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080055const int FIELD_ID_DIMENSION_IN_WHAT = 1;
56const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
57const int FIELD_ID_BUCKET_INFO = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070058const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
59const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5;
yro2b0f8862017-11-06 14:27:31 -080060// for DurationBucketInfo
yro2b0f8862017-11-06 14:27:31 -080061const int FIELD_ID_DURATION = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070062const int FIELD_ID_BUCKET_NUM = 4;
63const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
64const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
yro2b0f8862017-11-06 14:27:31 -080065
Yao Chenb3561512017-11-21 18:07:17 -080066DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric,
Yao Chen729093d2017-10-16 10:33:26 -070067 const int conditionIndex, const size_t startIndex,
68 const size_t stopIndex, const size_t stopAllIndex,
Yao Chen0ea19902017-11-15 15:44:45 -080069 const bool nesting,
Yao Chen5154a372017-10-30 22:57:06 -070070 const sp<ConditionWizard>& wizard,
Yangster-mac20877162017-12-22 17:19:39 -080071 const FieldMatcher& internalDimensions,
Chenjie Yue1361ed2018-07-23 17:33:09 -070072 const int64_t timeBaseNs, const int64_t startTimeNs)
73 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080074 mAggregationType(metric.aggregation_type()),
Yao Chen729093d2017-10-16 10:33:26 -070075 mStartIndex(startIndex),
76 mStopIndex(stopIndex),
Yao Chen5154a372017-10-30 22:57:06 -070077 mStopAllIndex(stopAllIndex),
Yangster13fb7e42018-03-07 17:30:49 -080078 mNested(nesting),
79 mContainANYPositionInInternalDimensions(false) {
Yangster-macb8144812018-01-04 10:56:23 -080080 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080081 mBucketSizeNs =
82 TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000;
Yao Chen729093d2017-10-16 10:33:26 -070083 } else {
84 mBucketSizeNs = LLONG_MAX;
85 }
86
Yao Chen8a8d16c2018-02-08 14:50:40 -080087 if (metric.has_dimensions_in_what()) {
88 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -080089 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -080090 }
91
92 if (internalDimensions.has_field()) {
93 translateFieldMatcher(internalDimensions, &mInternalDimensions);
Yangster13fb7e42018-03-07 17:30:49 -080094 mContainANYPositionInInternalDimensions = HasPositionANY(internalDimensions);
Yao Chen8a8d16c2018-02-08 14:50:40 -080095 }
Yangster-mace06cfd72018-03-10 23:22:59 -080096 if (mContainANYPositionInInternalDimensions) {
97 ALOGE("Position ANY in internal dimension not supported.");
98 }
99 if (mContainANYPositionInDimensionsInWhat) {
100 ALOGE("Position ANY in dimension_in_what not supported.");
101 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800102
103 if (metric.has_dimensions_in_condition()) {
104 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
105 }
Yao Chen729093d2017-10-16 10:33:26 -0700106
Yangster-mac9def8e32018-04-17 13:55:51 -0700107 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
108 HasPositionALL(metric.dimensions_in_condition());
109
Yao Chen729093d2017-10-16 10:33:26 -0700110 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800111 for (const auto& link : metric.links()) {
112 Metric2Condition mc;
113 mc.conditionId = link.condition();
114 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
115 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
116 mMetric2ConditionLinks.push_back(mc);
117 }
Yao Chen729093d2017-10-16 10:33:26 -0700118 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800119 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yangster13fb7e42018-03-07 17:30:49 -0800120 mUnSlicedPartCondition = ConditionState::kUnknown;
Yao Chen729093d2017-10-16 10:33:26 -0700121
Yangster13fb7e42018-03-07 17:30:49 -0800122 mUseWhatDimensionAsInternalDimension = equalDimensions(mDimensionsInWhat, mInternalDimensions);
123 if (mWizard != nullptr && mConditionTrackerIndex >= 0) {
124 mSameConditionDimensionsInTracker =
125 mWizard->equalOutputDimensions(mConditionTrackerIndex, mDimensionsInCondition);
126 if (mMetric2ConditionLinks.size() == 1) {
127 mHasLinksToAllConditionDimensionsInTracker =
128 mWizard->equalOutputDimensions(mConditionTrackerIndex,
129 mMetric2ConditionLinks.begin()->conditionFields);
Yangster-mac53928882018-02-25 23:02:56 -0800130 }
Yangster-mac53928882018-02-25 23:02:56 -0800131 }
Chenjie Yue1361ed2018-07-23 17:33:09 -0700132 flushIfNeededLocked(startTimeNs);
133 // Adjust start for partial bucket
134 mCurrentBucketStartTimeNs = startTimeNs;
Yangster-mac94e197c2018-01-02 16:03:03 -0800135 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700136 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Yao Chen729093d2017-10-16 10:33:26 -0700137}
138
139DurationMetricProducer::~DurationMetricProducer() {
140 VLOG("~DurationMetric() called");
141}
142
Yangster-mac932ecec2018-02-01 10:23:52 -0800143sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(
144 const Alert &alert, const sp<AlarmMonitor>& anomalyAlarmMonitor) {
Bookatz857aaa52017-12-19 15:29:06 -0800145 std::lock_guard<std::mutex> lock(mMutex);
Bookatz423f7532018-03-08 15:45:14 -0800146 if (mAggregationType == DurationMetric_AggregationType_SUM) {
147 if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) {
148 ALOGW("invalid alert for SUM: threshold (%f) > possible recordable value (%d x %lld)",
149 alert.trigger_if_sum_gt(), alert.num_buckets(), (long long)mBucketSizeNs);
150 return nullptr;
151 }
152 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800153 sp<DurationAnomalyTracker> anomalyTracker =
154 new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor);
Bookatz857aaa52017-12-19 15:29:06 -0800155 if (anomalyTracker != nullptr) {
156 mAnomalyTrackers.push_back(anomalyTracker);
157 }
158 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800159}
160
Yao Chen5154a372017-10-30 22:57:06 -0700161unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800162 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800163 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800164 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800165 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800166 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800167 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700168 mTimeBaseNs, mBucketSizeNs, mConditionSliced,
Yangster13fb7e42018-03-07 17:30:49 -0800169 mHasLinksToAllConditionDimensionsInTracker, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800170 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800171 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800172 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800173 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700174 mTimeBaseNs, mBucketSizeNs, mConditionSliced,
Yangster13fb7e42018-03-07 17:30:49 -0800175 mHasLinksToAllConditionDimensionsInTracker, mAnomalyTrackers);
176 }
177}
178
179// SlicedConditionChange optimization case 1:
180// 1. If combination condition, logical operation is AND, only one sliced child predicate.
181// 2. No condition in dimension
182// 3. The links covers all dimension fields in the sliced child condition predicate.
Yao Chen427d3722018-03-22 15:21:52 -0700183void DurationMetricProducer::onSlicedConditionMayChangeLocked_opt1(bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700184 const int64_t eventTime) {
Yangster13fb7e42018-03-07 17:30:49 -0800185 if (mMetric2ConditionLinks.size() != 1 ||
186 !mHasLinksToAllConditionDimensionsInTracker ||
187 !mDimensionsInCondition.empty()) {
188 return;
189 }
190
191 bool currentUnSlicedPartCondition = true;
192 if (!mWizard->IsSimpleCondition(mConditionTrackerIndex)) {
193 ConditionState unslicedPartState =
194 mWizard->getUnSlicedPartConditionState(mConditionTrackerIndex);
195 // When the unsliced part is still false, return directly.
196 if (mUnSlicedPartCondition == ConditionState::kFalse &&
197 unslicedPartState == ConditionState::kFalse) {
198 return;
199 }
200 mUnSlicedPartCondition = unslicedPartState;
201 currentUnSlicedPartCondition = mUnSlicedPartCondition > 0;
202 }
203
204 auto dimensionsChangedToTrue = mWizard->getChangedToTrueDimensions(mConditionTrackerIndex);
205 auto dimensionsChangedToFalse = mWizard->getChangedToFalseDimensions(mConditionTrackerIndex);
206
207 // The condition change is from the unsliced predicates.
208 // We need to find out the true dimensions from the sliced predicate and flip their condition
209 // state based on the new unsliced condition state.
210 if (dimensionsChangedToTrue == nullptr || dimensionsChangedToFalse == nullptr ||
211 (dimensionsChangedToTrue->empty() && dimensionsChangedToFalse->empty())) {
212 std::set<HashableDimensionKey> trueConditionDimensions;
213 mWizard->getTrueSlicedDimensions(mConditionTrackerIndex, &trueConditionDimensions);
214 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
215 HashableDimensionKey linkedConditionDimensionKey;
216 getDimensionForCondition(whatIt.first.getValues(),
217 mMetric2ConditionLinks[0],
218 &linkedConditionDimensionKey);
219 if (trueConditionDimensions.find(linkedConditionDimensionKey) !=
220 trueConditionDimensions.end()) {
221 for (auto& condIt : whatIt.second) {
222 condIt.second->onConditionChanged(
223 currentUnSlicedPartCondition, eventTime);
224 }
225 }
226 }
227 } else {
228 // Handle the condition change from the sliced predicate.
229 if (currentUnSlicedPartCondition) {
230 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
231 HashableDimensionKey linkedConditionDimensionKey;
232 getDimensionForCondition(whatIt.first.getValues(),
233 mMetric2ConditionLinks[0],
234 &linkedConditionDimensionKey);
235 if (dimensionsChangedToTrue->find(linkedConditionDimensionKey) !=
236 dimensionsChangedToTrue->end()) {
237 for (auto& condIt : whatIt.second) {
238 condIt.second->onConditionChanged(true, eventTime);
239 }
240 }
241 if (dimensionsChangedToFalse->find(linkedConditionDimensionKey) !=
242 dimensionsChangedToFalse->end()) {
243 for (auto& condIt : whatIt.second) {
244 condIt.second->onConditionChanged(false, eventTime);
245 }
246 }
247 }
248 }
249 }
250}
251
252
253// SlicedConditionChange optimization case 2:
254// 1. If combination condition, logical operation is AND, only one sliced child predicate.
255// 2. Has dimensions_in_condition and it equals to the output dimensions of the sliced predicate.
Yao Chen427d3722018-03-22 15:21:52 -0700256void DurationMetricProducer::onSlicedConditionMayChangeLocked_opt2(bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700257 const int64_t eventTime) {
Yangster13fb7e42018-03-07 17:30:49 -0800258 if (mMetric2ConditionLinks.size() > 1 || !mSameConditionDimensionsInTracker) {
259 return;
260 }
261
262 auto dimensionsChangedToTrue = mWizard->getChangedToTrueDimensions(mConditionTrackerIndex);
263 auto dimensionsChangedToFalse = mWizard->getChangedToFalseDimensions(mConditionTrackerIndex);
264
265 bool currentUnSlicedPartCondition = true;
266 if (!mWizard->IsSimpleCondition(mConditionTrackerIndex)) {
267 ConditionState unslicedPartState =
268 mWizard->getUnSlicedPartConditionState(mConditionTrackerIndex);
269 // When the unsliced part is still false, return directly.
270 if (mUnSlicedPartCondition == ConditionState::kFalse &&
271 unslicedPartState == ConditionState::kFalse) {
272 return;
273 }
274 mUnSlicedPartCondition = unslicedPartState;
275 currentUnSlicedPartCondition = mUnSlicedPartCondition > 0;
276 }
277
278 const std::set<HashableDimensionKey>* trueDimensionsToProcess = nullptr;
279 const std::set<HashableDimensionKey>* falseDimensionsToProcess = nullptr;
280
281 std::set<HashableDimensionKey> currentTrueConditionDimensions;
282 if (dimensionsChangedToTrue == nullptr || dimensionsChangedToFalse == nullptr ||
283 (dimensionsChangedToTrue->empty() && dimensionsChangedToFalse->empty())) {
284 mWizard->getTrueSlicedDimensions(mConditionTrackerIndex, &currentTrueConditionDimensions);
285 trueDimensionsToProcess = &currentTrueConditionDimensions;
286 } else if (currentUnSlicedPartCondition) {
287 // Handles the condition change from the sliced predicate. If the unsliced condition state
288 // is not true, not need to do anything.
289 trueDimensionsToProcess = dimensionsChangedToTrue;
290 falseDimensionsToProcess = dimensionsChangedToFalse;
291 }
292
293 if (trueDimensionsToProcess == nullptr && falseDimensionsToProcess == nullptr) {
294 return;
295 }
296
297 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
298 if (falseDimensionsToProcess != nullptr) {
299 for (const auto& changedDim : *falseDimensionsToProcess) {
300 auto condIt = whatIt.second.find(changedDim);
301 if (condIt != whatIt.second.end()) {
302 condIt->second->onConditionChanged(false, eventTime);
303 }
304 }
305 }
306 if (trueDimensionsToProcess != nullptr) {
307 HashableDimensionKey linkedConditionDimensionKey;
308 if (!trueDimensionsToProcess->empty() && mMetric2ConditionLinks.size() == 1) {
309 getDimensionForCondition(whatIt.first.getValues(),
310 mMetric2ConditionLinks[0],
311 &linkedConditionDimensionKey);
312 }
313 for (auto& trueDim : *trueDimensionsToProcess) {
314 auto condIt = whatIt.second.find(trueDim);
315 if (condIt != whatIt.second.end()) {
316 condIt->second->onConditionChanged(
317 currentUnSlicedPartCondition, eventTime);
318 } else {
319 if (mMetric2ConditionLinks.size() == 0 ||
320 trueDim.contains(linkedConditionDimensionKey)) {
321 if (!whatIt.second.empty()) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700322 auto newEventKey = MetricDimensionKey(whatIt.first, trueDim);
323 if (hitGuardRailLocked(newEventKey)) {
324 continue;
325 }
Yangster13fb7e42018-03-07 17:30:49 -0800326 unique_ptr<DurationTracker> newTracker =
327 whatIt.second.begin()->second->clone(eventTime);
328 if (newTracker != nullptr) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700329 newTracker->setEventKey(newEventKey);
Yangster13fb7e42018-03-07 17:30:49 -0800330 newTracker->onConditionChanged(true, eventTime);
331 whatIt.second[trueDim] = std::move(newTracker);
332 }
333 }
334 }
335 }
336 }
337 }
Yao Chen5154a372017-10-30 22:57:06 -0700338 }
339}
340
Yao Chen427d3722018-03-22 15:21:52 -0700341void DurationMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700342 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800343 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800344 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800345
Yangster13fb7e42018-03-07 17:30:49 -0800346 if (!mConditionSliced) {
347 return;
348 }
349
350 bool changeDimTrackable = mWizard->IsChangedDimensionTrackable(mConditionTrackerIndex);
351 if (changeDimTrackable && mHasLinksToAllConditionDimensionsInTracker &&
352 mDimensionsInCondition.empty()) {
Yao Chen427d3722018-03-22 15:21:52 -0700353 onSlicedConditionMayChangeLocked_opt1(overallCondition, eventTime);
Yangster13fb7e42018-03-07 17:30:49 -0800354 return;
355 }
356
357 if (changeDimTrackable && mSameConditionDimensionsInTracker &&
358 mMetric2ConditionLinks.size() <= 1) {
Yao Chen427d3722018-03-22 15:21:52 -0700359 onSlicedConditionMayChangeLocked_opt2(overallCondition, eventTime);
Yangster13fb7e42018-03-07 17:30:49 -0800360 return;
361 }
362
Yao Chen729093d2017-10-16 10:33:26 -0700363 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac53928882018-02-25 23:02:56 -0800364 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
365 for (auto& pair : whatIt.second) {
Yao Chen427d3722018-03-22 15:21:52 -0700366 pair.second->onSlicedConditionMayChange(overallCondition, eventTime);
Yangster-mac53928882018-02-25 23:02:56 -0800367 }
Yao Chen729093d2017-10-16 10:33:26 -0700368 }
Yangster-mac93694462018-01-22 20:49:31 -0800369
Yangster-mac53928882018-02-25 23:02:56 -0800370 if (mDimensionsInCondition.empty()) {
371 return;
Yangster-mac93694462018-01-22 20:49:31 -0800372 }
Yangster-mac53928882018-02-25 23:02:56 -0800373
374 if (mMetric2ConditionLinks.empty()) {
375 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
376 mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition,
Yangster13fb7e42018-03-07 17:30:49 -0800377 !mSameConditionDimensionsInTracker,
Yangster-mac53928882018-02-25 23:02:56 -0800378 &conditionDimensionsKeySet);
379 for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) {
380 for (const auto& pair : whatIt.second) {
381 conditionDimensionsKeySet.erase(pair.first);
Yangster-mac93694462018-01-22 20:49:31 -0800382 }
Yangster-mac53928882018-02-25 23:02:56 -0800383 }
384 for (const auto& conditionDimension : conditionDimensionsKeySet) {
385 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
386 if (!whatIt.second.empty()) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700387 auto newEventKey = MetricDimensionKey(whatIt.first, conditionDimension);
388 if (hitGuardRailLocked(newEventKey)) {
389 continue;
390 }
Yangster-mac53928882018-02-25 23:02:56 -0800391 unique_ptr<DurationTracker> newTracker =
392 whatIt.second.begin()->second->clone(eventTime);
Yangster13fb7e42018-03-07 17:30:49 -0800393 if (newTracker != nullptr) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700394 newTracker->setEventKey(MetricDimensionKey(newEventKey));
Yao Chen427d3722018-03-22 15:21:52 -0700395 newTracker->onSlicedConditionMayChange(overallCondition, eventTime);
Yangster13fb7e42018-03-07 17:30:49 -0800396 whatIt.second[conditionDimension] = std::move(newTracker);
397 }
Yangster-mac53928882018-02-25 23:02:56 -0800398 }
399 }
400 }
401 } else {
402 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
403 ConditionKey conditionKey;
404 for (const auto& link : mMetric2ConditionLinks) {
405 getDimensionForCondition(whatIt.first.getValues(), link,
406 &conditionKey[link.conditionId]);
407 }
408 std::unordered_set<HashableDimensionKey> conditionDimensionsKeys;
409 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
Yangster13fb7e42018-03-07 17:30:49 -0800410 !mSameConditionDimensionsInTracker,
411 !mHasLinksToAllConditionDimensionsInTracker,
Yangster-mac53928882018-02-25 23:02:56 -0800412 &conditionDimensionsKeys);
413
414 for (const auto& conditionDimension : conditionDimensionsKeys) {
415 if (!whatIt.second.empty() &&
416 whatIt.second.find(conditionDimension) == whatIt.second.end()) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700417 auto newEventKey = MetricDimensionKey(whatIt.first, conditionDimension);
418 if (hitGuardRailLocked(newEventKey)) {
419 continue;
420 }
Yangster-mac53928882018-02-25 23:02:56 -0800421 auto newTracker = whatIt.second.begin()->second->clone(eventTime);
Yangster13fb7e42018-03-07 17:30:49 -0800422 if (newTracker != nullptr) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700423 newTracker->setEventKey(newEventKey);
Yao Chen427d3722018-03-22 15:21:52 -0700424 newTracker->onSlicedConditionMayChange(overallCondition, eventTime);
Yangster13fb7e42018-03-07 17:30:49 -0800425 whatIt.second[conditionDimension] = std::move(newTracker);
426 }
Yangster-mac53928882018-02-25 23:02:56 -0800427 }
428 }
Yangster-mac93694462018-01-22 20:49:31 -0800429 }
430 }
Yao Chen729093d2017-10-16 10:33:26 -0700431}
432
Yangsterf2bee6f2017-11-29 12:01:05 -0800433void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
Yangster-macb142cc82018-03-30 15:22:08 -0700434 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800435 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000436 mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse;
Yangsterf2bee6f2017-11-29 12:01:05 -0800437 flushIfNeededLocked(eventTime);
Yangster-mac53928882018-02-25 23:02:56 -0800438 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
439 for (auto& pair : whatIt.second) {
440 pair.second->onConditionChanged(conditionMet, eventTime);
441 }
Yao Chen729093d2017-10-16 10:33:26 -0700442 }
443}
444
Yangster-macb142cc82018-03-30 15:22:08 -0700445void DurationMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800446 flushIfNeededLocked(dropTimeNs);
Olivier Gaillard320952b2019-02-06 13:57:24 +0000447 StatsdStats::getInstance().noteBucketDropped(mMetricId);
Yao Chen06dba5d2018-01-26 13:38:16 -0800448 mPastBuckets.clear();
449}
450
Yangster-maca802d732018-04-24 07:50:38 -0700451void DurationMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
452 flushIfNeededLocked(dumpTimeNs);
453 mPastBuckets.clear();
454}
455
Yangster-macb142cc82018-03-30 15:22:08 -0700456void DurationMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700457 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700458 const bool erase_data,
Yangster-mac9def8e32018-04-17 13:55:51 -0700459 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800460 ProtoOutputStream* protoOutput) {
Yangster-mace68f3a52018-04-04 00:01:43 -0700461 if (include_current_partial_bucket) {
462 flushLocked(dumpTimeNs);
463 } else {
464 flushIfNeededLocked(dumpTimeNs);
465 }
Yang Lub4722912018-11-15 11:02:03 -0800466 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800467 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800468
Yangster-mac635b4b32018-01-23 20:17:35 -0800469 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800470 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800471 return;
472 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000473
Yangster-mac9def8e32018-04-17 13:55:51 -0700474 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
475 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
476
477 if (!mSliceByPositionALL) {
478 if (!mDimensionsInWhat.empty()) {
479 uint64_t dimenPathToken = protoOutput->start(
480 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
481 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
482 protoOutput->end(dimenPathToken);
483 }
484 if (!mDimensionsInCondition.empty()) {
485 uint64_t dimenPathToken = protoOutput->start(
486 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
487 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
488 protoOutput->end(dimenPathToken);
489 }
490 }
491
Yi Jin5ee07872018-03-05 18:18:27 -0800492 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
Yao Chen288c6002017-12-12 13:43:18 -0800493
Yao Chen8a8d16c2018-02-08 14:50:40 -0800494 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000495
Yao Chen729093d2017-10-16 10:33:26 -0700496 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800497 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800498 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800499
Yi Jin5ee07872018-03-05 18:18:27 -0800500 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800501 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800502
Yangster-mac20877162017-12-22 17:19:39 -0800503 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700504 if (mSliceByPositionALL) {
505 uint64_t dimensionToken = protoOutput->start(
506 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
507 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
508 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800509
Yangster-mac9def8e32018-04-17 13:55:51 -0700510 if (dimensionKey.hasDimensionKeyInCondition()) {
511 uint64_t dimensionInConditionToken = protoOutput->start(
512 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
513 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(),
514 str_set, protoOutput);
515 protoOutput->end(dimensionInConditionToken);
516 }
517 } else {
518 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
519 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
520 if (dimensionKey.hasDimensionKeyInCondition()) {
521 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
522 FIELD_ID_DIMENSION_LEAF_IN_CONDITION,
523 str_set, protoOutput);
524 }
Yangster-mac93694462018-01-22 20:49:31 -0800525 }
yro2b0f8862017-11-06 14:27:31 -0800526 // Then fill bucket_info (DurationBucketInfo).
527 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800528 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800529 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700530 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
531 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
532 (long long)NanoToMillis(bucket.mBucketStartNs));
533 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
534 (long long)NanoToMillis(bucket.mBucketEndNs));
535 } else {
536 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
537 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
538 }
Yao Chen288c6002017-12-12 13:43:18 -0800539 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
540 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800541 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
542 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
543 }
544
Yao Chen288c6002017-12-12 13:43:18 -0800545 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700546 }
yro2b0f8862017-11-06 14:27:31 -0800547
Yao Chen288c6002017-12-12 13:43:18 -0800548 protoOutput->end(protoToken);
Bookatzff71cad2018-09-20 17:17:49 -0700549 if (erase_data) {
550 mPastBuckets.clear();
551 }
yro2b0f8862017-11-06 14:27:31 -0800552}
Yao Chen729093d2017-10-16 10:33:26 -0700553
Yangster-macb142cc82018-03-30 15:22:08 -0700554void DurationMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
555 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800556
557 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700558 return;
559 }
Yao Chen5154a372017-10-30 22:57:06 -0700560 VLOG("flushing...........");
Yangster-mac53928882018-02-25 23:02:56 -0800561 for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin();
562 whatIt != mCurrentSlicedDurationTrackerMap.end();) {
563 for (auto it = whatIt->second.begin(); it != whatIt->second.end();) {
564 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
Yangster13fb7e42018-03-07 17:30:49 -0800565 VLOG("erase bucket for key %s %s",
566 whatIt->first.toString().c_str(), it->first.toString().c_str());
Yangster-mac53928882018-02-25 23:02:56 -0800567 it = whatIt->second.erase(it);
568 } else {
569 ++it;
570 }
571 }
572 if (whatIt->second.empty()) {
573 whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt);
Yao Chend41c4222017-11-15 19:26:14 -0800574 } else {
Yangster-mac53928882018-02-25 23:02:56 -0800575 whatIt++;
Yao Chen729093d2017-10-16 10:33:26 -0700576 }
577 }
Yao Chen5154a372017-10-30 22:57:06 -0700578
David Chen27785a82018-01-19 17:06:45 -0800579 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
580 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800581 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700582}
583
Yangster-macb142cc82018-03-30 15:22:08 -0700584void DurationMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) {
Yangster-mac53928882018-02-25 23:02:56 -0800585 for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin();
586 whatIt != mCurrentSlicedDurationTrackerMap.end();) {
587 for (auto it = whatIt->second.begin(); it != whatIt->second.end();) {
588 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
Yangster13fb7e42018-03-07 17:30:49 -0800589 VLOG("erase bucket for key %s %s", whatIt->first.toString().c_str(),
590 it->first.toString().c_str());
Yangster-mac53928882018-02-25 23:02:56 -0800591 it = whatIt->second.erase(it);
592 } else {
593 ++it;
594 }
595 }
596 if (whatIt->second.empty()) {
597 whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt);
David Chen27785a82018-01-19 17:06:45 -0800598 } else {
Yangster-mac53928882018-02-25 23:02:56 -0800599 whatIt++;
David Chen27785a82018-01-19 17:06:45 -0800600 }
601 }
602}
603
Yao Chen884c8c12018-01-26 10:36:25 -0800604void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800605 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800606 return;
607 }
608
609 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800610 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800611 if (verbose) {
Yangster-mac53928882018-02-25 23:02:56 -0800612 for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) {
613 for (const auto& slice : whatIt.second) {
Yangster13fb7e42018-03-07 17:30:49 -0800614 fprintf(out, "\t(what)%s\t(condition)%s\n", whatIt.first.toString().c_str(),
615 slice.first.toString().c_str());
Yangster-mac53928882018-02-25 23:02:56 -0800616 slice.second->dumpStates(out, verbose);
617 }
Yao Chen884c8c12018-01-26 10:36:25 -0800618 }
619 }
620}
621
Yangster-mac93694462018-01-22 20:49:31 -0800622bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yangster-mac306ccc22018-03-24 15:03:40 -0700623 auto whatIt = mCurrentSlicedDurationTrackerMap.find(newKey.getDimensionKeyInWhat());
624 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
625 auto condIt = whatIt->second.find(newKey.getDimensionKeyInCondition());
626 if (condIt != whatIt->second.end()) {
627 return false;
628 }
629 if (whatIt->second.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
630 size_t newTupleCount = whatIt->second.size() + 1;
631 StatsdStats::getInstance().noteMetricDimensionInConditionSize(
632 mConfigKey, mMetricId, newTupleCount);
633 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
634 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
635 ALOGE("DurationMetric %lld dropping data for condition dimension key %s",
636 (long long)mMetricId, newKey.getDimensionKeyInCondition().toString().c_str());
637 return true;
638 }
639 }
640 } else {
641 // 1. Report the tuple count if the tuple count > soft limit
642 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
643 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
644 StatsdStats::getInstance().noteMetricDimensionSize(
645 mConfigKey, mMetricId, newTupleCount);
646 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
647 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
648 ALOGE("DurationMetric %lld dropping data for what dimension key %s",
649 (long long)mMetricId, newKey.getDimensionKeyInWhat().toString().c_str());
650 return true;
651 }
Yao Chenb3561512017-11-21 18:07:17 -0800652 }
653 }
654 return false;
655}
656
Yangster-mac53928882018-02-25 23:02:56 -0800657void DurationMetricProducer::handleStartEvent(const MetricDimensionKey& eventKey,
658 const ConditionKey& conditionKeys,
659 bool condition, const LogEvent& event) {
660 const auto& whatKey = eventKey.getDimensionKeyInWhat();
661 const auto& condKey = eventKey.getDimensionKeyInCondition();
Yao Chen5154a372017-10-30 22:57:06 -0700662
Yangster-mac53928882018-02-25 23:02:56 -0800663 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey);
664 if (whatIt == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800665 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800666 return;
667 }
Yangster-mac53928882018-02-25 23:02:56 -0800668 mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey);
669 } else {
670 if (whatIt->second.find(condKey) == whatIt->second.end()) {
671 if (hitGuardRailLocked(eventKey)) {
672 return;
673 }
674 mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey);
675 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000676 }
Yao Chen5154a372017-10-30 22:57:06 -0700677
Yangster-mac53928882018-02-25 23:02:56 -0800678 auto it = mCurrentSlicedDurationTrackerMap.find(whatKey)->second.find(condKey);
679 if (mUseWhatDimensionAsInternalDimension) {
680 it->second->noteStart(whatKey, condition,
681 event.GetElapsedTimestampNs(), conditionKeys);
682 return;
683 }
Yao Chen5154a372017-10-30 22:57:06 -0700684
Yangster13fb7e42018-03-07 17:30:49 -0800685 if (mInternalDimensions.empty()) {
Yangster-mac53928882018-02-25 23:02:56 -0800686 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
687 event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800688 } else {
Yangster-mace06cfd72018-03-10 23:22:59 -0800689 HashableDimensionKey dimensionKey = DEFAULT_DIMENSION_KEY;
690 filterValues(mInternalDimensions, event.getValues(), &dimensionKey);
691 it->second->noteStart(
692 dimensionKey, condition, event.GetElapsedTimestampNs(), conditionKeys);
Yao Chen5154a372017-10-30 22:57:06 -0700693 }
Yangster-mac20877162017-12-22 17:19:39 -0800694
Yao Chen729093d2017-10-16 10:33:26 -0700695}
696
Yangster-mac53928882018-02-25 23:02:56 -0800697void DurationMetricProducer::onMatchedLogEventInternalLocked(
698 const size_t matcherIndex, const MetricDimensionKey& eventKey,
699 const ConditionKey& conditionKeys, bool condition,
700 const LogEvent& event) {
701 ALOGW("Not used in duration tracker.");
702}
703
Yangster-mace06cfd72018-03-10 23:22:59 -0800704void DurationMetricProducer::onMatchedLogEventLocked(const size_t matcherIndex,
705 const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700706 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700707 if (eventTimeNs < mTimeBaseNs) {
Yangster13fb7e42018-03-07 17:30:49 -0800708 return;
709 }
710
711 flushIfNeededLocked(event.GetElapsedTimestampNs());
712
713 // Handles Stopall events.
714 if (matcherIndex == mStopAllIndex) {
715 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
716 for (auto& pair : whatIt.second) {
717 pair.second->noteStopAll(event.GetElapsedTimestampNs());
718 }
719 }
720 return;
721 }
722
723 HashableDimensionKey dimensionInWhat;
724 if (!mDimensionsInWhat.empty()) {
725 filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhat);
726 } else {
727 dimensionInWhat = DEFAULT_DIMENSION_KEY;
728 }
729
730 // Handles Stop events.
731 if (matcherIndex == mStopIndex) {
732 if (mUseWhatDimensionAsInternalDimension) {
733 auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat);
734 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
735 for (const auto& condIt : whatIt->second) {
736 condIt.second->noteStop(dimensionInWhat, event.GetElapsedTimestampNs(), false);
737 }
738 }
739 return;
740 }
741
742 HashableDimensionKey internalDimensionKey = DEFAULT_DIMENSION_KEY;
743 if (!mInternalDimensions.empty()) {
744 filterValues(mInternalDimensions, event.getValues(), &internalDimensionKey);
745 }
746
747 auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat);
748 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
749 for (const auto& condIt : whatIt->second) {
750 condIt.second->noteStop(
751 internalDimensionKey, event.GetElapsedTimestampNs(), false);
752 }
753 }
754 return;
755 }
756
757 bool condition;
758 ConditionKey conditionKey;
759 std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
760 if (mConditionSliced) {
761 for (const auto& link : mMetric2ConditionLinks) {
762 getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]);
763 }
764
765 auto conditionState =
766 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
767 !mSameConditionDimensionsInTracker,
768 !mHasLinksToAllConditionDimensionsInTracker,
769 &dimensionKeysInCondition);
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000770 condition = conditionState == ConditionState::kTrue;
Yangster13fb7e42018-03-07 17:30:49 -0800771 if (mDimensionsInCondition.empty() && condition) {
772 dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
773 }
774 } else {
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000775 // TODO: The unknown condition state is not handled here, we should fix it.
776 condition = mCondition == ConditionState::kTrue;
Yangster13fb7e42018-03-07 17:30:49 -0800777 if (condition) {
778 dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
779 }
780 }
781
782 if (dimensionKeysInCondition.empty()) {
783 handleStartEvent(MetricDimensionKey(dimensionInWhat, DEFAULT_DIMENSION_KEY),
784 conditionKey, condition, event);
785 } else {
786 auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat);
787 // If the what dimension is already there, we should update all the trackers even
788 // the condition is false.
789 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
790 for (const auto& condIt : whatIt->second) {
791 const bool cond = dimensionKeysInCondition.find(condIt.first) !=
Yao Chen0aff9032018-07-03 10:51:05 -0700792 dimensionKeysInCondition.end() && condition;
Yangster13fb7e42018-03-07 17:30:49 -0800793 handleStartEvent(MetricDimensionKey(dimensionInWhat, condIt.first),
794 conditionKey, cond, event);
795 dimensionKeysInCondition.erase(condIt.first);
796 }
797 }
798 for (const auto& conditionDimension : dimensionKeysInCondition) {
799 handleStartEvent(MetricDimensionKey(dimensionInWhat, conditionDimension), conditionKey,
800 condition, event);
801 }
802 }
803}
804
Yangsterf2bee6f2017-11-29 12:01:05 -0800805size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800806 size_t totalSize = 0;
807 for (const auto& pair : mPastBuckets) {
808 totalSize += pair.second.size() * kBucketSize;
809 }
810 return totalSize;
yro69007c82017-10-26 20:42:57 -0700811}
812
Yao Chen729093d2017-10-16 10:33:26 -0700813} // namespace statsd
814} // namespace os
815} // namespace android