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 "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 kMaxLogDepth = 2;
30 const int32_t kLastBitMask = 0x80;
31 const int32_t kClearLastBitDeco = 0x7f;
32 const int32_t kClearAllPositionMatcherMask = 0xffff00ff;
33
34 enum Type { UNKNOWN, INT, LONG, FLOAT, DOUBLE, STRING, STORAGE };
35
36 int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth);
37
38 int32_t encodeMatcherMask(int32_t mask[], int32_t depth);
39
40 // Get the encoded field for a leaf with a [field] number at depth 0;
getSimpleField(size_t field)41 inline int32_t getSimpleField(size_t field) {
42 return ((int32_t)field << 8 * 2);
43 }
44
45 /**
46 * Field is a wrapper class for 2 integers that represents the field of a log element in its Atom
47 * proto.
48 * [mTag]: the atom id.
49 * [mField]: encoded path from the root (atom) to leaf.
50 *
51 * For example:
52 * WakeLockStateChanged {
53 * repeated AttributionNode = 1;
54 * int state = 2;
55 * string tag = 3;
56 * }
57 * Read from logd, the items are structured as below:
58 * [[[1000, "tag"], [2000, "tag2"],], 2,"hello"]
59 *
60 * When we read through the list, we will encode each field in a 32bit integer.
61 * 8bit segments |--------|--------|--------|--------|
62 * Depth field0 [L]field1 [L]field1
63 *
64 * The first 8 bits are the depth of the field. for example, the uid 1000 has depth 2.
65 * The following 3 8-bit are for the item's position at each level.
66 * The first bit of each 8bits field is reserved to mark if the item is the last item at that level
67 * this is to make matching easier later.
68 *
69 * The above wakelock event is translated into FieldValue pairs.
70 * 0x02010101->1000
71 * 0x02010182->tag
72 * 0x02018201->2000
73 * 0x02018282->tag2
74 * 0x00020000->2
75 * 0x00030000->"hello"
76 *
77 * This encoding is the building block for the later operations.
78 * Please see the definition for Matcher below to see how the matching is done.
79 */
80 struct Field {
81 private:
82 int32_t mTag;
83 int32_t mField;
84
85 public:
FieldField86 Field() {}
87
FieldField88 Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) {
89 mField = getEncodedField(pos, depth, true);
90 }
91
FieldField92 Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) {
93 }
94
FieldField95 Field(int32_t tag, int32_t field) : mTag(tag), mField(field){};
96
setFieldField97 inline void setField(int32_t field) {
98 mField = field;
99 }
100
setTagField101 inline void setTag(int32_t tag) {
102 mTag = tag;
103 }
104
decorateLastPosField105 inline void decorateLastPos(int32_t depth) {
106 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
107 mField |= mask;
108 }
109
getTagField110 inline int32_t getTag() const {
111 return mTag;
112 }
113
getDepthField114 inline int32_t getDepth() const {
115 return (mField >> 24);
116 }
117
getPathField118 inline int32_t getPath(int32_t depth) const {
119 if (depth > 2 || depth < 0) return 0;
120
121 int32_t field = (mField & 0x00ffffff);
122 int32_t mask = 0xffffffff;
123 return (field & (mask << 8 * (kMaxLogDepth - depth)));
124 }
125
getPrefixField126 inline int32_t getPrefix(int32_t depth) const {
127 if (depth == 0) return 0;
128 return getPath(depth - 1);
129 }
130
getFieldField131 inline int32_t getField() const {
132 return mField;
133 }
134
getRawPosAtDepthField135 inline int32_t getRawPosAtDepth(int32_t depth) const {
136 int32_t field = (mField & 0x00ffffff);
137 int32_t shift = 8 * (kMaxLogDepth - depth);
138 int32_t mask = 0xff << shift;
139
140 return (field & mask) >> shift;
141 }
142
getPosAtDepthField143 inline int32_t getPosAtDepth(int32_t depth) const {
144 return getRawPosAtDepth(depth) & kClearLastBitDeco;
145 }
146
147 // Check if the first bit of the 8-bit segment for depth is 1
isLastPosField148 inline bool isLastPos(int32_t depth) const {
149 int32_t field = (mField & 0x00ffffff);
150 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
151 return (field & mask) != 0;
152 }
153
154 // if the 8-bit segment is all 0's
isAnyPosMatcherField155 inline bool isAnyPosMatcher(int32_t depth) const {
156 return getDepth() >= depth && getRawPosAtDepth(depth) == 0;
157 }
158 // if the 8bit is 0x80 (1000 0000)
isLastPosMatcherField159 inline bool isLastPosMatcher(int32_t depth) const {
160 return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask;
161 }
162
getSizeField163 inline size_t getSize() const {
164 return sizeof(mField) + sizeof(mTag);
165 }
166
167 inline bool operator==(const Field& that) const {
168 return mTag == that.getTag() && mField == that.getField();
169 };
170
171 inline bool operator!=(const Field& that) const {
172 return mTag != that.getTag() || mField != that.getField();
173 };
174
175 bool operator<(const Field& that) const {
176 if (mTag != that.getTag()) {
177 return mTag < that.getTag();
178 }
179
180 if (mField != that.getField()) {
181 return mField < that.getField();
182 }
183
184 return false;
185 }
186
187 bool matches(const Matcher& that) const;
188 };
189
190 /**
191 * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
192 *
193 * It contains all information needed to match one or more leaf node.
194 * All information is encoded in a Field(2 ints) and a bit mask(1 int).
195 *
196 * For example, to match the first/all/last uid field in attribution chain in Atom 10,
197 * we have the following FieldMatcher in statsd_config
198 * FieldMatcher {
199 * field:10
200 * FieldMatcher {
201 * field:1
202 * position: all/last/first
203 * FieldMatcher {
204 * field:1
205 * }
206 * }
207 * }
208 *
209 * We translate the FieldMatcher into a Field, and mask
210 * First: [Matcher Field] 0x02010101 [Mask]0xff7f7f7f
211 * Last: [Matcher Field] 0x02018001 [Mask]0xff7f807f
212 * All: [Matcher Field] 0x02010001 [Mask]0xff7f7f7f
213 *
214 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
215 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
216 * equal. Nothing can beat the performance of this matching algorithm.
217 *
218 * TODO(b/110561213): ADD EXAMPLE HERE.
219 */
220 struct Matcher {
MatcherMatcher221 Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
222
223 const Field mMatcher;
224 const int32_t mMask;
225
getMatcherMatcher226 inline const Field& getMatcher() const {
227 return mMatcher;
228 }
229
getMaskMatcher230 inline int32_t getMask() const {
231 return mMask;
232 }
233
getRawMaskAtDepthMatcher234 inline int32_t getRawMaskAtDepth(int32_t depth) const {
235 int32_t field = (mMask & 0x00ffffff);
236 int32_t shift = 8 * (kMaxLogDepth - depth);
237 int32_t mask = 0xff << shift;
238
239 return (field & mask) >> shift;
240 }
241
hasAllPositionMatcherMatcher242 bool hasAllPositionMatcher() const {
243 return mMatcher.getDepth() >= 1 && mMatcher.getRawPosAtDepth(1) == 0;
244 }
245
246 inline bool operator!=(const Matcher& that) const {
247 return mMatcher != that.getMatcher() || mMask != that.getMask();
248 }
249
250 inline bool operator==(const Matcher& that) const {
251 return mMatcher == that.mMatcher && mMask == that.mMask;
252 }
253 };
254
getSimpleMatcher(int32_t tag,size_t field)255 inline Matcher getSimpleMatcher(int32_t tag, size_t field) {
256 return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000);
257 }
258
getFirstUidMatcher(int32_t atomId)259 inline Matcher getFirstUidMatcher(int32_t atomId) {
260 int32_t pos[] = {1, 1, 1};
261 return Matcher(Field(atomId, pos, 2), 0xff7f7f7f);
262 }
263
264 /**
265 * A wrapper for a union type to contain multiple types of values.
266 *
267 */
268 struct Value {
ValueValue269 Value() : type(UNKNOWN) {}
270
ValueValue271 Value(int32_t v) {
272 int_value = v;
273 type = INT;
274 }
275
ValueValue276 Value(int64_t v) {
277 long_value = v;
278 type = LONG;
279 }
280
ValueValue281 Value(float v) {
282 float_value = v;
283 type = FLOAT;
284 }
285
ValueValue286 Value(double v) {
287 double_value = v;
288 type = DOUBLE;
289 }
290
ValueValue291 Value(const std::string& v) {
292 str_value = v;
293 type = STRING;
294 }
295
ValueValue296 Value(const std::vector<uint8_t>& v) {
297 storage_value = v;
298 type = STORAGE;
299 }
300
setIntValue301 void setInt(int32_t v) {
302 int_value = v;
303 type = INT;
304 }
305
setLongValue306 void setLong(int64_t v) {
307 long_value = v;
308 type = LONG;
309 }
310
setFloatValue311 void setFloat(float v) {
312 float_value = v;
313 type = FLOAT;
314 }
315
setDoubleValue316 void setDouble(double v) {
317 double_value = v;
318 type = DOUBLE;
319 }
320
321 union {
322 int32_t int_value;
323 int64_t long_value;
324 float float_value;
325 double double_value;
326 };
327 std::string str_value;
328 std::vector<uint8_t> storage_value;
329
330 Type type;
331
332 std::string toString() const;
333
334 bool isZero() const;
335
getTypeValue336 Type getType() const {
337 return type;
338 }
339
340 double getDouble() const;
341
342 size_t getSize() const;
343
344 Value(const Value& from);
345
346 bool operator==(const Value& that) const;
347 bool operator!=(const Value& that) const;
348
349 bool operator<(const Value& that) const;
350 bool operator>(const Value& that) const;
351 bool operator>=(const Value& that) const;
352 Value operator-(const Value& that) const;
353 Value& operator+=(const Value& that);
354 Value& operator=(const Value& that);
355 };
356
357 class Annotations {
358 public:
Annotations()359 Annotations() {
360 setNested(true); // Nested = true by default
361 }
362
363 // This enum stores where particular annotations can be found in the
364 // bitmask. Note that these pos do not correspond to annotation ids.
365 enum {
366 NESTED_POS = 0x0,
367 PRIMARY_POS = 0x1,
368 EXCLUSIVE_POS = 0x2,
369 UID_POS = 0x3
370 };
371
setNested(bool nested)372 inline void setNested(bool nested) { setBitmaskAtPos(NESTED_POS, nested); }
373
setPrimaryField(bool primary)374 inline void setPrimaryField(bool primary) { setBitmaskAtPos(PRIMARY_POS, primary); }
375
setExclusiveState(bool exclusive)376 inline void setExclusiveState(bool exclusive) { setBitmaskAtPos(EXCLUSIVE_POS, exclusive); }
377
setUidField(bool isUid)378 inline void setUidField(bool isUid) { setBitmaskAtPos(UID_POS, isUid); }
379
380 // Default value = false
isNested()381 inline bool isNested() const { return getValueFromBitmask(NESTED_POS); }
382
383 // Default value = false
isPrimaryField()384 inline bool isPrimaryField() const { return getValueFromBitmask(PRIMARY_POS); }
385
386 // Default value = false
isExclusiveState()387 inline bool isExclusiveState() const { return getValueFromBitmask(EXCLUSIVE_POS); }
388
389 // Default value = false
isUidField()390 inline bool isUidField() const { return getValueFromBitmask(UID_POS); }
391
392 std::string toString() const;
393
394 private:
setBitmaskAtPos(int pos,bool value)395 inline void setBitmaskAtPos(int pos, bool value) {
396 mBooleanBitmask &= ~(1 << pos); // clear
397 mBooleanBitmask |= (value << pos); // set
398 }
399
getValueFromBitmask(int pos)400 inline bool getValueFromBitmask(int pos) const {
401 return (mBooleanBitmask >> pos) & 0x1;
402 }
403
404 // This is a bitmask over all annotations stored in boolean form. Because
405 // there are only 4 booleans, just one byte is required.
406 uint8_t mBooleanBitmask = 0;
407 };
408
409 /**
410 * Represents a log item, or a dimension item (They are essentially the same).
411 */
412 struct FieldValue {
FieldValueFieldValue413 FieldValue() {}
FieldValueFieldValue414 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
415 }
416 bool operator==(const FieldValue& that) const {
417 return mField == that.mField && mValue == that.mValue;
418 }
419 bool operator!=(const FieldValue& that) const {
420 return mField != that.mField || mValue != that.mValue;
421 }
422 bool operator<(const FieldValue& that) const {
423 if (mField != that.mField) {
424 return mField < that.mField;
425 }
426
427 if (mValue != that.mValue) {
428 return mValue < that.mValue;
429 }
430
431 return false;
432 }
433
getSizeFieldValue434 size_t getSize() const {
435 return mField.getSize() + mValue.getSize();
436 }
437
438 Field mField;
439 Value mValue;
440 Annotations mAnnotations;
441 };
442
443 bool HasPositionANY(const FieldMatcher& matcher);
444 bool HasPositionALL(const FieldMatcher& matcher);
445 bool HasPrimitiveRepeatedField(const FieldMatcher& matcher);
446 bool ShouldUseNestedDimensions(const FieldMatcher& matcher);
447
448 bool isAttributionUidField(const FieldValue& value);
449
450 /* returns uid if the field is uid field, or -1 if the field is not a uid field */
451 int getUidIfExists(const FieldValue& value);
452
453 void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
454
455 bool isAttributionUidField(const Field& field, const Value& value);
456 bool isUidField(const FieldValue& fieldValue);
457 bool isPrimitiveRepeatedField(const Field& field);
458
459 bool equalDimensions(const std::vector<Matcher>& dimension_a,
460 const std::vector<Matcher>& dimension_b);
461
462 // Returns true if dimension_a is a subset of dimension_b.
463 bool subsetDimensions(const std::vector<Matcher>& dimension_a,
464 const std::vector<Matcher>& dimension_b);
465
466 // Estimate the memory size of the FieldValues. This is different from sizeof(FieldValue) because
467 // the size is computed at runtime using the actual contents stored in the FieldValue.
468 size_t getSize(const std::vector<FieldValue>& fieldValues);
469
470 bool shouldKeepSample(const FieldValue& sampleFieldValue, int shardOffset, int shardCount);
471
472 } // namespace statsd
473 } // namespace os
474 } // namespace android
475