blob: a78b1a2b073127d88f04e7b6e2f430e2a1ec4c81 [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001
2
3#include "Collation.h"
4
Stefan Lafonae2df012017-11-14 09:17:21 -08005#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +00006
7#include <set>
8#include <vector>
9
10#include <getopt.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15using namespace google::protobuf;
16using namespace std;
17
18namespace android {
19namespace stats_log_api_gen {
20
Yao Chenb3561512017-11-21 18:07:17 -080021const int PULL_ATOM_START_ID = 1000;
22
23int maxPushedAtomId = 2;
24
Stefan Lafonae2df012017-11-14 09:17:21 -080025using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000026
Yao Chend54f9dd2017-10-17 17:37:48 +000027/**
28 * Turn lower and camel case into upper case with underscores.
29 */
30static string
31make_constant_name(const string& str)
32{
33 string result;
34 const int N = str.size();
35 bool underscore_next = false;
36 for (int i=0; i<N; i++) {
37 char c = str[i];
38 if (c >= 'A' && c <= 'Z') {
39 if (underscore_next) {
40 result += '_';
41 underscore_next = false;
42 }
43 } else if (c >= 'a' && c <= 'z') {
44 c = 'A' + c - 'a';
45 underscore_next = true;
46 } else if (c == '_') {
47 underscore_next = false;
48 }
49 result += c;
50 }
51 return result;
52}
53
54static const char*
55cpp_type_name(java_type_t type)
56{
57 switch (type) {
58 case JAVA_TYPE_BOOLEAN:
59 return "bool";
60 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070061 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000062 return "int32_t";
63 case JAVA_TYPE_LONG:
64 return "int64_t";
65 case JAVA_TYPE_FLOAT:
66 return "float";
67 case JAVA_TYPE_DOUBLE:
68 return "double";
69 case JAVA_TYPE_STRING:
70 return "char const*";
71 default:
72 return "UNKNOWN";
73 }
74}
75
76static const char*
77java_type_name(java_type_t type)
78{
79 switch (type) {
80 case JAVA_TYPE_BOOLEAN:
81 return "boolean";
82 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070083 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000084 return "int";
85 case JAVA_TYPE_LONG:
86 return "long";
87 case JAVA_TYPE_FLOAT:
88 return "float";
89 case JAVA_TYPE_DOUBLE:
90 return "double";
91 case JAVA_TYPE_STRING:
92 return "java.lang.String";
93 default:
94 return "UNKNOWN";
95 }
96}
97
Yangster-mac7604aea2017-12-11 22:55:49 -080098static int write_stats_log_cpp(FILE *out, const Atoms &atoms,
99 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000100 // Print prelude
101 fprintf(out, "// This file is autogenerated\n");
102 fprintf(out, "\n");
103
Yangster-mac7604aea2017-12-11 22:55:49 -0800104 fprintf(out, "#include <exception>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000105 fprintf(out, "#include <log/log_event_list.h>\n");
106 fprintf(out, "#include <log/log.h>\n");
107 fprintf(out, "#include <statslog.h>\n");
Yangster-mac330af582018-02-08 15:24:38 -0800108 fprintf(out, "#include <utils/SystemClock.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000109 fprintf(out, "\n");
110
111 fprintf(out, "namespace android {\n");
112 fprintf(out, "namespace util {\n");
Yao Chen80235402017-11-13 20:42:25 -0800113 fprintf(out, "// the single event tag id for all stats logs\n");
114 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000115
116 // Print write methods
117 fprintf(out, "\n");
118 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800119 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000120 int argIndex;
121
122 fprintf(out, "void\n");
Yao Chen80235402017-11-13 20:42:25 -0800123 fprintf(out, "stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000124 argIndex = 1;
125 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800126 arg != signature->end(); arg++) {
127 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
128 for (auto chainField : attributionDecl.fields) {
129 if (chainField.javaType == JAVA_TYPE_STRING) {
130 fprintf(out, ", const std::vector<%s>& %s",
131 cpp_type_name(chainField.javaType),
132 chainField.name.c_str());
133 } else {
134 fprintf(out, ", const %s* %s, size_t %s_length",
135 cpp_type_name(chainField.javaType),
136 chainField.name.c_str(), chainField.name.c_str());
137 }
138 }
139 } else {
140 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
141 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000142 argIndex++;
143 }
144 fprintf(out, ")\n");
145
146 fprintf(out, "{\n");
147 argIndex = 1;
Yao Chen80235402017-11-13 20:42:25 -0800148 fprintf(out, " android_log_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800149 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800150 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000151 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800152 arg != signature->end(); arg++) {
153 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
154 for (const auto &chainField : attributionDecl.fields) {
155 if (chainField.javaType == JAVA_TYPE_STRING) {
156 fprintf(out, " if (%s_length != %s.size()) {\n",
157 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
158 fprintf(out, " throw std::invalid_argument(\"attribution fields with"
159 " diff length: %s vs %s\");\n",
160 attributionDecl.fields.front().name.c_str(),
161 chainField.name.c_str());
162 fprintf(out, " return;\n");
163 fprintf(out, " }\n");
164 }
165 }
166 fprintf(out, "\n event.begin();\n");
167 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
168 attributionDecl.fields.front().name.c_str());
169 fprintf(out, " event.begin();\n");
170 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800171 if (chainField.javaType == JAVA_TYPE_STRING) {
172 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
173 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
174 fprintf(out, " } else {\n");
175 fprintf(out, " event << \"\";\n");
176 fprintf(out, " }\n");
177 } else {
178 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
179 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800180 }
181 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000182 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800183 fprintf(out, " event.end();\n\n");
184 } else {
185 if (*arg == JAVA_TYPE_STRING) {
186 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
187 fprintf(out, " arg%d = \"\";\n", argIndex);
188 fprintf(out, " }\n");
189 }
190 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000191 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000192 argIndex++;
193 }
194
195 fprintf(out, " event.write(LOG_ID_STATS);\n");
196 fprintf(out, "}\n");
197 fprintf(out, "\n");
198 }
199
Yangster-macba5b9e42018-01-10 21:31:59 -0800200 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
201 signature != atoms.non_chained_signatures.end(); signature++) {
202 int argIndex;
203
204 fprintf(out, "void\n");
205 fprintf(out, "stats_write_non_chained(int32_t code");
206 argIndex = 1;
207 for (vector<java_type_t>::const_iterator arg = signature->begin();
208 arg != signature->end(); arg++) {
209 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
210 argIndex++;
211 }
212 fprintf(out, ")\n");
213
214 fprintf(out, "{\n");
215 argIndex = 1;
216 fprintf(out, " android_log_event_list event(kStatsEventTag);\n");
217 fprintf(out, " event << code;\n\n");
218 for (vector<java_type_t>::const_iterator arg = signature->begin();
219 arg != signature->end(); arg++) {
220 if (argIndex == 1) {
221 fprintf(out, " event.begin();\n\n");
222 fprintf(out, " event.begin();\n");
223 }
224 if (*arg == JAVA_TYPE_STRING) {
225 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
226 fprintf(out, " arg%d = \"\";\n", argIndex);
227 fprintf(out, " }\n");
228 }
229 fprintf(out, " event << arg%d;\n", argIndex);
230 if (argIndex == 2) {
231 fprintf(out, " event.end();\n\n");
232 fprintf(out, " event.end();\n\n");
233 }
234 argIndex++;
235 }
236
237 fprintf(out, " event.write(LOG_ID_STATS);\n");
238 fprintf(out, "}\n");
239 fprintf(out, "\n");
240 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000241 // Print footer
242 fprintf(out, "\n");
243 fprintf(out, "} // namespace util\n");
244 fprintf(out, "} // namespace android\n");
245
246 return 0;
247}
248
Yangster-macba5b9e42018-01-10 21:31:59 -0800249void build_non_chained_decl_map(const Atoms& atoms,
250 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
251 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
252 atom != atoms.non_chained_decls.end(); atom++) {
253 decl_map->insert(std::make_pair(atom->code, atom));
254 }
255}
256
257static void write_cpp_usage(
258 FILE* out, const string& method_name, const string& atom_code_name,
259 const AtomDecl& atom, const AtomDecl &attributionDecl) {
260 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str());
261 for (vector<AtomField>::const_iterator field = atom.fields.begin();
262 field != atom.fields.end(); field++) {
263 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
264 for (auto chainField : attributionDecl.fields) {
265 if (chainField.javaType == JAVA_TYPE_STRING) {
266 fprintf(out, ", const std::vector<%s>& %s",
267 cpp_type_name(chainField.javaType),
268 chainField.name.c_str());
269 } else {
270 fprintf(out, ", const %s* %s, size_t %s_length",
271 cpp_type_name(chainField.javaType),
272 chainField.name.c_str(), chainField.name.c_str());
273 }
274 }
275 } else {
276 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
277 }
278 }
279 fprintf(out, ");\n");
280}
281
282static void write_cpp_method_header(
283 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
284 const AtomDecl &attributionDecl) {
285 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
286 signature != signatures.end(); signature++) {
287 fprintf(out, "void %s(int32_t code ", method_name.c_str());
288 int argIndex = 1;
289 for (vector<java_type_t>::const_iterator arg = signature->begin();
290 arg != signature->end(); arg++) {
291 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
292 for (auto chainField : attributionDecl.fields) {
293 if (chainField.javaType == JAVA_TYPE_STRING) {
294 fprintf(out, ", const std::vector<%s>& %s",
295 cpp_type_name(chainField.javaType), chainField.name.c_str());
296 } else {
297 fprintf(out, ", const %s* %s, size_t %s_length",
298 cpp_type_name(chainField.javaType),
299 chainField.name.c_str(), chainField.name.c_str());
300 }
301 }
302 } else {
303 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
304 }
305 argIndex++;
306 }
307 fprintf(out, ");\n");
308
309 }
310}
Yao Chend54f9dd2017-10-17 17:37:48 +0000311
312static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800313write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000314{
Yao Chend54f9dd2017-10-17 17:37:48 +0000315 // Print prelude
316 fprintf(out, "// This file is autogenerated\n");
317 fprintf(out, "\n");
318 fprintf(out, "#pragma once\n");
319 fprintf(out, "\n");
320 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800321 fprintf(out, "#include <vector>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800322 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000323 fprintf(out, "\n");
324
325 fprintf(out, "namespace android {\n");
326 fprintf(out, "namespace util {\n");
327 fprintf(out, "\n");
328 fprintf(out, "/*\n");
329 fprintf(out, " * API For logging statistics events.\n");
330 fprintf(out, " */\n");
331 fprintf(out, "\n");
332 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700333 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000334 fprintf(out, " */\n");
335 fprintf(out, "enum {\n");
336
Yangster-macba5b9e42018-01-10 21:31:59 -0800337 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
338 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
339
Yao Chend54f9dd2017-10-17 17:37:48 +0000340 size_t i = 0;
341 // Print constants
342 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800343 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000344 string constant = make_constant_name(atom->name);
345 fprintf(out, "\n");
346 fprintf(out, " /**\n");
347 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800348 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
349
350 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
351 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
352 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
353 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000354 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000355 fprintf(out, " */\n");
356 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
357 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800358 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
359 maxPushedAtomId = atom->code;
360 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000361 i++;
362 }
363 fprintf(out, "\n");
364 fprintf(out, "};\n");
365 fprintf(out, "\n");
366
Yangster-mac68985802018-01-21 10:05:09 -0800367 fprintf(out, "const static std::set<int> kAtomsWithUidField = {\n");
368 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
369 atom != atoms.decls.end(); atom++) {
370 for (vector<AtomField>::const_iterator field = atom->fields.begin();
371 field != atom->fields.end(); field++) {
372 if (field->name == "uid") {
373 string constant = make_constant_name(atom->name);
374 fprintf(out, " %s,\n", constant.c_str());
375 break;
376 }
377 }
378 }
379 fprintf(out, "};\n");
380 fprintf(out, "\n");
381
382 fprintf(out, "const static std::set<int> kAtomsWithAttributionChain = {\n");
383 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
384 atom != atoms.decls.end(); atom++) {
385 for (vector<AtomField>::const_iterator field = atom->fields.begin();
386 field != atom->fields.end(); field++) {
387 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
388 string constant = make_constant_name(atom->name);
389 fprintf(out, " %s,\n", constant.c_str());
390 break;
391 }
392 }
393 }
394 fprintf(out, "};\n");
395 fprintf(out, "\n");
396
Yangster-mac7604aea2017-12-11 22:55:49 -0800397 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId);
Yao Chenb3561512017-11-21 18:07:17 -0800398
Yao Chend54f9dd2017-10-17 17:37:48 +0000399 // Print write methods
400 fprintf(out, "//\n");
401 fprintf(out, "// Write methods\n");
402 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800403 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
404
405 fprintf(out, "//\n");
406 fprintf(out, "// Write flattened methods\n");
407 fprintf(out, "//\n");
408 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
409 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000410
411 fprintf(out, "\n");
412 fprintf(out, "} // namespace util\n");
413 fprintf(out, "} // namespace android\n");
414
415 return 0;
416}
417
Yangster-macba5b9e42018-01-10 21:31:59 -0800418static void write_java_usage(
419 FILE* out, const string& method_name, const string& atom_code_name,
420 const AtomDecl& atom, const AtomDecl &attributionDecl) {
421 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
422 method_name.c_str(), atom_code_name.c_str());
423 for (vector<AtomField>::const_iterator field = atom.fields.begin();
424 field != atom.fields.end(); field++) {
425 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
426 for (auto chainField : attributionDecl.fields) {
427 fprintf(out, ", %s[] %s",
428 java_type_name(chainField.javaType), chainField.name.c_str());
429 }
430 } else {
431 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
432 }
433 }
434 fprintf(out, ");\n");
435}
436
437static void write_java_method(
438 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
439 const AtomDecl &attributionDecl) {
440 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
441 signature != signatures.end(); signature++) {
442 fprintf(out, " public static native void %s(int code", method_name.c_str());
443 int argIndex = 1;
444 for (vector<java_type_t>::const_iterator arg = signature->begin();
445 arg != signature->end(); arg++) {
446 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
447 for (auto chainField : attributionDecl.fields) {
448 fprintf(out, ", %s[] %s",
449 java_type_name(chainField.javaType), chainField.name.c_str());
450 }
451 } else {
452 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
453 }
454 argIndex++;
455 }
456 fprintf(out, ");\n");
457 }
458}
459
460
Yao Chend54f9dd2017-10-17 17:37:48 +0000461static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800462write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000463{
Yao Chend54f9dd2017-10-17 17:37:48 +0000464 // Print prelude
465 fprintf(out, "// This file is autogenerated\n");
466 fprintf(out, "\n");
467 fprintf(out, "package android.util;\n");
468 fprintf(out, "\n");
469 fprintf(out, "\n");
470 fprintf(out, "/**\n");
471 fprintf(out, " * API For logging statistics events.\n");
472 fprintf(out, " * @hide\n");
473 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800474 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700475 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000476
Yangster-macba5b9e42018-01-10 21:31:59 -0800477 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
478 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
479
Stefan Lafon9478f352017-10-30 21:20:20 -0700480 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000481 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
482 atom != atoms.decls.end(); atom++) {
483 string constant = make_constant_name(atom->name);
484 fprintf(out, "\n");
485 fprintf(out, " /**\n");
486 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800487 write_java_usage(out, "write", constant, *atom, attributionDecl);
488 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
489 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
490 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second,
491 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000492 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000493 fprintf(out, " */\n");
494 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
495 }
496 fprintf(out, "\n");
497
Stefan Lafon9478f352017-10-30 21:20:20 -0700498 // Print constants for the enum values.
499 fprintf(out, " // Constants for enum values.\n\n");
500 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800501 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700502 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800503 field != atom->fields.end(); field++) {
504 if (field->javaType == JAVA_TYPE_ENUM) {
505 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
506 field->name.c_str());
507 for (map<int, string>::const_iterator value = field->enumValues.begin();
508 value != field->enumValues.end(); value++) {
509 fprintf(out, " public static final int %s__%s__%s = %d;\n",
510 make_constant_name(atom->message).c_str(),
511 make_constant_name(field->name).c_str(),
512 make_constant_name(value->second).c_str(),
513 value->first);
514 }
515 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700516 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700517 }
518 }
519
Yao Chend54f9dd2017-10-17 17:37:48 +0000520 // Print write methods
521 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800522 write_java_method(out, "write", atoms.signatures, attributionDecl);
523 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000524
525 fprintf(out, "}\n");
526
527 return 0;
528}
529
530static const char*
531jni_type_name(java_type_t type)
532{
533 switch (type) {
534 case JAVA_TYPE_BOOLEAN:
535 return "jboolean";
536 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700537 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000538 return "jint";
539 case JAVA_TYPE_LONG:
540 return "jlong";
541 case JAVA_TYPE_FLOAT:
542 return "jfloat";
543 case JAVA_TYPE_DOUBLE:
544 return "jdouble";
545 case JAVA_TYPE_STRING:
546 return "jstring";
547 default:
548 return "UNKNOWN";
549 }
550}
551
Yangster-mac7604aea2017-12-11 22:55:49 -0800552static const char*
553jni_array_type_name(java_type_t type)
554{
555 switch (type) {
556 case JAVA_TYPE_INT:
557 return "jintArray";
558 case JAVA_TYPE_STRING:
559 return "jobjectArray";
560 default:
561 return "UNKNOWN";
562 }
563}
564
Yao Chend54f9dd2017-10-17 17:37:48 +0000565static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800566jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000567{
Yangster-macba5b9e42018-01-10 21:31:59 -0800568 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000569 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800570 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000571 switch (*arg) {
572 case JAVA_TYPE_BOOLEAN:
573 result += "_boolean";
574 break;
575 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700576 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000577 result += "_int";
578 break;
579 case JAVA_TYPE_LONG:
580 result += "_long";
581 break;
582 case JAVA_TYPE_FLOAT:
583 result += "_float";
584 break;
585 case JAVA_TYPE_DOUBLE:
586 result += "_double";
587 break;
588 case JAVA_TYPE_STRING:
589 result += "_String";
590 break;
Yangster-mac7604aea2017-12-11 22:55:49 -0800591 case JAVA_TYPE_ATTRIBUTION_CHAIN:
592 result += "_AttributionChain";
593 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000594 default:
595 result += "_UNKNOWN";
596 break;
597 }
598 }
599 return result;
600}
601
602static const char*
603java_type_signature(java_type_t type)
604{
605 switch (type) {
606 case JAVA_TYPE_BOOLEAN:
607 return "Z";
608 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700609 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000610 return "I";
611 case JAVA_TYPE_LONG:
612 return "J";
613 case JAVA_TYPE_FLOAT:
614 return "F";
615 case JAVA_TYPE_DOUBLE:
616 return "D";
617 case JAVA_TYPE_STRING:
618 return "Ljava/lang/String;";
619 default:
620 return "UNKNOWN";
621 }
622}
623
624static string
Yangster-mac7604aea2017-12-11 22:55:49 -0800625jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000626{
627 string result("(I");
628 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800629 arg != signature.end(); arg++) {
630 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
631 for (auto chainField : attributionDecl.fields) {
632 result += "[";
633 result += java_type_signature(chainField.javaType);
634 }
635 } else {
636 result += java_type_signature(*arg);
637 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000638 }
639 result += ")V";
640 return result;
641}
642
643static int
Yangster-macba5b9e42018-01-10 21:31:59 -0800644write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
645 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000646{
Yao Chend54f9dd2017-10-17 17:37:48 +0000647 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -0800648 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
649 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000650 int argIndex;
651
652 fprintf(out, "static void\n");
653 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -0800654 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000655 argIndex = 1;
656 for (vector<java_type_t>::const_iterator arg = signature->begin();
657 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800658 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
659 for (auto chainField : attributionDecl.fields) {
660 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
661 chainField.name.c_str());
662 }
663 } else {
664 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
665 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000666 argIndex++;
667 }
668 fprintf(out, ")\n");
669
670 fprintf(out, "{\n");
671
672 // Prepare strings
673 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -0800674 bool hadStringOrChain = false;
Yao Chend54f9dd2017-10-17 17:37:48 +0000675 for (vector<java_type_t>::const_iterator arg = signature->begin();
676 arg != signature->end(); arg++) {
677 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800678 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +0000679 fprintf(out, " const char* str%d;\n", argIndex);
680 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
681 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
682 argIndex, argIndex);
683 fprintf(out, " } else {\n");
684 fprintf(out, " str%d = NULL;\n", argIndex);
685 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800686 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
687 hadStringOrChain = true;
688 for (auto chainField : attributionDecl.fields) {
689 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
690 chainField.name.c_str(), chainField.name.c_str());
691 if (chainField.name != attributionDecl.fields.front().name) {
692 fprintf(out, " if (%s_length != %s_length) {\n",
693 chainField.name.c_str(),
694 attributionDecl.fields.front().name.c_str());
695 fprintf(out, " jniThrowException(env, "
696 "\"java/lang/IllegalArgumentException\", "
697 "\"invalid attribution field(%s) length.\");\n",
698 chainField.name.c_str());
699 fprintf(out, " return;\n");
700 fprintf(out, " }\n");
701 }
702 if (chainField.javaType == JAVA_TYPE_INT) {
703 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
704 chainField.name.c_str(), chainField.name.c_str());
705 } else if (chainField.javaType == JAVA_TYPE_STRING) {
706 fprintf(out, " std::vector<%s> %s_vec;\n",
707 cpp_type_name(chainField.javaType), chainField.name.c_str());
708 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
709 chainField.name.c_str());
710 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
711 chainField.name.c_str());
712 fprintf(out, " jstring jstr = "
713 "(jstring)env->GetObjectArrayElement(%s, i);\n",
714 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800715 fprintf(out, " if (jstr == NULL) {\n");
716 fprintf(out, " %s_vec.push_back(NULL);\n",
717 chainField.name.c_str());
718 fprintf(out, " } else {\n");
719 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -0800720 "new ScopedUtfChars(env, jstr);\n",
721 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800722 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800723 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800724 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800725 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800726 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800727 fprintf(out, " }\n");
728 }
729 fprintf(out, "\n");
730 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000731 }
732 argIndex++;
733 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800734 // Emit this to quiet the unused parameter warning if there were no strings or attribution
735 // chains.
736 if (!hadStringOrChain) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000737 fprintf(out, " (void)env;\n");
738 }
739
740 // stats_write call
741 argIndex = 1;
Yangster-macba5b9e42018-01-10 21:31:59 -0800742 fprintf(out, " android::util::%s(code", cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000743 for (vector<java_type_t>::const_iterator arg = signature->begin();
744 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800745 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
746 for (auto chainField : attributionDecl.fields) {
747 if (chainField.javaType == JAVA_TYPE_INT) {
748 fprintf(out, ", (const %s*)%s_array, %s_length",
749 cpp_type_name(chainField.javaType),
750 chainField.name.c_str(), chainField.name.c_str());
751 } else if (chainField.javaType == JAVA_TYPE_STRING) {
752 fprintf(out, ", %s_vec", chainField.name.c_str());
753 }
754 }
755 } else {
756 const char *argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
757 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
758 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000759 argIndex++;
760 }
761 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800762 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000763
764 // Clean up strings
765 argIndex = 1;
766 for (vector<java_type_t>::const_iterator arg = signature->begin();
767 arg != signature->end(); arg++) {
768 if (*arg == JAVA_TYPE_STRING) {
769 fprintf(out, " if (str%d != NULL) {\n", argIndex);
770 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
771 argIndex, argIndex);
772 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800773 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
774 for (auto chainField : attributionDecl.fields) {
775 if (chainField.javaType == JAVA_TYPE_INT) {
776 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
777 chainField.name.c_str(), chainField.name.c_str());
778 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800779 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800780 chainField.name.c_str());
781 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
782 fprintf(out, " }\n");
783 }
784 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000785 }
786 argIndex++;
787 }
788
789 fprintf(out, "}\n");
790 fprintf(out, "\n");
791 }
792
Yangster-macba5b9e42018-01-10 21:31:59 -0800793
794 return 0;
795}
796
797void write_jni_registration(FILE* out, const string& java_method_name,
798 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
799 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
800 signature != signatures.end(); signature++) {
801 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
802 java_method_name.c_str(),
803 jni_function_signature(*signature, attributionDecl).c_str(),
804 jni_function_name(java_method_name, *signature).c_str());
805 }
806}
807
808static int
809write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
810{
811 // Print prelude
812 fprintf(out, "// This file is autogenerated\n");
813 fprintf(out, "\n");
814
815 fprintf(out, "#include <statslog.h>\n");
816 fprintf(out, "\n");
817 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
818 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
819 fprintf(out, "#include <utils/Vector.h>\n");
820 fprintf(out, "#include \"core_jni_helpers.h\"\n");
821 fprintf(out, "#include \"jni.h\"\n");
822 fprintf(out, "\n");
823 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
824 fprintf(out, "\n");
825
826 fprintf(out, "namespace android {\n");
827 fprintf(out, "\n");
828
829 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
830 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
831 atoms.non_chained_signatures, attributionDecl);
832
Yao Chend54f9dd2017-10-17 17:37:48 +0000833 // Print registration function table
834 fprintf(out, "/*\n");
835 fprintf(out, " * JNI registration.\n");
836 fprintf(out, " */\n");
837 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800838 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
839 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000840 fprintf(out, "};\n");
841 fprintf(out, "\n");
842
843 // Print registration function
844 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
845 fprintf(out, " return RegisterMethodsOrDie(\n");
846 fprintf(out, " env,\n");
847 fprintf(out, " \"android/util/StatsLog\",\n");
848 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
849 fprintf(out, "}\n");
850
851 fprintf(out, "\n");
852 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000853 return 0;
854}
855
Yao Chend54f9dd2017-10-17 17:37:48 +0000856static void
857print_usage()
858{
859 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
860 fprintf(stderr, "\n");
861 fprintf(stderr, "OPTIONS\n");
862 fprintf(stderr, " --cpp FILENAME the header file to output\n");
863 fprintf(stderr, " --header FILENAME the cpp file to output\n");
864 fprintf(stderr, " --help this message\n");
865 fprintf(stderr, " --java FILENAME the java file to output\n");
866 fprintf(stderr, " --jni FILENAME the jni file to output\n");
867}
868
869/**
870 * Do the argument parsing and execute the tasks.
871 */
872static int
873run(int argc, char const*const* argv)
874{
875 string cppFilename;
876 string headerFilename;
877 string javaFilename;
878 string jniFilename;
879
880 int index = 1;
881 while (index < argc) {
882 if (0 == strcmp("--help", argv[index])) {
883 print_usage();
884 return 0;
885 } else if (0 == strcmp("--cpp", argv[index])) {
886 index++;
887 if (index >= argc) {
888 print_usage();
889 return 1;
890 }
891 cppFilename = argv[index];
892 } else if (0 == strcmp("--header", argv[index])) {
893 index++;
894 if (index >= argc) {
895 print_usage();
896 return 1;
897 }
898 headerFilename = argv[index];
899 } else if (0 == strcmp("--java", argv[index])) {
900 index++;
901 if (index >= argc) {
902 print_usage();
903 return 1;
904 }
905 javaFilename = argv[index];
906 } else if (0 == strcmp("--jni", argv[index])) {
907 index++;
908 if (index >= argc) {
909 print_usage();
910 return 1;
911 }
912 jniFilename = argv[index];
913 }
914 index++;
915 }
916
917 if (cppFilename.size() == 0
918 && headerFilename.size() == 0
919 && javaFilename.size() == 0
920 && jniFilename.size() == 0) {
921 print_usage();
922 return 1;
923 }
924
925 // Collate the parameters
926 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -0800927 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +0000928 if (errorCount != 0) {
929 return 1;
930 }
931
Yangster-mac7604aea2017-12-11 22:55:49 -0800932 AtomDecl attributionDecl;
933 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -0800934 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -0800935 &attributionDecl, &attributionSignature);
936
Yao Chend54f9dd2017-10-17 17:37:48 +0000937 // Write the .cpp file
938 if (cppFilename.size() != 0) {
939 FILE* out = fopen(cppFilename.c_str(), "w");
940 if (out == NULL) {
941 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
942 return 1;
943 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800944 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
945 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000946 fclose(out);
947 }
948
949 // Write the .h file
950 if (headerFilename.size() != 0) {
951 FILE* out = fopen(headerFilename.c_str(), "w");
952 if (out == NULL) {
953 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
954 return 1;
955 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800956 errorCount = android::stats_log_api_gen::write_stats_log_header(
957 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000958 fclose(out);
959 }
960
961 // Write the .java file
962 if (javaFilename.size() != 0) {
963 FILE* out = fopen(javaFilename.c_str(), "w");
964 if (out == NULL) {
965 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
966 return 1;
967 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800968 errorCount = android::stats_log_api_gen::write_stats_log_java(
969 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000970 fclose(out);
971 }
972
973 // Write the jni file
974 if (jniFilename.size() != 0) {
975 FILE* out = fopen(jniFilename.c_str(), "w");
976 if (out == NULL) {
977 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
978 return 1;
979 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800980 errorCount = android::stats_log_api_gen::write_stats_log_jni(
981 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000982 fclose(out);
983 }
984
985 return 0;
986}
987
988}
989}
990
991/**
992 * Main.
993 */
994int
995main(int argc, char const*const* argv)
996{
997 GOOGLE_PROTOBUF_VERIFY_VERSION;
998
999 return android::stats_log_api_gen::run(argc, argv);
Yangster-mac7604aea2017-12-11 22:55:49 -08001000}