Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 19 | |
| 20 | namespace android { |
| 21 | namespace os { |
| 22 | namespace statsd { |
| 23 | |
| 24 | class HashableDimensionKey; |
| 25 | struct Matcher; |
| 26 | struct Field; |
| 27 | struct FieldValue; |
| 28 | |
| 29 | const int32_t kAttributionField = 1; |
| 30 | const int32_t kMaxLogDepth = 2; |
| 31 | const int32_t kLastBitMask = 0x80; |
| 32 | const int32_t kClearLastBitDeco = 0x7f; |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 33 | const int32_t kClearAllPositionMatcherMask = 0xffff00ff; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 34 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 35 | enum Type { UNKNOWN, INT, LONG, FLOAT, DOUBLE, STRING, STORAGE }; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 36 | |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 37 | int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 38 | |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 39 | int32_t encodeMatcherMask(int32_t mask[], int32_t depth); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 40 | |
| 41 | // Get the encoded field for a leaf with a [field] number at depth 0; |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 42 | inline int32_t getSimpleField(size_t field) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 43 | return ((int32_t)field << 8 * 2); |
| 44 | } |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 45 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Field is a wrapper class for 2 integers that represents the field of a log element in its Atom |
| 48 | * proto. |
| 49 | * [mTag]: the atom id. |
| 50 | * [mField]: encoded path from the root (atom) to leaf. |
| 51 | * |
| 52 | * For example: |
| 53 | * WakeLockStateChanged { |
| 54 | * repeated AttributionNode = 1; |
| 55 | * int state = 2; |
| 56 | * string tag = 3; |
| 57 | * } |
| 58 | * Read from logd, the items are structured as below: |
| 59 | * [[[1000, "tag"], [2000, "tag2"],], 2,"hello"] |
| 60 | * |
| 61 | * When we read through the list, we will encode each field in a 32bit integer. |
| 62 | * 8bit segments |--------|--------|--------|--------| |
| 63 | * Depth field0 [L]field1 [L]field1 |
| 64 | * |
| 65 | * The first 8 bits are the depth of the field. for example, the uid 1000 has depth 2. |
| 66 | * The following 3 8-bit are for the item's position at each level. |
| 67 | * The first bit of each 8bits field is reserved to mark if the item is the last item at that level |
| 68 | * this is to make matching easier later. |
| 69 | * |
| 70 | * The above wakelock event is translated into FieldValue pairs. |
| 71 | * 0x02010101->1000 |
| 72 | * 0x02010182->tag |
| 73 | * 0x02018201->2000 |
| 74 | * 0x02018282->tag2 |
| 75 | * 0x00020000->2 |
| 76 | * 0x00030000->"hello" |
| 77 | * |
| 78 | * This encoding is the building block for the later operations. |
| 79 | * Please see the definition for Matcher below to see how the matching is done. |
| 80 | */ |
| 81 | struct Field { |
| 82 | private: |
| 83 | int32_t mTag; |
| 84 | int32_t mField; |
| 85 | |
| 86 | public: |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 87 | Field() {} |
| 88 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 89 | Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) { |
| 90 | mField = getEncodedField(pos, depth, true); |
| 91 | } |
| 92 | |
| 93 | Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) { |
| 94 | } |
| 95 | |
| 96 | Field(int32_t tag, int32_t field) : mTag(tag), mField(field){}; |
| 97 | |
| 98 | inline void setField(int32_t field) { |
| 99 | mField = field; |
| 100 | } |
| 101 | |
| 102 | inline void setTag(int32_t tag) { |
| 103 | mTag = tag; |
| 104 | } |
| 105 | |
| 106 | inline void decorateLastPos(int32_t depth) { |
| 107 | int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth); |
| 108 | mField |= mask; |
| 109 | } |
| 110 | |
| 111 | inline int32_t getTag() const { |
| 112 | return mTag; |
| 113 | } |
| 114 | |
| 115 | inline int32_t getDepth() const { |
| 116 | return (mField >> 24); |
| 117 | } |
| 118 | |
| 119 | inline int32_t getPath(int32_t depth) const { |
| 120 | if (depth > 2 || depth < 0) return 0; |
| 121 | |
| 122 | int32_t field = (mField & 0x00ffffff); |
| 123 | int32_t mask = 0xffffffff; |
| 124 | return (field & (mask << 8 * (kMaxLogDepth - depth))); |
| 125 | } |
| 126 | |
| 127 | inline int32_t getPrefix(int32_t depth) const { |
| 128 | if (depth == 0) return 0; |
| 129 | return getPath(depth - 1); |
| 130 | } |
| 131 | |
| 132 | inline int32_t getField() const { |
| 133 | return mField; |
| 134 | } |
| 135 | |
| 136 | inline int32_t getRawPosAtDepth(int32_t depth) const { |
| 137 | int32_t field = (mField & 0x00ffffff); |
| 138 | int32_t shift = 8 * (kMaxLogDepth - depth); |
| 139 | int32_t mask = 0xff << shift; |
| 140 | |
| 141 | return (field & mask) >> shift; |
| 142 | } |
| 143 | |
| 144 | inline int32_t getPosAtDepth(int32_t depth) const { |
| 145 | return getRawPosAtDepth(depth) & kClearLastBitDeco; |
| 146 | } |
| 147 | |
| 148 | // Check if the first bit of the 8-bit segment for depth is 1 |
| 149 | inline bool isLastPos(int32_t depth) const { |
| 150 | int32_t field = (mField & 0x00ffffff); |
| 151 | int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth); |
| 152 | return (field & mask) != 0; |
| 153 | } |
| 154 | |
| 155 | // if the 8-bit segment is all 0's |
| 156 | inline bool isAnyPosMatcher(int32_t depth) const { |
| 157 | return getDepth() >= depth && getRawPosAtDepth(depth) == 0; |
| 158 | } |
| 159 | // if the 8bit is 0x80 (1000 0000) |
| 160 | inline bool isLastPosMatcher(int32_t depth) const { |
| 161 | return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask; |
| 162 | } |
| 163 | |
| 164 | inline bool operator==(const Field& that) const { |
| 165 | return mTag == that.getTag() && mField == that.getField(); |
| 166 | }; |
| 167 | |
| 168 | inline bool operator!=(const Field& that) const { |
| 169 | return mTag != that.getTag() || mField != that.getField(); |
| 170 | }; |
| 171 | |
| 172 | bool operator<(const Field& that) const { |
| 173 | if (mTag != that.getTag()) { |
| 174 | return mTag < that.getTag(); |
| 175 | } |
| 176 | |
| 177 | if (mField != that.getField()) { |
| 178 | return mField < that.getField(); |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | bool matches(const Matcher& that) const; |
| 184 | }; |
| 185 | |
| 186 | /** |
| 187 | * Matcher represents a leaf matcher in the FieldMatcher in statsd_config. |
| 188 | * |
| 189 | * It contains all information needed to match one or more leaf node. |
| 190 | * All information is encoded in a Field(2 ints) and a bit mask(1 int). |
| 191 | * |
| 192 | * For example, to match the first/any/last uid field in attribution chain in Atom 10, |
| 193 | * we have the following FieldMatcher in statsd_config |
| 194 | * FieldMatcher { |
| 195 | * field:10 |
| 196 | * FieldMatcher { |
| 197 | * field:1 |
| 198 | * position: any/last/first |
| 199 | * FieldMatcher { |
| 200 | * field:1 |
| 201 | * } |
| 202 | * } |
| 203 | * } |
| 204 | * |
| 205 | * We translate the FieldMatcher into a Field, and mask |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 206 | * First: [Matcher Field] 0x02010101 [Mask]0xff7f7f7f |
| 207 | * Last: [Matcher Field] 0x02018001 [Mask]0xff7f807f |
| 208 | * Any: [Matcher Field] 0x02010001 [Mask]0xff7f007f |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 209 | * All: [Matcher Field] 0x02010001 [Mask]0xff7f7f7f |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 210 | * |
| 211 | * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if |
| 212 | * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are |
| 213 | * equal. Nothing can beat the performance of this matching algorithm. |
| 214 | * |
Yao Chen | 5bfffb5 | 2018-06-21 16:58:51 -0700 | [diff] [blame] | 215 | * TODO(b/110561213): ADD EXAMPLE HERE. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 216 | */ |
| 217 | struct Matcher { |
| 218 | Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){}; |
| 219 | |
| 220 | const Field mMatcher; |
| 221 | const int32_t mMask; |
| 222 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 223 | inline const Field& getMatcher() const { |
| 224 | return mMatcher; |
| 225 | } |
| 226 | |
| 227 | inline int32_t getMask() const { |
| 228 | return mMask; |
| 229 | } |
| 230 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 231 | inline int32_t getRawMaskAtDepth(int32_t depth) const { |
| 232 | int32_t field = (mMask & 0x00ffffff); |
| 233 | int32_t shift = 8 * (kMaxLogDepth - depth); |
| 234 | int32_t mask = 0xff << shift; |
| 235 | |
| 236 | return (field & mask) >> shift; |
| 237 | } |
| 238 | |
| 239 | bool hasAllPositionMatcher() const { |
| 240 | return mMatcher.getDepth() == 2 && getRawMaskAtDepth(1) == 0x7f; |
| 241 | } |
| 242 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 243 | bool hasAnyPositionMatcher(int* prefix) const { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 244 | if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(1) == 0) { |
| 245 | (*prefix) = mMatcher.getPrefix(1); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 250 | |
| 251 | inline bool operator!=(const Matcher& that) const { |
| 252 | return mMatcher != that.getMatcher() || mMask != that.getMask(); |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | inline bool operator==(const Matcher& that) const { |
| 256 | return mMatcher == that.mMatcher && mMask == that.mMask; |
| 257 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 258 | }; |
| 259 | |
Yao Chen | 580ea321 | 2018-02-26 14:21:54 -0800 | [diff] [blame] | 260 | inline Matcher getSimpleMatcher(int32_t tag, size_t field) { |
| 261 | return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000); |
| 262 | } |
| 263 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 264 | /** |
| 265 | * A wrapper for a union type to contain multiple types of values. |
| 266 | * |
| 267 | */ |
| 268 | struct Value { |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 269 | Value() : type(UNKNOWN) {} |
| 270 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 271 | Value(int32_t v) { |
| 272 | int_value = v; |
| 273 | type = INT; |
| 274 | } |
| 275 | |
| 276 | Value(int64_t v) { |
| 277 | long_value = v; |
| 278 | type = LONG; |
| 279 | } |
| 280 | |
| 281 | Value(float v) { |
| 282 | float_value = v; |
| 283 | type = FLOAT; |
| 284 | } |
| 285 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 286 | Value(double v) { |
| 287 | double_value = v; |
| 288 | type = DOUBLE; |
| 289 | } |
| 290 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 291 | Value(const std::string& v) { |
| 292 | str_value = v; |
| 293 | type = STRING; |
| 294 | } |
| 295 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 296 | Value(const std::vector<uint8_t>& v) { |
| 297 | storage_value = v; |
| 298 | type = STORAGE; |
| 299 | } |
| 300 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 301 | void setInt(int32_t v) { |
| 302 | int_value = v; |
| 303 | type = INT; |
| 304 | } |
| 305 | |
| 306 | void setLong(int64_t v) { |
| 307 | long_value = v; |
| 308 | type = LONG; |
| 309 | } |
| 310 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 311 | void setFloat(float v) { |
| 312 | float_value = v; |
| 313 | type = FLOAT; |
| 314 | } |
| 315 | |
| 316 | void setDouble(double v) { |
| 317 | double_value = v; |
| 318 | type = DOUBLE; |
| 319 | } |
| 320 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 321 | union { |
| 322 | int32_t int_value; |
| 323 | int64_t long_value; |
| 324 | float float_value; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 325 | double double_value; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 326 | }; |
| 327 | std::string str_value; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 328 | std::vector<uint8_t> storage_value; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 329 | |
| 330 | Type type; |
| 331 | |
| 332 | std::string toString() const; |
| 333 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 334 | bool isZero() const; |
| 335 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 336 | Type getType() const { |
| 337 | return type; |
| 338 | } |
| 339 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 340 | double getDouble() const; |
| 341 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 342 | Value(const Value& from); |
| 343 | |
| 344 | bool operator==(const Value& that) const; |
| 345 | bool operator!=(const Value& that) const; |
| 346 | |
| 347 | bool operator<(const Value& that) const; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 348 | bool operator>(const Value& that) const; |
| 349 | bool operator>=(const Value& that) const; |
| 350 | Value operator-(const Value& that) const; |
| 351 | Value& operator+=(const Value& that); |
| 352 | Value& operator=(const Value& that); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | /** |
| 356 | * Represents a log item, or a dimension item (They are essentially the same). |
| 357 | */ |
| 358 | struct FieldValue { |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 359 | FieldValue() {} |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 360 | FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) { |
| 361 | } |
| 362 | bool operator==(const FieldValue& that) const { |
| 363 | return mField == that.mField && mValue == that.mValue; |
| 364 | } |
| 365 | bool operator!=(const FieldValue& that) const { |
| 366 | return mField != that.mField || mValue != that.mValue; |
| 367 | } |
| 368 | bool operator<(const FieldValue& that) const { |
| 369 | if (mField != that.mField) { |
| 370 | return mField < that.mField; |
| 371 | } |
| 372 | |
| 373 | if (mValue != that.mValue) { |
| 374 | return mValue < that.mValue; |
| 375 | } |
| 376 | |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | Field mField; |
| 381 | Value mValue; |
| 382 | }; |
| 383 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 384 | bool HasPositionANY(const FieldMatcher& matcher); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 385 | bool HasPositionALL(const FieldMatcher& matcher); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 386 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 387 | bool isAttributionUidField(const FieldValue& value); |
| 388 | |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 389 | /* returns uid if the field is uid field, or -1 if the field is not a uid field */ |
| 390 | int getUidIfExists(const FieldValue& value); |
| 391 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 392 | void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output); |
| 393 | |
| 394 | bool isAttributionUidField(const Field& field, const Value& value); |
tsaichristine | 7a57b8e | 2019-06-24 18:25:38 -0700 | [diff] [blame^] | 395 | bool isUidField(const Field& field, const Value& value); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 396 | |
| 397 | bool equalDimensions(const std::vector<Matcher>& dimension_a, |
| 398 | const std::vector<Matcher>& dimension_b); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 399 | } // namespace statsd |
| 400 | } // namespace os |
| 401 | } // namespace android |