blob: 66ae964019743928e3f241227f7bff3abcb71241 [file] [log] [blame]
Muhammad Qureshic8e22662019-11-20 17:18:03 -08001/*
2 * Copyright (C) 2019, 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#include "atoms_info_writer.h"
18#include "utils.h"
19
20#include <map>
21#include <set>
22#include <vector>
23
24namespace android {
25namespace stats_log_api_gen {
26
27static void write_atoms_info_header_body(FILE* out, const Atoms& atoms) {
tsaichristineed615642020-01-02 12:53:41 -080028 fprintf(out, "static int FIRST_UID_IN_CHAIN = 0;\n");
29
Muhammad Qureshic8e22662019-11-20 17:18:03 -080030 fprintf(out, "struct StateAtomFieldOptions {\n");
31 fprintf(out, " std::vector<int> primaryFields;\n");
32 fprintf(out, " int exclusiveField;\n");
33 fprintf(out, "};\n");
34 fprintf(out, "\n");
35
36 fprintf(out, "struct AtomsInfo {\n");
37 fprintf(out,
38 " const static std::set<int> "
39 "kTruncatingTimestampAtomBlackList;\n");
40 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
41 fprintf(out,
42 " const static std::set<int> kAtomsWithAttributionChain;\n");
43 fprintf(out,
44 " const static std::map<int, StateAtomFieldOptions> "
45 "kStateAtomsFieldOptions;\n");
46 fprintf(out,
47 " const static std::map<int, std::vector<int>> "
48 "kBytesFieldAtoms;\n");
49 fprintf(out,
50 " const static std::set<int> kWhitelistedAtoms;\n");
51 fprintf(out, "};\n");
52 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", atoms.maxPushedAtomId);
53
54}
55
56static void write_atoms_info_cpp_body(FILE* out, const Atoms& atoms) {
57 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
58 "audio_state_changed",
59 "call_state_changed",
60 "phone_signal_strength_changed",
61 "mobile_bytes_transfer_by_fg_bg",
62 "mobile_bytes_transfer"};
63 fprintf(out,
64 "const std::set<int> "
65 "AtomsInfo::kTruncatingTimestampAtomBlackList = {\n");
66 for (set<string>::const_iterator blacklistedAtom = kTruncatingAtomNames.begin();
67 blacklistedAtom != kTruncatingAtomNames.end(); blacklistedAtom++) {
68 fprintf(out, " %s,\n", make_constant_name(*blacklistedAtom).c_str());
69 }
70 fprintf(out, "};\n");
71 fprintf(out, "\n");
72
73 fprintf(out,
74 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
75 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
76 atom != atoms.decls.end(); atom++) {
77 for (vector<AtomField>::const_iterator field = atom->fields.begin();
78 field != atom->fields.end(); field++) {
79 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
80 string constant = make_constant_name(atom->name);
81 fprintf(out, " %s,\n", constant.c_str());
82 break;
83 }
84 }
85 }
86
87 fprintf(out, "};\n");
88 fprintf(out, "\n");
89
90 fprintf(out,
91 "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
92 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
93 atom != atoms.decls.end(); atom++) {
94 if (atom->whitelisted) {
95 string constant = make_constant_name(atom->name);
96 fprintf(out, " %s,\n", constant.c_str());
97 }
98 }
99
100 fprintf(out, "};\n");
101 fprintf(out, "\n");
102
103 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
104 fprintf(out, " std::map<int, int> uidField;\n");
105 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
106 atom != atoms.decls.end(); atom++) {
107 if (atom->uidField == 0) {
108 continue;
109 }
110 fprintf(out,
111 "\n // Adding uid field for atom "
112 "(%d)%s\n",
113 atom->code, atom->name.c_str());
114 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
115 make_constant_name(atom->name).c_str(), atom->uidField);
116 }
117
118 fprintf(out, " return uidField;\n");
119 fprintf(out, "};\n");
120
121 fprintf(out,
122 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
123 "getAtomUidField();\n");
124
125 fprintf(out,
126 "static std::map<int, StateAtomFieldOptions> "
127 "getStateAtomFieldOptions() {\n");
128 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
129 fprintf(out, " StateAtomFieldOptions opt;\n");
130 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
131 atom != atoms.decls.end(); atom++) {
132 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
133 continue;
134 }
135 fprintf(out,
136 "\n // Adding primary and exclusive fields for atom "
137 "(%d)%s\n",
138 atom->code, atom->name.c_str());
139 fprintf(out, " opt.primaryFields.clear();\n");
140 for (const auto& field : atom->primaryFields) {
141 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
142 }
143
144 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
145 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
146 make_constant_name(atom->name).c_str());
147 }
148
149 fprintf(out, " return options;\n");
150 fprintf(out, "}\n");
151
152 fprintf(out,
153 "const std::map<int, StateAtomFieldOptions> "
154 "AtomsInfo::kStateAtomsFieldOptions = "
155 "getStateAtomFieldOptions();\n");
156
157 fprintf(out,
158 "static std::map<int, std::vector<int>> "
159 "getBinaryFieldAtoms() {\n");
160 fprintf(out, " std::map<int, std::vector<int>> options;\n");
161 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
162 atom != atoms.decls.end(); atom++) {
163 if (atom->binaryFields.size() == 0) {
164 continue;
165 }
166 fprintf(out,
167 "\n // Adding binary fields for atom "
168 "(%d)%s\n",
169 atom->code, atom->name.c_str());
170
171 for (const auto& field : atom->binaryFields) {
172 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
173 make_constant_name(atom->name).c_str(), field);
174 }
175 }
176
177 fprintf(out, " return options;\n");
178 fprintf(out, "}\n");
179
180 fprintf(out,
181 "const std::map<int, std::vector<int>> "
182 "AtomsInfo::kBytesFieldAtoms = "
183 "getBinaryFieldAtoms();\n");
184
185}
186
187int write_atoms_info_header(FILE* out, const Atoms &atoms, const string& namespaceStr) {
188 // Print prelude
189 fprintf(out, "// This file is autogenerated\n");
190 fprintf(out, "\n");
191 fprintf(out, "#pragma once\n");
192 fprintf(out, "\n");
193 fprintf(out, "#include <vector>\n");
194 fprintf(out, "#include <map>\n");
195 fprintf(out, "#include <set>\n");
196 fprintf(out, "\n");
197
198 write_namespace(out, namespaceStr);
199
200 write_atoms_info_header_body(out, atoms);
201
202 fprintf(out, "\n");
203 write_closing_namespace(out, namespaceStr);
204
205 return 0;
206}
207
208int write_atoms_info_cpp(FILE *out, const Atoms &atoms, const string& namespaceStr,
209 const string& importHeader, const string& statslogHeader) {
210 // Print prelude
211 fprintf(out, "// This file is autogenerated\n");
212 fprintf(out, "\n");
213 fprintf(out, "#include <%s>\n", importHeader.c_str());
214 fprintf(out, "#include <%s>\n", statslogHeader.c_str());
215 fprintf(out, "\n");
216
217 write_namespace(out, namespaceStr);
218
219 write_atoms_info_cpp_body(out, atoms);
220
221 // Print footer
222 fprintf(out, "\n");
223 write_closing_namespace(out, namespaceStr);
224
225 return 0;
226}
227
228} // namespace stats_log_api_gen
229} // namespace android