blob: 27e77fee83a30788655cc4b5fecb4c21f4cb20cb [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*";
Yao Chen8b71c742018-10-24 12:15:56 -070071 case JAVA_TYPE_BYTE_ARRAY:
Yao Chen037ad042019-01-09 15:41:50 -080072 return "const BytesField&";
Yao Chend54f9dd2017-10-17 17:37:48 +000073 default:
74 return "UNKNOWN";
75 }
76}
77
78static const char*
79java_type_name(java_type_t type)
80{
81 switch (type) {
82 case JAVA_TYPE_BOOLEAN:
83 return "boolean";
84 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070085 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000086 return "int";
87 case JAVA_TYPE_LONG:
88 return "long";
89 case JAVA_TYPE_FLOAT:
90 return "float";
91 case JAVA_TYPE_DOUBLE:
92 return "double";
93 case JAVA_TYPE_STRING:
94 return "java.lang.String";
Yao Chen8b71c742018-10-24 12:15:56 -070095 case JAVA_TYPE_BYTE_ARRAY:
96 return "byte[]";
Yao Chend54f9dd2017-10-17 17:37:48 +000097 default:
98 return "UNKNOWN";
99 }
100}
101
Yangster-mac7604aea2017-12-11 22:55:49 -0800102static int write_stats_log_cpp(FILE *out, const Atoms &atoms,
103 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000104 // Print prelude
105 fprintf(out, "// This file is autogenerated\n");
106 fprintf(out, "\n");
107
Yangster-macca5c0862018-04-09 22:39:53 -0700108 fprintf(out, "#include <mutex>\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700109 fprintf(out, "#include <chrono>\n");
110 fprintf(out, "#include <thread>\n");
Jack He34a892d2f2018-12-20 00:42:31 -0800111 fprintf(out, "#ifdef __ANDROID__\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700112 fprintf(out, "#include <cutils/properties.h>\n");
Jack He34a892d2f2018-12-20 00:42:31 -0800113 fprintf(out, "#endif\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700114 fprintf(out, "#include <stats_event_list.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000115 fprintf(out, "#include <log/log.h>\n");
116 fprintf(out, "#include <statslog.h>\n");
Yangster-mac330af582018-02-08 15:24:38 -0800117 fprintf(out, "#include <utils/SystemClock.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000118 fprintf(out, "\n");
119
120 fprintf(out, "namespace android {\n");
121 fprintf(out, "namespace util {\n");
Yao Chen80235402017-11-13 20:42:25 -0800122 fprintf(out, "// the single event tag id for all stats logs\n");
123 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Jack He34a892d2f2018-12-20 00:42:31 -0800124 fprintf(out, "#ifdef __ANDROID__\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700125 fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
Jack He34a892d2f2018-12-20 00:42:31 -0800126 fprintf(out, "#else\n");
127 fprintf(out, "const static bool kStatsdEnabled = false;\n");
128 fprintf(out, "#endif\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000129
Yao Chenc40a19d2018-03-15 16:48:25 -0700130 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
131 "audio_state_changed",
132 "call_state_changed",
133 "phone_signal_strength_changed",
134 "mobile_bytes_transfer_by_fg_bg",
135 "mobile_bytes_transfer"};
136 fprintf(out,
137 "const std::set<int> "
138 "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n");
139 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
140 atom != atoms.decls.end(); atom++) {
141 if (kTruncatingAtomNames.find(atom->name) ==
142 kTruncatingAtomNames.end()) {
143 string constant = make_constant_name(atom->name);
144 fprintf(out, " %s,\n", constant.c_str());
145 }
146 }
147 fprintf(out, "};\n");
148 fprintf(out, "\n");
149
150 fprintf(out,
151 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
152 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
153 atom != atoms.decls.end(); atom++) {
154 for (vector<AtomField>::const_iterator field = atom->fields.begin();
155 field != atom->fields.end(); field++) {
156 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
157 string constant = make_constant_name(atom->name);
158 fprintf(out, " %s,\n", constant.c_str());
159 break;
160 }
161 }
162 }
163 fprintf(out, "};\n");
164 fprintf(out, "\n");
165
Yangster-macca5c0862018-04-09 22:39:53 -0700166 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700167 fprintf(out, " std::map<int, int> uidField;\n");
168 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
169 atom != atoms.decls.end(); atom++) {
170 if (atom->uidField == 0) {
171 continue;
172 }
173 fprintf(out,
174 "\n // Adding uid field for atom "
175 "(%d)%s\n",
176 atom->code, atom->name.c_str());
177 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
178 make_constant_name(atom->name).c_str(), atom->uidField);
179 }
180
181 fprintf(out, " return uidField;\n");
182 fprintf(out, "};\n");
183
184 fprintf(out,
185 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
186 "getAtomUidField();\n");
187
188 fprintf(out,
189 "static std::map<int, StateAtomFieldOptions> "
190 "getStateAtomFieldOptions() {\n");
191 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
192 fprintf(out, " StateAtomFieldOptions opt;\n");
193 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
194 atom != atoms.decls.end(); atom++) {
195 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
196 continue;
197 }
198 fprintf(out,
199 "\n // Adding primary and exclusive fields for atom "
200 "(%d)%s\n",
201 atom->code, atom->name.c_str());
202 fprintf(out, " opt.primaryFields.clear();\n");
203 for (const auto& field : atom->primaryFields) {
204 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
205 }
206
207 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
208 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
209 make_constant_name(atom->name).c_str());
210 }
211
212 fprintf(out, " return options;\n");
Yao Chen8b71c742018-10-24 12:15:56 -0700213 fprintf(out, "}\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700214
215 fprintf(out,
216 "const std::map<int, StateAtomFieldOptions> "
217 "AtomsInfo::kStateAtomsFieldOptions = "
218 "getStateAtomFieldOptions();\n");
219
Yao Chen8b71c742018-10-24 12:15:56 -0700220 fprintf(out,
221 "static std::map<int, std::vector<int>> "
222 "getBinaryFieldAtoms() {\n");
223 fprintf(out, " std::map<int, std::vector<int>> options;\n");
224 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
225 atom != atoms.decls.end(); atom++) {
226 if (atom->binaryFields.size() == 0) {
227 continue;
228 }
229 fprintf(out,
230 "\n // Adding binary fields for atom "
231 "(%d)%s\n",
232 atom->code, atom->name.c_str());
233
234 for (const auto& field : atom->binaryFields) {
235 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
236 make_constant_name(atom->name).c_str(), field);
237 }
238 }
239
240 fprintf(out, " return options;\n");
241 fprintf(out, "}\n");
242
243 fprintf(out,
244 "const std::map<int, std::vector<int>> "
245 "AtomsInfo::kBytesFieldAtoms = "
246 "getBinaryFieldAtoms();\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700247
248 fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
249 fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
250 fprintf(out, "static std::mutex mLogdRetryMutex;\n");
251
Yao Chend54f9dd2017-10-17 17:37:48 +0000252 // Print write methods
253 fprintf(out, "\n");
254 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800255 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000256 int argIndex;
257
Yao Chen97e21ec2018-03-29 11:00:38 -0700258 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700259 fprintf(out, "try_stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000260 argIndex = 1;
261 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800262 arg != signature->end(); arg++) {
263 if (*arg == 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 arg%d", cpp_type_name(*arg), argIndex);
277 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000278 argIndex++;
279 }
280 fprintf(out, ")\n");
281
282 fprintf(out, "{\n");
283 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700284 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700285 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800286 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800287 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000288 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800289 arg != signature->end(); arg++) {
290 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
291 for (const auto &chainField : attributionDecl.fields) {
292 if (chainField.javaType == JAVA_TYPE_STRING) {
293 fprintf(out, " if (%s_length != %s.size()) {\n",
294 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700295 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800296 fprintf(out, " }\n");
297 }
298 }
299 fprintf(out, "\n event.begin();\n");
300 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
301 attributionDecl.fields.front().name.c_str());
302 fprintf(out, " event.begin();\n");
303 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800304 if (chainField.javaType == JAVA_TYPE_STRING) {
305 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
306 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
307 fprintf(out, " } else {\n");
308 fprintf(out, " event << \"\";\n");
309 fprintf(out, " }\n");
310 } else {
311 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
312 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800313 }
314 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000315 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800316 fprintf(out, " event.end();\n\n");
Yao Chend66ecfc2018-12-06 10:34:25 -0800317 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
318 fprintf(out,
Yao Chen037ad042019-01-09 15:41:50 -0800319 " event.AppendCharArray(arg%d.arg, "
320 "arg%d.arg_length);\n",
Yao Chend66ecfc2018-12-06 10:34:25 -0800321 argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800322 } else {
323 if (*arg == JAVA_TYPE_STRING) {
324 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
325 fprintf(out, " arg%d = \"\";\n", argIndex);
326 fprintf(out, " }\n");
327 }
328 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000329 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000330 argIndex++;
331 }
332
Yao Chen97e21ec2018-03-29 11:00:38 -0700333 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700334 fprintf(out, " } else {\n");
335 fprintf(out, " return 1;\n");
336 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000337 fprintf(out, "}\n");
338 fprintf(out, "\n");
339 }
340
Yangster-macb8382a12018-04-04 10:39:12 -0700341 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
342 signature != atoms.signatures.end(); signature++) {
343 int argIndex;
344
345 fprintf(out, "int \n");
346 fprintf(out, "stats_write(int32_t code");
347 argIndex = 1;
348 for (vector<java_type_t>::const_iterator arg = signature->begin();
349 arg != signature->end(); arg++) {
350 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
351 for (auto chainField : attributionDecl.fields) {
352 if (chainField.javaType == JAVA_TYPE_STRING) {
353 fprintf(out, ", const std::vector<%s>& %s",
354 cpp_type_name(chainField.javaType),
355 chainField.name.c_str());
356 } else {
357 fprintf(out, ", const %s* %s, size_t %s_length",
358 cpp_type_name(chainField.javaType),
359 chainField.name.c_str(), chainField.name.c_str());
360 }
361 }
362 } else {
363 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
364 }
365 argIndex++;
366 }
367 fprintf(out, ")\n");
368
369 fprintf(out, "{\n");
370 fprintf(out, " int ret = 0;\n");
371
Yangster-macca5c0862018-04-09 22:39:53 -0700372 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700373 fprintf(out, " ret = try_stats_write(code");
374
375 argIndex = 1;
376 for (vector<java_type_t>::const_iterator arg = signature->begin();
377 arg != signature->end(); arg++) {
378 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
379 for (auto chainField : attributionDecl.fields) {
380 if (chainField.javaType == JAVA_TYPE_STRING) {
381 fprintf(out, ", %s",
382 chainField.name.c_str());
383 } else {
384 fprintf(out, ", %s, %s_length",
385 chainField.name.c_str(), chainField.name.c_str());
386 }
387 }
388 } else {
389 fprintf(out, ", arg%d", argIndex);
390 }
391 argIndex++;
392 }
393 fprintf(out, ");\n");
394 fprintf(out, " if (ret >= 0) { return retry; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700395
396
397 fprintf(out, " {\n");
398 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
399 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
400 "kMinRetryIntervalNs) break;\n");
401 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
402 fprintf(out, " }\n");
403 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700404 fprintf(out, " }\n");
405 fprintf(out, " return ret;\n");
406 fprintf(out, "}\n");
407 fprintf(out, "\n");
408 }
409
Yangster-macba5b9e42018-01-10 21:31:59 -0800410 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
411 signature != atoms.non_chained_signatures.end(); signature++) {
412 int argIndex;
413
Yao Chen97e21ec2018-03-29 11:00:38 -0700414 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700415 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800416 argIndex = 1;
417 for (vector<java_type_t>::const_iterator arg = signature->begin();
418 arg != signature->end(); arg++) {
419 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
420 argIndex++;
421 }
422 fprintf(out, ")\n");
423
424 fprintf(out, "{\n");
425 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700426 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700427 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800428 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800429 fprintf(out, " event << code;\n\n");
430 for (vector<java_type_t>::const_iterator arg = signature->begin();
431 arg != signature->end(); arg++) {
432 if (argIndex == 1) {
433 fprintf(out, " event.begin();\n\n");
434 fprintf(out, " event.begin();\n");
435 }
436 if (*arg == JAVA_TYPE_STRING) {
437 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
438 fprintf(out, " arg%d = \"\";\n", argIndex);
439 fprintf(out, " }\n");
440 }
Yao Chen037ad042019-01-09 15:41:50 -0800441 if (*arg == JAVA_TYPE_BYTE_ARRAY) {
442 fprintf(out,
443 " event.AppendCharArray(arg%d.arg, "
444 "arg%d.arg_length);",
445 argIndex, argIndex);
446 } else {
447 fprintf(out, " event << arg%d;\n", argIndex);
448 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800449 if (argIndex == 2) {
450 fprintf(out, " event.end();\n\n");
451 fprintf(out, " event.end();\n\n");
452 }
453 argIndex++;
454 }
455
Yao Chen97e21ec2018-03-29 11:00:38 -0700456 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700457 fprintf(out, " } else {\n");
458 fprintf(out, " return 1;\n");
459 fprintf(out, " }\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800460 fprintf(out, "}\n");
461 fprintf(out, "\n");
462 }
Yangster-macb8382a12018-04-04 10:39:12 -0700463
464 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
465 signature != atoms.non_chained_signatures.end(); signature++) {
466 int argIndex;
467
468 fprintf(out, "int\n");
469 fprintf(out, "stats_write_non_chained(int32_t code");
470 argIndex = 1;
471 for (vector<java_type_t>::const_iterator arg = signature->begin();
472 arg != signature->end(); arg++) {
473 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
474 argIndex++;
475 }
476 fprintf(out, ")\n");
477
478 fprintf(out, "{\n");
479
480 fprintf(out, " int ret = 0;\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700481 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700482 fprintf(out, " ret = try_stats_write_non_chained(code");
483
484 argIndex = 1;
485 for (vector<java_type_t>::const_iterator arg = signature->begin();
486 arg != signature->end(); arg++) {
487 fprintf(out, ", arg%d", argIndex);
488 argIndex++;
489 }
490 fprintf(out, ");\n");
491 fprintf(out, " if (ret >= 0) { return retry; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700492
493 fprintf(out, " {\n");
494 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
495 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
496 "kMinRetryIntervalNs) break;\n");
497 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
498 fprintf(out, " }\n");
499
500 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700501 fprintf(out, " }\n");
502 fprintf(out, " return ret;\n");
503 fprintf(out, "}\n");
504
505 fprintf(out, "\n");
506 }
507
508
Yao Chend54f9dd2017-10-17 17:37:48 +0000509 // Print footer
510 fprintf(out, "\n");
511 fprintf(out, "} // namespace util\n");
512 fprintf(out, "} // namespace android\n");
513
514 return 0;
515}
516
Yangster-macba5b9e42018-01-10 21:31:59 -0800517void build_non_chained_decl_map(const Atoms& atoms,
518 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
519 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
520 atom != atoms.non_chained_decls.end(); atom++) {
521 decl_map->insert(std::make_pair(atom->code, atom));
522 }
523}
524
525static void write_cpp_usage(
526 FILE* out, const string& method_name, const string& atom_code_name,
527 const AtomDecl& atom, const AtomDecl &attributionDecl) {
Yao Chen037ad042019-01-09 15:41:50 -0800528 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
529 atom_code_name.c_str());
530
Yangster-macba5b9e42018-01-10 21:31:59 -0800531 for (vector<AtomField>::const_iterator field = atom.fields.begin();
532 field != atom.fields.end(); field++) {
533 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
534 for (auto chainField : attributionDecl.fields) {
535 if (chainField.javaType == JAVA_TYPE_STRING) {
536 fprintf(out, ", const std::vector<%s>& %s",
537 cpp_type_name(chainField.javaType),
538 chainField.name.c_str());
539 } else {
540 fprintf(out, ", const %s* %s, size_t %s_length",
541 cpp_type_name(chainField.javaType),
542 chainField.name.c_str(), chainField.name.c_str());
543 }
544 }
545 } else {
546 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
547 }
548 }
549 fprintf(out, ");\n");
550}
551
552static void write_cpp_method_header(
553 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
554 const AtomDecl &attributionDecl) {
555 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
556 signature != signatures.end(); signature++) {
Yao Chen97e21ec2018-03-29 11:00:38 -0700557 fprintf(out, "int %s(int32_t code ", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800558 int argIndex = 1;
559 for (vector<java_type_t>::const_iterator arg = signature->begin();
560 arg != signature->end(); arg++) {
561 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
562 for (auto chainField : attributionDecl.fields) {
563 if (chainField.javaType == JAVA_TYPE_STRING) {
564 fprintf(out, ", const std::vector<%s>& %s",
565 cpp_type_name(chainField.javaType), chainField.name.c_str());
566 } else {
567 fprintf(out, ", const %s* %s, size_t %s_length",
568 cpp_type_name(chainField.javaType),
569 chainField.name.c_str(), chainField.name.c_str());
570 }
571 }
572 } else {
573 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
574 }
575 argIndex++;
576 }
577 fprintf(out, ");\n");
578
579 }
580}
Yao Chend54f9dd2017-10-17 17:37:48 +0000581
582static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800583write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000584{
Yao Chend54f9dd2017-10-17 17:37:48 +0000585 // Print prelude
586 fprintf(out, "// This file is autogenerated\n");
587 fprintf(out, "\n");
588 fprintf(out, "#pragma once\n");
589 fprintf(out, "\n");
590 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800591 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800592 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800593 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000594 fprintf(out, "\n");
595
596 fprintf(out, "namespace android {\n");
597 fprintf(out, "namespace util {\n");
598 fprintf(out, "\n");
599 fprintf(out, "/*\n");
600 fprintf(out, " * API For logging statistics events.\n");
601 fprintf(out, " */\n");
602 fprintf(out, "\n");
603 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700604 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000605 fprintf(out, " */\n");
606 fprintf(out, "enum {\n");
607
Yangster-macba5b9e42018-01-10 21:31:59 -0800608 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
609 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
610
Yao Chend54f9dd2017-10-17 17:37:48 +0000611 size_t i = 0;
612 // Print constants
613 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800614 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000615 string constant = make_constant_name(atom->name);
616 fprintf(out, "\n");
617 fprintf(out, " /**\n");
618 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800619 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
620
621 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
622 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
623 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
624 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000625 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000626 fprintf(out, " */\n");
627 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
628 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800629 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
630 maxPushedAtomId = atom->code;
631 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000632 i++;
633 }
634 fprintf(out, "\n");
635 fprintf(out, "};\n");
636 fprintf(out, "\n");
637
Yao Chen037ad042019-01-09 15:41:50 -0800638 fprintf(out, "struct BytesField {\n");
639 fprintf(out,
640 " BytesField(char const* array, size_t len) : arg(array), "
641 "arg_length(len) {}\n");
642 fprintf(out, " char const* arg;\n");
643 fprintf(out, " size_t arg_length;\n");
644 fprintf(out, "};\n");
645 fprintf(out, "\n");
646
Yao Chen9c1debe2018-02-19 14:39:19 -0800647 fprintf(out, "struct StateAtomFieldOptions {\n");
648 fprintf(out, " std::vector<int> primaryFields;\n");
649 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700650 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800651 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700652
653 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800654 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700655 " const static std::set<int> "
656 "kNotTruncatingTimestampAtomWhiteList;\n");
657 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
658 fprintf(out,
659 " const static std::set<int> kAtomsWithAttributionChain;\n");
660 fprintf(out,
661 " const static std::map<int, StateAtomFieldOptions> "
662 "kStateAtomsFieldOptions;\n");
Yao Chen8b71c742018-10-24 12:15:56 -0700663 fprintf(out,
664 " const static std::map<int, std::vector<int>> "
665 "kBytesFieldAtoms;");
Yao Chen9c1debe2018-02-19 14:39:19 -0800666 fprintf(out, "};\n");
667
Yao Chenc40a19d2018-03-15 16:48:25 -0700668 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
669 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800670
Yao Chend54f9dd2017-10-17 17:37:48 +0000671 // Print write methods
672 fprintf(out, "//\n");
673 fprintf(out, "// Write methods\n");
674 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800675 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
676
677 fprintf(out, "//\n");
678 fprintf(out, "// Write flattened methods\n");
679 fprintf(out, "//\n");
680 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
681 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000682
683 fprintf(out, "\n");
684 fprintf(out, "} // namespace util\n");
685 fprintf(out, "} // namespace android\n");
686
687 return 0;
688}
689
Bookatz0bd97202018-06-05 12:42:37 -0700690static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
691 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800692 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
693 method_name.c_str(), atom_code_name.c_str());
694 for (vector<AtomField>::const_iterator field = atom.fields.begin();
695 field != atom.fields.end(); field++) {
696 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700697 fprintf(out, ", android.os.WorkSource workSource");
Yao Chen8b71c742018-10-24 12:15:56 -0700698 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
699 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800700 } else {
701 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
702 }
703 }
Bookatz0bd97202018-06-05 12:42:37 -0700704 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800705}
706
707static void write_java_method(
708 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
709 const AtomDecl &attributionDecl) {
710 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
711 signature != signatures.end(); signature++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800712 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700713 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800714 int argIndex = 1;
715 for (vector<java_type_t>::const_iterator arg = signature->begin();
716 arg != signature->end(); arg++) {
717 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
718 for (auto chainField : attributionDecl.fields) {
719 fprintf(out, ", %s[] %s",
720 java_type_name(chainField.javaType), chainField.name.c_str());
721 }
722 } else {
723 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
724 }
725 argIndex++;
726 }
727 fprintf(out, ");\n");
728 }
729}
730
Bookatz0bd97202018-06-05 12:42:37 -0700731static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) {
732 fprintf(out, "\n // WorkSource methods.\n");
733 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
734 signature != signatures.end(); signature++) {
735 // Determine if there is Attribution in this signature.
736 int attributionArg = -1;
737 int argIndexMax = 0;
738 for (vector<java_type_t>::const_iterator arg = signature->begin();
739 arg != signature->end(); arg++) {
740 argIndexMax++;
741 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
742 if (attributionArg > -1) {
743 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
744 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
745 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
746 return;
747 }
748 attributionArg = argIndexMax;
749 }
750 }
751 if (attributionArg < 0) {
752 continue;
753 }
754
755 // Method header (signature)
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800756 fprintf(out, " /** @hide */\n");
Bookatz0bd97202018-06-05 12:42:37 -0700757 fprintf(out, " public static void write(int code");
758 int argIndex = 1;
759 for (vector<java_type_t>::const_iterator arg = signature->begin();
760 arg != signature->end(); arg++) {
761 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
762 fprintf(out, ", WorkSource ws");
763 } else {
764 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
765 }
766 argIndex++;
767 }
768 fprintf(out, ") {\n");
769
770 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
771 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
772 fprintf(out, " write_non_chained(code");
773 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
774 if (argIndex == attributionArg) {
775 fprintf(out, ", ws.get(i), ws.getName(i)");
776 } else {
777 fprintf(out, ", arg%d", argIndex);
778 }
779 }
780 fprintf(out, ");\n");
781 fprintf(out, " }\n"); // close flor-loop
782
783 // write() component.
784 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
785 fprintf(out, " if (workChains != null) {\n");
786 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
787 fprintf(out, " write(code");
788 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
789 if (argIndex == attributionArg) {
790 fprintf(out, ", wc.getUids(), wc.getTags()");
791 } else {
792 fprintf(out, ", arg%d", argIndex);
793 }
794 }
795 fprintf(out, ");\n");
796 fprintf(out, " }\n"); // close for-loop
797 fprintf(out, " }\n"); // close if
798 fprintf(out, " }\n"); // close method
799 }
800}
Yangster-macba5b9e42018-01-10 21:31:59 -0800801
Yao Chend54f9dd2017-10-17 17:37:48 +0000802static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800803write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000804{
Yao Chend54f9dd2017-10-17 17:37:48 +0000805 // Print prelude
806 fprintf(out, "// This file is autogenerated\n");
807 fprintf(out, "\n");
808 fprintf(out, "package android.util;\n");
809 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -0700810 fprintf(out, "import android.os.WorkSource;\n");
811 fprintf(out, "import java.util.ArrayList;\n");
812 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000813 fprintf(out, "\n");
814 fprintf(out, "/**\n");
815 fprintf(out, " * API For logging statistics events.\n");
816 fprintf(out, " * @hide\n");
817 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800818 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700819 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000820
Yangster-macba5b9e42018-01-10 21:31:59 -0800821 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
822 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
823
Stefan Lafon9478f352017-10-30 21:20:20 -0700824 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000825 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
826 atom != atoms.decls.end(); atom++) {
827 string constant = make_constant_name(atom->name);
828 fprintf(out, "\n");
829 fprintf(out, " /**\n");
Bookatz0bd97202018-06-05 12:42:37 -0700830 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
831 write_java_usage(out, "write", constant, *atom);
Yangster-macba5b9e42018-01-10 21:31:59 -0800832 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
833 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
Bookatz0bd97202018-06-05 12:42:37 -0700834 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
Yao Chend54f9dd2017-10-17 17:37:48 +0000835 }
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800836 fprintf(out, " * @hide\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000837 fprintf(out, " */\n");
838 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
839 }
840 fprintf(out, "\n");
841
Stefan Lafon9478f352017-10-30 21:20:20 -0700842 // Print constants for the enum values.
843 fprintf(out, " // Constants for enum values.\n\n");
844 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800845 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700846 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800847 field != atom->fields.end(); field++) {
848 if (field->javaType == JAVA_TYPE_ENUM) {
849 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
850 field->name.c_str());
851 for (map<int, string>::const_iterator value = field->enumValues.begin();
852 value != field->enumValues.end(); value++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800853 fprintf(out, " /** @hide */\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800854 fprintf(out, " public static final int %s__%s__%s = %d;\n",
855 make_constant_name(atom->message).c_str(),
856 make_constant_name(field->name).c_str(),
857 make_constant_name(value->second).c_str(),
858 value->first);
859 }
860 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700861 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700862 }
863 }
864
Yao Chend54f9dd2017-10-17 17:37:48 +0000865 // Print write methods
866 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800867 write_java_method(out, "write", atoms.signatures, attributionDecl);
868 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Bookatz0bd97202018-06-05 12:42:37 -0700869 write_java_work_source_method(out, atoms.signatures);
Yao Chend54f9dd2017-10-17 17:37:48 +0000870
871 fprintf(out, "}\n");
872
873 return 0;
874}
875
876static const char*
877jni_type_name(java_type_t type)
878{
879 switch (type) {
880 case JAVA_TYPE_BOOLEAN:
881 return "jboolean";
882 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700883 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000884 return "jint";
885 case JAVA_TYPE_LONG:
886 return "jlong";
887 case JAVA_TYPE_FLOAT:
888 return "jfloat";
889 case JAVA_TYPE_DOUBLE:
890 return "jdouble";
891 case JAVA_TYPE_STRING:
892 return "jstring";
Yao Chen8b71c742018-10-24 12:15:56 -0700893 case JAVA_TYPE_BYTE_ARRAY:
894 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +0000895 default:
896 return "UNKNOWN";
897 }
898}
899
Yangster-mac7604aea2017-12-11 22:55:49 -0800900static const char*
901jni_array_type_name(java_type_t type)
902{
903 switch (type) {
904 case JAVA_TYPE_INT:
905 return "jintArray";
906 case JAVA_TYPE_STRING:
907 return "jobjectArray";
908 default:
909 return "UNKNOWN";
910 }
911}
912
Yao Chend54f9dd2017-10-17 17:37:48 +0000913static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800914jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000915{
Yangster-macba5b9e42018-01-10 21:31:59 -0800916 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000917 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800918 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000919 switch (*arg) {
920 case JAVA_TYPE_BOOLEAN:
921 result += "_boolean";
922 break;
923 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700924 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000925 result += "_int";
926 break;
927 case JAVA_TYPE_LONG:
928 result += "_long";
929 break;
930 case JAVA_TYPE_FLOAT:
931 result += "_float";
932 break;
933 case JAVA_TYPE_DOUBLE:
934 result += "_double";
935 break;
936 case JAVA_TYPE_STRING:
937 result += "_String";
938 break;
Yangster-mac7604aea2017-12-11 22:55:49 -0800939 case JAVA_TYPE_ATTRIBUTION_CHAIN:
940 result += "_AttributionChain";
941 break;
Yao Chen8b71c742018-10-24 12:15:56 -0700942 case JAVA_TYPE_BYTE_ARRAY:
943 result += "_bytes";
944 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000945 default:
946 result += "_UNKNOWN";
947 break;
948 }
949 }
950 return result;
951}
952
953static const char*
954java_type_signature(java_type_t type)
955{
956 switch (type) {
957 case JAVA_TYPE_BOOLEAN:
958 return "Z";
959 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700960 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000961 return "I";
962 case JAVA_TYPE_LONG:
963 return "J";
964 case JAVA_TYPE_FLOAT:
965 return "F";
966 case JAVA_TYPE_DOUBLE:
967 return "D";
968 case JAVA_TYPE_STRING:
969 return "Ljava/lang/String;";
Yao Chen8b71c742018-10-24 12:15:56 -0700970 case JAVA_TYPE_BYTE_ARRAY:
971 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +0000972 default:
973 return "UNKNOWN";
974 }
975}
976
977static string
Yangster-mac7604aea2017-12-11 22:55:49 -0800978jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000979{
980 string result("(I");
981 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800982 arg != signature.end(); arg++) {
983 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
984 for (auto chainField : attributionDecl.fields) {
985 result += "[";
986 result += java_type_signature(chainField.javaType);
987 }
988 } else {
989 result += java_type_signature(*arg);
990 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000991 }
Yao Chen97e21ec2018-03-29 11:00:38 -0700992 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +0000993 return result;
994}
995
996static int
Yangster-macba5b9e42018-01-10 21:31:59 -0800997write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
998 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000999{
Yao Chend54f9dd2017-10-17 17:37:48 +00001000 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -08001001 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1002 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001003 int argIndex;
1004
Yao Chen97e21ec2018-03-29 11:00:38 -07001005 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001006 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -08001007 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001008 argIndex = 1;
1009 for (vector<java_type_t>::const_iterator arg = signature->begin();
1010 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001011 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1012 for (auto chainField : attributionDecl.fields) {
1013 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1014 chainField.name.c_str());
1015 }
1016 } else {
1017 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1018 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001019 argIndex++;
1020 }
1021 fprintf(out, ")\n");
1022
1023 fprintf(out, "{\n");
1024
1025 // Prepare strings
1026 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001027 bool hadStringOrChain = false;
Yao Chend54f9dd2017-10-17 17:37:48 +00001028 for (vector<java_type_t>::const_iterator arg = signature->begin();
1029 arg != signature->end(); arg++) {
1030 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001031 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001032 fprintf(out, " const char* str%d;\n", argIndex);
1033 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1034 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1035 argIndex, argIndex);
1036 fprintf(out, " } else {\n");
1037 fprintf(out, " str%d = NULL;\n", argIndex);
1038 fprintf(out, " }\n");
Yao Chen8b71c742018-10-24 12:15:56 -07001039 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1040 hadStringOrChain = true;
1041 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1042 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chend66ecfc2018-12-06 10:34:25 -08001043 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen2822b4f2018-11-29 09:39:45 -08001044 fprintf(out,
1045 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1046 "0) {\n",
1047 argIndex, argIndex);
Yao Chen8b71c742018-10-24 12:15:56 -07001048 fprintf(out,
1049 " jbyte_array%d = "
1050 "env->GetByteArrayElements(arg%d, NULL);\n",
1051 argIndex, argIndex);
1052 fprintf(out,
Yao Chend66ecfc2018-12-06 10:34:25 -08001053 " str%d_length = env->GetArrayLength(arg%d);\n",
1054 argIndex, argIndex);
1055 fprintf(out,
Yao Chen8b71c742018-10-24 12:15:56 -07001056 " str%d = "
1057 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1058 "d, NULL));\n",
1059 argIndex, argIndex);
1060 fprintf(out, " } else {\n");
1061 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1062 fprintf(out, " str%d = NULL;\n", argIndex);
1063 fprintf(out, " }\n");
1064
Yao Chen037ad042019-01-09 15:41:50 -08001065 fprintf(out,
1066 " android::util::BytesField bytesField%d(str%d, "
1067 "str%d_length);",
1068 argIndex, argIndex, argIndex);
1069
Yangster-mac7604aea2017-12-11 22:55:49 -08001070 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1071 hadStringOrChain = true;
1072 for (auto chainField : attributionDecl.fields) {
1073 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1074 chainField.name.c_str(), chainField.name.c_str());
1075 if (chainField.name != attributionDecl.fields.front().name) {
1076 fprintf(out, " if (%s_length != %s_length) {\n",
1077 chainField.name.c_str(),
1078 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001079 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001080 fprintf(out, " }\n");
1081 }
1082 if (chainField.javaType == JAVA_TYPE_INT) {
1083 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1084 chainField.name.c_str(), chainField.name.c_str());
1085 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1086 fprintf(out, " std::vector<%s> %s_vec;\n",
1087 cpp_type_name(chainField.javaType), chainField.name.c_str());
1088 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1089 chainField.name.c_str());
1090 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1091 chainField.name.c_str());
1092 fprintf(out, " jstring jstr = "
1093 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1094 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001095 fprintf(out, " if (jstr == NULL) {\n");
1096 fprintf(out, " %s_vec.push_back(NULL);\n",
1097 chainField.name.c_str());
1098 fprintf(out, " } else {\n");
1099 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001100 "new ScopedUtfChars(env, jstr);\n",
1101 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001102 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001103 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001104 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001105 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001106 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001107 fprintf(out, " }\n");
1108 }
1109 fprintf(out, "\n");
1110 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001111 }
1112 argIndex++;
1113 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001114 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1115 // chains.
1116 if (!hadStringOrChain) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001117 fprintf(out, " (void)env;\n");
1118 }
1119
1120 // stats_write call
1121 argIndex = 1;
Yao Chen037ad042019-01-09 15:41:50 -08001122 fprintf(out, "\n int ret = android::util::%s(code",
1123 cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001124 for (vector<java_type_t>::const_iterator arg = signature->begin();
1125 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001126 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1127 for (auto chainField : attributionDecl.fields) {
1128 if (chainField.javaType == JAVA_TYPE_INT) {
1129 fprintf(out, ", (const %s*)%s_array, %s_length",
1130 cpp_type_name(chainField.javaType),
1131 chainField.name.c_str(), chainField.name.c_str());
1132 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1133 fprintf(out, ", %s_vec", chainField.name.c_str());
1134 }
1135 }
Yao Chen037ad042019-01-09 15:41:50 -08001136 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1137 fprintf(out, ", bytesField%d", argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -08001138 } else {
Yao Chen037ad042019-01-09 15:41:50 -08001139 const char* argName =
1140 (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001141 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1142 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001143 argIndex++;
1144 }
1145 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001146 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001147
1148 // Clean up strings
1149 argIndex = 1;
1150 for (vector<java_type_t>::const_iterator arg = signature->begin();
1151 arg != signature->end(); arg++) {
1152 if (*arg == JAVA_TYPE_STRING) {
1153 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1154 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1155 argIndex, argIndex);
1156 fprintf(out, " }\n");
Yao Chen8b71c742018-10-24 12:15:56 -07001157 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1158 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1159 fprintf(out,
1160 " env->ReleaseByteArrayElements(arg%d, "
1161 "jbyte_array%d, 0);\n",
1162 argIndex, argIndex);
1163 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001164 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1165 for (auto chainField : attributionDecl.fields) {
1166 if (chainField.javaType == JAVA_TYPE_INT) {
1167 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1168 chainField.name.c_str(), chainField.name.c_str());
1169 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001170 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001171 chainField.name.c_str());
1172 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1173 fprintf(out, " }\n");
1174 }
1175 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001176 }
1177 argIndex++;
1178 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001179 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001180
1181 fprintf(out, "}\n");
1182 fprintf(out, "\n");
1183 }
1184
Yangster-macba5b9e42018-01-10 21:31:59 -08001185
1186 return 0;
1187}
1188
1189void write_jni_registration(FILE* out, const string& java_method_name,
1190 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
1191 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1192 signature != signatures.end(); signature++) {
1193 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1194 java_method_name.c_str(),
1195 jni_function_signature(*signature, attributionDecl).c_str(),
1196 jni_function_name(java_method_name, *signature).c_str());
1197 }
1198}
1199
1200static int
1201write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1202{
1203 // Print prelude
1204 fprintf(out, "// This file is autogenerated\n");
1205 fprintf(out, "\n");
1206
1207 fprintf(out, "#include <statslog.h>\n");
1208 fprintf(out, "\n");
1209 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1210 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1211 fprintf(out, "#include <utils/Vector.h>\n");
1212 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1213 fprintf(out, "#include \"jni.h\"\n");
1214 fprintf(out, "\n");
1215 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1216 fprintf(out, "\n");
1217
1218 fprintf(out, "namespace android {\n");
1219 fprintf(out, "\n");
1220
1221 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
1222 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
1223 atoms.non_chained_signatures, attributionDecl);
1224
Yao Chend54f9dd2017-10-17 17:37:48 +00001225 // Print registration function table
1226 fprintf(out, "/*\n");
1227 fprintf(out, " * JNI registration.\n");
1228 fprintf(out, " */\n");
1229 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -08001230 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
1231 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001232 fprintf(out, "};\n");
1233 fprintf(out, "\n");
1234
1235 // Print registration function
1236 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
1237 fprintf(out, " return RegisterMethodsOrDie(\n");
1238 fprintf(out, " env,\n");
1239 fprintf(out, " \"android/util/StatsLog\",\n");
1240 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1241 fprintf(out, "}\n");
1242
1243 fprintf(out, "\n");
1244 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001245 return 0;
1246}
1247
Yao Chend54f9dd2017-10-17 17:37:48 +00001248static void
1249print_usage()
1250{
1251 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1252 fprintf(stderr, "\n");
1253 fprintf(stderr, "OPTIONS\n");
1254 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1255 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1256 fprintf(stderr, " --help this message\n");
1257 fprintf(stderr, " --java FILENAME the java file to output\n");
1258 fprintf(stderr, " --jni FILENAME the jni file to output\n");
1259}
1260
1261/**
1262 * Do the argument parsing and execute the tasks.
1263 */
1264static int
1265run(int argc, char const*const* argv)
1266{
1267 string cppFilename;
1268 string headerFilename;
1269 string javaFilename;
1270 string jniFilename;
1271
1272 int index = 1;
1273 while (index < argc) {
1274 if (0 == strcmp("--help", argv[index])) {
1275 print_usage();
1276 return 0;
1277 } else if (0 == strcmp("--cpp", argv[index])) {
1278 index++;
1279 if (index >= argc) {
1280 print_usage();
1281 return 1;
1282 }
1283 cppFilename = argv[index];
1284 } else if (0 == strcmp("--header", argv[index])) {
1285 index++;
1286 if (index >= argc) {
1287 print_usage();
1288 return 1;
1289 }
1290 headerFilename = argv[index];
1291 } else if (0 == strcmp("--java", argv[index])) {
1292 index++;
1293 if (index >= argc) {
1294 print_usage();
1295 return 1;
1296 }
1297 javaFilename = argv[index];
1298 } else if (0 == strcmp("--jni", argv[index])) {
1299 index++;
1300 if (index >= argc) {
1301 print_usage();
1302 return 1;
1303 }
1304 jniFilename = argv[index];
1305 }
1306 index++;
1307 }
1308
1309 if (cppFilename.size() == 0
1310 && headerFilename.size() == 0
1311 && javaFilename.size() == 0
1312 && jniFilename.size() == 0) {
1313 print_usage();
1314 return 1;
1315 }
1316
1317 // Collate the parameters
1318 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001319 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001320 if (errorCount != 0) {
1321 return 1;
1322 }
1323
Yangster-mac7604aea2017-12-11 22:55:49 -08001324 AtomDecl attributionDecl;
1325 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001326 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001327 &attributionDecl, &attributionSignature);
1328
Yao Chend54f9dd2017-10-17 17:37:48 +00001329 // Write the .cpp file
1330 if (cppFilename.size() != 0) {
1331 FILE* out = fopen(cppFilename.c_str(), "w");
1332 if (out == NULL) {
1333 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1334 return 1;
1335 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001336 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1337 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001338 fclose(out);
1339 }
1340
1341 // Write the .h file
1342 if (headerFilename.size() != 0) {
1343 FILE* out = fopen(headerFilename.c_str(), "w");
1344 if (out == NULL) {
1345 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1346 return 1;
1347 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001348 errorCount = android::stats_log_api_gen::write_stats_log_header(
1349 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001350 fclose(out);
1351 }
1352
1353 // Write the .java file
1354 if (javaFilename.size() != 0) {
1355 FILE* out = fopen(javaFilename.c_str(), "w");
1356 if (out == NULL) {
1357 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1358 return 1;
1359 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001360 errorCount = android::stats_log_api_gen::write_stats_log_java(
1361 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001362 fclose(out);
1363 }
1364
1365 // Write the jni file
1366 if (jniFilename.size() != 0) {
1367 FILE* out = fopen(jniFilename.c_str(), "w");
1368 if (out == NULL) {
1369 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1370 return 1;
1371 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001372 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1373 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001374 fclose(out);
1375 }
1376
1377 return 0;
1378}
1379
1380}
1381}
1382
1383/**
1384 * Main.
1385 */
1386int
1387main(int argc, char const*const* argv)
1388{
1389 GOOGLE_PROTOBUF_VERIFY_VERSION;
1390
1391 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07001392}