1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
#include "Collation.h"
#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
#include <set>
#include <vector>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace google::protobuf;
using namespace std;
namespace android {
namespace stats_log_api_gen {
const int PULL_ATOM_START_ID = 1000;
int maxPushedAtomId = 2;
using android::os::statsd::Atom;
// TODO: Support WorkSources
/**
* Turn lower and camel case into upper case with underscores.
*/
static string
make_constant_name(const string& str)
{
string result;
const int N = str.size();
bool underscore_next = false;
for (int i=0; i<N; i++) {
char c = str[i];
if (c >= 'A' && c <= 'Z') {
if (underscore_next) {
result += '_';
underscore_next = false;
}
} else if (c >= 'a' && c <= 'z') {
c = 'A' + c - 'a';
underscore_next = true;
} else if (c == '_') {
underscore_next = false;
}
result += c;
}
return result;
}
static const char*
cpp_type_name(java_type_t type)
{
switch (type) {
case JAVA_TYPE_BOOLEAN:
return "bool";
case JAVA_TYPE_INT:
case JAVA_TYPE_ENUM:
return "int32_t";
case JAVA_TYPE_LONG:
return "int64_t";
case JAVA_TYPE_FLOAT:
return "float";
case JAVA_TYPE_DOUBLE:
return "double";
case JAVA_TYPE_STRING:
return "char const*";
default:
return "UNKNOWN";
}
}
static const char*
java_type_name(java_type_t type)
{
switch (type) {
case JAVA_TYPE_BOOLEAN:
return "boolean";
case JAVA_TYPE_INT:
case JAVA_TYPE_ENUM:
return "int";
case JAVA_TYPE_LONG:
return "long";
case JAVA_TYPE_FLOAT:
return "float";
case JAVA_TYPE_DOUBLE:
return "double";
case JAVA_TYPE_STRING:
return "java.lang.String";
default:
return "UNKNOWN";
}
}
static int
write_stats_log_cpp(FILE* out, const Atoms& atoms)
{
// Print prelude
fprintf(out, "// This file is autogenerated\n");
fprintf(out, "\n");
fprintf(out, "#include <log/log_event_list.h>\n");
fprintf(out, "#include <log/log.h>\n");
fprintf(out, "#include <statslog.h>\n");
fprintf(out, "\n");
fprintf(out, "namespace android {\n");
fprintf(out, "namespace util {\n");
fprintf(out, "// the single event tag id for all stats logs\n");
fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
// Print write methods
fprintf(out, "\n");
for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
signature != atoms.signatures.end(); signature++) {
int argIndex;
fprintf(out, "void\n");
fprintf(out, "stats_write(int32_t code");
argIndex = 1;
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
argIndex++;
}
fprintf(out, ")\n");
fprintf(out, "{\n");
argIndex = 1;
fprintf(out, " android_log_event_list event(kStatsEventTag);\n");
fprintf(out, " event << code;\n");
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
if (*arg == JAVA_TYPE_STRING) {
fprintf(out, " if (arg%d == NULL) {\n", argIndex);
fprintf(out, " arg%d = \"\";\n", argIndex);
fprintf(out, " }\n");
}
fprintf(out, " event << arg%d;\n", argIndex);
argIndex++;
}
fprintf(out, " event.write(LOG_ID_STATS);\n");
fprintf(out, "}\n");
fprintf(out, "\n");
}
// Print footer
fprintf(out, "\n");
fprintf(out, "} // namespace util\n");
fprintf(out, "} // namespace android\n");
return 0;
}
static int
write_stats_log_header(FILE* out, const Atoms& atoms)
{
// Print prelude
fprintf(out, "// This file is autogenerated\n");
fprintf(out, "\n");
fprintf(out, "#pragma once\n");
fprintf(out, "\n");
fprintf(out, "#include <stdint.h>\n");
fprintf(out, "\n");
fprintf(out, "namespace android {\n");
fprintf(out, "namespace util {\n");
fprintf(out, "\n");
fprintf(out, "/*\n");
fprintf(out, " * API For logging statistics events.\n");
fprintf(out, " */\n");
fprintf(out, "\n");
fprintf(out, "/**\n");
fprintf(out, " * Constants for atom codes.\n");
fprintf(out, " */\n");
fprintf(out, "enum {\n");
size_t i = 0;
// Print constants
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
atom != atoms.decls.end(); atom++) {
string constant = make_constant_name(atom->name);
fprintf(out, "\n");
fprintf(out, " /**\n");
fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
fprintf(out, " * Usage: stats_write(StatsLog.%s", constant.c_str());
for (vector<AtomField>::const_iterator field = atom->fields.begin();
field != atom->fields.end(); field++) {
fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
}
fprintf(out, ");\n");
fprintf(out, " */\n");
char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
maxPushedAtomId = atom->code;
}
i++;
}
fprintf(out, "\n");
fprintf(out, "};\n");
fprintf(out, "\n");
fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
maxPushedAtomId);
// Print write methods
fprintf(out, "//\n");
fprintf(out, "// Write methods\n");
fprintf(out, "//\n");
for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
signature != atoms.signatures.end(); signature++) {
fprintf(out, "void stats_write(int32_t code ");
int argIndex = 1;
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
argIndex++;
}
fprintf(out, ");\n");
}
fprintf(out, "\n");
fprintf(out, "} // namespace util\n");
fprintf(out, "} // namespace android\n");
return 0;
}
static int
write_stats_log_java(FILE* out, const Atoms& atoms)
{
// Print prelude
fprintf(out, "// This file is autogenerated\n");
fprintf(out, "\n");
fprintf(out, "package android.util;\n");
fprintf(out, "\n");
fprintf(out, "\n");
fprintf(out, "/**\n");
fprintf(out, " * API For logging statistics events.\n");
fprintf(out, " * @hide\n");
fprintf(out, " */\n");
fprintf(out, "public final class StatsLog {\n");
fprintf(out, " // Constants for atom codes.\n");
// Print constants for the atom codes.
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
atom != atoms.decls.end(); atom++) {
string constant = make_constant_name(atom->name);
fprintf(out, "\n");
fprintf(out, " /**\n");
fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
fprintf(out, " * Usage: StatsLog.write(StatsLog.%s", constant.c_str());
for (vector<AtomField>::const_iterator field = atom->fields.begin();
field != atom->fields.end(); field++) {
fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
}
fprintf(out, ");\n");
fprintf(out, " */\n");
fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
}
fprintf(out, "\n");
// Print constants for the enum values.
fprintf(out, " // Constants for enum values.\n\n");
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
atom != atoms.decls.end(); atom++) {
for (vector<AtomField>::const_iterator field = atom->fields.begin();
field != atom->fields.end(); field++) {
if (field->javaType == JAVA_TYPE_ENUM) {
fprintf(out, " // Values for %s.%s\n", atom->message.c_str(), field->name.c_str());
for (map<int, string>::const_iterator value = field->enumValues.begin();
value != field->enumValues.end(); value++) {
fprintf(out, " public static final int %s__%s__%s = %d;\n",
make_constant_name(atom->message).c_str(),
make_constant_name(field->name).c_str(),
make_constant_name(value->second).c_str(),
value->first);
}
fprintf(out, "\n");
}
}
}
// Print write methods
fprintf(out, " // Write methods\n");
for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
signature != atoms.signatures.end(); signature++) {
fprintf(out, " public static native void write(int code");
int argIndex = 1;
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
argIndex++;
}
fprintf(out, ");\n");
}
fprintf(out, "}\n");
return 0;
}
static const char*
jni_type_name(java_type_t type)
{
switch (type) {
case JAVA_TYPE_BOOLEAN:
return "jboolean";
case JAVA_TYPE_INT:
case JAVA_TYPE_ENUM:
return "jint";
case JAVA_TYPE_LONG:
return "jlong";
case JAVA_TYPE_FLOAT:
return "jfloat";
case JAVA_TYPE_DOUBLE:
return "jdouble";
case JAVA_TYPE_STRING:
return "jstring";
default:
return "UNKNOWN";
}
}
static string
jni_function_name(const vector<java_type_t>& signature)
{
string result("StatsLog_write");
for (vector<java_type_t>::const_iterator arg = signature.begin();
arg != signature.end(); arg++) {
switch (*arg) {
case JAVA_TYPE_BOOLEAN:
result += "_boolean";
break;
case JAVA_TYPE_INT:
case JAVA_TYPE_ENUM:
result += "_int";
break;
case JAVA_TYPE_LONG:
result += "_long";
break;
case JAVA_TYPE_FLOAT:
result += "_float";
break;
case JAVA_TYPE_DOUBLE:
result += "_double";
break;
case JAVA_TYPE_STRING:
result += "_String";
break;
default:
result += "_UNKNOWN";
break;
}
}
return result;
}
static const char*
java_type_signature(java_type_t type)
{
switch (type) {
case JAVA_TYPE_BOOLEAN:
return "Z";
case JAVA_TYPE_INT:
case JAVA_TYPE_ENUM:
return "I";
case JAVA_TYPE_LONG:
return "J";
case JAVA_TYPE_FLOAT:
return "F";
case JAVA_TYPE_DOUBLE:
return "D";
case JAVA_TYPE_STRING:
return "Ljava/lang/String;";
default:
return "UNKNOWN";
}
}
static string
jni_function_signature(const vector<java_type_t>& signature)
{
string result("(I");
for (vector<java_type_t>::const_iterator arg = signature.begin();
arg != signature.end(); arg++) {
result += java_type_signature(*arg);
}
result += ")V";
return result;
}
static int
write_stats_log_jni(FILE* out, const Atoms& atoms)
{
// Print prelude
fprintf(out, "// This file is autogenerated\n");
fprintf(out, "\n");
fprintf(out, "#include <statslog.h>\n");
fprintf(out, "\n");
fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
fprintf(out, "#include \"core_jni_helpers.h\"\n");
fprintf(out, "#include \"jni.h\"\n");
fprintf(out, "\n");
fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
fprintf(out, "\n");
fprintf(out, "namespace android {\n");
fprintf(out, "\n");
// Print write methods
for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
signature != atoms.signatures.end(); signature++) {
int argIndex;
fprintf(out, "static void\n");
fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
jni_function_name(*signature).c_str());
argIndex = 1;
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
argIndex++;
}
fprintf(out, ")\n");
fprintf(out, "{\n");
// Prepare strings
argIndex = 1;
bool hadString = false;
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
if (*arg == JAVA_TYPE_STRING) {
fprintf(out, " const char* str%d;\n", argIndex);
fprintf(out, " if (arg%d != NULL) {\n", argIndex);
fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
argIndex, argIndex);
fprintf(out, " } else {\n");
fprintf(out, " str%d = NULL;\n", argIndex);
fprintf(out, " }\n");
hadString = true;
}
argIndex++;
}
// Emit this to quiet the unused parameter warning if there were no strings.
if (!hadString) {
fprintf(out, " (void)env;\n");
}
// stats_write call
argIndex = 1;
fprintf(out, " android::util::stats_write(code");
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
const char* argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
argIndex++;
}
fprintf(out, ");\n");
// Clean up strings
argIndex = 1;
for (vector<java_type_t>::const_iterator arg = signature->begin();
arg != signature->end(); arg++) {
if (*arg == JAVA_TYPE_STRING) {
fprintf(out, " if (str%d != NULL) {\n", argIndex);
fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
argIndex, argIndex);
fprintf(out, " }\n");
}
argIndex++;
}
fprintf(out, "}\n");
fprintf(out, "\n");
}
// Print registration function table
fprintf(out, "/*\n");
fprintf(out, " * JNI registration.\n");
fprintf(out, " */\n");
fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
signature != atoms.signatures.end(); signature++) {
fprintf(out, " { \"write\", \"%s\", (void*)%s },\n",
jni_function_signature(*signature).c_str(),
jni_function_name(*signature).c_str());
}
fprintf(out, "};\n");
fprintf(out, "\n");
// Print registration function
fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
fprintf(out, " return RegisterMethodsOrDie(\n");
fprintf(out, " env,\n");
fprintf(out, " \"android/util/StatsLog\",\n");
fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
fprintf(out, "}\n");
fprintf(out, "\n");
fprintf(out, "} // namespace android\n");
return 0;
}
static void
print_usage()
{
fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
fprintf(stderr, "\n");
fprintf(stderr, "OPTIONS\n");
fprintf(stderr, " --cpp FILENAME the header file to output\n");
fprintf(stderr, " --header FILENAME the cpp file to output\n");
fprintf(stderr, " --help this message\n");
fprintf(stderr, " --java FILENAME the java file to output\n");
fprintf(stderr, " --jni FILENAME the jni file to output\n");
}
/**
* Do the argument parsing and execute the tasks.
*/
static int
run(int argc, char const*const* argv)
{
string cppFilename;
string headerFilename;
string javaFilename;
string jniFilename;
int index = 1;
while (index < argc) {
if (0 == strcmp("--help", argv[index])) {
print_usage();
return 0;
} else if (0 == strcmp("--cpp", argv[index])) {
index++;
if (index >= argc) {
print_usage();
return 1;
}
cppFilename = argv[index];
} else if (0 == strcmp("--header", argv[index])) {
index++;
if (index >= argc) {
print_usage();
return 1;
}
headerFilename = argv[index];
} else if (0 == strcmp("--java", argv[index])) {
index++;
if (index >= argc) {
print_usage();
return 1;
}
javaFilename = argv[index];
} else if (0 == strcmp("--jni", argv[index])) {
index++;
if (index >= argc) {
print_usage();
return 1;
}
jniFilename = argv[index];
}
index++;
}
if (cppFilename.size() == 0
&& headerFilename.size() == 0
&& javaFilename.size() == 0
&& jniFilename.size() == 0) {
print_usage();
return 1;
}
// Collate the parameters
Atoms atoms;
int errorCount = collate_atoms(Atom::descriptor(), &atoms);
if (errorCount != 0) {
return 1;
}
// Write the .cpp file
if (cppFilename.size() != 0) {
FILE* out = fopen(cppFilename.c_str(), "w");
if (out == NULL) {
fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
return 1;
}
errorCount = android::stats_log_api_gen::write_stats_log_cpp(out, atoms);
fclose(out);
}
// Write the .h file
if (headerFilename.size() != 0) {
FILE* out = fopen(headerFilename.c_str(), "w");
if (out == NULL) {
fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
return 1;
}
errorCount = android::stats_log_api_gen::write_stats_log_header(out, atoms);
fclose(out);
}
// Write the .java file
if (javaFilename.size() != 0) {
FILE* out = fopen(javaFilename.c_str(), "w");
if (out == NULL) {
fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
return 1;
}
errorCount = android::stats_log_api_gen::write_stats_log_java(out, atoms);
fclose(out);
}
// Write the jni file
if (jniFilename.size() != 0) {
FILE* out = fopen(jniFilename.c_str(), "w");
if (out == NULL) {
fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
return 1;
}
errorCount = android::stats_log_api_gen::write_stats_log_jni(out, atoms);
fclose(out);
}
return 0;
}
}
}
/**
* Main.
*/
int
main(int argc, char const*const* argv)
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
return android::stats_log_api_gen::run(argc, argv);
}
|