• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <gtest/gtest_prod.h>
21 #include "ConditionTracker.h"
22 #include "config/ConfigKey.h"
23 #include "src/statsd_config.pb.h"
24 #include "stats_util.h"
25 
26 namespace android {
27 namespace os {
28 namespace statsd {
29 
30 class SimpleConditionTracker : public ConditionTracker {
31 public:
32     SimpleConditionTracker(const ConfigKey& key, const int64_t& id, const uint64_t protoHash,
33                            const int index, const SimplePredicate& simplePredicate,
34                            const std::unordered_map<int64_t, int>& atomMatchingTrackerMap);
35 
36     ~SimpleConditionTracker();
37 
38     bool init(const std::vector<Predicate>& allConditionConfig,
39               const std::vector<sp<ConditionTracker>>& allConditionTrackers,
40               const std::unordered_map<int64_t, int>& conditionIdIndexMap, std::vector<bool>& stack,
41               std::vector<ConditionState>& conditionCache) override;
42 
43     bool onConfigUpdated(const std::vector<Predicate>& allConditionProtos, const int index,
44                          const std::vector<sp<ConditionTracker>>& allConditionTrackers,
45                          const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
46                          const std::unordered_map<int64_t, int>& conditionTrackerMap) override;
47 
48     void evaluateCondition(const LogEvent& event,
49                            const std::vector<MatchingState>& eventMatcherValues,
50                            const std::vector<sp<ConditionTracker>>& mAllConditions,
51                            std::vector<ConditionState>& conditionCache,
52                            std::vector<bool>& changedCache) override;
53 
54     void isConditionMet(const ConditionKey& conditionParameters,
55                         const std::vector<sp<ConditionTracker>>& allConditions,
56                         const bool isPartialLink,
57                         std::vector<ConditionState>& conditionCache) const override;
58 
getChangedToTrueDimensions(const std::vector<sp<ConditionTracker>> & allConditions)59     virtual const std::set<HashableDimensionKey>* getChangedToTrueDimensions(
60             const std::vector<sp<ConditionTracker>>& allConditions) const {
61         if (mSliced) {
62             return &mLastChangedToTrueDimensions;
63         } else {
64             return nullptr;
65         }
66     }
67 
getChangedToFalseDimensions(const std::vector<sp<ConditionTracker>> & allConditions)68     virtual const std::set<HashableDimensionKey>* getChangedToFalseDimensions(
69             const std::vector<sp<ConditionTracker>>& allConditions) const {
70         if (mSliced) {
71             return &mLastChangedToFalseDimensions;
72         } else {
73             return nullptr;
74         }
75     }
76 
getSlicedDimensionMap(const std::vector<sp<ConditionTracker>> & allConditions)77     const std::map<HashableDimensionKey, int>* getSlicedDimensionMap(
78             const std::vector<sp<ConditionTracker>>& allConditions) const override {
79         return &mSlicedConditionState;
80     }
81 
IsChangedDimensionTrackable()82     bool IsChangedDimensionTrackable() const  override { return true; }
83 
IsSimpleCondition()84     bool IsSimpleCondition() const  override { return true; }
85 
equalOutputDimensions(const std::vector<sp<ConditionTracker>> & allConditions,const vector<Matcher> & dimensions)86     bool equalOutputDimensions(
87         const std::vector<sp<ConditionTracker>>& allConditions,
88         const vector<Matcher>& dimensions) const override {
89             return equalDimensions(mOutputDimensions, dimensions);
90     }
91 
92 private:
93     const ConfigKey mConfigKey;
94     // The index of the LogEventMatcher which defines the start.
95     int mStartLogMatcherIndex;
96 
97     // The index of the LogEventMatcher which defines the end.
98     int mStopLogMatcherIndex;
99 
100     // if the start end needs to be nested.
101     bool mCountNesting;
102 
103     // The index of the LogEventMatcher which defines the stop all.
104     int mStopAllLogMatcherIndex;
105 
106     ConditionState mInitialValue;
107 
108     std::vector<Matcher> mOutputDimensions;
109 
110     bool mContainANYPositionInInternalDimensions;
111 
112     std::set<HashableDimensionKey> mLastChangedToTrueDimensions;
113     std::set<HashableDimensionKey> mLastChangedToFalseDimensions;
114 
115     std::map<HashableDimensionKey, int> mSlicedConditionState;
116 
117     void setMatcherIndices(const SimplePredicate& predicate,
118                            const std::unordered_map<int64_t, int>& logTrackerMap);
119 
120     void handleStopAll(std::vector<ConditionState>& conditionCache,
121                        std::vector<bool>& changedCache);
122 
123     void handleConditionEvent(const HashableDimensionKey& outputKey, bool matchStart,
124                               ConditionState* conditionCache, bool* changedCache);
125 
126     bool hitGuardRail(const HashableDimensionKey& newKey);
127 
128     void dumpState();
129 
130     FRIEND_TEST(SimpleConditionTrackerTest, TestSlicedCondition);
131     FRIEND_TEST(SimpleConditionTrackerTest, TestSlicedWithNoOutputDim);
132     FRIEND_TEST(SimpleConditionTrackerTest, TestStopAll);
133     FRIEND_TEST(SimpleConditionTrackerTest, TestGuardrailNotHitWhenDefaultFalse);
134     FRIEND_TEST(SimpleConditionTrackerTest, TestGuardrailHitWhenDefaultUnknown);
135     FRIEND_TEST(ConfigUpdateTest, TestUpdateConditions);
136 };
137 
138 }  // namespace statsd
139 }  // namespace os
140 }  // namespace android
141 
142 #endif  // SIMPLE_CONDITION_TRACKER_H
143