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