Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 19 | |
| 20 | #include "SimpleConditionTracker.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 27 | using std::unordered_map; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 28 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 29 | SimpleConditionTracker::SimpleConditionTracker( |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 30 | const ConfigKey& key, const int64_t& id, const int index, |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 31 | const SimplePredicate& simplePredicate, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 32 | const unordered_map<int64_t, int>& trackerNameIndexMap) |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 33 | : ConditionTracker(id, index), mConfigKey(key), mContainANYPositionInInternalDimensions(false) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 34 | VLOG("creating SimpleConditionTracker %lld", (long long)mConditionId); |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 35 | mCountNesting = simplePredicate.count_nesting(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 36 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 37 | if (simplePredicate.has_start()) { |
| 38 | auto pair = trackerNameIndexMap.find(simplePredicate.start()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 39 | if (pair == trackerNameIndexMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 40 | ALOGW("Start matcher %lld not found in the config", (long long)simplePredicate.start()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | mStartLogMatcherIndex = pair->second; |
| 44 | mTrackerIndex.insert(mStartLogMatcherIndex); |
| 45 | } else { |
| 46 | mStartLogMatcherIndex = -1; |
| 47 | } |
| 48 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 49 | if (simplePredicate.has_stop()) { |
| 50 | auto pair = trackerNameIndexMap.find(simplePredicate.stop()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 51 | if (pair == trackerNameIndexMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 52 | ALOGW("Stop matcher %lld not found in the config", (long long)simplePredicate.stop()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | mStopLogMatcherIndex = pair->second; |
Yao Chen | 4b14685 | 2017-10-10 15:34:42 -0700 | [diff] [blame] | 56 | mTrackerIndex.insert(mStopLogMatcherIndex); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 57 | } else { |
| 58 | mStopLogMatcherIndex = -1; |
| 59 | } |
| 60 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 61 | if (simplePredicate.has_stop_all()) { |
| 62 | auto pair = trackerNameIndexMap.find(simplePredicate.stop_all()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 63 | if (pair == trackerNameIndexMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 64 | ALOGW("Stop all matcher %lld found in the config", (long long)simplePredicate.stop_all()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | mStopAllLogMatcherIndex = pair->second; |
| 68 | mTrackerIndex.insert(mStopAllLogMatcherIndex); |
| 69 | } else { |
| 70 | mStopAllLogMatcherIndex = -1; |
| 71 | } |
| 72 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 73 | if (simplePredicate.has_dimensions()) { |
| 74 | translateFieldMatcher(simplePredicate.dimensions(), &mOutputDimensions); |
| 75 | if (mOutputDimensions.size() > 0) { |
| 76 | mSliced = true; |
| 77 | mDimensionTag = mOutputDimensions[0].mMatcher.getTag(); |
| 78 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 79 | mContainANYPositionInInternalDimensions = HasPositionANY(simplePredicate.dimensions()); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 82 | if (simplePredicate.initial_value() == SimplePredicate_InitialValue_FALSE) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 83 | mInitialValue = ConditionState::kFalse; |
| 84 | } else { |
| 85 | mInitialValue = ConditionState::kUnknown; |
| 86 | } |
| 87 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 88 | mInitialized = true; |
| 89 | } |
| 90 | |
| 91 | SimpleConditionTracker::~SimpleConditionTracker() { |
| 92 | VLOG("~SimpleConditionTracker()"); |
| 93 | } |
| 94 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 95 | bool SimpleConditionTracker::init(const vector<Predicate>& allConditionConfig, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 96 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 97 | const unordered_map<int64_t, int>& conditionIdIndexMap, |
tsaichristine | 6e2e92d | 2020-05-18 14:39:45 -0700 | [diff] [blame] | 98 | vector<bool>& stack, |
| 99 | vector<ConditionState>& initialConditionCache) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 100 | // SimpleConditionTracker does not have dependency on other conditions, thus we just return |
| 101 | // if the initialization was successful. |
tsaichristine | 6e2e92d | 2020-05-18 14:39:45 -0700 | [diff] [blame] | 102 | initialConditionCache[mIndex] = mInitialValue; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 103 | return mInitialized; |
| 104 | } |
| 105 | |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 106 | void SimpleConditionTracker::dumpState() { |
| 107 | VLOG("%lld DUMP:", (long long)mConditionId); |
| 108 | for (const auto& pair : mSlicedConditionState) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 109 | VLOG("\t%s : %d", pair.first.toString().c_str(), pair.second); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 110 | } |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 111 | |
| 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 Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 122 | void 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 Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 130 | for (const auto& cond : mSlicedConditionState) { |
| 131 | if (cond.second > 0) { |
| 132 | mLastChangedToFalseDimensions.insert(cond.first); |
| 133 | } |
| 134 | } |
| 135 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 136 | // 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 Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 142 | bool 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-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 150 | StatsdStats::getInstance().noteConditionDimensionSize(mConfigKey, mConditionId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 151 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 152 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 153 | ALOGE("Predicate %lld dropping data for dimension key %s", |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 154 | (long long)mConditionId, newKey.toString().c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 161 | void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 162 | bool matchStart, ConditionState* conditionCache, |
| 163 | bool* conditionChangedCache) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 164 | bool changed = false; |
| 165 | auto outputIt = mSlicedConditionState.find(outputKey); |
| 166 | ConditionState newCondition; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 167 | if (hitGuardRail(outputKey)) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 168 | (*conditionChangedCache) = false; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 169 | // Tells the caller it's evaluated. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 170 | (*conditionCache) = ConditionState::kUnknown; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 173 | if (outputIt == mSlicedConditionState.end()) { |
| 174 | // We get a new output key. |
| 175 | newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse; |
| 176 | if (matchStart && mInitialValue != ConditionState::kTrue) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 177 | mSlicedConditionState[outputKey] = 1; |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 178 | changed = true; |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 179 | mLastChangedToTrueDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 180 | } 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. |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 183 | mSlicedConditionState[outputKey] = 0; |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 184 | mLastChangedToFalseDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 185 | 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 Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 194 | mLastChangedToTrueDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 195 | // 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 Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 218 | mLastChangedToFalseDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 219 | 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); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 226 | VLOG("erase key %s", outputKey.toString().c_str()); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // dump all dimensions for debugging |
| 232 | if (DEBUG) { |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 233 | dumpState(); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 236 | (*conditionChangedCache) = changed; |
| 237 | (*conditionCache) = newCondition; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 238 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 239 | VLOG("SimplePredicate %lld nonSlicedChange? %d", (long long)mConditionId, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 240 | conditionChangedCache[mIndex] == true); |
| 241 | } |
| 242 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 243 | void 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 Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 249 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 250 | // it has been evaluated. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 251 | VLOG("Yes, already evaluated, %lld %d", |
| 252 | (long long)mConditionId, conditionCache[mIndex]); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 253 | return; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 254 | } |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 255 | mLastChangedToTrueDimensions.clear(); |
| 256 | mLastChangedToFalseDimensions.clear(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 257 | |
David Chen | c18abed | 2017-11-22 16:47:59 -0800 | [diff] [blame] | 258 | if (mStopAllLogMatcherIndex >= 0 && mStopAllLogMatcherIndex < int(eventMatcherValues.size()) && |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 259 | eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) { |
| 260 | handleStopAll(conditionCache, conditionChangedCache); |
| 261 | return; |
| 262 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 263 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 264 | int matchedState = -1; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 265 | // 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 Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 269 | matchedState = 1; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (mStopLogMatcherIndex >= 0 && |
| 273 | eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 274 | matchedState = 0; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 277 | if (matchedState < 0) { |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 278 | // The event doesn't match this condition. So we just report existing condition values. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 279 | conditionChangedCache[mIndex] = false; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 280 | if (mSliced) { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 281 | // if the condition result is sliced. The overall condition is true if any of the sliced |
| 282 | // condition is true |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 283 | conditionCache[mIndex] = mInitialValue; |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 284 | for (const auto& slicedCondition : mSlicedConditionState) { |
| 285 | if (slicedCondition.second > 0) { |
| 286 | conditionCache[mIndex] = ConditionState::kTrue; |
| 287 | break; |
| 288 | } |
| 289 | } |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 290 | } else { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 291 | 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 Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 301 | } |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 302 | return; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 305 | ConditionState overallState = mInitialValue; |
| 306 | bool overallChanged = false; |
| 307 | |
| 308 | if (mOutputDimensions.size() == 0) { |
| 309 | handleConditionEvent(DEFAULT_DIMENSION_KEY, matchedState == 1, &overallState, |
| 310 | &overallChanged); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 311 | } 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-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 322 | } else { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 323 | ALOGE("The condition tracker should not be sliced by ANY position matcher."); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 324 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 325 | conditionCache[mIndex] = overallState; |
| 326 | conditionChangedCache[mIndex] = overallChanged; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void SimpleConditionTracker::isConditionMet( |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 330 | const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 331 | const bool isPartialLink, |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 332 | vector<ConditionState>& conditionCache) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 333 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 334 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 335 | // it has been evaluated. |
| 336 | VLOG("Yes, already evaluated, %lld %d", |
| 337 | (long long)mConditionId, conditionCache[mIndex]); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 338 | return; |
| 339 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 340 | const auto pair = conditionParameters.find(mConditionId); |
| 341 | |
| 342 | if (pair == conditionParameters.end()) { |
| 343 | ConditionState conditionState = ConditionState::kNotEvaluated; |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 344 | 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-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | conditionCache[mIndex] = conditionState; |
| 354 | return; |
| 355 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 356 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 357 | ConditionState conditionState = ConditionState::kNotEvaluated; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 358 | 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; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | } else { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 371 | auto startedCountIt = mSlicedConditionState.find(key); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 372 | conditionState = conditionState | mInitialValue; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 373 | if (startedCountIt != mSlicedConditionState.end()) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 374 | ConditionState sliceState = |
| 375 | startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 376 | conditionState = conditionState | sliceState; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 377 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 378 | |
| 379 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 380 | conditionCache[mIndex] = conditionState; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 381 | VLOG("Predicate %lld return %d", (long long)mConditionId, conditionCache[mIndex]); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 384 | } // namespace statsd |
| 385 | } // namespace os |
| 386 | } // namespace android |