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 |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 54 | const int FIELD_ID_START_BUCKET_ELAPSED_NANOS = 1; |
| 55 | const int FIELD_ID_END_BUCKET_ELAPSED_NANOS = 2; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 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, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 64 | const int64_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), |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 70 | mNested(nesting), |
| 71 | mContainANYPositionInInternalDimensions(false) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 72 | // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract |
| 73 | // them in the base class, because the proto generated CountMetric, and DurationMetric are |
| 74 | // not related. Maybe we should add a template in the future?? |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 75 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 76 | mBucketSizeNs = |
| 77 | TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 78 | } else { |
| 79 | mBucketSizeNs = LLONG_MAX; |
| 80 | } |
| 81 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 82 | if (metric.has_dimensions_in_what()) { |
| 83 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 84 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (internalDimensions.has_field()) { |
| 88 | translateFieldMatcher(internalDimensions, &mInternalDimensions); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 89 | mContainANYPositionInInternalDimensions = HasPositionANY(internalDimensions); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 90 | } |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 91 | if (mContainANYPositionInInternalDimensions) { |
| 92 | ALOGE("Position ANY in internal dimension not supported."); |
| 93 | } |
| 94 | if (mContainANYPositionInDimensionsInWhat) { |
| 95 | ALOGE("Position ANY in dimension_in_what not supported."); |
| 96 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 97 | |
| 98 | if (metric.has_dimensions_in_condition()) { |
| 99 | translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition); |
| 100 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 101 | |
| 102 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 103 | for (const auto& link : metric.links()) { |
| 104 | Metric2Condition mc; |
| 105 | mc.conditionId = link.condition(); |
| 106 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 107 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 108 | mMetric2ConditionLinks.push_back(mc); |
| 109 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 110 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 111 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 112 | mUnSlicedPartCondition = ConditionState::kUnknown; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 113 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 114 | mUseWhatDimensionAsInternalDimension = equalDimensions(mDimensionsInWhat, mInternalDimensions); |
| 115 | if (mWizard != nullptr && mConditionTrackerIndex >= 0) { |
| 116 | mSameConditionDimensionsInTracker = |
| 117 | mWizard->equalOutputDimensions(mConditionTrackerIndex, mDimensionsInCondition); |
| 118 | if (mMetric2ConditionLinks.size() == 1) { |
| 119 | mHasLinksToAllConditionDimensionsInTracker = |
| 120 | mWizard->equalOutputDimensions(mConditionTrackerIndex, |
| 121 | mMetric2ConditionLinks.begin()->conditionFields); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 122 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 123 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 124 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame^] | 125 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | DurationMetricProducer::~DurationMetricProducer() { |
| 129 | VLOG("~DurationMetric() called"); |
| 130 | } |
| 131 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 132 | sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker( |
| 133 | const Alert &alert, const sp<AlarmMonitor>& anomalyAlarmMonitor) { |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 134 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | 423f753 | 2018-03-08 15:45:14 -0800 | [diff] [blame] | 135 | if (mAggregationType == DurationMetric_AggregationType_SUM) { |
| 136 | if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) { |
| 137 | ALOGW("invalid alert for SUM: threshold (%f) > possible recordable value (%d x %lld)", |
| 138 | alert.trigger_if_sum_gt(), alert.num_buckets(), (long long)mBucketSizeNs); |
| 139 | return nullptr; |
| 140 | } |
| 141 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 142 | sp<DurationAnomalyTracker> anomalyTracker = |
| 143 | new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 144 | if (anomalyTracker != nullptr) { |
| 145 | mAnomalyTrackers.push_back(anomalyTracker); |
| 146 | } |
| 147 | return anomalyTracker; |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 150 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 151 | const MetricDimensionKey& eventKey) const { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 152 | switch (mAggregationType) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 153 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 154 | return make_unique<OringDurationTracker>( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 155 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 156 | mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame^] | 157 | mTimeBaseNs, mBucketSizeNs, mConditionSliced, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 158 | mHasLinksToAllConditionDimensionsInTracker, mAnomalyTrackers); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 159 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 160 | return make_unique<MaxDurationTracker>( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 161 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 162 | mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame^] | 163 | mTimeBaseNs, mBucketSizeNs, mConditionSliced, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 164 | mHasLinksToAllConditionDimensionsInTracker, mAnomalyTrackers); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // SlicedConditionChange optimization case 1: |
| 169 | // 1. If combination condition, logical operation is AND, only one sliced child predicate. |
| 170 | // 2. No condition in dimension |
| 171 | // 3. The links covers all dimension fields in the sliced child condition predicate. |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 172 | void DurationMetricProducer::onSlicedConditionMayChangeLocked_opt1(bool condition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 173 | const int64_t eventTime) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 174 | if (mMetric2ConditionLinks.size() != 1 || |
| 175 | !mHasLinksToAllConditionDimensionsInTracker || |
| 176 | !mDimensionsInCondition.empty()) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | bool currentUnSlicedPartCondition = true; |
| 181 | if (!mWizard->IsSimpleCondition(mConditionTrackerIndex)) { |
| 182 | ConditionState unslicedPartState = |
| 183 | mWizard->getUnSlicedPartConditionState(mConditionTrackerIndex); |
| 184 | // When the unsliced part is still false, return directly. |
| 185 | if (mUnSlicedPartCondition == ConditionState::kFalse && |
| 186 | unslicedPartState == ConditionState::kFalse) { |
| 187 | return; |
| 188 | } |
| 189 | mUnSlicedPartCondition = unslicedPartState; |
| 190 | currentUnSlicedPartCondition = mUnSlicedPartCondition > 0; |
| 191 | } |
| 192 | |
| 193 | auto dimensionsChangedToTrue = mWizard->getChangedToTrueDimensions(mConditionTrackerIndex); |
| 194 | auto dimensionsChangedToFalse = mWizard->getChangedToFalseDimensions(mConditionTrackerIndex); |
| 195 | |
| 196 | // The condition change is from the unsliced predicates. |
| 197 | // We need to find out the true dimensions from the sliced predicate and flip their condition |
| 198 | // state based on the new unsliced condition state. |
| 199 | if (dimensionsChangedToTrue == nullptr || dimensionsChangedToFalse == nullptr || |
| 200 | (dimensionsChangedToTrue->empty() && dimensionsChangedToFalse->empty())) { |
| 201 | std::set<HashableDimensionKey> trueConditionDimensions; |
| 202 | mWizard->getTrueSlicedDimensions(mConditionTrackerIndex, &trueConditionDimensions); |
| 203 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 204 | HashableDimensionKey linkedConditionDimensionKey; |
| 205 | getDimensionForCondition(whatIt.first.getValues(), |
| 206 | mMetric2ConditionLinks[0], |
| 207 | &linkedConditionDimensionKey); |
| 208 | if (trueConditionDimensions.find(linkedConditionDimensionKey) != |
| 209 | trueConditionDimensions.end()) { |
| 210 | for (auto& condIt : whatIt.second) { |
| 211 | condIt.second->onConditionChanged( |
| 212 | currentUnSlicedPartCondition, eventTime); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } else { |
| 217 | // Handle the condition change from the sliced predicate. |
| 218 | if (currentUnSlicedPartCondition) { |
| 219 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 220 | HashableDimensionKey linkedConditionDimensionKey; |
| 221 | getDimensionForCondition(whatIt.first.getValues(), |
| 222 | mMetric2ConditionLinks[0], |
| 223 | &linkedConditionDimensionKey); |
| 224 | if (dimensionsChangedToTrue->find(linkedConditionDimensionKey) != |
| 225 | dimensionsChangedToTrue->end()) { |
| 226 | for (auto& condIt : whatIt.second) { |
| 227 | condIt.second->onConditionChanged(true, eventTime); |
| 228 | } |
| 229 | } |
| 230 | if (dimensionsChangedToFalse->find(linkedConditionDimensionKey) != |
| 231 | dimensionsChangedToFalse->end()) { |
| 232 | for (auto& condIt : whatIt.second) { |
| 233 | condIt.second->onConditionChanged(false, eventTime); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | // SlicedConditionChange optimization case 2: |
| 243 | // 1. If combination condition, logical operation is AND, only one sliced child predicate. |
| 244 | // 2. Has dimensions_in_condition and it equals to the output dimensions of the sliced predicate. |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 245 | void DurationMetricProducer::onSlicedConditionMayChangeLocked_opt2(bool condition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 246 | const int64_t eventTime) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 247 | if (mMetric2ConditionLinks.size() > 1 || !mSameConditionDimensionsInTracker) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | auto dimensionsChangedToTrue = mWizard->getChangedToTrueDimensions(mConditionTrackerIndex); |
| 252 | auto dimensionsChangedToFalse = mWizard->getChangedToFalseDimensions(mConditionTrackerIndex); |
| 253 | |
| 254 | bool currentUnSlicedPartCondition = true; |
| 255 | if (!mWizard->IsSimpleCondition(mConditionTrackerIndex)) { |
| 256 | ConditionState unslicedPartState = |
| 257 | mWizard->getUnSlicedPartConditionState(mConditionTrackerIndex); |
| 258 | // When the unsliced part is still false, return directly. |
| 259 | if (mUnSlicedPartCondition == ConditionState::kFalse && |
| 260 | unslicedPartState == ConditionState::kFalse) { |
| 261 | return; |
| 262 | } |
| 263 | mUnSlicedPartCondition = unslicedPartState; |
| 264 | currentUnSlicedPartCondition = mUnSlicedPartCondition > 0; |
| 265 | } |
| 266 | |
| 267 | const std::set<HashableDimensionKey>* trueDimensionsToProcess = nullptr; |
| 268 | const std::set<HashableDimensionKey>* falseDimensionsToProcess = nullptr; |
| 269 | |
| 270 | std::set<HashableDimensionKey> currentTrueConditionDimensions; |
| 271 | if (dimensionsChangedToTrue == nullptr || dimensionsChangedToFalse == nullptr || |
| 272 | (dimensionsChangedToTrue->empty() && dimensionsChangedToFalse->empty())) { |
| 273 | mWizard->getTrueSlicedDimensions(mConditionTrackerIndex, ¤tTrueConditionDimensions); |
| 274 | trueDimensionsToProcess = ¤tTrueConditionDimensions; |
| 275 | } else if (currentUnSlicedPartCondition) { |
| 276 | // Handles the condition change from the sliced predicate. If the unsliced condition state |
| 277 | // is not true, not need to do anything. |
| 278 | trueDimensionsToProcess = dimensionsChangedToTrue; |
| 279 | falseDimensionsToProcess = dimensionsChangedToFalse; |
| 280 | } |
| 281 | |
| 282 | if (trueDimensionsToProcess == nullptr && falseDimensionsToProcess == nullptr) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 287 | if (falseDimensionsToProcess != nullptr) { |
| 288 | for (const auto& changedDim : *falseDimensionsToProcess) { |
| 289 | auto condIt = whatIt.second.find(changedDim); |
| 290 | if (condIt != whatIt.second.end()) { |
| 291 | condIt->second->onConditionChanged(false, eventTime); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | if (trueDimensionsToProcess != nullptr) { |
| 296 | HashableDimensionKey linkedConditionDimensionKey; |
| 297 | if (!trueDimensionsToProcess->empty() && mMetric2ConditionLinks.size() == 1) { |
| 298 | getDimensionForCondition(whatIt.first.getValues(), |
| 299 | mMetric2ConditionLinks[0], |
| 300 | &linkedConditionDimensionKey); |
| 301 | } |
| 302 | for (auto& trueDim : *trueDimensionsToProcess) { |
| 303 | auto condIt = whatIt.second.find(trueDim); |
| 304 | if (condIt != whatIt.second.end()) { |
| 305 | condIt->second->onConditionChanged( |
| 306 | currentUnSlicedPartCondition, eventTime); |
| 307 | } else { |
| 308 | if (mMetric2ConditionLinks.size() == 0 || |
| 309 | trueDim.contains(linkedConditionDimensionKey)) { |
| 310 | if (!whatIt.second.empty()) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 311 | auto newEventKey = MetricDimensionKey(whatIt.first, trueDim); |
| 312 | if (hitGuardRailLocked(newEventKey)) { |
| 313 | continue; |
| 314 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 315 | unique_ptr<DurationTracker> newTracker = |
| 316 | whatIt.second.begin()->second->clone(eventTime); |
| 317 | if (newTracker != nullptr) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 318 | newTracker->setEventKey(newEventKey); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 319 | newTracker->onConditionChanged(true, eventTime); |
| 320 | whatIt.second[trueDim] = std::move(newTracker); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 330 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 331 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 332 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 333 | flushIfNeededLocked(eventTime); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 334 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 335 | if (!mConditionSliced) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | bool changeDimTrackable = mWizard->IsChangedDimensionTrackable(mConditionTrackerIndex); |
| 340 | if (changeDimTrackable && mHasLinksToAllConditionDimensionsInTracker && |
| 341 | mDimensionsInCondition.empty()) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 342 | onSlicedConditionMayChangeLocked_opt1(overallCondition, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | |
| 346 | if (changeDimTrackable && mSameConditionDimensionsInTracker && |
| 347 | mMetric2ConditionLinks.size() <= 1) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 348 | onSlicedConditionMayChangeLocked_opt2(overallCondition, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 352 | // Now for each of the on-going event, check if the condition has changed for them. |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 353 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 354 | for (auto& pair : whatIt.second) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 355 | pair.second->onSlicedConditionMayChange(overallCondition, eventTime); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 356 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 357 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 358 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 359 | if (mDimensionsInCondition.empty()) { |
| 360 | return; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 361 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 362 | |
| 363 | if (mMetric2ConditionLinks.empty()) { |
| 364 | std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet; |
| 365 | mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 366 | !mSameConditionDimensionsInTracker, |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 367 | &conditionDimensionsKeySet); |
| 368 | for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 369 | for (const auto& pair : whatIt.second) { |
| 370 | conditionDimensionsKeySet.erase(pair.first); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 371 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 372 | } |
| 373 | for (const auto& conditionDimension : conditionDimensionsKeySet) { |
| 374 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 375 | if (!whatIt.second.empty()) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 376 | auto newEventKey = MetricDimensionKey(whatIt.first, conditionDimension); |
| 377 | if (hitGuardRailLocked(newEventKey)) { |
| 378 | continue; |
| 379 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 380 | unique_ptr<DurationTracker> newTracker = |
| 381 | whatIt.second.begin()->second->clone(eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 382 | if (newTracker != nullptr) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 383 | newTracker->setEventKey(MetricDimensionKey(newEventKey)); |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 384 | newTracker->onSlicedConditionMayChange(overallCondition, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 385 | whatIt.second[conditionDimension] = std::move(newTracker); |
| 386 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | } else { |
| 391 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 392 | ConditionKey conditionKey; |
| 393 | for (const auto& link : mMetric2ConditionLinks) { |
| 394 | getDimensionForCondition(whatIt.first.getValues(), link, |
| 395 | &conditionKey[link.conditionId]); |
| 396 | } |
| 397 | std::unordered_set<HashableDimensionKey> conditionDimensionsKeys; |
| 398 | mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 399 | !mSameConditionDimensionsInTracker, |
| 400 | !mHasLinksToAllConditionDimensionsInTracker, |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 401 | &conditionDimensionsKeys); |
| 402 | |
| 403 | for (const auto& conditionDimension : conditionDimensionsKeys) { |
| 404 | if (!whatIt.second.empty() && |
| 405 | whatIt.second.find(conditionDimension) == whatIt.second.end()) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 406 | auto newEventKey = MetricDimensionKey(whatIt.first, conditionDimension); |
| 407 | if (hitGuardRailLocked(newEventKey)) { |
| 408 | continue; |
| 409 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 410 | auto newTracker = whatIt.second.begin()->second->clone(eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 411 | if (newTracker != nullptr) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 412 | newTracker->setEventKey(newEventKey); |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 413 | newTracker->onSlicedConditionMayChange(overallCondition, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 414 | whatIt.second[conditionDimension] = std::move(newTracker); |
| 415 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 422 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 423 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 424 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 425 | mCondition = conditionMet; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 426 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 427 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 428 | // change, instead of using current time. |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 429 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 430 | for (auto& pair : whatIt.second) { |
| 431 | pair.second->onConditionChanged(conditionMet, eventTime); |
| 432 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 436 | void DurationMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 437 | flushIfNeededLocked(dropTimeNs); |
| 438 | mPastBuckets.clear(); |
| 439 | } |
| 440 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 441 | void DurationMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 442 | const bool include_current_partial_bucket, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 443 | ProtoOutputStream* protoOutput) { |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 444 | if (include_current_partial_bucket) { |
| 445 | flushLocked(dumpTimeNs); |
| 446 | } else { |
| 447 | flushIfNeededLocked(dumpTimeNs); |
| 448 | } |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 449 | if (mPastBuckets.empty()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 450 | VLOG(" Duration metric, empty return"); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 451 | return; |
| 452 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 453 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 454 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 455 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 456 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 457 | VLOG("Duration metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 458 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 459 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 460 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 461 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 462 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 463 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 464 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 465 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 466 | // First fill dimension. |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 467 | uint64_t dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 468 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 469 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 470 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 471 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 472 | if (dimensionKey.hasDimensionKeyInCondition()) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 473 | uint64_t dimensionInConditionToken = protoOutput->start( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 474 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 475 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 476 | protoOutput->end(dimensionInConditionToken); |
| 477 | } |
| 478 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 479 | // Then fill bucket_info (DurationBucketInfo). |
| 480 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 481 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 482 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 483 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_NANOS, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 484 | (long long)bucket.mBucketStartNs); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 485 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_NANOS, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 486 | (long long)bucket.mBucketEndNs); |
| 487 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 488 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 489 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 490 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 491 | } |
| 492 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 493 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 494 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 495 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 496 | protoOutput->end(protoToken); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 497 | mPastBuckets.clear(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 498 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 499 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 500 | void DurationMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 501 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 502 | |
| 503 | if (currentBucketEndTimeNs > eventTimeNs) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 504 | return; |
| 505 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 506 | VLOG("flushing..........."); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 507 | for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin(); |
| 508 | whatIt != mCurrentSlicedDurationTrackerMap.end();) { |
| 509 | for (auto it = whatIt->second.begin(); it != whatIt->second.end();) { |
| 510 | if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 511 | VLOG("erase bucket for key %s %s", |
| 512 | whatIt->first.toString().c_str(), it->first.toString().c_str()); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 513 | it = whatIt->second.erase(it); |
| 514 | } else { |
| 515 | ++it; |
| 516 | } |
| 517 | } |
| 518 | if (whatIt->second.empty()) { |
| 519 | whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 520 | } else { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 521 | whatIt++; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 524 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 525 | int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 526 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 527 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 530 | void DurationMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 531 | for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin(); |
| 532 | whatIt != mCurrentSlicedDurationTrackerMap.end();) { |
| 533 | for (auto it = whatIt->second.begin(); it != whatIt->second.end();) { |
| 534 | if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 535 | VLOG("erase bucket for key %s %s", whatIt->first.toString().c_str(), |
| 536 | it->first.toString().c_str()); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 537 | it = whatIt->second.erase(it); |
| 538 | } else { |
| 539 | ++it; |
| 540 | } |
| 541 | } |
| 542 | if (whatIt->second.empty()) { |
| 543 | whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 544 | } else { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 545 | whatIt++; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 550 | void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 551 | if (mCurrentSlicedDurationTrackerMap.size() == 0) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
| 555 | fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 556 | (unsigned long)mCurrentSlicedDurationTrackerMap.size()); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 557 | if (verbose) { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 558 | for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 559 | for (const auto& slice : whatIt.second) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 560 | fprintf(out, "\t(what)%s\t(condition)%s\n", whatIt.first.toString().c_str(), |
| 561 | slice.first.toString().c_str()); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 562 | slice.second->dumpStates(out, verbose); |
| 563 | } |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 568 | bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 569 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(newKey.getDimensionKeyInWhat()); |
| 570 | if (whatIt != mCurrentSlicedDurationTrackerMap.end()) { |
| 571 | auto condIt = whatIt->second.find(newKey.getDimensionKeyInCondition()); |
| 572 | if (condIt != whatIt->second.end()) { |
| 573 | return false; |
| 574 | } |
| 575 | if (whatIt->second.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 576 | size_t newTupleCount = whatIt->second.size() + 1; |
| 577 | StatsdStats::getInstance().noteMetricDimensionInConditionSize( |
| 578 | mConfigKey, mMetricId, newTupleCount); |
| 579 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 580 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
| 581 | ALOGE("DurationMetric %lld dropping data for condition dimension key %s", |
| 582 | (long long)mMetricId, newKey.getDimensionKeyInCondition().toString().c_str()); |
| 583 | return true; |
| 584 | } |
| 585 | } |
| 586 | } else { |
| 587 | // 1. Report the tuple count if the tuple count > soft limit |
| 588 | if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 589 | size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1; |
| 590 | StatsdStats::getInstance().noteMetricDimensionSize( |
| 591 | mConfigKey, mMetricId, newTupleCount); |
| 592 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 593 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
| 594 | ALOGE("DurationMetric %lld dropping data for what dimension key %s", |
| 595 | (long long)mMetricId, newKey.getDimensionKeyInWhat().toString().c_str()); |
| 596 | return true; |
| 597 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 603 | void DurationMetricProducer::handleStartEvent(const MetricDimensionKey& eventKey, |
| 604 | const ConditionKey& conditionKeys, |
| 605 | bool condition, const LogEvent& event) { |
| 606 | const auto& whatKey = eventKey.getDimensionKeyInWhat(); |
| 607 | const auto& condKey = eventKey.getDimensionKeyInCondition(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 608 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 609 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey); |
| 610 | if (whatIt == mCurrentSlicedDurationTrackerMap.end()) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 611 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 612 | return; |
| 613 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 614 | mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey); |
| 615 | } else { |
| 616 | if (whatIt->second.find(condKey) == whatIt->second.end()) { |
| 617 | if (hitGuardRailLocked(eventKey)) { |
| 618 | return; |
| 619 | } |
| 620 | mCurrentSlicedDurationTrackerMap[whatKey][condKey] = createDurationTracker(eventKey); |
| 621 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 622 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 623 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 624 | auto it = mCurrentSlicedDurationTrackerMap.find(whatKey)->second.find(condKey); |
| 625 | if (mUseWhatDimensionAsInternalDimension) { |
| 626 | it->second->noteStart(whatKey, condition, |
| 627 | event.GetElapsedTimestampNs(), conditionKeys); |
| 628 | return; |
| 629 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 630 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 631 | if (mInternalDimensions.empty()) { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 632 | it->second->noteStart(DEFAULT_DIMENSION_KEY, condition, |
| 633 | event.GetElapsedTimestampNs(), conditionKeys); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 634 | } else { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 635 | HashableDimensionKey dimensionKey = DEFAULT_DIMENSION_KEY; |
| 636 | filterValues(mInternalDimensions, event.getValues(), &dimensionKey); |
| 637 | it->second->noteStart( |
| 638 | dimensionKey, condition, event.GetElapsedTimestampNs(), conditionKeys); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 639 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 640 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 643 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
| 644 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
| 645 | const ConditionKey& conditionKeys, bool condition, |
| 646 | const LogEvent& event) { |
| 647 | ALOGW("Not used in duration tracker."); |
| 648 | } |
| 649 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 650 | void DurationMetricProducer::onMatchedLogEventLocked(const size_t matcherIndex, |
| 651 | const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 652 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame^] | 653 | if (eventTimeNs < mTimeBaseNs) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
| 657 | flushIfNeededLocked(event.GetElapsedTimestampNs()); |
| 658 | |
| 659 | // Handles Stopall events. |
| 660 | if (matcherIndex == mStopAllIndex) { |
| 661 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 662 | for (auto& pair : whatIt.second) { |
| 663 | pair.second->noteStopAll(event.GetElapsedTimestampNs()); |
| 664 | } |
| 665 | } |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | HashableDimensionKey dimensionInWhat; |
| 670 | if (!mDimensionsInWhat.empty()) { |
| 671 | filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhat); |
| 672 | } else { |
| 673 | dimensionInWhat = DEFAULT_DIMENSION_KEY; |
| 674 | } |
| 675 | |
| 676 | // Handles Stop events. |
| 677 | if (matcherIndex == mStopIndex) { |
| 678 | if (mUseWhatDimensionAsInternalDimension) { |
| 679 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat); |
| 680 | if (whatIt != mCurrentSlicedDurationTrackerMap.end()) { |
| 681 | for (const auto& condIt : whatIt->second) { |
| 682 | condIt.second->noteStop(dimensionInWhat, event.GetElapsedTimestampNs(), false); |
| 683 | } |
| 684 | } |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | HashableDimensionKey internalDimensionKey = DEFAULT_DIMENSION_KEY; |
| 689 | if (!mInternalDimensions.empty()) { |
| 690 | filterValues(mInternalDimensions, event.getValues(), &internalDimensionKey); |
| 691 | } |
| 692 | |
| 693 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat); |
| 694 | if (whatIt != mCurrentSlicedDurationTrackerMap.end()) { |
| 695 | for (const auto& condIt : whatIt->second) { |
| 696 | condIt.second->noteStop( |
| 697 | internalDimensionKey, event.GetElapsedTimestampNs(), false); |
| 698 | } |
| 699 | } |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | bool condition; |
| 704 | ConditionKey conditionKey; |
| 705 | std::unordered_set<HashableDimensionKey> dimensionKeysInCondition; |
| 706 | if (mConditionSliced) { |
| 707 | for (const auto& link : mMetric2ConditionLinks) { |
| 708 | getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]); |
| 709 | } |
| 710 | |
| 711 | auto conditionState = |
| 712 | mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition, |
| 713 | !mSameConditionDimensionsInTracker, |
| 714 | !mHasLinksToAllConditionDimensionsInTracker, |
| 715 | &dimensionKeysInCondition); |
| 716 | condition = (conditionState == ConditionState::kTrue); |
| 717 | if (mDimensionsInCondition.empty() && condition) { |
| 718 | dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY); |
| 719 | } |
| 720 | } else { |
| 721 | condition = mCondition; |
| 722 | if (condition) { |
| 723 | dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (dimensionKeysInCondition.empty()) { |
| 728 | handleStartEvent(MetricDimensionKey(dimensionInWhat, DEFAULT_DIMENSION_KEY), |
| 729 | conditionKey, condition, event); |
| 730 | } else { |
| 731 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat); |
| 732 | // If the what dimension is already there, we should update all the trackers even |
| 733 | // the condition is false. |
| 734 | if (whatIt != mCurrentSlicedDurationTrackerMap.end()) { |
| 735 | for (const auto& condIt : whatIt->second) { |
| 736 | const bool cond = dimensionKeysInCondition.find(condIt.first) != |
| 737 | dimensionKeysInCondition.end(); |
| 738 | handleStartEvent(MetricDimensionKey(dimensionInWhat, condIt.first), |
| 739 | conditionKey, cond, event); |
| 740 | dimensionKeysInCondition.erase(condIt.first); |
| 741 | } |
| 742 | } |
| 743 | for (const auto& conditionDimension : dimensionKeysInCondition) { |
| 744 | handleStartEvent(MetricDimensionKey(dimensionInWhat, conditionDimension), conditionKey, |
| 745 | condition, event); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 750 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 751 | size_t totalSize = 0; |
| 752 | for (const auto& pair : mPastBuckets) { |
| 753 | totalSize += pair.second.size() * kBucketSize; |
| 754 | } |
| 755 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 758 | } // namespace statsd |
| 759 | } // namespace os |
| 760 | } // namespace android |