blob: a545fc5718d01849550d5de41043405695cbb13d [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"
Muhammad Qureshic8e22662019-11-20 17:18:03 -080021#include "atoms_info.h"
Chenjie Yuc715b9e2018-10-19 07:52:12 -070022#include "math.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080023
24namespace android {
25namespace os {
26namespace statsd {
27
Yao Chen4c959cb2018-02-13 13:27:48 -080028int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth) {
29 int32_t field = 0;
30 for (int32_t i = 0; i <= depth; i++) {
31 int32_t shiftBits = 8 * (kMaxLogDepth - i);
32 field |= (pos[i] << shiftBits);
33 }
34
35 if (includeDepth) {
36 field |= (depth << 24);
37 }
38 return field;
39}
40
41int32_t encodeMatcherMask(int32_t mask[], int32_t depth) {
42 return getEncodedField(mask, depth, false) | 0xff000000;
43}
44
Yao Chen8a8d16c2018-02-08 14:50:40 -080045bool Field::matches(const Matcher& matcher) const {
46 if (mTag != matcher.mMatcher.getTag()) {
47 return false;
48 }
49 if ((mField & matcher.mMask) == matcher.mMatcher.getField()) {
50 return true;
51 }
52
Yangster-mace06cfd72018-03-10 23:22:59 -080053 if (matcher.hasAllPositionMatcher() &&
54 (mField & (matcher.mMask & kClearAllPositionMatcherMask)) == matcher.mMatcher.getField()) {
55 return true;
56 }
57
Yao Chen8a8d16c2018-02-08 14:50:40 -080058 return false;
Yao Chen4c959cb2018-02-13 13:27:48 -080059}
Yao Chen8a8d16c2018-02-08 14:50:40 -080060
61void translateFieldMatcher(int tag, const FieldMatcher& matcher, int depth, int* pos, int* mask,
62 std::vector<Matcher>* output) {
63 if (depth > kMaxLogDepth) {
64 ALOGE("depth > 2");
65 return;
66 }
67
68 pos[depth] = matcher.field();
69 mask[depth] = 0x7f;
70
71 if (matcher.has_position()) {
72 depth++;
73 if (depth > 2) {
74 return;
75 }
76 switch (matcher.position()) {
Yangster-mace06cfd72018-03-10 23:22:59 -080077 case Position::ALL:
78 pos[depth] = 0x00;
79 mask[depth] = 0x7f;
80 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -080081 case Position::ANY:
82 pos[depth] = 0;
83 mask[depth] = 0;
84 break;
85 case Position::FIRST:
86 pos[depth] = 1;
87 mask[depth] = 0x7f;
88 break;
89 case Position::LAST:
90 pos[depth] = 0x80;
91 mask[depth] = 0x80;
92 break;
93 case Position::POSITION_UNKNOWN:
94 pos[depth] = 0;
95 mask[depth] = 0;
96 break;
97 }
98 }
99
100 if (matcher.child_size() == 0) {
101 output->push_back(Matcher(Field(tag, pos, depth), encodeMatcherMask(mask, depth)));
Yao Chen8a8d16c2018-02-08 14:50:40 -0800102 } else {
103 for (const auto& child : matcher.child()) {
104 translateFieldMatcher(tag, child, depth + 1, pos, mask, output);
105 }
106 }
107}
108
109void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output) {
110 int pos[] = {1, 1, 1};
111 int mask[] = {0x7f, 0x7f, 0x7f};
112 int tag = matcher.field();
113 for (const auto& child : matcher.child()) {
114 translateFieldMatcher(tag, child, 0, pos, mask, output);
115 }
116}
117
118bool isAttributionUidField(const FieldValue& value) {
Rafal Slawik390692b2019-09-18 12:40:17 +0100119 return isAttributionUidField(value.mField, value.mValue);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800120}
121
Yao Chen4ce07292019-02-13 13:06:36 -0800122int32_t getUidIfExists(const FieldValue& value) {
Yao Chen4ce07292019-02-13 13:06:36 -0800123 // the field is uid field if the field is the uid field in attribution node or marked as
124 // is_uid in atoms.proto
Rafal Slawik390692b2019-09-18 12:40:17 +0100125 bool isUid = isAttributionUidField(value) || isUidField(value.mField, value.mValue);
Yao Chen4ce07292019-02-13 13:06:36 -0800126 return isUid ? value.mValue.int_value : -1;
127}
128
Yao Chen8a8d16c2018-02-08 14:50:40 -0800129bool isAttributionUidField(const Field& field, const Value& value) {
130 int f = field.getField() & 0xff007f;
131 if (f == 0x10001 && value.getType() == INT) {
132 return true;
133 }
134 return false;
135}
136
tsaichristine7a57b8e2019-06-24 18:25:38 -0700137bool isUidField(const Field& field, const Value& value) {
138 auto it = android::util::AtomsInfo::kAtomsWithUidField.find(field.getTag());
139
140 if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) {
Rafal Slawik390692b2019-09-18 12:40:17 +0100141 int uidField = it->second; // uidField is the field number in proto
tsaichristine7a57b8e2019-06-24 18:25:38 -0700142 return field.getDepth() == 0 && field.getPosAtDepth(0) == uidField &&
143 value.getType() == INT;
144 }
145
146 return false;
147}
148
Yao Chen8a8d16c2018-02-08 14:50:40 -0800149Value::Value(const Value& from) {
150 type = from.getType();
151 switch (type) {
152 case INT:
153 int_value = from.int_value;
154 break;
155 case LONG:
156 long_value = from.long_value;
157 break;
158 case FLOAT:
159 float_value = from.float_value;
160 break;
Chenjie Yua0f02242018-07-06 16:14:34 -0700161 case DOUBLE:
162 double_value = from.double_value;
163 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800164 case STRING:
165 str_value = from.str_value;
166 break;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700167 case STORAGE:
168 storage_value = from.storage_value;
169 break;
Yangster-macf5204922018-02-23 13:08:03 -0800170 default:
171 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800172 }
173}
174
175std::string Value::toString() const {
176 switch (type) {
177 case INT:
178 return std::to_string(int_value) + "[I]";
179 case LONG:
180 return std::to_string(long_value) + "[L]";
181 case FLOAT:
182 return std::to_string(float_value) + "[F]";
Chenjie Yua0f02242018-07-06 16:14:34 -0700183 case DOUBLE:
184 return std::to_string(double_value) + "[D]";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800185 case STRING:
186 return str_value + "[S]";
Chenjie Yu12e5e672018-09-14 15:54:59 -0700187 case STORAGE:
188 return "bytes of size " + std::to_string(storage_value.size()) + "[ST]";
Yangster-macf5204922018-02-23 13:08:03 -0800189 default:
190 return "[UNKNOWN]";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800191 }
192}
193
Chenjie Yuc715b9e2018-10-19 07:52:12 -0700194bool Value::isZero() const {
195 switch (type) {
196 case INT:
197 return int_value == 0;
198 case LONG:
199 return long_value == 0;
200 case FLOAT:
201 return fabs(float_value) <= std::numeric_limits<float>::epsilon();
202 case DOUBLE:
203 return fabs(double_value) <= std::numeric_limits<double>::epsilon();
204 case STRING:
205 return str_value.size() == 0;
206 case STORAGE:
207 return storage_value.size() == 0;
208 default:
209 return false;
210 }
211}
212
Yao Chen8a8d16c2018-02-08 14:50:40 -0800213bool Value::operator==(const Value& that) const {
214 if (type != that.getType()) return false;
215
216 switch (type) {
217 case INT:
218 return int_value == that.int_value;
219 case LONG:
220 return long_value == that.long_value;
221 case FLOAT:
222 return float_value == that.float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700223 case DOUBLE:
224 return double_value == that.double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800225 case STRING:
226 return str_value == that.str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700227 case STORAGE:
228 return storage_value == that.storage_value;
Yangster-macf5204922018-02-23 13:08:03 -0800229 default:
230 return false;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800231 }
232}
233
234bool Value::operator!=(const Value& that) const {
235 if (type != that.getType()) return true;
236 switch (type) {
237 case INT:
238 return int_value != that.int_value;
239 case LONG:
240 return long_value != that.long_value;
241 case FLOAT:
242 return float_value != that.float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700243 case DOUBLE:
244 return double_value != that.double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800245 case STRING:
246 return str_value != that.str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700247 case STORAGE:
248 return storage_value != that.storage_value;
Yangster-macf5204922018-02-23 13:08:03 -0800249 default:
250 return false;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800251 }
252}
253
254bool Value::operator<(const Value& that) const {
255 if (type != that.getType()) return type < that.getType();
256
257 switch (type) {
258 case INT:
259 return int_value < that.int_value;
260 case LONG:
261 return long_value < that.long_value;
262 case FLOAT:
263 return float_value < that.float_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700264 case DOUBLE:
265 return double_value < that.double_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800266 case STRING:
267 return str_value < that.str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700268 case STORAGE:
269 return storage_value < that.storage_value;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800270 default:
271 return false;
272 }
273}
274
Chenjie Yua0f02242018-07-06 16:14:34 -0700275bool Value::operator>(const Value& that) const {
276 if (type != that.getType()) return type > that.getType();
277
278 switch (type) {
279 case INT:
280 return int_value > that.int_value;
281 case LONG:
282 return long_value > that.long_value;
283 case FLOAT:
284 return float_value > that.float_value;
285 case DOUBLE:
286 return double_value > that.double_value;
287 case STRING:
288 return str_value > that.str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700289 case STORAGE:
290 return storage_value > that.storage_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700291 default:
292 return false;
293 }
294}
295
296bool Value::operator>=(const Value& that) const {
297 if (type != that.getType()) return type >= that.getType();
298
299 switch (type) {
300 case INT:
301 return int_value >= that.int_value;
302 case LONG:
303 return long_value >= that.long_value;
304 case FLOAT:
305 return float_value >= that.float_value;
306 case DOUBLE:
307 return double_value >= that.double_value;
308 case STRING:
309 return str_value >= that.str_value;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700310 case STORAGE:
311 return storage_value >= that.storage_value;
Chenjie Yua0f02242018-07-06 16:14:34 -0700312 default:
313 return false;
314 }
315}
316
317Value Value::operator-(const Value& that) const {
318 Value v;
319 if (type != that.type) {
320 ALOGE("Can't operate on different value types, %d, %d", type, that.type);
321 return v;
322 }
323 if (type == STRING) {
324 ALOGE("Can't operate on string value type");
325 return v;
326 }
327
Chenjie Yu12e5e672018-09-14 15:54:59 -0700328 if (type == STORAGE) {
329 ALOGE("Can't operate on storage value type");
330 return v;
331 }
332
Chenjie Yua0f02242018-07-06 16:14:34 -0700333 switch (type) {
334 case INT:
335 v.setInt(int_value - that.int_value);
336 break;
337 case LONG:
338 v.setLong(long_value - that.long_value);
339 break;
340 case FLOAT:
341 v.setFloat(float_value - that.float_value);
342 break;
343 case DOUBLE:
344 v.setDouble(double_value - that.double_value);
345 break;
346 default:
347 break;
348 }
349 return v;
350}
351
352Value& Value::operator=(const Value& that) {
353 type = that.type;
354 switch (type) {
355 case INT:
356 int_value = that.int_value;
357 break;
358 case LONG:
359 long_value = that.long_value;
360 break;
361 case FLOAT:
362 float_value = that.float_value;
363 break;
364 case DOUBLE:
365 double_value = that.double_value;
366 break;
367 case STRING:
368 str_value = that.str_value;
369 break;
Chenjie Yu12e5e672018-09-14 15:54:59 -0700370 case STORAGE:
371 storage_value = that.storage_value;
372 break;
Chenjie Yua0f02242018-07-06 16:14:34 -0700373 default:
374 break;
375 }
376 return *this;
377}
378
379Value& Value::operator+=(const Value& that) {
380 if (type != that.type) {
381 ALOGE("Can't operate on different value types, %d, %d", type, that.type);
382 return *this;
383 }
384 if (type == STRING) {
385 ALOGE("Can't operate on string value type");
386 return *this;
387 }
Chenjie Yu12e5e672018-09-14 15:54:59 -0700388 if (type == STORAGE) {
389 ALOGE("Can't operate on storage value type");
390 return *this;
391 }
Chenjie Yua0f02242018-07-06 16:14:34 -0700392
393 switch (type) {
394 case INT:
395 int_value += that.int_value;
396 break;
397 case LONG:
398 long_value += that.long_value;
399 break;
400 case FLOAT:
401 float_value += that.float_value;
402 break;
403 case DOUBLE:
404 double_value += that.double_value;
405 break;
406 default:
407 break;
408 }
409 return *this;
410}
411
412double Value::getDouble() const {
413 switch (type) {
414 case INT:
415 return int_value;
416 case LONG:
417 return long_value;
418 case FLOAT:
419 return float_value;
420 case DOUBLE:
421 return double_value;
422 default:
423 return 0;
424 }
425}
426
Yangster13fb7e42018-03-07 17:30:49 -0800427bool equalDimensions(const std::vector<Matcher>& dimension_a,
428 const std::vector<Matcher>& dimension_b) {
429 bool eq = dimension_a.size() == dimension_b.size();
430 for (size_t i = 0; eq && i < dimension_a.size(); ++i) {
431 if (dimension_b[i] != dimension_a[i]) {
432 eq = false;
433 }
434 }
435 return eq;
436}
437
438bool HasPositionANY(const FieldMatcher& matcher) {
439 if (matcher.has_position() && matcher.position() == Position::ANY) {
440 return true;
441 }
442 for (const auto& child : matcher.child()) {
443 if (HasPositionANY(child)) {
444 return true;
445 }
446 }
447 return false;
448}
449
Yangster-mac9def8e32018-04-17 13:55:51 -0700450bool HasPositionALL(const FieldMatcher& matcher) {
451 if (matcher.has_position() && matcher.position() == Position::ALL) {
452 return true;
453 }
454 for (const auto& child : matcher.child()) {
455 if (HasPositionALL(child)) {
456 return true;
457 }
458 }
459 return false;
460}
461
Yao Chen8a8d16c2018-02-08 14:50:40 -0800462} // namespace statsd
463} // namespace os
tsaichristine7a57b8e2019-06-24 18:25:38 -0700464} // namespace android