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: { |
| 49 | float floatVal = fieldValue.mValue.float_value; |
| 50 | hash = android::JenkinsHashMixBytes(hash, (uint8_t*)&floatVal, sizeof(float)); |
| 51 | break; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 52 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 53 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 54 | } |
| 55 | return JenkinsHashWhiten(hash); |
| 56 | } |
| 57 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 58 | // Filter fields using the matchers and output the results as a HashableDimensionKey. |
| 59 | // Note: HashableDimensionKey is just a wrapper for vector<FieldValue> |
| 60 | bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values, |
| 61 | vector<HashableDimensionKey>* output) { |
| 62 | output->push_back(HashableDimensionKey()); |
| 63 | // Top level is only tag id. Now take the real child matchers |
| 64 | int prevAnyMatcherPrefix = 0; |
| 65 | size_t prevPrevFanout = 0; |
| 66 | size_t prevFanout = 0; |
| 67 | // For each matcher get matched results. |
| 68 | for (const auto& matcher : matcherFields) { |
| 69 | vector<FieldValue> matchedResults; |
| 70 | for (const auto& value : values) { |
| 71 | // TODO: potential optimization here to break early because all fields are naturally |
| 72 | // sorted. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 73 | if (value.mField.matches(matcher)) { |
| 74 | matchedResults.push_back(FieldValue( |
| 75 | Field(value.mField.getTag(), (value.mField.getField() & matcher.mMask)), |
| 76 | value.mValue)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 77 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 78 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 79 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 80 | if (matchedResults.size() == 0) { |
| 81 | VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(), |
| 82 | matcher.mMatcher.getField()); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (matchedResults.size() == 1) { |
| 87 | for (auto& dimension : *output) { |
| 88 | dimension.addValue(matchedResults[0]); |
| 89 | } |
| 90 | prevAnyMatcherPrefix = 0; |
| 91 | prevFanout = 0; |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | // All the complexity below is because we support ANY in dimension. |
| 96 | bool createFanout = true; |
| 97 | // createFanout is true when the matcher doesn't need to follow the prev matcher's |
| 98 | // order. |
| 99 | // e.g., get (uid, tag) from any position in attribution. because we have translated |
| 100 | // it as 2 matchers, they need to follow the same ordering, we can't create a cross |
| 101 | // product of all uid and tags. |
| 102 | // However, if the 2 matchers have different prefix, they will create a cross product |
| 103 | // e.g., [any uid] [any some other repeated field], we will create a cross product for them |
| 104 | if (prevAnyMatcherPrefix != 0) { |
| 105 | int anyMatcherPrefix = 0; |
| 106 | bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix); |
| 107 | if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) { |
| 108 | createFanout = false; |
| 109 | } else { |
| 110 | prevAnyMatcherPrefix = anyMatcherPrefix; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Each matcher should match exact one field, unless position is ANY |
| 115 | // When x number of fields matches a matcher, the returned dimension |
| 116 | // size is multiplied by x. |
| 117 | int oldSize; |
| 118 | if (createFanout) { |
| 119 | // First create fanout (fanout size is matchedResults.Size which could be one, |
| 120 | // which means we do nothing here) |
| 121 | oldSize = output->size(); |
| 122 | for (size_t i = 1; i < matchedResults.size(); i++) { |
| 123 | output->insert(output->end(), output->begin(), output->begin() + oldSize); |
| 124 | } |
| 125 | prevPrevFanout = oldSize; |
| 126 | prevFanout = matchedResults.size(); |
| 127 | } else { |
| 128 | // If we should not create fanout, e.g., uid tag from same position should be remain |
| 129 | // together. |
| 130 | oldSize = prevPrevFanout; |
| 131 | if (prevFanout != matchedResults.size()) { |
| 132 | // sanity check. |
| 133 | ALOGE("2 Any matcher result in different output"); |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 134 | return false; |
| 135 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 136 | } |
| 137 | // now add the matched field value to output |
| 138 | for (size_t i = 0; i < matchedResults.size(); i++) { |
| 139 | for (int j = 0; j < oldSize; j++) { |
| 140 | (*output)[i * oldSize + j].addValue(matchedResults[i]); |
| 141 | } |
| 142 | } |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 143 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 144 | |
| 145 | return output->size() > 0 && (*output)[0].getValues().size() > 0; |
| 146 | } |
| 147 | |
| 148 | void filterGaugeValues(const std::vector<Matcher>& matcherFields, |
| 149 | const std::vector<FieldValue>& values, std::vector<FieldValue>* output) { |
| 150 | for (const auto& field : matcherFields) { |
| 151 | for (const auto& value : values) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 152 | if (value.mField.matches(field)) { |
| 153 | output->push_back(value); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void getDimensionForCondition(const LogEvent& event, Metric2Condition links, |
| 160 | vector<HashableDimensionKey>* conditionDimension) { |
| 161 | // Get the dimension first by using dimension from what. |
| 162 | filterValues(links.metricFields, event.getValues(), conditionDimension); |
| 163 | |
| 164 | // Then replace the field with the dimension from condition. |
| 165 | for (auto& dim : *conditionDimension) { |
| 166 | size_t count = dim.getValues().size(); |
| 167 | if (count != links.conditionFields.size()) { |
| 168 | // ALOGE("WTF condition link is bad"); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | for (size_t i = 0; i < count; i++) { |
| 173 | dim.mutableValue(i)->mField.setField(links.conditionFields[i].mMatcher.getField()); |
| 174 | dim.mutableValue(i)->mField.setTag(links.conditionFields[i].mMatcher.getTag()); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) { |
| 180 | if (s1.size() != s2.size()) { |
| 181 | return s1.size() < s2.size(); |
| 182 | } |
| 183 | |
| 184 | size_t count = s1.size(); |
| 185 | for (size_t i = 0; i < count; i++) { |
| 186 | if (s1[i] != s2[i]) { |
| 187 | return s1[i] < s2[i]; |
| 188 | } |
| 189 | } |
| 190 | return false; |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 193 | bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 194 | if (mValues.size() != that.getValues().size()) { |
| 195 | return false; |
| 196 | } |
| 197 | size_t count = mValues.size(); |
| 198 | for (size_t i = 0; i < count; i++) { |
| 199 | if (mValues[i] != (that.getValues())[i]) { |
| 200 | return false; |
| 201 | } |
| 202 | } |
| 203 | return true; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 207 | return LessThan(getValues(), that.getValues()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 208 | }; |
| 209 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 210 | bool HashableDimensionKey::contains(const HashableDimensionKey& that) const { |
| 211 | if (mValues.size() < that.getValues().size()) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if (mValues.size() == that.getValues().size()) { |
| 216 | return (*this) == that; |
| 217 | } |
| 218 | |
| 219 | for (const auto& value : that.getValues()) { |
| 220 | bool found = false; |
| 221 | for (const auto& myValue : mValues) { |
| 222 | if (value.mField == myValue.mField && value.mValue == myValue.mValue) { |
| 223 | found = true; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if (!found) { |
| 228 | return false; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | string HashableDimensionKey::toString() const { |
| 236 | std::string output; |
| 237 | for (const auto& value : mValues) { |
| 238 | output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(), |
| 239 | value.mValue.toString().c_str()); |
| 240 | } |
| 241 | return output; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const { |
| 245 | return mDimensionKeyInWhat == that.getDimensionKeyInWhat() && |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 246 | mDimensionKeyInCondition == that.getDimensionKeyInCondition(); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 247 | }; |
| 248 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 249 | string MetricDimensionKey::toString() const { |
| 250 | return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString(); |
| 251 | } |
| 252 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 253 | bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 254 | if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) { |
| 255 | return true; |
| 256 | } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) { |
| 257 | return false; |
| 258 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 259 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 260 | return mDimensionKeyInCondition < that.getDimensionKeyInCondition(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 261 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 262 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 263 | } // namespace statsd |
| 264 | } // namespace os |
| 265 | } // namespace android |