blob: 80329c3a8a4218603cdc7254bcafb1bccc5afb54 [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-mac53928882018-02-25 23:02:56 -0800104 if (mDimensionsInWhat.size() == mInternalDimensions.size()) {
105 bool mUseWhatDimensionAsInternalDimension = true;
106 for (size_t i = 0; mUseWhatDimensionAsInternalDimension &&
107 i < mDimensionsInWhat.size(); ++i) {
108 if (mDimensionsInWhat[i] != mInternalDimensions[i]) {
109 mUseWhatDimensionAsInternalDimension = false;
110 }
111 }
112 } else {
113 mUseWhatDimensionAsInternalDimension = false;
114 }
115
Yangster-mac94e197c2018-01-02 16:03:03 -0800116 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen729093d2017-10-16 10:33:26 -0700117 (long long)mBucketSizeNs, (long long)mStartTimeNs);
118}
119
120DurationMetricProducer::~DurationMetricProducer() {
121 VLOG("~DurationMetric() called");
122}
123
Yangster-mac932ecec2018-02-01 10:23:52 -0800124sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(
125 const Alert &alert, const sp<AlarmMonitor>& anomalyAlarmMonitor) {
Bookatz857aaa52017-12-19 15:29:06 -0800126 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac932ecec2018-02-01 10:23:52 -0800127 sp<DurationAnomalyTracker> anomalyTracker =
128 new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor);
Bookatz857aaa52017-12-19 15:29:06 -0800129 if (anomalyTracker != nullptr) {
130 mAnomalyTrackers.push_back(anomalyTracker);
131 }
132 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800133}
134
Yao Chen5154a372017-10-30 22:57:06 -0700135unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800136 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800137 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800138 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800139 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800140 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800141 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
142 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800143 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800144 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800145 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800146 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
147 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a372017-10-30 22:57:06 -0700148 }
149}
150
Yangsterf2bee6f2017-11-29 12:01:05 -0800151void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800152 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800153 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800154
Yao Chen729093d2017-10-16 10:33:26 -0700155 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac53928882018-02-25 23:02:56 -0800156 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
157 for (auto& pair : whatIt.second) {
158 pair.second->onSlicedConditionMayChange(eventTime);
159 }
Yao Chen729093d2017-10-16 10:33:26 -0700160 }
Yangster-mac93694462018-01-22 20:49:31 -0800161
Yangster-mac53928882018-02-25 23:02:56 -0800162 if (mDimensionsInCondition.empty()) {
163 return;
Yangster-mac93694462018-01-22 20:49:31 -0800164 }
Yangster-mac53928882018-02-25 23:02:56 -0800165
166 if (mMetric2ConditionLinks.empty()) {
167 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
168 mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition,
169 &conditionDimensionsKeySet);
170 for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) {
171 for (const auto& pair : whatIt.second) {
172 conditionDimensionsKeySet.erase(pair.first);
Yangster-mac93694462018-01-22 20:49:31 -0800173 }
Yangster-mac53928882018-02-25 23:02:56 -0800174 }
175 for (const auto& conditionDimension : conditionDimensionsKeySet) {
176 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
177 if (!whatIt.second.empty()) {
178 unique_ptr<DurationTracker> newTracker =
179 whatIt.second.begin()->second->clone(eventTime);
180 newTracker->setEventKey(MetricDimensionKey(whatIt.first, conditionDimension));
181 newTracker->onSlicedConditionMayChange(eventTime);
182 whatIt.second[conditionDimension] = std::move(newTracker);
183 }
184 }
185 }
186 } else {
187 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
188 ConditionKey conditionKey;
189 for (const auto& link : mMetric2ConditionLinks) {
190 getDimensionForCondition(whatIt.first.getValues(), link,
191 &conditionKey[link.conditionId]);
192 }
193 std::unordered_set<HashableDimensionKey> conditionDimensionsKeys;
194 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
195 &conditionDimensionsKeys);
196
197 for (const auto& conditionDimension : conditionDimensionsKeys) {
198 if (!whatIt.second.empty() &&
199 whatIt.second.find(conditionDimension) == whatIt.second.end()) {
200 auto newTracker = whatIt.second.begin()->second->clone(eventTime);
201 newTracker->setEventKey(MetricDimensionKey(whatIt.first, conditionDimension));
202 newTracker->onSlicedConditionMayChange(eventTime);
203 whatIt.second[conditionDimension] = std::move(newTracker);
204 }
205 }
Yangster-mac93694462018-01-22 20:49:31 -0800206 }
207 }
Yao Chen729093d2017-10-16 10:33:26 -0700208}
209
Yangsterf2bee6f2017-11-29 12:01:05 -0800210void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
211 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800212 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700213 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800214 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700215 // TODO: need to populate the condition change time from the event which triggers the condition
216 // change, instead of using current time.
Yangster-mac53928882018-02-25 23:02:56 -0800217 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
218 for (auto& pair : whatIt.second) {
219 pair.second->onConditionChanged(conditionMet, eventTime);
220 }
Yao Chen729093d2017-10-16 10:33:26 -0700221 }
222}
223
Yao Chen288c6002017-12-12 13:43:18 -0800224void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
225 ProtoOutputStream* protoOutput) {
226 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800227 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800228 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800229 return;
230 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000231
Yangster-mac94e197c2018-01-02 16:03:03 -0800232 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800233 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
234
Yao Chen8a8d16c2018-02-08 14:50:40 -0800235 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000236
Yao Chen729093d2017-10-16 10:33:26 -0700237 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800238 const MetricDimensionKey& dimensionKey = pair.first;
239 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800240
yrob0378b02017-11-09 20:36:25 -0800241 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800242 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800243
Yangster-mac20877162017-12-22 17:19:39 -0800244 // First fill dimension.
245 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800246 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800247 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800248 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800249
Yangster-mac93694462018-01-22 20:49:31 -0800250 if (dimensionKey.hasDimensionKeyInCondition()) {
251 long long dimensionInConditionToken = protoOutput->start(
252 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800253 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800254 protoOutput->end(dimensionInConditionToken);
255 }
256
yro2b0f8862017-11-06 14:27:31 -0800257 // Then fill bucket_info (DurationBucketInfo).
258 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800259 long long bucketInfoToken = protoOutput->start(
260 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac330af582018-02-08 15:24:38 -0800261 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800262 (long long)bucket.mBucketStartNs);
Yangster-mac330af582018-02-08 15:24:38 -0800263 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800264 (long long)bucket.mBucketEndNs);
265 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
266 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800267 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
268 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
269 }
270
Yao Chen288c6002017-12-12 13:43:18 -0800271 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700272 }
yro2b0f8862017-11-06 14:27:31 -0800273
Yao Chen288c6002017-12-12 13:43:18 -0800274 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800275 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800276}
Yao Chen729093d2017-10-16 10:33:26 -0700277
David Chen27785a82018-01-19 17:06:45 -0800278void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
279 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
280
281 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700282 return;
283 }
Yao Chen5154a372017-10-30 22:57:06 -0700284 VLOG("flushing...........");
Yangster-mac53928882018-02-25 23:02:56 -0800285 for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin();
286 whatIt != mCurrentSlicedDurationTrackerMap.end();) {
287 for (auto it = whatIt->second.begin(); it != whatIt->second.end();) {
288 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
289 VLOG("erase bucket for key %s %s", whatIt->first.c_str(), it->first.c_str());
290 it = whatIt->second.erase(it);
291 } else {
292 ++it;
293 }
294 }
295 if (whatIt->second.empty()) {
296 whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt);
Yao Chend41c4222017-11-15 19:26:14 -0800297 } else {
Yangster-mac53928882018-02-25 23:02:56 -0800298 whatIt++;
Yao Chen729093d2017-10-16 10:33:26 -0700299 }
300 }
Yao Chen5154a372017-10-30 22:57:06 -0700301
David Chen27785a82018-01-19 17:06:45 -0800302 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
303 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800304 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700305}
306
David Chen27785a82018-01-19 17:06:45 -0800307void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
Yangster-mac53928882018-02-25 23:02:56 -0800308 for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin();
309 whatIt != mCurrentSlicedDurationTrackerMap.end();) {
310 for (auto it = whatIt->second.begin(); it != whatIt->second.end();) {
311 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
312 VLOG("erase bucket for key %s %s", whatIt->first.c_str(), it->first.c_str());
313 it = whatIt->second.erase(it);
314 } else {
315 ++it;
316 }
317 }
318 if (whatIt->second.empty()) {
319 whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt);
David Chen27785a82018-01-19 17:06:45 -0800320 } else {
Yangster-mac53928882018-02-25 23:02:56 -0800321 whatIt++;
David Chen27785a82018-01-19 17:06:45 -0800322 }
323 }
324}
325
Yao Chen884c8c12018-01-26 10:36:25 -0800326void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800327 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800328 return;
329 }
330
331 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800332 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800333 if (verbose) {
Yangster-mac53928882018-02-25 23:02:56 -0800334 for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) {
335 for (const auto& slice : whatIt.second) {
336 fprintf(out, "\t%s\t%s\n", whatIt.first.c_str(), slice.first.c_str());
337 slice.second->dumpStates(out, verbose);
338 }
Yao Chen884c8c12018-01-26 10:36:25 -0800339 }
340 }
341}
342
Yangster-mac93694462018-01-22 20:49:31 -0800343bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800344 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800345 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
346 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800347 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800348 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
349 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800350 ALOGE("DurationMetric %lld dropping data for dimension key %s",
351 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800352 return true;
353 }
354 }
355 return false;
356}
357
Yangster-mac53928882018-02-25 23:02:56 -0800358void DurationMetricProducer::handleStartEvent(const MetricDimensionKey& eventKey,
359 const ConditionKey& conditionKeys,
360 bool condition, const LogEvent& event) {
361 const auto& whatKey = eventKey.getDimensionKeyInWhat();
362 const auto& condKey = eventKey.getDimensionKeyInCondition();
Yao Chen5154a372017-10-30 22:57:06 -0700363
Yangster-mac53928882018-02-25 23:02:56 -0800364 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey);
365 if (whatIt == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800366 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800367 return;
368 }
Yangster-mac53928882018-02-25 23:02:56 -0800369 mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey);
370 } else {
371 if (whatIt->second.find(condKey) == whatIt->second.end()) {
372 if (hitGuardRailLocked(eventKey)) {
373 return;
374 }
375 mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey);
376 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000377 }
Yao Chen5154a372017-10-30 22:57:06 -0700378
Yangster-mac53928882018-02-25 23:02:56 -0800379 auto it = mCurrentSlicedDurationTrackerMap.find(whatKey)->second.find(condKey);
380 if (mUseWhatDimensionAsInternalDimension) {
381 it->second->noteStart(whatKey, condition,
382 event.GetElapsedTimestampNs(), conditionKeys);
383 return;
384 }
Yao Chen5154a372017-10-30 22:57:06 -0700385
Yao Chen8a8d16c2018-02-08 14:50:40 -0800386 std::vector<HashableDimensionKey> values;
387 filterValues(mInternalDimensions, event.getValues(), &values);
Yangster-mac20877162017-12-22 17:19:39 -0800388 if (values.empty()) {
Yangster-mac53928882018-02-25 23:02:56 -0800389 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
390 event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800391 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800392 for (const auto& value : values) {
Yangster-mac53928882018-02-25 23:02:56 -0800393 it->second->noteStart(value, condition, event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800394 }
Yao Chen5154a372017-10-30 22:57:06 -0700395 }
Yangster-mac20877162017-12-22 17:19:39 -0800396
Yao Chen729093d2017-10-16 10:33:26 -0700397}
398
Yangster-mac53928882018-02-25 23:02:56 -0800399void DurationMetricProducer::onMatchedLogEventInternalLocked(
400 const size_t matcherIndex, const MetricDimensionKey& eventKey,
401 const ConditionKey& conditionKeys, bool condition,
402 const LogEvent& event) {
403 ALOGW("Not used in duration tracker.");
404}
405
406void DurationMetricProducer::onMatchedLogEventLocked(const size_t matcherIndex,
407 const LogEvent& event) {
408 uint64_t eventTimeNs = event.GetElapsedTimestampNs();
409 if (eventTimeNs < mStartTimeNs) {
410 return;
411 }
412
413 flushIfNeededLocked(event.GetElapsedTimestampNs());
414
415 // Handles Stopall events.
416 if (matcherIndex == mStopAllIndex) {
417 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
418 for (auto& pair : whatIt.second) {
419 pair.second->noteStopAll(event.GetElapsedTimestampNs());
420 }
421 }
422 return;
423 }
424
425 vector<HashableDimensionKey> dimensionInWhatValues;
426 if (!mDimensionsInWhat.empty()) {
427 filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhatValues);
428 } else {
429 dimensionInWhatValues.push_back(DEFAULT_DIMENSION_KEY);
430 }
431
432 // Handles Stop events.
433 if (matcherIndex == mStopIndex) {
434 if (mUseWhatDimensionAsInternalDimension) {
435 for (const HashableDimensionKey& whatKey : dimensionInWhatValues) {
436 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey);
437 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
438 for (const auto& condIt : whatIt->second) {
439 condIt.second->noteStop(whatKey, event.GetElapsedTimestampNs(), false);
440 }
441 }
442 }
443 return;
444 }
445
446 std::vector<HashableDimensionKey> internalDimensionKeys;
447 filterValues(mInternalDimensions, event.getValues(), &internalDimensionKeys);
448 if (internalDimensionKeys.empty()) {
449 internalDimensionKeys.push_back(DEFAULT_DIMENSION_KEY);
450 }
451 for (const HashableDimensionKey& whatDimension : dimensionInWhatValues) {
452 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatDimension);
453 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
454 for (const auto& condIt : whatIt->second) {
455 for (const auto& internalDimensionKey : internalDimensionKeys) {
456 condIt.second->noteStop(
457 internalDimensionKey, event.GetElapsedTimestampNs(), false);
458 }
459 }
460 }
461 }
462 return;
463 }
464
465 bool condition;
466 ConditionKey conditionKey;
467 std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
468 if (mConditionSliced) {
469 for (const auto& link : mMetric2ConditionLinks) {
470 getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]);
471 }
472
473 auto conditionState =
474 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
475 &dimensionKeysInCondition);
476 condition = (conditionState == ConditionState::kTrue);
477 if (mDimensionsInCondition.empty() && condition) {
478 dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
479 }
480 } else {
481 condition = mCondition;
482 if (condition) {
483 dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
484 }
485 }
486
487 for (const auto& whatDimension : dimensionInWhatValues) {
488 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatDimension);
489 // If the what dimension is already there, we should update all the trackers even
490 // the condition is false.
491 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
492 for (const auto& condIt : whatIt->second) {
493 const bool cond = dimensionKeysInCondition.find(condIt.first) !=
494 dimensionKeysInCondition.end();
495 handleStartEvent(MetricDimensionKey(whatDimension, condIt.first),
496 conditionKey, cond, event);
497 }
498 } else {
499 // If it is a new what dimension key, we need to handle the start events for all current
500 // condition dimensions.
501 for (const auto& conditionDimension : dimensionKeysInCondition) {
502 handleStartEvent(MetricDimensionKey(whatDimension, conditionDimension),
503 conditionKey, condition, event);
504 }
505 }
506 if (dimensionKeysInCondition.empty()) {
507 handleStartEvent(MetricDimensionKey(whatDimension, DEFAULT_DIMENSION_KEY),
508 conditionKey, condition, event);
509 }
510 }
511}
512
513
Yangsterf2bee6f2017-11-29 12:01:05 -0800514size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800515 size_t totalSize = 0;
516 for (const auto& pair : mPastBuckets) {
517 totalSize += pair.second.size() * kBucketSize;
518 }
519 return totalSize;
yro69007c82017-10-26 20:42:57 -0700520}
521
Yao Chen729093d2017-10-16 10:33:26 -0700522} // namespace statsd
523} // namespace os
524} // namespace android