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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #pragma once |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 18 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | #include "condition/condition_util.h" |
| 20 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 21 | #include "matchers/LogMatchingTracker.h" |
| 22 | #include "matchers/matcher_util.h" |
| 23 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 24 | #include <log/logprint.h> |
| 25 | #include <utils/RefBase.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
| 33 | class ConditionTracker : public virtual RefBase { |
| 34 | public: |
| 35 | ConditionTracker(const std::string& name, const int index) |
| 36 | : mName(name), |
| 37 | mIndex(index), |
| 38 | mInitialized(false), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 39 | mTrackerIndex(), |
| 40 | mNonSlicedConditionState(ConditionState::kUnknown), |
| 41 | mSliced(false){}; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 42 | |
| 43 | virtual ~ConditionTracker(){}; |
| 44 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 45 | inline const string& getName() { return mName; } |
| 46 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 47 | // Initialize this ConditionTracker. This initialization is done recursively (DFS). It can also |
| 48 | // be done in the constructor, but we do it separately because (1) easy to return a bool to |
| 49 | // indicate whether the initialization is successful. (2) makes unit test easier. |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 50 | // allConditionConfig: the list of all Predicate config from statsd_config. |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 51 | // allConditionTrackers: the list of all ConditionTrackers (this is needed because we may also |
| 52 | // need to call init() on children conditions) |
| 53 | // conditionNameIndexMap: the mapping from condition name to its index. |
| 54 | // stack: a bit map to keep track which nodes have been visited on the stack in the recursion. |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 55 | virtual bool init(const std::vector<Predicate>& allConditionConfig, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 56 | const std::vector<sp<ConditionTracker>>& allConditionTrackers, |
| 57 | const std::unordered_map<std::string, int>& conditionNameIndexMap, |
| 58 | std::vector<bool>& stack) = 0; |
| 59 | |
| 60 | // evaluate current condition given the new event. |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 61 | // event: the new log event |
| 62 | // eventMatcherValues: the results of the LogMatcherTrackers. LogMatcherTrackers always process |
| 63 | // event before ConditionTrackers, because ConditionTracker depends on |
| 64 | // LogMatchingTrackers. |
| 65 | // mAllConditions: the list of all ConditionTracker |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 66 | // conditionCache: the cached non-sliced condition of the ConditionTrackers for this new event. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 67 | // conditionChanged: the bit map to record whether the condition has changed. |
| 68 | // If the condition has dimension, then any sub condition changes will report |
| 69 | // conditionChanged. |
| 70 | virtual void evaluateCondition(const LogEvent& event, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 71 | const std::vector<MatchingState>& eventMatcherValues, |
| 72 | const std::vector<sp<ConditionTracker>>& mAllConditions, |
| 73 | std::vector<ConditionState>& conditionCache, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 74 | std::vector<bool>& conditionChanged) = 0; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 75 | |
| 76 | // Return the current condition state. |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 77 | virtual ConditionState isConditionMet() const { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 78 | return mNonSlicedConditionState; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 81 | // Query the condition with parameters. |
| 82 | // [conditionParameters]: a map from condition name to the HashableDimensionKey to query the |
| 83 | // condition. |
| 84 | // [allConditions]: all condition trackers. This is needed because the condition evaluation is |
| 85 | // done recursively |
| 86 | // [conditionCache]: the cache holding the condition evaluation values. |
| 87 | virtual void isConditionMet( |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 88 | const ConditionKey& conditionParameters, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 89 | const std::vector<sp<ConditionTracker>>& allConditions, |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 90 | std::vector<ConditionState>& conditionCache) const = 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 91 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 92 | // return the list of LogMatchingTracker index that this ConditionTracker uses. |
| 93 | virtual const std::set<int>& getLogTrackerIndex() const { |
| 94 | return mTrackerIndex; |
| 95 | } |
| 96 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 97 | virtual void setSliced(bool sliced) { |
| 98 | mSliced = mSliced | sliced; |
| 99 | } |
| 100 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 101 | protected: |
| 102 | // We don't really need the string name, but having a name here makes log messages |
| 103 | // easy to debug. |
| 104 | const std::string mName; |
| 105 | |
| 106 | // the index of this condition in the manager's condition list. |
| 107 | const int mIndex; |
| 108 | |
| 109 | // if it's properly initialized. |
| 110 | bool mInitialized; |
| 111 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 112 | // the list of LogMatchingTracker index that this ConditionTracker uses. |
| 113 | std::set<int> mTrackerIndex; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 114 | |
| 115 | ConditionState mNonSlicedConditionState; |
| 116 | |
| 117 | bool mSliced; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace statsd |
| 121 | } // namespace os |
| 122 | } // namespace android |