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