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 | */ |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 16 | #define DEBUG false // STOPSHIP if true |
| 17 | #include "Log.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 18 | |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 19 | #include <mutex> |
| 20 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 21 | #include "HashableDimensionKey.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 22 | #include "FieldValue.h" |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 27 | |
| 28 | using std::string; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 29 | using std::vector; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 30 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 31 | android::hash_t hashDimension(const HashableDimensionKey& value) { |
| 32 | android::hash_t hash = 0; |
| 33 | for (const auto& fieldValue : value.getValues()) { |
| 34 | hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField())); |
| 35 | hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag())); |
| 36 | hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType())); |
| 37 | switch (fieldValue.mValue.getType()) { |
| 38 | case INT: |
| 39 | hash = android::JenkinsHashMix(hash, |
| 40 | android::hash_type(fieldValue.mValue.int_value)); |
| 41 | break; |
| 42 | case LONG: |
| 43 | hash = android::JenkinsHashMix(hash, |
| 44 | android::hash_type(fieldValue.mValue.long_value)); |
| 45 | break; |
| 46 | case STRING: |
| 47 | hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()( |
| 48 | fieldValue.mValue.str_value))); |
| 49 | break; |
| 50 | case FLOAT: { |
Yangster-mac | 16b7ff7 | 2018-02-23 11:11:36 -0800 | [diff] [blame] | 51 | hash = android::JenkinsHashMix(hash, |
| 52 | android::hash_type(fieldValue.mValue.float_value)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 53 | break; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 54 | } |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 55 | default: |
| 56 | break; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 57 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 58 | } |
| 59 | return JenkinsHashWhiten(hash); |
| 60 | } |
| 61 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 62 | bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values, |
| 63 | FieldValue* output) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 64 | for (const auto& value : values) { |
| 65 | if (value.mField.matches(matcherField)) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 66 | (*output) = value; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 73 | bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values, |
| 74 | HashableDimensionKey* output) { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 75 | size_t num_matches = 0; |
| 76 | for (const auto& value : values) { |
| 77 | for (size_t i = 0; i < matcherFields.size(); ++i) { |
| 78 | const auto& matcher = matcherFields[i]; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 79 | if (value.mField.matches(matcher)) { |
| 80 | output->addValue(value); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 81 | output->mutableValue(num_matches)->mField.setTag(value.mField.getTag()); |
| 82 | output->mutableValue(num_matches)->mField.setField( |
| 83 | value.mField.getField() & matcher.mMask); |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 84 | num_matches++; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 85 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 86 | } |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 87 | } |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 88 | return num_matches > 0; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void filterGaugeValues(const std::vector<Matcher>& matcherFields, |
| 92 | const std::vector<FieldValue>& values, std::vector<FieldValue>* output) { |
| 93 | for (const auto& field : matcherFields) { |
| 94 | for (const auto& value : values) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 95 | if (value.mField.matches(field)) { |
| 96 | output->push_back(value); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 102 | void getDimensionForCondition(const std::vector<FieldValue>& eventValues, |
| 103 | const Metric2Condition& links, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 104 | HashableDimensionKey* conditionDimension) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 105 | // Get the dimension first by using dimension from what. |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 106 | filterValues(links.metricFields, eventValues, conditionDimension); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 107 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 108 | size_t count = conditionDimension->getValues().size(); |
| 109 | if (count != links.conditionFields.size()) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 112 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 113 | for (size_t i = 0; i < count; i++) { |
| 114 | conditionDimension->mutableValue(i)->mField.setField( |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 115 | links.conditionFields[i].mMatcher.getField()); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 116 | conditionDimension->mutableValue(i)->mField.setTag( |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 117 | links.conditionFields[i].mMatcher.getTag()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link, |
| 122 | HashableDimensionKey* statePrimaryKey) { |
| 123 | // First, get the dimension from the event using the "what" fields from the |
| 124 | // MetricStateLinks. |
| 125 | filterValues(link.metricFields, eventValues, statePrimaryKey); |
| 126 | |
| 127 | // Then check that the statePrimaryKey size equals the number of state fields |
| 128 | size_t count = statePrimaryKey->getValues().size(); |
| 129 | if (count != link.stateFields.size()) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // For each dimension Value in the statePrimaryKey, set the field and tag |
| 134 | // using the state atom fields from MetricStateLinks. |
| 135 | for (size_t i = 0; i < count; i++) { |
| 136 | statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField()); |
| 137 | statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) { |
| 142 | if (s1.size() != s2.size()) { |
| 143 | return s1.size() < s2.size(); |
| 144 | } |
| 145 | |
| 146 | size_t count = s1.size(); |
| 147 | for (size_t i = 0; i < count; i++) { |
| 148 | if (s1[i] != s2[i]) { |
| 149 | return s1[i] < s2[i]; |
| 150 | } |
| 151 | } |
| 152 | return false; |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 153 | } |
| 154 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame^] | 155 | bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const { |
| 156 | return !((*this) == that); |
| 157 | } |
| 158 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 159 | bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 160 | if (mValues.size() != that.getValues().size()) { |
| 161 | return false; |
| 162 | } |
| 163 | size_t count = mValues.size(); |
| 164 | for (size_t i = 0; i < count; i++) { |
| 165 | if (mValues[i] != (that.getValues())[i]) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 173 | return LessThan(getValues(), that.getValues()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 176 | bool HashableDimensionKey::contains(const HashableDimensionKey& that) const { |
| 177 | if (mValues.size() < that.getValues().size()) { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | if (mValues.size() == that.getValues().size()) { |
| 182 | return (*this) == that; |
| 183 | } |
| 184 | |
| 185 | for (const auto& value : that.getValues()) { |
| 186 | bool found = false; |
| 187 | for (const auto& myValue : mValues) { |
| 188 | if (value.mField == myValue.mField && value.mValue == myValue.mValue) { |
| 189 | found = true; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | if (!found) { |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | string HashableDimensionKey::toString() const { |
| 202 | std::string output; |
| 203 | for (const auto& value : mValues) { |
| 204 | output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(), |
| 205 | value.mValue.toString().c_str()); |
| 206 | } |
| 207 | return output; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const { |
| 211 | return mDimensionKeyInWhat == that.getDimensionKeyInWhat() && |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 212 | mStateValuesKey == that.getStateValuesKey(); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 215 | string MetricDimensionKey::toString() const { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 216 | return mDimensionKeyInWhat.toString() + mStateValuesKey.toString(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 219 | bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 220 | if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) { |
| 221 | return true; |
| 222 | } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) { |
| 223 | return false; |
| 224 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 225 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 226 | return mStateValuesKey < that.getStateValuesKey(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 227 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 228 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 229 | } // namespace statsd |
| 230 | } // namespace os |
Yao Chen | 5bfffb5 | 2018-06-21 16:58:51 -0700 | [diff] [blame] | 231 | } // namespace android |