blob: 1154b6fed1771d3d6703f8a9685412eb6b24ba51 [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#pragma once
Yao Chencaf339d2017-10-06 16:01:10 -070018
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#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 Chencaf339d2017-10-06 16:01:10 -070024#include <log/logprint.h>
25#include <utils/RefBase.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026
Yao Chencaf339d2017-10-06 16:01:10 -070027#include <unordered_map>
Yao Chencaf339d2017-10-06 16:01:10 -070028
29namespace android {
30namespace os {
31namespace statsd {
32
33class ConditionTracker : public virtual RefBase {
34public:
35 ConditionTracker(const std::string& name, const int index)
36 : mName(name),
37 mIndex(index),
38 mInitialized(false),
Yao Chen729093d2017-10-16 10:33:26 -070039 mTrackerIndex(),
40 mNonSlicedConditionState(ConditionState::kUnknown),
41 mSliced(false){};
Yao Chencaf339d2017-10-06 16:01:10 -070042
43 virtual ~ConditionTracker(){};
44
Yangster-mac20877162017-12-22 17:19:39 -080045 inline const string& getName() { return mName; }
46
Yao Chencaf339d2017-10-06 16:01:10 -070047 // 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 Lafon12d01fa2017-12-04 20:56:09 -080050 // allConditionConfig: the list of all Predicate config from statsd_config.
Yao Chencaf339d2017-10-06 16:01:10 -070051 // 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 Lafon12d01fa2017-12-04 20:56:09 -080055 virtual bool init(const std::vector<Predicate>& allConditionConfig,
Yao Chencaf339d2017-10-06 16:01:10 -070056 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 Chencaf339d2017-10-06 16:01:10 -070061 // 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 Chen729093d2017-10-16 10:33:26 -070066 // conditionCache: the cached non-sliced condition of the ConditionTrackers for this new event.
Yao Chen967b2052017-11-07 16:36:43 -080067 // 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 Chencaf339d2017-10-06 16:01:10 -070071 const std::vector<MatchingState>& eventMatcherValues,
72 const std::vector<sp<ConditionTracker>>& mAllConditions,
73 std::vector<ConditionState>& conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -080074 std::vector<bool>& conditionChanged) = 0;
Yao Chencaf339d2017-10-06 16:01:10 -070075
76 // Return the current condition state.
Yangster7c334a12017-11-22 14:24:24 -080077 virtual ConditionState isConditionMet() const {
Yao Chen729093d2017-10-16 10:33:26 -070078 return mNonSlicedConditionState;
Yao Chencaf339d2017-10-06 16:01:10 -070079 };
80
Yao Chen729093d2017-10-16 10:33:26 -070081 // 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-mac20877162017-12-22 17:19:39 -080088 const ConditionKey& conditionParameters,
Yao Chen729093d2017-10-16 10:33:26 -070089 const std::vector<sp<ConditionTracker>>& allConditions,
Yangster7c334a12017-11-22 14:24:24 -080090 std::vector<ConditionState>& conditionCache) const = 0;
Yao Chen729093d2017-10-16 10:33:26 -070091
Yao Chencaf339d2017-10-06 16:01:10 -070092 // return the list of LogMatchingTracker index that this ConditionTracker uses.
93 virtual const std::set<int>& getLogTrackerIndex() const {
94 return mTrackerIndex;
95 }
96
Yao Chen729093d2017-10-16 10:33:26 -070097 virtual void setSliced(bool sliced) {
98 mSliced = mSliced | sliced;
99 }
100
Yao Chencaf339d2017-10-06 16:01:10 -0700101protected:
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 Chencaf339d2017-10-06 16:01:10 -0700112 // the list of LogMatchingTracker index that this ConditionTracker uses.
113 std::set<int> mTrackerIndex;
Yao Chen729093d2017-10-16 10:33:26 -0700114
115 ConditionState mNonSlicedConditionState;
116
117 bool mSliced;
Yao Chencaf339d2017-10-06 16:01:10 -0700118};
119
120} // namespace statsd
121} // namespace os
122} // namespace android