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