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 | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 17 | #define DEBUG true // 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 | |
| 20 | #include "CombinationConditionTracker.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 22 | #include <log/logprint.h> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 28 | using std::map; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | using std::unique_ptr; |
| 31 | using std::unordered_map; |
| 32 | using std::vector; |
| 33 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 34 | CombinationConditionTracker::CombinationConditionTracker(const string& name, const int index) |
| 35 | : ConditionTracker(name, index) { |
| 36 | VLOG("creating CombinationConditionTracker %s", mName.c_str()); |
| 37 | } |
| 38 | |
| 39 | CombinationConditionTracker::~CombinationConditionTracker() { |
| 40 | VLOG("~CombinationConditionTracker() %s", mName.c_str()); |
| 41 | } |
| 42 | |
| 43 | bool CombinationConditionTracker::init(const vector<Condition>& allConditionConfig, |
| 44 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
| 45 | const unordered_map<string, int>& conditionNameIndexMap, |
| 46 | vector<bool>& stack) { |
| 47 | VLOG("Combiniation condition init() %s", mName.c_str()); |
| 48 | if (mInitialized) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | // mark this node as visited in the recursion stack. |
| 53 | stack[mIndex] = true; |
| 54 | |
| 55 | Condition_Combination combinationCondition = allConditionConfig[mIndex].combination(); |
| 56 | |
| 57 | if (!combinationCondition.has_operation()) { |
| 58 | return false; |
| 59 | } |
| 60 | mLogicalOperation = combinationCondition.operation(); |
| 61 | |
| 62 | if (mLogicalOperation == LogicalOperation::NOT && combinationCondition.condition_size() != 1) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | for (string child : combinationCondition.condition()) { |
| 67 | auto it = conditionNameIndexMap.find(child); |
| 68 | |
| 69 | if (it == conditionNameIndexMap.end()) { |
| 70 | ALOGW("Condition %s not found in the config", child.c_str()); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | int childIndex = it->second; |
| 75 | const auto& childTracker = allConditionTrackers[childIndex]; |
| 76 | // if the child is a visited node in the recursion -> circle detected. |
| 77 | if (stack[childIndex]) { |
| 78 | ALOGW("Circle detected!!!"); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | bool initChildSucceeded = childTracker->init(allConditionConfig, allConditionTrackers, |
| 83 | conditionNameIndexMap, stack); |
| 84 | |
| 85 | if (!initChildSucceeded) { |
| 86 | ALOGW("Child initialization failed %s ", child.c_str()); |
| 87 | return false; |
| 88 | } else { |
| 89 | ALOGW("Child initialization success %s ", child.c_str()); |
| 90 | } |
| 91 | |
| 92 | mChildren.push_back(childIndex); |
| 93 | |
| 94 | mTrackerIndex.insert(childTracker->getLogTrackerIndex().begin(), |
| 95 | childTracker->getLogTrackerIndex().end()); |
| 96 | } |
| 97 | |
| 98 | // unmark this node in the recursion stack. |
| 99 | stack[mIndex] = false; |
| 100 | |
| 101 | mInitialized = true; |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 106 | void CombinationConditionTracker::isConditionMet( |
| 107 | const map<string, HashableDimensionKey>& conditionParameters, |
| 108 | const vector<sp<ConditionTracker>>& allConditions, vector<ConditionState>& conditionCache) { |
| 109 | for (const int childIndex : mChildren) { |
| 110 | if (conditionCache[childIndex] == ConditionState::kNotEvaluated) { |
| 111 | allConditions[childIndex]->isConditionMet(conditionParameters, allConditions, |
| 112 | conditionCache); |
| 113 | } |
| 114 | } |
| 115 | conditionCache[mIndex] = |
| 116 | evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache); |
| 117 | } |
| 118 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 119 | bool CombinationConditionTracker::evaluateCondition( |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 120 | const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 121 | const std::vector<sp<ConditionTracker>>& mAllConditions, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 122 | std::vector<ConditionState>& nonSlicedConditionCache, |
| 123 | std::vector<bool>& nonSlicedChangedCache, vector<bool>& slicedConditionChanged) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 124 | // value is up to date. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 125 | if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | for (const int childIndex : mChildren) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 130 | if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 131 | const sp<ConditionTracker>& child = mAllConditions[childIndex]; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 132 | child->evaluateCondition(event, eventMatcherValues, mAllConditions, |
| 133 | nonSlicedConditionCache, nonSlicedChangedCache, |
| 134 | slicedConditionChanged); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | ConditionState newCondition = |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 139 | evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 140 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 141 | bool nonSlicedChanged = (mNonSlicedConditionState != newCondition); |
| 142 | mNonSlicedConditionState = newCondition; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 143 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 144 | nonSlicedConditionCache[mIndex] = mNonSlicedConditionState; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 145 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame^] | 146 | nonSlicedChangedCache[mIndex] = nonSlicedChanged; |
| 147 | |
| 148 | if (mSliced) { |
| 149 | for (const int childIndex : mChildren) { |
| 150 | // If any of the sliced condition in children condition changes, the combination |
| 151 | // condition may be changed too. |
| 152 | if (slicedConditionChanged[childIndex]) { |
| 153 | slicedConditionChanged[mIndex] = true; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | ALOGD("CombinationCondition %s sliced may changed? %d", mName.c_str(), |
| 158 | slicedConditionChanged[mIndex] == true); |
| 159 | } |
| 160 | |
| 161 | return nonSlicedChanged; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace statsd |
| 165 | } // namespace os |
| 166 | } // namespace android |