David Chen | dd89694 | 2017-09-26 11:44:40 -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 "LogEntryMatcherManager.h" |
| 18 | #include <log/event_tag_map.h> |
| 19 | #include <log/logprint.h> |
| 20 | #include <utils/Errors.h> |
| 21 | #include <cutils/log.h> |
| 22 | #include <unordered_map> |
| 23 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
| 24 | |
| 25 | using std::unordered_map; |
| 26 | using std::string; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
| 32 | bool LogEntryMatcherManager::matches(const LogEntryMatcher &matcher, const int tagId, |
| 33 | const unordered_map<int, long> &intMap, |
| 34 | const unordered_map<int, string> &strMap, |
| 35 | const unordered_map<int, float> &floatMap, |
| 36 | const unordered_map<int, bool> &boolMap) { |
| 37 | if (matcher.has_combination()) { // Need to evaluate composite matching |
| 38 | switch (matcher.combination().operation()) { |
| 39 | case LogicalOperation::AND: |
| 40 | for (auto nestedMatcher : matcher.combination().matcher()) { |
| 41 | if (!matches(nestedMatcher, tagId, intMap, strMap, floatMap, boolMap)) { |
| 42 | return false; // return false if any nested matcher is false; |
| 43 | } |
| 44 | } |
| 45 | return true; // Otherwise, return true. |
| 46 | case LogicalOperation::OR: |
| 47 | for (auto nestedMatcher : matcher.combination().matcher()) { |
| 48 | if (matches(nestedMatcher, tagId, intMap, strMap, floatMap, boolMap)) { |
| 49 | return true; // return true if any nested matcher is true; |
| 50 | } |
| 51 | } |
| 52 | return false; |
| 53 | case LogicalOperation::NOT: |
| 54 | return !matches(matcher.combination().matcher(0), tagId, intMap, strMap, floatMap, |
| 55 | boolMap); |
| 56 | |
| 57 | // Case NAND is just inverting the return statement of AND |
| 58 | case LogicalOperation::NAND: |
| 59 | for (auto nestedMatcher : matcher.combination().matcher()) { |
| 60 | auto simple = nestedMatcher.simple_log_entry_matcher(); |
| 61 | if (!matches(nestedMatcher, tagId, intMap, strMap, floatMap, boolMap)) { |
| 62 | return true; // return false if any nested matcher is false; |
| 63 | } |
| 64 | } |
| 65 | return false; // Otherwise, return true. |
| 66 | case LogicalOperation::NOR: |
| 67 | for (auto nestedMatcher : matcher.combination().matcher()) { |
| 68 | if (matches(nestedMatcher, tagId, intMap, strMap, floatMap, boolMap)) { |
| 69 | return false; // return true if any nested matcher is true; |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } else { |
| 76 | return matchesSimple(matcher.simple_log_entry_matcher(), tagId, intMap, strMap, floatMap, |
| 77 | boolMap); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool LogEntryMatcherManager::matchesSimple(const SimpleLogEntryMatcher &simpleMatcher, |
| 82 | const int tagId, |
| 83 | const unordered_map<int, long> &intMap, |
| 84 | const unordered_map<int, string> &strMap, |
| 85 | const unordered_map<int, float> &floatMap, |
| 86 | const unordered_map<int, bool> &boolMap) { |
| 87 | for (int i = 0; i < simpleMatcher.tag_size(); i++) { |
| 88 | if (simpleMatcher.tag(i) != tagId) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | // now see if this event is interesting to us -- matches ALL the matchers |
| 93 | // defined in the metrics. |
| 94 | bool allMatched = true; |
| 95 | for (int j = 0; j < simpleMatcher.key_value_matcher_size(); j++) { |
| 96 | auto cur = simpleMatcher.key_value_matcher(j); |
| 97 | |
| 98 | // TODO: Check if this key is a magic key (eg package name). |
| 99 | int key = cur.key_matcher().key(); |
| 100 | |
| 101 | switch (cur.value_matcher_case()) { |
| 102 | case KeyValueMatcher::ValueMatcherCase::kEqString: { |
| 103 | auto it = strMap.find(key); |
| 104 | if (it == strMap.end() || cur.eq_string().compare(it->second) != 0) { |
| 105 | allMatched = false; |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | case KeyValueMatcher::ValueMatcherCase::kEqInt: { |
| 110 | auto it = intMap.find(key); |
| 111 | if (it == intMap.end() || cur.eq_int() != it->second) { |
| 112 | allMatched = false; |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | case KeyValueMatcher::ValueMatcherCase::kEqBool: { |
| 117 | auto it = boolMap.find(key); |
| 118 | if (it == boolMap.end() || cur.eq_bool() != it->second) { |
| 119 | allMatched = false; |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | // Begin numeric comparisons |
| 124 | case KeyValueMatcher::ValueMatcherCase::kLtInt: { |
| 125 | auto it = intMap.find(key); |
| 126 | if (it == intMap.end() || cur.lt_int() <= it->second) { |
| 127 | allMatched = false; |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | case KeyValueMatcher::ValueMatcherCase::kGtInt: { |
| 132 | auto it = intMap.find(key); |
| 133 | if (it == intMap.end() || cur.gt_int() >= it->second) { |
| 134 | allMatched = false; |
| 135 | } |
| 136 | break; |
| 137 | } |
| 138 | case KeyValueMatcher::ValueMatcherCase::kLtFloat: { |
| 139 | auto it = floatMap.find(key); |
| 140 | if (it == floatMap.end() || cur.lt_float() <= it->second) { |
| 141 | allMatched = false; |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | case KeyValueMatcher::ValueMatcherCase::kGtFloat: { |
| 146 | auto it = floatMap.find(key); |
| 147 | if (it == floatMap.end() || cur.gt_float() >= it->second) { |
| 148 | allMatched = false; |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | // Begin comparisons with equality |
| 153 | case KeyValueMatcher::ValueMatcherCase::kLteInt: { |
| 154 | auto it = intMap.find(key); |
| 155 | if (it == intMap.end() || cur.lte_int() < it->second) { |
| 156 | allMatched = false; |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | case KeyValueMatcher::ValueMatcherCase::kGteInt: { |
| 161 | auto it = intMap.find(key); |
| 162 | if (it == intMap.end() || cur.gte_int() > it->second) { |
| 163 | allMatched = false; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | case KeyValueMatcher::ValueMatcherCase::VALUE_MATCHER_NOT_SET: |
| 168 | // If value matcher is not present, assume that we match. |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (allMatched) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | } // namespace statsd |
| 181 | } // namespace os |
| 182 | } // namespace android |