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 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 37 | class HashableDimensionKey { |
| 38 | public: |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 39 | explicit HashableDimensionKey(const std::vector<FieldValue>& values) { |
| 40 | mValues = values; |
| 41 | } |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 42 | |
| 43 | HashableDimensionKey(){}; |
| 44 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 45 | HashableDimensionKey(const HashableDimensionKey& that) : mValues(that.getValues()){}; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 46 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 47 | inline void addValue(const FieldValue& value) { |
| 48 | mValues.push_back(value); |
| 49 | } |
| 50 | |
| 51 | inline const std::vector<FieldValue>& getValues() const { |
| 52 | return mValues; |
| 53 | } |
| 54 | |
| 55 | inline std::vector<FieldValue>* mutableValues() { |
| 56 | return &mValues; |
| 57 | } |
| 58 | |
| 59 | inline FieldValue* mutableValue(size_t i) { |
| 60 | if (i >= 0 && i < mValues.size()) { |
| 61 | return &(mValues[i]); |
| 62 | } |
| 63 | return nullptr; |
| 64 | } |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 65 | |
| 66 | std::string toString() const; |
| 67 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 68 | inline const char* c_str() const { |
| 69 | return toString().c_str(); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 72 | bool operator==(const HashableDimensionKey& that) const; |
| 73 | |
| 74 | bool operator<(const HashableDimensionKey& that) const; |
| 75 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 76 | bool contains(const HashableDimensionKey& that) const; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 77 | |
| 78 | private: |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 79 | std::vector<FieldValue> mValues; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 82 | class MetricDimensionKey { |
| 83 | public: |
| 84 | explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat, |
| 85 | const HashableDimensionKey& dimensionKeyInCondition) |
| 86 | : mDimensionKeyInWhat(dimensionKeyInWhat), |
| 87 | mDimensionKeyInCondition(dimensionKeyInCondition) {}; |
| 88 | |
| 89 | MetricDimensionKey(){}; |
| 90 | |
| 91 | MetricDimensionKey(const MetricDimensionKey& that) |
| 92 | : mDimensionKeyInWhat(that.getDimensionKeyInWhat()), |
| 93 | mDimensionKeyInCondition(that.getDimensionKeyInCondition()) {}; |
| 94 | |
| 95 | MetricDimensionKey& operator=(const MetricDimensionKey& from) = default; |
| 96 | |
| 97 | std::string toString() const; |
| 98 | |
| 99 | inline const HashableDimensionKey& getDimensionKeyInWhat() const { |
| 100 | return mDimensionKeyInWhat; |
| 101 | } |
| 102 | |
| 103 | inline const HashableDimensionKey& getDimensionKeyInCondition() const { |
| 104 | return mDimensionKeyInCondition; |
| 105 | } |
| 106 | |
| 107 | bool hasDimensionKeyInCondition() const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 108 | return mDimensionKeyInCondition.getValues().size() > 0; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool operator==(const MetricDimensionKey& that) const; |
| 112 | |
| 113 | bool operator<(const MetricDimensionKey& that) const; |
| 114 | |
| 115 | inline const char* c_str() const { |
| 116 | return toString().c_str(); |
| 117 | } |
| 118 | private: |
| 119 | HashableDimensionKey mDimensionKeyInWhat; |
| 120 | HashableDimensionKey mDimensionKeyInCondition; |
| 121 | }; |
| 122 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 123 | android::hash_t hashDimension(const HashableDimensionKey& key); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 124 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Creating HashableDimensionKeys from FieldValues using matcher. |
| 127 | * |
| 128 | * This function may make modifications to the Field if the matcher has Position=LAST or ANY in |
| 129 | * it. This is because: for example, when we create dimension from last uid in attribution chain, |
| 130 | * In one event, uid 1000 is at position 5 and it's the last |
| 131 | * In another event, uid 1000 is at position 6, and it's the last |
| 132 | * these 2 events should be mapped to the same dimension. So we will remove the original position |
| 133 | * from the dimension key for the uid field (by applying 0x80 bit mask). |
| 134 | */ |
| 135 | bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values, |
| 136 | std::vector<HashableDimensionKey>* output); |
| 137 | |
| 138 | /** |
| 139 | * Filter the values from FieldValues using the matchers. |
| 140 | * |
| 141 | * In contrast to the above function, this function will not do any modification to the original |
| 142 | * data. Considering it as taking a snapshot on the atom event. |
| 143 | */ |
| 144 | void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values, |
| 145 | std::vector<FieldValue>* output); |
| 146 | |
| 147 | void getDimensionForCondition(const LogEvent& event, Metric2Condition links, |
| 148 | std::vector<HashableDimensionKey>* conditionDimension); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 149 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 150 | } // namespace statsd |
| 151 | } // namespace os |
| 152 | } // namespace android |
| 153 | |
| 154 | namespace std { |
| 155 | |
| 156 | using android::os::statsd::HashableDimensionKey; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 157 | using android::os::statsd::MetricDimensionKey; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 158 | |
| 159 | template <> |
| 160 | struct hash<HashableDimensionKey> { |
| 161 | std::size_t operator()(const HashableDimensionKey& key) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 162 | return hashDimension(key); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 163 | } |
| 164 | }; |
| 165 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 166 | template <> |
| 167 | struct hash<MetricDimensionKey> { |
| 168 | std::size_t operator()(const MetricDimensionKey& key) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 169 | android::hash_t hash = hashDimension(key.getDimensionKeyInWhat()); |
| 170 | hash = android::JenkinsHashMix(hash, hashDimension(key.getDimensionKeyInCondition())); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 171 | return android::JenkinsHashWhiten(hash); |
| 172 | } |
| 173 | }; |
| 174 | } // namespace std |