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