blob: b0e2c43e8cdd3c607f3a5fa6a9c198b2d06b1639 [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 Chen8a8d16c2018-02-08 14:50:40 -080044/**
45 * Field is a wrapper class for 2 integers that represents the field of a log element in its Atom
46 * proto.
47 * [mTag]: the atom id.
48 * [mField]: encoded path from the root (atom) to leaf.
49 *
50 * For example:
51 * WakeLockStateChanged {
52 * repeated AttributionNode = 1;
53 * int state = 2;
54 * string tag = 3;
55 * }
56 * Read from logd, the items are structured as below:
57 * [[[1000, "tag"], [2000, "tag2"],], 2,"hello"]
58 *
59 * When we read through the list, we will encode each field in a 32bit integer.
60 * 8bit segments |--------|--------|--------|--------|
61 * Depth field0 [L]field1 [L]field1
62 *
63 * The first 8 bits are the depth of the field. for example, the uid 1000 has depth 2.
64 * The following 3 8-bit are for the item's position at each level.
65 * The first bit of each 8bits field is reserved to mark if the item is the last item at that level
66 * this is to make matching easier later.
67 *
68 * The above wakelock event is translated into FieldValue pairs.
69 * 0x02010101->1000
70 * 0x02010182->tag
71 * 0x02018201->2000
72 * 0x02018282->tag2
73 * 0x00020000->2
74 * 0x00030000->"hello"
75 *
76 * This encoding is the building block for the later operations.
77 * Please see the definition for Matcher below to see how the matching is done.
78 */
79struct Field {
80private:
81 int32_t mTag;
82 int32_t mField;
83
84public:
Yangster-macf5204922018-02-23 13:08:03 -080085 Field() {}
86
Yao Chen8a8d16c2018-02-08 14:50:40 -080087 Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) {
88 mField = getEncodedField(pos, depth, true);
89 }
90
91 Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) {
92 }
93
94 Field(int32_t tag, int32_t field) : mTag(tag), mField(field){};
95
96 inline void setField(int32_t field) {
97 mField = field;
98 }
99
100 inline void setTag(int32_t tag) {
101 mTag = tag;
102 }
103
104 inline void decorateLastPos(int32_t depth) {
105 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
106 mField |= mask;
107 }
108
109 inline int32_t getTag() const {
110 return mTag;
111 }
112
113 inline int32_t getDepth() const {
114 return (mField >> 24);
115 }
116
117 inline int32_t getPath(int32_t depth) const {
118 if (depth > 2 || depth < 0) return 0;
119
120 int32_t field = (mField & 0x00ffffff);
121 int32_t mask = 0xffffffff;
122 return (field & (mask << 8 * (kMaxLogDepth - depth)));
123 }
124
125 inline int32_t getPrefix(int32_t depth) const {
126 if (depth == 0) return 0;
127 return getPath(depth - 1);
128 }
129
130 inline int32_t getField() const {
131 return mField;
132 }
133
134 inline int32_t getRawPosAtDepth(int32_t depth) const {
135 int32_t field = (mField & 0x00ffffff);
136 int32_t shift = 8 * (kMaxLogDepth - depth);
137 int32_t mask = 0xff << shift;
138
139 return (field & mask) >> shift;
140 }
141
142 inline int32_t getPosAtDepth(int32_t depth) const {
143 return getRawPosAtDepth(depth) & kClearLastBitDeco;
144 }
145
146 // Check if the first bit of the 8-bit segment for depth is 1
147 inline bool isLastPos(int32_t depth) const {
148 int32_t field = (mField & 0x00ffffff);
149 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
150 return (field & mask) != 0;
151 }
152
153 // if the 8-bit segment is all 0's
154 inline bool isAnyPosMatcher(int32_t depth) const {
155 return getDepth() >= depth && getRawPosAtDepth(depth) == 0;
156 }
157 // if the 8bit is 0x80 (1000 0000)
158 inline bool isLastPosMatcher(int32_t depth) const {
159 return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask;
160 }
161
162 inline bool operator==(const Field& that) const {
163 return mTag == that.getTag() && mField == that.getField();
164 };
165
166 inline bool operator!=(const Field& that) const {
167 return mTag != that.getTag() || mField != that.getField();
168 };
169
170 bool operator<(const Field& that) const {
171 if (mTag != that.getTag()) {
172 return mTag < that.getTag();
173 }
174
175 if (mField != that.getField()) {
176 return mField < that.getField();
177 }
178
179 return false;
180 }
181 bool matches(const Matcher& that) const;
182};
183
184/**
185 * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
186 *
187 * It contains all information needed to match one or more leaf node.
188 * All information is encoded in a Field(2 ints) and a bit mask(1 int).
189 *
190 * For example, to match the first/any/last uid field in attribution chain in Atom 10,
191 * we have the following FieldMatcher in statsd_config
192 * FieldMatcher {
193 * field:10
194 * FieldMatcher {
195 * field:1
196 * position: any/last/first
197 * FieldMatcher {
198 * field:1
199 * }
200 * }
201 * }
202 *
203 * We translate the FieldMatcher into a Field, and mask
204 * First: [Matcher Field] 0x02010101 [Mask]0xffff7fff
205 * Last: [Matcher Field] 0x02018001 [Mask]0xffff80ff
206 * Any: [Matcher Field] 0x02010001 [Mask]0xffff00ff
207 *
208 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
209 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
210 * equal. Nothing can beat the performance of this matching algorithm.
211 *
212 * TODO: ADD EXAMPLE HERE.
213 */
214struct Matcher {
215 Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
216
217 const Field mMatcher;
218 const int32_t mMask;
219
Yangster-mac53928882018-02-25 23:02:56 -0800220 inline const Field& getMatcher() const {
221 return mMatcher;
222 }
223
224 inline int32_t getMask() const {
225 return mMask;
226 }
227
Yao Chen8a8d16c2018-02-08 14:50:40 -0800228 bool hasAnyPositionMatcher(int* prefix) const {
229 if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(2) == 0) {
230 (*prefix) = mMatcher.getPrefix(2);
231 return true;
232 }
233 return false;
234 }
Yangster-mac53928882018-02-25 23:02:56 -0800235
236 inline bool operator!=(const Matcher& that) const {
237 return mMatcher != that.getMatcher() || mMask != that.getMask();
238 };
Yao Chen8a8d16c2018-02-08 14:50:40 -0800239};
240
241/**
242 * A wrapper for a union type to contain multiple types of values.
243 *
244 */
245struct Value {
Yangster-macf5204922018-02-23 13:08:03 -0800246 Value() : type(UNKNOWN) {}
247
Yao Chen8a8d16c2018-02-08 14:50:40 -0800248 Value(int32_t v) {
249 int_value = v;
250 type = INT;
251 }
252
253 Value(int64_t v) {
254 long_value = v;
255 type = LONG;
256 }
257
258 Value(float v) {
259 float_value = v;
260 type = FLOAT;
261 }
262
263 Value(const std::string& v) {
264 str_value = v;
265 type = STRING;
266 }
267
268 void setInt(int32_t v) {
269 int_value = v;
270 type = INT;
271 }
272
273 void setLong(int64_t v) {
274 long_value = v;
275 type = LONG;
276 }
277
278 union {
279 int32_t int_value;
280 int64_t long_value;
281 float float_value;
282 };
283 std::string str_value;
284
285 Type type;
286
287 std::string toString() const;
288
289 Type getType() const {
290 return type;
291 }
292
293 Value(const Value& from);
294
295 bool operator==(const Value& that) const;
296 bool operator!=(const Value& that) const;
297
298 bool operator<(const Value& that) const;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800299};
300
301/**
302 * Represents a log item, or a dimension item (They are essentially the same).
303 */
304struct FieldValue {
Yangster-macf5204922018-02-23 13:08:03 -0800305 FieldValue() {}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800306 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
307 }
308 bool operator==(const FieldValue& that) const {
309 return mField == that.mField && mValue == that.mValue;
310 }
311 bool operator!=(const FieldValue& that) const {
312 return mField != that.mField || mValue != that.mValue;
313 }
314 bool operator<(const FieldValue& that) const {
315 if (mField != that.mField) {
316 return mField < that.mField;
317 }
318
319 if (mValue != that.mValue) {
320 return mValue < that.mValue;
321 }
322
323 return false;
324 }
325
326 Field mField;
327 Value mValue;
328};
329
330bool isAttributionUidField(const FieldValue& value);
331
332void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
333
334bool isAttributionUidField(const Field& field, const Value& value);
335} // namespace statsd
336} // namespace os
337} // namespace android