blob: aff476814c943f5fbe2674d9a0f968ca64f1691a [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 Chen5110bed2017-10-23 12:50:02 -070017#define DEBUG false // 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 Chen729093d2017-10-16 10:33:26 -070028using std::map;
Yao Chencaf339d2017-10-06 16:01:10 -070029using std::string;
30using std::unique_ptr;
31using std::unordered_map;
32using std::vector;
33
Yao Chencaf339d2017-10-06 16:01:10 -070034SimpleConditionTracker::SimpleConditionTracker(
35 const string& name, const int index, const SimpleCondition& simpleCondition,
36 const unordered_map<string, int>& trackerNameIndexMap)
37 : ConditionTracker(name, index) {
38 VLOG("creating SimpleConditionTracker %s", mName.c_str());
39 mCountNesting = simpleCondition.count_nesting();
40
41 if (simpleCondition.has_start()) {
42 auto pair = trackerNameIndexMap.find(simpleCondition.start());
43 if (pair == trackerNameIndexMap.end()) {
44 ALOGW("Start matcher %s not found in the config", simpleCondition.start().c_str());
45 return;
46 }
47 mStartLogMatcherIndex = pair->second;
48 mTrackerIndex.insert(mStartLogMatcherIndex);
49 } else {
50 mStartLogMatcherIndex = -1;
51 }
52
53 if (simpleCondition.has_stop()) {
54 auto pair = trackerNameIndexMap.find(simpleCondition.stop());
55 if (pair == trackerNameIndexMap.end()) {
56 ALOGW("Stop matcher %s not found in the config", simpleCondition.stop().c_str());
57 return;
58 }
59 mStopLogMatcherIndex = pair->second;
Yao Chen4b146852017-10-10 15:34:42 -070060 mTrackerIndex.insert(mStopLogMatcherIndex);
Yao Chencaf339d2017-10-06 16:01:10 -070061 } else {
62 mStopLogMatcherIndex = -1;
63 }
64
65 if (simpleCondition.has_stop_all()) {
66 auto pair = trackerNameIndexMap.find(simpleCondition.stop_all());
67 if (pair == trackerNameIndexMap.end()) {
Yao Chen729093d2017-10-16 10:33:26 -070068 ALOGW("Stop all matcher %s not found in the config", simpleCondition.stop().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070069 return;
70 }
71 mStopAllLogMatcherIndex = pair->second;
72 mTrackerIndex.insert(mStopAllLogMatcherIndex);
73 } else {
74 mStopAllLogMatcherIndex = -1;
75 }
76
77 mInitialized = true;
78}
79
80SimpleConditionTracker::~SimpleConditionTracker() {
81 VLOG("~SimpleConditionTracker()");
82}
83
84bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig,
85 const vector<sp<ConditionTracker>>& allConditionTrackers,
86 const unordered_map<string, int>& conditionNameIndexMap,
87 vector<bool>& stack) {
88 // SimpleConditionTracker does not have dependency on other conditions, thus we just return
89 // if the initialization was successful.
90 return mInitialized;
91}
92
Yao Chen729093d2017-10-16 10:33:26 -070093void print(unordered_map<HashableDimensionKey, ConditionState>& conditions, const string& name) {
94 VLOG("%s DUMP:", name.c_str());
95
96 for (const auto& pair : conditions) {
97 VLOG("\t%s %d", pair.first.c_str(), pair.second);
98 }
99}
100
101void SimpleConditionTracker::addDimensions(const std::vector<KeyMatcher>& keyMatchers) {
102 VLOG("Added dimensions size %lu", (unsigned long)keyMatchers.size());
103 mDimensionsList.push_back(keyMatchers);
104 mSliced = true;
105}
106
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700107bool SimpleConditionTracker::evaluateCondition(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -0700108 const vector<MatchingState>& eventMatcherValues,
109 const vector<sp<ConditionTracker>>& mAllConditions,
110 vector<ConditionState>& conditionCache,
Yao Chen729093d2017-10-16 10:33:26 -0700111 vector<bool>& nonSlicedConditionChanged,
112 std::vector<bool>& slicedConditionChanged) {
Yao Chencaf339d2017-10-06 16:01:10 -0700113 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
114 // it has been evaluated.
Yao Chen729093d2017-10-16 10:33:26 -0700115 VLOG("Yes, already evaluated, %s %d", mName.c_str(), mNonSlicedConditionState);
Yao Chencaf339d2017-10-06 16:01:10 -0700116 return false;
117 }
118
119 // Ignore nesting, because we know we cannot trust ourselves on tracking nesting conditions.
Yao Chen729093d2017-10-16 10:33:26 -0700120
121 ConditionState newCondition = mNonSlicedConditionState;
122 bool matched = false;
Yao Chencaf339d2017-10-06 16:01:10 -0700123 // Note: The order to evaluate the following start, stop, stop_all matters.
124 // The priority of overwrite is stop_all > stop > start.
125 if (mStartLogMatcherIndex >= 0 &&
126 eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen729093d2017-10-16 10:33:26 -0700127 matched = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700128 newCondition = ConditionState::kTrue;
129 }
130
131 if (mStopLogMatcherIndex >= 0 &&
132 eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen729093d2017-10-16 10:33:26 -0700133 matched = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700134 newCondition = ConditionState::kFalse;
135 }
136
Yao Chen729093d2017-10-16 10:33:26 -0700137 bool stopAll = false;
Yao Chencaf339d2017-10-06 16:01:10 -0700138 if (mStopAllLogMatcherIndex >= 0 &&
139 eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen729093d2017-10-16 10:33:26 -0700140 matched = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700141 newCondition = ConditionState::kFalse;
Yao Chen729093d2017-10-16 10:33:26 -0700142 stopAll = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700143 }
144
Yao Chen729093d2017-10-16 10:33:26 -0700145 if (matched == false) {
146 slicedConditionChanged[mIndex] = false;
147 nonSlicedConditionChanged[mIndex] = false;
148 conditionCache[mIndex] = mNonSlicedConditionState;
149 return false;
150 }
151
152 bool nonSlicedChanged = mNonSlicedConditionState != newCondition;
153
154 bool slicedChanged = false;
155
156 if (stopAll) {
157 // TODO: handle stop all; all dimension should be cleared.
158 }
159
160 if (mDimensionsList.size() > 0) {
161 for (size_t i = 0; i < mDimensionsList.size(); i++) {
162 const auto& dim = mDimensionsList[i];
163 vector<KeyValuePair> key = getDimensionKey(event, dim);
164 HashableDimensionKey hashableKey = getHashableKey(key);
165 if (mSlicedConditionState.find(hashableKey) == mSlicedConditionState.end() ||
166 mSlicedConditionState[hashableKey] != newCondition) {
167 slicedChanged = true;
168 mSlicedConditionState[hashableKey] = newCondition;
169 }
170 VLOG("key: %s %d", hashableKey.c_str(), newCondition);
171 }
172 // dump all dimensions for debugging
173 if (DEBUG) {
174 print(mSlicedConditionState, mName);
175 }
176 }
177
178 // even if this SimpleCondition is not sliced, it may be part of a sliced CombinationCondition
179 // if the nonSliced condition changed, it may affect the sliced condition in the parent node.
180 // so mark the slicedConditionChanged to be true.
181 // For example: APP_IN_BACKGROUND_OR_SCREEN_OFF
182 // APP_IN_BACKGROUND is sliced [App_A->True, App_B->False].
183 // SCREEN_OFF is not sliced, and it changes from False -> True;
184 // We need to populate this change to parent condition. Because for App_B,
185 // the APP_IN_BACKGROUND_OR_SCREEN_OFF condition would change from False->True.
186 slicedConditionChanged[mIndex] = mSliced ? slicedChanged : nonSlicedChanged;
187 nonSlicedConditionChanged[mIndex] = nonSlicedChanged;
188
189 VLOG("SimpleCondition %s nonSlicedChange? %d SlicedChanged? %d", mName.c_str(),
190 nonSlicedConditionChanged[mIndex] == true, slicedConditionChanged[mIndex] == true);
191 mNonSlicedConditionState = newCondition;
192 conditionCache[mIndex] = mNonSlicedConditionState;
193
194 return nonSlicedConditionChanged[mIndex];
195}
196
197void SimpleConditionTracker::isConditionMet(
198 const map<string, HashableDimensionKey>& conditionParameters,
199 const vector<sp<ConditionTracker>>& allConditions, vector<ConditionState>& conditionCache) {
200 const auto pair = conditionParameters.find(mName);
201 if (pair == conditionParameters.end()) {
202 // the query does not need my sliced condition. just return the non sliced condition.
203 conditionCache[mIndex] = mNonSlicedConditionState;
204 VLOG("Condition %s return %d", mName.c_str(), mNonSlicedConditionState);
205 return;
206 }
207
208 const HashableDimensionKey& key = pair->second;
209 VLOG("simpleCondition %s query key: %s", mName.c_str(), key.c_str());
210
211 if (mSlicedConditionState.find(key) == mSlicedConditionState.end()) {
212 // never seen this key before. the condition is unknown to us.
213 conditionCache[mIndex] = ConditionState::kUnknown;
214 } else {
215 conditionCache[mIndex] = mSlicedConditionState[key];
216 }
217
218 VLOG("Condition %s return %d", mName.c_str(), conditionCache[mIndex]);
219
220 if (DEBUG) {
221 print(mSlicedConditionState, mName);
222 }
Yao Chencaf339d2017-10-06 16:01:10 -0700223}
224
225} // namespace statsd
226} // namespace os
227} // namespace android