blob: a0de3d389669a1cd36cd51de283aee3ed10f2976 [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:
72 return "char const*";
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 He34a892d2018-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 He34a892d2018-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 He34a892d2018-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 He34a892d2018-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 }
Yao Chend66ecfc2018-12-06 10:34:25 -0800275 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
276 fprintf(out, ", %s arg%d, size_t arg%d_length",
277 cpp_type_name(*arg), argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800278 } else {
279 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
280 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000281 argIndex++;
282 }
283 fprintf(out, ")\n");
284
285 fprintf(out, "{\n");
286 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700287 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700288 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800289 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800290 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000291 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800292 arg != signature->end(); arg++) {
293 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
294 for (const auto &chainField : attributionDecl.fields) {
295 if (chainField.javaType == JAVA_TYPE_STRING) {
296 fprintf(out, " if (%s_length != %s.size()) {\n",
297 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700298 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800299 fprintf(out, " }\n");
300 }
301 }
302 fprintf(out, "\n event.begin();\n");
303 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
304 attributionDecl.fields.front().name.c_str());
305 fprintf(out, " event.begin();\n");
306 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800307 if (chainField.javaType == JAVA_TYPE_STRING) {
308 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
309 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
310 fprintf(out, " } else {\n");
311 fprintf(out, " event << \"\";\n");
312 fprintf(out, " }\n");
313 } else {
314 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
315 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800316 }
317 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000318 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800319 fprintf(out, " event.end();\n\n");
Yao Chend66ecfc2018-12-06 10:34:25 -0800320 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
321 fprintf(out,
322 " event.AppendCharArray(arg%d, arg%d_length);\n",
323 argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800324 } else {
325 if (*arg == JAVA_TYPE_STRING) {
326 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
327 fprintf(out, " arg%d = \"\";\n", argIndex);
328 fprintf(out, " }\n");
329 }
330 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000331 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000332 argIndex++;
333 }
334
Yao Chen97e21ec2018-03-29 11:00:38 -0700335 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700336 fprintf(out, " } else {\n");
337 fprintf(out, " return 1;\n");
338 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000339 fprintf(out, "}\n");
340 fprintf(out, "\n");
341 }
342
Yangster-macb8382a12018-04-04 10:39:12 -0700343 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
344 signature != atoms.signatures.end(); signature++) {
345 int argIndex;
346
347 fprintf(out, "int \n");
348 fprintf(out, "stats_write(int32_t code");
349 argIndex = 1;
350 for (vector<java_type_t>::const_iterator arg = signature->begin();
351 arg != signature->end(); arg++) {
352 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
353 for (auto chainField : attributionDecl.fields) {
354 if (chainField.javaType == JAVA_TYPE_STRING) {
355 fprintf(out, ", const std::vector<%s>& %s",
356 cpp_type_name(chainField.javaType),
357 chainField.name.c_str());
358 } else {
359 fprintf(out, ", const %s* %s, size_t %s_length",
360 cpp_type_name(chainField.javaType),
361 chainField.name.c_str(), chainField.name.c_str());
362 }
363 }
Yao Chend66ecfc2018-12-06 10:34:25 -0800364 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
365 fprintf(out, ", %s arg%d, size_t arg%d_length",
366 cpp_type_name(*arg), argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700367 } else {
368 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
369 }
370 argIndex++;
371 }
372 fprintf(out, ")\n");
373
374 fprintf(out, "{\n");
375 fprintf(out, " int ret = 0;\n");
376
Yangster-macca5c0862018-04-09 22:39:53 -0700377 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700378 fprintf(out, " ret = try_stats_write(code");
379
380 argIndex = 1;
381 for (vector<java_type_t>::const_iterator arg = signature->begin();
382 arg != signature->end(); arg++) {
383 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
384 for (auto chainField : attributionDecl.fields) {
385 if (chainField.javaType == JAVA_TYPE_STRING) {
386 fprintf(out, ", %s",
387 chainField.name.c_str());
388 } else {
389 fprintf(out, ", %s, %s_length",
390 chainField.name.c_str(), chainField.name.c_str());
391 }
392 }
Yao Chend66ecfc2018-12-06 10:34:25 -0800393 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
394 fprintf(out, ", arg%d, arg%d_length", argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700395 } else {
396 fprintf(out, ", arg%d", argIndex);
397 }
398 argIndex++;
399 }
400 fprintf(out, ");\n");
401 fprintf(out, " if (ret >= 0) { return retry; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700402
403
404 fprintf(out, " {\n");
405 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
406 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
407 "kMinRetryIntervalNs) break;\n");
408 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
409 fprintf(out, " }\n");
410 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700411 fprintf(out, " }\n");
412 fprintf(out, " return ret;\n");
413 fprintf(out, "}\n");
414 fprintf(out, "\n");
415 }
416
Yangster-macba5b9e42018-01-10 21:31:59 -0800417 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
418 signature != atoms.non_chained_signatures.end(); signature++) {
419 int argIndex;
420
Yao Chen97e21ec2018-03-29 11:00:38 -0700421 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700422 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800423 argIndex = 1;
424 for (vector<java_type_t>::const_iterator arg = signature->begin();
425 arg != signature->end(); arg++) {
426 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
427 argIndex++;
428 }
429 fprintf(out, ")\n");
430
431 fprintf(out, "{\n");
432 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700433 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700434 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800435 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800436 fprintf(out, " event << code;\n\n");
437 for (vector<java_type_t>::const_iterator arg = signature->begin();
438 arg != signature->end(); arg++) {
439 if (argIndex == 1) {
440 fprintf(out, " event.begin();\n\n");
441 fprintf(out, " event.begin();\n");
442 }
443 if (*arg == JAVA_TYPE_STRING) {
444 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
445 fprintf(out, " arg%d = \"\";\n", argIndex);
446 fprintf(out, " }\n");
447 }
448 fprintf(out, " event << arg%d;\n", argIndex);
449 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) {
528 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str());
529 for (vector<AtomField>::const_iterator field = atom.fields.begin();
530 field != atom.fields.end(); field++) {
531 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
532 for (auto chainField : attributionDecl.fields) {
533 if (chainField.javaType == JAVA_TYPE_STRING) {
534 fprintf(out, ", const std::vector<%s>& %s",
535 cpp_type_name(chainField.javaType),
536 chainField.name.c_str());
537 } else {
538 fprintf(out, ", const %s* %s, size_t %s_length",
539 cpp_type_name(chainField.javaType),
540 chainField.name.c_str(), chainField.name.c_str());
541 }
542 }
Yao Chend66ecfc2018-12-06 10:34:25 -0800543 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
544 fprintf(out, ", %s %s, size_t %s_length",
545 cpp_type_name(field->javaType), field->name.c_str(),
546 field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800547 } else {
548 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
549 }
550 }
551 fprintf(out, ");\n");
552}
553
554static void write_cpp_method_header(
555 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
556 const AtomDecl &attributionDecl) {
557 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
558 signature != signatures.end(); signature++) {
Yao Chen97e21ec2018-03-29 11:00:38 -0700559 fprintf(out, "int %s(int32_t code ", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800560 int argIndex = 1;
561 for (vector<java_type_t>::const_iterator arg = signature->begin();
562 arg != signature->end(); arg++) {
563 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
564 for (auto chainField : attributionDecl.fields) {
565 if (chainField.javaType == JAVA_TYPE_STRING) {
566 fprintf(out, ", const std::vector<%s>& %s",
567 cpp_type_name(chainField.javaType), chainField.name.c_str());
568 } else {
569 fprintf(out, ", const %s* %s, size_t %s_length",
570 cpp_type_name(chainField.javaType),
571 chainField.name.c_str(), chainField.name.c_str());
572 }
573 }
Yao Chend66ecfc2018-12-06 10:34:25 -0800574 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
575 fprintf(out, ", %s arg%d, size_t arg%d_length",
576 cpp_type_name(*arg), argIndex, argIndex);
Yangster-macba5b9e42018-01-10 21:31:59 -0800577 } else {
578 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
579 }
580 argIndex++;
581 }
582 fprintf(out, ");\n");
583
584 }
585}
Yao Chend54f9dd2017-10-17 17:37:48 +0000586
587static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800588write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000589{
Yao Chend54f9dd2017-10-17 17:37:48 +0000590 // Print prelude
591 fprintf(out, "// This file is autogenerated\n");
592 fprintf(out, "\n");
593 fprintf(out, "#pragma once\n");
594 fprintf(out, "\n");
595 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800596 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800597 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800598 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000599 fprintf(out, "\n");
600
601 fprintf(out, "namespace android {\n");
602 fprintf(out, "namespace util {\n");
603 fprintf(out, "\n");
604 fprintf(out, "/*\n");
605 fprintf(out, " * API For logging statistics events.\n");
606 fprintf(out, " */\n");
607 fprintf(out, "\n");
608 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700609 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000610 fprintf(out, " */\n");
611 fprintf(out, "enum {\n");
612
Yangster-macba5b9e42018-01-10 21:31:59 -0800613 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
614 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
615
Yao Chend54f9dd2017-10-17 17:37:48 +0000616 size_t i = 0;
617 // Print constants
618 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800619 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000620 string constant = make_constant_name(atom->name);
621 fprintf(out, "\n");
622 fprintf(out, " /**\n");
623 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800624 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
625
626 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
627 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
628 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
629 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000630 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000631 fprintf(out, " */\n");
632 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
633 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800634 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
635 maxPushedAtomId = atom->code;
636 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000637 i++;
638 }
639 fprintf(out, "\n");
640 fprintf(out, "};\n");
641 fprintf(out, "\n");
642
Yao Chen9c1debe2018-02-19 14:39:19 -0800643 fprintf(out, "struct StateAtomFieldOptions {\n");
644 fprintf(out, " std::vector<int> primaryFields;\n");
645 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700646 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800647 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700648
649 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800650 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700651 " const static std::set<int> "
652 "kNotTruncatingTimestampAtomWhiteList;\n");
653 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
654 fprintf(out,
655 " const static std::set<int> kAtomsWithAttributionChain;\n");
656 fprintf(out,
657 " const static std::map<int, StateAtomFieldOptions> "
658 "kStateAtomsFieldOptions;\n");
Yao Chen8b71c742018-10-24 12:15:56 -0700659 fprintf(out,
660 " const static std::map<int, std::vector<int>> "
661 "kBytesFieldAtoms;");
Yao Chen9c1debe2018-02-19 14:39:19 -0800662 fprintf(out, "};\n");
663
Yao Chenc40a19d2018-03-15 16:48:25 -0700664 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
665 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800666
Yao Chend54f9dd2017-10-17 17:37:48 +0000667 // Print write methods
668 fprintf(out, "//\n");
669 fprintf(out, "// Write methods\n");
670 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800671 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
672
673 fprintf(out, "//\n");
674 fprintf(out, "// Write flattened methods\n");
675 fprintf(out, "//\n");
676 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
677 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000678
679 fprintf(out, "\n");
680 fprintf(out, "} // namespace util\n");
681 fprintf(out, "} // namespace android\n");
682
683 return 0;
684}
685
Bookatz0bd97202018-06-05 12:42:37 -0700686static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
687 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800688 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
689 method_name.c_str(), atom_code_name.c_str());
690 for (vector<AtomField>::const_iterator field = atom.fields.begin();
691 field != atom.fields.end(); field++) {
692 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700693 fprintf(out, ", android.os.WorkSource workSource");
Yao Chen8b71c742018-10-24 12:15:56 -0700694 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
695 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800696 } else {
697 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
698 }
699 }
Bookatz0bd97202018-06-05 12:42:37 -0700700 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800701}
702
703static void write_java_method(
704 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
705 const AtomDecl &attributionDecl) {
706 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
707 signature != signatures.end(); signature++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800708 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700709 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800710 int argIndex = 1;
711 for (vector<java_type_t>::const_iterator arg = signature->begin();
712 arg != signature->end(); arg++) {
713 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
714 for (auto chainField : attributionDecl.fields) {
715 fprintf(out, ", %s[] %s",
716 java_type_name(chainField.javaType), chainField.name.c_str());
717 }
718 } else {
719 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
720 }
721 argIndex++;
722 }
723 fprintf(out, ");\n");
724 }
725}
726
Bookatz0bd97202018-06-05 12:42:37 -0700727static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) {
728 fprintf(out, "\n // WorkSource methods.\n");
729 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
730 signature != signatures.end(); signature++) {
731 // Determine if there is Attribution in this signature.
732 int attributionArg = -1;
733 int argIndexMax = 0;
734 for (vector<java_type_t>::const_iterator arg = signature->begin();
735 arg != signature->end(); arg++) {
736 argIndexMax++;
737 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
738 if (attributionArg > -1) {
739 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
740 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
741 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
742 return;
743 }
744 attributionArg = argIndexMax;
745 }
746 }
747 if (attributionArg < 0) {
748 continue;
749 }
750
751 // Method header (signature)
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800752 fprintf(out, " /** @hide */\n");
Bookatz0bd97202018-06-05 12:42:37 -0700753 fprintf(out, " public static void write(int code");
754 int argIndex = 1;
755 for (vector<java_type_t>::const_iterator arg = signature->begin();
756 arg != signature->end(); arg++) {
757 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
758 fprintf(out, ", WorkSource ws");
759 } else {
760 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
761 }
762 argIndex++;
763 }
764 fprintf(out, ") {\n");
765
766 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
767 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
768 fprintf(out, " write_non_chained(code");
769 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
770 if (argIndex == attributionArg) {
771 fprintf(out, ", ws.get(i), ws.getName(i)");
772 } else {
773 fprintf(out, ", arg%d", argIndex);
774 }
775 }
776 fprintf(out, ");\n");
777 fprintf(out, " }\n"); // close flor-loop
778
779 // write() component.
780 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
781 fprintf(out, " if (workChains != null) {\n");
782 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
783 fprintf(out, " write(code");
784 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
785 if (argIndex == attributionArg) {
786 fprintf(out, ", wc.getUids(), wc.getTags()");
787 } else {
788 fprintf(out, ", arg%d", argIndex);
789 }
790 }
791 fprintf(out, ");\n");
792 fprintf(out, " }\n"); // close for-loop
793 fprintf(out, " }\n"); // close if
794 fprintf(out, " }\n"); // close method
795 }
796}
Yangster-macba5b9e42018-01-10 21:31:59 -0800797
Yao Chend54f9dd2017-10-17 17:37:48 +0000798static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800799write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000800{
Yao Chend54f9dd2017-10-17 17:37:48 +0000801 // Print prelude
802 fprintf(out, "// This file is autogenerated\n");
803 fprintf(out, "\n");
804 fprintf(out, "package android.util;\n");
805 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -0700806 fprintf(out, "import android.os.WorkSource;\n");
807 fprintf(out, "import java.util.ArrayList;\n");
808 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000809 fprintf(out, "\n");
810 fprintf(out, "/**\n");
811 fprintf(out, " * API For logging statistics events.\n");
812 fprintf(out, " * @hide\n");
813 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800814 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700815 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000816
Yangster-macba5b9e42018-01-10 21:31:59 -0800817 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
818 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
819
Stefan Lafon9478f352017-10-30 21:20:20 -0700820 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000821 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
822 atom != atoms.decls.end(); atom++) {
823 string constant = make_constant_name(atom->name);
824 fprintf(out, "\n");
825 fprintf(out, " /**\n");
Bookatz0bd97202018-06-05 12:42:37 -0700826 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
827 write_java_usage(out, "write", constant, *atom);
Yangster-macba5b9e42018-01-10 21:31:59 -0800828 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
829 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
Bookatz0bd97202018-06-05 12:42:37 -0700830 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
Yao Chend54f9dd2017-10-17 17:37:48 +0000831 }
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800832 fprintf(out, " * @hide\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000833 fprintf(out, " */\n");
834 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
835 }
836 fprintf(out, "\n");
837
Stefan Lafon9478f352017-10-30 21:20:20 -0700838 // Print constants for the enum values.
839 fprintf(out, " // Constants for enum values.\n\n");
840 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800841 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700842 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800843 field != atom->fields.end(); field++) {
844 if (field->javaType == JAVA_TYPE_ENUM) {
845 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
846 field->name.c_str());
847 for (map<int, string>::const_iterator value = field->enumValues.begin();
848 value != field->enumValues.end(); value++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800849 fprintf(out, " /** @hide */\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800850 fprintf(out, " public static final int %s__%s__%s = %d;\n",
851 make_constant_name(atom->message).c_str(),
852 make_constant_name(field->name).c_str(),
853 make_constant_name(value->second).c_str(),
854 value->first);
855 }
856 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700857 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700858 }
859 }
860
Yao Chend54f9dd2017-10-17 17:37:48 +0000861 // Print write methods
862 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800863 write_java_method(out, "write", atoms.signatures, attributionDecl);
864 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Bookatz0bd97202018-06-05 12:42:37 -0700865 write_java_work_source_method(out, atoms.signatures);
Yao Chend54f9dd2017-10-17 17:37:48 +0000866
867 fprintf(out, "}\n");
868
869 return 0;
870}
871
872static const char*
873jni_type_name(java_type_t type)
874{
875 switch (type) {
876 case JAVA_TYPE_BOOLEAN:
877 return "jboolean";
878 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700879 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000880 return "jint";
881 case JAVA_TYPE_LONG:
882 return "jlong";
883 case JAVA_TYPE_FLOAT:
884 return "jfloat";
885 case JAVA_TYPE_DOUBLE:
886 return "jdouble";
887 case JAVA_TYPE_STRING:
888 return "jstring";
Yao Chen8b71c742018-10-24 12:15:56 -0700889 case JAVA_TYPE_BYTE_ARRAY:
890 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +0000891 default:
892 return "UNKNOWN";
893 }
894}
895
Yangster-mac7604aea2017-12-11 22:55:49 -0800896static const char*
897jni_array_type_name(java_type_t type)
898{
899 switch (type) {
900 case JAVA_TYPE_INT:
901 return "jintArray";
902 case JAVA_TYPE_STRING:
903 return "jobjectArray";
904 default:
905 return "UNKNOWN";
906 }
907}
908
Yao Chend54f9dd2017-10-17 17:37:48 +0000909static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800910jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000911{
Yangster-macba5b9e42018-01-10 21:31:59 -0800912 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000913 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800914 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000915 switch (*arg) {
916 case JAVA_TYPE_BOOLEAN:
917 result += "_boolean";
918 break;
919 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700920 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000921 result += "_int";
922 break;
923 case JAVA_TYPE_LONG:
924 result += "_long";
925 break;
926 case JAVA_TYPE_FLOAT:
927 result += "_float";
928 break;
929 case JAVA_TYPE_DOUBLE:
930 result += "_double";
931 break;
932 case JAVA_TYPE_STRING:
933 result += "_String";
934 break;
Yangster-mac7604aea2017-12-11 22:55:49 -0800935 case JAVA_TYPE_ATTRIBUTION_CHAIN:
936 result += "_AttributionChain";
937 break;
Yao Chen8b71c742018-10-24 12:15:56 -0700938 case JAVA_TYPE_BYTE_ARRAY:
939 result += "_bytes";
940 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000941 default:
942 result += "_UNKNOWN";
943 break;
944 }
945 }
946 return result;
947}
948
949static const char*
950java_type_signature(java_type_t type)
951{
952 switch (type) {
953 case JAVA_TYPE_BOOLEAN:
954 return "Z";
955 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700956 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000957 return "I";
958 case JAVA_TYPE_LONG:
959 return "J";
960 case JAVA_TYPE_FLOAT:
961 return "F";
962 case JAVA_TYPE_DOUBLE:
963 return "D";
964 case JAVA_TYPE_STRING:
965 return "Ljava/lang/String;";
Yao Chen8b71c742018-10-24 12:15:56 -0700966 case JAVA_TYPE_BYTE_ARRAY:
967 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +0000968 default:
969 return "UNKNOWN";
970 }
971}
972
973static string
Yangster-mac7604aea2017-12-11 22:55:49 -0800974jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000975{
976 string result("(I");
977 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800978 arg != signature.end(); arg++) {
979 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
980 for (auto chainField : attributionDecl.fields) {
981 result += "[";
982 result += java_type_signature(chainField.javaType);
983 }
984 } else {
985 result += java_type_signature(*arg);
986 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000987 }
Yao Chen97e21ec2018-03-29 11:00:38 -0700988 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +0000989 return result;
990}
991
992static int
Yangster-macba5b9e42018-01-10 21:31:59 -0800993write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
994 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000995{
Yao Chend54f9dd2017-10-17 17:37:48 +0000996 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -0800997 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
998 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000999 int argIndex;
1000
Yao Chen97e21ec2018-03-29 11:00:38 -07001001 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001002 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -08001003 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001004 argIndex = 1;
1005 for (vector<java_type_t>::const_iterator arg = signature->begin();
1006 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001007 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1008 for (auto chainField : attributionDecl.fields) {
1009 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1010 chainField.name.c_str());
1011 }
1012 } else {
1013 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1014 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001015 argIndex++;
1016 }
1017 fprintf(out, ")\n");
1018
1019 fprintf(out, "{\n");
1020
1021 // Prepare strings
1022 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001023 bool hadStringOrChain = false;
Yao Chend54f9dd2017-10-17 17:37:48 +00001024 for (vector<java_type_t>::const_iterator arg = signature->begin();
1025 arg != signature->end(); arg++) {
1026 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001027 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001028 fprintf(out, " const char* str%d;\n", argIndex);
1029 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1030 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1031 argIndex, argIndex);
1032 fprintf(out, " } else {\n");
1033 fprintf(out, " str%d = NULL;\n", argIndex);
1034 fprintf(out, " }\n");
Yao Chen8b71c742018-10-24 12:15:56 -07001035 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1036 hadStringOrChain = true;
1037 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1038 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chend66ecfc2018-12-06 10:34:25 -08001039 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen2822b4f2018-11-29 09:39:45 -08001040 fprintf(out,
1041 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1042 "0) {\n",
1043 argIndex, argIndex);
Yao Chen8b71c742018-10-24 12:15:56 -07001044 fprintf(out,
1045 " jbyte_array%d = "
1046 "env->GetByteArrayElements(arg%d, NULL);\n",
1047 argIndex, argIndex);
1048 fprintf(out,
Yao Chend66ecfc2018-12-06 10:34:25 -08001049 " str%d_length = env->GetArrayLength(arg%d);\n",
1050 argIndex, argIndex);
1051 fprintf(out,
Yao Chen8b71c742018-10-24 12:15:56 -07001052 " str%d = "
1053 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1054 "d, NULL));\n",
1055 argIndex, argIndex);
1056 fprintf(out, " } else {\n");
1057 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1058 fprintf(out, " str%d = NULL;\n", argIndex);
1059 fprintf(out, " }\n");
1060
Yangster-mac7604aea2017-12-11 22:55:49 -08001061 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1062 hadStringOrChain = true;
1063 for (auto chainField : attributionDecl.fields) {
1064 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1065 chainField.name.c_str(), chainField.name.c_str());
1066 if (chainField.name != attributionDecl.fields.front().name) {
1067 fprintf(out, " if (%s_length != %s_length) {\n",
1068 chainField.name.c_str(),
1069 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001070 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001071 fprintf(out, " }\n");
1072 }
1073 if (chainField.javaType == JAVA_TYPE_INT) {
1074 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1075 chainField.name.c_str(), chainField.name.c_str());
1076 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1077 fprintf(out, " std::vector<%s> %s_vec;\n",
1078 cpp_type_name(chainField.javaType), chainField.name.c_str());
1079 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1080 chainField.name.c_str());
1081 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1082 chainField.name.c_str());
1083 fprintf(out, " jstring jstr = "
1084 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1085 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001086 fprintf(out, " if (jstr == NULL) {\n");
1087 fprintf(out, " %s_vec.push_back(NULL);\n",
1088 chainField.name.c_str());
1089 fprintf(out, " } else {\n");
1090 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001091 "new ScopedUtfChars(env, jstr);\n",
1092 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001093 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001094 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001095 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001096 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001097 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001098 fprintf(out, " }\n");
1099 }
1100 fprintf(out, "\n");
1101 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001102 }
1103 argIndex++;
1104 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001105 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1106 // chains.
1107 if (!hadStringOrChain) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001108 fprintf(out, " (void)env;\n");
1109 }
1110
1111 // stats_write call
1112 argIndex = 1;
Yao Chen97e21ec2018-03-29 11:00:38 -07001113 fprintf(out, " int ret = android::util::%s(code", cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001114 for (vector<java_type_t>::const_iterator arg = signature->begin();
1115 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001116 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1117 for (auto chainField : attributionDecl.fields) {
1118 if (chainField.javaType == JAVA_TYPE_INT) {
1119 fprintf(out, ", (const %s*)%s_array, %s_length",
1120 cpp_type_name(chainField.javaType),
1121 chainField.name.c_str(), chainField.name.c_str());
1122 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1123 fprintf(out, ", %s_vec", chainField.name.c_str());
1124 }
1125 }
1126 } else {
Yao Chen8b71c742018-10-24 12:15:56 -07001127 const char* argName = (*arg == JAVA_TYPE_STRING ||
1128 *arg == JAVA_TYPE_BYTE_ARRAY)
1129 ? "str"
1130 : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001131 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
Yao Chend66ecfc2018-12-06 10:34:25 -08001132
1133 if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1134 fprintf(out, ", %s%d_length", argName, argIndex);
1135 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001136 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001137 argIndex++;
1138 }
1139 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001140 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001141
1142 // Clean up strings
1143 argIndex = 1;
1144 for (vector<java_type_t>::const_iterator arg = signature->begin();
1145 arg != signature->end(); arg++) {
1146 if (*arg == JAVA_TYPE_STRING) {
1147 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1148 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1149 argIndex, argIndex);
1150 fprintf(out, " }\n");
Yao Chen8b71c742018-10-24 12:15:56 -07001151 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1152 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1153 fprintf(out,
1154 " env->ReleaseByteArrayElements(arg%d, "
1155 "jbyte_array%d, 0);\n",
1156 argIndex, argIndex);
1157 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001158 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1159 for (auto chainField : attributionDecl.fields) {
1160 if (chainField.javaType == JAVA_TYPE_INT) {
1161 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1162 chainField.name.c_str(), chainField.name.c_str());
1163 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001164 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001165 chainField.name.c_str());
1166 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1167 fprintf(out, " }\n");
1168 }
1169 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001170 }
1171 argIndex++;
1172 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001173 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001174
1175 fprintf(out, "}\n");
1176 fprintf(out, "\n");
1177 }
1178
Yangster-macba5b9e42018-01-10 21:31:59 -08001179
1180 return 0;
1181}
1182
1183void write_jni_registration(FILE* out, const string& java_method_name,
1184 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
1185 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1186 signature != signatures.end(); signature++) {
1187 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1188 java_method_name.c_str(),
1189 jni_function_signature(*signature, attributionDecl).c_str(),
1190 jni_function_name(java_method_name, *signature).c_str());
1191 }
1192}
1193
1194static int
1195write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1196{
1197 // Print prelude
1198 fprintf(out, "// This file is autogenerated\n");
1199 fprintf(out, "\n");
1200
1201 fprintf(out, "#include <statslog.h>\n");
1202 fprintf(out, "\n");
1203 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1204 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1205 fprintf(out, "#include <utils/Vector.h>\n");
1206 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1207 fprintf(out, "#include \"jni.h\"\n");
1208 fprintf(out, "\n");
1209 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1210 fprintf(out, "\n");
1211
1212 fprintf(out, "namespace android {\n");
1213 fprintf(out, "\n");
1214
1215 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
1216 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
1217 atoms.non_chained_signatures, attributionDecl);
1218
Yao Chend54f9dd2017-10-17 17:37:48 +00001219 // Print registration function table
1220 fprintf(out, "/*\n");
1221 fprintf(out, " * JNI registration.\n");
1222 fprintf(out, " */\n");
1223 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -08001224 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
1225 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001226 fprintf(out, "};\n");
1227 fprintf(out, "\n");
1228
1229 // Print registration function
1230 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
1231 fprintf(out, " return RegisterMethodsOrDie(\n");
1232 fprintf(out, " env,\n");
1233 fprintf(out, " \"android/util/StatsLog\",\n");
1234 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1235 fprintf(out, "}\n");
1236
1237 fprintf(out, "\n");
1238 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001239 return 0;
1240}
1241
Yao Chend54f9dd2017-10-17 17:37:48 +00001242static void
1243print_usage()
1244{
1245 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1246 fprintf(stderr, "\n");
1247 fprintf(stderr, "OPTIONS\n");
1248 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1249 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1250 fprintf(stderr, " --help this message\n");
1251 fprintf(stderr, " --java FILENAME the java file to output\n");
1252 fprintf(stderr, " --jni FILENAME the jni file to output\n");
1253}
1254
1255/**
1256 * Do the argument parsing and execute the tasks.
1257 */
1258static int
1259run(int argc, char const*const* argv)
1260{
1261 string cppFilename;
1262 string headerFilename;
1263 string javaFilename;
1264 string jniFilename;
1265
1266 int index = 1;
1267 while (index < argc) {
1268 if (0 == strcmp("--help", argv[index])) {
1269 print_usage();
1270 return 0;
1271 } else if (0 == strcmp("--cpp", argv[index])) {
1272 index++;
1273 if (index >= argc) {
1274 print_usage();
1275 return 1;
1276 }
1277 cppFilename = argv[index];
1278 } else if (0 == strcmp("--header", argv[index])) {
1279 index++;
1280 if (index >= argc) {
1281 print_usage();
1282 return 1;
1283 }
1284 headerFilename = argv[index];
1285 } else if (0 == strcmp("--java", argv[index])) {
1286 index++;
1287 if (index >= argc) {
1288 print_usage();
1289 return 1;
1290 }
1291 javaFilename = argv[index];
1292 } else if (0 == strcmp("--jni", argv[index])) {
1293 index++;
1294 if (index >= argc) {
1295 print_usage();
1296 return 1;
1297 }
1298 jniFilename = argv[index];
1299 }
1300 index++;
1301 }
1302
1303 if (cppFilename.size() == 0
1304 && headerFilename.size() == 0
1305 && javaFilename.size() == 0
1306 && jniFilename.size() == 0) {
1307 print_usage();
1308 return 1;
1309 }
1310
1311 // Collate the parameters
1312 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001313 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001314 if (errorCount != 0) {
1315 return 1;
1316 }
1317
Yangster-mac7604aea2017-12-11 22:55:49 -08001318 AtomDecl attributionDecl;
1319 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001320 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001321 &attributionDecl, &attributionSignature);
1322
Yao Chend54f9dd2017-10-17 17:37:48 +00001323 // Write the .cpp file
1324 if (cppFilename.size() != 0) {
1325 FILE* out = fopen(cppFilename.c_str(), "w");
1326 if (out == NULL) {
1327 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1328 return 1;
1329 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001330 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1331 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001332 fclose(out);
1333 }
1334
1335 // Write the .h file
1336 if (headerFilename.size() != 0) {
1337 FILE* out = fopen(headerFilename.c_str(), "w");
1338 if (out == NULL) {
1339 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1340 return 1;
1341 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001342 errorCount = android::stats_log_api_gen::write_stats_log_header(
1343 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001344 fclose(out);
1345 }
1346
1347 // Write the .java file
1348 if (javaFilename.size() != 0) {
1349 FILE* out = fopen(javaFilename.c_str(), "w");
1350 if (out == NULL) {
1351 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1352 return 1;
1353 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001354 errorCount = android::stats_log_api_gen::write_stats_log_java(
1355 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001356 fclose(out);
1357 }
1358
1359 // Write the jni file
1360 if (jniFilename.size() != 0) {
1361 FILE* out = fopen(jniFilename.c_str(), "w");
1362 if (out == NULL) {
1363 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1364 return 1;
1365 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001366 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1367 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001368 fclose(out);
1369 }
1370
1371 return 0;
1372}
1373
1374}
1375}
1376
1377/**
1378 * Main.
1379 */
1380int
1381main(int argc, char const*const* argv)
1382{
1383 GOOGLE_PROTOBUF_VERIFY_VERSION;
1384
1385 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07001386}