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 | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 27 | using std::map; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::unique_ptr; |
| 30 | using std::unordered_map; |
| 31 | using std::vector; |
| 32 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 33 | SimpleConditionTracker::SimpleConditionTracker( |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 34 | const ConfigKey& key, const int64_t& id, const int index, |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 35 | const SimplePredicate& simplePredicate, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 36 | const unordered_map<int64_t, int>& trackerNameIndexMap) |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 37 | : ConditionTracker(id, index), mConfigKey(key), mContainANYPositionInInternalDimensions(false) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 38 | VLOG("creating SimpleConditionTracker %lld", (long long)mConditionId); |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 39 | mCountNesting = simplePredicate.count_nesting(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 40 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 41 | if (simplePredicate.has_start()) { |
| 42 | auto pair = trackerNameIndexMap.find(simplePredicate.start()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 43 | if (pair == trackerNameIndexMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 44 | 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] | 45 | return; |
| 46 | } |
| 47 | mStartLogMatcherIndex = pair->second; |
| 48 | mTrackerIndex.insert(mStartLogMatcherIndex); |
| 49 | } else { |
| 50 | mStartLogMatcherIndex = -1; |
| 51 | } |
| 52 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 53 | if (simplePredicate.has_stop()) { |
| 54 | auto pair = trackerNameIndexMap.find(simplePredicate.stop()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 55 | if (pair == trackerNameIndexMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 56 | 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] | 57 | return; |
| 58 | } |
| 59 | mStopLogMatcherIndex = pair->second; |
Yao Chen | 4b14685 | 2017-10-10 15:34:42 -0700 | [diff] [blame] | 60 | mTrackerIndex.insert(mStopLogMatcherIndex); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 61 | } else { |
| 62 | mStopLogMatcherIndex = -1; |
| 63 | } |
| 64 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 65 | if (simplePredicate.has_stop_all()) { |
| 66 | auto pair = trackerNameIndexMap.find(simplePredicate.stop_all()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 67 | if (pair == trackerNameIndexMap.end()) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 68 | 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] | 69 | return; |
| 70 | } |
| 71 | mStopAllLogMatcherIndex = pair->second; |
| 72 | mTrackerIndex.insert(mStopAllLogMatcherIndex); |
| 73 | } else { |
| 74 | mStopAllLogMatcherIndex = -1; |
| 75 | } |
| 76 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 77 | if (simplePredicate.has_dimensions()) { |
| 78 | translateFieldMatcher(simplePredicate.dimensions(), &mOutputDimensions); |
| 79 | if (mOutputDimensions.size() > 0) { |
| 80 | mSliced = true; |
| 81 | mDimensionTag = mOutputDimensions[0].mMatcher.getTag(); |
| 82 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 83 | mContainANYPositionInInternalDimensions = HasPositionANY(simplePredicate.dimensions()); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 86 | if (simplePredicate.initial_value() == SimplePredicate_InitialValue_FALSE) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 87 | mInitialValue = ConditionState::kFalse; |
| 88 | } else { |
| 89 | mInitialValue = ConditionState::kUnknown; |
| 90 | } |
| 91 | |
| 92 | mNonSlicedConditionState = mInitialValue; |
| 93 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 94 | if (!mSliced) { |
| 95 | mUnSlicedPart = mInitialValue; |
| 96 | } |
| 97 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 98 | mInitialized = true; |
| 99 | } |
| 100 | |
| 101 | SimpleConditionTracker::~SimpleConditionTracker() { |
| 102 | VLOG("~SimpleConditionTracker()"); |
| 103 | } |
| 104 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 105 | bool SimpleConditionTracker::init(const vector<Predicate>& allConditionConfig, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 106 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 107 | const unordered_map<int64_t, int>& conditionIdIndexMap, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 108 | vector<bool>& stack) { |
| 109 | // SimpleConditionTracker does not have dependency on other conditions, thus we just return |
| 110 | // if the initialization was successful. |
| 111 | return mInitialized; |
| 112 | } |
| 113 | |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 114 | void SimpleConditionTracker::dumpState() { |
| 115 | VLOG("%lld DUMP:", (long long)mConditionId); |
| 116 | for (const auto& pair : mSlicedConditionState) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 117 | VLOG("\t%s : %d", pair.first.toString().c_str(), pair.second); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 118 | } |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 119 | |
| 120 | VLOG("Changed to true keys: \n"); |
| 121 | for (const auto& key : mLastChangedToTrueDimensions) { |
| 122 | VLOG("%s", key.toString().c_str()); |
| 123 | } |
| 124 | VLOG("Changed to false keys: \n"); |
| 125 | for (const auto& key : mLastChangedToFalseDimensions) { |
| 126 | VLOG("%s", key.toString().c_str()); |
| 127 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 130 | void SimpleConditionTracker::handleStopAll(std::vector<ConditionState>& conditionCache, |
| 131 | std::vector<bool>& conditionChangedCache) { |
| 132 | // Unless the default condition is false, and there was nothing started, otherwise we have |
| 133 | // triggered a condition change. |
| 134 | conditionChangedCache[mIndex] = |
| 135 | (mInitialValue == ConditionState::kFalse && mSlicedConditionState.empty()) ? false |
| 136 | : true; |
| 137 | |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 138 | for (const auto& cond : mSlicedConditionState) { |
| 139 | if (cond.second > 0) { |
| 140 | mLastChangedToFalseDimensions.insert(cond.first); |
| 141 | } |
| 142 | } |
| 143 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 144 | // After StopAll, we know everything has stopped. From now on, default condition is false. |
| 145 | mInitialValue = ConditionState::kFalse; |
| 146 | mSlicedConditionState.clear(); |
| 147 | conditionCache[mIndex] = ConditionState::kFalse; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 148 | if (!mSliced) { |
| 149 | mUnSlicedPart = ConditionState::kFalse; |
| 150 | } |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 153 | bool SimpleConditionTracker::hitGuardRail(const HashableDimensionKey& newKey) { |
| 154 | if (!mSliced || mSlicedConditionState.find(newKey) != mSlicedConditionState.end()) { |
| 155 | // if the condition is not sliced or the key is not new, we are good! |
| 156 | return false; |
| 157 | } |
| 158 | // 1. Report the tuple count if the tuple count > soft limit |
| 159 | if (mSlicedConditionState.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 160 | size_t newTupleCount = mSlicedConditionState.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 161 | StatsdStats::getInstance().noteConditionDimensionSize(mConfigKey, mConditionId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 162 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 163 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 164 | ALOGE("Predicate %lld dropping data for dimension key %s", |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 165 | (long long)mConditionId, newKey.toString().c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 172 | void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 173 | bool matchStart, ConditionState* conditionCache, |
| 174 | bool* conditionChangedCache) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 175 | bool changed = false; |
| 176 | auto outputIt = mSlicedConditionState.find(outputKey); |
| 177 | ConditionState newCondition; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 178 | if (hitGuardRail(outputKey)) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 179 | (*conditionChangedCache) = false; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 180 | // Tells the caller it's evaluated. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 181 | (*conditionCache) = ConditionState::kUnknown; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 182 | return; |
| 183 | } |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 184 | if (outputIt == mSlicedConditionState.end()) { |
| 185 | // We get a new output key. |
| 186 | newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse; |
| 187 | if (matchStart && mInitialValue != ConditionState::kTrue) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 188 | mSlicedConditionState[outputKey] = 1; |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 189 | changed = true; |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 190 | mLastChangedToTrueDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 191 | } else if (mInitialValue != ConditionState::kFalse) { |
| 192 | // it's a stop and we don't have history about it. |
| 193 | // 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] | 194 | mSlicedConditionState[outputKey] = 0; |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 195 | mLastChangedToFalseDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 196 | changed = true; |
| 197 | } |
| 198 | } else { |
| 199 | // we have history about this output key. |
| 200 | auto& startedCount = outputIt->second; |
| 201 | // assign the old value first. |
| 202 | newCondition = startedCount > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 203 | if (matchStart) { |
| 204 | if (startedCount == 0) { |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 205 | mLastChangedToTrueDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 206 | // This condition for this output key will change from false -> true |
| 207 | changed = true; |
| 208 | } |
| 209 | |
| 210 | // it's ok to do ++ here, even if we don't count nesting. The >1 counts will be treated |
| 211 | // as 1 if not counting nesting. |
| 212 | startedCount++; |
| 213 | newCondition = ConditionState::kTrue; |
| 214 | } else { |
| 215 | // This is a stop event. |
| 216 | if (startedCount > 0) { |
| 217 | if (mCountNesting) { |
| 218 | startedCount--; |
| 219 | if (startedCount == 0) { |
| 220 | newCondition = ConditionState::kFalse; |
| 221 | } |
| 222 | } else { |
| 223 | // not counting nesting, so ignore the number of starts, stop now. |
| 224 | startedCount = 0; |
| 225 | newCondition = ConditionState::kFalse; |
| 226 | } |
| 227 | // if everything has stopped for this output key, condition true -> false; |
| 228 | if (startedCount == 0) { |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 229 | mLastChangedToFalseDimensions.insert(outputKey); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 230 | changed = true; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // if default condition is false, it means we don't need to keep the false values. |
| 235 | if (mInitialValue == ConditionState::kFalse && startedCount == 0) { |
| 236 | mSlicedConditionState.erase(outputIt); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 237 | VLOG("erase key %s", outputKey.toString().c_str()); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // dump all dimensions for debugging |
| 243 | if (DEBUG) { |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 244 | dumpState(); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 247 | (*conditionChangedCache) = changed; |
| 248 | (*conditionCache) = newCondition; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 249 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 250 | VLOG("SimplePredicate %lld nonSlicedChange? %d", (long long)mConditionId, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 251 | conditionChangedCache[mIndex] == true); |
| 252 | } |
| 253 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 254 | void SimpleConditionTracker::evaluateCondition( |
| 255 | const LogEvent& event, |
| 256 | const vector<MatchingState>& eventMatcherValues, |
| 257 | const vector<sp<ConditionTracker>>& mAllConditions, |
| 258 | vector<ConditionState>& conditionCache, |
| 259 | vector<bool>& conditionChangedCache) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 260 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 261 | // it has been evaluated. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 262 | VLOG("Yes, already evaluated, %lld %d", |
| 263 | (long long)mConditionId, conditionCache[mIndex]); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 264 | return; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 265 | } |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 266 | mLastChangedToTrueDimensions.clear(); |
| 267 | mLastChangedToFalseDimensions.clear(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 268 | |
David Chen | c18abed | 2017-11-22 16:47:59 -0800 | [diff] [blame] | 269 | if (mStopAllLogMatcherIndex >= 0 && mStopAllLogMatcherIndex < int(eventMatcherValues.size()) && |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 270 | eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) { |
| 271 | handleStopAll(conditionCache, conditionChangedCache); |
| 272 | return; |
| 273 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 274 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 275 | int matchedState = -1; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 276 | // Note: The order to evaluate the following start, stop, stop_all matters. |
| 277 | // The priority of overwrite is stop_all > stop > start. |
| 278 | if (mStartLogMatcherIndex >= 0 && |
| 279 | eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 280 | matchedState = 1; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | if (mStopLogMatcherIndex >= 0 && |
| 284 | eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 285 | matchedState = 0; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 288 | if (matchedState < 0) { |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 289 | // 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] | 290 | conditionChangedCache[mIndex] = false; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 291 | if (mSliced) { |
| 292 | // if the condition result is sliced. metrics won't directly get value from the |
| 293 | // cache, so just set any value other than kNotEvaluated. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 294 | conditionCache[mIndex] = mInitialValue; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 295 | } else { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 296 | const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY); |
| 297 | if (itr == mSlicedConditionState.end()) { |
| 298 | // condition not sliced, but we haven't seen the matched start or stop yet. so |
| 299 | // return initial value. |
| 300 | conditionCache[mIndex] = mInitialValue; |
| 301 | } else { |
| 302 | // return the cached condition. |
| 303 | conditionCache[mIndex] = |
| 304 | itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 305 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 306 | mUnSlicedPart = conditionCache[mIndex]; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 307 | } |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 308 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 309 | return; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 312 | ConditionState overallState = mInitialValue; |
| 313 | bool overallChanged = false; |
| 314 | |
| 315 | if (mOutputDimensions.size() == 0) { |
| 316 | handleConditionEvent(DEFAULT_DIMENSION_KEY, matchedState == 1, &overallState, |
| 317 | &overallChanged); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 318 | } else if (!mContainANYPositionInInternalDimensions) { |
| 319 | HashableDimensionKey outputValue; |
| 320 | filterValues(mOutputDimensions, event.getValues(), &outputValue); |
| 321 | |
| 322 | // If this event has multiple nodes in the attribution chain, this log event probably will |
| 323 | // generate multiple dimensions. If so, we will find if the condition changes for any |
| 324 | // dimension and ask the corresponding metric producer to verify whether the actual sliced |
| 325 | // condition has changed or not. |
| 326 | // A high level assumption is that a predicate is either sliced or unsliced. We will never |
| 327 | // have both sliced and unsliced version of a predicate. |
| 328 | handleConditionEvent(outputValue, matchedState == 1, &overallState, &overallChanged); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 329 | } else { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 330 | ALOGE("The condition tracker should not be sliced by ANY position matcher."); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 331 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 332 | conditionCache[mIndex] = overallState; |
| 333 | conditionChangedCache[mIndex] = overallChanged; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 334 | if (!mSliced) { |
| 335 | mUnSlicedPart = overallState; |
| 336 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void SimpleConditionTracker::isConditionMet( |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 340 | const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 341 | const vector<Matcher>& dimensionFields, |
| 342 | const bool isSubOutputDimensionFields, |
| 343 | const bool isPartialLink, |
| 344 | vector<ConditionState>& conditionCache, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 345 | std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const { |
| 346 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 347 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 348 | // it has been evaluated. |
| 349 | VLOG("Yes, already evaluated, %lld %d", |
| 350 | (long long)mConditionId, conditionCache[mIndex]); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 351 | return; |
| 352 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 353 | const auto pair = conditionParameters.find(mConditionId); |
| 354 | |
| 355 | if (pair == conditionParameters.end()) { |
| 356 | ConditionState conditionState = ConditionState::kNotEvaluated; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 357 | if (dimensionFields.size() > 0 && dimensionFields[0].mMatcher.getTag() == mDimensionTag) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 358 | conditionState = conditionState | getMetConditionDimension( |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 359 | allConditions, dimensionFields, isSubOutputDimensionFields, dimensionsKeySet); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 360 | } else { |
| 361 | conditionState = conditionState | mInitialValue; |
| 362 | if (!mSliced) { |
| 363 | const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY); |
| 364 | if (itr != mSlicedConditionState.end()) { |
| 365 | ConditionState sliceState = |
| 366 | itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 367 | conditionState = conditionState | sliceState; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | conditionCache[mIndex] = conditionState; |
| 372 | return; |
| 373 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 374 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 375 | ConditionState conditionState = ConditionState::kNotEvaluated; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 376 | const HashableDimensionKey& key = pair->second; |
| 377 | if (isPartialLink) { |
| 378 | // For unseen key, check whether the require dimensions are subset of sliced condition |
| 379 | // output. |
| 380 | conditionState = conditionState | mInitialValue; |
| 381 | for (const auto& slice : mSlicedConditionState) { |
| 382 | ConditionState sliceState = |
| 383 | slice.second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 384 | if (slice.first.contains(key)) { |
| 385 | conditionState = conditionState | sliceState; |
| 386 | if (sliceState == ConditionState::kTrue && dimensionFields.size() > 0) { |
| 387 | if (isSubOutputDimensionFields) { |
| 388 | HashableDimensionKey dimensionKey; |
| 389 | filterValues(dimensionFields, slice.first.getValues(), &dimensionKey); |
| 390 | dimensionsKeySet.insert(dimensionKey); |
| 391 | } else { |
| 392 | dimensionsKeySet.insert(slice.first); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } else { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 398 | auto startedCountIt = mSlicedConditionState.find(key); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 399 | conditionState = conditionState | mInitialValue; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 400 | if (startedCountIt != mSlicedConditionState.end()) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 401 | ConditionState sliceState = |
| 402 | startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 403 | conditionState = conditionState | sliceState; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 404 | if (sliceState == ConditionState::kTrue && dimensionFields.size() > 0) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 405 | if (isSubOutputDimensionFields) { |
| 406 | HashableDimensionKey dimensionKey; |
| 407 | filterValues(dimensionFields, startedCountIt->first.getValues(), &dimensionKey); |
| 408 | dimensionsKeySet.insert(dimensionKey); |
| 409 | } else { |
| 410 | dimensionsKeySet.insert(startedCountIt->first); |
Yangster-mac | 5503f5c | 2018-01-15 23:20:44 -0800 | [diff] [blame] | 411 | } |
Yangster-mac | 5503f5c | 2018-01-15 23:20:44 -0800 | [diff] [blame] | 412 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 413 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 414 | |
| 415 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 416 | conditionCache[mIndex] = conditionState; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 417 | VLOG("Predicate %lld return %d", (long long)mConditionId, conditionCache[mIndex]); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 420 | ConditionState SimpleConditionTracker::getMetConditionDimension( |
| 421 | const std::vector<sp<ConditionTracker>>& allConditions, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 422 | const vector<Matcher>& dimensionFields, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 423 | const bool isSubOutputDimensionFields, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 424 | std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 425 | ConditionState conditionState = mInitialValue; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 426 | if (dimensionFields.size() == 0 || mOutputDimensions.size() == 0 || |
| 427 | dimensionFields[0].mMatcher.getTag() != mOutputDimensions[0].mMatcher.getTag()) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 428 | const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY); |
| 429 | if (itr != mSlicedConditionState.end()) { |
| 430 | ConditionState sliceState = |
| 431 | itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 432 | conditionState = conditionState | sliceState; |
| 433 | } |
| 434 | return conditionState; |
| 435 | } |
| 436 | |
| 437 | for (const auto& slice : mSlicedConditionState) { |
| 438 | ConditionState sliceState = |
| 439 | slice.second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 440 | conditionState = conditionState | sliceState; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 441 | |
| 442 | if (sliceState == ConditionState::kTrue && dimensionFields.size() > 0) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 443 | if (isSubOutputDimensionFields) { |
| 444 | HashableDimensionKey dimensionKey; |
| 445 | filterValues(dimensionFields, slice.first.getValues(), &dimensionKey); |
| 446 | dimensionsKeySet.insert(dimensionKey); |
| 447 | } else { |
| 448 | dimensionsKeySet.insert(slice.first); |
| 449 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | return conditionState; |
| 453 | } |
| 454 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 455 | } // namespace statsd |
| 456 | } // namespace os |
| 457 | } // namespace android |