CombinationConditionTracker.cpp revision 12d01fa7c165a1f79091d4d742a48bffd7552650
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 17#define DEBUG true // STOPSHIP if true 18#include "Log.h" 19#include "CombinationConditionTracker.h" 20 21#include <log/logprint.h> 22 23namespace android { 24namespace os { 25namespace statsd { 26 27using std::map; 28using std::string; 29using std::unique_ptr; 30using std::unordered_map; 31using std::vector; 32 33CombinationConditionTracker::CombinationConditionTracker(const string& name, const int index) 34 : ConditionTracker(name, index) { 35 VLOG("creating CombinationConditionTracker %s", mName.c_str()); 36} 37 38CombinationConditionTracker::~CombinationConditionTracker() { 39 VLOG("~CombinationConditionTracker() %s", mName.c_str()); 40} 41 42bool CombinationConditionTracker::init(const vector<Predicate>& allConditionConfig, 43 const vector<sp<ConditionTracker>>& allConditionTrackers, 44 const unordered_map<string, int>& conditionNameIndexMap, 45 vector<bool>& stack) { 46 VLOG("Combination predicate 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 Predicate_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.predicate_size() != 1) { 62 return false; 63 } 64 65 for (string child : combinationCondition.predicate()) { 66 auto it = conditionNameIndexMap.find(child); 67 68 if (it == conditionNameIndexMap.end()) { 69 ALOGW("Predicate %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 105void CombinationConditionTracker::isConditionMet( 106 const map<string, HashableDimensionKey>& conditionParameters, 107 const vector<sp<ConditionTracker>>& allConditions, 108 vector<ConditionState>& conditionCache) const { 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 119void CombinationConditionTracker::evaluateCondition( 120 const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues, 121 const std::vector<sp<ConditionTracker>>& mAllConditions, 122 std::vector<ConditionState>& nonSlicedConditionCache, 123 std::vector<bool>& conditionChangedCache) { 124 // value is up to date. 125 if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) { 126 return; 127 } 128 129 for (const int childIndex : mChildren) { 130 if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) { 131 const sp<ConditionTracker>& child = mAllConditions[childIndex]; 132 child->evaluateCondition(event, eventMatcherValues, mAllConditions, 133 nonSlicedConditionCache, conditionChangedCache); 134 } 135 } 136 137 if (!mSliced) { 138 ConditionState newCondition = 139 evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache); 140 141 bool nonSlicedChanged = (mNonSlicedConditionState != newCondition); 142 mNonSlicedConditionState = newCondition; 143 144 nonSlicedConditionCache[mIndex] = mNonSlicedConditionState; 145 146 conditionChangedCache[mIndex] = nonSlicedChanged; 147 } else { 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 (conditionChangedCache[childIndex]) { 152 conditionChangedCache[mIndex] = true; 153 break; 154 } 155 } 156 nonSlicedConditionCache[mIndex] = ConditionState::kUnknown; 157 ALOGD("CombinationPredicate %s sliced may changed? %d", mName.c_str(), 158 conditionChangedCache[mIndex] == true); 159 } 160} 161 162} // namespace statsd 163} // namespace os 164} // namespace android 165