CombinationConditionTracker.cpp revision 8a8d16ceea1e5b7a2f8c41e17b5d993035f50f5d
1/*
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 */
16
17#define DEBUG false  // STOPSHIP if true
18#include "Log.h"
19#include "CombinationConditionTracker.h"
20
21#include <log/logprint.h>
22
23namespace android {
24namespace os {
25namespace statsd {
26
27using std::map;
28using std::string;
29using std::unique_ptr;
30using std::unordered_map;
31using std::vector;
32
33CombinationConditionTracker::CombinationConditionTracker(const int64_t& id, const int index)
34    : ConditionTracker(id, index) {
35    VLOG("creating CombinationConditionTracker %lld", (long long)mConditionId);
36}
37
38CombinationConditionTracker::~CombinationConditionTracker() {
39    VLOG("~CombinationConditionTracker() %lld", (long long)mConditionId);
40}
41
42bool CombinationConditionTracker::init(const vector<Predicate>& allConditionConfig,
43                                       const vector<sp<ConditionTracker>>& allConditionTrackers,
44                                       const unordered_map<int64_t, int>& conditionIdIndexMap,
45                                       vector<bool>& stack) {
46    VLOG("Combination predicate init() %lld", (long long)mConditionId);
47    if (mInitialized) {
48        return true;
49    }
50
51    // mark this node as visited in the recursion stack.
52    stack[mIndex] = true;
53
54    Predicate_Combination combinationCondition = allConditionConfig[mIndex].combination();
55
56    if (!combinationCondition.has_operation()) {
57        return false;
58    }
59    mLogicalOperation = combinationCondition.operation();
60
61    if (mLogicalOperation == LogicalOperation::NOT && combinationCondition.predicate_size() != 1) {
62        return false;
63    }
64
65    for (auto child : combinationCondition.predicate()) {
66        auto it = conditionIdIndexMap.find(child);
67
68        if (it == conditionIdIndexMap.end()) {
69            ALOGW("Predicate %lld not found in the config", (long long)child);
70            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
82        bool initChildSucceeded = childTracker->init(allConditionConfig, allConditionTrackers,
83                                                     conditionIdIndexMap, stack);
84
85        if (!initChildSucceeded) {
86            ALOGW("Child initialization failed %lld ", (long long)child);
87            return false;
88        } else {
89            ALOGW("Child initialization success %lld ", (long long)child);
90        }
91
92        if (allConditionTrackers[childIndex]->isSliced()) {
93            setSliced(true);
94        }
95        mChildren.push_back(childIndex);
96        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
108void CombinationConditionTracker::isConditionMet(
109        const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions,
110        const std::vector<Matcher>& dimensionFields, vector<ConditionState>& conditionCache,
111        std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const {
112    // So far, this is fine as there is at most one child having sliced output.
113    for (const int childIndex : mChildren) {
114        if (conditionCache[childIndex] == ConditionState::kNotEvaluated) {
115            allConditions[childIndex]->isConditionMet(conditionParameters, allConditions,
116                                                      dimensionFields, conditionCache,
117                                                      dimensionsKeySet);
118        }
119    }
120    conditionCache[mIndex] =
121            evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
122}
123
124void CombinationConditionTracker::evaluateCondition(
125        const LogEvent& event, const std::vector<MatchingState>& eventMatcherValues,
126        const std::vector<sp<ConditionTracker>>& mAllConditions,
127        std::vector<ConditionState>& nonSlicedConditionCache,
128        std::vector<bool>& conditionChangedCache) {
129    // value is up to date.
130    if (nonSlicedConditionCache[mIndex] != ConditionState::kNotEvaluated) {
131        return;
132    }
133
134    for (const int childIndex : mChildren) {
135        // So far, this is fine as there is at most one child having sliced output.
136        if (nonSlicedConditionCache[childIndex] == ConditionState::kNotEvaluated) {
137            const sp<ConditionTracker>& child = mAllConditions[childIndex];
138            child->evaluateCondition(event, eventMatcherValues, mAllConditions,
139                                     nonSlicedConditionCache, conditionChangedCache);
140        }
141    }
142
143    if (!mSliced) {
144        ConditionState newCondition =
145                evaluateCombinationCondition(mChildren, mLogicalOperation, nonSlicedConditionCache);
146
147        bool nonSlicedChanged = (mNonSlicedConditionState != newCondition);
148        mNonSlicedConditionState = newCondition;
149
150        nonSlicedConditionCache[mIndex] = mNonSlicedConditionState;
151
152        conditionChangedCache[mIndex] = nonSlicedChanged;
153    } else {
154        for (const int childIndex : mChildren) {
155            // If any of the sliced condition in children condition changes, the combination
156            // condition may be changed too.
157            if (conditionChangedCache[childIndex]) {
158                conditionChangedCache[mIndex] = true;
159                break;
160            }
161        }
162        nonSlicedConditionCache[mIndex] = ConditionState::kUnknown;
163        VLOG("CombinationPredicate %lld sliced may changed? %d", (long long)mConditionId,
164            conditionChangedCache[mIndex] == true);
165    }
166}
167
168ConditionState CombinationConditionTracker::getMetConditionDimension(
169        const std::vector<sp<ConditionTracker>>& allConditions,
170        const std::vector<Matcher>& dimensionFields,
171        std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const {
172    vector<ConditionState> conditionCache(allConditions.size(), ConditionState::kNotEvaluated);
173    // So far, this is fine as there is at most one child having sliced output.
174    for (const int childIndex : mChildren) {
175        conditionCache[childIndex] = conditionCache[childIndex] |
176            allConditions[childIndex]->getMetConditionDimension(
177                allConditions, dimensionFields, dimensionsKeySet);
178    }
179    evaluateCombinationCondition(mChildren, mLogicalOperation, conditionCache);
180    if (conditionCache[mIndex] == ConditionState::kTrue && dimensionsKeySet.empty()) {
181        dimensionsKeySet.insert(DEFAULT_DIMENSION_KEY);
182    }
183    return conditionCache[mIndex];
184}
185
186}  // namespace statsd
187}  // namespace os
188}  // namespace android
189