blob: fa583bf6e6ec6e847a84fe38fa0d7e85d2265138 [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
Yao Chencaf339d2017-10-06 16:01:10 -070017#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Yao Chencaf339d2017-10-06 16:01:10 -070019
20#include "SimpleConditionTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021
Yao Chencaf339d2017-10-06 16:01:10 -070022#include <log/logprint.h>
23
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024namespace android {
25namespace os {
26namespace statsd {
27
Yao Chencaf339d2017-10-06 16:01:10 -070028using std::string;
29using std::unique_ptr;
30using std::unordered_map;
31using std::vector;
32
Yao Chencaf339d2017-10-06 16:01:10 -070033SimpleConditionTracker::SimpleConditionTracker(
34 const string& name, const int index, const SimpleCondition& simpleCondition,
35 const unordered_map<string, int>& trackerNameIndexMap)
36 : ConditionTracker(name, index) {
37 VLOG("creating SimpleConditionTracker %s", mName.c_str());
38 mCountNesting = simpleCondition.count_nesting();
39
40 if (simpleCondition.has_start()) {
41 auto pair = trackerNameIndexMap.find(simpleCondition.start());
42 if (pair == trackerNameIndexMap.end()) {
43 ALOGW("Start matcher %s not found in the config", simpleCondition.start().c_str());
44 return;
45 }
46 mStartLogMatcherIndex = pair->second;
47 mTrackerIndex.insert(mStartLogMatcherIndex);
48 } else {
49 mStartLogMatcherIndex = -1;
50 }
51
52 if (simpleCondition.has_stop()) {
53 auto pair = trackerNameIndexMap.find(simpleCondition.stop());
54 if (pair == trackerNameIndexMap.end()) {
55 ALOGW("Stop matcher %s not found in the config", simpleCondition.stop().c_str());
56 return;
57 }
58 mStopLogMatcherIndex = pair->second;
Yao Chen4b146852017-10-10 15:34:42 -070059 mTrackerIndex.insert(mStopLogMatcherIndex);
Yao Chencaf339d2017-10-06 16:01:10 -070060 } else {
61 mStopLogMatcherIndex = -1;
62 }
63
64 if (simpleCondition.has_stop_all()) {
65 auto pair = trackerNameIndexMap.find(simpleCondition.stop_all());
66 if (pair == trackerNameIndexMap.end()) {
67 ALOGW("Stop matcher %s not found in the config", simpleCondition.stop().c_str());
68 return;
69 }
70 mStopAllLogMatcherIndex = pair->second;
71 mTrackerIndex.insert(mStopAllLogMatcherIndex);
72 } else {
73 mStopAllLogMatcherIndex = -1;
74 }
75
76 mInitialized = true;
77}
78
79SimpleConditionTracker::~SimpleConditionTracker() {
80 VLOG("~SimpleConditionTracker()");
81}
82
83bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig,
84 const vector<sp<ConditionTracker>>& allConditionTrackers,
85 const unordered_map<string, int>& conditionNameIndexMap,
86 vector<bool>& stack) {
87 // SimpleConditionTracker does not have dependency on other conditions, thus we just return
88 // if the initialization was successful.
89 return mInitialized;
90}
91
Joe Onoratoc4dfae52017-10-17 23:38:21 -070092bool SimpleConditionTracker::evaluateCondition(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -070093 const vector<MatchingState>& eventMatcherValues,
94 const vector<sp<ConditionTracker>>& mAllConditions,
95 vector<ConditionState>& conditionCache,
96 vector<bool>& changedCache) {
97 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
98 // it has been evaluated.
99 VLOG("Yes, already evaluated, %s %d", mName.c_str(), mConditionState);
100 return false;
101 }
102
103 // Ignore nesting, because we know we cannot trust ourselves on tracking nesting conditions.
104 ConditionState newCondition = mConditionState;
105 // Note: The order to evaluate the following start, stop, stop_all matters.
106 // The priority of overwrite is stop_all > stop > start.
107 if (mStartLogMatcherIndex >= 0 &&
108 eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
109 newCondition = ConditionState::kTrue;
110 }
111
112 if (mStopLogMatcherIndex >= 0 &&
113 eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
114 newCondition = ConditionState::kFalse;
115 }
116
117 if (mStopAllLogMatcherIndex >= 0 &&
118 eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
119 newCondition = ConditionState::kFalse;
120 }
121
122 bool changed = (mConditionState != newCondition);
123 mConditionState = newCondition;
124 conditionCache[mIndex] = mConditionState;
125 changedCache[mIndex] = changed;
126 return changed;
127}
128
129} // namespace statsd
130} // namespace os
131} // namespace android