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_DURATION_METRICS = 6; |
| 47 | // for DurationMetricDataWrapper |
| 48 | const int FIELD_ID_DATA = 1; |
| 49 | // for DurationMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 50 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 51 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 52 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 53 | // for DurationBucketInfo |
| 54 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 55 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 56 | const int FIELD_ID_DURATION = 3; |
| 57 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 58 | DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 59 | const int conditionIndex, const size_t startIndex, |
| 60 | const size_t stopIndex, const size_t stopAllIndex, |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 61 | const bool nesting, |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 62 | const sp<ConditionWizard>& wizard, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 63 | const FieldMatcher& internalDimensions, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 64 | const uint64_t startTimeNs) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 65 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 66 | mAggregationType(metric.aggregation_type()), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 67 | mStartIndex(startIndex), |
| 68 | mStopIndex(stopIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 69 | mStopAllIndex(stopAllIndex), |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 70 | mNested(nesting) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 71 | // 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-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 74 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame^] | 75 | mBucketSizeNs = |
| 76 | TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 77 | } else { |
| 78 | mBucketSizeNs = LLONG_MAX; |
| 79 | } |
| 80 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 81 | 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 Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 92 | |
| 93 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 94 | 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 Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 101 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 102 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 103 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 104 | 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] | 105 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 106 | } |
| 107 | |
| 108 | DurationMetricProducer::~DurationMetricProducer() { |
| 109 | VLOG("~DurationMetric() called"); |
| 110 | } |
| 111 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 112 | sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker(const Alert &alert) { |
| 113 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 114 | if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) { |
| 115 | ALOGW("invalid alert: threshold (%f) > possible recordable value (%d x %lld)", |
| 116 | alert.trigger_if_sum_gt(), alert.num_buckets(), |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 117 | (long long)mBucketSizeNs); |
| 118 | return nullptr; |
| 119 | } |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 120 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, mConfigKey); |
| 121 | if (anomalyTracker != nullptr) { |
| 122 | mAnomalyTrackers.push_back(anomalyTracker); |
| 123 | } |
| 124 | return anomalyTracker; |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 127 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 128 | const MetricDimensionKey& eventKey) const { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 129 | switch (mAggregationType) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 130 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 131 | return make_unique<OringDurationTracker>( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 132 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 133 | mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum, |
| 134 | mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 135 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 136 | return make_unique<MaxDurationTracker>( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 137 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 138 | mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum, |
| 139 | mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 143 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 144 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 145 | flushIfNeededLocked(eventTime); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 146 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 147 | // Now for each of the on-going event, check if the condition has changed for them. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 148 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 149 | pair.second->onSlicedConditionMayChange(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 150 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 151 | |
| 152 | |
| 153 | std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet; |
| 154 | ConditionState conditionState = mWizard->getMetConditionDimension( |
| 155 | mConditionTrackerIndex, mDimensionsInCondition, &conditionDimensionsKeySet); |
| 156 | |
| 157 | bool condition = (conditionState == ConditionState::kTrue); |
| 158 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
| 159 | conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition()); |
| 160 | } |
| 161 | std::unordered_set<MetricDimensionKey> newKeys; |
| 162 | for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) { |
| 163 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
| 164 | auto newKey = |
| 165 | MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey); |
| 166 | if (newKeys.find(newKey) == newKeys.end()) { |
| 167 | mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime); |
| 168 | mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey); |
| 169 | mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime); |
| 170 | } |
| 171 | newKeys.insert(newKey); |
| 172 | } |
| 173 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 176 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 177 | const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 178 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 179 | mCondition = conditionMet; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 180 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 181 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 182 | // change, instead of using current time. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 183 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 184 | pair.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 188 | void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 189 | ProtoOutputStream* protoOutput) { |
| 190 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 191 | if (mPastBuckets.empty()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 192 | VLOG(" Duration metric, empty return"); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 193 | return; |
| 194 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 195 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 196 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 197 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
| 198 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 199 | VLOG("Duration metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 200 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 201 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 202 | const MetricDimensionKey& dimensionKey = pair.first; |
| 203 | VLOG(" dimension key %s", dimensionKey.c_str()); |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 204 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 205 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 206 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 207 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 208 | // First fill dimension. |
| 209 | long long dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 210 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 211 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 212 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 213 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 214 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 215 | long long dimensionInConditionToken = protoOutput->start( |
| 216 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 217 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 218 | protoOutput->end(dimensionInConditionToken); |
| 219 | } |
| 220 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 221 | // Then fill bucket_info (DurationBucketInfo). |
| 222 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 223 | long long bucketInfoToken = protoOutput->start( |
| 224 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 225 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 226 | (long long)bucket.mBucketStartNs); |
| 227 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 228 | (long long)bucket.mBucketEndNs); |
| 229 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 230 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 231 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 232 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 233 | } |
| 234 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 235 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 236 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 237 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 238 | protoOutput->end(protoToken); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 239 | mPastBuckets.clear(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 240 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 241 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 242 | void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
| 243 | uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 244 | |
| 245 | if (currentBucketEndTimeNs > eventTimeNs) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 248 | VLOG("flushing..........."); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 249 | for (auto it = mCurrentSlicedDurationTrackerMap.begin(); |
| 250 | it != mCurrentSlicedDurationTrackerMap.end();) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 251 | if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 252 | VLOG("erase bucket for key %s", it->first.c_str()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 253 | it = mCurrentSlicedDurationTrackerMap.erase(it); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 254 | } else { |
| 255 | ++it; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 258 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 259 | int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 260 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 261 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 262 | } |
| 263 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 264 | void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) { |
| 265 | for (auto it = mCurrentSlicedDurationTrackerMap.begin(); |
| 266 | it != mCurrentSlicedDurationTrackerMap.end();) { |
| 267 | if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) { |
| 268 | VLOG("erase bucket for key %s", it->first.c_str()); |
| 269 | it = mCurrentSlicedDurationTrackerMap.erase(it); |
| 270 | } else { |
| 271 | ++it; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 276 | void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 277 | if (mCurrentSlicedDurationTrackerMap.size() == 0) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
| 281 | fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 282 | (unsigned long)mCurrentSlicedDurationTrackerMap.size()); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 283 | if (verbose) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 284 | for (const auto& slice : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 285 | fprintf(out, "\t%s\n", slice.first.c_str()); |
| 286 | slice.second->dumpStates(out, verbose); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 291 | bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 292 | // the key is not new, we are good. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 293 | if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | // 1. Report the tuple count if the tuple count > soft limit |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 297 | if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 298 | size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 299 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 300 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 301 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 302 | ALOGE("DurationMetric %lld dropping data for dimension key %s", |
| 303 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 304 | return true; |
| 305 | } |
| 306 | } |
| 307 | return false; |
| 308 | } |
| 309 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 310 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 311 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 312 | const ConditionKey& conditionKeys, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 313 | const LogEvent& event) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 314 | flushIfNeededLocked(event.GetTimestampNs()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 315 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 316 | if (matcherIndex == mStopAllIndex) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 317 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 318 | pair.second->noteStopAll(event.GetTimestampNs()); |
| 319 | } |
| 320 | return; |
| 321 | } |
| 322 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 323 | if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 324 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 325 | return; |
| 326 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 327 | mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 328 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 329 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 330 | auto it = mCurrentSlicedDurationTrackerMap.find(eventKey); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 331 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 332 | std::vector<HashableDimensionKey> values; |
| 333 | filterValues(mInternalDimensions, event.getValues(), &values); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 334 | if (values.empty()) { |
| 335 | if (matcherIndex == mStartIndex) { |
| 336 | it->second->noteStart(DEFAULT_DIMENSION_KEY, condition, |
| 337 | event.GetTimestampNs(), conditionKeys); |
| 338 | } else if (matcherIndex == mStopIndex) { |
| 339 | it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetTimestampNs(), false); |
| 340 | } |
| 341 | } else { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 342 | for (const auto& value : values) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 343 | if (matcherIndex == mStartIndex) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 344 | it->second->noteStart(value, condition, event.GetTimestampNs(), conditionKeys); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 345 | } else if (matcherIndex == mStopIndex) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 346 | it->second->noteStop(value, event.GetTimestampNs(), false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 349 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 350 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 353 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 354 | size_t totalSize = 0; |
| 355 | for (const auto& pair : mPastBuckets) { |
| 356 | totalSize += pair.second.size() * kBucketSize; |
| 357 | } |
| 358 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 361 | } // namespace statsd |
| 362 | } // namespace os |
| 363 | } // namespace android |