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