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 | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 17 | #define DEBUG true // 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 | 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( |
| 34 | const string& name, const int index, const SimpleCondition& simpleCondition, |
| 35 | const unordered_map<string, int>& trackerNameIndexMap) |
| 36 | : ConditionTracker(name, index) { |
| 37 | VLOG("creating SimpleConditionTracker %s", mName.c_str()); |
| 38 | mCountNesting = simpleCondition.count_nesting(); |
| 39 | |
| 40 | if (simpleCondition.has_start()) { |
| 41 | auto pair = trackerNameIndexMap.find(simpleCondition.start()); |
| 42 | if (pair == trackerNameIndexMap.end()) { |
| 43 | ALOGW("Start matcher %s not found in the config", simpleCondition.start().c_str()); |
| 44 | return; |
| 45 | } |
| 46 | mStartLogMatcherIndex = pair->second; |
| 47 | mTrackerIndex.insert(mStartLogMatcherIndex); |
| 48 | } else { |
| 49 | mStartLogMatcherIndex = -1; |
| 50 | } |
| 51 | |
| 52 | if (simpleCondition.has_stop()) { |
| 53 | auto pair = trackerNameIndexMap.find(simpleCondition.stop()); |
| 54 | if (pair == trackerNameIndexMap.end()) { |
| 55 | ALOGW("Stop matcher %s not found in the config", simpleCondition.stop().c_str()); |
| 56 | return; |
| 57 | } |
| 58 | mStopLogMatcherIndex = pair->second; |
Yao Chen | 4b14685 | 2017-10-10 15:34:42 -0700 | [diff] [blame] | 59 | mTrackerIndex.insert(mStopLogMatcherIndex); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 60 | } else { |
| 61 | mStopLogMatcherIndex = -1; |
| 62 | } |
| 63 | |
| 64 | if (simpleCondition.has_stop_all()) { |
| 65 | auto pair = trackerNameIndexMap.find(simpleCondition.stop_all()); |
| 66 | if (pair == trackerNameIndexMap.end()) { |
| 67 | ALOGW("Stop matcher %s not found in the config", simpleCondition.stop().c_str()); |
| 68 | return; |
| 69 | } |
| 70 | mStopAllLogMatcherIndex = pair->second; |
| 71 | mTrackerIndex.insert(mStopAllLogMatcherIndex); |
| 72 | } else { |
| 73 | mStopAllLogMatcherIndex = -1; |
| 74 | } |
| 75 | |
| 76 | mInitialized = true; |
| 77 | } |
| 78 | |
| 79 | SimpleConditionTracker::~SimpleConditionTracker() { |
| 80 | VLOG("~SimpleConditionTracker()"); |
| 81 | } |
| 82 | |
| 83 | bool SimpleConditionTracker::init(const vector<Condition>& allConditionConfig, |
| 84 | const vector<sp<ConditionTracker>>& allConditionTrackers, |
| 85 | const unordered_map<string, int>& conditionNameIndexMap, |
| 86 | vector<bool>& stack) { |
| 87 | // SimpleConditionTracker does not have dependency on other conditions, thus we just return |
| 88 | // if the initialization was successful. |
| 89 | return mInitialized; |
| 90 | } |
| 91 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 92 | bool SimpleConditionTracker::evaluateCondition(const LogEvent& event, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 93 | const vector<MatchingState>& eventMatcherValues, |
| 94 | const vector<sp<ConditionTracker>>& mAllConditions, |
| 95 | vector<ConditionState>& conditionCache, |
| 96 | vector<bool>& changedCache) { |
| 97 | if (conditionCache[mIndex] != ConditionState::kNotEvaluated) { |
| 98 | // it has been evaluated. |
| 99 | VLOG("Yes, already evaluated, %s %d", mName.c_str(), mConditionState); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Ignore nesting, because we know we cannot trust ourselves on tracking nesting conditions. |
| 104 | ConditionState newCondition = mConditionState; |
| 105 | // Note: The order to evaluate the following start, stop, stop_all matters. |
| 106 | // The priority of overwrite is stop_all > stop > start. |
| 107 | if (mStartLogMatcherIndex >= 0 && |
| 108 | eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) { |
| 109 | newCondition = ConditionState::kTrue; |
| 110 | } |
| 111 | |
| 112 | if (mStopLogMatcherIndex >= 0 && |
| 113 | eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) { |
| 114 | newCondition = ConditionState::kFalse; |
| 115 | } |
| 116 | |
| 117 | if (mStopAllLogMatcherIndex >= 0 && |
| 118 | eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) { |
| 119 | newCondition = ConditionState::kFalse; |
| 120 | } |
| 121 | |
| 122 | bool changed = (mConditionState != newCondition); |
| 123 | mConditionState = newCondition; |
| 124 | conditionCache[mIndex] = mConditionState; |
| 125 | changedCache[mIndex] = changed; |
| 126 | return changed; |
| 127 | } |
| 128 | |
| 129 | } // namespace statsd |
| 130 | } // namespace os |
| 131 | } // namespace android |