blob: ea7f87bde2b8044c94720487e3d33f6d810628f1 [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001/*
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 Chen967b2052017-11-07 16:36:43 -080020#include <gtest/gtest_prod.h>
Yao Chencaf339d2017-10-06 16:01:10 -070021#include "ConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080022#include "config/ConfigKey.h"
Yao Chencaf339d2017-10-06 16:01:10 -070023#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yao Chen729093d2017-10-16 10:33:26 -070024#include "stats_util.h"
Yao Chencaf339d2017-10-06 16:01:10 -070025
26namespace android {
27namespace os {
28namespace statsd {
29
30class SimpleConditionTracker : public virtual ConditionTracker {
31public:
Yangster-mac94e197c2018-01-02 16:03:03 -080032 SimpleConditionTracker(const ConfigKey& key, const int64_t& id, const int index,
Stefan Lafon12d01fa2017-12-04 20:56:09 -080033 const SimplePredicate& simplePredicate,
Yangster-mac94e197c2018-01-02 16:03:03 -080034 const std::unordered_map<int64_t, int>& trackerNameIndexMap);
Yao Chencaf339d2017-10-06 16:01:10 -070035
36 ~SimpleConditionTracker();
37
Stefan Lafon12d01fa2017-12-04 20:56:09 -080038 bool init(const std::vector<Predicate>& allConditionConfig,
Yao Chencaf339d2017-10-06 16:01:10 -070039 const std::vector<sp<ConditionTracker>>& allConditionTrackers,
tsaichristine6e2e92d2020-05-18 14:39:45 -070040 const std::unordered_map<int64_t, int>& conditionIdIndexMap, std::vector<bool>& stack,
41 std::vector<ConditionState>& initialConditionCache) override;
Yao Chencaf339d2017-10-06 16:01:10 -070042
Yao Chen967b2052017-11-07 16:36:43 -080043 void evaluateCondition(const LogEvent& event,
Yao Chencaf339d2017-10-06 16:01:10 -070044 const std::vector<MatchingState>& eventMatcherValues,
45 const std::vector<sp<ConditionTracker>>& mAllConditions,
46 std::vector<ConditionState>& conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -080047 std::vector<bool>& changedCache) override;
Yao Chen729093d2017-10-16 10:33:26 -070048
Yangster-mac20877162017-12-22 17:19:39 -080049 void isConditionMet(const ConditionKey& conditionParameters,
Yao Chen729093d2017-10-16 10:33:26 -070050 const std::vector<sp<ConditionTracker>>& allConditions,
Yangster13fb7e42018-03-07 17:30:49 -080051 const bool isPartialLink,
tsaichristine76853372019-08-06 17:17:03 -070052 std::vector<ConditionState>& conditionCache) const override;
Yao Chen729093d2017-10-16 10:33:26 -070053
Yao Chen580ea3212018-02-26 14:21:54 -080054 virtual const std::set<HashableDimensionKey>* getChangedToTrueDimensions(
55 const std::vector<sp<ConditionTracker>>& allConditions) const {
56 if (mSliced) {
57 return &mLastChangedToTrueDimensions;
58 } else {
59 return nullptr;
60 }
61 }
Yangster13fb7e42018-03-07 17:30:49 -080062
Yao Chen580ea3212018-02-26 14:21:54 -080063 virtual const std::set<HashableDimensionKey>* getChangedToFalseDimensions(
64 const std::vector<sp<ConditionTracker>>& allConditions) const {
65 if (mSliced) {
66 return &mLastChangedToFalseDimensions;
67 } else {
68 return nullptr;
69 }
70 }
71
Yangster13fb7e42018-03-07 17:30:49 -080072 void getTrueSlicedDimensions(
73 const std::vector<sp<ConditionTracker>>& allConditions,
74 std::set<HashableDimensionKey>* dimensions) const override {
75 for (const auto& itr : mSlicedConditionState) {
76 if (itr.second > 0) {
77 dimensions->insert(itr.first);
78 }
79 }
80 }
81
82 bool IsChangedDimensionTrackable() const override { return true; }
83
84 bool IsSimpleCondition() const override { return true; }
85
86 bool equalOutputDimensions(
87 const std::vector<sp<ConditionTracker>>& allConditions,
88 const vector<Matcher>& dimensions) const override {
89 return equalDimensions(mOutputDimensions, dimensions);
90 }
91
Yao Chencaf339d2017-10-06 16:01:10 -070092private:
Yao Chenb3561512017-11-21 18:07:17 -080093 const ConfigKey mConfigKey;
Yao Chencaf339d2017-10-06 16:01:10 -070094 // 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;
Yao Chen729093d2017-10-16 10:33:26 -0700105
Yao Chen967b2052017-11-07 16:36:43 -0800106 ConditionState mInitialValue;
Yao Chen729093d2017-10-16 10:33:26 -0700107
Yao Chen8a8d16c2018-02-08 14:50:40 -0800108 std::vector<Matcher> mOutputDimensions;
109
Yangster13fb7e42018-03-07 17:30:49 -0800110 bool mContainANYPositionInInternalDimensions;
111
Yao Chen580ea3212018-02-26 14:21:54 -0800112 std::set<HashableDimensionKey> mLastChangedToTrueDimensions;
113 std::set<HashableDimensionKey> mLastChangedToFalseDimensions;
114
Yao Chen8a8d16c2018-02-08 14:50:40 -0800115 int mDimensionTag;
Yao Chen967b2052017-11-07 16:36:43 -0800116
117 std::map<HashableDimensionKey, int> mSlicedConditionState;
118
119 void handleStopAll(std::vector<ConditionState>& conditionCache,
120 std::vector<bool>& changedCache);
121
Yao Chen8a8d16c2018-02-08 14:50:40 -0800122 void handleConditionEvent(const HashableDimensionKey& outputKey, bool matchStart,
123 ConditionState* conditionCache, bool* changedCache);
Yao Chen967b2052017-11-07 16:36:43 -0800124
Yao Chenb3561512017-11-21 18:07:17 -0800125 bool hitGuardRail(const HashableDimensionKey& newKey);
126
Yao Chen580ea3212018-02-26 14:21:54 -0800127 void dumpState();
128
Yao Chen967b2052017-11-07 16:36:43 -0800129 FRIEND_TEST(SimpleConditionTrackerTest, TestSlicedCondition);
130 FRIEND_TEST(SimpleConditionTrackerTest, TestSlicedWithNoOutputDim);
131 FRIEND_TEST(SimpleConditionTrackerTest, TestStopAll);
Yao Chencaf339d2017-10-06 16:01:10 -0700132};
133
134} // namespace statsd
135} // namespace os
136} // namespace android
137
138#endif // SIMPLE_CONDITION_TRACKER_H