• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ATOM_MATCHING_TRACKER_H
18 #define ATOM_MATCHING_TRACKER_H
19 
20 #include "src/statsd_config.pb.h"
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>
29 
30 namespace android {
31 namespace os {
32 namespace statsd {
33 
34 class AtomMatchingTracker : public virtual RefBase {
35 public:
AtomMatchingTracker(const int64_t & id,const int index,const uint64_t protoHash)36     AtomMatchingTracker(const int64_t& id, const int index, const uint64_t protoHash)
37         : mId(id), mIndex(index), mInitialized(false), mProtoHash(protoHash){};
38 
~AtomMatchingTracker()39     virtual ~AtomMatchingTracker(){};
40 
41     // Initialize this AtomMatchingTracker.
42     // allAtomMatchers: the list of the AtomMatcher proto config. This is needed because we don't
43     //                  store the proto object in memory. We only need it during initilization.
44     // allAtomMatchingTrackers: the list of the AtomMatchingTracker objects. It's a one-to-one
45     //                          mapping with allAtomMatchers. This is needed because the
46     //                          initialization is done recursively for
47     //                          CombinationAtomMatchingTrackers using DFS.
48     // stack: a bit map to record which matcher has been visited on the stack. This is for detecting
49     //        circle dependency.
50     virtual bool init(const std::vector<AtomMatcher>& allAtomMatchers,
51                       const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
52                       const std::unordered_map<int64_t, int>& matcherMap,
53                       std::vector<bool>& stack) = 0;
54 
55     // Update appropriate state on config updates. Primarily, all indices need to be updated.
56     // This matcher and all of its children are guaranteed to be preserved across the update.
57     // matcher: the AtomMatcher proto from the config.
58     // index: the index of this matcher in mAllAtomMatchingTrackers.
59     // atomMatchingTrackerMap: map from matcher id to index in mAllAtomMatchingTrackers
60     virtual bool onConfigUpdated(
61             const AtomMatcher& matcher, const int index,
62             const std::unordered_map<int64_t, int>& atomMatchingTrackerMap) = 0;
63 
64     // Called when a log event comes.
65     // event: the log event.
66     // allAtomMatchingTrackers: the list of all AtomMatchingTrackers. This is needed because the log
67     //                          processing is done recursively.
68     // matcherResults: The cached results for all matchers for this event. Parent matchers can
69     //                 directly access the children's matching results if they have been evaluated.
70     //                 Otherwise, call children matchers' onLogEvent.
71     virtual void onLogEvent(const LogEvent& event,
72                             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
73                             std::vector<MatchingState>& matcherResults) = 0;
74 
75     // Get the tagIds that this matcher cares about. The combined collection is stored
76     // in MetricMananger, so that we can pass any LogEvents that are not interest of us. It uses
77     // some memory but hopefully it can save us much CPU time when there is flood of events.
getAtomIds()78     virtual const std::set<int>& getAtomIds() const {
79         return mAtomIds;
80     }
81 
getId()82     int64_t getId() const {
83         return mId;
84     }
85 
getProtoHash()86     uint64_t getProtoHash() const {
87         return mProtoHash;
88     }
89 
90 protected:
91     // Name of this matching. We don't really need the name, but it makes log message easy to debug.
92     const int64_t mId;
93 
94     // Index of this AtomMatchingTracker in MetricsManager's container.
95     int mIndex;
96 
97     // Whether this AtomMatchingTracker has been properly initialized.
98     bool mInitialized;
99 
100     // The collection of the event tag ids that this AtomMatchingTracker cares. So we can quickly
101     // return kNotMatched when we receive an event with an id not in the list. This is especially
102     // useful when we have a complex CombinationAtomMatchingTracker.
103     std::set<int> mAtomIds;
104 
105     // Hash of the AtomMatcher's proto bytes from StatsdConfig.
106     // Used to determine if the definition of this matcher has changed across a config update.
107     const uint64_t mProtoHash;
108 
109     FRIEND_TEST(MetricsManagerTest, TestCreateAtomMatchingTrackerSimple);
110     FRIEND_TEST(MetricsManagerTest, TestCreateAtomMatchingTrackerCombination);
111     FRIEND_TEST(ConfigUpdateTest, TestUpdateMatchers);
112 };
113 
114 }  // namespace statsd
115 }  // namespace os
116 }  // namespace android
117 
118 #endif  // ATOM_MATCHING_TRACKER_H
119