• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "annotations.h"
20 
21 namespace android {
22 namespace os {
23 namespace statsd {
24 
25 class HashableDimensionKey;
26 struct Matcher;
27 struct Field;
28 struct FieldValue;
29 
30 const int32_t kMaxLogDepth = 2;
31 const int32_t kLastBitMask = 0x80;
32 const int32_t kClearLastBitDeco = 0x7f;
33 const int32_t kClearAllPositionMatcherMask = 0xffff00ff;
34 
35 enum Type { UNKNOWN, INT, LONG, FLOAT, DOUBLE, STRING, STORAGE };
36 
37 int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth);
38 
39 int32_t encodeMatcherMask(int32_t mask[], int32_t depth);
40 
41 // Get the encoded field for a leaf with a [field] number at depth 0;
getSimpleField(size_t field)42 inline int32_t getSimpleField(size_t field) {
43     return ((int32_t)field << 8 * 2);
44 }
45 
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:
FieldField87     Field() {}
88 
FieldField89     Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) {
90         mField = getEncodedField(pos, depth, true);
91     }
92 
FieldField93     Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) {
94     }
95 
FieldField96     Field(int32_t tag, int32_t field) : mTag(tag), mField(field){};
97 
setFieldField98     inline void setField(int32_t field) {
99         mField = field;
100     }
101 
setTagField102     inline void setTag(int32_t tag) {
103         mTag = tag;
104     }
105 
decorateLastPosField106     inline void decorateLastPos(int32_t depth) {
107         int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
108         mField |= mask;
109     }
110 
getTagField111     inline int32_t getTag() const {
112         return mTag;
113     }
114 
getDepthField115     inline int32_t getDepth() const {
116         return (mField >> 24);
117     }
118 
getPathField119     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 
getPrefixField127     inline int32_t getPrefix(int32_t depth) const {
128         if (depth == 0) return 0;
129         return getPath(depth - 1);
130     }
131 
getFieldField132     inline int32_t getField() const {
133         return mField;
134     }
135 
getRawPosAtDepthField136     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 
getPosAtDepthField144     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
isLastPosField149     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
isAnyPosMatcherField156     inline bool isAnyPosMatcher(int32_t depth) const {
157         return getDepth() >= depth && getRawPosAtDepth(depth) == 0;
158     }
159     // if the 8bit is 0x80 (1000 0000)
isLastPosMatcherField160     inline bool isLastPosMatcher(int32_t depth) const {
161         return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask;
162     }
163 
getSizeField164     inline size_t getSize() const {
165         return sizeof(mField) + sizeof(mTag);
166     }
167 
168     inline bool operator==(const Field& that) const {
169         return mTag == that.getTag() && mField == that.getField();
170     };
171 
172     inline bool operator!=(const Field& that) const {
173         return mTag != that.getTag() || mField != that.getField();
174     };
175 
176     bool operator<(const Field& that) const {
177         if (mTag != that.getTag()) {
178             return mTag < that.getTag();
179         }
180 
181         if (mField != that.getField()) {
182             return mField < that.getField();
183         }
184 
185         return false;
186     }
187 
188     bool matches(const Matcher& that) const;
189 };
190 
191 /**
192  * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
193  *
194  * It contains all information needed to match one or more leaf node.
195  * All information is encoded in a Field(2 ints) and a bit mask(1 int).
196  *
197  * For example, to match the first/all/last uid field in attribution chain in Atom 10,
198  * we have the following FieldMatcher in statsd_config
199  *    FieldMatcher {
200  *        field:10
201  *         FieldMatcher {
202  *              field:1
203  *              position: all/last/first
204  *              FieldMatcher {
205  *                  field:1
206  *              }
207  *          }
208  *     }
209  *
210  * We translate the FieldMatcher into a Field, and mask
211  * First: [Matcher Field] 0x02010101  [Mask]0xff7f7f7f
212  * Last:  [Matcher Field] 0x02018001  [Mask]0xff7f807f
213  * All:   [Matcher Field] 0x02010001  [Mask]0xff7f7f7f
214  *
215  * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
216  * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
217  * equal. Nothing can beat the performance of this matching algorithm.
218  *
219  * TODO(b/110561213): ADD EXAMPLE HERE.
220  */
221 struct Matcher {
MatcherMatcher222     Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
223 
224     const Field mMatcher;
225     const int32_t mMask;
226 
getMatcherMatcher227     inline const Field& getMatcher() const {
228         return mMatcher;
229     }
230 
getMaskMatcher231     inline int32_t getMask() const {
232         return mMask;
233     }
234 
getRawMaskAtDepthMatcher235     inline int32_t getRawMaskAtDepth(int32_t depth) const {
236         int32_t field = (mMask & 0x00ffffff);
237         int32_t shift = 8 * (kMaxLogDepth - depth);
238         int32_t mask = 0xff << shift;
239 
240         return (field & mask) >> shift;
241     }
242 
hasAllPositionMatcherMatcher243     bool hasAllPositionMatcher() const {
244         return mMatcher.getDepth() >= 1 && getRawMaskAtDepth(1) == 0x7f;
245     }
246 
247     inline bool operator!=(const Matcher& that) const {
248         return mMatcher != that.getMatcher() || mMask != that.getMask();
249     }
250 
251     inline bool operator==(const Matcher& that) const {
252         return mMatcher == that.mMatcher && mMask == that.mMask;
253     }
254 };
255 
getSimpleMatcher(int32_t tag,size_t field)256 inline Matcher getSimpleMatcher(int32_t tag, size_t field) {
257     return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000);
258 }
259 
getFirstUidMatcher(int32_t atomId)260 inline Matcher getFirstUidMatcher(int32_t atomId) {
261     int32_t pos[] = {1, 1, 1};
262     return Matcher(Field(atomId, pos, 2), 0xff7f7f7f);
263 }
264 
265 /**
266  * A wrapper for a union type to contain multiple types of values.
267  *
268  */
269 struct Value {
ValueValue270     Value() : type(UNKNOWN) {}
271 
ValueValue272     Value(int32_t v) {
273         int_value = v;
274         type = INT;
275     }
276 
ValueValue277     Value(int64_t v) {
278         long_value = v;
279         type = LONG;
280     }
281 
ValueValue282     Value(float v) {
283         float_value = v;
284         type = FLOAT;
285     }
286 
ValueValue287     Value(double v) {
288         double_value = v;
289         type = DOUBLE;
290     }
291 
ValueValue292     Value(const std::string& v) {
293         str_value = v;
294         type = STRING;
295     }
296 
ValueValue297     Value(const std::vector<uint8_t>& v) {
298         storage_value = v;
299         type = STORAGE;
300     }
301 
setIntValue302     void setInt(int32_t v) {
303         int_value = v;
304         type = INT;
305     }
306 
setLongValue307     void setLong(int64_t v) {
308         long_value = v;
309         type = LONG;
310     }
311 
setFloatValue312     void setFloat(float v) {
313         float_value = v;
314         type = FLOAT;
315     }
316 
setDoubleValue317     void setDouble(double v) {
318         double_value = v;
319         type = DOUBLE;
320     }
321 
322     union {
323         int32_t int_value;
324         int64_t long_value;
325         float float_value;
326         double double_value;
327     };
328     std::string str_value;
329     std::vector<uint8_t> storage_value;
330 
331     Type type;
332 
333     std::string toString() const;
334 
335     bool isZero() const;
336 
getTypeValue337     Type getType() const {
338         return type;
339     }
340 
341     double getDouble() const;
342 
343     size_t getSize() const;
344 
345     Value(const Value& from);
346 
347     bool operator==(const Value& that) const;
348     bool operator!=(const Value& that) const;
349 
350     bool operator<(const Value& that) const;
351     bool operator>(const Value& that) const;
352     bool operator>=(const Value& that) const;
353     Value operator-(const Value& that) const;
354     Value& operator+=(const Value& that);
355     Value& operator=(const Value& that);
356 };
357 
358 class Annotations {
359 public:
Annotations()360     Annotations() {
361         setNested(true);  // Nested = true by default
362     }
363 
364     // This enum stores where particular annotations can be found in the
365     // bitmask. Note that these pos do not correspond to annotation ids.
366     enum {
367         NESTED_POS = 0x0,
368         PRIMARY_POS = 0x1,
369         EXCLUSIVE_POS = 0x2,
370         UID_POS = 0x3
371     };
372 
setNested(bool nested)373     inline void setNested(bool nested) { setBitmaskAtPos(NESTED_POS, nested); }
374 
setPrimaryField(bool primary)375     inline void setPrimaryField(bool primary) { setBitmaskAtPos(PRIMARY_POS, primary); }
376 
setExclusiveState(bool exclusive)377     inline void setExclusiveState(bool exclusive) { setBitmaskAtPos(EXCLUSIVE_POS, exclusive); }
378 
setUidField(bool isUid)379     inline void setUidField(bool isUid) { setBitmaskAtPos(UID_POS, isUid); }
380 
381     // Default value = false
isNested()382     inline bool isNested() const { return getValueFromBitmask(NESTED_POS); }
383 
384     // Default value = false
isPrimaryField()385     inline bool isPrimaryField() const { return getValueFromBitmask(PRIMARY_POS); }
386 
387     // Default value = false
isExclusiveState()388     inline bool isExclusiveState() const { return getValueFromBitmask(EXCLUSIVE_POS); }
389 
390     // Default value = false
isUidField()391     inline bool isUidField() const { return getValueFromBitmask(UID_POS); }
392 
393 private:
setBitmaskAtPos(int pos,bool value)394     inline void setBitmaskAtPos(int pos, bool value) {
395         mBooleanBitmask &= ~(1 << pos); // clear
396         mBooleanBitmask |= (value << pos); // set
397     }
398 
getValueFromBitmask(int pos)399     inline bool getValueFromBitmask(int pos) const {
400         return (mBooleanBitmask >> pos) & 0x1;
401     }
402 
403     // This is a bitmask over all annotations stored in boolean form. Because
404     // there are only 4 booleans, just one byte is required.
405     uint8_t mBooleanBitmask = 0;
406 };
407 
408 /**
409  * Represents a log item, or a dimension item (They are essentially the same).
410  */
411 struct FieldValue {
FieldValueFieldValue412     FieldValue() {}
FieldValueFieldValue413     FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
414     }
415     bool operator==(const FieldValue& that) const {
416         return mField == that.mField && mValue == that.mValue;
417     }
418     bool operator!=(const FieldValue& that) const {
419         return mField != that.mField || mValue != that.mValue;
420     }
421     bool operator<(const FieldValue& that) const {
422         if (mField != that.mField) {
423             return mField < that.mField;
424         }
425 
426         if (mValue != that.mValue) {
427             return mValue < that.mValue;
428         }
429 
430         return false;
431     }
432 
getSizeFieldValue433     size_t getSize() const {
434         return mField.getSize() + mValue.getSize();
435     }
436 
437     Field mField;
438     Value mValue;
439     Annotations mAnnotations;
440 };
441 
442 bool HasPositionANY(const FieldMatcher& matcher);
443 bool HasPositionALL(const FieldMatcher& matcher);
444 bool HasPrimitiveRepeatedField(const FieldMatcher& matcher);
445 bool ShouldUseNestedDimensions(const FieldMatcher& matcher);
446 
447 bool isAttributionUidField(const FieldValue& value);
448 
449 /* returns uid if the field is uid field, or -1 if the field is not a uid field */
450 int getUidIfExists(const FieldValue& value);
451 
452 void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
453 
454 bool isAttributionUidField(const Field& field, const Value& value);
455 bool isUidField(const FieldValue& fieldValue);
456 bool isPrimitiveRepeatedField(const Field& field);
457 
458 bool equalDimensions(const std::vector<Matcher>& dimension_a,
459                      const std::vector<Matcher>& dimension_b);
460 
461 // Returns true if dimension_a is a subset of dimension_b.
462 bool subsetDimensions(const std::vector<Matcher>& dimension_a,
463                       const std::vector<Matcher>& dimension_b);
464 
465 // Estimate the memory size of the FieldValues. This is different from sizeof(FieldValue) because
466 // the size is computed at runtime using the actual contents stored in the FieldValue.
467 size_t getSize(const std::vector<FieldValue>& fieldValues);
468 }  // namespace statsd
469 }  // namespace os
470 }  // namespace android
471