Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame^] | 26 | int32_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 | |
| 39 | int32_t encodeMatcherMask(int32_t mask[], int32_t depth) { |
| 40 | return getEncodedField(mask, depth, false) | 0xff000000; |
| 41 | } |
| 42 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 43 | bool 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 | |
| 51 | return false; |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame^] | 52 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 53 | |
| 54 | void translateFieldMatcher(int tag, const FieldMatcher& matcher, int depth, int* pos, int* mask, |
| 55 | std::vector<Matcher>* output) { |
| 56 | if (depth > kMaxLogDepth) { |
| 57 | ALOGE("depth > 2"); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | pos[depth] = matcher.field(); |
| 62 | mask[depth] = 0x7f; |
| 63 | |
| 64 | if (matcher.has_position()) { |
| 65 | depth++; |
| 66 | if (depth > 2) { |
| 67 | return; |
| 68 | } |
| 69 | switch (matcher.position()) { |
| 70 | case Position::ANY: |
| 71 | pos[depth] = 0; |
| 72 | mask[depth] = 0; |
| 73 | break; |
| 74 | case Position::FIRST: |
| 75 | pos[depth] = 1; |
| 76 | mask[depth] = 0x7f; |
| 77 | break; |
| 78 | case Position::LAST: |
| 79 | pos[depth] = 0x80; |
| 80 | mask[depth] = 0x80; |
| 81 | break; |
| 82 | case Position::POSITION_UNKNOWN: |
| 83 | pos[depth] = 0; |
| 84 | mask[depth] = 0; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (matcher.child_size() == 0) { |
| 90 | output->push_back(Matcher(Field(tag, pos, depth), encodeMatcherMask(mask, depth))); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 91 | } else { |
| 92 | for (const auto& child : matcher.child()) { |
| 93 | translateFieldMatcher(tag, child, depth + 1, pos, mask, output); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output) { |
| 99 | int pos[] = {1, 1, 1}; |
| 100 | int mask[] = {0x7f, 0x7f, 0x7f}; |
| 101 | int tag = matcher.field(); |
| 102 | for (const auto& child : matcher.child()) { |
| 103 | translateFieldMatcher(tag, child, 0, pos, mask, output); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool isAttributionUidField(const FieldValue& value) { |
| 108 | int field = value.mField.getField() & 0xff007f; |
| 109 | if (field == 0x10001 && value.mValue.getType() == INT) { |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool isAttributionUidField(const Field& field, const Value& value) { |
| 116 | int f = field.getField() & 0xff007f; |
| 117 | if (f == 0x10001 && value.getType() == INT) { |
| 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | Value::Value(const Value& from) { |
| 124 | type = from.getType(); |
| 125 | switch (type) { |
| 126 | case INT: |
| 127 | int_value = from.int_value; |
| 128 | break; |
| 129 | case LONG: |
| 130 | long_value = from.long_value; |
| 131 | break; |
| 132 | case FLOAT: |
| 133 | float_value = from.float_value; |
| 134 | break; |
| 135 | case STRING: |
| 136 | str_value = from.str_value; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | std::string Value::toString() const { |
| 142 | switch (type) { |
| 143 | case INT: |
| 144 | return std::to_string(int_value) + "[I]"; |
| 145 | case LONG: |
| 146 | return std::to_string(long_value) + "[L]"; |
| 147 | case FLOAT: |
| 148 | return std::to_string(float_value) + "[F]"; |
| 149 | case STRING: |
| 150 | return str_value + "[S]"; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | bool Value::operator==(const Value& that) const { |
| 155 | if (type != that.getType()) return false; |
| 156 | |
| 157 | switch (type) { |
| 158 | case INT: |
| 159 | return int_value == that.int_value; |
| 160 | case LONG: |
| 161 | return long_value == that.long_value; |
| 162 | case FLOAT: |
| 163 | return float_value == that.float_value; |
| 164 | case STRING: |
| 165 | return str_value == that.str_value; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | bool Value::operator!=(const Value& that) const { |
| 170 | if (type != that.getType()) return true; |
| 171 | switch (type) { |
| 172 | case INT: |
| 173 | return int_value != that.int_value; |
| 174 | case LONG: |
| 175 | return long_value != that.long_value; |
| 176 | case FLOAT: |
| 177 | return float_value != that.float_value; |
| 178 | case STRING: |
| 179 | return str_value != that.str_value; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | bool Value::operator<(const Value& that) const { |
| 184 | if (type != that.getType()) return type < that.getType(); |
| 185 | |
| 186 | switch (type) { |
| 187 | case INT: |
| 188 | return int_value < that.int_value; |
| 189 | case LONG: |
| 190 | return long_value < that.long_value; |
| 191 | case FLOAT: |
| 192 | return float_value < that.float_value; |
| 193 | case STRING: |
| 194 | return str_value < that.str_value; |
| 195 | default: |
| 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } // namespace statsd |
| 201 | } // namespace os |
| 202 | } // namespace android |