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