blob: 18b93ee0fd47f0af87c78f759817e6368f6feaae [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"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022
Yao Chencaf339d2017-10-06 16:01:10 -070023#include <log/logprint.h>
24
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025namespace android {
26namespace os {
27namespace statsd {
28
Yao Chen729093d2017-10-16 10:33:26 -070029using std::map;
Yao Chencaf339d2017-10-06 16:01:10 -070030using std::string;
31using std::unique_ptr;
32using std::unordered_map;
33using std::vector;
34
Yao Chencaf339d2017-10-06 16:01:10 -070035SimpleConditionTracker::SimpleConditionTracker(
Yao Chenb3561512017-11-21 18:07:17 -080036 const ConfigKey& key, const string& name, const int index,
Stefan Lafon12d01fa2017-12-04 20:56:09 -080037 const SimplePredicate& simplePredicate,
Yao Chencaf339d2017-10-06 16:01:10 -070038 const unordered_map<string, int>& trackerNameIndexMap)
Yao Chenb3561512017-11-21 18:07:17 -080039 : ConditionTracker(name, index), mConfigKey(key) {
Yao Chencaf339d2017-10-06 16:01:10 -070040 VLOG("creating SimpleConditionTracker %s", mName.c_str());
Stefan Lafon12d01fa2017-12-04 20:56:09 -080041 mCountNesting = simplePredicate.count_nesting();
Yao Chencaf339d2017-10-06 16:01:10 -070042
Stefan Lafon12d01fa2017-12-04 20:56:09 -080043 if (simplePredicate.has_start()) {
44 auto pair = trackerNameIndexMap.find(simplePredicate.start());
Yao Chencaf339d2017-10-06 16:01:10 -070045 if (pair == trackerNameIndexMap.end()) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080046 ALOGW("Start matcher %s not found in the config", simplePredicate.start().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070047 return;
48 }
49 mStartLogMatcherIndex = pair->second;
50 mTrackerIndex.insert(mStartLogMatcherIndex);
51 } else {
52 mStartLogMatcherIndex = -1;
53 }
54
Stefan Lafon12d01fa2017-12-04 20:56:09 -080055 if (simplePredicate.has_stop()) {
56 auto pair = trackerNameIndexMap.find(simplePredicate.stop());
Yao Chencaf339d2017-10-06 16:01:10 -070057 if (pair == trackerNameIndexMap.end()) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080058 ALOGW("Stop matcher %s not found in the config", simplePredicate.stop().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070059 return;
60 }
61 mStopLogMatcherIndex = pair->second;
Yao Chen4b146852017-10-10 15:34:42 -070062 mTrackerIndex.insert(mStopLogMatcherIndex);
Yao Chencaf339d2017-10-06 16:01:10 -070063 } else {
64 mStopLogMatcherIndex = -1;
65 }
66
Stefan Lafon12d01fa2017-12-04 20:56:09 -080067 if (simplePredicate.has_stop_all()) {
68 auto pair = trackerNameIndexMap.find(simplePredicate.stop_all());
Yao Chencaf339d2017-10-06 16:01:10 -070069 if (pair == trackerNameIndexMap.end()) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080070 ALOGW("Stop all matcher %s not found in the config", simplePredicate.stop().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070071 return;
72 }
73 mStopAllLogMatcherIndex = pair->second;
74 mTrackerIndex.insert(mStopAllLogMatcherIndex);
75 } else {
76 mStopAllLogMatcherIndex = -1;
77 }
78
Stefan Lafon12d01fa2017-12-04 20:56:09 -080079 mOutputDimension.insert(mOutputDimension.begin(), simplePredicate.dimension().begin(),
80 simplePredicate.dimension().end());
Yao Chen5154a3792017-10-30 22:57:06 -070081
Yao Chen967b2052017-11-07 16:36:43 -080082 if (mOutputDimension.size() > 0) {
Yao Chen5154a3792017-10-30 22:57:06 -070083 mSliced = true;
84 }
85
Stefan Lafon12d01fa2017-12-04 20:56:09 -080086 if (simplePredicate.initial_value() == SimplePredicate_InitialValue_FALSE) {
Yao Chen967b2052017-11-07 16:36:43 -080087 mInitialValue = ConditionState::kFalse;
88 } else {
89 mInitialValue = ConditionState::kUnknown;
90 }
91
92 mNonSlicedConditionState = mInitialValue;
93
Yao Chencaf339d2017-10-06 16:01:10 -070094 mInitialized = true;
95}
96
97SimpleConditionTracker::~SimpleConditionTracker() {
98 VLOG("~SimpleConditionTracker()");
99}
100
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800101bool SimpleConditionTracker::init(const vector<Predicate>& allConditionConfig,
Yao Chencaf339d2017-10-06 16:01:10 -0700102 const vector<sp<ConditionTracker>>& allConditionTrackers,
103 const unordered_map<string, int>& conditionNameIndexMap,
104 vector<bool>& stack) {
105 // SimpleConditionTracker does not have dependency on other conditions, thus we just return
106 // if the initialization was successful.
107 return mInitialized;
108}
109
Yao Chen967b2052017-11-07 16:36:43 -0800110void print(map<HashableDimensionKey, int>& conditions, const string& name) {
Yao Chen729093d2017-10-16 10:33:26 -0700111 VLOG("%s DUMP:", name.c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700112 for (const auto& pair : conditions) {
Yao Chen967b2052017-11-07 16:36:43 -0800113 VLOG("\t%s : %d", pair.first.c_str(), pair.second);
Yao Chen729093d2017-10-16 10:33:26 -0700114 }
115}
116
Yao Chen967b2052017-11-07 16:36:43 -0800117void SimpleConditionTracker::handleStopAll(std::vector<ConditionState>& conditionCache,
118 std::vector<bool>& conditionChangedCache) {
119 // Unless the default condition is false, and there was nothing started, otherwise we have
120 // triggered a condition change.
121 conditionChangedCache[mIndex] =
122 (mInitialValue == ConditionState::kFalse && mSlicedConditionState.empty()) ? false
123 : true;
124
125 // After StopAll, we know everything has stopped. From now on, default condition is false.
126 mInitialValue = ConditionState::kFalse;
127 mSlicedConditionState.clear();
128 conditionCache[mIndex] = ConditionState::kFalse;
129}
130
Yao Chenb3561512017-11-21 18:07:17 -0800131bool SimpleConditionTracker::hitGuardRail(const HashableDimensionKey& newKey) {
132 if (!mSliced || mSlicedConditionState.find(newKey) != mSlicedConditionState.end()) {
133 // if the condition is not sliced or the key is not new, we are good!
134 return false;
135 }
136 // 1. Report the tuple count if the tuple count > soft limit
137 if (mSlicedConditionState.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
138 size_t newTupleCount = mSlicedConditionState.size() + 1;
139 StatsdStats::getInstance().noteConditionDimensionSize(mConfigKey, mName, newTupleCount);
140 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
141 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800142 ALOGE("Predicate %s dropping data for dimension key %s", mName.c_str(), newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800143 return true;
144 }
145 }
146 return false;
147}
148
Yao Chen967b2052017-11-07 16:36:43 -0800149void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey,
150 bool matchStart,
151 std::vector<ConditionState>& conditionCache,
152 std::vector<bool>& conditionChangedCache) {
153 bool changed = false;
154 auto outputIt = mSlicedConditionState.find(outputKey);
155 ConditionState newCondition;
Yao Chenb3561512017-11-21 18:07:17 -0800156 if (hitGuardRail(outputKey)) {
157 conditionChangedCache[mIndex] = false;
158 // Tells the caller it's evaluated.
159 conditionCache[mIndex] = ConditionState::kUnknown;
160 return;
161 }
Yao Chen967b2052017-11-07 16:36:43 -0800162 if (outputIt == mSlicedConditionState.end()) {
163 // We get a new output key.
164 newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse;
165 if (matchStart && mInitialValue != ConditionState::kTrue) {
166 mSlicedConditionState[outputKey] = 1;
167 changed = true;
168 } else if (mInitialValue != ConditionState::kFalse) {
169 // it's a stop and we don't have history about it.
170 // If the default condition is not false, it means this stop is valuable to us.
171 mSlicedConditionState[outputKey] = 0;
172 changed = true;
173 }
174 } else {
175 // we have history about this output key.
176 auto& startedCount = outputIt->second;
177 // assign the old value first.
178 newCondition = startedCount > 0 ? ConditionState::kTrue : ConditionState::kFalse;
179 if (matchStart) {
180 if (startedCount == 0) {
181 // This condition for this output key will change from false -> true
182 changed = true;
183 }
184
185 // it's ok to do ++ here, even if we don't count nesting. The >1 counts will be treated
186 // as 1 if not counting nesting.
187 startedCount++;
188 newCondition = ConditionState::kTrue;
189 } else {
190 // This is a stop event.
191 if (startedCount > 0) {
192 if (mCountNesting) {
193 startedCount--;
194 if (startedCount == 0) {
195 newCondition = ConditionState::kFalse;
196 }
197 } else {
198 // not counting nesting, so ignore the number of starts, stop now.
199 startedCount = 0;
200 newCondition = ConditionState::kFalse;
201 }
202 // if everything has stopped for this output key, condition true -> false;
203 if (startedCount == 0) {
204 changed = true;
205 }
206 }
207
208 // if default condition is false, it means we don't need to keep the false values.
209 if (mInitialValue == ConditionState::kFalse && startedCount == 0) {
210 mSlicedConditionState.erase(outputIt);
211 VLOG("erase key %s", outputKey.c_str());
212 }
213 }
214 }
215
216 // dump all dimensions for debugging
217 if (DEBUG) {
218 print(mSlicedConditionState, mName);
219 }
220
221 conditionChangedCache[mIndex] = changed;
222 conditionCache[mIndex] = newCondition;
223
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800224 VLOG("SimplePredicate %s nonSlicedChange? %d", mName.c_str(),
Yao Chen967b2052017-11-07 16:36:43 -0800225 conditionChangedCache[mIndex] == true);
226}
227
228void SimpleConditionTracker::evaluateCondition(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -0700229 const vector<MatchingState>& eventMatcherValues,
230 const vector<sp<ConditionTracker>>& mAllConditions,
231 vector<ConditionState>& conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800232 vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700233 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
234 // it has been evaluated.
Yao Chend41c4222017-11-15 19:26:14 -0800235 VLOG("Yes, already evaluated, %s %d", mName.c_str(), conditionCache[mIndex]);
Yao Chen967b2052017-11-07 16:36:43 -0800236 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700237 }
238
David Chenc18abed2017-11-22 16:47:59 -0800239 if (mStopAllLogMatcherIndex >= 0 && mStopAllLogMatcherIndex < int(eventMatcherValues.size()) &&
Yao Chen967b2052017-11-07 16:36:43 -0800240 eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
241 handleStopAll(conditionCache, conditionChangedCache);
242 return;
243 }
Yao Chen729093d2017-10-16 10:33:26 -0700244
Yao Chen967b2052017-11-07 16:36:43 -0800245 int matchedState = -1;
Yao Chencaf339d2017-10-06 16:01:10 -0700246 // Note: The order to evaluate the following start, stop, stop_all matters.
247 // The priority of overwrite is stop_all > stop > start.
248 if (mStartLogMatcherIndex >= 0 &&
249 eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen967b2052017-11-07 16:36:43 -0800250 matchedState = 1;
Yao Chencaf339d2017-10-06 16:01:10 -0700251 }
252
253 if (mStopLogMatcherIndex >= 0 &&
254 eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen967b2052017-11-07 16:36:43 -0800255 matchedState = 0;
Yao Chencaf339d2017-10-06 16:01:10 -0700256 }
257
Yao Chen967b2052017-11-07 16:36:43 -0800258 if (matchedState < 0) {
Yao Chend41c4222017-11-15 19:26:14 -0800259 // The event doesn't match this condition. So we just report existing condition values.
Yao Chen967b2052017-11-07 16:36:43 -0800260 conditionChangedCache[mIndex] = false;
Yao Chend41c4222017-11-15 19:26:14 -0800261 if (mSliced) {
262 // if the condition result is sliced. metrics won't directly get value from the
263 // cache, so just set any value other than kNotEvaluated.
264 conditionCache[mIndex] = ConditionState::kUnknown;
Yao Chend41c4222017-11-15 19:26:14 -0800265 } else {
Yangster7c334a12017-11-22 14:24:24 -0800266 const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY);
267 if (itr == mSlicedConditionState.end()) {
268 // condition not sliced, but we haven't seen the matched start or stop yet. so
269 // return initial value.
270 conditionCache[mIndex] = mInitialValue;
271 } else {
272 // return the cached condition.
273 conditionCache[mIndex] =
274 itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
275 }
Yao Chend41c4222017-11-15 19:26:14 -0800276 }
Yangster7c334a12017-11-22 14:24:24 -0800277
Yao Chen967b2052017-11-07 16:36:43 -0800278 return;
Yao Chen729093d2017-10-16 10:33:26 -0700279 }
280
Yao Chen967b2052017-11-07 16:36:43 -0800281 // outputKey is the output key values. e.g, uid:1234
282 const HashableDimensionKey outputKey = getHashableKey(getDimensionKey(event, mOutputDimension));
283 handleConditionEvent(outputKey, matchedState == 1, conditionCache, conditionChangedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700284}
285
286void SimpleConditionTracker::isConditionMet(
287 const map<string, HashableDimensionKey>& conditionParameters,
Yangster7c334a12017-11-22 14:24:24 -0800288 const vector<sp<ConditionTracker>>& allConditions,
289 vector<ConditionState>& conditionCache) const {
Yao Chen729093d2017-10-16 10:33:26 -0700290 const auto pair = conditionParameters.find(mName);
Yao Chen967b2052017-11-07 16:36:43 -0800291 HashableDimensionKey key =
292 (pair == conditionParameters.end()) ? DEFAULT_DIMENSION_KEY : pair->second;
293
294 if (pair == conditionParameters.end() && mOutputDimension.size() > 0) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800295 ALOGE("Predicate %s output has dimension, but it's not specified in the query!",
Yao Chen967b2052017-11-07 16:36:43 -0800296 mName.c_str());
297 conditionCache[mIndex] = mInitialValue;
Yao Chen729093d2017-10-16 10:33:26 -0700298 return;
299 }
300
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800301 VLOG("simplePredicate %s query key: %s", mName.c_str(), key.c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700302
Yao Chen967b2052017-11-07 16:36:43 -0800303 auto startedCountIt = mSlicedConditionState.find(key);
304 if (startedCountIt == mSlicedConditionState.end()) {
305 conditionCache[mIndex] = mInitialValue;
Yao Chen729093d2017-10-16 10:33:26 -0700306 } else {
Yao Chen967b2052017-11-07 16:36:43 -0800307 conditionCache[mIndex] =
308 startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
Yao Chen729093d2017-10-16 10:33:26 -0700309 }
310
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800311 VLOG("Predicate %s return %d", mName.c_str(), conditionCache[mIndex]);
Yao Chencaf339d2017-10-06 16:01:10 -0700312}
313
314} // namespace statsd
315} // namespace os
316} // namespace android