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