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 | |
| 17 | #define DEBUG true |
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" |
| 23 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 27 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 28 | using android::util::FIELD_TYPE_BOOL; |
| 29 | using android::util::FIELD_TYPE_FLOAT; |
| 30 | using android::util::FIELD_TYPE_INT32; |
| 31 | using android::util::FIELD_TYPE_INT64; |
| 32 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 33 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 34 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 35 | using std::string; |
| 36 | using std::unordered_map; |
| 37 | using std::vector; |
| 38 | |
| 39 | namespace android { |
| 40 | namespace os { |
| 41 | namespace statsd { |
| 42 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 43 | // for StatsLogReport |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 44 | const int FIELD_ID_NAME = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 45 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 46 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 47 | const int FIELD_ID_DURATION_METRICS = 6; |
| 48 | // for DurationMetricDataWrapper |
| 49 | const int FIELD_ID_DATA = 1; |
| 50 | // for DurationMetricData |
| 51 | const int FIELD_ID_DIMENSION = 1; |
| 52 | const int FIELD_ID_BUCKET_INFO = 2; |
| 53 | // for KeyValuePair |
| 54 | const int FIELD_ID_KEY = 1; |
| 55 | const int FIELD_ID_VALUE_STR = 2; |
| 56 | const int FIELD_ID_VALUE_INT = 3; |
| 57 | const int FIELD_ID_VALUE_BOOL = 4; |
| 58 | const int FIELD_ID_VALUE_FLOAT = 5; |
| 59 | // for DurationBucketInfo |
| 60 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 61 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 62 | const int FIELD_ID_DURATION = 3; |
| 63 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 64 | DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 65 | const int conditionIndex, const size_t startIndex, |
| 66 | const size_t stopIndex, const size_t stopAllIndex, |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 67 | const bool nesting, |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 68 | const sp<ConditionWizard>& wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 69 | const vector<KeyMatcher>& internalDimension, |
| 70 | const uint64_t startTimeNs) |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 71 | : MetricProducer(key, startTimeNs, conditionIndex, wizard), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 72 | mMetric(metric), |
| 73 | mStartIndex(startIndex), |
| 74 | mStopIndex(stopIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 75 | mStopAllIndex(stopAllIndex), |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 76 | mNested(nesting), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 77 | mInternalDimension(internalDimension) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 78 | // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract |
| 79 | // them in the base class, because the proto generated CountMetric, and DurationMetric are |
| 80 | // not related. Maybe we should add a template in the future?? |
| 81 | if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) { |
| 82 | mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000000; |
| 83 | } else { |
| 84 | mBucketSizeNs = LLONG_MAX; |
| 85 | } |
| 86 | |
| 87 | // TODO: use UidMap if uid->pkg_name is required |
| 88 | mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end()); |
| 89 | |
| 90 | if (metric.links().size() > 0) { |
| 91 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 92 | metric.links().end()); |
| 93 | mConditionSliced = true; |
| 94 | } |
| 95 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 96 | startNewProtoOutputStreamLocked(mStartTimeNs); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 97 | |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 98 | 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] | 99 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 100 | } |
| 101 | |
| 102 | DurationMetricProducer::~DurationMetricProducer() { |
| 103 | VLOG("~DurationMetric() called"); |
| 104 | } |
| 105 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 106 | void DurationMetricProducer::startNewProtoOutputStreamLocked(long long startTime) { |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 107 | mProto = std::make_unique<ProtoOutputStream>(); |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 108 | mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 109 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime); |
| 110 | mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
| 111 | } |
| 112 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 113 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 114 | const HashableDimensionKey& eventKey, vector<DurationBucket>& bucket) const { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 115 | switch (mMetric.aggregation_type()) { |
| 116 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 117 | return make_unique<OringDurationTracker>( |
| 118 | mConfigKey, mMetric.name(), eventKey, mWizard, mConditionTrackerIndex, mNested, |
| 119 | mCurrentBucketStartTimeNs, mBucketSizeNs, mAnomalyTrackers, bucket); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 120 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 121 | return make_unique<MaxDurationTracker>( |
| 122 | mConfigKey, mMetric.name(), eventKey, mWizard, mConditionTrackerIndex, mNested, |
| 123 | mCurrentBucketStartTimeNs, mBucketSizeNs, mAnomalyTrackers, bucket); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 127 | void DurationMetricProducer::finish() { |
| 128 | // TODO: write the StatsLogReport to dropbox using |
| 129 | // DropboxWriter. |
| 130 | } |
| 131 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 132 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 133 | VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str()); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 134 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 135 | // Now for each of the on-going event, check if the condition has changed for them. |
| 136 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 137 | pair.second->onSlicedConditionMayChange(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 141 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 142 | const uint64_t eventTime) { |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 143 | VLOG("Metric %s onConditionChanged", mMetric.name().c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 144 | mCondition = conditionMet; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 145 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 146 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 147 | // change, instead of using current time. |
| 148 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 149 | pair.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 153 | std::unique_ptr<std::vector<uint8_t>> DurationMetricProducer::onDumpReportLocked() { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 154 | long long endTime = time(nullptr) * NS_PER_SEC; |
| 155 | |
| 156 | // Dump current bucket if it's stale. |
| 157 | // If current bucket is still on-going, don't force dump current bucket. |
| 158 | // In finish(), We can force dump current bucket. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 159 | flushIfNeededLocked(endTime); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 160 | VLOG("metric %s dump report now...", mMetric.name().c_str()); |
| 161 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 162 | for (const auto& pair : mPastBuckets) { |
| 163 | const HashableDimensionKey& hashableKey = pair.first; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 164 | VLOG(" dimension key %s", hashableKey.c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 165 | auto it = mDimensionKeyMap.find(hashableKey); |
| 166 | if (it == mDimensionKeyMap.end()) { |
| 167 | ALOGW("Dimension key %s not found?!?! skip...", hashableKey.c_str()); |
| 168 | continue; |
| 169 | } |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 170 | |
| 171 | // If there is no duration bucket info for this key, don't include it in the report. |
| 172 | // For example, duration started, but condition is never turned to true. |
| 173 | // TODO: Only add the key to the map when we add duration buckets info for it. |
| 174 | if (pair.second.size() == 0) { |
| 175 | continue; |
| 176 | } |
| 177 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 178 | long long wrapperToken = |
| 179 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 180 | |
| 181 | // First fill dimension (KeyValuePairs). |
| 182 | for (const auto& kv : it->second) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 183 | long long dimensionToken = |
| 184 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 185 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key()); |
| 186 | if (kv.has_value_str()) { |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 187 | mProto->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 188 | } else if (kv.has_value_int()) { |
| 189 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int()); |
| 190 | } else if (kv.has_value_bool()) { |
| 191 | mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool()); |
| 192 | } else if (kv.has_value_float()) { |
| 193 | mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float()); |
| 194 | } |
| 195 | mProto->end(dimensionToken); |
| 196 | } |
| 197 | |
| 198 | // Then fill bucket_info (DurationBucketInfo). |
| 199 | for (const auto& bucket : pair.second) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 200 | long long bucketInfoToken = |
| 201 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 202 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 203 | (long long)bucket.mBucketStartNs); |
| 204 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 205 | (long long)bucket.mBucketEndNs); |
| 206 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 207 | mProto->end(bucketInfoToken); |
| 208 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 209 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 210 | } |
| 211 | |
| 212 | mProto->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 213 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 214 | |
| 215 | mProto->end(mProtoToken); |
| 216 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, |
| 217 | (long long)mCurrentBucketStartTimeNs); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 218 | std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(); |
| 219 | startNewProtoOutputStreamLocked(endTime); |
Yao Chen | 30b6a20 | 2017-11-18 14:14:38 -0800 | [diff] [blame] | 220 | // TODO: Properly clear the old buckets. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 221 | return buffer; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 222 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 223 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 224 | void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTime) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 225 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) { |
| 226 | return; |
| 227 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 228 | VLOG("flushing..........."); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 229 | for (auto it = mCurrentSlicedDuration.begin(); it != mCurrentSlicedDuration.end();) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 230 | if (it->second->flushIfNeeded(eventTime)) { |
| 231 | VLOG("erase bucket for key %s", it->first.c_str()); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 232 | it = mCurrentSlicedDuration.erase(it); |
| 233 | } else { |
| 234 | ++it; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 237 | |
| 238 | int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
| 239 | mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 240 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 243 | bool DurationMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 244 | // the key is not new, we are good. |
| 245 | if (mCurrentSlicedDuration.find(newKey) != mCurrentSlicedDuration.end()) { |
| 246 | return false; |
| 247 | } |
| 248 | // 1. Report the tuple count if the tuple count > soft limit |
| 249 | if (mCurrentSlicedDuration.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 250 | size_t newTupleCount = mCurrentSlicedDuration.size() + 1; |
| 251 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(), |
| 252 | newTupleCount); |
| 253 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 254 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
| 255 | ALOGE("DurationMetric %s dropping data for dimension key %s", mMetric.name().c_str(), |
| 256 | newKey.c_str()); |
| 257 | return true; |
| 258 | } |
| 259 | } |
| 260 | return false; |
| 261 | } |
| 262 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 263 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 264 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 265 | const map<string, HashableDimensionKey>& conditionKeys, bool condition, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 266 | const LogEvent& event, bool scheduledPull) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 267 | flushIfNeededLocked(event.GetTimestampNs()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 268 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 269 | if (matcherIndex == mStopAllIndex) { |
| 270 | for (auto& pair : mCurrentSlicedDuration) { |
| 271 | pair.second->noteStopAll(event.GetTimestampNs()); |
| 272 | } |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | HashableDimensionKey atomKey = getHashableKey(getDimensionKey(event, mInternalDimension)); |
| 277 | |
| 278 | if (mCurrentSlicedDuration.find(eventKey) == mCurrentSlicedDuration.end()) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 279 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 280 | return; |
| 281 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 282 | mCurrentSlicedDuration[eventKey] = createDurationTracker(eventKey, mPastBuckets[eventKey]); |
| 283 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 284 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 285 | auto it = mCurrentSlicedDuration.find(eventKey); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 286 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 287 | if (matcherIndex == mStartIndex) { |
| 288 | it->second->noteStart(atomKey, condition, event.GetTimestampNs(), conditionKeys); |
| 289 | } else if (matcherIndex == mStopIndex) { |
| 290 | it->second->noteStop(atomKey, event.GetTimestampNs(), false); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 291 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 294 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 295 | size_t totalSize = 0; |
| 296 | for (const auto& pair : mPastBuckets) { |
| 297 | totalSize += pair.second.size() * kBucketSize; |
| 298 | } |
| 299 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 302 | } // namespace statsd |
| 303 | } // namespace os |
| 304 | } // namespace android |