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" |
| 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 | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 71 | : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard), |
| 72 | mAggregationType(metric.aggregation_type()), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 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-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 96 | 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] | 97 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 98 | } |
| 99 | |
| 100 | DurationMetricProducer::~DurationMetricProducer() { |
| 101 | VLOG("~DurationMetric() called"); |
| 102 | } |
| 103 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 104 | sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) { |
| 105 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 106 | if (alert.trigger_if_sum_gt() > alert.number_of_buckets() * mBucketSizeNs) { |
| 107 | ALOGW("invalid alert: threshold (%lld) > possible recordable value (%d x %lld)", |
| 108 | alert.trigger_if_sum_gt(), alert.number_of_buckets(), |
| 109 | (long long)mBucketSizeNs); |
| 110 | return nullptr; |
| 111 | } |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 112 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey); |
| 113 | if (anomalyTracker != nullptr) { |
| 114 | mAnomalyTrackers.push_back(anomalyTracker); |
| 115 | } |
| 116 | return anomalyTracker; |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 119 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 120 | const HashableDimensionKey& eventKey) const { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 121 | switch (mAggregationType) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 122 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 123 | return make_unique<OringDurationTracker>( |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 124 | mConfigKey, mName, eventKey, mWizard, mConditionTrackerIndex, mNested, |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 125 | mCurrentBucketStartTimeNs, mBucketSizeNs, mAnomalyTrackers); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 126 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 127 | return make_unique<MaxDurationTracker>( |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 128 | mConfigKey, mName, eventKey, mWizard, mConditionTrackerIndex, mNested, |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 129 | mCurrentBucketStartTimeNs, mBucketSizeNs, mAnomalyTrackers); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 133 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 134 | VLOG("Metric %s onSlicedConditionMayChange", mName.c_str()); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 135 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 136 | // Now for each of the on-going event, check if the condition has changed for them. |
| 137 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 138 | pair.second->onSlicedConditionMayChange(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 142 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 143 | const uint64_t eventTime) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 144 | VLOG("Metric %s onConditionChanged", mName.c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 145 | mCondition = conditionMet; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 146 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 147 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 148 | // change, instead of using current time. |
| 149 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 150 | pair.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 154 | void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 155 | ProtoOutputStream* protoOutput) { |
| 156 | flushIfNeededLocked(dumpTimeNs); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 157 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 158 | protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 159 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 160 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
| 161 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 162 | VLOG("metric %s dump report now...", mName.c_str()); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 163 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 164 | for (const auto& pair : mPastBuckets) { |
| 165 | const HashableDimensionKey& hashableKey = pair.first; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 166 | const vector<KeyValuePair>& kvs = hashableKey.getKeyValuePairs(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 167 | VLOG(" dimension key %s", hashableKey.c_str()); |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 168 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 169 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 170 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 171 | |
| 172 | // First fill dimension (KeyValuePairs). |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 173 | for (const auto& kv : kvs) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 174 | long long dimensionToken = protoOutput->start( |
| 175 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION); |
| 176 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 177 | if (kv.has_value_str()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 178 | protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 179 | } else if (kv.has_value_int()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 180 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 181 | } else if (kv.has_value_bool()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 182 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 183 | } else if (kv.has_value_float()) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 184 | protoOutput->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 185 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 186 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Then fill bucket_info (DurationBucketInfo). |
| 190 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 191 | long long bucketInfoToken = protoOutput->start( |
| 192 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 193 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 194 | (long long)bucket.mBucketStartNs); |
| 195 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 196 | (long long)bucket.mBucketEndNs); |
| 197 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 198 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 199 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 200 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 201 | } |
| 202 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 203 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 204 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 205 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 206 | protoOutput->end(protoToken); |
| 207 | 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] | 208 | mPastBuckets.clear(); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 209 | mStartTimeNs = mCurrentBucketStartTimeNs; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 210 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 211 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 212 | void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTime) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 213 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) { |
| 214 | return; |
| 215 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 216 | VLOG("flushing..........."); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 217 | for (auto it = mCurrentSlicedDuration.begin(); it != mCurrentSlicedDuration.end();) { |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 218 | if (it->second->flushIfNeeded(eventTime, &mPastBuckets)) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 219 | VLOG("erase bucket for key %s", it->first.c_str()); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 220 | it = mCurrentSlicedDuration.erase(it); |
| 221 | } else { |
| 222 | ++it; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 225 | |
| 226 | int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
| 227 | mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 228 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 231 | bool DurationMetricProducer::hitGuardRailLocked(const HashableDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 232 | // the key is not new, we are good. |
| 233 | if (mCurrentSlicedDuration.find(newKey) != mCurrentSlicedDuration.end()) { |
| 234 | return false; |
| 235 | } |
| 236 | // 1. Report the tuple count if the tuple count > soft limit |
| 237 | if (mCurrentSlicedDuration.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 238 | size_t newTupleCount = mCurrentSlicedDuration.size() + 1; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 239 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 240 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 241 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 242 | ALOGE("DurationMetric %s dropping data for dimension key %s", mName.c_str(), |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 243 | newKey.c_str()); |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 250 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 251 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 252 | const map<string, HashableDimensionKey>& conditionKeys, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 253 | const LogEvent& event) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 254 | flushIfNeededLocked(event.GetTimestampNs()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 255 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 256 | if (matcherIndex == mStopAllIndex) { |
| 257 | for (auto& pair : mCurrentSlicedDuration) { |
| 258 | pair.second->noteStopAll(event.GetTimestampNs()); |
| 259 | } |
| 260 | return; |
| 261 | } |
| 262 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 263 | HashableDimensionKey atomKey(getDimensionKey(event, mInternalDimension)); |
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 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 274 | if (matcherIndex == mStartIndex) { |
| 275 | it->second->noteStart(atomKey, condition, event.GetTimestampNs(), conditionKeys); |
| 276 | } else if (matcherIndex == mStopIndex) { |
| 277 | it->second->noteStop(atomKey, event.GetTimestampNs(), false); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 278 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 281 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 282 | size_t totalSize = 0; |
| 283 | for (const auto& pair : mPastBuckets) { |
| 284 | totalSize += pair.second.size() * kBucketSize; |
| 285 | } |
| 286 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 289 | } // namespace statsd |
| 290 | } // namespace os |
| 291 | } // namespace android |