blob: 60a4b236df11f7d305b3ab8907cce71f3deacfd1 [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 std::vector<Matcher>& dimensionFields,
114 const bool isSubOutputDimensionFields,
115 const bool isPartialLink,
116 vector<ConditionState>& conditionCache,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800117 std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const {
Yangster-mac93694462018-01-22 20:49:31 -0800118 // So far, this is fine as there is at most one child having sliced output.
Yao Chen729093d2017-10-16 10:33:26 -0700119 for (const int childIndex : mChildren) {
120 if (conditionCache[childIndex] == ConditionState::kNotEvaluated) {
121 allConditions[childIndex]->isConditionMet(conditionParameters, allConditions,
Yangster13fb7e42018-03-07 17:30:49 -0800122 dimensionFields,
123 isSubOutputDimensionFields,
124 isPartialLink,
125 conditionCache,
Yangster-mac93694462018-01-22 20:49:31 -0800126 dimensionsKeySet);
Yao Chen729093d2017-10-16 10:33:26 -0700127 }
128 }
129 conditionCache[mIndex] =
130 evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
131}
132
Yao Chen967b2052017-11-07 16:36:43 -0800133void CombinationConditionTracker::evaluateCondition(
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700134 const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues,
Yao Chencaf339d2017-10-06 16:01:10 -0700135 const std::vector<sp<ConditionTracker>>& mAllConditions,
Yao Chen729093d2017-10-16 10:33:26 -0700136 std::vector<ConditionState>& nonSlicedConditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800137 std::vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700138 // value is up to date.
Yao Chen729093d2017-10-16 10:33:26 -0700139 if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) {
Yao Chen967b2052017-11-07 16:36:43 -0800140 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700141 }
142
143 for (const int childIndex : mChildren) {
Yangster-mac93694462018-01-22 20:49:31 -0800144 // So far, this is fine as there is at most one child having sliced output.
Yao Chen729093d2017-10-16 10:33:26 -0700145 if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) {
Yao Chencaf339d2017-10-06 16:01:10 -0700146 const sp<ConditionTracker>& child = mAllConditions[childIndex];
Yao Chen729093d2017-10-16 10:33:26 -0700147 child->evaluateCondition(event, eventMatcherValues, mAllConditions,
Yao Chen967b2052017-11-07 16:36:43 -0800148 nonSlicedConditionCache, conditionChangedCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700149 }
150 }
151
Yao Chen427d37252018-03-22 15:21:52 -0700152 ConditionState newCondition =
153 evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800154 if (!mSliced) {
Yao Chencaf339d2017-10-06 16:01:10 -0700155
Yao Chen967b2052017-11-07 16:36:43 -0800156 bool nonSlicedChanged = (mNonSlicedConditionState != newCondition);
157 mNonSlicedConditionState = newCondition;
Yao Chencaf339d2017-10-06 16:01:10 -0700158
Yao Chen967b2052017-11-07 16:36:43 -0800159 nonSlicedConditionCache[mIndex] = mNonSlicedConditionState;
Yao Chencaf339d2017-10-06 16:01:10 -0700160
Yao Chen967b2052017-11-07 16:36:43 -0800161 conditionChangedCache[mIndex] = nonSlicedChanged;
Yangster13fb7e42018-03-07 17:30:49 -0800162 mUnSlicedPart = newCondition;
Yao Chen967b2052017-11-07 16:36:43 -0800163 } else {
Yangster13fb7e42018-03-07 17:30:49 -0800164 mUnSlicedPart = evaluateCombinationCondition(
165 mUnSlicedChildren, mLogicalOperation, nonSlicedConditionCache);
166
Yao Chen729093d2017-10-16 10:33:26 -0700167 for (const int childIndex : mChildren) {
168 // If any of the sliced condition in children condition changes, the combination
169 // condition may be changed too.
Yao Chen967b2052017-11-07 16:36:43 -0800170 if (conditionChangedCache[childIndex]) {
171 conditionChangedCache[mIndex] = true;
Yao Chen729093d2017-10-16 10:33:26 -0700172 break;
173 }
174 }
Yao Chen427d37252018-03-22 15:21:52 -0700175 nonSlicedConditionCache[mIndex] = newCondition;
Yangster-mac86179502018-01-23 15:47:15 -0800176 VLOG("CombinationPredicate %lld sliced may changed? %d", (long long)mConditionId,
Yangster-mac94e197c2018-01-02 16:03:03 -0800177 conditionChangedCache[mIndex] == true);
Yao Chen729093d2017-10-16 10:33:26 -0700178 }
Yao Chencaf339d2017-10-06 16:01:10 -0700179}
180
Yangster-mac93694462018-01-22 20:49:31 -0800181ConditionState CombinationConditionTracker::getMetConditionDimension(
182 const std::vector<sp<ConditionTracker>>& allConditions,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800183 const std::vector<Matcher>& dimensionFields,
Yangster13fb7e42018-03-07 17:30:49 -0800184 const bool isSubOutputDimensionFields,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800185 std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const {
Yangster-mac93694462018-01-22 20:49:31 -0800186 vector<ConditionState> conditionCache(allConditions.size(), ConditionState::kNotEvaluated);
187 // So far, this is fine as there is at most one child having sliced output.
188 for (const int childIndex : mChildren) {
189 conditionCache[childIndex] = conditionCache[childIndex] |
190 allConditions[childIndex]->getMetConditionDimension(
Yangster13fb7e42018-03-07 17:30:49 -0800191 allConditions, dimensionFields, isSubOutputDimensionFields, dimensionsKeySet);
Yangster-mac93694462018-01-22 20:49:31 -0800192 }
193 evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
194 if (conditionCache[mIndex] == ConditionState::kTrue && dimensionsKeySet.empty()) {
195 dimensionsKeySet.insert(DEFAULT_DIMENSION_KEY);
196 }
197 return conditionCache[mIndex];
198}
199
Yangster13fb7e42018-03-07 17:30:49 -0800200bool CombinationConditionTracker::equalOutputDimensions(
201 const std::vector<sp<ConditionTracker>>& allConditions,
202 const vector<Matcher>& dimensions) const {
203 if (mSlicedChildren.size() != 1 ||
204 mSlicedChildren.front() >= (int)allConditions.size() ||
205 mLogicalOperation != LogicalOperation::AND) {
206 return false;
207 }
208 const sp<ConditionTracker>& slicedChild = allConditions.at(mSlicedChildren.front());
209 return slicedChild->equalOutputDimensions(allConditions, dimensions);
210}
211
Yao Chencaf339d2017-10-06 16:01:10 -0700212} // namespace statsd
213} // namespace os
214} // namespace android