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