Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017, 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 | #ifndef ANDROID_STATS_LOG_API_GEN_COLLATION_H |
| 18 | #define ANDROID_STATS_LOG_API_GEN_COLLATION_H |
| 19 | |
| 20 | |
| 21 | #include <google/protobuf/descriptor.h> |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 22 | #include "frameworks/base/cmds/statsd/src/atom_field_options.pb.h" |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 23 | |
| 24 | #include <set> |
| 25 | #include <vector> |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 26 | #include <map> |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace stats_log_api_gen { |
| 30 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 31 | using std::map; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 32 | using std::set; |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 33 | using std::shared_ptr; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 34 | using std::string; |
| 35 | using std::vector; |
| 36 | using google::protobuf::Descriptor; |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 37 | using google::protobuf::FieldDescriptor; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 38 | |
Chenjie Yu | 159e4f8 | 2018-08-29 11:49:11 -0700 | [diff] [blame] | 39 | const int PULL_ATOM_START_ID = 10000; |
| 40 | |
tsaichristine | ed61564 | 2020-01-02 12:53:41 -0800 | [diff] [blame] | 41 | const int FIRST_UID_IN_CHAIN_ID = 0; |
| 42 | |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 43 | const unsigned char ANNOTATION_ID_IS_UID = 1; |
| 44 | const unsigned char ANNOTATION_ID_TRUNCATE_TIMESTAMP = 2; |
| 45 | const unsigned char ANNOTATION_ID_STATE_OPTION = 3; |
| 46 | const unsigned char ANNOTATION_ID_DEFAULT_STATE = 4; |
| 47 | const unsigned char ANNOTATION_ID_RESET_STATE = 5; |
| 48 | const unsigned char ANNOTATION_ID_STATE_NESTED = 6; |
| 49 | |
| 50 | const int STATE_OPTION_UNSET = os::statsd::StateField::STATE_FIELD_UNSET; |
| 51 | const int STATE_OPTION_EXCLUSIVE = os::statsd::StateField::EXCLUSIVE_STATE; |
| 52 | const int STATE_OPTION_PRIMARY_FIELD_FIRST_UID = os::statsd::StateField::PRIMARY_FIELD_FIRST_UID; |
| 53 | const int STATE_OPTION_PRIMARY = os::statsd::StateField::PRIMARY_FIELD; |
| 54 | |
| 55 | const string DEFAULT_MODULE_NAME = "DEFAULT"; |
| 56 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 57 | /** |
| 58 | * The types for atom parameters. |
| 59 | */ |
| 60 | typedef enum { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 61 | JAVA_TYPE_UNKNOWN = 0, |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 62 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 63 | JAVA_TYPE_ATTRIBUTION_CHAIN = 1, |
| 64 | JAVA_TYPE_BOOLEAN = 2, |
| 65 | JAVA_TYPE_INT = 3, |
| 66 | JAVA_TYPE_LONG = 4, |
| 67 | JAVA_TYPE_FLOAT = 5, |
| 68 | JAVA_TYPE_DOUBLE = 6, |
| 69 | JAVA_TYPE_STRING = 7, |
| 70 | JAVA_TYPE_ENUM = 8, |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 71 | JAVA_TYPE_KEY_VALUE_PAIR = 9, |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 72 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 73 | JAVA_TYPE_OBJECT = -1, |
| 74 | JAVA_TYPE_BYTE_ARRAY = -2, |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 75 | } java_type_t; |
| 76 | |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 77 | enum AnnotationType { |
| 78 | ANNOTATION_TYPE_UNKNOWN = 0, |
| 79 | ANNOTATION_TYPE_INT = 1, |
| 80 | ANNOTATION_TYPE_BOOL = 2, |
| 81 | }; |
| 82 | |
| 83 | union AnnotationValue { |
| 84 | int intValue; |
| 85 | bool boolValue; |
| 86 | |
| 87 | AnnotationValue(const int value): intValue(value) {} |
| 88 | AnnotationValue(const bool value): boolValue(value) {} |
| 89 | }; |
| 90 | |
| 91 | struct Annotation { |
| 92 | const unsigned char annotationId; |
| 93 | const int atomId; |
| 94 | AnnotationType type; |
| 95 | AnnotationValue value; |
| 96 | |
| 97 | inline Annotation(unsigned char annotationId, int atomId, AnnotationType type, |
| 98 | AnnotationValue value): |
| 99 | annotationId(annotationId), atomId(atomId), type(type), value(value) {} |
| 100 | inline ~Annotation() {} |
| 101 | |
| 102 | inline bool operator<(const Annotation& that) const { |
| 103 | return atomId == that.atomId ? annotationId < that.annotationId : atomId < that.atomId; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | using FieldNumberToAnnotations = map<int, set<shared_ptr<Annotation>>>; |
| 108 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 109 | /** |
| 110 | * The name and type for an atom field. |
| 111 | */ |
| 112 | struct AtomField { |
| 113 | string name; |
| 114 | java_type_t javaType; |
| 115 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 116 | // If the field is of type enum, the following map contains the list of enum values. |
| 117 | map<int /* numeric value */, string /* value name */> enumValues; |
| 118 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 119 | inline AtomField() :name(), javaType(JAVA_TYPE_UNKNOWN) {} |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 120 | inline AtomField(const AtomField& that) :name(that.name), |
| 121 | javaType(that.javaType), |
| 122 | enumValues(that.enumValues) {} |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 123 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 124 | inline AtomField(string n, java_type_t jt) :name(n), javaType(jt) {} |
| 125 | inline ~AtomField() {} |
| 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * The name and code for an atom. |
| 130 | */ |
| 131 | struct AtomDecl { |
| 132 | int code; |
| 133 | string name; |
| 134 | |
| 135 | string message; |
| 136 | vector<AtomField> fields; |
| 137 | |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 138 | FieldNumberToAnnotations fieldNumberToAnnotations; |
| 139 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 140 | vector<int> primaryFields; |
| 141 | int exclusiveField = 0; |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 142 | int defaultState = INT_MAX; |
| 143 | int resetState = INT_MAX; |
| 144 | bool nested; |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 145 | |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 146 | int uidField = 0; |
| 147 | |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 148 | bool whitelisted = false; |
| 149 | |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 150 | vector<int> binaryFields; |
| 151 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 152 | AtomDecl(); |
| 153 | AtomDecl(const AtomDecl& that); |
| 154 | AtomDecl(int code, const string& name, const string& message); |
| 155 | ~AtomDecl(); |
| 156 | |
| 157 | inline bool operator<(const AtomDecl& that) const { |
| 158 | return (code == that.code) ? (name < that.name) : (code < that.code); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | struct Atoms { |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 163 | map<vector<java_type_t>, FieldNumberToAnnotations> signatureInfoMap; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 164 | set<AtomDecl> decls; |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 165 | set<AtomDecl> non_chained_decls; |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 166 | map<vector<java_type_t>, FieldNumberToAnnotations> nonChainedSignatureInfoMap; |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 167 | int maxPushedAtomId; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Gather the information about the atoms. Returns the number of errors. |
| 172 | */ |
Muhammad Qureshi | b13a321 | 2020-03-12 07:37:13 -0700 | [diff] [blame^] | 173 | int collate_atoms(const Descriptor* descriptor, const string& moduleName, Atoms* atoms); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 174 | int collate_atom(const Descriptor *atom, AtomDecl *atomDecl, vector<java_type_t> *signature); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 175 | |
| 176 | } // namespace stats_log_api_gen |
| 177 | } // namespace android |
| 178 | |
| 179 | |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 180 | #endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H |