Home | History | Annotate | Download | only in matchers
      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 
     20 #include "frameworks/base/cmds/statsd/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 LogMatchingTracker : public virtual RefBase {
     35 public:
     36     LogMatchingTracker(const int64_t& id, const int index)
     37         : mId(id), mIndex(index), mInitialized(false){};
     38 
     39     virtual ~LogMatchingTracker(){};
     40 
     41     // Initialize this LogMatchingTracker.
     42     // allLogMatchers: 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     // 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.
     49     virtual bool init(const std::vector<AtomMatcher>& allLogMatchers,
     50                       const std::vector<sp<LogMatchingTracker>>& allTrackers,
     51                       const std::unordered_map<int64_t, 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.
     61     virtual void onLogEvent(const LogEvent& event,
     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.
     68     virtual const std::set<int>& getAtomIds() const {
     69         return mAtomIds;
     70     }
     71 
     72     const int64_t& getId() const {
     73         return mId;
     74     }
     75 
     76 protected:
     77     // Name of this matching. We don't really need the name, but it makes log message easy to debug.
     78     const int64_t mId;
     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.
     91     std::set<int> mAtomIds;
     92 };
     93 
     94 }  // namespace statsd
     95 }  // namespace os
     96 }  // namespace android
     97 
     98 #endif  // LOG_MATCHING_TRACKER_H
     99