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