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