blob: 0e3ae06033e75c3efc8e2e57309937e3009ff59b [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"
19
20namespace android {
21namespace os {
22namespace statsd {
23
24class HashableDimensionKey;
25struct Matcher;
26struct Field;
27struct FieldValue;
28
29const int32_t kAttributionField = 1;
30const int32_t kMaxLogDepth = 2;
31const int32_t kLastBitMask = 0x80;
32const int32_t kClearLastBitDeco = 0x7f;
33
Yangster-macf5204922018-02-23 13:08:03 -080034enum Type { UNKNOWN, INT, LONG, FLOAT, STRING };
Yao Chen8a8d16c2018-02-08 14:50:40 -080035
Yao Chen4c959cb2018-02-13 13:27:48 -080036int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -080037
Yao Chen4c959cb2018-02-13 13:27:48 -080038int32_t encodeMatcherMask(int32_t mask[], int32_t depth);
Yao Chen8a8d16c2018-02-08 14:50:40 -080039
40// Get the encoded field for a leaf with a [field] number at depth 0;
Yao Chen4c959cb2018-02-13 13:27:48 -080041inline int32_t getSimpleField(size_t field) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080042 return ((int32_t)field << 8 * 2);
43}
Yao Chen580ea3212018-02-26 14:21:54 -080044
Yao Chen8a8d16c2018-02-08 14:50:40 -080045/**
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 */
80struct Field {
81private:
82 int32_t mTag;
83 int32_t mField;
84
85public:
Yangster-macf5204922018-02-23 13:08:03 -080086 Field() {}
87
Yao Chen8a8d16c2018-02-08 14:50:40 -080088 Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) {
89 mField = getEncodedField(pos, depth, true);
90 }
91
92 Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) {
93 }
94
95 Field(int32_t tag, int32_t field) : mTag(tag), mField(field){};
96
97 inline void setField(int32_t field) {
98 mField = field;
99 }
100
101 inline void setTag(int32_t tag) {
102 mTag = tag;
103 }
104
105 inline void decorateLastPos(int32_t depth) {
106 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
107 mField |= mask;
108 }
109
110 inline int32_t getTag() const {
111 return mTag;
112 }
113
114 inline int32_t getDepth() const {
115 return (mField >> 24);
116 }
117
118 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
126 inline int32_t getPrefix(int32_t depth) const {
127 if (depth == 0) return 0;
128 return getPath(depth - 1);
129 }
130
131 inline int32_t getField() const {
132 return mField;
133 }
134
135 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
143 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
148 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
155 inline bool isAnyPosMatcher(int32_t depth) const {
156 return getDepth() >= depth && getRawPosAtDepth(depth) == 0;
157 }
158 // if the 8bit is 0x80 (1000 0000)
159 inline bool isLastPosMatcher(int32_t depth) const {
160 return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask;
161 }
162
163 inline bool operator==(const Field& that) const {
164 return mTag == that.getTag() && mField == that.getField();
165 };
166
167 inline bool operator!=(const Field& that) const {
168 return mTag != that.getTag() || mField != that.getField();
169 };
170
171 bool operator<(const Field& that) const {
172 if (mTag != that.getTag()) {
173 return mTag < that.getTag();
174 }
175
176 if (mField != that.getField()) {
177 return mField < that.getField();
178 }
179
180 return false;
181 }
182 bool matches(const Matcher& that) const;
183};
184
185/**
186 * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
187 *
188 * It contains all information needed to match one or more leaf node.
189 * All information is encoded in a Field(2 ints) and a bit mask(1 int).
190 *
191 * For example, to match the first/any/last uid field in attribution chain in Atom 10,
192 * we have the following FieldMatcher in statsd_config
193 * FieldMatcher {
194 * field:10
195 * FieldMatcher {
196 * field:1
197 * position: any/last/first
198 * FieldMatcher {
199 * field:1
200 * }
201 * }
202 * }
203 *
204 * We translate the FieldMatcher into a Field, and mask
Yao Chen580ea3212018-02-26 14:21:54 -0800205 * First: [Matcher Field] 0x02010101 [Mask]0xff7f7f7f
206 * Last: [Matcher Field] 0x02018001 [Mask]0xff7f807f
207 * Any: [Matcher Field] 0x02010001 [Mask]0xff7f007f
Yao Chen8a8d16c2018-02-08 14:50:40 -0800208 *
209 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
210 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
211 * equal. Nothing can beat the performance of this matching algorithm.
212 *
213 * TODO: ADD EXAMPLE HERE.
214 */
215struct Matcher {
216 Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
217
218 const Field mMatcher;
219 const int32_t mMask;
220
Yangster-mac53928882018-02-25 23:02:56 -0800221 inline const Field& getMatcher() const {
222 return mMatcher;
223 }
224
225 inline int32_t getMask() const {
226 return mMask;
227 }
228
Yao Chen8a8d16c2018-02-08 14:50:40 -0800229 bool hasAnyPositionMatcher(int* prefix) const {
230 if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(2) == 0) {
231 (*prefix) = mMatcher.getPrefix(2);
232 return true;
233 }
234 return false;
235 }
Yangster-mac53928882018-02-25 23:02:56 -0800236
237 inline bool operator!=(const Matcher& that) const {
238 return mMatcher != that.getMatcher() || mMask != that.getMask();
Yao Chen580ea3212018-02-26 14:21:54 -0800239 }
240
241 inline bool operator==(const Matcher& that) const {
242 return mMatcher == that.mMatcher && mMask == that.mMask;
243 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800244};
245
Yao Chen580ea3212018-02-26 14:21:54 -0800246inline Matcher getSimpleMatcher(int32_t tag, size_t field) {
247 return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000);
248}
249
Yao Chen8a8d16c2018-02-08 14:50:40 -0800250/**
251 * A wrapper for a union type to contain multiple types of values.
252 *
253 */
254struct Value {
Yangster-macf5204922018-02-23 13:08:03 -0800255 Value() : type(UNKNOWN) {}
256
Yao Chen8a8d16c2018-02-08 14:50:40 -0800257 Value(int32_t v) {
258 int_value = v;
259 type = INT;
260 }
261
262 Value(int64_t v) {
263 long_value = v;
264 type = LONG;
265 }
266
267 Value(float v) {
268 float_value = v;
269 type = FLOAT;
270 }
271
272 Value(const std::string& v) {
273 str_value = v;
274 type = STRING;
275 }
276
277 void setInt(int32_t v) {
278 int_value = v;
279 type = INT;
280 }
281
282 void setLong(int64_t v) {
283 long_value = v;
284 type = LONG;
285 }
286
287 union {
288 int32_t int_value;
289 int64_t long_value;
290 float float_value;
291 };
292 std::string str_value;
293
294 Type type;
295
296 std::string toString() const;
297
298 Type getType() const {
299 return type;
300 }
301
302 Value(const Value& from);
303
304 bool operator==(const Value& that) const;
305 bool operator!=(const Value& that) const;
306
307 bool operator<(const Value& that) const;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800308};
309
310/**
311 * Represents a log item, or a dimension item (They are essentially the same).
312 */
313struct FieldValue {
Yangster-macf5204922018-02-23 13:08:03 -0800314 FieldValue() {}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800315 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
316 }
317 bool operator==(const FieldValue& that) const {
318 return mField == that.mField && mValue == that.mValue;
319 }
320 bool operator!=(const FieldValue& that) const {
321 return mField != that.mField || mValue != that.mValue;
322 }
323 bool operator<(const FieldValue& that) const {
324 if (mField != that.mField) {
325 return mField < that.mField;
326 }
327
328 if (mValue != that.mValue) {
329 return mValue < that.mValue;
330 }
331
332 return false;
333 }
334
335 Field mField;
336 Value mValue;
337};
338
Yangster13fb7e42018-03-07 17:30:49 -0800339bool HasPositionANY(const FieldMatcher& matcher);
340
Yao Chen8a8d16c2018-02-08 14:50:40 -0800341bool isAttributionUidField(const FieldValue& value);
342
343void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
344
345bool isAttributionUidField(const Field& field, const Value& value);
Yangster13fb7e42018-03-07 17:30:49 -0800346
347bool equalDimensions(const std::vector<Matcher>& dimension_a,
348 const std::vector<Matcher>& dimension_b);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800349} // namespace statsd
350} // namespace os
351} // namespace android