| 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 | |
| 17 | #ifndef SIMPLE_CONDITION_TRACKER_H |
| 18 | #define SIMPLE_CONDITION_TRACKER_H |
| 19 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 20 | #include <gtest/gtest_prod.h> |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 21 | #include "ConditionTracker.h" |
| Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 23 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 24 | #include "stats_util.h" |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace statsd { |
| 29 | |
| 30 | class SimpleConditionTracker : public virtual ConditionTracker { |
| 31 | public: |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 32 | SimpleConditionTracker(const ConfigKey& key, const int64_t& id, const int index, |
| Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 33 | const SimplePredicate& simplePredicate, |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 34 | const std::unordered_map<int64_t, int>& trackerNameIndexMap); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 35 | |
| 36 | ~SimpleConditionTracker(); |
| 37 | |
| Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 38 | bool init(const std::vector<Predicate>& allConditionConfig, |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 39 | const std::vector<sp<ConditionTracker>>& allConditionTrackers, |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 40 | const std::unordered_map<int64_t, int>& conditionIdIndexMap, |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 41 | std::vector<bool>& stack) override; |
| 42 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 43 | void evaluateCondition(const LogEvent& event, |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 44 | const std::vector<MatchingState>& eventMatcherValues, |
| 45 | const std::vector<sp<ConditionTracker>>& mAllConditions, |
| 46 | std::vector<ConditionState>& conditionCache, |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 47 | std::vector<bool>& changedCache) override; |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 48 | |
| Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 49 | void isConditionMet(const ConditionKey& conditionParameters, |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 50 | const std::vector<sp<ConditionTracker>>& allConditions, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 51 | const vector<Matcher>& dimensionFields, |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 52 | const bool isSubOutputDimensionFields, |
| 53 | const bool isPartialLink, |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 54 | std::vector<ConditionState>& conditionCache, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 55 | std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const override; |
| Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 56 | |
| 57 | ConditionState getMetConditionDimension( |
| 58 | const std::vector<sp<ConditionTracker>>& allConditions, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 59 | const vector<Matcher>& dimensionFields, |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 60 | const bool isSubOutputDimensionFields, |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 61 | std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const override; |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 62 | |
| Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 63 | virtual const std::set<HashableDimensionKey>* getChangedToTrueDimensions( |
| 64 | const std::vector<sp<ConditionTracker>>& allConditions) const { |
| 65 | if (mSliced) { |
| 66 | return &mLastChangedToTrueDimensions; |
| 67 | } else { |
| 68 | return nullptr; |
| 69 | } |
| 70 | } |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 71 | |
| Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 72 | virtual const std::set<HashableDimensionKey>* getChangedToFalseDimensions( |
| 73 | const std::vector<sp<ConditionTracker>>& allConditions) const { |
| 74 | if (mSliced) { |
| 75 | return &mLastChangedToFalseDimensions; |
| 76 | } else { |
| 77 | return nullptr; |
| 78 | } |
| 79 | } |
| 80 | |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 81 | void getTrueSlicedDimensions( |
| 82 | const std::vector<sp<ConditionTracker>>& allConditions, |
| 83 | std::set<HashableDimensionKey>* dimensions) const override { |
| 84 | for (const auto& itr : mSlicedConditionState) { |
| 85 | if (itr.second > 0) { |
| 86 | dimensions->insert(itr.first); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool IsChangedDimensionTrackable() const override { return true; } |
| 92 | |
| 93 | bool IsSimpleCondition() const override { return true; } |
| 94 | |
| 95 | bool equalOutputDimensions( |
| 96 | const std::vector<sp<ConditionTracker>>& allConditions, |
| 97 | const vector<Matcher>& dimensions) const override { |
| 98 | return equalDimensions(mOutputDimensions, dimensions); |
| 99 | } |
| 100 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 101 | private: |
| Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 102 | const ConfigKey mConfigKey; |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 103 | // The index of the LogEventMatcher which defines the start. |
| 104 | int mStartLogMatcherIndex; |
| 105 | |
| 106 | // The index of the LogEventMatcher which defines the end. |
| 107 | int mStopLogMatcherIndex; |
| 108 | |
| 109 | // if the start end needs to be nested. |
| 110 | bool mCountNesting; |
| 111 | |
| 112 | // The index of the LogEventMatcher which defines the stop all. |
| 113 | int mStopAllLogMatcherIndex; |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 114 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 115 | ConditionState mInitialValue; |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 116 | |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 117 | std::vector<Matcher> mOutputDimensions; |
| 118 | |
| Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 119 | bool mContainANYPositionInInternalDimensions; |
| 120 | |
| Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 121 | std::set<HashableDimensionKey> mLastChangedToTrueDimensions; |
| 122 | std::set<HashableDimensionKey> mLastChangedToFalseDimensions; |
| 123 | |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 124 | int mDimensionTag; |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 125 | |
| 126 | std::map<HashableDimensionKey, int> mSlicedConditionState; |
| 127 | |
| 128 | void handleStopAll(std::vector<ConditionState>& conditionCache, |
| 129 | std::vector<bool>& changedCache); |
| 130 | |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 131 | void handleConditionEvent(const HashableDimensionKey& outputKey, bool matchStart, |
| 132 | ConditionState* conditionCache, bool* changedCache); |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 133 | |
| Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 134 | bool hitGuardRail(const HashableDimensionKey& newKey); |
| 135 | |
| Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 136 | void dumpState(); |
| 137 | |
| Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 138 | FRIEND_TEST(SimpleConditionTrackerTest, TestSlicedCondition); |
| 139 | FRIEND_TEST(SimpleConditionTrackerTest, TestSlicedWithNoOutputDim); |
| 140 | FRIEND_TEST(SimpleConditionTrackerTest, TestStopAll); |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace statsd |
| 144 | } // namespace os |
| 145 | } // namespace android |
| 146 | |
| 147 | #endif // SIMPLE_CONDITION_TRACKER_H |