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 #include <google/protobuf/descriptor.h> 21 #include <stdint.h> 22 23 #include <map> 24 #include <set> 25 #include <vector> 26 27 #include "frameworks/proto_logging/stats/atom_field_options.pb.h" 28 29 namespace android { 30 namespace stats_log_api_gen { 31 32 using google::protobuf::OneofDescriptor; 33 using google::protobuf::Descriptor; 34 using google::protobuf::FieldDescriptor; 35 using std::map; 36 using std::set; 37 using std::shared_ptr; 38 using std::string; 39 using std::vector; 40 41 const int PULL_ATOM_START_ID = 10000; 42 43 const int FIRST_UID_IN_CHAIN_ID = 0; 44 45 /** 46 * The types of oneof atoms. 47 * 48 * `OneofDescriptor::name()` returns the name of the oneof. 49 */ 50 const char ONEOF_PUSHED_ATOM_NAME[] = "pushed"; 51 const char ONEOF_PULLED_ATOM_NAME[] = "pulled"; 52 53 enum AnnotationId : uint8_t { 54 ANNOTATION_ID_IS_UID = 1, 55 ANNOTATION_ID_TRUNCATE_TIMESTAMP = 2, 56 ANNOTATION_ID_PRIMARY_FIELD = 3, 57 ANNOTATION_ID_EXCLUSIVE_STATE = 4, 58 ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID = 5, 59 ANNOTATION_ID_DEFAULT_STATE = 6, 60 ANNOTATION_ID_TRIGGER_STATE_RESET = 7, 61 ANNOTATION_ID_STATE_NESTED = 8, 62 }; 63 64 const int ATOM_ID_FIELD_NUMBER = -1; 65 66 const char DEFAULT_MODULE_NAME[] = "DEFAULT"; 67 68 /** 69 * The types for atom parameters. 70 */ 71 typedef enum { 72 JAVA_TYPE_UNKNOWN = 0, 73 74 JAVA_TYPE_ATTRIBUTION_CHAIN = 1, 75 JAVA_TYPE_BOOLEAN = 2, 76 JAVA_TYPE_INT = 3, 77 JAVA_TYPE_LONG = 4, 78 JAVA_TYPE_FLOAT = 5, 79 JAVA_TYPE_DOUBLE = 6, 80 JAVA_TYPE_STRING = 7, 81 JAVA_TYPE_ENUM = 8, 82 JAVA_TYPE_KEY_VALUE_PAIR = 9, 83 84 JAVA_TYPE_OBJECT = -1, 85 JAVA_TYPE_BYTE_ARRAY = -2, 86 } java_type_t; 87 88 enum AnnotationType { 89 ANNOTATION_TYPE_UNKNOWN = 0, 90 ANNOTATION_TYPE_INT = 1, 91 ANNOTATION_TYPE_BOOL = 2, 92 }; 93 94 union AnnotationValue { 95 int intValue; 96 bool boolValue; 97 AnnotationValue(const int value)98 explicit AnnotationValue(const int value) : intValue(value) { 99 } AnnotationValue(const bool value)100 explicit AnnotationValue(const bool value) : boolValue(value) { 101 } 102 }; 103 104 struct Annotation { 105 const AnnotationId annotationId; 106 const int atomId; 107 AnnotationType type; 108 AnnotationValue value; 109 AnnotationAnnotation110 inline Annotation(AnnotationId annotationId, int atomId, AnnotationType type, 111 AnnotationValue value) 112 : annotationId(annotationId), atomId(atomId), type(type), value(value) { 113 } ~AnnotationAnnotation114 inline ~Annotation() { 115 } 116 117 inline bool operator<(const Annotation& that) const { 118 return atomId == that.atomId ? annotationId < that.annotationId : atomId < that.atomId; 119 } 120 }; 121 122 struct SharedComparator { 123 template <typename T> operatorSharedComparator124 inline bool operator()(const shared_ptr<T>& lhs, const shared_ptr<T>& rhs) const { 125 return (*lhs) < (*rhs); 126 } 127 }; 128 129 using AnnotationSet = set<shared_ptr<Annotation>, SharedComparator>; 130 131 using FieldNumberToAnnotations = map<int, AnnotationSet>; 132 133 /** 134 * The name and type for an atom field. 135 */ 136 struct AtomField { 137 string name; 138 java_type_t javaType; 139 140 // If the field is of type enum, the following map contains the list of enum 141 // values. 142 map<int /* numeric value */, string /* value name */> enumValues; 143 AtomFieldAtomField144 inline AtomField() : name(), javaType(JAVA_TYPE_UNKNOWN) { 145 } AtomFieldAtomField146 inline AtomField(const AtomField& that) 147 : name(that.name), javaType(that.javaType), enumValues(that.enumValues) { 148 } 149 AtomFieldAtomField150 inline AtomField(string n, java_type_t jt) : name(n), javaType(jt) { 151 } ~AtomFieldAtomField152 inline ~AtomField() { 153 } 154 }; 155 156 /** 157 * The name and code for an atom. 158 */ 159 struct AtomDecl { 160 int code; 161 string name; 162 163 string message; 164 vector<AtomField> fields; 165 166 string oneOfName; 167 168 FieldNumberToAnnotations fieldNumberToAnnotations; 169 170 vector<int> primaryFields; 171 int exclusiveField = 0; 172 int defaultState = INT_MAX; 173 int triggerStateReset = INT_MAX; 174 bool nested = true; 175 176 int uidField = 0; 177 178 AtomDecl(); 179 AtomDecl(const AtomDecl& that); 180 AtomDecl(int code, const string& name, const string& message, const string& oneOfName); 181 ~AtomDecl(); 182 183 inline bool operator<(const AtomDecl& that) const { 184 return (code == that.code) ? (name < that.name) : (code < that.code); 185 } 186 }; 187 188 using AtomDeclSet = set<shared_ptr<AtomDecl>, SharedComparator>; 189 190 // Maps a field number to a set of atoms that have annotation(s) for their field with that field 191 // number. 192 using FieldNumberToAtomDeclSet = map<int, AtomDeclSet>; 193 194 using SignatureInfoMap = map<vector<java_type_t>, FieldNumberToAtomDeclSet>; 195 196 struct Atoms { 197 SignatureInfoMap signatureInfoMap; 198 SignatureInfoMap pulledAtomsSignatureInfoMap; 199 AtomDeclSet decls; 200 AtomDeclSet non_chained_decls; 201 SignatureInfoMap nonChainedSignatureInfoMap; 202 }; 203 204 /** 205 * Gather the information about the atoms. Returns the number of errors. 206 */ 207 int collate_atoms(const Descriptor* descriptor, const string& moduleName, Atoms* atoms); 208 int collate_atom(const Descriptor* atom, AtomDecl* atomDecl, vector<java_type_t>* signature); 209 210 } // namespace stats_log_api_gen 211 } // namespace android 212 213 #endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H 214