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" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 22 | #include <log/logprint.h> |
| 23 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 28 | using std::map; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | using std::unique_ptr; |
| 31 | using std::unordered_map; |
| 32 | using std::vector; |
| 33 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 34 | SimpleConditionTracker::SimpleConditionTracker( |
| 35 | const string& name, const int index, const SimpleCondition& simpleCondition, |
| 36 | const unordered_map<string, int>& trackerNameIndexMap) |
| 37 | : ConditionTracker(name, index) { |
| 38 | VLOG("creating SimpleConditionTracker %s", mName.c_str()); |
| 39 | mCountNesting = simpleCondition.count_nesting(); |
| 40 | |
| 41 | if (simpleCondition.has_start()) { |
| 42 | auto pair = trackerNameIndexMap.find(simpleCondition.start()); |
| 43 | if (pair == trackerNameIndexMap.end()) { |
| 44 | ALOGW("Start matcher %s not found in the config", simpleCondition.start().c_str()); |
| 45 | return; |
| 46 | } |
| 47 | mStartLogMatcherIndex = pair->second; |
| 48 | mTrackerIndex.insert(mStartLogMatcherIndex); |
| 49 | } else { |
| 50 | mStartLogMatcherIndex = -1; |
| 51 | } |
| 52 | |
| 53 | if (simpleCondition.has_stop()) { |
| 54 | auto pair = trackerNameIndexMap.find(simpleCondition.stop()); |
| 55 | if (pair == trackerNameIndexMap.end()) { |
| 56 | ALOGW("Stop matcher %s not found in the config", simpleCondition.stop().c_str()); |
| 57 | return; |
| 58 | } |
| 59 | mStopLogMatcherIndex = pair->second; |
Yao 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 | |
| 65 | if (simpleCondition.has_stop_all()) { |
| 66 | auto pair = trackerNameIndexMap.find(simpleCondition.stop_all()); |
| 67 | if (pair == trackerNameIndexMap.end()) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 68 | ALOGW("Stop all matcher %s not found in the config", simpleCondition.stop().c_str()); |
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 | |
| 77 | mInitialized = true; |
| 78 | } |
| 79 | |
| 80 | SimpleConditionTracker::~SimpleConditionTracker() { |
| 81 | VLOG("~SimpleConditionTracker()"); |
| 82 | } |
| 83 | |
| 84 | bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig, |
| 85 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
| 86 | const unordered_map<string, int>& conditionNameIndexMap, |
| 87 | vector<bool>& stack) { |
| 88 | // SimpleConditionTracker does not have dependency on other conditions, thus we just return |
| 89 | // if the initialization was successful. |
| 90 | return mInitialized; |
| 91 | } |
| 92 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 93 | void print(unordered_map<HashableDimensionKey, ConditionState>& conditions, const string& name) { |
| 94 | VLOG("%s DUMP:", name.c_str()); |
| 95 | |
| 96 | for (const auto& pair : conditions) { |
| 97 | VLOG("\t%s %d", pair.first.c_str(), pair.second); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void SimpleConditionTracker::addDimensions(const std::vector<KeyMatcher>& keyMatchers) { |
| 102 | VLOG("Added dimensions size %lu", (unsigned long)keyMatchers.size()); |
| 103 | mDimensionsList.push_back(keyMatchers); |
| 104 | mSliced = true; |
| 105 | } |
| 106 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 107 | bool SimpleConditionTracker::evaluateCondition(const LogEvent& event, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 108 | const vector<MatchingState>& eventMatcherValues, |
| 109 | const vector<sp<ConditionTracker>>& mAllConditions, |
| 110 | vector<ConditionState>& conditionCache, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 111 | vector<bool>& nonSlicedConditionChanged, |
| 112 | std::vector<bool>& slicedConditionChanged) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 113 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 114 | // it has been evaluated. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 115 | VLOG("Yes, already evaluated, %s %d", mName.c_str(), mNonSlicedConditionState); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Ignore nesting, because we know we cannot trust ourselves on tracking nesting conditions. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 120 | |
| 121 | ConditionState newCondition = mNonSlicedConditionState; |
| 122 | bool matched = false; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 123 | // Note: The order to evaluate the following start, stop, stop_all matters. |
| 124 | // The priority of overwrite is stop_all > stop > start. |
| 125 | if (mStartLogMatcherIndex >= 0 && |
| 126 | eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 127 | matched = true; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 128 | newCondition = ConditionState::kTrue; |
| 129 | } |
| 130 | |
| 131 | if (mStopLogMatcherIndex >= 0 && |
| 132 | eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 133 | matched = true; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 134 | newCondition = ConditionState::kFalse; |
| 135 | } |
| 136 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 137 | bool stopAll = false; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 138 | if (mStopAllLogMatcherIndex >= 0 && |
| 139 | eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 140 | matched = true; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 141 | newCondition = ConditionState::kFalse; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 142 | stopAll = true; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 145 | if (matched == false) { |
| 146 | slicedConditionChanged[mIndex] = false; |
| 147 | nonSlicedConditionChanged[mIndex] = false; |
| 148 | conditionCache[mIndex] = mNonSlicedConditionState; |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | bool nonSlicedChanged = mNonSlicedConditionState != newCondition; |
| 153 | |
| 154 | bool slicedChanged = false; |
| 155 | |
| 156 | if (stopAll) { |
| 157 | // TODO: handle stop all; all dimension should be cleared. |
| 158 | } |
| 159 | |
| 160 | if (mDimensionsList.size() > 0) { |
| 161 | for (size_t i = 0; i < mDimensionsList.size(); i++) { |
| 162 | const auto& dim = mDimensionsList[i]; |
| 163 | vector<KeyValuePair> key = getDimensionKey(event, dim); |
| 164 | HashableDimensionKey hashableKey = getHashableKey(key); |
| 165 | if (mSlicedConditionState.find(hashableKey) == mSlicedConditionState.end() || |
| 166 | mSlicedConditionState[hashableKey] != newCondition) { |
| 167 | slicedChanged = true; |
| 168 | mSlicedConditionState[hashableKey] = newCondition; |
| 169 | } |
| 170 | VLOG("key: %s %d", hashableKey.c_str(), newCondition); |
| 171 | } |
| 172 | // dump all dimensions for debugging |
| 173 | if (DEBUG) { |
| 174 | print(mSlicedConditionState, mName); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // even if this SimpleCondition is not sliced, it may be part of a sliced CombinationCondition |
| 179 | // if the nonSliced condition changed, it may affect the sliced condition in the parent node. |
| 180 | // so mark the slicedConditionChanged to be true. |
| 181 | // For example: APP_IN_BACKGROUND_OR_SCREEN_OFF |
| 182 | // APP_IN_BACKGROUND is sliced [App_A->True, App_B->False]. |
| 183 | // SCREEN_OFF is not sliced, and it changes from False -> True; |
| 184 | // We need to populate this change to parent condition. Because for App_B, |
| 185 | // the APP_IN_BACKGROUND_OR_SCREEN_OFF condition would change from False->True. |
| 186 | slicedConditionChanged[mIndex] = mSliced ? slicedChanged : nonSlicedChanged; |
| 187 | nonSlicedConditionChanged[mIndex] = nonSlicedChanged; |
| 188 | |
| 189 | VLOG("SimpleCondition %s nonSlicedChange? %d SlicedChanged? %d", mName.c_str(), |
| 190 | nonSlicedConditionChanged[mIndex] == true, slicedConditionChanged[mIndex] == true); |
| 191 | mNonSlicedConditionState = newCondition; |
| 192 | conditionCache[mIndex] = mNonSlicedConditionState; |
| 193 | |
| 194 | return nonSlicedConditionChanged[mIndex]; |
| 195 | } |
| 196 | |
| 197 | void SimpleConditionTracker::isConditionMet( |
| 198 | const map<string, HashableDimensionKey>& conditionParameters, |
| 199 | const vector<sp<ConditionTracker>>& allConditions, vector<ConditionState>& conditionCache) { |
| 200 | const auto pair = conditionParameters.find(mName); |
| 201 | if (pair == conditionParameters.end()) { |
| 202 | // the query does not need my sliced condition. just return the non sliced condition. |
| 203 | conditionCache[mIndex] = mNonSlicedConditionState; |
| 204 | VLOG("Condition %s return %d", mName.c_str(), mNonSlicedConditionState); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | const HashableDimensionKey& key = pair->second; |
| 209 | VLOG("simpleCondition %s query key: %s", mName.c_str(), key.c_str()); |
| 210 | |
| 211 | if (mSlicedConditionState.find(key) == mSlicedConditionState.end()) { |
| 212 | // never seen this key before. the condition is unknown to us. |
| 213 | conditionCache[mIndex] = ConditionState::kUnknown; |
| 214 | } else { |
| 215 | conditionCache[mIndex] = mSlicedConditionState[key]; |
| 216 | } |
| 217 | |
| 218 | VLOG("Condition %s return %d", mName.c_str(), conditionCache[mIndex]); |
| 219 | |
| 220 | if (DEBUG) { |
| 221 | print(mSlicedConditionState, mName); |
| 222 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace statsd |
| 226 | } // namespace os |
| 227 | } // namespace android |