blob: 7f8c264c0e3710922f94cd2c767dec3935aaf18c [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 Chen5154a3792017-10-30 22:57:06 -070018
Yao Chen729093d2017-10-16 10:33:26 -070019#include "Log.h"
Yao Chen5154a3792017-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 Chen5154a3792017-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 Chen5154a3792017-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);
Bookatz423f7532018-03-08 15:45:14 -0800127 if (mAggregationType == DurationMetric_AggregationType_SUM) {
128 if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) {
129 ALOGW("invalid alert for SUM: threshold (%f) > possible recordable value (%d x %lld)",
130 alert.trigger_if_sum_gt(), alert.num_buckets(), (long long)mBucketSizeNs);
131 return nullptr;
132 }
133 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800134 sp<DurationAnomalyTracker> anomalyTracker =
135 new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor);
Bookatz857aaa52017-12-19 15:29:06 -0800136 if (anomalyTracker != nullptr) {
137 mAnomalyTrackers.push_back(anomalyTracker);
138 }
139 return anomalyTracker;
Bookatz450099d2017-11-30 17:09:30 -0800140}
141
Yao Chen5154a3792017-10-30 22:57:06 -0700142unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mac93694462018-01-22 20:49:31 -0800143 const MetricDimensionKey& eventKey) const {
Yao Chenf09569f2017-12-13 17:00:51 -0800144 switch (mAggregationType) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800145 case DurationMetric_AggregationType_SUM:
Yao Chenb3561512017-11-21 18:07:17 -0800146 return make_unique<OringDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800147 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800148 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
149 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800150 case DurationMetric_AggregationType_MAX_SPARSE:
Yao Chenb3561512017-11-21 18:07:17 -0800151 return make_unique<MaxDurationTracker>(
Yangster-mac93694462018-01-22 20:49:31 -0800152 mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex,
David Chen27785a82018-01-19 17:06:45 -0800153 mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum,
154 mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers);
Yao Chen5154a3792017-10-30 22:57:06 -0700155 }
156}
157
Yangsterf2bee6f2017-11-29 12:01:05 -0800158void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800159 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yangsterf2bee6f2017-11-29 12:01:05 -0800160 flushIfNeededLocked(eventTime);
Yangster-mac93694462018-01-22 20:49:31 -0800161
Yao Chen729093d2017-10-16 10:33:26 -0700162 // Now for each of the on-going event, check if the condition has changed for them.
Yangster-mac53928882018-02-25 23:02:56 -0800163 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
164 for (auto& pair : whatIt.second) {
165 pair.second->onSlicedConditionMayChange(eventTime);
166 }
Yao Chen729093d2017-10-16 10:33:26 -0700167 }
Yangster-mac93694462018-01-22 20:49:31 -0800168
Yangster-mac53928882018-02-25 23:02:56 -0800169 if (mDimensionsInCondition.empty()) {
170 return;
Yangster-mac93694462018-01-22 20:49:31 -0800171 }
Yangster-mac53928882018-02-25 23:02:56 -0800172
173 if (mMetric2ConditionLinks.empty()) {
174 std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet;
175 mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition,
176 &conditionDimensionsKeySet);
177 for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) {
178 for (const auto& pair : whatIt.second) {
179 conditionDimensionsKeySet.erase(pair.first);
Yangster-mac93694462018-01-22 20:49:31 -0800180 }
Yangster-mac53928882018-02-25 23:02:56 -0800181 }
182 for (const auto& conditionDimension : conditionDimensionsKeySet) {
183 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
184 if (!whatIt.second.empty()) {
185 unique_ptr<DurationTracker> newTracker =
186 whatIt.second.begin()->second->clone(eventTime);
187 newTracker->setEventKey(MetricDimensionKey(whatIt.first, conditionDimension));
188 newTracker->onSlicedConditionMayChange(eventTime);
189 whatIt.second[conditionDimension] = std::move(newTracker);
190 }
191 }
192 }
193 } else {
194 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
195 ConditionKey conditionKey;
196 for (const auto& link : mMetric2ConditionLinks) {
197 getDimensionForCondition(whatIt.first.getValues(), link,
198 &conditionKey[link.conditionId]);
199 }
200 std::unordered_set<HashableDimensionKey> conditionDimensionsKeys;
201 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
202 &conditionDimensionsKeys);
203
204 for (const auto& conditionDimension : conditionDimensionsKeys) {
205 if (!whatIt.second.empty() &&
206 whatIt.second.find(conditionDimension) == whatIt.second.end()) {
207 auto newTracker = whatIt.second.begin()->second->clone(eventTime);
208 newTracker->setEventKey(MetricDimensionKey(whatIt.first, conditionDimension));
209 newTracker->onSlicedConditionMayChange(eventTime);
210 whatIt.second[conditionDimension] = std::move(newTracker);
211 }
212 }
Yangster-mac93694462018-01-22 20:49:31 -0800213 }
214 }
Yao Chen729093d2017-10-16 10:33:26 -0700215}
216
Yangsterf2bee6f2017-11-29 12:01:05 -0800217void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
218 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800219 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700220 mCondition = conditionMet;
Yangsterf2bee6f2017-11-29 12:01:05 -0800221 flushIfNeededLocked(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700222 // TODO: need to populate the condition change time from the event which triggers the condition
223 // change, instead of using current time.
Yangster-mac53928882018-02-25 23:02:56 -0800224 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
225 for (auto& pair : whatIt.second) {
226 pair.second->onConditionChanged(conditionMet, eventTime);
227 }
Yao Chen729093d2017-10-16 10:33:26 -0700228 }
229}
230
Yao Chen06dba5d2018-01-26 13:38:16 -0800231void DurationMetricProducer::dropDataLocked(const uint64_t dropTimeNs) {
232 flushIfNeededLocked(dropTimeNs);
233 mPastBuckets.clear();
234}
235
Yao Chen288c6002017-12-12 13:43:18 -0800236void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
237 ProtoOutputStream* protoOutput) {
238 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800239 if (mPastBuckets.empty()) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800240 VLOG(" Duration metric, empty return");
Yangster-mac635b4b32018-01-23 20:17:35 -0800241 return;
242 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000243
Yangster-mac94e197c2018-01-02 16:03:03 -0800244 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800245 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
246
Yao Chen8a8d16c2018-02-08 14:50:40 -0800247 VLOG("Duration metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000248
Yao Chen729093d2017-10-16 10:33:26 -0700249 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800250 const MetricDimensionKey& dimensionKey = pair.first;
251 VLOG(" dimension key %s", dimensionKey.c_str());
Yao Chen1ff4f432017-11-16 17:01:40 -0800252
yrob0378b02017-11-09 20:36:25 -0800253 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800254 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800255
Yangster-mac20877162017-12-22 17:19:39 -0800256 // First fill dimension.
257 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800258 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800259 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800260 protoOutput->end(dimensionToken);
yro2b0f8862017-11-06 14:27:31 -0800261
Yangster-mac93694462018-01-22 20:49:31 -0800262 if (dimensionKey.hasDimensionKeyInCondition()) {
263 long long dimensionInConditionToken = protoOutput->start(
264 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800265 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800266 protoOutput->end(dimensionInConditionToken);
267 }
268
yro2b0f8862017-11-06 14:27:31 -0800269 // Then fill bucket_info (DurationBucketInfo).
270 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800271 long long bucketInfoToken = protoOutput->start(
272 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac330af582018-02-08 15:24:38 -0800273 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800274 (long long)bucket.mBucketStartNs);
Yangster-mac330af582018-02-08 15:24:38 -0800275 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_NANOS,
Yao Chen288c6002017-12-12 13:43:18 -0800276 (long long)bucket.mBucketEndNs);
277 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
278 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800279 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
280 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
281 }
282
Yao Chen288c6002017-12-12 13:43:18 -0800283 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700284 }
yro2b0f8862017-11-06 14:27:31 -0800285
Yao Chen288c6002017-12-12 13:43:18 -0800286 protoOutput->end(protoToken);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800287 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800288}
Yao Chen729093d2017-10-16 10:33:26 -0700289
David Chen27785a82018-01-19 17:06:45 -0800290void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
291 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
292
293 if (currentBucketEndTimeNs > eventTimeNs) {
Yao Chen729093d2017-10-16 10:33:26 -0700294 return;
295 }
Yao Chen5154a3792017-10-30 22:57:06 -0700296 VLOG("flushing...........");
Yangster-mac53928882018-02-25 23:02:56 -0800297 for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin();
298 whatIt != mCurrentSlicedDurationTrackerMap.end();) {
299 for (auto it = whatIt->second.begin(); it != whatIt->second.end();) {
300 if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) {
301 VLOG("erase bucket for key %s %s", whatIt->first.c_str(), it->first.c_str());
302 it = whatIt->second.erase(it);
303 } else {
304 ++it;
305 }
306 }
307 if (whatIt->second.empty()) {
308 whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt);
Yao Chend41c4222017-11-15 19:26:14 -0800309 } else {
Yangster-mac53928882018-02-25 23:02:56 -0800310 whatIt++;
Yao Chen729093d2017-10-16 10:33:26 -0700311 }
312 }
Yao Chen5154a3792017-10-30 22:57:06 -0700313
David Chen27785a82018-01-19 17:06:45 -0800314 int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
315 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800316 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a3792017-10-30 22:57:06 -0700317}
318
David Chen27785a82018-01-19 17:06:45 -0800319void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
Yangster-mac53928882018-02-25 23:02:56 -0800320 for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin();
321 whatIt != mCurrentSlicedDurationTrackerMap.end();) {
322 for (auto it = whatIt->second.begin(); it != whatIt->second.end();) {
323 if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) {
324 VLOG("erase bucket for key %s %s", whatIt->first.c_str(), it->first.c_str());
325 it = whatIt->second.erase(it);
326 } else {
327 ++it;
328 }
329 }
330 if (whatIt->second.empty()) {
331 whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt);
David Chen27785a82018-01-19 17:06:45 -0800332 } else {
Yangster-mac53928882018-02-25 23:02:56 -0800333 whatIt++;
David Chen27785a82018-01-19 17:06:45 -0800334 }
335 }
336}
337
Yao Chen884c8c12018-01-26 10:36:25 -0800338void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
Yangster-mac93694462018-01-22 20:49:31 -0800339 if (mCurrentSlicedDurationTrackerMap.size() == 0) {
Yao Chen884c8c12018-01-26 10:36:25 -0800340 return;
341 }
342
343 fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId,
Yangster-mac93694462018-01-22 20:49:31 -0800344 (unsigned long)mCurrentSlicedDurationTrackerMap.size());
Yao Chen884c8c12018-01-26 10:36:25 -0800345 if (verbose) {
Yangster-mac53928882018-02-25 23:02:56 -0800346 for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) {
347 for (const auto& slice : whatIt.second) {
348 fprintf(out, "\t%s\t%s\n", whatIt.first.c_str(), slice.first.c_str());
349 slice.second->dumpStates(out, verbose);
350 }
Yao Chen884c8c12018-01-26 10:36:25 -0800351 }
352 }
353}
354
Yangster-mac93694462018-01-22 20:49:31 -0800355bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800356 // 1. Report the tuple count if the tuple count > soft limit
Yangster-mac93694462018-01-22 20:49:31 -0800357 if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
358 size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800359 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800360 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
361 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800362 ALOGE("DurationMetric %lld dropping data for dimension key %s",
363 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800364 return true;
365 }
366 }
367 return false;
368}
369
Yangster-mac53928882018-02-25 23:02:56 -0800370void DurationMetricProducer::handleStartEvent(const MetricDimensionKey& eventKey,
371 const ConditionKey& conditionKeys,
372 bool condition, const LogEvent& event) {
373 const auto& whatKey = eventKey.getDimensionKeyInWhat();
374 const auto& condKey = eventKey.getDimensionKeyInCondition();
Yao Chen5154a3792017-10-30 22:57:06 -0700375
Yangster-mac53928882018-02-25 23:02:56 -0800376 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey);
377 if (whatIt == mCurrentSlicedDurationTrackerMap.end()) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800378 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800379 return;
380 }
Yangster-mac53928882018-02-25 23:02:56 -0800381 mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey);
382 } else {
383 if (whatIt->second.find(condKey) == whatIt->second.end()) {
384 if (hitGuardRailLocked(eventKey)) {
385 return;
386 }
387 mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey);
388 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000389 }
Yao Chen5154a3792017-10-30 22:57:06 -0700390
Yangster-mac53928882018-02-25 23:02:56 -0800391 auto it = mCurrentSlicedDurationTrackerMap.find(whatKey)->second.find(condKey);
392 if (mUseWhatDimensionAsInternalDimension) {
393 it->second->noteStart(whatKey, condition,
394 event.GetElapsedTimestampNs(), conditionKeys);
395 return;
396 }
Yao Chen5154a3792017-10-30 22:57:06 -0700397
Yao Chen8a8d16c2018-02-08 14:50:40 -0800398 std::vector<HashableDimensionKey> values;
399 filterValues(mInternalDimensions, event.getValues(), &values);
Yangster-mac20877162017-12-22 17:19:39 -0800400 if (values.empty()) {
Yangster-mac53928882018-02-25 23:02:56 -0800401 it->second->noteStart(DEFAULT_DIMENSION_KEY, condition,
402 event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800403 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800404 for (const auto& value : values) {
Yangster-mac53928882018-02-25 23:02:56 -0800405 it->second->noteStart(value, condition, event.GetElapsedTimestampNs(), conditionKeys);
Yangster-mac20877162017-12-22 17:19:39 -0800406 }
Yao Chen5154a3792017-10-30 22:57:06 -0700407 }
Yangster-mac20877162017-12-22 17:19:39 -0800408
Yao Chen729093d2017-10-16 10:33:26 -0700409}
410
Yangster-mac53928882018-02-25 23:02:56 -0800411void DurationMetricProducer::onMatchedLogEventInternalLocked(
412 const size_t matcherIndex, const MetricDimensionKey& eventKey,
413 const ConditionKey& conditionKeys, bool condition,
414 const LogEvent& event) {
415 ALOGW("Not used in duration tracker.");
416}
417
418void DurationMetricProducer::onMatchedLogEventLocked(const size_t matcherIndex,
419 const LogEvent& event) {
420 uint64_t eventTimeNs = event.GetElapsedTimestampNs();
421 if (eventTimeNs < mStartTimeNs) {
422 return;
423 }
424
425 flushIfNeededLocked(event.GetElapsedTimestampNs());
426
427 // Handles Stopall events.
428 if (matcherIndex == mStopAllIndex) {
429 for (auto& whatIt : mCurrentSlicedDurationTrackerMap) {
430 for (auto& pair : whatIt.second) {
431 pair.second->noteStopAll(event.GetElapsedTimestampNs());
432 }
433 }
434 return;
435 }
436
437 vector<HashableDimensionKey> dimensionInWhatValues;
438 if (!mDimensionsInWhat.empty()) {
439 filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhatValues);
440 } else {
441 dimensionInWhatValues.push_back(DEFAULT_DIMENSION_KEY);
442 }
443
444 // Handles Stop events.
445 if (matcherIndex == mStopIndex) {
446 if (mUseWhatDimensionAsInternalDimension) {
447 for (const HashableDimensionKey& whatKey : dimensionInWhatValues) {
448 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey);
449 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
450 for (const auto& condIt : whatIt->second) {
451 condIt.second->noteStop(whatKey, event.GetElapsedTimestampNs(), false);
452 }
453 }
454 }
455 return;
456 }
457
458 std::vector<HashableDimensionKey> internalDimensionKeys;
459 filterValues(mInternalDimensions, event.getValues(), &internalDimensionKeys);
460 if (internalDimensionKeys.empty()) {
461 internalDimensionKeys.push_back(DEFAULT_DIMENSION_KEY);
462 }
463 for (const HashableDimensionKey& whatDimension : dimensionInWhatValues) {
464 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatDimension);
465 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
466 for (const auto& condIt : whatIt->second) {
467 for (const auto& internalDimensionKey : internalDimensionKeys) {
468 condIt.second->noteStop(
469 internalDimensionKey, event.GetElapsedTimestampNs(), false);
470 }
471 }
472 }
473 }
474 return;
475 }
476
477 bool condition;
478 ConditionKey conditionKey;
479 std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
480 if (mConditionSliced) {
481 for (const auto& link : mMetric2ConditionLinks) {
482 getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]);
483 }
484
485 auto conditionState =
486 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
487 &dimensionKeysInCondition);
488 condition = (conditionState == ConditionState::kTrue);
489 if (mDimensionsInCondition.empty() && condition) {
490 dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
491 }
492 } else {
493 condition = mCondition;
494 if (condition) {
495 dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
496 }
497 }
498
499 for (const auto& whatDimension : dimensionInWhatValues) {
500 auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatDimension);
501 // If the what dimension is already there, we should update all the trackers even
502 // the condition is false.
503 if (whatIt != mCurrentSlicedDurationTrackerMap.end()) {
504 for (const auto& condIt : whatIt->second) {
505 const bool cond = dimensionKeysInCondition.find(condIt.first) !=
506 dimensionKeysInCondition.end();
507 handleStartEvent(MetricDimensionKey(whatDimension, condIt.first),
508 conditionKey, cond, event);
509 }
510 } else {
511 // If it is a new what dimension key, we need to handle the start events for all current
512 // condition dimensions.
513 for (const auto& conditionDimension : dimensionKeysInCondition) {
514 handleStartEvent(MetricDimensionKey(whatDimension, conditionDimension),
515 conditionKey, condition, event);
516 }
517 }
518 if (dimensionKeysInCondition.empty()) {
519 handleStartEvent(MetricDimensionKey(whatDimension, DEFAULT_DIMENSION_KEY),
520 conditionKey, condition, event);
521 }
522 }
523}
524
525
Yangsterf2bee6f2017-11-29 12:01:05 -0800526size_t DurationMetricProducer::byteSizeLocked() const {
Yangster7c334a12017-11-22 14:24:24 -0800527 size_t totalSize = 0;
528 for (const auto& pair : mPastBuckets) {
529 totalSize += pair.second.size() * kBucketSize;
530 }
531 return totalSize;
yro69007c82017-10-26 20:42:57 -0700532}
533
Yao Chen729093d2017-10-16 10:33:26 -0700534} // namespace statsd
535} // namespace os
536} // namespace android