blob: f0628c099e2ec69f648583d67bf16e4ab69cb3ec [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");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800217 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800218 fprintf(out, " event << code;\n\n");
219 for (vector<java_type_t>::const_iterator arg = signature->begin();
220 arg != signature->end(); arg++) {
221 if (argIndex == 1) {
222 fprintf(out, " event.begin();\n\n");
223 fprintf(out, " event.begin();\n");
224 }
225 if (*arg == JAVA_TYPE_STRING) {
226 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
227 fprintf(out, " arg%d = \"\";\n", argIndex);
228 fprintf(out, " }\n");
229 }
230 fprintf(out, " event << arg%d;\n", argIndex);
231 if (argIndex == 2) {
232 fprintf(out, " event.end();\n\n");
233 fprintf(out, " event.end();\n\n");
234 }
235 argIndex++;
236 }
237
238 fprintf(out, " event.write(LOG_ID_STATS);\n");
239 fprintf(out, "}\n");
240 fprintf(out, "\n");
241 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000242 // Print footer
243 fprintf(out, "\n");
244 fprintf(out, "} // namespace util\n");
245 fprintf(out, "} // namespace android\n");
246
247 return 0;
248}
249
Yangster-macba5b9e42018-01-10 21:31:59 -0800250void build_non_chained_decl_map(const Atoms& atoms,
251 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
252 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
253 atom != atoms.non_chained_decls.end(); atom++) {
254 decl_map->insert(std::make_pair(atom->code, atom));
255 }
256}
257
258static void write_cpp_usage(
259 FILE* out, const string& method_name, const string& atom_code_name,
260 const AtomDecl& atom, const AtomDecl &attributionDecl) {
261 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str());
262 for (vector<AtomField>::const_iterator field = atom.fields.begin();
263 field != atom.fields.end(); field++) {
264 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
265 for (auto chainField : attributionDecl.fields) {
266 if (chainField.javaType == JAVA_TYPE_STRING) {
267 fprintf(out, ", const std::vector<%s>& %s",
268 cpp_type_name(chainField.javaType),
269 chainField.name.c_str());
270 } else {
271 fprintf(out, ", const %s* %s, size_t %s_length",
272 cpp_type_name(chainField.javaType),
273 chainField.name.c_str(), chainField.name.c_str());
274 }
275 }
276 } else {
277 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
278 }
279 }
280 fprintf(out, ");\n");
281}
282
283static void write_cpp_method_header(
284 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
285 const AtomDecl &attributionDecl) {
286 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
287 signature != signatures.end(); signature++) {
288 fprintf(out, "void %s(int32_t code ", method_name.c_str());
289 int argIndex = 1;
290 for (vector<java_type_t>::const_iterator arg = signature->begin();
291 arg != signature->end(); arg++) {
292 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
293 for (auto chainField : attributionDecl.fields) {
294 if (chainField.javaType == JAVA_TYPE_STRING) {
295 fprintf(out, ", const std::vector<%s>& %s",
296 cpp_type_name(chainField.javaType), chainField.name.c_str());
297 } else {
298 fprintf(out, ", const %s* %s, size_t %s_length",
299 cpp_type_name(chainField.javaType),
300 chainField.name.c_str(), chainField.name.c_str());
301 }
302 }
303 } else {
304 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
305 }
306 argIndex++;
307 }
308 fprintf(out, ");\n");
309
310 }
311}
Yao Chend54f9dd2017-10-17 17:37:48 +0000312
313static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800314write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000315{
Yao Chend54f9dd2017-10-17 17:37:48 +0000316 // Print prelude
317 fprintf(out, "// This file is autogenerated\n");
318 fprintf(out, "\n");
319 fprintf(out, "#pragma once\n");
320 fprintf(out, "\n");
321 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800322 fprintf(out, "#include <vector>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800323 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000324 fprintf(out, "\n");
325
326 fprintf(out, "namespace android {\n");
327 fprintf(out, "namespace util {\n");
328 fprintf(out, "\n");
329 fprintf(out, "/*\n");
330 fprintf(out, " * API For logging statistics events.\n");
331 fprintf(out, " */\n");
332 fprintf(out, "\n");
333 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700334 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000335 fprintf(out, " */\n");
336 fprintf(out, "enum {\n");
337
Yangster-macba5b9e42018-01-10 21:31:59 -0800338 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
339 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
340
Yao Chend54f9dd2017-10-17 17:37:48 +0000341 size_t i = 0;
342 // Print constants
343 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800344 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000345 string constant = make_constant_name(atom->name);
346 fprintf(out, "\n");
347 fprintf(out, " /**\n");
348 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800349 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
350
351 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
352 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
353 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
354 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000355 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000356 fprintf(out, " */\n");
357 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
358 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800359 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
360 maxPushedAtomId = atom->code;
361 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000362 i++;
363 }
364 fprintf(out, "\n");
365 fprintf(out, "};\n");
366 fprintf(out, "\n");
367
Yangster-mac68985802018-01-21 10:05:09 -0800368 fprintf(out, "const static std::set<int> kAtomsWithUidField = {\n");
369 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
370 atom != atoms.decls.end(); atom++) {
371 for (vector<AtomField>::const_iterator field = atom->fields.begin();
372 field != atom->fields.end(); field++) {
373 if (field->name == "uid") {
374 string constant = make_constant_name(atom->name);
375 fprintf(out, " %s,\n", constant.c_str());
376 break;
377 }
378 }
379 }
380 fprintf(out, "};\n");
381 fprintf(out, "\n");
382
383 fprintf(out, "const static std::set<int> kAtomsWithAttributionChain = {\n");
384 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
385 atom != atoms.decls.end(); atom++) {
386 for (vector<AtomField>::const_iterator field = atom->fields.begin();
387 field != atom->fields.end(); field++) {
388 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
389 string constant = make_constant_name(atom->name);
390 fprintf(out, " %s,\n", constant.c_str());
391 break;
392 }
393 }
394 }
395 fprintf(out, "};\n");
396 fprintf(out, "\n");
397
Yangster-mac7604aea2017-12-11 22:55:49 -0800398 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId);
Yao Chenb3561512017-11-21 18:07:17 -0800399
Yao Chend54f9dd2017-10-17 17:37:48 +0000400 // Print write methods
401 fprintf(out, "//\n");
402 fprintf(out, "// Write methods\n");
403 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800404 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
405
406 fprintf(out, "//\n");
407 fprintf(out, "// Write flattened methods\n");
408 fprintf(out, "//\n");
409 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
410 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000411
412 fprintf(out, "\n");
413 fprintf(out, "} // namespace util\n");
414 fprintf(out, "} // namespace android\n");
415
416 return 0;
417}
418
Yangster-macba5b9e42018-01-10 21:31:59 -0800419static void write_java_usage(
420 FILE* out, const string& method_name, const string& atom_code_name,
421 const AtomDecl& atom, const AtomDecl &attributionDecl) {
422 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
423 method_name.c_str(), atom_code_name.c_str());
424 for (vector<AtomField>::const_iterator field = atom.fields.begin();
425 field != atom.fields.end(); field++) {
426 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
427 for (auto chainField : attributionDecl.fields) {
428 fprintf(out, ", %s[] %s",
429 java_type_name(chainField.javaType), chainField.name.c_str());
430 }
431 } else {
432 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
433 }
434 }
435 fprintf(out, ");\n");
436}
437
438static void write_java_method(
439 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
440 const AtomDecl &attributionDecl) {
441 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
442 signature != signatures.end(); signature++) {
443 fprintf(out, " public static native void %s(int code", method_name.c_str());
444 int argIndex = 1;
445 for (vector<java_type_t>::const_iterator arg = signature->begin();
446 arg != signature->end(); arg++) {
447 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
448 for (auto chainField : attributionDecl.fields) {
449 fprintf(out, ", %s[] %s",
450 java_type_name(chainField.javaType), chainField.name.c_str());
451 }
452 } else {
453 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
454 }
455 argIndex++;
456 }
457 fprintf(out, ");\n");
458 }
459}
460
461
Yao Chend54f9dd2017-10-17 17:37:48 +0000462static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800463write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000464{
Yao Chend54f9dd2017-10-17 17:37:48 +0000465 // Print prelude
466 fprintf(out, "// This file is autogenerated\n");
467 fprintf(out, "\n");
468 fprintf(out, "package android.util;\n");
469 fprintf(out, "\n");
470 fprintf(out, "\n");
471 fprintf(out, "/**\n");
472 fprintf(out, " * API For logging statistics events.\n");
473 fprintf(out, " * @hide\n");
474 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800475 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700476 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000477
Yangster-macba5b9e42018-01-10 21:31:59 -0800478 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
479 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
480
Stefan Lafon9478f352017-10-30 21:20:20 -0700481 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000482 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
483 atom != atoms.decls.end(); atom++) {
484 string constant = make_constant_name(atom->name);
485 fprintf(out, "\n");
486 fprintf(out, " /**\n");
487 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800488 write_java_usage(out, "write", constant, *atom, attributionDecl);
489 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
490 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
491 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second,
492 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000493 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000494 fprintf(out, " */\n");
495 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
496 }
497 fprintf(out, "\n");
498
Stefan Lafon9478f352017-10-30 21:20:20 -0700499 // Print constants for the enum values.
500 fprintf(out, " // Constants for enum values.\n\n");
501 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800502 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700503 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800504 field != atom->fields.end(); field++) {
505 if (field->javaType == JAVA_TYPE_ENUM) {
506 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
507 field->name.c_str());
508 for (map<int, string>::const_iterator value = field->enumValues.begin();
509 value != field->enumValues.end(); value++) {
510 fprintf(out, " public static final int %s__%s__%s = %d;\n",
511 make_constant_name(atom->message).c_str(),
512 make_constant_name(field->name).c_str(),
513 make_constant_name(value->second).c_str(),
514 value->first);
515 }
516 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700517 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700518 }
519 }
520
Yao Chend54f9dd2017-10-17 17:37:48 +0000521 // Print write methods
522 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800523 write_java_method(out, "write", atoms.signatures, attributionDecl);
524 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000525
526 fprintf(out, "}\n");
527
528 return 0;
529}
530
531static const char*
532jni_type_name(java_type_t type)
533{
534 switch (type) {
535 case JAVA_TYPE_BOOLEAN:
536 return "jboolean";
537 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700538 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000539 return "jint";
540 case JAVA_TYPE_LONG:
541 return "jlong";
542 case JAVA_TYPE_FLOAT:
543 return "jfloat";
544 case JAVA_TYPE_DOUBLE:
545 return "jdouble";
546 case JAVA_TYPE_STRING:
547 return "jstring";
548 default:
549 return "UNKNOWN";
550 }
551}
552
Yangster-mac7604aea2017-12-11 22:55:49 -0800553static const char*
554jni_array_type_name(java_type_t type)
555{
556 switch (type) {
557 case JAVA_TYPE_INT:
558 return "jintArray";
559 case JAVA_TYPE_STRING:
560 return "jobjectArray";
561 default:
562 return "UNKNOWN";
563 }
564}
565
Yao Chend54f9dd2017-10-17 17:37:48 +0000566static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800567jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000568{
Yangster-macba5b9e42018-01-10 21:31:59 -0800569 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000570 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800571 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000572 switch (*arg) {
573 case JAVA_TYPE_BOOLEAN:
574 result += "_boolean";
575 break;
576 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700577 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000578 result += "_int";
579 break;
580 case JAVA_TYPE_LONG:
581 result += "_long";
582 break;
583 case JAVA_TYPE_FLOAT:
584 result += "_float";
585 break;
586 case JAVA_TYPE_DOUBLE:
587 result += "_double";
588 break;
589 case JAVA_TYPE_STRING:
590 result += "_String";
591 break;
Yangster-mac7604aea2017-12-11 22:55:49 -0800592 case JAVA_TYPE_ATTRIBUTION_CHAIN:
593 result += "_AttributionChain";
594 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000595 default:
596 result += "_UNKNOWN";
597 break;
598 }
599 }
600 return result;
601}
602
603static const char*
604java_type_signature(java_type_t type)
605{
606 switch (type) {
607 case JAVA_TYPE_BOOLEAN:
608 return "Z";
609 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700610 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000611 return "I";
612 case JAVA_TYPE_LONG:
613 return "J";
614 case JAVA_TYPE_FLOAT:
615 return "F";
616 case JAVA_TYPE_DOUBLE:
617 return "D";
618 case JAVA_TYPE_STRING:
619 return "Ljava/lang/String;";
620 default:
621 return "UNKNOWN";
622 }
623}
624
625static string
Yangster-mac7604aea2017-12-11 22:55:49 -0800626jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000627{
628 string result("(I");
629 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800630 arg != signature.end(); arg++) {
631 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
632 for (auto chainField : attributionDecl.fields) {
633 result += "[";
634 result += java_type_signature(chainField.javaType);
635 }
636 } else {
637 result += java_type_signature(*arg);
638 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000639 }
640 result += ")V";
641 return result;
642}
643
644static int
Yangster-macba5b9e42018-01-10 21:31:59 -0800645write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
646 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000647{
Yao Chend54f9dd2017-10-17 17:37:48 +0000648 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -0800649 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
650 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000651 int argIndex;
652
653 fprintf(out, "static void\n");
654 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -0800655 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000656 argIndex = 1;
657 for (vector<java_type_t>::const_iterator arg = signature->begin();
658 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800659 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
660 for (auto chainField : attributionDecl.fields) {
661 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
662 chainField.name.c_str());
663 }
664 } else {
665 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
666 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000667 argIndex++;
668 }
669 fprintf(out, ")\n");
670
671 fprintf(out, "{\n");
672
673 // Prepare strings
674 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -0800675 bool hadStringOrChain = false;
Yao Chend54f9dd2017-10-17 17:37:48 +0000676 for (vector<java_type_t>::const_iterator arg = signature->begin();
677 arg != signature->end(); arg++) {
678 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800679 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +0000680 fprintf(out, " const char* str%d;\n", argIndex);
681 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
682 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
683 argIndex, argIndex);
684 fprintf(out, " } else {\n");
685 fprintf(out, " str%d = NULL;\n", argIndex);
686 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800687 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
688 hadStringOrChain = true;
689 for (auto chainField : attributionDecl.fields) {
690 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
691 chainField.name.c_str(), chainField.name.c_str());
692 if (chainField.name != attributionDecl.fields.front().name) {
693 fprintf(out, " if (%s_length != %s_length) {\n",
694 chainField.name.c_str(),
695 attributionDecl.fields.front().name.c_str());
696 fprintf(out, " jniThrowException(env, "
697 "\"java/lang/IllegalArgumentException\", "
698 "\"invalid attribution field(%s) length.\");\n",
699 chainField.name.c_str());
700 fprintf(out, " return;\n");
701 fprintf(out, " }\n");
702 }
703 if (chainField.javaType == JAVA_TYPE_INT) {
704 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
705 chainField.name.c_str(), chainField.name.c_str());
706 } else if (chainField.javaType == JAVA_TYPE_STRING) {
707 fprintf(out, " std::vector<%s> %s_vec;\n",
708 cpp_type_name(chainField.javaType), chainField.name.c_str());
709 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
710 chainField.name.c_str());
711 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
712 chainField.name.c_str());
713 fprintf(out, " jstring jstr = "
714 "(jstring)env->GetObjectArrayElement(%s, i);\n",
715 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800716 fprintf(out, " if (jstr == NULL) {\n");
717 fprintf(out, " %s_vec.push_back(NULL);\n",
718 chainField.name.c_str());
719 fprintf(out, " } else {\n");
720 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -0800721 "new ScopedUtfChars(env, jstr);\n",
722 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800723 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800724 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800725 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800726 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800727 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800728 fprintf(out, " }\n");
729 }
730 fprintf(out, "\n");
731 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000732 }
733 argIndex++;
734 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800735 // Emit this to quiet the unused parameter warning if there were no strings or attribution
736 // chains.
737 if (!hadStringOrChain) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000738 fprintf(out, " (void)env;\n");
739 }
740
741 // stats_write call
742 argIndex = 1;
Yangster-macba5b9e42018-01-10 21:31:59 -0800743 fprintf(out, " android::util::%s(code", cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000744 for (vector<java_type_t>::const_iterator arg = signature->begin();
745 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800746 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
747 for (auto chainField : attributionDecl.fields) {
748 if (chainField.javaType == JAVA_TYPE_INT) {
749 fprintf(out, ", (const %s*)%s_array, %s_length",
750 cpp_type_name(chainField.javaType),
751 chainField.name.c_str(), chainField.name.c_str());
752 } else if (chainField.javaType == JAVA_TYPE_STRING) {
753 fprintf(out, ", %s_vec", chainField.name.c_str());
754 }
755 }
756 } else {
757 const char *argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
758 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
759 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000760 argIndex++;
761 }
762 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800763 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000764
765 // Clean up strings
766 argIndex = 1;
767 for (vector<java_type_t>::const_iterator arg = signature->begin();
768 arg != signature->end(); arg++) {
769 if (*arg == JAVA_TYPE_STRING) {
770 fprintf(out, " if (str%d != NULL) {\n", argIndex);
771 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
772 argIndex, argIndex);
773 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800774 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
775 for (auto chainField : attributionDecl.fields) {
776 if (chainField.javaType == JAVA_TYPE_INT) {
777 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
778 chainField.name.c_str(), chainField.name.c_str());
779 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800780 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800781 chainField.name.c_str());
782 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
783 fprintf(out, " }\n");
784 }
785 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000786 }
787 argIndex++;
788 }
789
790 fprintf(out, "}\n");
791 fprintf(out, "\n");
792 }
793
Yangster-macba5b9e42018-01-10 21:31:59 -0800794
795 return 0;
796}
797
798void write_jni_registration(FILE* out, const string& java_method_name,
799 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
800 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
801 signature != signatures.end(); signature++) {
802 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
803 java_method_name.c_str(),
804 jni_function_signature(*signature, attributionDecl).c_str(),
805 jni_function_name(java_method_name, *signature).c_str());
806 }
807}
808
809static int
810write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
811{
812 // Print prelude
813 fprintf(out, "// This file is autogenerated\n");
814 fprintf(out, "\n");
815
816 fprintf(out, "#include <statslog.h>\n");
817 fprintf(out, "\n");
818 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
819 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
820 fprintf(out, "#include <utils/Vector.h>\n");
821 fprintf(out, "#include \"core_jni_helpers.h\"\n");
822 fprintf(out, "#include \"jni.h\"\n");
823 fprintf(out, "\n");
824 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
825 fprintf(out, "\n");
826
827 fprintf(out, "namespace android {\n");
828 fprintf(out, "\n");
829
830 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
831 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
832 atoms.non_chained_signatures, attributionDecl);
833
Yao Chend54f9dd2017-10-17 17:37:48 +0000834 // Print registration function table
835 fprintf(out, "/*\n");
836 fprintf(out, " * JNI registration.\n");
837 fprintf(out, " */\n");
838 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800839 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
840 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000841 fprintf(out, "};\n");
842 fprintf(out, "\n");
843
844 // Print registration function
845 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
846 fprintf(out, " return RegisterMethodsOrDie(\n");
847 fprintf(out, " env,\n");
848 fprintf(out, " \"android/util/StatsLog\",\n");
849 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
850 fprintf(out, "}\n");
851
852 fprintf(out, "\n");
853 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000854 return 0;
855}
856
Yao Chend54f9dd2017-10-17 17:37:48 +0000857static void
858print_usage()
859{
860 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
861 fprintf(stderr, "\n");
862 fprintf(stderr, "OPTIONS\n");
863 fprintf(stderr, " --cpp FILENAME the header file to output\n");
864 fprintf(stderr, " --header FILENAME the cpp file to output\n");
865 fprintf(stderr, " --help this message\n");
866 fprintf(stderr, " --java FILENAME the java file to output\n");
867 fprintf(stderr, " --jni FILENAME the jni file to output\n");
868}
869
870/**
871 * Do the argument parsing and execute the tasks.
872 */
873static int
874run(int argc, char const*const* argv)
875{
876 string cppFilename;
877 string headerFilename;
878 string javaFilename;
879 string jniFilename;
880
881 int index = 1;
882 while (index < argc) {
883 if (0 == strcmp("--help", argv[index])) {
884 print_usage();
885 return 0;
886 } else if (0 == strcmp("--cpp", argv[index])) {
887 index++;
888 if (index >= argc) {
889 print_usage();
890 return 1;
891 }
892 cppFilename = argv[index];
893 } else if (0 == strcmp("--header", argv[index])) {
894 index++;
895 if (index >= argc) {
896 print_usage();
897 return 1;
898 }
899 headerFilename = argv[index];
900 } else if (0 == strcmp("--java", argv[index])) {
901 index++;
902 if (index >= argc) {
903 print_usage();
904 return 1;
905 }
906 javaFilename = argv[index];
907 } else if (0 == strcmp("--jni", argv[index])) {
908 index++;
909 if (index >= argc) {
910 print_usage();
911 return 1;
912 }
913 jniFilename = argv[index];
914 }
915 index++;
916 }
917
918 if (cppFilename.size() == 0
919 && headerFilename.size() == 0
920 && javaFilename.size() == 0
921 && jniFilename.size() == 0) {
922 print_usage();
923 return 1;
924 }
925
926 // Collate the parameters
927 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -0800928 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +0000929 if (errorCount != 0) {
930 return 1;
931 }
932
Yangster-mac7604aea2017-12-11 22:55:49 -0800933 AtomDecl attributionDecl;
934 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -0800935 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -0800936 &attributionDecl, &attributionSignature);
937
Yao Chend54f9dd2017-10-17 17:37:48 +0000938 // Write the .cpp file
939 if (cppFilename.size() != 0) {
940 FILE* out = fopen(cppFilename.c_str(), "w");
941 if (out == NULL) {
942 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
943 return 1;
944 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800945 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
946 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000947 fclose(out);
948 }
949
950 // Write the .h file
951 if (headerFilename.size() != 0) {
952 FILE* out = fopen(headerFilename.c_str(), "w");
953 if (out == NULL) {
954 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
955 return 1;
956 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800957 errorCount = android::stats_log_api_gen::write_stats_log_header(
958 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000959 fclose(out);
960 }
961
962 // Write the .java file
963 if (javaFilename.size() != 0) {
964 FILE* out = fopen(javaFilename.c_str(), "w");
965 if (out == NULL) {
966 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
967 return 1;
968 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800969 errorCount = android::stats_log_api_gen::write_stats_log_java(
970 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000971 fclose(out);
972 }
973
974 // Write the jni file
975 if (jniFilename.size() != 0) {
976 FILE* out = fopen(jniFilename.c_str(), "w");
977 if (out == NULL) {
978 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
979 return 1;
980 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800981 errorCount = android::stats_log_api_gen::write_stats_log_jni(
982 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000983 fclose(out);
984 }
985
986 return 0;
987}
988
989}
990}
991
992/**
993 * Main.
994 */
995int
996main(int argc, char const*const* argv)
997{
998 GOOGLE_PROTOBUF_VERIFY_VERSION;
999
1000 return android::stats_log_api_gen::run(argc, argv);
Yangster-mac7604aea2017-12-11 22:55:49 -08001001}