blob: 15c067ee936dee7e9f270b44d0f04b2014edda3e [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 */
16
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
Yao Chencaf339d2017-10-06 16:01:10 -070018
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#include "CombinationLogMatchingTracker.h"
20#include "matchers/matcher_util.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
Yao Chencaf339d2017-10-06 16:01:10 -070026using std::set;
27using std::string;
28using std::unique_ptr;
29using std::unordered_map;
30using std::vector;
31
Yangster-mac94e197c2018-01-02 16:03:03 -080032CombinationLogMatchingTracker::CombinationLogMatchingTracker(const int64_t& id, const int index)
33 : LogMatchingTracker(id, index) {
Yao Chencaf339d2017-10-06 16:01:10 -070034}
35
36CombinationLogMatchingTracker::~CombinationLogMatchingTracker() {
37}
38
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080039bool CombinationLogMatchingTracker::init(const vector<AtomMatcher>& allLogMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -070040 const vector<sp<LogMatchingTracker>>& allTrackers,
Yangster-mac94e197c2018-01-02 16:03:03 -080041 const unordered_map<int64_t, int>& matcherMap,
Yao Chencaf339d2017-10-06 16:01:10 -070042 vector<bool>& stack) {
43 if (mInitialized) {
44 return true;
45 }
46
47 // mark this node as visited in the recursion stack.
48 stack[mIndex] = true;
49
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080050 AtomMatcher_Combination matcher = allLogMatchers[mIndex].combination();
Yao Chencaf339d2017-10-06 16:01:10 -070051
52 // LogicalOperation is missing in the config
53 if (!matcher.has_operation()) {
54 return false;
55 }
56
57 mLogicalOperation = matcher.operation();
58
59 if (mLogicalOperation == LogicalOperation::NOT && matcher.matcher_size() != 1) {
60 return false;
61 }
62
Yangster-mac94e197c2018-01-02 16:03:03 -080063 for (const auto& child : matcher.matcher()) {
Yao Chencaf339d2017-10-06 16:01:10 -070064 auto pair = matcherMap.find(child);
65 if (pair == matcherMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -080066 ALOGW("Matcher %lld not found in the config", (long long)child);
Yao Chencaf339d2017-10-06 16:01:10 -070067 return false;
68 }
69
70 int childIndex = pair->second;
71
72 // if the child is a visited node in the recursion -> circle detected.
73 if (stack[childIndex]) {
74 ALOGE("Circle detected in matcher config");
75 return false;
76 }
77
78 if (!allTrackers[childIndex]->init(allLogMatchers, allTrackers, matcherMap, stack)) {
Yangster-mac94e197c2018-01-02 16:03:03 -080079 ALOGW("child matcher init failed %lld", (long long)child);
Yao Chencaf339d2017-10-06 16:01:10 -070080 return false;
81 }
82
83 mChildren.push_back(childIndex);
84
Yangster-mac20877162017-12-22 17:19:39 -080085 const set<int>& childTagIds = allTrackers[childIndex]->getAtomIds();
86 mAtomIds.insert(childTagIds.begin(), childTagIds.end());
Yao Chencaf339d2017-10-06 16:01:10 -070087 }
88
89 mInitialized = true;
90 // unmark this node in the recursion stack.
91 stack[mIndex] = false;
92 return true;
93}
94
Joe Onoratoc4dfae52017-10-17 23:38:21 -070095void CombinationLogMatchingTracker::onLogEvent(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -070096 const vector<sp<LogMatchingTracker>>& allTrackers,
97 vector<MatchingState>& matcherResults) {
98 // this event has been processed.
99 if (matcherResults[mIndex] != MatchingState::kNotComputed) {
100 return;
101 }
102
Yangster-mac20877162017-12-22 17:19:39 -0800103 if (mAtomIds.find(event.GetTagId()) == mAtomIds.end()) {
Yao Chencaf339d2017-10-06 16:01:10 -0700104 matcherResults[mIndex] = MatchingState::kNotMatched;
105 return;
106 }
107
108 // evaluate children matchers if they haven't been evaluated.
109 for (const int childIndex : mChildren) {
110 if (matcherResults[childIndex] == MatchingState::kNotComputed) {
111 const sp<LogMatchingTracker>& child = allTrackers[childIndex];
112 child->onLogEvent(event, allTrackers, matcherResults);
113 }
114 }
115
116 bool matched = combinationMatch(mChildren, mLogicalOperation, matcherResults);
117 matcherResults[mIndex] = matched ? MatchingState::kMatched : MatchingState::kNotMatched;
118}
119
120} // namespace statsd
121} // namespace os
122} // namespace android