blob: 7b6d29b905bdd9c4e05d6c02fb60e161722ea06d [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
17#define DEBUG false
18#include "Log.h"
19#include "FieldValue.h"
20#include "HashableDimensionKey.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
Yao Chen4c959cb2018-02-13 13:27:48 -080026int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth) {
27 int32_t field = 0;
28 for (int32_t i = 0; i <= depth; i++) {
29 int32_t shiftBits = 8 * (kMaxLogDepth - i);
30 field |= (pos[i] << shiftBits);
31 }
32
33 if (includeDepth) {
34 field |= (depth << 24);
35 }
36 return field;
37}
38
39int32_t encodeMatcherMask(int32_t mask[], int32_t depth) {
40 return getEncodedField(mask, depth, false) | 0xff000000;
41}
42
Yao Chen8a8d16c2018-02-08 14:50:40 -080043bool Field::matches(const Matcher& matcher) const {
44 if (mTag != matcher.mMatcher.getTag()) {
45 return false;
46 }
47 if ((mField & matcher.mMask) == matcher.mMatcher.getField()) {
48 return true;
49 }
50
Yangster-mace06cfd72018-03-10 23:22:59 -080051 if (matcher.hasAllPositionMatcher() &&
52 (mField & (matcher.mMask & kClearAllPositionMatcherMask)) == matcher.mMatcher.getField()) {
53 return true;
54 }
55
Yao Chen8a8d16c2018-02-08 14:50:40 -080056 return false;
Yao Chen4c959cb2018-02-13 13:27:48 -080057}
Yao Chen8a8d16c2018-02-08 14:50:40 -080058
59void translateFieldMatcher(int tag, const FieldMatcher& matcher, int depth, int* pos, int* mask,
60 std::vector<Matcher>* output) {
61 if (depth > kMaxLogDepth) {
62 ALOGE("depth > 2");
63 return;
64 }
65
66 pos[depth] = matcher.field();
67 mask[depth] = 0x7f;
68
69 if (matcher.has_position()) {
70 depth++;
71 if (depth > 2) {
72 return;
73 }
74 switch (matcher.position()) {
Yangster-mace06cfd72018-03-10 23:22:59 -080075 case Position::ALL:
76 pos[depth] = 0x00;
77 mask[depth] = 0x7f;
78 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -080079 case Position::ANY:
80 pos[depth] = 0;
81 mask[depth] = 0;
82 break;
83 case Position::FIRST:
84 pos[depth] = 1;
85 mask[depth] = 0x7f;
86 break;
87 case Position::LAST:
88 pos[depth] = 0x80;
89 mask[depth] = 0x80;
90 break;
91 case Position::POSITION_UNKNOWN:
92 pos[depth] = 0;
93 mask[depth] = 0;
94 break;
95 }
96 }
97
98 if (matcher.child_size() == 0) {
99 output->push_back(Matcher(Field(tag, pos, depth), encodeMatcherMask(mask, depth)));
Yao Chen8a8d16c2018-02-08 14:50:40 -0800100 } else {
101 for (const auto& child : matcher.child()) {
102 translateFieldMatcher(tag, child, depth + 1, pos, mask, output);
103 }
104 }
105}
106
107void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output) {
108 int pos[] = {1, 1, 1};
109 int mask[] = {0x7f, 0x7f, 0x7f};
110 int tag = matcher.field();
111 for (const auto& child : matcher.child()) {
112 translateFieldMatcher(tag, child, 0, pos, mask, output);
113 }
114}
115
116bool isAttributionUidField(const FieldValue& value) {
117 int field = value.mField.getField() & 0xff007f;
118 if (field == 0x10001 && value.mValue.getType() == INT) {
119 return true;
120 }
121 return false;
122}
123
124bool isAttributionUidField(const Field& field, const Value& value) {
125 int f = field.getField() & 0xff007f;
126 if (f == 0x10001 && value.getType() == INT) {
127 return true;
128 }
129 return false;
130}
131
132Value::Value(const Value& from) {
133 type = from.getType();
134 switch (type) {
135 case INT:
136 int_value = from.int_value;
137 break;
138 case LONG:
139 long_value = from.long_value;
140 break;
141 case FLOAT:
142 float_value = from.float_value;
143 break;
Chenjie Yua0f02242018-07-06 16:14:34 -0700144 case DOUBLE:
145 double_value = from.double_value;
146 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800147 case STRING:
148 str_value = from.str_value;
149 break;
Yangster-macf5204922018-02-23 13:08:03 -0800150 default:
151 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800152 }
153}
154
155std::string Value::toString() const {
156 switch (type) {
157 case INT:
158 return std::to_string(int_value) + "[I]";
159 case LONG:
160 return std::to_string(long_value) + "[L]";
161 case FLOAT:
162 return std::to_string(float_value) + "[F]";
Chenjie Yua0f02242018-07-06 16:14:34 -0700163 case DOUBLE:
164 return std::to_string(double_value) + "[D]";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800165 case STRING:
166 return str_value + "[S]";
Yangster-macf5204922018-02-23 13:08:03 -0800167 default:
168 return "[UNKNOWN]";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800169 }
170}
171
172bool Value::operator==(const Value& that) const {
173 if (type != that.getType()) return false;
174
175 switch (type) {
176 case INT:
177 return int_value == that.int_value;
178 case LONG:
179 return long_value == that.long_value;
180 case FLOAT:
181 return float_value == that.float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700182 case DOUBLE:
183 return double_value == that.double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800184 case STRING:
185 return str_value == that.str_value;
Yangster-macf5204922018-02-23 13:08:03 -0800186 default:
187 return false;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800188 }
189}
190
191bool Value::operator!=(const Value& that) const {
192 if (type != that.getType()) return true;
193 switch (type) {
194 case INT:
195 return int_value != that.int_value;
196 case LONG:
197 return long_value != that.long_value;
198 case FLOAT:
199 return float_value != that.float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700200 case DOUBLE:
201 return double_value != that.double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800202 case STRING:
203 return str_value != that.str_value;
Yangster-macf5204922018-02-23 13:08:03 -0800204 default:
205 return false;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800206 }
207}
208
209bool Value::operator<(const Value& that) const {
210 if (type != that.getType()) return type < that.getType();
211
212 switch (type) {
213 case INT:
214 return int_value < that.int_value;
215 case LONG:
216 return long_value < that.long_value;
217 case FLOAT:
218 return float_value < that.float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700219 case DOUBLE:
220 return double_value < that.double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800221 case STRING:
222 return str_value < that.str_value;
223 default:
224 return false;
225 }
226}
227
Chenjie Yua0f02242018-07-06 16:14:34 -0700228bool Value::operator>(const Value& that) const {
229 if (type != that.getType()) return type > that.getType();
230
231 switch (type) {
232 case INT:
233 return int_value > that.int_value;
234 case LONG:
235 return long_value > that.long_value;
236 case FLOAT:
237 return float_value > that.float_value;
238 case DOUBLE:
239 return double_value > that.double_value;
240 case STRING:
241 return str_value > that.str_value;
242 default:
243 return false;
244 }
245}
246
247bool Value::operator>=(const Value& that) const {
248 if (type != that.getType()) return type >= that.getType();
249
250 switch (type) {
251 case INT:
252 return int_value >= that.int_value;
253 case LONG:
254 return long_value >= that.long_value;
255 case FLOAT:
256 return float_value >= that.float_value;
257 case DOUBLE:
258 return double_value >= that.double_value;
259 case STRING:
260 return str_value >= that.str_value;
261 default:
262 return false;
263 }
264}
265
266Value Value::operator-(const Value& that) const {
267 Value v;
268 if (type != that.type) {
269 ALOGE("Can't operate on different value types, %d, %d", type, that.type);
270 return v;
271 }
272 if (type == STRING) {
273 ALOGE("Can't operate on string value type");
274 return v;
275 }
276
277 switch (type) {
278 case INT:
279 v.setInt(int_value - that.int_value);
280 break;
281 case LONG:
282 v.setLong(long_value - that.long_value);
283 break;
284 case FLOAT:
285 v.setFloat(float_value - that.float_value);
286 break;
287 case DOUBLE:
288 v.setDouble(double_value - that.double_value);
289 break;
290 default:
291 break;
292 }
293 return v;
294}
295
296Value& Value::operator=(const Value& that) {
297 type = that.type;
298 switch (type) {
299 case INT:
300 int_value = that.int_value;
301 break;
302 case LONG:
303 long_value = that.long_value;
304 break;
305 case FLOAT:
306 float_value = that.float_value;
307 break;
308 case DOUBLE:
309 double_value = that.double_value;
310 break;
311 case STRING:
312 str_value = that.str_value;
313 break;
314 default:
315 break;
316 }
317 return *this;
318}
319
320Value& Value::operator+=(const Value& that) {
321 if (type != that.type) {
322 ALOGE("Can't operate on different value types, %d, %d", type, that.type);
323 return *this;
324 }
325 if (type == STRING) {
326 ALOGE("Can't operate on string value type");
327 return *this;
328 }
329
330 switch (type) {
331 case INT:
332 int_value += that.int_value;
333 break;
334 case LONG:
335 long_value += that.long_value;
336 break;
337 case FLOAT:
338 float_value += that.float_value;
339 break;
340 case DOUBLE:
341 double_value += that.double_value;
342 break;
343 default:
344 break;
345 }
346 return *this;
347}
348
349double Value::getDouble() const {
350 switch (type) {
351 case INT:
352 return int_value;
353 case LONG:
354 return long_value;
355 case FLOAT:
356 return float_value;
357 case DOUBLE:
358 return double_value;
359 default:
360 return 0;
361 }
362}
363
Yangster13fb7e42018-03-07 17:30:49 -0800364bool equalDimensions(const std::vector<Matcher>& dimension_a,
365 const std::vector<Matcher>& dimension_b) {
366 bool eq = dimension_a.size() == dimension_b.size();
367 for (size_t i = 0; eq && i < dimension_a.size(); ++i) {
368 if (dimension_b[i] != dimension_a[i]) {
369 eq = false;
370 }
371 }
372 return eq;
373}
374
375bool HasPositionANY(const FieldMatcher& matcher) {
376 if (matcher.has_position() && matcher.position() == Position::ANY) {
377 return true;
378 }
379 for (const auto& child : matcher.child()) {
380 if (HasPositionANY(child)) {
381 return true;
382 }
383 }
384 return false;
385}
386
Yangster-mac9def8e32018-04-17 13:55:51 -0700387bool HasPositionALL(const FieldMatcher& matcher) {
388 if (matcher.has_position() && matcher.position() == Position::ALL) {
389 return true;
390 }
391 for (const auto& child : matcher.child()) {
392 if (HasPositionALL(child)) {
393 return true;
394 }
395 }
396 return false;
397}
398
Yao Chen8a8d16c2018-02-08 14:50:40 -0800399} // namespace statsd
400} // namespace os
401} // namespace android