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; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 28 | using android::base::StringPrintf; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 29 | |
Ruchir Rastogi | 56da4c3 | 2020-02-03 18:53:24 -0800 | [diff] [blame] | 30 | // These constants must be kept in sync with those in StatsDimensionsValue.java |
| 31 | const static int STATS_DIMENSIONS_VALUE_STRING_TYPE = 2; |
| 32 | const static int STATS_DIMENSIONS_VALUE_INT_TYPE = 3; |
| 33 | const static int STATS_DIMENSIONS_VALUE_LONG_TYPE = 4; |
| 34 | // const static int STATS_DIMENSIONS_VALUE_BOOL_TYPE = 5; (commented out because |
| 35 | // unused -- statsd does not correctly support bool types) |
| 36 | const static int STATS_DIMENSIONS_VALUE_FLOAT_TYPE = 6; |
| 37 | const static int STATS_DIMENSIONS_VALUE_TUPLE_TYPE = 7; |
| 38 | |
| 39 | /** |
| 40 | * Recursive helper function that populates a parent StatsDimensionsValueParcel |
| 41 | * with children StatsDimensionsValueParcels. |
| 42 | * |
| 43 | * \param dims vector of FieldValues stored by HashableDimensionKey |
| 44 | * \param index positions in dims vector to start reading children from |
| 45 | * \param depth level of parent parcel in the full StatsDimensionsValueParcel |
| 46 | * tree |
| 47 | */ |
| 48 | static void populateStatsDimensionsValueParcelChildren(StatsDimensionsValueParcel &parentParcel, |
| 49 | const vector<FieldValue>& dims, size_t& index, |
| 50 | int depth, int prefix) { |
| 51 | while (index < dims.size()) { |
| 52 | const FieldValue& dim = dims[index]; |
| 53 | int fieldDepth = dim.mField.getDepth(); |
| 54 | int fieldPrefix = dim.mField.getPrefix(depth); |
| 55 | StatsDimensionsValueParcel childParcel; |
| 56 | childParcel.field = dim.mField.getPosAtDepth(depth); |
| 57 | if (depth > 2) { |
| 58 | ALOGE("Depth > 2 not supported by StatsDimensionsValueParcel."); |
| 59 | return; |
| 60 | } |
| 61 | if (depth == fieldDepth && prefix == fieldPrefix) { |
| 62 | switch (dim.mValue.getType()) { |
| 63 | case INT: |
| 64 | childParcel.valueType = STATS_DIMENSIONS_VALUE_INT_TYPE; |
| 65 | childParcel.intValue = dim.mValue.int_value; |
| 66 | break; |
| 67 | case LONG: |
| 68 | childParcel.valueType = STATS_DIMENSIONS_VALUE_LONG_TYPE; |
| 69 | childParcel.longValue = dim.mValue.long_value; |
| 70 | break; |
| 71 | case FLOAT: |
| 72 | childParcel.valueType = STATS_DIMENSIONS_VALUE_FLOAT_TYPE; |
| 73 | childParcel.floatValue = dim.mValue.float_value; |
| 74 | break; |
| 75 | case STRING: |
| 76 | childParcel.valueType = STATS_DIMENSIONS_VALUE_STRING_TYPE; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 77 | childParcel.stringValue = dim.mValue.str_value; |
Ruchir Rastogi | 56da4c3 | 2020-02-03 18:53:24 -0800 | [diff] [blame] | 78 | break; |
| 79 | default: |
| 80 | ALOGE("Encountered FieldValue with unsupported value type."); |
| 81 | break; |
| 82 | } |
| 83 | index++; |
| 84 | parentParcel.tupleValue.push_back(childParcel); |
| 85 | } else if (fieldDepth > depth && fieldPrefix == prefix) { |
| 86 | childParcel.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE; |
| 87 | populateStatsDimensionsValueParcelChildren(childParcel, dims, index, depth + 1, |
| 88 | dim.mField.getPrefix(depth + 1)); |
| 89 | parentParcel.tupleValue.push_back(childParcel); |
| 90 | } else { |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | StatsDimensionsValueParcel HashableDimensionKey::toStatsDimensionsValueParcel() const { |
| 97 | StatsDimensionsValueParcel parcel; |
| 98 | if (mValues.size() == 0) { |
| 99 | return parcel; |
| 100 | } |
| 101 | |
| 102 | parcel.field = mValues[0].mField.getTag(); |
| 103 | parcel.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE; |
| 104 | |
| 105 | size_t index = 0; |
| 106 | populateStatsDimensionsValueParcelChildren(parcel, mValues, index, /*depth=*/0, /*prefix=*/0); |
| 107 | return parcel; |
| 108 | } |
| 109 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 110 | android::hash_t hashDimension(const HashableDimensionKey& value) { |
| 111 | android::hash_t hash = 0; |
| 112 | for (const auto& fieldValue : value.getValues()) { |
| 113 | hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField())); |
| 114 | hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag())); |
| 115 | hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType())); |
| 116 | switch (fieldValue.mValue.getType()) { |
| 117 | case INT: |
| 118 | hash = android::JenkinsHashMix(hash, |
| 119 | android::hash_type(fieldValue.mValue.int_value)); |
| 120 | break; |
| 121 | case LONG: |
| 122 | hash = android::JenkinsHashMix(hash, |
| 123 | android::hash_type(fieldValue.mValue.long_value)); |
| 124 | break; |
| 125 | case STRING: |
| 126 | hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()( |
| 127 | fieldValue.mValue.str_value))); |
| 128 | break; |
| 129 | case FLOAT: { |
Yangster-mac | 16b7ff7 | 2018-02-23 11:11:36 -0800 | [diff] [blame] | 130 | hash = android::JenkinsHashMix(hash, |
| 131 | android::hash_type(fieldValue.mValue.float_value)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 132 | break; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 133 | } |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 134 | default: |
| 135 | break; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 136 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 137 | } |
| 138 | return JenkinsHashWhiten(hash); |
| 139 | } |
| 140 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 141 | bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values, |
| 142 | FieldValue* output) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 143 | for (const auto& value : values) { |
| 144 | if (value.mField.matches(matcherField)) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 145 | (*output) = value; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 152 | bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values, |
| 153 | HashableDimensionKey* output) { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 154 | size_t num_matches = 0; |
| 155 | for (const auto& value : values) { |
| 156 | for (size_t i = 0; i < matcherFields.size(); ++i) { |
| 157 | const auto& matcher = matcherFields[i]; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 158 | if (value.mField.matches(matcher)) { |
| 159 | output->addValue(value); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 160 | output->mutableValue(num_matches)->mField.setTag(value.mField.getTag()); |
| 161 | output->mutableValue(num_matches)->mField.setField( |
| 162 | value.mField.getField() & matcher.mMask); |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 163 | num_matches++; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 164 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 165 | } |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 166 | } |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 167 | return num_matches > 0; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void filterGaugeValues(const std::vector<Matcher>& matcherFields, |
| 171 | const std::vector<FieldValue>& values, std::vector<FieldValue>* output) { |
| 172 | for (const auto& field : matcherFields) { |
| 173 | for (const auto& value : values) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 174 | if (value.mField.matches(field)) { |
| 175 | output->push_back(value); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 181 | void getDimensionForCondition(const std::vector<FieldValue>& eventValues, |
| 182 | const Metric2Condition& links, |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 183 | HashableDimensionKey* conditionDimension) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 184 | // Get the dimension first by using dimension from what. |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 185 | filterValues(links.metricFields, eventValues, conditionDimension); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 186 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 187 | size_t count = conditionDimension->getValues().size(); |
| 188 | if (count != links.conditionFields.size()) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 189 | return; |
| 190 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 191 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 192 | for (size_t i = 0; i < count; i++) { |
| 193 | conditionDimension->mutableValue(i)->mField.setField( |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 194 | links.conditionFields[i].mMatcher.getField()); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 195 | conditionDimension->mutableValue(i)->mField.setTag( |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 196 | links.conditionFields[i].mMatcher.getTag()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link, |
| 201 | HashableDimensionKey* statePrimaryKey) { |
| 202 | // First, get the dimension from the event using the "what" fields from the |
| 203 | // MetricStateLinks. |
| 204 | filterValues(link.metricFields, eventValues, statePrimaryKey); |
| 205 | |
| 206 | // Then check that the statePrimaryKey size equals the number of state fields |
| 207 | size_t count = statePrimaryKey->getValues().size(); |
| 208 | if (count != link.stateFields.size()) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // For each dimension Value in the statePrimaryKey, set the field and tag |
| 213 | // using the state atom fields from MetricStateLinks. |
| 214 | for (size_t i = 0; i < count; i++) { |
| 215 | statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField()); |
| 216 | statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) { |
| 221 | if (s1.size() != s2.size()) { |
| 222 | return s1.size() < s2.size(); |
| 223 | } |
| 224 | |
| 225 | size_t count = s1.size(); |
| 226 | for (size_t i = 0; i < count; i++) { |
| 227 | if (s1[i] != s2[i]) { |
| 228 | return s1[i] < s2[i]; |
| 229 | } |
| 230 | } |
| 231 | return false; |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 232 | } |
| 233 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 234 | bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const { |
| 235 | return !((*this) == that); |
| 236 | } |
| 237 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 238 | bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 239 | if (mValues.size() != that.getValues().size()) { |
| 240 | return false; |
| 241 | } |
| 242 | size_t count = mValues.size(); |
| 243 | for (size_t i = 0; i < count; i++) { |
| 244 | if (mValues[i] != (that.getValues())[i]) { |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | return true; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 252 | return LessThan(getValues(), that.getValues()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 255 | bool HashableDimensionKey::contains(const HashableDimensionKey& that) const { |
| 256 | if (mValues.size() < that.getValues().size()) { |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | if (mValues.size() == that.getValues().size()) { |
| 261 | return (*this) == that; |
| 262 | } |
| 263 | |
| 264 | for (const auto& value : that.getValues()) { |
| 265 | bool found = false; |
| 266 | for (const auto& myValue : mValues) { |
| 267 | if (value.mField == myValue.mField && value.mValue == myValue.mValue) { |
| 268 | found = true; |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | if (!found) { |
| 273 | return false; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | string HashableDimensionKey::toString() const { |
| 281 | std::string output; |
| 282 | for (const auto& value : mValues) { |
| 283 | output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(), |
| 284 | value.mValue.toString().c_str()); |
| 285 | } |
| 286 | return output; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const { |
| 290 | return mDimensionKeyInWhat == that.getDimensionKeyInWhat() && |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 291 | mStateValuesKey == that.getStateValuesKey(); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 294 | string MetricDimensionKey::toString() const { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 295 | return mDimensionKeyInWhat.toString() + mStateValuesKey.toString(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 298 | bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 299 | if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) { |
| 300 | return true; |
| 301 | } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) { |
| 302 | return false; |
| 303 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 304 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 305 | return mStateValuesKey < that.getStateValuesKey(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 306 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 307 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 308 | } // namespace statsd |
| 309 | } // namespace os |
Yao Chen | 5bfffb5 | 2018-06-21 16:58:51 -0700 | [diff] [blame] | 310 | } // namespace android |