blob: ea5c64fd2b3a4521a7086fff2e4ddc03360f5e25 [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 Chen967b2052017-11-07 16:36:43 -080077 mOutputDimension.insert(mOutputDimension.begin(), simpleCondition.dimension().begin(),
78 simpleCondition.dimension().end());
Yao Chen5154a3792017-10-30 22:57:06 -070079
Yao Chen967b2052017-11-07 16:36:43 -080080 if (mOutputDimension.size() > 0) {
Yao Chen5154a3792017-10-30 22:57:06 -070081 mSliced = true;
82 }
83
Yao Chen967b2052017-11-07 16:36:43 -080084 if (simpleCondition.initial_value() == SimpleCondition_InitialValue_FALSE) {
85 mInitialValue = ConditionState::kFalse;
86 } else {
87 mInitialValue = ConditionState::kUnknown;
88 }
89
90 mNonSlicedConditionState = mInitialValue;
91
Yao Chencaf339d2017-10-06 16:01:10 -070092 mInitialized = true;
93}
94
95SimpleConditionTracker::~SimpleConditionTracker() {
96 VLOG("~SimpleConditionTracker()");
97}
98
99bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig,
100 const vector<sp<ConditionTracker>>& allConditionTrackers,
101 const unordered_map<string, int>& conditionNameIndexMap,
102 vector<bool>& stack) {
103 // SimpleConditionTracker does not have dependency on other conditions, thus we just return
104 // if the initialization was successful.
105 return mInitialized;
106}
107
Yao Chen967b2052017-11-07 16:36:43 -0800108void print(map<HashableDimensionKey, int>& conditions, const string& name) {
Yao Chen729093d2017-10-16 10:33:26 -0700109 VLOG("%s DUMP:", name.c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700110 for (const auto& pair : conditions) {
Yao Chen967b2052017-11-07 16:36:43 -0800111 VLOG("\t%s : %d", pair.first.c_str(), pair.second);
Yao Chen729093d2017-10-16 10:33:26 -0700112 }
113}
114
Yao Chen967b2052017-11-07 16:36:43 -0800115void SimpleConditionTracker::handleStopAll(std::vector<ConditionState>& conditionCache,
116 std::vector<bool>& conditionChangedCache) {
117 // Unless the default condition is false, and there was nothing started, otherwise we have
118 // triggered a condition change.
119 conditionChangedCache[mIndex] =
120 (mInitialValue == ConditionState::kFalse && mSlicedConditionState.empty()) ? false
121 : true;
122
123 // After StopAll, we know everything has stopped. From now on, default condition is false.
124 mInitialValue = ConditionState::kFalse;
125 mSlicedConditionState.clear();
126 conditionCache[mIndex] = ConditionState::kFalse;
127}
128
129void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey,
130 bool matchStart,
131 std::vector<ConditionState>& conditionCache,
132 std::vector<bool>& conditionChangedCache) {
133 bool changed = false;
134 auto outputIt = mSlicedConditionState.find(outputKey);
135 ConditionState newCondition;
136 if (outputIt == mSlicedConditionState.end()) {
137 // We get a new output key.
138 newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse;
139 if (matchStart && mInitialValue != ConditionState::kTrue) {
140 mSlicedConditionState[outputKey] = 1;
141 changed = true;
142 } else if (mInitialValue != ConditionState::kFalse) {
143 // it's a stop and we don't have history about it.
144 // If the default condition is not false, it means this stop is valuable to us.
145 mSlicedConditionState[outputKey] = 0;
146 changed = true;
147 }
148 } else {
149 // we have history about this output key.
150 auto& startedCount = outputIt->second;
151 // assign the old value first.
152 newCondition = startedCount > 0 ? ConditionState::kTrue : ConditionState::kFalse;
153 if (matchStart) {
154 if (startedCount == 0) {
155 // This condition for this output key will change from false -> true
156 changed = true;
157 }
158
159 // it's ok to do ++ here, even if we don't count nesting. The >1 counts will be treated
160 // as 1 if not counting nesting.
161 startedCount++;
162 newCondition = ConditionState::kTrue;
163 } else {
164 // This is a stop event.
165 if (startedCount > 0) {
166 if (mCountNesting) {
167 startedCount--;
168 if (startedCount == 0) {
169 newCondition = ConditionState::kFalse;
170 }
171 } else {
172 // not counting nesting, so ignore the number of starts, stop now.
173 startedCount = 0;
174 newCondition = ConditionState::kFalse;
175 }
176 // if everything has stopped for this output key, condition true -> false;
177 if (startedCount == 0) {
178 changed = true;
179 }
180 }
181
182 // if default condition is false, it means we don't need to keep the false values.
183 if (mInitialValue == ConditionState::kFalse && startedCount == 0) {
184 mSlicedConditionState.erase(outputIt);
185 VLOG("erase key %s", outputKey.c_str());
186 }
187 }
188 }
189
190 // dump all dimensions for debugging
191 if (DEBUG) {
192 print(mSlicedConditionState, mName);
193 }
194
195 conditionChangedCache[mIndex] = changed;
196 conditionCache[mIndex] = newCondition;
197
198 VLOG("SimpleCondition %s nonSlicedChange? %d", mName.c_str(),
199 conditionChangedCache[mIndex] == true);
200}
201
202void SimpleConditionTracker::evaluateCondition(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -0700203 const vector<MatchingState>& eventMatcherValues,
204 const vector<sp<ConditionTracker>>& mAllConditions,
205 vector<ConditionState>& conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800206 vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700207 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
208 // it has been evaluated.
Yao Chend41c4222017-11-15 19:26:14 -0800209 VLOG("Yes, already evaluated, %s %d", mName.c_str(), conditionCache[mIndex]);
Yao Chen967b2052017-11-07 16:36:43 -0800210 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700211 }
212
Yao Chen967b2052017-11-07 16:36:43 -0800213 if (mStopAllLogMatcherIndex >= 0 &&
214 eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
215 handleStopAll(conditionCache, conditionChangedCache);
216 return;
217 }
Yao Chen729093d2017-10-16 10:33:26 -0700218
Yao Chen967b2052017-11-07 16:36:43 -0800219 int matchedState = -1;
Yao Chencaf339d2017-10-06 16:01:10 -0700220 // Note: The order to evaluate the following start, stop, stop_all matters.
221 // The priority of overwrite is stop_all > stop > start.
222 if (mStartLogMatcherIndex >= 0 &&
223 eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen967b2052017-11-07 16:36:43 -0800224 matchedState = 1;
Yao Chencaf339d2017-10-06 16:01:10 -0700225 }
226
227 if (mStopLogMatcherIndex >= 0 &&
228 eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen967b2052017-11-07 16:36:43 -0800229 matchedState = 0;
Yao Chencaf339d2017-10-06 16:01:10 -0700230 }
231
Yao Chen967b2052017-11-07 16:36:43 -0800232 if (matchedState < 0) {
Yao Chend41c4222017-11-15 19:26:14 -0800233 // The event doesn't match this condition. So we just report existing condition values.
Yao Chen967b2052017-11-07 16:36:43 -0800234 conditionChangedCache[mIndex] = false;
Yao Chend41c4222017-11-15 19:26:14 -0800235 if (mSliced) {
236 // if the condition result is sliced. metrics won't directly get value from the
237 // cache, so just set any value other than kNotEvaluated.
238 conditionCache[mIndex] = ConditionState::kUnknown;
Yao Chend41c4222017-11-15 19:26:14 -0800239 } else {
Yangster7c334a12017-11-22 14:24:24 -0800240 const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY);
241 if (itr == mSlicedConditionState.end()) {
242 // condition not sliced, but we haven't seen the matched start or stop yet. so
243 // return initial value.
244 conditionCache[mIndex] = mInitialValue;
245 } else {
246 // return the cached condition.
247 conditionCache[mIndex] =
248 itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
249 }
Yao Chend41c4222017-11-15 19:26:14 -0800250 }
Yangster7c334a12017-11-22 14:24:24 -0800251
Yao Chen967b2052017-11-07 16:36:43 -0800252 return;
Yao Chen729093d2017-10-16 10:33:26 -0700253 }
254
Yao Chen967b2052017-11-07 16:36:43 -0800255 // outputKey is the output key values. e.g, uid:1234
256 const HashableDimensionKey outputKey = getHashableKey(getDimensionKey(event, mOutputDimension));
257 handleConditionEvent(outputKey, matchedState == 1, conditionCache, conditionChangedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700258}
259
260void SimpleConditionTracker::isConditionMet(
261 const map<string, HashableDimensionKey>& conditionParameters,
Yangster7c334a12017-11-22 14:24:24 -0800262 const vector<sp<ConditionTracker>>& allConditions,
263 vector<ConditionState>& conditionCache) const {
Yao Chen729093d2017-10-16 10:33:26 -0700264 const auto pair = conditionParameters.find(mName);
Yao Chen967b2052017-11-07 16:36:43 -0800265 HashableDimensionKey key =
266 (pair == conditionParameters.end()) ? DEFAULT_DIMENSION_KEY : pair->second;
267
268 if (pair == conditionParameters.end() && mOutputDimension.size() > 0) {
269 ALOGE("Condition %s output has dimension, but it's not specified in the query!",
270 mName.c_str());
271 conditionCache[mIndex] = mInitialValue;
Yao Chen729093d2017-10-16 10:33:26 -0700272 return;
273 }
274
Yao Chen729093d2017-10-16 10:33:26 -0700275 VLOG("simpleCondition %s query key: %s", mName.c_str(), key.c_str());
276
Yao Chen967b2052017-11-07 16:36:43 -0800277 auto startedCountIt = mSlicedConditionState.find(key);
278 if (startedCountIt == mSlicedConditionState.end()) {
279 conditionCache[mIndex] = mInitialValue;
Yao Chen729093d2017-10-16 10:33:26 -0700280 } else {
Yao Chen967b2052017-11-07 16:36:43 -0800281 conditionCache[mIndex] =
282 startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
Yao Chen729093d2017-10-16 10:33:26 -0700283 }
284
285 VLOG("Condition %s return %d", mName.c_str(), conditionCache[mIndex]);
Yao Chencaf339d2017-10-06 16:01:10 -0700286}
287
288} // namespace statsd
289} // namespace os
290} // namespace android