blob: 4c20ccb61afe506397cd5cc4babc4ff2d77b1a70 [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);
94 }
Yao Chencaf339d2017-10-06 16:01:10 -070095 mChildren.push_back(childIndex);
Yao Chencaf339d2017-10-06 16:01:10 -070096 mTrackerIndex.insert(childTracker->getLogTrackerIndex().begin(),
97 childTracker->getLogTrackerIndex().end());
98 }
99
100 // unmark this node in the recursion stack.
101 stack[mIndex] = false;
102
103 mInitialized = true;
104
105 return true;
106}
107
Yao Chen729093d2017-10-16 10:33:26 -0700108void CombinationConditionTracker::isConditionMet(
Yangster-mac20877162017-12-22 17:19:39 -0800109 const ConditionKey& conditionParameters,
Yangster7c334a12017-11-22 14:24:24 -0800110 const vector<sp<ConditionTracker>>& allConditions,
Yangster-mac93694462018-01-22 20:49:31 -0800111 const FieldMatcher& dimensionFields,
112 vector<ConditionState>& conditionCache,
113 std::unordered_set<HashableDimensionKey> &dimensionsKeySet) const {
114 // So far, this is fine as there is at most one child having sliced output.
Yao Chen729093d2017-10-16 10:33:26 -0700115 for (const int childIndex : mChildren) {
116 if (conditionCache[childIndex] == ConditionState::kNotEvaluated) {
117 allConditions[childIndex]->isConditionMet(conditionParameters, allConditions,
Yangster-mac93694462018-01-22 20:49:31 -0800118 dimensionFields, conditionCache,
119 dimensionsKeySet);
Yao Chen729093d2017-10-16 10:33:26 -0700120 }
121 }
122 conditionCache[mIndex] =
123 evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
124}
125
Yao Chen967b2052017-11-07 16:36:43 -0800126void CombinationConditionTracker::evaluateCondition(
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700127 const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues,
Yao Chencaf339d2017-10-06 16:01:10 -0700128 const std::vector<sp<ConditionTracker>>& mAllConditions,
Yao Chen729093d2017-10-16 10:33:26 -0700129 std::vector<ConditionState>& nonSlicedConditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800130 std::vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700131 // value is up to date.
Yao Chen729093d2017-10-16 10:33:26 -0700132 if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) {
Yao Chen967b2052017-11-07 16:36:43 -0800133 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700134 }
135
136 for (const int childIndex : mChildren) {
Yangster-mac93694462018-01-22 20:49:31 -0800137 // So far, this is fine as there is at most one child having sliced output.
Yao Chen729093d2017-10-16 10:33:26 -0700138 if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) {
Yao Chencaf339d2017-10-06 16:01:10 -0700139 const sp<ConditionTracker>& child = mAllConditions[childIndex];
Yao Chen729093d2017-10-16 10:33:26 -0700140 child->evaluateCondition(event, eventMatcherValues, mAllConditions,
Yao Chen967b2052017-11-07 16:36:43 -0800141 nonSlicedConditionCache, conditionChangedCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700142 }
143 }
144
Yao Chen967b2052017-11-07 16:36:43 -0800145 if (!mSliced) {
146 ConditionState newCondition =
147 evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache);
Yao Chencaf339d2017-10-06 16:01:10 -0700148
Yao Chen967b2052017-11-07 16:36:43 -0800149 bool nonSlicedChanged = (mNonSlicedConditionState != newCondition);
150 mNonSlicedConditionState = newCondition;
Yao Chencaf339d2017-10-06 16:01:10 -0700151
Yao Chen967b2052017-11-07 16:36:43 -0800152 nonSlicedConditionCache[mIndex] = mNonSlicedConditionState;
Yao Chencaf339d2017-10-06 16:01:10 -0700153
Yao Chen967b2052017-11-07 16:36:43 -0800154 conditionChangedCache[mIndex] = nonSlicedChanged;
155 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700156 for (const int childIndex : mChildren) {
157 // If any of the sliced condition in children condition changes, the combination
158 // condition may be changed too.
Yao Chen967b2052017-11-07 16:36:43 -0800159 if (conditionChangedCache[childIndex]) {
160 conditionChangedCache[mIndex] = true;
Yao Chen729093d2017-10-16 10:33:26 -0700161 break;
162 }
163 }
Yao Chen967b2052017-11-07 16:36:43 -0800164 nonSlicedConditionCache[mIndex] = ConditionState::kUnknown;
Yangster-mac86179502018-01-23 15:47:15 -0800165 VLOG("CombinationPredicate %lld sliced may changed? %d", (long long)mConditionId,
Yangster-mac94e197c2018-01-02 16:03:03 -0800166 conditionChangedCache[mIndex] == true);
Yao Chen729093d2017-10-16 10:33:26 -0700167 }
Yao Chencaf339d2017-10-06 16:01:10 -0700168}
169
Yangster-mac93694462018-01-22 20:49:31 -0800170ConditionState CombinationConditionTracker::getMetConditionDimension(
171 const std::vector<sp<ConditionTracker>>& allConditions,
172 const FieldMatcher& dimensionFields,
173 std::unordered_set<HashableDimensionKey> &dimensionsKeySet) const {
174 vector<ConditionState> conditionCache(allConditions.size(), ConditionState::kNotEvaluated);
175 // So far, this is fine as there is at most one child having sliced output.
176 for (const int childIndex : mChildren) {
177 conditionCache[childIndex] = conditionCache[childIndex] |
178 allConditions[childIndex]->getMetConditionDimension(
179 allConditions, dimensionFields, dimensionsKeySet);
180 }
181 evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
182 if (conditionCache[mIndex] == ConditionState::kTrue && dimensionsKeySet.empty()) {
183 dimensionsKeySet.insert(DEFAULT_DIMENSION_KEY);
184 }
185 return conditionCache[mIndex];
186}
187
Yao Chencaf339d2017-10-06 16:01:10 -0700188} // namespace statsd
189} // namespace os
190} // namespace android