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