• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "Log.h"
18 
19 #include "CombinationAtomMatchingTracker.h"
20 
21 #include "matchers/matcher_util.h"
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
27 using std::set;
28 using std::unordered_map;
29 using std::vector;
30 
CombinationAtomMatchingTracker(const int64_t & id,const int index,const uint64_t protoHash)31 CombinationAtomMatchingTracker::CombinationAtomMatchingTracker(const int64_t& id, const int index,
32                                                                const uint64_t protoHash)
33     : AtomMatchingTracker(id, index, protoHash) {
34 }
35 
~CombinationAtomMatchingTracker()36 CombinationAtomMatchingTracker::~CombinationAtomMatchingTracker() {
37 }
38 
init(const vector<AtomMatcher> & allAtomMatchers,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,const unordered_map<int64_t,int> & matcherMap,vector<bool> & stack)39 optional<InvalidConfigReason> CombinationAtomMatchingTracker::init(
40         const vector<AtomMatcher>& allAtomMatchers,
41         const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
42         const unordered_map<int64_t, int>& matcherMap, vector<bool>& stack) {
43     if (mInitialized) {
44         return nullopt;
45     }
46 
47     // mark this node as visited in the recursion stack.
48     stack[mIndex] = true;
49 
50     AtomMatcher_Combination matcher = allAtomMatchers[mIndex].combination();
51 
52     // LogicalOperation is missing in the config
53     if (!matcher.has_operation()) {
54         return createInvalidConfigReasonWithMatcher(INVALID_CONFIG_REASON_MATCHER_NO_OPERATION,
55                                                     mId);
56     }
57 
58     mLogicalOperation = matcher.operation();
59 
60     if (mLogicalOperation == LogicalOperation::NOT && matcher.matcher_size() != 1) {
61         return createInvalidConfigReasonWithMatcher(
62                 INVALID_CONFIG_REASON_MATCHER_NOT_OPERATION_IS_NOT_UNARY, mId);
63     }
64 
65     for (const auto& child : matcher.matcher()) {
66         auto pair = matcherMap.find(child);
67         if (pair == matcherMap.end()) {
68             ALOGW("Matcher %lld not found in the config", (long long)child);
69             optional<InvalidConfigReason> invalidConfigReason =
70                     createInvalidConfigReasonWithMatcher(
71                             INVALID_CONFIG_REASON_MATCHER_CHILD_NOT_FOUND, mId);
72             invalidConfigReason->matcherIds.push_back(child);
73             return invalidConfigReason;
74         }
75 
76         int childIndex = pair->second;
77 
78         // if the child is a visited node in the recursion -> circle detected.
79         if (stack[childIndex]) {
80             ALOGE("Circle detected in matcher config");
81             optional<InvalidConfigReason> invalidConfigReason =
82                     createInvalidConfigReasonWithMatcher(INVALID_CONFIG_REASON_MATCHER_CYCLE, mId);
83             invalidConfigReason->matcherIds.push_back(child);
84             return invalidConfigReason;
85         }
86         optional<InvalidConfigReason> invalidConfigReason =
87                 allAtomMatchingTrackers[childIndex]->init(allAtomMatchers, allAtomMatchingTrackers,
88                                                           matcherMap, stack);
89         if (invalidConfigReason.has_value()) {
90             ALOGW("child matcher init failed %lld", (long long)child);
91             invalidConfigReason->matcherIds.push_back(mId);
92             return invalidConfigReason;
93         }
94 
95         mChildren.push_back(childIndex);
96 
97         const set<int>& childTagIds = allAtomMatchingTrackers[childIndex]->getAtomIds();
98         mAtomIds.insert(childTagIds.begin(), childTagIds.end());
99     }
100 
101     mInitialized = true;
102     // unmark this node in the recursion stack.
103     stack[mIndex] = false;
104     return nullopt;
105 }
106 
onConfigUpdated(const AtomMatcher & matcher,const int index,const unordered_map<int64_t,int> & atomMatchingTrackerMap)107 optional<InvalidConfigReason> CombinationAtomMatchingTracker::onConfigUpdated(
108         const AtomMatcher& matcher, const int index,
109         const unordered_map<int64_t, int>& atomMatchingTrackerMap) {
110     mIndex = index;
111     mChildren.clear();
112     AtomMatcher_Combination combinationMatcher = matcher.combination();
113     for (const int64_t child : combinationMatcher.matcher()) {
114         const auto& pair = atomMatchingTrackerMap.find(child);
115         if (pair == atomMatchingTrackerMap.end()) {
116             ALOGW("Matcher %lld not found in the config", (long long)child);
117             optional<InvalidConfigReason> invalidConfigReason =
118                     createInvalidConfigReasonWithMatcher(
119                             INVALID_CONFIG_REASON_MATCHER_CHILD_NOT_FOUND, matcher.id());
120             invalidConfigReason->matcherIds.push_back(child);
121             return invalidConfigReason;
122         }
123         mChildren.push_back(pair->second);
124     }
125     return nullopt;
126 }
127 
onLogEvent(const LogEvent & event,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,vector<MatchingState> & matcherResults)128 void CombinationAtomMatchingTracker::onLogEvent(
129         const LogEvent& event, const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
130         vector<MatchingState>& matcherResults) {
131     // this event has been processed.
132     if (matcherResults[mIndex] != MatchingState::kNotComputed) {
133         return;
134     }
135 
136     if (mAtomIds.find(event.GetTagId()) == mAtomIds.end()) {
137         matcherResults[mIndex] = MatchingState::kNotMatched;
138         return;
139     }
140 
141     // evaluate children matchers if they haven't been evaluated.
142     for (const int childIndex : mChildren) {
143         if (matcherResults[childIndex] == MatchingState::kNotComputed) {
144             const sp<AtomMatchingTracker>& child = allAtomMatchingTrackers[childIndex];
145             child->onLogEvent(event, allAtomMatchingTrackers, matcherResults);
146         }
147     }
148 
149     bool matched = combinationMatch(mChildren, mLogicalOperation, matcherResults);
150     matcherResults[mIndex] = matched ? MatchingState::kMatched : MatchingState::kNotMatched;
151 }
152 
153 }  // namespace statsd
154 }  // namespace os
155 }  // namespace android
156