blob: d17dded8d69108ee531330fc15a5326306ee543b [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
34enum Type { INT, LONG, FLOAT, STRING };
35
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:
85 Field(int32_t tag, int32_t pos[], int32_t depth) : mTag(tag) {
86 mField = getEncodedField(pos, depth, true);
87 }
88
89 Field(const Field& from) : mTag(from.getTag()), mField(from.getField()) {
90 }
91
92 Field(int32_t tag, int32_t field) : mTag(tag), mField(field){};
93
94 inline void setField(int32_t field) {
95 mField = field;
96 }
97
98 inline void setTag(int32_t tag) {
99 mTag = tag;
100 }
101
102 inline void decorateLastPos(int32_t depth) {
103 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
104 mField |= mask;
105 }
106
107 inline int32_t getTag() const {
108 return mTag;
109 }
110
111 inline int32_t getDepth() const {
112 return (mField >> 24);
113 }
114
115 inline int32_t getPath(int32_t depth) const {
116 if (depth > 2 || depth < 0) return 0;
117
118 int32_t field = (mField & 0x00ffffff);
119 int32_t mask = 0xffffffff;
120 return (field & (mask << 8 * (kMaxLogDepth - depth)));
121 }
122
123 inline int32_t getPrefix(int32_t depth) const {
124 if (depth == 0) return 0;
125 return getPath(depth - 1);
126 }
127
128 inline int32_t getField() const {
129 return mField;
130 }
131
132 inline int32_t getRawPosAtDepth(int32_t depth) const {
133 int32_t field = (mField & 0x00ffffff);
134 int32_t shift = 8 * (kMaxLogDepth - depth);
135 int32_t mask = 0xff << shift;
136
137 return (field & mask) >> shift;
138 }
139
140 inline int32_t getPosAtDepth(int32_t depth) const {
141 return getRawPosAtDepth(depth) & kClearLastBitDeco;
142 }
143
144 // Check if the first bit of the 8-bit segment for depth is 1
145 inline bool isLastPos(int32_t depth) const {
146 int32_t field = (mField & 0x00ffffff);
147 int32_t mask = kLastBitMask << 8 * (kMaxLogDepth - depth);
148 return (field & mask) != 0;
149 }
150
151 // if the 8-bit segment is all 0's
152 inline bool isAnyPosMatcher(int32_t depth) const {
153 return getDepth() >= depth && getRawPosAtDepth(depth) == 0;
154 }
155 // if the 8bit is 0x80 (1000 0000)
156 inline bool isLastPosMatcher(int32_t depth) const {
157 return getDepth() >= depth && getRawPosAtDepth(depth) == kLastBitMask;
158 }
159
160 inline bool operator==(const Field& that) const {
161 return mTag == that.getTag() && mField == that.getField();
162 };
163
164 inline bool operator!=(const Field& that) const {
165 return mTag != that.getTag() || mField != that.getField();
166 };
167
168 bool operator<(const Field& that) const {
169 if (mTag != that.getTag()) {
170 return mTag < that.getTag();
171 }
172
173 if (mField != that.getField()) {
174 return mField < that.getField();
175 }
176
177 return false;
178 }
179 bool matches(const Matcher& that) const;
180};
181
182/**
183 * Matcher represents a leaf matcher in the FieldMatcher in statsd_config.
184 *
185 * It contains all information needed to match one or more leaf node.
186 * All information is encoded in a Field(2 ints) and a bit mask(1 int).
187 *
188 * For example, to match the first/any/last uid field in attribution chain in Atom 10,
189 * we have the following FieldMatcher in statsd_config
190 * FieldMatcher {
191 * field:10
192 * FieldMatcher {
193 * field:1
194 * position: any/last/first
195 * FieldMatcher {
196 * field:1
197 * }
198 * }
199 * }
200 *
201 * We translate the FieldMatcher into a Field, and mask
202 * First: [Matcher Field] 0x02010101 [Mask]0xffff7fff
203 * Last: [Matcher Field] 0x02018001 [Mask]0xffff80ff
204 * Any: [Matcher Field] 0x02010001 [Mask]0xffff00ff
205 *
206 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
207 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
208 * equal. Nothing can beat the performance of this matching algorithm.
209 *
210 * TODO: ADD EXAMPLE HERE.
211 */
212struct Matcher {
213 Matcher(const Field& matcher, int32_t mask) : mMatcher(matcher), mMask(mask){};
214
215 const Field mMatcher;
216 const int32_t mMask;
217
218 bool hasAnyPositionMatcher(int* prefix) const {
219 if (mMatcher.getDepth() == 2 && mMatcher.getRawPosAtDepth(2) == 0) {
220 (*prefix) = mMatcher.getPrefix(2);
221 return true;
222 }
223 return false;
224 }
225};
226
227/**
228 * A wrapper for a union type to contain multiple types of values.
229 *
230 */
231struct Value {
232 Value(int32_t v) {
233 int_value = v;
234 type = INT;
235 }
236
237 Value(int64_t v) {
238 long_value = v;
239 type = LONG;
240 }
241
242 Value(float v) {
243 float_value = v;
244 type = FLOAT;
245 }
246
247 Value(const std::string& v) {
248 str_value = v;
249 type = STRING;
250 }
251
252 void setInt(int32_t v) {
253 int_value = v;
254 type = INT;
255 }
256
257 void setLong(int64_t v) {
258 long_value = v;
259 type = LONG;
260 }
261
262 union {
263 int32_t int_value;
264 int64_t long_value;
265 float float_value;
266 };
267 std::string str_value;
268
269 Type type;
270
271 std::string toString() const;
272
273 Type getType() const {
274 return type;
275 }
276
277 Value(const Value& from);
278
279 bool operator==(const Value& that) const;
280 bool operator!=(const Value& that) const;
281
282 bool operator<(const Value& that) const;
283
284private:
285 Value(){};
286};
287
288/**
289 * Represents a log item, or a dimension item (They are essentially the same).
290 */
291struct FieldValue {
292 FieldValue(const Field& field, const Value& value) : mField(field), mValue(value) {
293 }
294 bool operator==(const FieldValue& that) const {
295 return mField == that.mField && mValue == that.mValue;
296 }
297 bool operator!=(const FieldValue& that) const {
298 return mField != that.mField || mValue != that.mValue;
299 }
300 bool operator<(const FieldValue& that) const {
301 if (mField != that.mField) {
302 return mField < that.mField;
303 }
304
305 if (mValue != that.mValue) {
306 return mValue < that.mValue;
307 }
308
309 return false;
310 }
311
312 Field mField;
313 Value mValue;
314};
315
316bool isAttributionUidField(const FieldValue& value);
317
318void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output);
319
320bool isAttributionUidField(const Field& field, const Value& value);
321} // namespace statsd
322} // namespace os
323} // namespace android