blob: 52a1269798cae8c30260230a1f7d7b8962520a32 [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
Yangster-mac94e197c2018-01-02 16:03:03 -080033CombinationConditionTracker::CombinationConditionTracker(const int64_t& id, const int index)
34 : ConditionTracker(id, index) {
35 VLOG("creating CombinationConditionTracker %lld", (long long)mConditionId);
Yao Chencaf339d2017-10-06 16:01:10 -070036}
37
38CombinationConditionTracker::~CombinationConditionTracker() {
Yangster-mac94e197c2018-01-02 16:03:03 -080039 VLOG("~CombinationConditionTracker() %lld", (long long)mConditionId);
Yao Chencaf339d2017-10-06 16:01:10 -070040}
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,
Yangster-mac94e197c2018-01-02 16:03:03 -080044 const unordered_map<int64_t, int>& conditionIdIndexMap,
Yao Chencaf339d2017-10-06 16:01:10 -070045 vector<bool>& stack) {
Yangster-mac94e197c2018-01-02 16:03:03 -080046 VLOG("Combination predicate init() %lld", (long long)mConditionId);
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
Yangster-mac94e197c2018-01-02 16:03:03 -080065 for (auto child : combinationCondition.predicate()) {
66 auto it = conditionIdIndexMap.find(child);
Yao Chencaf339d2017-10-06 16:01:10 -070067
Yangster-mac94e197c2018-01-02 16:03:03 -080068 if (it == conditionIdIndexMap.end()) {
69 ALOGW("Predicate %lld not found in the config", (long long)child);
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
Yangster-mac93694462018-01-22 20:49:31 -080081
Yao Chencaf339d2017-10-06 16:01:10 -070082 bool initChildSucceeded = childTracker->init(allConditionConfig, allConditionTrackers,
Yangster-mac94e197c2018-01-02 16:03:03 -080083 conditionIdIndexMap, stack);
Yao Chencaf339d2017-10-06 16:01:10 -070084
85 if (!initChildSucceeded) {
Yangster-mac94e197c2018-01-02 16:03:03 -080086 ALOGW("Child initialization failed %lld ", (long long)child);
Yao Chencaf339d2017-10-06 16:01:10 -070087 return false;
88 } else {
Yangster-mac94e197c2018-01-02 16:03:03 -080089 ALOGW("Child initialization success %lld ", (long long)child);
Yao Chencaf339d2017-10-06 16:01:10 -070090 }
91
Yangster-mac93694462018-01-22 20:49:31 -080092 if (allConditionTrackers[childIndex]->isSliced()) {
93 setSliced(true);
Yangster13fb7e42018-03-07 17:30:49 -080094 mSlicedChildren.push_back(childIndex);
95 } else {
96 mUnSlicedChildren.push_back(childIndex);
Yangster-mac93694462018-01-22 20:49:31 -080097 }
Yao Chencaf339d2017-10-06 16:01:10 -070098 mChildren.push_back(childIndex);
Yao Chencaf339d2017-10-06 16:01:10 -070099 mTrackerIndex.insert(childTracker->getLogTrackerIndex().begin(),
100 childTracker->getLogTrackerIndex().end());
101 }
102
103 // unmark this node in the recursion stack.
104 stack[mIndex] = false;
105
106 mInitialized = true;
107
108 return true;
109}
110
Yao Chen729093d2017-10-16 10:33:26 -0700111void CombinationConditionTracker::isConditionMet(
Yao Chen8a8d16c2018-02-08 14:50:40 -0800112 const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions,
Yangster13fb7e42018-03-07 17:30:49 -0800113 const bool isPartialLink,
tsaichristine76853372019-08-06 17:17:03 -0700114 vector<ConditionState>& conditionCache) const {
Yangster-mac93694462018-01-22 20:49:31 -0800115 // So far, this is fine as there is at most one child having sliced output.
Yao Chen729093d2017-10-16 10:33:26 -0700116 for (const int childIndex : mChildren) {
117 if (conditionCache[childIndex] == ConditionState::kNotEvaluated) {
118 allConditions[childIndex]->isConditionMet(conditionParameters, allConditions,
Yangster13fb7e42018-03-07 17:30:49 -0800119 isPartialLink,
tsaichristine76853372019-08-06 17:17:03 -0700120 conditionCache);
Yao Chen729093d2017-10-16 10:33:26 -0700121 }
122 }
123 conditionCache[mIndex] =
124 evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
125}
126
Yao Chen967b2052017-11-07 16:36:43 -0800127void CombinationConditionTracker::evaluateCondition(
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700128 const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues,
Yao Chencaf339d2017-10-06 16:01:10 -0700129 const std::vector<sp<ConditionTracker>>& mAllConditions,
Yao Chen729093d2017-10-16 10:33:26 -0700130 std::vector<ConditionState>& nonSlicedConditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800131 std::vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700132 // value is up to date.
Yao Chen729093d2017-10-16 10:33:26 -0700133 if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) {
Yao Chen967b2052017-11-07 16:36:43 -0800134 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700135 }
136
137 for (const int childIndex : mChildren) {
Yangster-mac93694462018-01-22 20:49:31 -0800138 // So far, this is fine as there is at most one child having sliced output.
Yao Chen729093d2017-10-16 10:33:26 -0700139 if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) {
Yao Chencaf339d2017-10-06 16:01:10 -0700140 const sp<ConditionTracker>& child = mAllConditions[childIndex];
Yao Chen729093d2017-10-16 10:33:26 -0700141 child->evaluateCondition(event, eventMatcherValues, mAllConditions,
Yao Chen967b2052017-11-07 16:36:43 -0800142 nonSlicedConditionCache, conditionChangedCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700143 }
144 }
145
Yao Chen427d3722018-03-22 15:21:52 -0700146 ConditionState newCondition =
147 evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800148 if (!mSliced) {
Yao Chencaf339d2017-10-06 16:01:10 -0700149
Yao Chen967b2052017-11-07 16:36:43 -0800150 bool nonSlicedChanged = (mNonSlicedConditionState != newCondition);
151 mNonSlicedConditionState = newCondition;
Yao Chencaf339d2017-10-06 16:01:10 -0700152
Yao Chen967b2052017-11-07 16:36:43 -0800153 nonSlicedConditionCache[mIndex] = mNonSlicedConditionState;
Yao Chencaf339d2017-10-06 16:01:10 -0700154
Yao Chen967b2052017-11-07 16:36:43 -0800155 conditionChangedCache[mIndex] = nonSlicedChanged;
Yangster13fb7e42018-03-07 17:30:49 -0800156 mUnSlicedPart = newCondition;
Yao Chen967b2052017-11-07 16:36:43 -0800157 } else {
Yangster13fb7e42018-03-07 17:30:49 -0800158 mUnSlicedPart = evaluateCombinationCondition(
159 mUnSlicedChildren, mLogicalOperation, nonSlicedConditionCache);
160
Yao Chen729093d2017-10-16 10:33:26 -0700161 for (const int childIndex : mChildren) {
162 // If any of the sliced condition in children condition changes, the combination
163 // condition may be changed too.
Yao Chen967b2052017-11-07 16:36:43 -0800164 if (conditionChangedCache[childIndex]) {
165 conditionChangedCache[mIndex] = true;
Yao Chen729093d2017-10-16 10:33:26 -0700166 break;
167 }
168 }
Yao Chen427d3722018-03-22 15:21:52 -0700169 nonSlicedConditionCache[mIndex] = newCondition;
Yangster-mac86179502018-01-23 15:47:15 -0800170 VLOG("CombinationPredicate %lld sliced may changed? %d", (long long)mConditionId,
Yangster-mac94e197c2018-01-02 16:03:03 -0800171 conditionChangedCache[mIndex] == true);
Yao Chen729093d2017-10-16 10:33:26 -0700172 }
Yao Chencaf339d2017-10-06 16:01:10 -0700173}
174
Yangster13fb7e42018-03-07 17:30:49 -0800175bool CombinationConditionTracker::equalOutputDimensions(
176 const std::vector<sp<ConditionTracker>>& allConditions,
177 const vector<Matcher>& dimensions) const {
178 if (mSlicedChildren.size() != 1 ||
179 mSlicedChildren.front() >= (int)allConditions.size() ||
180 mLogicalOperation != LogicalOperation::AND) {
181 return false;
182 }
183 const sp<ConditionTracker>& slicedChild = allConditions.at(mSlicedChildren.front());
184 return slicedChild->equalOutputDimensions(allConditions, dimensions);
185}
186
Yao Chencaf339d2017-10-06 16:01:10 -0700187} // namespace statsd
188} // namespace os
189} // namespace android