blob: b691faea205d6d7546f3e5da572907050a7a3968 [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
Yao Chen5154a3792017-10-30 22:57:06 -070077 mDimension.insert(mDimension.begin(), simpleCondition.dimension().begin(),
78 simpleCondition.dimension().end());
79
80 if (mDimension.size() > 0) {
81 mSliced = true;
82 }
83
Yao Chencaf339d2017-10-06 16:01:10 -070084 mInitialized = true;
85}
86
87SimpleConditionTracker::~SimpleConditionTracker() {
88 VLOG("~SimpleConditionTracker()");
89}
90
91bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig,
92 const vector<sp<ConditionTracker>>& allConditionTrackers,
93 const unordered_map<string, int>& conditionNameIndexMap,
94 vector<bool>& stack) {
95 // SimpleConditionTracker does not have dependency on other conditions, thus we just return
96 // if the initialization was successful.
97 return mInitialized;
98}
99
Yao Chen729093d2017-10-16 10:33:26 -0700100void print(unordered_map<HashableDimensionKey, ConditionState>& conditions, const string& name) {
101 VLOG("%s DUMP:", name.c_str());
102
103 for (const auto& pair : conditions) {
104 VLOG("\t%s %d", pair.first.c_str(), pair.second);
105 }
106}
107
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700108bool SimpleConditionTracker::evaluateCondition(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -0700109 const vector<MatchingState>& eventMatcherValues,
110 const vector<sp<ConditionTracker>>& mAllConditions,
111 vector<ConditionState>& conditionCache,
Yao Chen729093d2017-10-16 10:33:26 -0700112 vector<bool>& nonSlicedConditionChanged,
113 std::vector<bool>& slicedConditionChanged) {
Yao Chencaf339d2017-10-06 16:01:10 -0700114 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
115 // it has been evaluated.
Yao Chen729093d2017-10-16 10:33:26 -0700116 VLOG("Yes, already evaluated, %s %d", mName.c_str(), mNonSlicedConditionState);
Yao Chencaf339d2017-10-06 16:01:10 -0700117 return false;
118 }
119
120 // Ignore nesting, because we know we cannot trust ourselves on tracking nesting conditions.
Yao Chen729093d2017-10-16 10:33:26 -0700121
122 ConditionState newCondition = mNonSlicedConditionState;
123 bool matched = false;
Yao Chencaf339d2017-10-06 16:01:10 -0700124 // Note: The order to evaluate the following start, stop, stop_all matters.
125 // The priority of overwrite is stop_all > stop > start.
126 if (mStartLogMatcherIndex >= 0 &&
127 eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen729093d2017-10-16 10:33:26 -0700128 matched = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700129 newCondition = ConditionState::kTrue;
130 }
131
132 if (mStopLogMatcherIndex >= 0 &&
133 eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen729093d2017-10-16 10:33:26 -0700134 matched = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700135 newCondition = ConditionState::kFalse;
136 }
137
Yao Chen729093d2017-10-16 10:33:26 -0700138 bool stopAll = false;
Yao Chencaf339d2017-10-06 16:01:10 -0700139 if (mStopAllLogMatcherIndex >= 0 &&
140 eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen729093d2017-10-16 10:33:26 -0700141 matched = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700142 newCondition = ConditionState::kFalse;
Yao Chen729093d2017-10-16 10:33:26 -0700143 stopAll = true;
Yao Chencaf339d2017-10-06 16:01:10 -0700144 }
145
Yao Chen729093d2017-10-16 10:33:26 -0700146 if (matched == false) {
147 slicedConditionChanged[mIndex] = false;
148 nonSlicedConditionChanged[mIndex] = false;
149 conditionCache[mIndex] = mNonSlicedConditionState;
150 return false;
151 }
152
153 bool nonSlicedChanged = mNonSlicedConditionState != newCondition;
154
155 bool slicedChanged = false;
156
157 if (stopAll) {
158 // TODO: handle stop all; all dimension should be cleared.
159 }
160
Yao Chen5154a3792017-10-30 22:57:06 -0700161
162 if (mDimension.size() > 0) {
163 HashableDimensionKey hashableKey = getHashableKey(getDimensionKey(event, mDimension));
164 if (mSlicedConditionState.find(hashableKey) == mSlicedConditionState.end() ||
165 mSlicedConditionState[hashableKey] != newCondition) {
166 slicedChanged = true;
167 mSlicedConditionState[hashableKey] = newCondition;
Yao Chen729093d2017-10-16 10:33:26 -0700168 }
Yao Chen5154a3792017-10-30 22:57:06 -0700169 VLOG("key: %s %d", hashableKey.c_str(), newCondition);
Yao Chen729093d2017-10-16 10:33:26 -0700170 // dump all dimensions for debugging
171 if (DEBUG) {
172 print(mSlicedConditionState, mName);
173 }
174 }
175
176 // even if this SimpleCondition is not sliced, it may be part of a sliced CombinationCondition
177 // if the nonSliced condition changed, it may affect the sliced condition in the parent node.
178 // so mark the slicedConditionChanged to be true.
179 // For example: APP_IN_BACKGROUND_OR_SCREEN_OFF
180 // APP_IN_BACKGROUND is sliced [App_A->True, App_B->False].
181 // SCREEN_OFF is not sliced, and it changes from False -> True;
182 // We need to populate this change to parent condition. Because for App_B,
183 // the APP_IN_BACKGROUND_OR_SCREEN_OFF condition would change from False->True.
184 slicedConditionChanged[mIndex] = mSliced ? slicedChanged : nonSlicedChanged;
185 nonSlicedConditionChanged[mIndex] = nonSlicedChanged;
186
187 VLOG("SimpleCondition %s nonSlicedChange? %d SlicedChanged? %d", mName.c_str(),
188 nonSlicedConditionChanged[mIndex] == true, slicedConditionChanged[mIndex] == true);
189 mNonSlicedConditionState = newCondition;
190 conditionCache[mIndex] = mNonSlicedConditionState;
191
192 return nonSlicedConditionChanged[mIndex];
193}
194
195void SimpleConditionTracker::isConditionMet(
196 const map<string, HashableDimensionKey>& conditionParameters,
197 const vector<sp<ConditionTracker>>& allConditions, vector<ConditionState>& conditionCache) {
198 const auto pair = conditionParameters.find(mName);
199 if (pair == conditionParameters.end()) {
200 // the query does not need my sliced condition. just return the non sliced condition.
201 conditionCache[mIndex] = mNonSlicedConditionState;
202 VLOG("Condition %s return %d", mName.c_str(), mNonSlicedConditionState);
203 return;
204 }
205
206 const HashableDimensionKey& key = pair->second;
207 VLOG("simpleCondition %s query key: %s", mName.c_str(), key.c_str());
208
209 if (mSlicedConditionState.find(key) == mSlicedConditionState.end()) {
210 // never seen this key before. the condition is unknown to us.
211 conditionCache[mIndex] = ConditionState::kUnknown;
212 } else {
213 conditionCache[mIndex] = mSlicedConditionState[key];
214 }
215
216 VLOG("Condition %s return %d", mName.c_str(), conditionCache[mIndex]);
217
218 if (DEBUG) {
219 print(mSlicedConditionState, mName);
220 }
Yao Chencaf339d2017-10-06 16:01:10 -0700221}
222
223} // namespace statsd
224} // namespace os
225} // namespace android