blob: 21f30e288c257b3c0a2c9d27e62bd5bdce69bf20 [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
220 bool hasAnyPositionMatcher(int* prefix) const {
221 if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(2) == 0) {
222 (*prefix) = mMatcher.getPrefix(2);
223 return true;
224 }
225 return false;
226 }
227};
228
229/**
230 * A wrapper for a union type to contain multiple types of values.
231 *
232 */
233struct Value {
Yangster-macf5204922018-02-23 13:08:03 -0800234 Value() : type(UNKNOWN) {}
235
Yao Chen8a8d16c2018-02-08 14:50:40 -0800236 Value(int32_t v) {
237 int_value = v;
238 type = INT;
239 }
240
241 Value(int64_t v) {
242 long_value = v;
243 type = LONG;
244 }
245
246 Value(float v) {
247 float_value = v;
248 type = FLOAT;
249 }
250
251 Value(const std::string& v) {
252 str_value = v;
253 type = STRING;
254 }
255
256 void setInt(int32_t v) {
257 int_value = v;
258 type = INT;
259 }
260
261 void setLong(int64_t v) {
262 long_value = v;
263 type = LONG;
264 }
265
266 union {
267 int32_t int_value;
268 int64_t long_value;
269 float float_value;
270 };
271 std::string str_value;
272
273 Type type;
274
275 std::string toString() const;
276
277 Type getType() const {
278 return type;
279 }
280
281 Value(const Value& from);
282
283 bool operator==(const Value& that) const;
284 bool operator!=(const Value& that) const;
285
286 bool operator<(const Value& that) const;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800287};
288
289/**
290 * Represents a log item, or a dimension item (They are essentially the same).
291 */
292struct FieldValue {
Yangster-macf5204922018-02-23 13:08:03 -0800293 FieldValue() {}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800294 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
295 }
296 bool operator==(const FieldValue& that) const {
297 return mField == that.mField && mValue == that.mValue;
298 }
299 bool operator!=(const FieldValue& that) const {
300 return mField != that.mField || mValue != that.mValue;
301 }
302 bool operator<(const FieldValue& that) const {
303 if (mField != that.mField) {
304 return mField < that.mField;
305 }
306
307 if (mValue != that.mValue) {
308 return mValue < that.mValue;
309 }
310
311 return false;
312 }
313
314 Field mField;
315 Value mValue;
316};
317
318bool isAttributionUidField(const FieldValue& value);
319
320void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
321
322bool isAttributionUidField(const Field& field, const Value& value);
323} // namespace statsd
324} // namespace os
325} // namespace android