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