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 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 77 | mOutputDimension.insert(mOutputDimension.begin(), simpleCondition.dimension().begin(), |
| 78 | simpleCondition.dimension().end()); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 79 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 80 | if (mOutputDimension.size() > 0) { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 81 | mSliced = true; |
| 82 | } |
| 83 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 84 | if (simpleCondition.initial_value() == SimpleCondition_InitialValue_FALSE) { |
| 85 | mInitialValue = ConditionState::kFalse; |
| 86 | } else { |
| 87 | mInitialValue = ConditionState::kUnknown; |
| 88 | } |
| 89 | |
| 90 | mNonSlicedConditionState = mInitialValue; |
| 91 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 92 | mInitialized = true; |
| 93 | } |
| 94 | |
| 95 | SimpleConditionTracker::~SimpleConditionTracker() { |
| 96 | VLOG("~SimpleConditionTracker()"); |
| 97 | } |
| 98 | |
| 99 | bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig, |
| 100 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
| 101 | const unordered_map<string, int>& conditionNameIndexMap, |
| 102 | vector<bool>& stack) { |
| 103 | // SimpleConditionTracker does not have dependency on other conditions, thus we just return |
| 104 | // if the initialization was successful. |
| 105 | return mInitialized; |
| 106 | } |
| 107 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 108 | void print(map<HashableDimensionKey, int>& conditions, const string& name) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 109 | VLOG("%s DUMP:", name.c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 110 | for (const auto& pair : conditions) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 111 | VLOG("\t%s : %d", pair.first.c_str(), pair.second); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 115 | void SimpleConditionTracker::handleStopAll(std::vector<ConditionState>& conditionCache, |
| 116 | std::vector<bool>& conditionChangedCache) { |
| 117 | // Unless the default condition is false, and there was nothing started, otherwise we have |
| 118 | // triggered a condition change. |
| 119 | conditionChangedCache[mIndex] = |
| 120 | (mInitialValue == ConditionState::kFalse && mSlicedConditionState.empty()) ? false |
| 121 | : true; |
| 122 | |
| 123 | // After StopAll, we know everything has stopped. From now on, default condition is false. |
| 124 | mInitialValue = ConditionState::kFalse; |
| 125 | mSlicedConditionState.clear(); |
| 126 | conditionCache[mIndex] = ConditionState::kFalse; |
| 127 | } |
| 128 | |
| 129 | void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey, |
| 130 | bool matchStart, |
| 131 | std::vector<ConditionState>& conditionCache, |
| 132 | std::vector<bool>& conditionChangedCache) { |
| 133 | bool changed = false; |
| 134 | auto outputIt = mSlicedConditionState.find(outputKey); |
| 135 | ConditionState newCondition; |
| 136 | if (outputIt == mSlicedConditionState.end()) { |
| 137 | // We get a new output key. |
| 138 | newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse; |
| 139 | if (matchStart && mInitialValue != ConditionState::kTrue) { |
| 140 | mSlicedConditionState[outputKey] = 1; |
| 141 | changed = true; |
| 142 | } else if (mInitialValue != ConditionState::kFalse) { |
| 143 | // it's a stop and we don't have history about it. |
| 144 | // If the default condition is not false, it means this stop is valuable to us. |
| 145 | mSlicedConditionState[outputKey] = 0; |
| 146 | changed = true; |
| 147 | } |
| 148 | } else { |
| 149 | // we have history about this output key. |
| 150 | auto& startedCount = outputIt->second; |
| 151 | // assign the old value first. |
| 152 | newCondition = startedCount > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
| 153 | if (matchStart) { |
| 154 | if (startedCount == 0) { |
| 155 | // This condition for this output key will change from false -> true |
| 156 | changed = true; |
| 157 | } |
| 158 | |
| 159 | // it's ok to do ++ here, even if we don't count nesting. The >1 counts will be treated |
| 160 | // as 1 if not counting nesting. |
| 161 | startedCount++; |
| 162 | newCondition = ConditionState::kTrue; |
| 163 | } else { |
| 164 | // This is a stop event. |
| 165 | if (startedCount > 0) { |
| 166 | if (mCountNesting) { |
| 167 | startedCount--; |
| 168 | if (startedCount == 0) { |
| 169 | newCondition = ConditionState::kFalse; |
| 170 | } |
| 171 | } else { |
| 172 | // not counting nesting, so ignore the number of starts, stop now. |
| 173 | startedCount = 0; |
| 174 | newCondition = ConditionState::kFalse; |
| 175 | } |
| 176 | // if everything has stopped for this output key, condition true -> false; |
| 177 | if (startedCount == 0) { |
| 178 | changed = true; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // if default condition is false, it means we don't need to keep the false values. |
| 183 | if (mInitialValue == ConditionState::kFalse && startedCount == 0) { |
| 184 | mSlicedConditionState.erase(outputIt); |
| 185 | VLOG("erase key %s", outputKey.c_str()); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // dump all dimensions for debugging |
| 191 | if (DEBUG) { |
| 192 | print(mSlicedConditionState, mName); |
| 193 | } |
| 194 | |
| 195 | conditionChangedCache[mIndex] = changed; |
| 196 | conditionCache[mIndex] = newCondition; |
| 197 | |
| 198 | VLOG("SimpleCondition %s nonSlicedChange? %d", mName.c_str(), |
| 199 | conditionChangedCache[mIndex] == true); |
| 200 | } |
| 201 | |
| 202 | void SimpleConditionTracker::evaluateCondition(const LogEvent& event, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 203 | const vector<MatchingState>& eventMatcherValues, |
| 204 | const vector<sp<ConditionTracker>>& mAllConditions, |
| 205 | vector<ConditionState>& conditionCache, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 206 | vector<bool>& conditionChangedCache) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 207 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 208 | // it has been evaluated. |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 209 | VLOG("Yes, already evaluated, %s %d", mName.c_str(), conditionCache[mIndex]); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 210 | return; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 213 | if (mStopAllLogMatcherIndex >= 0 && |
| 214 | eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) { |
| 215 | handleStopAll(conditionCache, conditionChangedCache); |
| 216 | return; |
| 217 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 218 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 219 | int matchedState = -1; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 220 | // Note: The order to evaluate the following start, stop, stop_all matters. |
| 221 | // The priority of overwrite is stop_all > stop > start. |
| 222 | if (mStartLogMatcherIndex >= 0 && |
| 223 | eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 224 | matchedState = 1; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if (mStopLogMatcherIndex >= 0 && |
| 228 | eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 229 | matchedState = 0; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 232 | if (matchedState < 0) { |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 233 | // 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] | 234 | conditionChangedCache[mIndex] = false; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 235 | if (mSliced) { |
| 236 | // if the condition result is sliced. metrics won't directly get value from the |
| 237 | // cache, so just set any value other than kNotEvaluated. |
| 238 | conditionCache[mIndex] = ConditionState::kUnknown; |
| 239 | } else if (mSlicedConditionState.find(DEFAULT_DIMENSION_KEY) == |
| 240 | mSlicedConditionState.end()) { |
| 241 | // condition not sliced, but we haven't seen the matched start or stop yet. so return |
| 242 | // initial value. |
| 243 | conditionCache[mIndex] = mInitialValue; |
| 244 | } else { |
| 245 | // return the cached condition. |
| 246 | conditionCache[mIndex] = mSlicedConditionState[DEFAULT_DIMENSION_KEY] > 0 |
| 247 | ? ConditionState::kTrue |
| 248 | : ConditionState::kFalse; |
| 249 | } |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 250 | return; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 253 | // outputKey is the output key values. e.g, uid:1234 |
| 254 | const HashableDimensionKey outputKey = getHashableKey(getDimensionKey(event, mOutputDimension)); |
| 255 | handleConditionEvent(outputKey, matchedState == 1, conditionCache, conditionChangedCache); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void SimpleConditionTracker::isConditionMet( |
| 259 | const map<string, HashableDimensionKey>& conditionParameters, |
| 260 | const vector<sp<ConditionTracker>>& allConditions, vector<ConditionState>& conditionCache) { |
| 261 | const auto pair = conditionParameters.find(mName); |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 262 | HashableDimensionKey key = |
| 263 | (pair == conditionParameters.end()) ? DEFAULT_DIMENSION_KEY : pair->second; |
| 264 | |
| 265 | if (pair == conditionParameters.end() && mOutputDimension.size() > 0) { |
| 266 | ALOGE("Condition %s output has dimension, but it's not specified in the query!", |
| 267 | mName.c_str()); |
| 268 | conditionCache[mIndex] = mInitialValue; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 272 | VLOG("simpleCondition %s query key: %s", mName.c_str(), key.c_str()); |
| 273 | |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 274 | auto startedCountIt = mSlicedConditionState.find(key); |
| 275 | if (startedCountIt == mSlicedConditionState.end()) { |
| 276 | conditionCache[mIndex] = mInitialValue; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 277 | } else { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 278 | conditionCache[mIndex] = |
| 279 | startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | VLOG("Condition %s return %d", mName.c_str(), conditionCache[mIndex]); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | } // namespace statsd |
| 286 | } // namespace os |
| 287 | } // namespace android |