blob: 92e09ea0f8f913191c9c33af53b4a0e017cd0d42 [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 }
184 bool matches(const Matcher& that) const;
185};
186
187/**
188 * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
189 *
190 * It contains all information needed to match one or more leaf node.
191 * All information is encoded in a Field(2 ints) and a bit mask(1 int).
192 *
193 * For example, to match the first/any/last uid field in attribution chain in Atom 10,
194 * we have the following FieldMatcher in statsd_config
195 * FieldMatcher {
196 * field:10
197 * FieldMatcher {
198 * field:1
199 * position: any/last/first
200 * FieldMatcher {
201 * field:1
202 * }
203 * }
204 * }
205 *
206 * We translate the FieldMatcher into a Field, and mask
Yao Chen580ea3212018-02-26 14:21:54 -0800207 * First: [Matcher Field] 0x02010101 [Mask]0xff7f7f7f
208 * Last: [Matcher Field] 0x02018001 [Mask]0xff7f807f
209 * Any: [Matcher Field] 0x02010001 [Mask]0xff7f007f
Yangster-mace06cfd72018-03-10 23:22:59 -0800210 * All: [Matcher Field] 0x02010001 [Mask]0xff7f7f7f
Yao Chen8a8d16c2018-02-08 14:50:40 -0800211 *
212 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
213 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
214 * equal. Nothing can beat the performance of this matching algorithm.
215 *
Yao Chen5bfffb52018-06-21 16:58:51 -0700216 * TODO(b/110561213): ADD EXAMPLE HERE.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800217 */
218struct Matcher {
219 Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
220
221 const Field mMatcher;
222 const int32_t mMask;
223
Yangster-mac53928882018-02-25 23:02:56 -0800224 inline const Field& getMatcher() const {
225 return mMatcher;
226 }
227
228 inline int32_t getMask() const {
229 return mMask;
230 }
231
Yangster-mace06cfd72018-03-10 23:22:59 -0800232 inline int32_t getRawMaskAtDepth(int32_t depth) const {
233 int32_t field = (mMask & 0x00ffffff);
234 int32_t shift = 8 * (kMaxLogDepth - depth);
235 int32_t mask = 0xff << shift;
236
237 return (field & mask) >> shift;
238 }
239
240 bool hasAllPositionMatcher() const {
241 return mMatcher.getDepth() == 2 && getRawMaskAtDepth(1) == 0x7f;
242 }
243
Yao Chen8a8d16c2018-02-08 14:50:40 -0800244 bool hasAnyPositionMatcher(int* prefix) const {
Yangster-mace06cfd72018-03-10 23:22:59 -0800245 if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(1) == 0) {
246 (*prefix) = mMatcher.getPrefix(1);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800247 return true;
248 }
249 return false;
250 }
Yangster-mac53928882018-02-25 23:02:56 -0800251
252 inline bool operator!=(const Matcher& that) const {
253 return mMatcher != that.getMatcher() || mMask != that.getMask();
Yao Chen580ea3212018-02-26 14:21:54 -0800254 }
255
256 inline bool operator==(const Matcher& that) const {
257 return mMatcher == that.mMatcher && mMask == that.mMask;
258 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800259};
260
Yao Chen580ea3212018-02-26 14:21:54 -0800261inline Matcher getSimpleMatcher(int32_t tag, size_t field) {
262 return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000);
263}
264
tsaichristineed615642020-01-02 12:53:41 -0800265inline Matcher getFirstUidMatcher(int32_t atomId) {
266 int32_t pos[] = {1, 1, 1};
267 return Matcher(Field(atomId, pos, 2), 0xff7f7f7f);
268}
269
Yao Chen8a8d16c2018-02-08 14:50:40 -0800270/**
271 * A wrapper for a union type to contain multiple types of values.
272 *
273 */
274struct Value {
Yangster-macf5204922018-02-23 13:08:03 -0800275 Value() : type(UNKNOWN) {}
276
Yao Chen8a8d16c2018-02-08 14:50:40 -0800277 Value(int32_t v) {
278 int_value = v;
279 type = INT;
280 }
281
282 Value(int64_t v) {
283 long_value = v;
284 type = LONG;
285 }
286
287 Value(float v) {
288 float_value = v;
289 type = FLOAT;
290 }
291
Chenjie Yua0f02242018-07-06 16:14:34 -0700292 Value(double v) {
293 double_value = v;
294 type = DOUBLE;
295 }
296
Yao Chen8a8d16c2018-02-08 14:50:40 -0800297 Value(const std::string& v) {
298 str_value = v;
299 type = STRING;
300 }
301
Chenjie Yu12e5e672018-09-14 15:54:59 -0700302 Value(const std::vector<uint8_t>& v) {
303 storage_value = v;
304 type = STORAGE;
305 }
306
Yao Chen8a8d16c2018-02-08 14:50:40 -0800307 void setInt(int32_t v) {
308 int_value = v;
309 type = INT;
310 }
311
312 void setLong(int64_t v) {
313 long_value = v;
314 type = LONG;
315 }
316
Chenjie Yua0f02242018-07-06 16:14:34 -0700317 void setFloat(float v) {
318 float_value = v;
319 type = FLOAT;
320 }
321
322 void setDouble(double v) {
323 double_value = v;
324 type = DOUBLE;
325 }
326
Yao Chen8a8d16c2018-02-08 14:50:40 -0800327 union {
328 int32_t int_value;
329 int64_t long_value;
330 float float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700331 double double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800332 };
333 std::string str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700334 std::vector<uint8_t> storage_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800335
336 Type type;
337
338 std::string toString() const;
339
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700340 bool isZero() const;
341
Yao Chen8a8d16c2018-02-08 14:50:40 -0800342 Type getType() const {
343 return type;
344 }
345
Chenjie Yua0f02242018-07-06 16:14:34 -0700346 double getDouble() const;
347
Yao Chen8a8d16c2018-02-08 14:50:40 -0800348 Value(const Value& from);
349
350 bool operator==(const Value& that) const;
351 bool operator!=(const Value& that) const;
352
353 bool operator<(const Value& that) const;
Chenjie Yua0f02242018-07-06 16:14:34 -0700354 bool operator>(const Value& that) const;
355 bool operator>=(const Value& that) const;
356 Value operator-(const Value& that) const;
357 Value& operator+=(const Value& that);
358 Value& operator=(const Value& that);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800359};
360
Ruchir Rastogi13296512020-03-24 10:59:49 -0700361class Annotations {
362public:
363 Annotations() {}
364
365 // This enum stores where particular annotations can be found in the
366 // bitmask. Note that these pos do not correspond to annotation ids.
367 enum {
368 NESTED_POS = 0x0,
369 PRIMARY_POS = 0x1,
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700370 EXCLUSIVE_POS = 0x2,
371 UID_POS = 0x3
Ruchir Rastogi13296512020-03-24 10:59:49 -0700372 };
373
374 inline void setNested(bool nested) { setBitmaskAtPos(NESTED_POS, nested); }
375
376 inline void setPrimaryField(bool primary) { setBitmaskAtPos(PRIMARY_POS, primary); }
377
378 inline void setExclusiveState(bool exclusive) { setBitmaskAtPos(EXCLUSIVE_POS, exclusive); }
379
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700380 inline void setUidField(bool isUid) { setBitmaskAtPos(UID_POS, isUid); }
381
Ruchir Rastogi13296512020-03-24 10:59:49 -0700382 inline void setResetState(int resetState) { mResetState = resetState; }
383
384 // Default value = false
385 inline bool isNested() const { return getValueFromBitmask(NESTED_POS); }
386
387 // Default value = false
388 inline bool isPrimaryField() const { return getValueFromBitmask(PRIMARY_POS); }
389
390 // Default value = false
391 inline bool isExclusiveState() const { return getValueFromBitmask(EXCLUSIVE_POS); }
392
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700393 // Default value = false
394 inline bool isUidField() const { return getValueFromBitmask(UID_POS); }
395
Ruchir Rastogi13296512020-03-24 10:59:49 -0700396 // If a reset state is not sent in the StatsEvent, returns -1. Note that a
397 // reset satate is only sent if and only if a reset should be triggered.
398 inline int getResetState() const { return mResetState; }
399
400private:
401 inline void setBitmaskAtPos(int pos, bool value) {
402 mBooleanBitmask &= ~(1 << pos); // clear
403 mBooleanBitmask |= (value << pos); // set
404 }
405
406 inline bool getValueFromBitmask(int pos) const {
407 return (mBooleanBitmask >> pos) & 0x1;
408 }
409
410 // This is a bitmask over all annotations stored in boolean form. Because
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700411 // there are only 4 booleans, just one byte is required.
Ruchir Rastogi13296512020-03-24 10:59:49 -0700412 uint8_t mBooleanBitmask = 0;
413
414 int mResetState = -1;
415};
416
Yao Chen8a8d16c2018-02-08 14:50:40 -0800417/**
418 * Represents a log item, or a dimension item (They are essentially the same).
419 */
420struct FieldValue {
Yangster-macf5204922018-02-23 13:08:03 -0800421 FieldValue() {}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800422 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
423 }
424 bool operator==(const FieldValue& that) const {
425 return mField == that.mField && mValue == that.mValue;
426 }
427 bool operator!=(const FieldValue& that) const {
428 return mField != that.mField || mValue != that.mValue;
429 }
430 bool operator<(const FieldValue& that) const {
431 if (mField != that.mField) {
432 return mField < that.mField;
433 }
434
435 if (mValue != that.mValue) {
436 return mValue < that.mValue;
437 }
438
439 return false;
440 }
441
442 Field mField;
443 Value mValue;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700444 Annotations mAnnotations;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800445};
446
Yangster13fb7e42018-03-07 17:30:49 -0800447bool HasPositionANY(const FieldMatcher& matcher);
Yangster-mac9def8e32018-04-17 13:55:51 -0700448bool HasPositionALL(const FieldMatcher& matcher);
Yangster13fb7e42018-03-07 17:30:49 -0800449
Yao Chen8a8d16c2018-02-08 14:50:40 -0800450bool isAttributionUidField(const FieldValue& value);
451
Yao Chen4ce07292019-02-13 13:06:36 -0800452/* returns uid if the field is uid field, or -1 if the field is not a uid field */
453int getUidIfExists(const FieldValue& value);
454
Yao Chen8a8d16c2018-02-08 14:50:40 -0800455void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
456
457bool isAttributionUidField(const Field& field, const Value& value);
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700458bool isUidField(const FieldValue& fieldValue);
Yangster13fb7e42018-03-07 17:30:49 -0800459
460bool equalDimensions(const std::vector<Matcher>& dimension_a,
461 const std::vector<Matcher>& dimension_b);
tsaichristine3a83e812019-12-13 16:46:11 -0800462
463// Returns true if dimension_a is a subset of dimension_b.
464bool subsetDimensions(const std::vector<Matcher>& dimension_a,
465 const std::vector<Matcher>& dimension_b);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800466} // namespace statsd
467} // namespace os
468} // namespace android