blob: c6dad1d07d89821d53174e84eb6cbeef6b90340b [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
20
21#include <google/protobuf/descriptor.h>
Muhammad Qureshib13a3212020-03-12 07:37:13 -070022#include "frameworks/base/cmds/statsd/src/atom_field_options.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +000023
24#include <set>
25#include <vector>
Stefan Lafon9478f352017-10-30 21:20:20 -070026#include <map>
Yao Chend54f9dd2017-10-17 17:37:48 +000027
28namespace android {
29namespace stats_log_api_gen {
30
Stefan Lafon9478f352017-10-30 21:20:20 -070031using std::map;
Yao Chend54f9dd2017-10-17 17:37:48 +000032using std::set;
Muhammad Qureshib13a3212020-03-12 07:37:13 -070033using std::shared_ptr;
Yao Chend54f9dd2017-10-17 17:37:48 +000034using std::string;
35using std::vector;
36using google::protobuf::Descriptor;
Yangster-macba5b9e42018-01-10 21:31:59 -080037using google::protobuf::FieldDescriptor;
Yao Chend54f9dd2017-10-17 17:37:48 +000038
Chenjie Yu159e4f82018-08-29 11:49:11 -070039const int PULL_ATOM_START_ID = 10000;
40
tsaichristineed615642020-01-02 12:53:41 -080041const int FIRST_UID_IN_CHAIN_ID = 0;
42
Muhammad Qureshib13a3212020-03-12 07:37:13 -070043const unsigned char ANNOTATION_ID_IS_UID = 1;
44const unsigned char ANNOTATION_ID_TRUNCATE_TIMESTAMP = 2;
45const unsigned char ANNOTATION_ID_STATE_OPTION = 3;
46const unsigned char ANNOTATION_ID_DEFAULT_STATE = 4;
47const unsigned char ANNOTATION_ID_RESET_STATE = 5;
48const unsigned char ANNOTATION_ID_STATE_NESTED = 6;
49
50const int STATE_OPTION_UNSET = os::statsd::StateField::STATE_FIELD_UNSET;
51const int STATE_OPTION_EXCLUSIVE = os::statsd::StateField::EXCLUSIVE_STATE;
52const int STATE_OPTION_PRIMARY_FIELD_FIRST_UID = os::statsd::StateField::PRIMARY_FIELD_FIRST_UID;
53const int STATE_OPTION_PRIMARY = os::statsd::StateField::PRIMARY_FIELD;
54
55const string DEFAULT_MODULE_NAME = "DEFAULT";
56
Yao Chend54f9dd2017-10-17 17:37:48 +000057/**
58 * The types for atom parameters.
59 */
60typedef enum {
Yangster-mac7604aea2017-12-11 22:55:49 -080061 JAVA_TYPE_UNKNOWN = 0,
Yao Chend54f9dd2017-10-17 17:37:48 +000062
Yangster-mac7604aea2017-12-11 22:55:49 -080063 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-mac48b3d622018-08-18 12:38:11 -070071 JAVA_TYPE_KEY_VALUE_PAIR = 9,
Yao Chend54f9dd2017-10-17 17:37:48 +000072
Yangster-mac7604aea2017-12-11 22:55:49 -080073 JAVA_TYPE_OBJECT = -1,
74 JAVA_TYPE_BYTE_ARRAY = -2,
Yao Chend54f9dd2017-10-17 17:37:48 +000075} java_type_t;
76
Muhammad Qureshib13a3212020-03-12 07:37:13 -070077enum AnnotationType {
78 ANNOTATION_TYPE_UNKNOWN = 0,
79 ANNOTATION_TYPE_INT = 1,
80 ANNOTATION_TYPE_BOOL = 2,
81};
82
83union AnnotationValue {
84 int intValue;
85 bool boolValue;
86
87 AnnotationValue(const int value): intValue(value) {}
88 AnnotationValue(const bool value): boolValue(value) {}
89};
90
91struct 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
107using FieldNumberToAnnotations = map<int, set<shared_ptr<Annotation>>>;
108
Yao Chend54f9dd2017-10-17 17:37:48 +0000109/**
110 * The name and type for an atom field.
111 */
112struct AtomField {
113 string name;
114 java_type_t javaType;
115
Stefan Lafon9478f352017-10-30 21:20:20 -0700116 // 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 Chend54f9dd2017-10-17 17:37:48 +0000119 inline AtomField() :name(), javaType(JAVA_TYPE_UNKNOWN) {}
Stefan Lafon9478f352017-10-30 21:20:20 -0700120 inline AtomField(const AtomField& that) :name(that.name),
121 javaType(that.javaType),
122 enumValues(that.enumValues) {}
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700123
Yao Chend54f9dd2017-10-17 17:37:48 +0000124 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 */
131struct AtomDecl {
132 int code;
133 string name;
134
135 string message;
136 vector<AtomField> fields;
137
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700138 FieldNumberToAnnotations fieldNumberToAnnotations;
139
Yao Chen9c1debe2018-02-19 14:39:19 -0800140 vector<int> primaryFields;
141 int exclusiveField = 0;
tsaichristine5adc7e02020-01-14 17:07:39 -0800142 int defaultState = INT_MAX;
143 int resetState = INT_MAX;
144 bool nested;
Yao Chen9c1debe2018-02-19 14:39:19 -0800145
Yao Chenc40a19d2018-03-15 16:48:25 -0700146 int uidField = 0;
147
Andrei Oneada01ea52019-01-30 15:28:36 +0000148 bool whitelisted = false;
149
Yao Chend54f9dd2017-10-17 17:37:48 +0000150 AtomDecl();
151 AtomDecl(const AtomDecl& that);
152 AtomDecl(int code, const string& name, const string& message);
153 ~AtomDecl();
154
155 inline bool operator<(const AtomDecl& that) const {
156 return (code == that.code) ? (name < that.name) : (code < that.code);
157 }
158};
159
160struct Atoms {
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700161 map<vector<java_type_t>, FieldNumberToAnnotations> signatureInfoMap;
Yao Chend54f9dd2017-10-17 17:37:48 +0000162 set<AtomDecl> decls;
Yangster-macba5b9e42018-01-10 21:31:59 -0800163 set<AtomDecl> non_chained_decls;
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700164 map<vector<java_type_t>, FieldNumberToAnnotations> nonChainedSignatureInfoMap;
Muhammad Qureshic8e22662019-11-20 17:18:03 -0800165 int maxPushedAtomId;
Yao Chend54f9dd2017-10-17 17:37:48 +0000166};
167
168/**
169 * Gather the information about the atoms. Returns the number of errors.
170 */
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700171int collate_atoms(const Descriptor* descriptor, const string& moduleName, Atoms* atoms);
Yangster-macba5b9e42018-01-10 21:31:59 -0800172int collate_atom(const Descriptor *atom, AtomDecl *atomDecl, vector<java_type_t> *signature);
Yao Chend54f9dd2017-10-17 17:37:48 +0000173
174} // namespace stats_log_api_gen
175} // namespace android
176
177
Muhammad Qureshic8e22662019-11-20 17:18:03 -0800178#endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H