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