blob: 4f30a047e2560d2dacf75e0dc2ff5bfd3e8deddf [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
17#ifndef LOG_MATCHING_TRACKER_H
18#define LOG_MATCHING_TRACKER_H
19
Yao Chencaf339d2017-10-06 16:01:10 -070020#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070021#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 Chencaf339d2017-10-06 16:01:10 -070029
30namespace android {
31namespace os {
32namespace statsd {
33
34class LogMatchingTracker : public virtual RefBase {
35public:
Yangster-mac94e197c2018-01-02 16:03:03 -080036 LogMatchingTracker(const int64_t& id, const int index)
37 : mId(id), mIndex(index), mInitialized(false){};
Yao Chencaf339d2017-10-06 16:01:10 -070038
39 virtual ~LogMatchingTracker(){};
40
41 // Initialize this LogMatchingTracker.
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080042 // allLogMatchers: the list of the AtomMatcher proto config. This is needed because we don't
Yao Chencaf339d2017-10-06 16:01:10 -070043 // 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 Lafonb8c9aa82017-12-03 14:27:25 -080049 virtual bool init(const std::vector<AtomMatcher>& allLogMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -070050 const std::vector<sp<LogMatchingTracker>>& allTrackers,
Yangster-mac94e197c2018-01-02 16:03:03 -080051 const std::unordered_map<int64_t, int>& matcherMap,
Yao Chencaf339d2017-10-06 16:01:10 -070052 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 Onoratoc4dfae52017-10-17 23:38:21 -070061 virtual void onLogEvent(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -070062 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-mac20877162017-12-22 17:19:39 -080068 virtual const std::set<int>& getAtomIds() const {
69 return mAtomIds;
Yao Chencaf339d2017-10-06 16:01:10 -070070 }
71
Yangster-mac94e197c2018-01-02 16:03:03 -080072 const int64_t& getId() const {
73 return mId;
Yao Chenb3561512017-11-21 18:07:17 -080074 }
75
Yao Chencaf339d2017-10-06 16:01:10 -070076protected:
77 // Name of this matching. We don't really need the name, but it makes log message easy to debug.
Yangster-mac94e197c2018-01-02 16:03:03 -080078 const int64_t mId;
Yao Chencaf339d2017-10-06 16:01:10 -070079
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-mac20877162017-12-22 17:19:39 -080091 std::set<int> mAtomIds;
Yao Chencaf339d2017-10-06 16:01:10 -070092};
93
94} // namespace statsd
95} // namespace os
96} // namespace android
97
98#endif // LOG_MATCHING_TRACKER_H