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