blob: e251399776fb606a5a638f65f2ae11aadc13cb07 [file] [log] [blame]
Yao Chen8a8d16c2018-02-08 14:50:40 -08001/*
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"
Ruchir Rastogi13296512020-03-24 10:59:49 -070019#include "annotations.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080020
21namespace android {
22namespace os {
23namespace statsd {
24
25class HashableDimensionKey;
26struct Matcher;
27struct Field;
28struct FieldValue;
29
30const int32_t kAttributionField = 1;
31const int32_t kMaxLogDepth = 2;
32const int32_t kLastBitMask = 0x80;
33const int32_t kClearLastBitDeco = 0x7f;
Yangster-mace06cfd72018-03-10 23:22:59 -080034const int32_t kClearAllPositionMatcherMask = 0xffff00ff;
Yao Chen8a8d16c2018-02-08 14:50:40 -080035
Chenjie Yu12e5e672018-09-14 15:54:59 -070036enum Type { UNKNOWN, INT, LONG, FLOAT, DOUBLE, STRING, STORAGE };
Yao Chen8a8d16c2018-02-08 14:50:40 -080037
Yao Chen4c959cb2018-02-13 13:27:48 -080038int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -080039
Yao Chen4c959cb2018-02-13 13:27:48 -080040int32_t encodeMatcherMask(int32_t mask[], int32_t depth);
Yao Chen8a8d16c2018-02-08 14:50:40 -080041
42// Get the encoded field for a leaf with a [field] number at depth 0;
Yao Chen4c959cb2018-02-13 13:27:48 -080043inline int32_t getSimpleField(size_t field) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080044 return ((int32_t)field << 8 * 2);
45}
Yao Chen580ea3212018-02-26 14:21:54 -080046
Yao Chen8a8d16c2018-02-08 14:50:40 -080047/**
48 * Field is a wrapper class for 2 integers that represents the field of a log element in its Atom
49 * proto.
50 * [mTag]: the atom id.
51 * [mField]: encoded path from the root (atom) to leaf.
52 *
53 * For example:
54 * WakeLockStateChanged {
55 * repeated AttributionNode = 1;
56 * int state = 2;
57 * string tag = 3;
58 * }
59 * Read from logd, the items are structured as below:
60 * [[[1000, "tag"], [2000, "tag2"],], 2,"hello"]
61 *
62 * When we read through the list, we will encode each field in a 32bit integer.
63 * 8bit segments |--------|--------|--------|--------|
64 * Depth field0 [L]field1 [L]field1
65 *
66 * The first 8 bits are the depth of the field. for example, the uid 1000 has depth 2.
67 * The following 3 8-bit are for the item's position at each level.
68 * The first bit of each 8bits field is reserved to mark if the item is the last item at that level
69 * this is to make matching easier later.
70 *
71 * The above wakelock event is translated into FieldValue pairs.
72 * 0x02010101->1000
73 * 0x02010182->tag
74 * 0x02018201->2000
75 * 0x02018282->tag2
76 * 0x00020000->2
77 * 0x00030000->"hello"
78 *
79 * This encoding is the building block for the later operations.
80 * Please see the definition for Matcher below to see how the matching is done.
81 */
82struct Field {
83private:
84 int32_t mTag;
85 int32_t mField;
86
87public:
Yangster-macf5204922018-02-23 13:08:03 -080088 Field() {}
89
Yao Chen8a8d16c2018-02-08 14:50:40 -080090 Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) {
91 mField = getEncodedField(pos, depth, true);
92 }
93
94 Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) {
95 }
96
97 Field(int32_t tag, int32_t field) : mTag(tag), mField(field){};
98
99 inline void setField(int32_t field) {
100 mField = field;
101 }
102
103 inline void setTag(int32_t tag) {
104 mTag = tag;
105 }
106
107 inline void decorateLastPos(int32_t depth) {
108 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
109 mField |= mask;
110 }
111
112 inline int32_t getTag() const {
113 return mTag;
114 }
115
116 inline int32_t getDepth() const {
117 return (mField >> 24);
118 }
119
120 inline int32_t getPath(int32_t depth) const {
121 if (depth > 2 || depth < 0) return 0;
122
123 int32_t field = (mField & 0x00ffffff);
124 int32_t mask = 0xffffffff;
125 return (field & (mask << 8 * (kMaxLogDepth - depth)));
126 }
127
128 inline int32_t getPrefix(int32_t depth) const {
129 if (depth == 0) return 0;
130 return getPath(depth - 1);
131 }
132
133 inline int32_t getField() const {
134 return mField;
135 }
136
137 inline int32_t getRawPosAtDepth(int32_t depth) const {
138 int32_t field = (mField & 0x00ffffff);
139 int32_t shift = 8 * (kMaxLogDepth - depth);
140 int32_t mask = 0xff << shift;
141
142 return (field & mask) >> shift;
143 }
144
145 inline int32_t getPosAtDepth(int32_t depth) const {
146 return getRawPosAtDepth(depth) & kClearLastBitDeco;
147 }
148
149 // Check if the first bit of the 8-bit segment for depth is 1
150 inline bool isLastPos(int32_t depth) const {
151 int32_t field = (mField & 0x00ffffff);
152 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
153 return (field & mask) != 0;
154 }
155
156 // if the 8-bit segment is all 0's
157 inline bool isAnyPosMatcher(int32_t depth) const {
158 return getDepth() >= depth && getRawPosAtDepth(depth) == 0;
159 }
160 // if the 8bit is 0x80 (1000 0000)
161 inline bool isLastPosMatcher(int32_t depth) const {
162 return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask;
163 }
164
165 inline bool operator==(const Field& that) const {
166 return mTag == that.getTag() && mField == that.getField();
167 };
168
169 inline bool operator!=(const Field& that) const {
170 return mTag != that.getTag() || mField != that.getField();
171 };
172
173 bool operator<(const Field& that) const {
174 if (mTag != that.getTag()) {
175 return mTag < that.getTag();
176 }
177
178 if (mField != that.getField()) {
179 return mField < that.getField();
180 }
181
182 return false;
183 }
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700184
Yao Chen8a8d16c2018-02-08 14:50:40 -0800185 bool matches(const Matcher& that) const;
186};
187
188/**
189 * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
190 *
191 * It contains all information needed to match one or more leaf node.
192 * All information is encoded in a Field(2 ints) and a bit mask(1 int).
193 *
194 * For example, to match the first/any/last uid field in attribution chain in Atom 10,
195 * we have the following FieldMatcher in statsd_config
196 * FieldMatcher {
197 * field:10
198 * FieldMatcher {
199 * field:1
200 * position: any/last/first
201 * FieldMatcher {
202 * field:1
203 * }
204 * }
205 * }
206 *
207 * We translate the FieldMatcher into a Field, and mask
Yao Chen580ea3212018-02-26 14:21:54 -0800208 * First: [Matcher Field] 0x02010101 [Mask]0xff7f7f7f
209 * Last: [Matcher Field] 0x02018001 [Mask]0xff7f807f
210 * Any: [Matcher Field] 0x02010001 [Mask]0xff7f007f
Yangster-mace06cfd72018-03-10 23:22:59 -0800211 * All: [Matcher Field] 0x02010001 [Mask]0xff7f7f7f
Yao Chen8a8d16c2018-02-08 14:50:40 -0800212 *
213 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
214 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
215 * equal. Nothing can beat the performance of this matching algorithm.
216 *
Yao Chen5bfffb52018-06-21 16:58:51 -0700217 * TODO(b/110561213): ADD EXAMPLE HERE.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800218 */
219struct Matcher {
220 Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
221
222 const Field mMatcher;
223 const int32_t mMask;
224
Yangster-mac53928882018-02-25 23:02:56 -0800225 inline const Field& getMatcher() const {
226 return mMatcher;
227 }
228
229 inline int32_t getMask() const {
230 return mMask;
231 }
232
Yangster-mace06cfd72018-03-10 23:22:59 -0800233 inline int32_t getRawMaskAtDepth(int32_t depth) const {
234 int32_t field = (mMask & 0x00ffffff);
235 int32_t shift = 8 * (kMaxLogDepth - depth);
236 int32_t mask = 0xff << shift;
237
238 return (field & mask) >> shift;
239 }
240
241 bool hasAllPositionMatcher() const {
242 return mMatcher.getDepth() == 2 && getRawMaskAtDepth(1) == 0x7f;
243 }
244
Yao Chen8a8d16c2018-02-08 14:50:40 -0800245 bool hasAnyPositionMatcher(int* prefix) const {
Yangster-mace06cfd72018-03-10 23:22:59 -0800246 if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(1) == 0) {
247 (*prefix) = mMatcher.getPrefix(1);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800248 return true;
249 }
250 return false;
251 }
Yangster-mac53928882018-02-25 23:02:56 -0800252
253 inline bool operator!=(const Matcher& that) const {
254 return mMatcher != that.getMatcher() || mMask != that.getMask();
Yao Chen580ea3212018-02-26 14:21:54 -0800255 }
256
257 inline bool operator==(const Matcher& that) const {
258 return mMatcher == that.mMatcher && mMask == that.mMask;
259 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800260};
261
Yao Chen580ea3212018-02-26 14:21:54 -0800262inline Matcher getSimpleMatcher(int32_t tag, size_t field) {
263 return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000);
264}
265
tsaichristineed615642020-01-02 12:53:41 -0800266inline Matcher getFirstUidMatcher(int32_t atomId) {
267 int32_t pos[] = {1, 1, 1};
268 return Matcher(Field(atomId, pos, 2), 0xff7f7f7f);
269}
270
Yao Chen8a8d16c2018-02-08 14:50:40 -0800271/**
272 * A wrapper for a union type to contain multiple types of values.
273 *
274 */
275struct Value {
Yangster-macf5204922018-02-23 13:08:03 -0800276 Value() : type(UNKNOWN) {}
277
Yao Chen8a8d16c2018-02-08 14:50:40 -0800278 Value(int32_t v) {
279 int_value = v;
280 type = INT;
281 }
282
283 Value(int64_t v) {
284 long_value = v;
285 type = LONG;
286 }
287
288 Value(float v) {
289 float_value = v;
290 type = FLOAT;
291 }
292
Chenjie Yua0f02242018-07-06 16:14:34 -0700293 Value(double v) {
294 double_value = v;
295 type = DOUBLE;
296 }
297
Yao Chen8a8d16c2018-02-08 14:50:40 -0800298 Value(const std::string& v) {
299 str_value = v;
300 type = STRING;
301 }
302
Chenjie Yu12e5e672018-09-14 15:54:59 -0700303 Value(const std::vector<uint8_t>& v) {
304 storage_value = v;
305 type = STORAGE;
306 }
307
Yao Chen8a8d16c2018-02-08 14:50:40 -0800308 void setInt(int32_t v) {
309 int_value = v;
310 type = INT;
311 }
312
313 void setLong(int64_t v) {
314 long_value = v;
315 type = LONG;
316 }
317
Chenjie Yua0f02242018-07-06 16:14:34 -0700318 void setFloat(float v) {
319 float_value = v;
320 type = FLOAT;
321 }
322
323 void setDouble(double v) {
324 double_value = v;
325 type = DOUBLE;
326 }
327
Yao Chen8a8d16c2018-02-08 14:50:40 -0800328 union {
329 int32_t int_value;
330 int64_t long_value;
331 float float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700332 double double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800333 };
334 std::string str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700335 std::vector<uint8_t> storage_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800336
337 Type type;
338
339 std::string toString() const;
340
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700341 bool isZero() const;
342
Yao Chen8a8d16c2018-02-08 14:50:40 -0800343 Type getType() const {
344 return type;
345 }
346
Chenjie Yua0f02242018-07-06 16:14:34 -0700347 double getDouble() const;
348
Yao Chen8a8d16c2018-02-08 14:50:40 -0800349 Value(const Value& from);
350
351 bool operator==(const Value& that) const;
352 bool operator!=(const Value& that) const;
353
354 bool operator<(const Value& that) const;
Chenjie Yua0f02242018-07-06 16:14:34 -0700355 bool operator>(const Value& that) const;
356 bool operator>=(const Value& that) const;
357 Value operator-(const Value& that) const;
358 Value& operator+=(const Value& that);
359 Value& operator=(const Value& that);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800360};
361
Ruchir Rastogi13296512020-03-24 10:59:49 -0700362class Annotations {
363public:
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700364 Annotations() {
365 setNested(true); // Nested = true by default
366 }
Ruchir Rastogi13296512020-03-24 10:59:49 -0700367
368 // This enum stores where particular annotations can be found in the
369 // bitmask. Note that these pos do not correspond to annotation ids.
370 enum {
371 NESTED_POS = 0x0,
372 PRIMARY_POS = 0x1,
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700373 EXCLUSIVE_POS = 0x2,
374 UID_POS = 0x3
Ruchir Rastogi13296512020-03-24 10:59:49 -0700375 };
376
377 inline void setNested(bool nested) { setBitmaskAtPos(NESTED_POS, nested); }
378
379 inline void setPrimaryField(bool primary) { setBitmaskAtPos(PRIMARY_POS, primary); }
380
381 inline void setExclusiveState(bool exclusive) { setBitmaskAtPos(EXCLUSIVE_POS, exclusive); }
382
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700383 inline void setUidField(bool isUid) { setBitmaskAtPos(UID_POS, isUid); }
384
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700385 inline void setResetState(int32_t resetState) {
386 mResetState = resetState;
387 }
Ruchir Rastogi13296512020-03-24 10:59:49 -0700388
389 // Default value = false
390 inline bool isNested() const { return getValueFromBitmask(NESTED_POS); }
391
392 // Default value = false
393 inline bool isPrimaryField() const { return getValueFromBitmask(PRIMARY_POS); }
394
395 // Default value = false
396 inline bool isExclusiveState() const { return getValueFromBitmask(EXCLUSIVE_POS); }
397
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700398 // Default value = false
399 inline bool isUidField() const { return getValueFromBitmask(UID_POS); }
400
Ruchir Rastogi13296512020-03-24 10:59:49 -0700401 // If a reset state is not sent in the StatsEvent, returns -1. Note that a
402 // reset satate is only sent if and only if a reset should be triggered.
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700403 inline int32_t getResetState() const {
404 return mResetState;
405 }
Ruchir Rastogi13296512020-03-24 10:59:49 -0700406
407private:
408 inline void setBitmaskAtPos(int pos, bool value) {
409 mBooleanBitmask &= ~(1 << pos); // clear
410 mBooleanBitmask |= (value << pos); // set
411 }
412
413 inline bool getValueFromBitmask(int pos) const {
414 return (mBooleanBitmask >> pos) & 0x1;
415 }
416
417 // This is a bitmask over all annotations stored in boolean form. Because
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700418 // there are only 4 booleans, just one byte is required.
Ruchir Rastogi13296512020-03-24 10:59:49 -0700419 uint8_t mBooleanBitmask = 0;
420
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700421 int32_t mResetState = -1;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700422};
423
Yao Chen8a8d16c2018-02-08 14:50:40 -0800424/**
425 * Represents a log item, or a dimension item (They are essentially the same).
426 */
427struct FieldValue {
Yangster-macf5204922018-02-23 13:08:03 -0800428 FieldValue() {}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800429 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
430 }
431 bool operator==(const FieldValue& that) const {
432 return mField == that.mField && mValue == that.mValue;
433 }
434 bool operator!=(const FieldValue& that) const {
435 return mField != that.mField || mValue != that.mValue;
436 }
437 bool operator<(const FieldValue& that) const {
438 if (mField != that.mField) {
439 return mField < that.mField;
440 }
441
442 if (mValue != that.mValue) {
443 return mValue < that.mValue;
444 }
445
446 return false;
447 }
448
449 Field mField;
450 Value mValue;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700451 Annotations mAnnotations;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800452};
453
Yangster13fb7e42018-03-07 17:30:49 -0800454bool HasPositionANY(const FieldMatcher& matcher);
Yangster-mac9def8e32018-04-17 13:55:51 -0700455bool HasPositionALL(const FieldMatcher& matcher);
Yangster13fb7e42018-03-07 17:30:49 -0800456
Yao Chen8a8d16c2018-02-08 14:50:40 -0800457bool isAttributionUidField(const FieldValue& value);
458
Yao Chen4ce07292019-02-13 13:06:36 -0800459/* returns uid if the field is uid field, or -1 if the field is not a uid field */
460int getUidIfExists(const FieldValue& value);
461
Yao Chen8a8d16c2018-02-08 14:50:40 -0800462void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
463
464bool isAttributionUidField(const Field& field, const Value& value);
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700465bool isUidField(const FieldValue& fieldValue);
Yangster13fb7e42018-03-07 17:30:49 -0800466
467bool equalDimensions(const std::vector<Matcher>& dimension_a,
468 const std::vector<Matcher>& dimension_b);
tsaichristine3a83e812019-12-13 16:46:11 -0800469
470// Returns true if dimension_a is a subset of dimension_b.
471bool subsetDimensions(const std::vector<Matcher>& dimension_a,
472 const std::vector<Matcher>& dimension_b);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800473} // namespace statsd
474} // namespace os
475} // namespace android