| Yao Chen | caf339d | 2017-10-06 16:01:10 -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 | */ |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 16 | |
| Yao Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 19 | #include "CombinationConditionTracker.h" |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 21 | #include <log/logprint.h> |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 27 | using std::map; |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::unique_ptr; |
| 30 | using std::unordered_map; |
| 31 | using std::vector; |
| 32 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 33 | CombinationConditionTracker::CombinationConditionTracker(const int64_t& id, const int index) |
| 34 | : ConditionTracker(id, index) { |
| 35 | VLOG("creating CombinationConditionTracker %lld", (long long)mConditionId); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | CombinationConditionTracker::~CombinationConditionTracker() { |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 39 | VLOG("~CombinationConditionTracker() %lld", (long long)mConditionId); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 42 | bool CombinationConditionTracker::init(const vector<Predicate>& allConditionConfig, |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 43 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 44 | const unordered_map<int64_t, int>& conditionIdIndexMap, |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 45 | vector<bool>& stack) { |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 46 | VLOG("Combination predicate init() %lld", (long long)mConditionId); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 47 | if (mInitialized) { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | // mark this node as visited in the recursion stack. |
| 52 | stack[mIndex] = true; |
| 53 | |
| Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 54 | Predicate_Combination combinationCondition = allConditionConfig[mIndex].combination(); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 55 | |
| 56 | if (!combinationCondition.has_operation()) { |
| 57 | return false; |
| 58 | } |
| 59 | mLogicalOperation = combinationCondition.operation(); |
| 60 | |
| Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 61 | if (mLogicalOperation == LogicalOperation::NOT && combinationCondition.predicate_size() != 1) { |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 65 | for (auto child : combinationCondition.predicate()) { |
| 66 | auto it = conditionIdIndexMap.find(child); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 67 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 68 | if (it == conditionIdIndexMap.end()) { |
| 69 | ALOGW("Predicate %lld not found in the config", (long long)child); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
| 73 | int childIndex = it->second; |
| 74 | const auto& childTracker = allConditionTrackers[childIndex]; |
| 75 | // if the child is a visited node in the recursion -> circle detected. |
| 76 | if (stack[childIndex]) { |
| 77 | ALOGW("Circle detected!!!"); |
| 78 | return false; |
| 79 | } |
| 80 | |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 81 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 82 | bool initChildSucceeded = childTracker->init(allConditionConfig, allConditionTrackers, |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 83 | conditionIdIndexMap, stack); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 84 | |
| 85 | if (!initChildSucceeded) { |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 86 | ALOGW("Child initialization failed %lld ", (long long)child); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } else { |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 89 | ALOGW("Child initialization success %lld ", (long long)child); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 92 | if (allConditionTrackers[childIndex]->isSliced()) { |
| 93 | setSliced(true); |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 94 | mSlicedChildren.push_back(childIndex); |
| 95 | } else { |
| 96 | mUnSlicedChildren.push_back(childIndex); |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 97 | } |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 98 | mChildren.push_back(childIndex); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 99 | mTrackerIndex.insert(childTracker->getLogTrackerIndex().begin(), |
| 100 | childTracker->getLogTrackerIndex().end()); |
| 101 | } |
| 102 | |
| 103 | // unmark this node in the recursion stack. |
| 104 | stack[mIndex] = false; |
| 105 | |
| 106 | mInitialized = true; |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 111 | void CombinationConditionTracker::isConditionMet( |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 112 | const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions, |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 113 | const std::vector<Matcher>& dimensionFields, |
| 114 | const bool isSubOutputDimensionFields, |
| 115 | const bool isPartialLink, |
| 116 | vector<ConditionState>& conditionCache, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 117 | std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const { |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 118 | // So far, this is fine as there is at most one child having sliced output. |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 119 | for (const int childIndex : mChildren) { |
| 120 | if (conditionCache[childIndex] == ConditionState::kNotEvaluated) { |
| 121 | allConditions[childIndex]->isConditionMet(conditionParameters, allConditions, |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 122 | dimensionFields, |
| 123 | isSubOutputDimensionFields, |
| 124 | isPartialLink, |
| 125 | conditionCache, |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 126 | dimensionsKeySet); |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | conditionCache[mIndex] = |
| 130 | evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache); |
| 131 | } |
| 132 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 133 | void CombinationConditionTracker::evaluateCondition( |
| Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 134 | const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues, |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 135 | const std::vector<sp<ConditionTracker>>& mAllConditions, |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 136 | std::vector<ConditionState>& nonSlicedConditionCache, |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 137 | std::vector<bool>& conditionChangedCache) { |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 138 | // value is up to date. |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 139 | if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 140 | return; |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | for (const int childIndex : mChildren) { |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 144 | // So far, this is fine as there is at most one child having sliced output. |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 145 | if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) { |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 146 | const sp<ConditionTracker>& child = mAllConditions[childIndex]; |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 147 | child->evaluateCondition(event, eventMatcherValues, mAllConditions, |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 148 | nonSlicedConditionCache, conditionChangedCache); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| Yao Chen | 427d3725 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 152 | ConditionState newCondition = |
| 153 | evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache); |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 154 | if (!mSliced) { |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 155 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 156 | bool nonSlicedChanged = (mNonSlicedConditionState != newCondition); |
| 157 | mNonSlicedConditionState = newCondition; |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 158 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 159 | nonSlicedConditionCache[mIndex] = mNonSlicedConditionState; |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 160 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 161 | conditionChangedCache[mIndex] = nonSlicedChanged; |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 162 | mUnSlicedPart = newCondition; |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 163 | } else { |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 164 | mUnSlicedPart = evaluateCombinationCondition( |
| 165 | mUnSlicedChildren, mLogicalOperation, nonSlicedConditionCache); |
| 166 | |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 167 | for (const int childIndex : mChildren) { |
| 168 | // If any of the sliced condition in children condition changes, the combination |
| 169 | // condition may be changed too. |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 170 | if (conditionChangedCache[childIndex]) { |
| 171 | conditionChangedCache[mIndex] = true; |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
| Yao Chen | 427d3725 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 175 | nonSlicedConditionCache[mIndex] = newCondition; |
| Yangster-mac | 8617950 | 2018-01-23 15:47:15 -0800 | [diff] [blame] | 176 | VLOG("CombinationPredicate %lld sliced may changed? %d", (long long)mConditionId, |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 177 | conditionChangedCache[mIndex] == true); |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 178 | } |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 181 | ConditionState CombinationConditionTracker::getMetConditionDimension( |
| 182 | const std::vector<sp<ConditionTracker>>& allConditions, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 183 | const std::vector<Matcher>& dimensionFields, |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 184 | const bool isSubOutputDimensionFields, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 185 | std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const { |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 186 | vector<ConditionState> conditionCache(allConditions.size(), ConditionState::kNotEvaluated); |
| 187 | // So far, this is fine as there is at most one child having sliced output. |
| 188 | for (const int childIndex : mChildren) { |
| 189 | conditionCache[childIndex] = conditionCache[childIndex] | |
| 190 | allConditions[childIndex]->getMetConditionDimension( |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 191 | allConditions, dimensionFields, isSubOutputDimensionFields, dimensionsKeySet); |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 192 | } |
| 193 | evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache); |
| 194 | if (conditionCache[mIndex] == ConditionState::kTrue && dimensionsKeySet.empty()) { |
| 195 | dimensionsKeySet.insert(DEFAULT_DIMENSION_KEY); |
| 196 | } |
| 197 | return conditionCache[mIndex]; |
| 198 | } |
| 199 | |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 200 | bool CombinationConditionTracker::equalOutputDimensions( |
| 201 | const std::vector<sp<ConditionTracker>>& allConditions, |
| 202 | const vector<Matcher>& dimensions) const { |
| 203 | if (mSlicedChildren.size() != 1 || |
| 204 | mSlicedChildren.front() >= (int)allConditions.size() || |
| 205 | mLogicalOperation != LogicalOperation::AND) { |
| 206 | return false; |
| 207 | } |
| 208 | const sp<ConditionTracker>& slicedChild = allConditions.at(mSlicedChildren.front()); |
| 209 | return slicedChild->equalOutputDimensions(allConditions, dimensions); |
| 210 | } |
| 211 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 212 | } // namespace statsd |
| 213 | } // namespace os |
| 214 | } // namespace android |