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