Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 17 | #define DEBUG false |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 18 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 19 | #include "Log.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 20 | #include "DurationMetricProducer.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | #include "guardrail/StatsdStats.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 22 | #include "stats_util.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 23 | #include "stats_log_util.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 24 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 25 | #include <limits.h> |
| 26 | #include <stdlib.h> |
| 27 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 28 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 29 | using android::util::FIELD_TYPE_BOOL; |
| 30 | using android::util::FIELD_TYPE_FLOAT; |
| 31 | using android::util::FIELD_TYPE_INT32; |
| 32 | using android::util::FIELD_TYPE_INT64; |
| 33 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 34 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 35 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 36 | using std::string; |
| 37 | using std::unordered_map; |
| 38 | using std::vector; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace os { |
| 42 | namespace statsd { |
| 43 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 44 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 45 | const int FIELD_ID_ID = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 46 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 47 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 48 | const int FIELD_ID_DURATION_METRICS = 6; |
| 49 | // for DurationMetricDataWrapper |
| 50 | const int FIELD_ID_DATA = 1; |
| 51 | // for DurationMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 52 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 53 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 54 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 55 | // for DurationBucketInfo |
| 56 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 57 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 58 | const int FIELD_ID_DURATION = 3; |
| 59 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 60 | DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 61 | const int conditionIndex, const size_t startIndex, |
| 62 | const size_t stopIndex, const size_t stopAllIndex, |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 63 | const bool nesting, |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 64 | const sp<ConditionWizard>& wizard, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 65 | const FieldMatcher& internalDimensions, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 66 | const uint64_t startTimeNs) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 67 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 68 | mAggregationType(metric.aggregation_type()), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 69 | mStartIndex(startIndex), |
| 70 | mStopIndex(stopIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 71 | mStopAllIndex(stopAllIndex), |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 72 | mNested(nesting), |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 73 | mInternalDimensions(internalDimensions) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 74 | // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract |
| 75 | // them in the base class, because the proto generated CountMetric, and DurationMetric are |
| 76 | // not related. Maybe we should add a template in the future?? |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 77 | if (metric.has_bucket()) { |
| 78 | mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 79 | } else { |
| 80 | mBucketSizeNs = LLONG_MAX; |
| 81 | } |
| 82 | |
| 83 | // TODO: use UidMap if uid->pkg_name is required |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 84 | mDimensions = metric.dimensions_in_what(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 85 | |
| 86 | if (metric.links().size() > 0) { |
| 87 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 88 | metric.links().end()); |
| 89 | mConditionSliced = true; |
| 90 | } |
| 91 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 92 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 93 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 94 | } |
| 95 | |
| 96 | DurationMetricProducer::~DurationMetricProducer() { |
| 97 | VLOG("~DurationMetric() called"); |
| 98 | } |
| 99 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 100 | sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) { |
| 101 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 102 | if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) { |
| 103 | ALOGW("invalid alert: threshold (%f) > possible recordable value (%d x %lld)", |
| 104 | alert.trigger_if_sum_gt(), alert.num_buckets(), |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 105 | (long long)mBucketSizeNs); |
| 106 | return nullptr; |
| 107 | } |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 108 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey); |
| 109 | if (anomalyTracker != nullptr) { |
| 110 | mAnomalyTrackers.push_back(anomalyTracker); |
| 111 | } |
| 112 | return anomalyTracker; |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 115 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 116 | const HashableDimensionKey& eventKey) const { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 117 | switch (mAggregationType) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 118 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 119 | return make_unique<OringDurationTracker>( |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 120 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, mNested, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 121 | mCurrentBucketStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 122 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 123 | return make_unique<MaxDurationTracker>( |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 124 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, mNested, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 125 | mCurrentBucketStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 129 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 130 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 131 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 132 | // Now for each of the on-going event, check if the condition has changed for them. |
| 133 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 134 | pair.second->onSlicedConditionMayChange(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 138 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 139 | const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 140 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 141 | mCondition = conditionMet; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 142 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 143 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 144 | // change, instead of using current time. |
| 145 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 146 | pair.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 150 | void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) { |
| 151 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 152 | report->set_metric_id(mMetricId); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 153 | report->set_start_report_nanos(mStartTimeNs); |
| 154 | |
| 155 | auto duration_metrics = report->mutable_duration_metrics(); |
| 156 | for (const auto& pair : mPastBuckets) { |
| 157 | DurationMetricData* metricData = duration_metrics->add_data(); |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 158 | *metricData->mutable_dimensions_in_what() = pair.first.getDimensionsValue(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 159 | for (const auto& bucket : pair.second) { |
| 160 | auto bucketInfo = metricData->add_bucket_info(); |
| 161 | bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs); |
| 162 | bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs); |
| 163 | bucketInfo->set_duration_nanos(bucket.mDuration); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 168 | void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 169 | ProtoOutputStream* protoOutput) { |
| 170 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 171 | if (mPastBuckets.empty()) { |
| 172 | return; |
| 173 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 174 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 175 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 176 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 177 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
| 178 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 179 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 180 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 181 | for (const auto& pair : mPastBuckets) { |
| 182 | const HashableDimensionKey& hashableKey = pair.first; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 183 | VLOG(" dimension key %s", hashableKey.c_str()); |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 184 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 185 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 186 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 187 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 188 | // First fill dimension. |
| 189 | long long dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 190 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 191 | writeDimensionsValueProtoToStream(hashableKey.getDimensionsValue(), protoOutput); |
| 192 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 193 | |
| 194 | // Then fill bucket_info (DurationBucketInfo). |
| 195 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 196 | long long bucketInfoToken = protoOutput->start( |
| 197 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 198 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 199 | (long long)bucket.mBucketStartNs); |
| 200 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 201 | (long long)bucket.mBucketEndNs); |
| 202 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 203 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 204 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 205 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 206 | } |
| 207 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 208 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 209 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 210 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 211 | protoOutput->end(protoToken); |
| 212 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 213 | mPastBuckets.clear(); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 214 | mStartTimeNs = mCurrentBucketStartTimeNs; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 215 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 216 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 217 | void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTime) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 218 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) { |
| 219 | return; |
| 220 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 221 | VLOG("flushing..........."); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 222 | for (auto it = mCurrentSlicedDuration.begin(); it != mCurrentSlicedDuration.end();) { |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 223 | if (it->second->flushIfNeeded(eventTime, &mPastBuckets)) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 224 | VLOG("erase bucket for key %s", it->first.c_str()); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 225 | it = mCurrentSlicedDuration.erase(it); |
| 226 | } else { |
| 227 | ++it; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 230 | |
| 231 | int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
| 232 | mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 233 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame^] | 236 | void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 237 | if (mCurrentSlicedDuration.size() == 0) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 242 | (unsigned long)mCurrentSlicedDuration.size()); |
| 243 | if (verbose) { |
| 244 | for (const auto& slice : mCurrentSlicedDuration) { |
| 245 | fprintf(out, "\t%s\n", slice.first.c_str()); |
| 246 | slice.second->dumpStates(out, verbose); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 251 | bool DurationMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 252 | // the key is not new, we are good. |
| 253 | if (mCurrentSlicedDuration.find(newKey) != mCurrentSlicedDuration.end()) { |
| 254 | return false; |
| 255 | } |
| 256 | // 1. Report the tuple count if the tuple count > soft limit |
| 257 | if (mCurrentSlicedDuration.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 258 | size_t newTupleCount = mCurrentSlicedDuration.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 259 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 260 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 261 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 262 | ALOGE("DurationMetric %lld dropping data for dimension key %s", |
| 263 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 270 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 271 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 272 | const ConditionKey& conditionKeys, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 273 | const LogEvent& event) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 274 | flushIfNeededLocked(event.GetTimestampNs()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 275 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 276 | if (matcherIndex == mStopAllIndex) { |
| 277 | for (auto& pair : mCurrentSlicedDuration) { |
| 278 | pair.second->noteStopAll(event.GetTimestampNs()); |
| 279 | } |
| 280 | return; |
| 281 | } |
| 282 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 283 | |
| 284 | if (mCurrentSlicedDuration.find(eventKey) == mCurrentSlicedDuration.end()) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 285 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 286 | return; |
| 287 | } |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 288 | mCurrentSlicedDuration[eventKey] = createDurationTracker(eventKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 289 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 290 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 291 | auto it = mCurrentSlicedDuration.find(eventKey); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 292 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 293 | std::vector<DimensionsValue> values = getDimensionKeys(event, mInternalDimensions); |
| 294 | if (values.empty()) { |
| 295 | if (matcherIndex == mStartIndex) { |
| 296 | it->second->noteStart(DEFAULT_DIMENSION_KEY, condition, |
| 297 | event.GetTimestampNs(), conditionKeys); |
| 298 | } else if (matcherIndex == mStopIndex) { |
| 299 | it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetTimestampNs(), false); |
| 300 | } |
| 301 | } else { |
| 302 | for (const DimensionsValue& value : values) { |
| 303 | if (matcherIndex == mStartIndex) { |
| 304 | it->second->noteStart(HashableDimensionKey(value), condition, |
| 305 | event.GetTimestampNs(), conditionKeys); |
| 306 | } else if (matcherIndex == mStopIndex) { |
| 307 | it->second->noteStop(HashableDimensionKey(value), event.GetTimestampNs(), false); |
| 308 | } |
| 309 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 310 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 311 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 314 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 315 | size_t totalSize = 0; |
| 316 | for (const auto& pair : mPastBuckets) { |
| 317 | totalSize += pair.second.size() * kBucketSize; |
| 318 | } |
| 319 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 322 | } // namespace statsd |
| 323 | } // namespace os |
| 324 | } // namespace android |