blob: 10b34ecf5f54d61cbf478a785cf84d91f08c2e6b [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001/*
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
Yao Chend54f9dd2017-10-17 17:37:48 +000020#include <google/protobuf/descriptor.h>
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -070021#include <stdint.h>
Yao Chend54f9dd2017-10-17 17:37:48 +000022
Muhammad Qureshia345af92020-03-24 17:05:14 -070023#include <map>
Yao Chend54f9dd2017-10-17 17:37:48 +000024#include <set>
25#include <vector>
Muhammad Qureshia345af92020-03-24 17:05:14 -070026
27#include "frameworks/base/cmds/statsd/src/atom_field_options.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +000028
29namespace android {
30namespace stats_log_api_gen {
31
Muhammad Qureshia345af92020-03-24 17:05:14 -070032using google::protobuf::Descriptor;
33using google::protobuf::FieldDescriptor;
Stefan Lafon9478f352017-10-30 21:20:20 -070034using std::map;
Yao Chend54f9dd2017-10-17 17:37:48 +000035using std::set;
Muhammad Qureshib13a3212020-03-12 07:37:13 -070036using std::shared_ptr;
Yao Chend54f9dd2017-10-17 17:37:48 +000037using std::string;
38using std::vector;
Yao Chend54f9dd2017-10-17 17:37:48 +000039
Chenjie Yu159e4f82018-08-29 11:49:11 -070040const int PULL_ATOM_START_ID = 10000;
41
tsaichristineed615642020-01-02 12:53:41 -080042const int FIRST_UID_IN_CHAIN_ID = 0;
43
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -070044enum AnnotationId : uint8_t {
45 ANNOTATION_ID_IS_UID = 1,
46 ANNOTATION_ID_TRUNCATE_TIMESTAMP = 2,
47 ANNOTATION_ID_PRIMARY_FIELD = 3,
48 ANNOTATION_ID_EXCLUSIVE_STATE = 4,
49 ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID = 5,
50 ANNOTATION_ID_DEFAULT_STATE = 6,
51 ANNOTATION_ID_TRIGGER_STATE_RESET = 7,
52 ANNOTATION_ID_STATE_NESTED = 8,
53};
Muhammad Qureshib13a3212020-03-12 07:37:13 -070054
Muhammad Qureshi9b995802020-03-20 13:55:51 -070055const int ATOM_ID_FIELD_NUMBER = -1;
56
Muhammad Qureshib13a3212020-03-12 07:37:13 -070057const string DEFAULT_MODULE_NAME = "DEFAULT";
58
Yao Chend54f9dd2017-10-17 17:37:48 +000059/**
60 * The types for atom parameters.
61 */
62typedef enum {
Muhammad Qureshia345af92020-03-24 17:05:14 -070063 JAVA_TYPE_UNKNOWN = 0,
Yao Chend54f9dd2017-10-17 17:37:48 +000064
Muhammad Qureshia345af92020-03-24 17:05:14 -070065 JAVA_TYPE_ATTRIBUTION_CHAIN = 1,
66 JAVA_TYPE_BOOLEAN = 2,
67 JAVA_TYPE_INT = 3,
68 JAVA_TYPE_LONG = 4,
69 JAVA_TYPE_FLOAT = 5,
70 JAVA_TYPE_DOUBLE = 6,
71 JAVA_TYPE_STRING = 7,
72 JAVA_TYPE_ENUM = 8,
73 JAVA_TYPE_KEY_VALUE_PAIR = 9,
Yao Chend54f9dd2017-10-17 17:37:48 +000074
Muhammad Qureshia345af92020-03-24 17:05:14 -070075 JAVA_TYPE_OBJECT = -1,
76 JAVA_TYPE_BYTE_ARRAY = -2,
Yao Chend54f9dd2017-10-17 17:37:48 +000077} java_type_t;
78
Muhammad Qureshib13a3212020-03-12 07:37:13 -070079enum AnnotationType {
80 ANNOTATION_TYPE_UNKNOWN = 0,
81 ANNOTATION_TYPE_INT = 1,
82 ANNOTATION_TYPE_BOOL = 2,
83};
84
85union AnnotationValue {
86 int intValue;
87 bool boolValue;
88
Muhammad Qureshia345af92020-03-24 17:05:14 -070089 AnnotationValue(const int value) : intValue(value) {
90 }
91 AnnotationValue(const bool value) : boolValue(value) {
92 }
Muhammad Qureshib13a3212020-03-12 07:37:13 -070093};
94
95struct Annotation {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -070096 const AnnotationId annotationId;
Muhammad Qureshib13a3212020-03-12 07:37:13 -070097 const int atomId;
98 AnnotationType type;
99 AnnotationValue value;
100
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700101 inline Annotation(AnnotationId annotationId, int atomId, AnnotationType type,
Muhammad Qureshia345af92020-03-24 17:05:14 -0700102 AnnotationValue value)
103 : annotationId(annotationId), atomId(atomId), type(type), value(value) {
104 }
105 inline ~Annotation() {
106 }
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700107
108 inline bool operator<(const Annotation& that) const {
109 return atomId == that.atomId ? annotationId < that.annotationId : atomId < that.atomId;
110 }
111};
112
Muhammad Qureshic6c38632020-03-25 17:45:01 -0700113struct SharedComparator {
114 template <typename T>
115 inline bool operator()(const shared_ptr<T>& lhs, const shared_ptr<T>& rhs) const {
116 return (*lhs) < (*rhs);
117 }
118};
119
120using AnnotationSet = set<shared_ptr<Annotation>, SharedComparator>;
121
122using FieldNumberToAnnotations = map<int, AnnotationSet>;
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700123
Yao Chend54f9dd2017-10-17 17:37:48 +0000124/**
125 * The name and type for an atom field.
126 */
127struct AtomField {
128 string name;
129 java_type_t javaType;
130
Muhammad Qureshia345af92020-03-24 17:05:14 -0700131 // If the field is of type enum, the following map contains the list of enum
132 // values.
Stefan Lafon9478f352017-10-30 21:20:20 -0700133 map<int /* numeric value */, string /* value name */> enumValues;
134
Muhammad Qureshia345af92020-03-24 17:05:14 -0700135 inline AtomField() : name(), javaType(JAVA_TYPE_UNKNOWN) {
136 }
137 inline AtomField(const AtomField& that)
138 : name(that.name), javaType(that.javaType), enumValues(that.enumValues) {
139 }
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700140
Muhammad Qureshia345af92020-03-24 17:05:14 -0700141 inline AtomField(string n, java_type_t jt) : name(n), javaType(jt) {
142 }
143 inline ~AtomField() {
144 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000145};
146
147/**
148 * The name and code for an atom.
149 */
150struct AtomDecl {
151 int code;
152 string name;
153
154 string message;
155 vector<AtomField> fields;
156
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700157 FieldNumberToAnnotations fieldNumberToAnnotations;
158
Yao Chen9c1debe2018-02-19 14:39:19 -0800159 vector<int> primaryFields;
160 int exclusiveField = 0;
tsaichristine5adc7e02020-01-14 17:07:39 -0800161 int defaultState = INT_MAX;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700162 int triggerStateReset = INT_MAX;
Muhammad Qureshi79e10622020-04-07 07:23:21 -0700163 bool nested = true;
Yao Chen9c1debe2018-02-19 14:39:19 -0800164
Yao Chenc40a19d2018-03-15 16:48:25 -0700165 int uidField = 0;
166
Yao Chend54f9dd2017-10-17 17:37:48 +0000167 AtomDecl();
168 AtomDecl(const AtomDecl& that);
169 AtomDecl(int code, const string& name, const string& message);
170 ~AtomDecl();
171
172 inline bool operator<(const AtomDecl& that) const {
173 return (code == that.code) ? (name < that.name) : (code < that.code);
174 }
175};
176
Muhammad Qureshic6c38632020-03-25 17:45:01 -0700177using AtomDeclSet = set<shared_ptr<AtomDecl>, SharedComparator>;
178
179// Maps a field number to a set of atoms that have annotation(s) for their field with that field
180// number.
181using FieldNumberToAtomDeclSet = map<int, AtomDeclSet>;
182
183using SignatureInfoMap = map<vector<java_type_t>, FieldNumberToAtomDeclSet>;
184
Yao Chend54f9dd2017-10-17 17:37:48 +0000185struct Atoms {
Muhammad Qureshic6c38632020-03-25 17:45:01 -0700186 SignatureInfoMap signatureInfoMap;
187 AtomDeclSet decls;
188 AtomDeclSet non_chained_decls;
189 SignatureInfoMap nonChainedSignatureInfoMap;
Yao Chend54f9dd2017-10-17 17:37:48 +0000190};
191
192/**
193 * Gather the information about the atoms. Returns the number of errors.
194 */
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700195int collate_atoms(const Descriptor* descriptor, const string& moduleName, Atoms* atoms);
Muhammad Qureshia345af92020-03-24 17:05:14 -0700196int collate_atom(const Descriptor* atom, AtomDecl* atomDecl, vector<java_type_t>* signature);
Yao Chend54f9dd2017-10-17 17:37:48 +0000197
198} // namespace stats_log_api_gen
199} // namespace android
200
Muhammad Qureshia345af92020-03-24 17:05:14 -0700201#endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H