blob: efb4d4989425e779644d68589870ba3182cf0b7a [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023namespace android {
24namespace os {
25namespace statsd {
26
Yao Chencaf339d2017-10-06 16:01:10 -070027using std::unordered_map;
Yao Chencaf339d2017-10-06 16:01:10 -070028
Yao Chencaf339d2017-10-06 16:01:10 -070029SimpleConditionTracker::SimpleConditionTracker(
Yangster-mac94e197c2018-01-02 16:03:03 -080030 const ConfigKey& key, const int64_t& id, const int index,
Stefan Lafon12d01fa2017-12-04 20:56:09 -080031 const SimplePredicate& simplePredicate,
Yangster-mac94e197c2018-01-02 16:03:03 -080032 const unordered_map<int64_t, int>& trackerNameIndexMap)
Yangster13fb7e42018-03-07 17:30:49 -080033 : ConditionTracker(id, index), mConfigKey(key), mContainANYPositionInInternalDimensions(false) {
Yangster-mac94e197c2018-01-02 16:03:03 -080034 VLOG("creating SimpleConditionTracker %lld", (long long)mConditionId);
Stefan Lafon12d01fa2017-12-04 20:56:09 -080035 mCountNesting = simplePredicate.count_nesting();
Yao Chencaf339d2017-10-06 16:01:10 -070036
Stefan Lafon12d01fa2017-12-04 20:56:09 -080037 if (simplePredicate.has_start()) {
38 auto pair = trackerNameIndexMap.find(simplePredicate.start());
Yao Chencaf339d2017-10-06 16:01:10 -070039 if (pair == trackerNameIndexMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -080040 ALOGW("Start matcher %lld not found in the config", (long long)simplePredicate.start());
Yao Chencaf339d2017-10-06 16:01:10 -070041 return;
42 }
43 mStartLogMatcherIndex = pair->second;
44 mTrackerIndex.insert(mStartLogMatcherIndex);
45 } else {
46 mStartLogMatcherIndex = -1;
47 }
48
Stefan Lafon12d01fa2017-12-04 20:56:09 -080049 if (simplePredicate.has_stop()) {
50 auto pair = trackerNameIndexMap.find(simplePredicate.stop());
Yao Chencaf339d2017-10-06 16:01:10 -070051 if (pair == trackerNameIndexMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -080052 ALOGW("Stop matcher %lld not found in the config", (long long)simplePredicate.stop());
Yao Chencaf339d2017-10-06 16:01:10 -070053 return;
54 }
55 mStopLogMatcherIndex = pair->second;
Yao Chen4b146852017-10-10 15:34:42 -070056 mTrackerIndex.insert(mStopLogMatcherIndex);
Yao Chencaf339d2017-10-06 16:01:10 -070057 } else {
58 mStopLogMatcherIndex = -1;
59 }
60
Stefan Lafon12d01fa2017-12-04 20:56:09 -080061 if (simplePredicate.has_stop_all()) {
62 auto pair = trackerNameIndexMap.find(simplePredicate.stop_all());
Yao Chencaf339d2017-10-06 16:01:10 -070063 if (pair == trackerNameIndexMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -080064 ALOGW("Stop all matcher %lld found in the config", (long long)simplePredicate.stop_all());
Yao Chencaf339d2017-10-06 16:01:10 -070065 return;
66 }
67 mStopAllLogMatcherIndex = pair->second;
68 mTrackerIndex.insert(mStopAllLogMatcherIndex);
69 } else {
70 mStopAllLogMatcherIndex = -1;
71 }
72
Yao Chen8a8d16c2018-02-08 14:50:40 -080073 if (simplePredicate.has_dimensions()) {
74 translateFieldMatcher(simplePredicate.dimensions(), &mOutputDimensions);
75 if (mOutputDimensions.size() > 0) {
76 mSliced = true;
77 mDimensionTag = mOutputDimensions[0].mMatcher.getTag();
78 }
Yangster13fb7e42018-03-07 17:30:49 -080079 mContainANYPositionInInternalDimensions = HasPositionANY(simplePredicate.dimensions());
Yao Chen5154a3792017-10-30 22:57:06 -070080 }
81
Stefan Lafon12d01fa2017-12-04 20:56:09 -080082 if (simplePredicate.initial_value() == SimplePredicate_InitialValue_FALSE) {
Yao Chen967b2052017-11-07 16:36:43 -080083 mInitialValue = ConditionState::kFalse;
84 } else {
85 mInitialValue = ConditionState::kUnknown;
86 }
87
Yao Chencaf339d2017-10-06 16:01:10 -070088 mInitialized = true;
89}
90
91SimpleConditionTracker::~SimpleConditionTracker() {
92 VLOG("~SimpleConditionTracker()");
93}
94
Stefan Lafon12d01fa2017-12-04 20:56:09 -080095bool SimpleConditionTracker::init(const vector<Predicate>& allConditionConfig,
Yao Chencaf339d2017-10-06 16:01:10 -070096 const vector<sp<ConditionTracker>>& allConditionTrackers,
Yangster-mac94e197c2018-01-02 16:03:03 -080097 const unordered_map<int64_t, int>& conditionIdIndexMap,
tsaichristine6e2e92d2020-05-18 14:39:45 -070098 vector<bool>& stack,
99 vector<ConditionState>& initialConditionCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700100 // SimpleConditionTracker does not have dependency on other conditions, thus we just return
101 // if the initialization was successful.
tsaichristine6e2e92d2020-05-18 14:39:45 -0700102 initialConditionCache[mIndex] = mInitialValue;
Yao Chencaf339d2017-10-06 16:01:10 -0700103 return mInitialized;
104}
105
Yao Chen580ea3212018-02-26 14:21:54 -0800106void SimpleConditionTracker::dumpState() {
107 VLOG("%lld DUMP:", (long long)mConditionId);
108 for (const auto& pair : mSlicedConditionState) {
Yangster13fb7e42018-03-07 17:30:49 -0800109 VLOG("\t%s : %d", pair.first.toString().c_str(), pair.second);
Yao Chen729093d2017-10-16 10:33:26 -0700110 }
Yao Chen580ea3212018-02-26 14:21:54 -0800111
112 VLOG("Changed to true keys: \n");
113 for (const auto& key : mLastChangedToTrueDimensions) {
114 VLOG("%s", key.toString().c_str());
115 }
116 VLOG("Changed to false keys: \n");
117 for (const auto& key : mLastChangedToFalseDimensions) {
118 VLOG("%s", key.toString().c_str());
119 }
Yao Chen729093d2017-10-16 10:33:26 -0700120}
121
Yao Chen967b2052017-11-07 16:36:43 -0800122void SimpleConditionTracker::handleStopAll(std::vector<ConditionState>& conditionCache,
123 std::vector<bool>& conditionChangedCache) {
124 // Unless the default condition is false, and there was nothing started, otherwise we have
125 // triggered a condition change.
126 conditionChangedCache[mIndex] =
127 (mInitialValue == ConditionState::kFalse && mSlicedConditionState.empty()) ? false
128 : true;
129
Yao Chen580ea3212018-02-26 14:21:54 -0800130 for (const auto& cond : mSlicedConditionState) {
131 if (cond.second > 0) {
132 mLastChangedToFalseDimensions.insert(cond.first);
133 }
134 }
135
Yao Chen967b2052017-11-07 16:36:43 -0800136 // After StopAll, we know everything has stopped. From now on, default condition is false.
137 mInitialValue = ConditionState::kFalse;
138 mSlicedConditionState.clear();
139 conditionCache[mIndex] = ConditionState::kFalse;
140}
141
Yao Chenb3561512017-11-21 18:07:17 -0800142bool SimpleConditionTracker::hitGuardRail(const HashableDimensionKey& newKey) {
143 if (!mSliced || mSlicedConditionState.find(newKey) != mSlicedConditionState.end()) {
144 // if the condition is not sliced or the key is not new, we are good!
145 return false;
146 }
147 // 1. Report the tuple count if the tuple count > soft limit
148 if (mSlicedConditionState.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
149 size_t newTupleCount = mSlicedConditionState.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800150 StatsdStats::getInstance().noteConditionDimensionSize(mConfigKey, mConditionId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800151 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
152 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800153 ALOGE("Predicate %lld dropping data for dimension key %s",
Yangster13fb7e42018-03-07 17:30:49 -0800154 (long long)mConditionId, newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800155 return true;
156 }
157 }
158 return false;
159}
160
Yao Chen967b2052017-11-07 16:36:43 -0800161void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800162 bool matchStart, ConditionState* conditionCache,
163 bool* conditionChangedCache) {
Yao Chen967b2052017-11-07 16:36:43 -0800164 bool changed = false;
165 auto outputIt = mSlicedConditionState.find(outputKey);
166 ConditionState newCondition;
Yao Chenb3561512017-11-21 18:07:17 -0800167 if (hitGuardRail(outputKey)) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800168 (*conditionChangedCache) = false;
Yao Chenb3561512017-11-21 18:07:17 -0800169 // Tells the caller it's evaluated.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800170 (*conditionCache) = ConditionState::kUnknown;
Yao Chenb3561512017-11-21 18:07:17 -0800171 return;
172 }
Yao Chen967b2052017-11-07 16:36:43 -0800173 if (outputIt == mSlicedConditionState.end()) {
174 // We get a new output key.
175 newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse;
176 if (matchStart && mInitialValue != ConditionState::kTrue) {
Yangster13fb7e42018-03-07 17:30:49 -0800177 mSlicedConditionState[outputKey] = 1;
Yao Chen967b2052017-11-07 16:36:43 -0800178 changed = true;
Yao Chen580ea3212018-02-26 14:21:54 -0800179 mLastChangedToTrueDimensions.insert(outputKey);
Yao Chen967b2052017-11-07 16:36:43 -0800180 } else if (mInitialValue != ConditionState::kFalse) {
181 // it's a stop and we don't have history about it.
182 // If the default condition is not false, it means this stop is valuable to us.
Yangster13fb7e42018-03-07 17:30:49 -0800183 mSlicedConditionState[outputKey] = 0;
Yao Chen580ea3212018-02-26 14:21:54 -0800184 mLastChangedToFalseDimensions.insert(outputKey);
Yao Chen967b2052017-11-07 16:36:43 -0800185 changed = true;
186 }
187 } else {
188 // we have history about this output key.
189 auto& startedCount = outputIt->second;
190 // assign the old value first.
191 newCondition = startedCount > 0 ? ConditionState::kTrue : ConditionState::kFalse;
192 if (matchStart) {
193 if (startedCount == 0) {
Yao Chen580ea3212018-02-26 14:21:54 -0800194 mLastChangedToTrueDimensions.insert(outputKey);
Yao Chen967b2052017-11-07 16:36:43 -0800195 // This condition for this output key will change from false -> true
196 changed = true;
197 }
198
199 // it's ok to do ++ here, even if we don't count nesting. The >1 counts will be treated
200 // as 1 if not counting nesting.
201 startedCount++;
202 newCondition = ConditionState::kTrue;
203 } else {
204 // This is a stop event.
205 if (startedCount > 0) {
206 if (mCountNesting) {
207 startedCount--;
208 if (startedCount == 0) {
209 newCondition = ConditionState::kFalse;
210 }
211 } else {
212 // not counting nesting, so ignore the number of starts, stop now.
213 startedCount = 0;
214 newCondition = ConditionState::kFalse;
215 }
216 // if everything has stopped for this output key, condition true -> false;
217 if (startedCount == 0) {
Yao Chen580ea3212018-02-26 14:21:54 -0800218 mLastChangedToFalseDimensions.insert(outputKey);
Yao Chen967b2052017-11-07 16:36:43 -0800219 changed = true;
220 }
221 }
222
223 // if default condition is false, it means we don't need to keep the false values.
224 if (mInitialValue == ConditionState::kFalse && startedCount == 0) {
225 mSlicedConditionState.erase(outputIt);
Yangster13fb7e42018-03-07 17:30:49 -0800226 VLOG("erase key %s", outputKey.toString().c_str());
Yao Chen967b2052017-11-07 16:36:43 -0800227 }
228 }
229 }
230
231 // dump all dimensions for debugging
232 if (DEBUG) {
Yao Chen580ea3212018-02-26 14:21:54 -0800233 dumpState();
Yao Chen967b2052017-11-07 16:36:43 -0800234 }
235
Yao Chen8a8d16c2018-02-08 14:50:40 -0800236 (*conditionChangedCache) = changed;
237 (*conditionCache) = newCondition;
Yangster13fb7e42018-03-07 17:30:49 -0800238
Yangster-mac94e197c2018-01-02 16:03:03 -0800239 VLOG("SimplePredicate %lld nonSlicedChange? %d", (long long)mConditionId,
Yao Chen967b2052017-11-07 16:36:43 -0800240 conditionChangedCache[mIndex] == true);
241}
242
Yangster-mac93694462018-01-22 20:49:31 -0800243void SimpleConditionTracker::evaluateCondition(
244 const LogEvent& event,
245 const vector<MatchingState>& eventMatcherValues,
246 const vector<sp<ConditionTracker>>& mAllConditions,
247 vector<ConditionState>& conditionCache,
248 vector<bool>& conditionChangedCache) {
Yao Chencaf339d2017-10-06 16:01:10 -0700249 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
250 // it has been evaluated.
Yangster-mac94e197c2018-01-02 16:03:03 -0800251 VLOG("Yes, already evaluated, %lld %d",
252 (long long)mConditionId, conditionCache[mIndex]);
Yao Chen967b2052017-11-07 16:36:43 -0800253 return;
Yao Chencaf339d2017-10-06 16:01:10 -0700254 }
Yao Chen580ea3212018-02-26 14:21:54 -0800255 mLastChangedToTrueDimensions.clear();
256 mLastChangedToFalseDimensions.clear();
Yao Chencaf339d2017-10-06 16:01:10 -0700257
David Chenc18abed2017-11-22 16:47:59 -0800258 if (mStopAllLogMatcherIndex >= 0 && mStopAllLogMatcherIndex < int(eventMatcherValues.size()) &&
Yao Chen967b2052017-11-07 16:36:43 -0800259 eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
260 handleStopAll(conditionCache, conditionChangedCache);
261 return;
262 }
Yao Chen729093d2017-10-16 10:33:26 -0700263
Yao Chen967b2052017-11-07 16:36:43 -0800264 int matchedState = -1;
Yao Chencaf339d2017-10-06 16:01:10 -0700265 // Note: The order to evaluate the following start, stop, stop_all matters.
266 // The priority of overwrite is stop_all > stop > start.
267 if (mStartLogMatcherIndex >= 0 &&
268 eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen967b2052017-11-07 16:36:43 -0800269 matchedState = 1;
Yao Chencaf339d2017-10-06 16:01:10 -0700270 }
271
272 if (mStopLogMatcherIndex >= 0 &&
273 eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
Yao Chen967b2052017-11-07 16:36:43 -0800274 matchedState = 0;
Yao Chencaf339d2017-10-06 16:01:10 -0700275 }
276
Yao Chen967b2052017-11-07 16:36:43 -0800277 if (matchedState < 0) {
Yao Chend41c4222017-11-15 19:26:14 -0800278 // The event doesn't match this condition. So we just report existing condition values.
Yao Chen967b2052017-11-07 16:36:43 -0800279 conditionChangedCache[mIndex] = false;
Yao Chend41c4222017-11-15 19:26:14 -0800280 if (mSliced) {
Yao Chen427d3722018-03-22 15:21:52 -0700281 // if the condition result is sliced. The overall condition is true if any of the sliced
282 // condition is true
Yangster-mac93694462018-01-22 20:49:31 -0800283 conditionCache[mIndex] = mInitialValue;
Yao Chen427d3722018-03-22 15:21:52 -0700284 for (const auto& slicedCondition : mSlicedConditionState) {
285 if (slicedCondition.second > 0) {
286 conditionCache[mIndex] = ConditionState::kTrue;
287 break;
288 }
289 }
Yao Chend41c4222017-11-15 19:26:14 -0800290 } else {
Yangster7c334a12017-11-22 14:24:24 -0800291 const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY);
292 if (itr == mSlicedConditionState.end()) {
293 // condition not sliced, but we haven't seen the matched start or stop yet. so
294 // return initial value.
295 conditionCache[mIndex] = mInitialValue;
296 } else {
297 // return the cached condition.
298 conditionCache[mIndex] =
299 itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
300 }
Yao Chend41c4222017-11-15 19:26:14 -0800301 }
Yao Chen967b2052017-11-07 16:36:43 -0800302 return;
Yao Chen729093d2017-10-16 10:33:26 -0700303 }
304
Yao Chen8a8d16c2018-02-08 14:50:40 -0800305 ConditionState overallState = mInitialValue;
306 bool overallChanged = false;
307
308 if (mOutputDimensions.size() == 0) {
309 handleConditionEvent(DEFAULT_DIMENSION_KEY, matchedState == 1, &overallState,
310 &overallChanged);
Yangster13fb7e42018-03-07 17:30:49 -0800311 } else if (!mContainANYPositionInInternalDimensions) {
312 HashableDimensionKey outputValue;
313 filterValues(mOutputDimensions, event.getValues(), &outputValue);
314
315 // If this event has multiple nodes in the attribution chain, this log event probably will
316 // generate multiple dimensions. If so, we will find if the condition changes for any
317 // dimension and ask the corresponding metric producer to verify whether the actual sliced
318 // condition has changed or not.
319 // A high level assumption is that a predicate is either sliced or unsliced. We will never
320 // have both sliced and unsliced version of a predicate.
321 handleConditionEvent(outputValue, matchedState == 1, &overallState, &overallChanged);
Yangster-mac20877162017-12-22 17:19:39 -0800322 } else {
Yangster-mace06cfd72018-03-10 23:22:59 -0800323 ALOGE("The condition tracker should not be sliced by ANY position matcher.");
Yangster-mac20877162017-12-22 17:19:39 -0800324 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800325 conditionCache[mIndex] = overallState;
326 conditionChangedCache[mIndex] = overallChanged;
Yao Chen729093d2017-10-16 10:33:26 -0700327}
328
329void SimpleConditionTracker::isConditionMet(
Yao Chen8a8d16c2018-02-08 14:50:40 -0800330 const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions,
Yangster13fb7e42018-03-07 17:30:49 -0800331 const bool isPartialLink,
tsaichristine76853372019-08-06 17:17:03 -0700332 vector<ConditionState>& conditionCache) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800333
Yangster-mac93694462018-01-22 20:49:31 -0800334 if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
335 // it has been evaluated.
336 VLOG("Yes, already evaluated, %lld %d",
337 (long long)mConditionId, conditionCache[mIndex]);
Yao Chen729093d2017-10-16 10:33:26 -0700338 return;
339 }
Yangster-mac93694462018-01-22 20:49:31 -0800340 const auto pair = conditionParameters.find(mConditionId);
341
342 if (pair == conditionParameters.end()) {
343 ConditionState conditionState = ConditionState::kNotEvaluated;
tsaichristine76853372019-08-06 17:17:03 -0700344 conditionState = conditionState | mInitialValue;
345 if (!mSliced) {
346 const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY);
347 if (itr != mSlicedConditionState.end()) {
348 ConditionState sliceState =
349 itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
350 conditionState = conditionState | sliceState;
Yangster-mac93694462018-01-22 20:49:31 -0800351 }
352 }
353 conditionCache[mIndex] = conditionState;
354 return;
355 }
Yao Chen729093d2017-10-16 10:33:26 -0700356
Yangster-mac20877162017-12-22 17:19:39 -0800357 ConditionState conditionState = ConditionState::kNotEvaluated;
Yangster13fb7e42018-03-07 17:30:49 -0800358 const HashableDimensionKey& key = pair->second;
359 if (isPartialLink) {
360 // For unseen key, check whether the require dimensions are subset of sliced condition
361 // output.
362 conditionState = conditionState | mInitialValue;
363 for (const auto& slice : mSlicedConditionState) {
364 ConditionState sliceState =
365 slice.second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
366 if (slice.first.contains(key)) {
367 conditionState = conditionState | sliceState;
Yangster13fb7e42018-03-07 17:30:49 -0800368 }
369 }
370 } else {
Yangster-mac20877162017-12-22 17:19:39 -0800371 auto startedCountIt = mSlicedConditionState.find(key);
Yangster13fb7e42018-03-07 17:30:49 -0800372 conditionState = conditionState | mInitialValue;
Yangster-mac20877162017-12-22 17:19:39 -0800373 if (startedCountIt != mSlicedConditionState.end()) {
Yangster-mac93694462018-01-22 20:49:31 -0800374 ConditionState sliceState =
375 startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
376 conditionState = conditionState | sliceState;
Yangster-mac20877162017-12-22 17:19:39 -0800377 }
Yangster13fb7e42018-03-07 17:30:49 -0800378
379 }
Yangster-mac20877162017-12-22 17:19:39 -0800380 conditionCache[mIndex] = conditionState;
Yangster-mac94e197c2018-01-02 16:03:03 -0800381 VLOG("Predicate %lld return %d", (long long)mConditionId, conditionCache[mIndex]);
Yao Chencaf339d2017-10-06 16:01:10 -0700382}
383
Yao Chencaf339d2017-10-06 16:01:10 -0700384} // namespace statsd
385} // namespace os
386} // namespace android