Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 18 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | #include "CombinationLogMatchingTracker.h" |
| 20 | #include "matchers/matcher_util.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 26 | using std::set; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 27 | using std::unordered_map; |
| 28 | using std::vector; |
| 29 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 30 | CombinationLogMatchingTracker::CombinationLogMatchingTracker(const int64_t& id, const int index) |
| 31 | : LogMatchingTracker(id, index) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | CombinationLogMatchingTracker::~CombinationLogMatchingTracker() { |
| 35 | } |
| 36 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 37 | bool CombinationLogMatchingTracker::init(const vector<AtomMatcher>& allLogMatchers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 38 | const vector<sp<LogMatchingTracker>>& allTrackers, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 39 | const unordered_map<int64_t, int>& matcherMap, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 40 | vector<bool>& stack) { |
| 41 | if (mInitialized) { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // mark this node as visited in the recursion stack. |
| 46 | stack[mIndex] = true; |
| 47 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 48 | AtomMatcher_Combination matcher = allLogMatchers[mIndex].combination(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 49 | |
| 50 | // LogicalOperation is missing in the config |
| 51 | if (!matcher.has_operation()) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | mLogicalOperation = matcher.operation(); |
| 56 | |
| 57 | if (mLogicalOperation == LogicalOperation::NOT && matcher.matcher_size() != 1) { |
| 58 | return false; |
| 59 | } |
| 60 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 61 | for (const auto& child : matcher.matcher()) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 62 | auto pair = matcherMap.find(child); |
| 63 | if (pair == matcherMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 64 | ALOGW("Matcher %lld not found in the config", (long long)child); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
| 68 | int childIndex = pair->second; |
| 69 | |
| 70 | // if the child is a visited node in the recursion -> circle detected. |
| 71 | if (stack[childIndex]) { |
| 72 | ALOGE("Circle detected in matcher config"); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if (!allTrackers[childIndex]->init(allLogMatchers, allTrackers, matcherMap, stack)) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 77 | ALOGW("child matcher init failed %lld", (long long)child); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
| 81 | mChildren.push_back(childIndex); |
| 82 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 83 | const set<int>& childTagIds = allTrackers[childIndex]->getAtomIds(); |
| 84 | mAtomIds.insert(childTagIds.begin(), childTagIds.end()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | mInitialized = true; |
| 88 | // unmark this node in the recursion stack. |
| 89 | stack[mIndex] = false; |
| 90 | return true; |
| 91 | } |
| 92 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 93 | void CombinationLogMatchingTracker::onLogEvent(const LogEvent& event, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 94 | const vector<sp<LogMatchingTracker>>& allTrackers, |
| 95 | vector<MatchingState>& matcherResults) { |
| 96 | // this event has been processed. |
| 97 | if (matcherResults[mIndex] != MatchingState::kNotComputed) { |
| 98 | return; |
| 99 | } |
| 100 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 101 | if (mAtomIds.find(event.GetTagId()) == mAtomIds.end()) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 102 | matcherResults[mIndex] = MatchingState::kNotMatched; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | // evaluate children matchers if they haven't been evaluated. |
| 107 | for (const int childIndex : mChildren) { |
| 108 | if (matcherResults[childIndex] == MatchingState::kNotComputed) { |
| 109 | const sp<LogMatchingTracker>& child = allTrackers[childIndex]; |
| 110 | child->onLogEvent(event, allTrackers, matcherResults); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool matched = combinationMatch(mChildren, mLogicalOperation, matcherResults); |
| 115 | matcherResults[mIndex] = matched ? MatchingState::kMatched : MatchingState::kNotMatched; |
| 116 | } |
| 117 | |
| 118 | } // namespace statsd |
| 119 | } // namespace os |
| 120 | } // namespace android |