Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [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 | #pragma once |
| 18 | |
| 19 | #include <utils/JenkinsHash.h> |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 20 | #include <vector> |
| 21 | #include "FieldValue.h" |
| 22 | #include "android-base/stringprintf.h" |
| 23 | #include "logd/LogEvent.h" |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 29 | using android::base::StringPrintf; |
| 30 | |
| 31 | struct Metric2Condition { |
| 32 | int64_t conditionId; |
| 33 | std::vector<Matcher> metricFields; |
| 34 | std::vector<Matcher> conditionFields; |
| 35 | }; |
| 36 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 37 | struct Metric2State { |
| 38 | int32_t stateAtomId; |
| 39 | std::vector<Matcher> metricFields; |
| 40 | std::vector<Matcher> stateFields; |
| 41 | }; |
| 42 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 43 | class HashableDimensionKey { |
| 44 | public: |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 45 | explicit HashableDimensionKey(const std::vector<FieldValue>& values) { |
| 46 | mValues = values; |
| 47 | } |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 48 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 49 | HashableDimensionKey() {}; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 50 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 51 | HashableDimensionKey(const HashableDimensionKey& that) : mValues(that.getValues()){}; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 52 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 53 | inline void addValue(const FieldValue& value) { |
| 54 | mValues.push_back(value); |
| 55 | } |
| 56 | |
| 57 | inline const std::vector<FieldValue>& getValues() const { |
| 58 | return mValues; |
| 59 | } |
| 60 | |
| 61 | inline std::vector<FieldValue>* mutableValues() { |
| 62 | return &mValues; |
| 63 | } |
| 64 | |
| 65 | inline FieldValue* mutableValue(size_t i) { |
| 66 | if (i >= 0 && i < mValues.size()) { |
| 67 | return &(mValues[i]); |
| 68 | } |
| 69 | return nullptr; |
| 70 | } |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 71 | |
| 72 | std::string toString() const; |
| 73 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 74 | bool operator!=(const HashableDimensionKey& that) const; |
| 75 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 76 | bool operator==(const HashableDimensionKey& that) const; |
| 77 | |
| 78 | bool operator<(const HashableDimensionKey& that) const; |
| 79 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 80 | bool contains(const HashableDimensionKey& that) const; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 81 | |
| 82 | private: |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 83 | std::vector<FieldValue> mValues; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 86 | class MetricDimensionKey { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 87 | public: |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 88 | explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat, |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 89 | const HashableDimensionKey& stateValuesKey) |
| 90 | : mDimensionKeyInWhat(dimensionKeyInWhat), mStateValuesKey(stateValuesKey){}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 91 | |
| 92 | MetricDimensionKey(){}; |
| 93 | |
| 94 | MetricDimensionKey(const MetricDimensionKey& that) |
| 95 | : mDimensionKeyInWhat(that.getDimensionKeyInWhat()), |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 96 | mStateValuesKey(that.getStateValuesKey()){}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 97 | |
| 98 | MetricDimensionKey& operator=(const MetricDimensionKey& from) = default; |
| 99 | |
| 100 | std::string toString() const; |
| 101 | |
| 102 | inline const HashableDimensionKey& getDimensionKeyInWhat() const { |
| 103 | return mDimensionKeyInWhat; |
| 104 | } |
| 105 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 106 | inline const HashableDimensionKey& getStateValuesKey() const { |
| 107 | return mStateValuesKey; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 108 | } |
| 109 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 110 | inline void setStateValuesKey(const HashableDimensionKey& key) { |
| 111 | mStateValuesKey = key; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 112 | } |
| 113 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 114 | bool hasStateValuesKey() const { |
| 115 | return mStateValuesKey.getValues().size() > 0; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool operator==(const MetricDimensionKey& that) const; |
| 119 | |
| 120 | bool operator<(const MetricDimensionKey& that) const; |
| 121 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 122 | private: |
| 123 | HashableDimensionKey mDimensionKeyInWhat; |
| 124 | HashableDimensionKey mStateValuesKey; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 127 | android::hash_t hashDimension(const HashableDimensionKey& key); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 128 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 129 | /** |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 130 | * Returns true if a FieldValue field matches the matcher field. |
| 131 | * The value of the FieldValue is output. |
| 132 | */ |
| 133 | bool filterValues(const Matcher& matcherField, const std::vector<FieldValue>& values, |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 134 | FieldValue* output); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 137 | * Creating HashableDimensionKeys from FieldValues using matcher. |
| 138 | * |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 139 | * This function may make modifications to the Field if the matcher has Position=FIRST,LAST or ALL |
| 140 | * in it. This is because: for example, when we create dimension from last uid in attribution chain, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 141 | * In one event, uid 1000 is at position 5 and it's the last |
| 142 | * In another event, uid 1000 is at position 6, and it's the last |
| 143 | * these 2 events should be mapped to the same dimension. So we will remove the original position |
| 144 | * from the dimension key for the uid field (by applying 0x80 bit mask). |
| 145 | */ |
| 146 | bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 147 | HashableDimensionKey* output); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Filter the values from FieldValues using the matchers. |
| 151 | * |
| 152 | * In contrast to the above function, this function will not do any modification to the original |
| 153 | * data. Considering it as taking a snapshot on the atom event. |
| 154 | */ |
| 155 | void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values, |
| 156 | std::vector<FieldValue>* output); |
| 157 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 158 | void getDimensionForCondition(const std::vector<FieldValue>& eventValues, |
| 159 | const Metric2Condition& links, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 160 | HashableDimensionKey* conditionDimension); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 161 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Get dimension values using metric's "what" fields and fill statePrimaryKey's |
| 164 | * mField information using "state" fields. |
| 165 | */ |
| 166 | void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link, |
| 167 | HashableDimensionKey* statePrimaryKey); |
| 168 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 169 | } // namespace statsd |
| 170 | } // namespace os |
| 171 | } // namespace android |
| 172 | |
| 173 | namespace std { |
| 174 | |
| 175 | using android::os::statsd::HashableDimensionKey; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 176 | using android::os::statsd::MetricDimensionKey; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 177 | |
| 178 | template <> |
| 179 | struct hash<HashableDimensionKey> { |
| 180 | std::size_t operator()(const HashableDimensionKey& key) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 181 | return hashDimension(key); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 182 | } |
| 183 | }; |
| 184 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 185 | template <> |
| 186 | struct hash<MetricDimensionKey> { |
| 187 | std::size_t operator()(const MetricDimensionKey& key) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 188 | android::hash_t hash = hashDimension(key.getDimensionKeyInWhat()); |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 189 | hash = android::JenkinsHashMix(hash, hashDimension(key.getStateValuesKey())); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 190 | return android::JenkinsHashWhiten(hash); |
| 191 | } |
| 192 | }; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 193 | } // namespace std |