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 | #ifndef LOG_MATCHING_TRACKER_H |
| 18 | #define LOG_MATCHING_TRACKER_H |
| 19 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 20 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 21 | #include "logd/LogEvent.h" |
| 22 | #include "matchers/matcher_util.h" |
| 23 | |
| 24 | #include <utils/RefBase.h> |
| 25 | |
| 26 | #include <set> |
| 27 | #include <unordered_map> |
| 28 | #include <vector> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
| 34 | class LogMatchingTracker : public virtual RefBase { |
| 35 | public: |
| 36 | LogMatchingTracker(const std::string& name, const int index) |
| 37 | : mName(name), mIndex(index), mInitialized(false){}; |
| 38 | |
| 39 | virtual ~LogMatchingTracker(){}; |
| 40 | |
| 41 | // Initialize this LogMatchingTracker. |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 42 | // allLogMatchers: the list of the AtomMatcher proto config. This is needed because we don't |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 43 | // store the proto object in memory. We only need it during initilization. |
| 44 | // allTrackers: the list of the LogMatchingTracker objects. It's a one-to-one mapping with |
| 45 | // allLogMatchers. This is needed because the initialization is done recursively |
| 46 | // for CombinationLogMatchingTrackers using DFS. |
| 47 | // stack: a bit map to record which matcher has been visited on the stack. This is for detecting |
| 48 | // circle dependency. |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 49 | virtual bool init(const std::vector<AtomMatcher>& allLogMatchers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 50 | const std::vector<sp<LogMatchingTracker>>& allTrackers, |
| 51 | const std::unordered_map<std::string, int>& matcherMap, |
| 52 | std::vector<bool>& stack) = 0; |
| 53 | |
| 54 | // Called when a log event comes. |
| 55 | // event: the log event. |
| 56 | // allTrackers: the list of all LogMatchingTrackers. This is needed because the log processing |
| 57 | // is done recursively. |
| 58 | // matcherResults: The cached results for all matchers for this event. Parent matchers can |
| 59 | // directly access the children's matching results if they have been evaluated. |
| 60 | // Otherwise, call children matchers' onLogEvent. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 61 | virtual void onLogEvent(const LogEvent& event, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 62 | const std::vector<sp<LogMatchingTracker>>& allTrackers, |
| 63 | std::vector<MatchingState>& matcherResults) = 0; |
| 64 | |
| 65 | // Get the tagIds that this matcher cares about. The combined collection is stored |
| 66 | // in MetricMananger, so that we can pass any LogEvents that are not interest of us. It uses |
| 67 | // some memory but hopefully it can save us much CPU time when there is flood of events. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 68 | virtual const std::set<int>& getAtomIds() const { |
| 69 | return mAtomIds; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 72 | const std::string& getName() const { |
| 73 | return mName; |
| 74 | } |
| 75 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 76 | protected: |
| 77 | // Name of this matching. We don't really need the name, but it makes log message easy to debug. |
| 78 | const std::string mName; |
| 79 | |
| 80 | // Index of this LogMatchingTracker in MetricsManager's container. |
| 81 | const int mIndex; |
| 82 | |
| 83 | // Whether this LogMatchingTracker has been properly initialized. |
| 84 | bool mInitialized; |
| 85 | |
| 86 | // The collection of the event tag ids that this LogMatchingTracker cares. So we can quickly |
| 87 | // return kNotMatched when we receive an event with an id not in the list. This is especially |
| 88 | // useful when we have a complex CombinationLogMatcherTracker. |
| 89 | // TODO: Consider use an array instead of stl set. In reality, the number of the tag ids a |
| 90 | // LogMatchingTracker cares is only a few. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 91 | std::set<int> mAtomIds; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace statsd |
| 95 | } // namespace os |
| 96 | } // namespace android |
| 97 | |
| 98 | #endif // LOG_MATCHING_TRACKER_H |