blob: f56c15a37086957ba5412d7d2ff0def479857e85 [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 Chencaf339d2017-10-06 16:01:10 -070017#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Yao Chencaf339d2017-10-06 16:01:10 -070019
20#include "CombinationConditionTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021
Yao Chencaf339d2017-10-06 16:01:10 -070022#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070023
24namespace android {
25namespace os {
26namespace statsd {
27
Yao Chen729093d2017-10-16 10:33:26 -070028using std::map;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070029using std::string;
30using std::unique_ptr;
31using std::unordered_map;
32using std::vector;
33
Yao Chencaf339d2017-10-06 16:01:10 -070034CombinationConditionTracker::CombinationConditionTracker(const string& name, const int index)
35 : ConditionTracker(name, index) {
36 VLOG("creating CombinationConditionTracker %s", mName.c_str());
37}
38
39CombinationConditionTracker::~CombinationConditionTracker() {
40 VLOG("~CombinationConditionTracker() %s", mName.c_str());
41}
42
43bool 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 Chen729093d2017-10-16 10:33:26 -0700106void 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 Chencaf339d2017-10-06 16:01:10 -0700119bool 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,
123 std::vector<bool>& nonSlicedChangedCache, vector<bool>& slicedConditionChanged) {
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 Chencaf339d2017-10-06 16:01:10 -0700126 return false;
127 }
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,
133 nonSlicedConditionCache, nonSlicedChangedCache,
134 slicedConditionChanged);
Yao Chencaf339d2017-10-06 16:01:10 -0700135 }
136 }
137
138 ConditionState newCondition =
Yao Chen729093d2017-10-16 10:33:26 -0700139 evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700140
Yao Chen729093d2017-10-16 10:33:26 -0700141 bool nonSlicedChanged = (mNonSlicedConditionState != newCondition);
142 mNonSlicedConditionState = newCondition;
Yao Chencaf339d2017-10-06 16:01:10 -0700143
Yao Chen729093d2017-10-16 10:33:26 -0700144 nonSlicedConditionCache[mIndex] = mNonSlicedConditionState;
Yao Chencaf339d2017-10-06 16:01:10 -0700145
Yao Chen729093d2017-10-16 10:33:26 -0700146 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 Chencaf339d2017-10-06 16:01:10 -0700162}
163
164} // namespace statsd
165} // namespace os
166} // namespace android