blob: 52b83d875caa6a87bac103ea4728128793a169b4 [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001/*
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 Onorato9fc9edf2017-10-15 20:08:52 -070016
Yao Chen3c0b95c2017-12-16 14:34:20 -080017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Yao Chencaf339d2017-10-06 16:01:10 -070019#include "CombinationConditionTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070020
Yao Chencaf339d2017-10-06 16:01:10 -070021#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070022
23namespace android {
24namespace os {
25namespace statsd {
26
Yao Chen729093d2017-10-16 10:33:26 -070027using std::map;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070028using std::string;
29using std::unique_ptr;
30using std::unordered_map;
31using std::vector;
32
Yao Chencaf339d2017-10-06 16:01:10 -070033CombinationConditionTracker::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
Stefan Lafon12d01fa2017-12-04 20:56:09 -080042bool CombinationConditionTracker::init(const vector<Predicate>& allConditionConfig,
Yao Chencaf339d2017-10-06 16:01:10 -070043 const vector<sp<ConditionTracker>>& allConditionTrackers,
44 const unordered_map<string, int>& conditionNameIndexMap,
45 vector<bool>& stack) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080046 VLOG("Combination predicate init() %s", mName.c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070047 if (mInitialized) {
48 return true;
49 }
50
51 // mark this node as visited in the recursion stack.
52 stack[mIndex] = true;
53
Stefan Lafon12d01fa2017-12-04 20:56:09 -080054 Predicate_Combination combinationCondition = allConditionConfig[mIndex].combination();
Yao Chencaf339d2017-10-06 16:01:10 -070055
56 if (!combinationCondition.has_operation()) {
57 return false;
58 }
59 mLogicalOperation = combinationCondition.operation();
60
Stefan Lafon12d01fa2017-12-04 20:56:09 -080061 if (mLogicalOperation == LogicalOperation::NOT && combinationCondition.predicate_size() != 1) {
Yao Chencaf339d2017-10-06 16:01:10 -070062 return false;
63 }
64
Stefan Lafon12d01fa2017-12-04 20:56:09 -080065 for (string child : combinationCondition.predicate()) {
Yao Chencaf339d2017-10-06 16:01:10 -070066 auto it = conditionNameIndexMap.find(child);
67
68 if (it == conditionNameIndexMap.end()) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080069 ALOGW("Predicate %s not found in the config", child.c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070070 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 Chen729093d2017-10-16 10:33:26 -0700105void CombinationConditionTracker::isConditionMet(
Yangster-mac20877162017-12-22 17:19:39 -0800106 const ConditionKey& conditionParameters,
Yangster7c334a12017-11-22 14:24:24 -0800107 const vector<sp<ConditionTracker>>& allConditions,
108 vector<ConditionState>& conditionCache) const {
Yao Chen729093d2017-10-16 10:33:26 -0700109 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 Chen967b2052017-11-07 16:36:43 -0800119void CombinationConditionTracker::evaluateCondition(
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700120 const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues,
Yao Chencaf339d2017-10-06 16:01:10 -0700121 const std::vector<sp<ConditionTracker>>& mAllConditions,
Yao Chen729093d2017-10-16 10:33:26 -0700122 std::vector<ConditionState>& nonSlicedConditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800123 std::vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700124 // value is up to date.
Yao Chen729093d2017-10-16 10:33:26 -0700125 if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) {
Yao Chen967b2052017-11-07 16:36:43 -0800126 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700127 }
128
129 for (const int childIndex : mChildren) {
Yao Chen729093d2017-10-16 10:33:26 -0700130 if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) {
Yao Chencaf339d2017-10-06 16:01:10 -0700131 const sp<ConditionTracker>& child = mAllConditions[childIndex];
Yao Chen729093d2017-10-16 10:33:26 -0700132 child->evaluateCondition(event, eventMatcherValues, mAllConditions,
Yao Chen967b2052017-11-07 16:36:43 -0800133 nonSlicedConditionCache, conditionChangedCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700134 }
135 }
136
Yao Chen967b2052017-11-07 16:36:43 -0800137 if (!mSliced) {
138 ConditionState newCondition =
139 evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700140
Yao Chen967b2052017-11-07 16:36:43 -0800141 bool nonSlicedChanged = (mNonSlicedConditionState != newCondition);
142 mNonSlicedConditionState = newCondition;
Yao Chencaf339d2017-10-06 16:01:10 -0700143
Yao Chen967b2052017-11-07 16:36:43 -0800144 nonSlicedConditionCache[mIndex] = mNonSlicedConditionState;
Yao Chencaf339d2017-10-06 16:01:10 -0700145
Yao Chen967b2052017-11-07 16:36:43 -0800146 conditionChangedCache[mIndex] = nonSlicedChanged;
147 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700148 for (const int childIndex : mChildren) {
149 // If any of the sliced condition in children condition changes, the combination
150 // condition may be changed too.
Yao Chen967b2052017-11-07 16:36:43 -0800151 if (conditionChangedCache[childIndex]) {
152 conditionChangedCache[mIndex] = true;
Yao Chen729093d2017-10-16 10:33:26 -0700153 break;
154 }
155 }
Yao Chen967b2052017-11-07 16:36:43 -0800156 nonSlicedConditionCache[mIndex] = ConditionState::kUnknown;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800157 ALOGD("CombinationPredicate %s sliced may changed? %d", mName.c_str(),
Yao Chen967b2052017-11-07 16:36:43 -0800158 conditionChangedCache[mIndex] == true);
Yao Chen729093d2017-10-16 10:33:26 -0700159 }
Yao Chencaf339d2017-10-06 16:01:10 -0700160}
161
162} // namespace statsd
163} // namespace os
164} // namespace android