blob: 55440d2261d3beaff5165a08a5626edec97177e0 [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 -080021int maxPushedAtomId = 2;
22
Stefan Lafonae2df012017-11-14 09:17:21 -080023using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000024
Yao Chend54f9dd2017-10-17 17:37:48 +000025/**
26 * Turn lower and camel case into upper case with underscores.
27 */
28static string
29make_constant_name(const string& str)
30{
31 string result;
32 const int N = str.size();
33 bool underscore_next = false;
34 for (int i=0; i<N; i++) {
35 char c = str[i];
36 if (c >= 'A' && c <= 'Z') {
37 if (underscore_next) {
38 result += '_';
39 underscore_next = false;
40 }
41 } else if (c >= 'a' && c <= 'z') {
42 c = 'A' + c - 'a';
43 underscore_next = true;
44 } else if (c == '_') {
45 underscore_next = false;
46 }
47 result += c;
48 }
49 return result;
50}
51
52static const char*
53cpp_type_name(java_type_t type)
54{
55 switch (type) {
56 case JAVA_TYPE_BOOLEAN:
57 return "bool";
58 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070059 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000060 return "int32_t";
61 case JAVA_TYPE_LONG:
62 return "int64_t";
63 case JAVA_TYPE_FLOAT:
64 return "float";
65 case JAVA_TYPE_DOUBLE:
66 return "double";
67 case JAVA_TYPE_STRING:
68 return "char const*";
Yao Chenbbdd67d2018-10-24 12:15:56 -070069 case JAVA_TYPE_BYTE_ARRAY:
Yao Chene89572c2019-01-09 15:41:50 -080070 return "const BytesField&";
Yao Chend54f9dd2017-10-17 17:37:48 +000071 default:
72 return "UNKNOWN";
73 }
74}
75
76static const char*
77java_type_name(java_type_t type)
78{
79 switch (type) {
80 case JAVA_TYPE_BOOLEAN:
81 return "boolean";
82 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070083 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000084 return "int";
85 case JAVA_TYPE_LONG:
86 return "long";
87 case JAVA_TYPE_FLOAT:
88 return "float";
89 case JAVA_TYPE_DOUBLE:
90 return "double";
91 case JAVA_TYPE_STRING:
92 return "java.lang.String";
Yao Chenbbdd67d2018-10-24 12:15:56 -070093 case JAVA_TYPE_BYTE_ARRAY:
94 return "byte[]";
Yao Chend54f9dd2017-10-17 17:37:48 +000095 default:
96 return "UNKNOWN";
97 }
98}
99
Yangster-mac7604aea2017-12-11 22:55:49 -0800100static int write_stats_log_cpp(FILE *out, const Atoms &atoms,
101 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000102 // Print prelude
103 fprintf(out, "// This file is autogenerated\n");
104 fprintf(out, "\n");
105
Yangster-macca5c0862018-04-09 22:39:53 -0700106 fprintf(out, "#include <mutex>\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700107 fprintf(out, "#include <chrono>\n");
108 fprintf(out, "#include <thread>\n");
Jack He34a892d2018-12-20 00:42:31 -0800109 fprintf(out, "#ifdef __ANDROID__\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700110 fprintf(out, "#include <cutils/properties.h>\n");
Jack He34a892d2018-12-20 00:42:31 -0800111 fprintf(out, "#endif\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700112 fprintf(out, "#include <stats_event_list.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000113 fprintf(out, "#include <log/log.h>\n");
114 fprintf(out, "#include <statslog.h>\n");
Yangster-mac330af582018-02-08 15:24:38 -0800115 fprintf(out, "#include <utils/SystemClock.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000116 fprintf(out, "\n");
117
118 fprintf(out, "namespace android {\n");
119 fprintf(out, "namespace util {\n");
Yao Chen80235402017-11-13 20:42:25 -0800120 fprintf(out, "// the single event tag id for all stats logs\n");
121 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Jack He34a892d2018-12-20 00:42:31 -0800122 fprintf(out, "#ifdef __ANDROID__\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700123 fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
Jack He34a892d2018-12-20 00:42:31 -0800124 fprintf(out, "#else\n");
125 fprintf(out, "const static bool kStatsdEnabled = false;\n");
126 fprintf(out, "#endif\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000127
Yao Chenc40a19d2018-03-15 16:48:25 -0700128 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
129 "audio_state_changed",
130 "call_state_changed",
131 "phone_signal_strength_changed",
132 "mobile_bytes_transfer_by_fg_bg",
133 "mobile_bytes_transfer"};
134 fprintf(out,
135 "const std::set<int> "
136 "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n");
137 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
138 atom != atoms.decls.end(); atom++) {
139 if (kTruncatingAtomNames.find(atom->name) ==
140 kTruncatingAtomNames.end()) {
141 string constant = make_constant_name(atom->name);
142 fprintf(out, " %s,\n", constant.c_str());
143 }
144 }
145 fprintf(out, "};\n");
146 fprintf(out, "\n");
147
148 fprintf(out,
149 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
150 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
151 atom != atoms.decls.end(); atom++) {
152 for (vector<AtomField>::const_iterator field = atom->fields.begin();
153 field != atom->fields.end(); field++) {
154 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
155 string constant = make_constant_name(atom->name);
156 fprintf(out, " %s,\n", constant.c_str());
157 break;
158 }
159 }
160 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000161
162 fprintf(out, "};\n");
163 fprintf(out, "\n");
164
165 fprintf(out,
166 "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
167 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
168 atom != atoms.decls.end(); atom++) {
169 if (atom->whitelisted) {
170 string constant = make_constant_name(atom->name);
171 fprintf(out, " %s,\n", constant.c_str());
172 }
173 }
174
Yao Chenc40a19d2018-03-15 16:48:25 -0700175 fprintf(out, "};\n");
176 fprintf(out, "\n");
177
Yangster-macca5c0862018-04-09 22:39:53 -0700178 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700179 fprintf(out, " std::map<int, int> uidField;\n");
180 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
181 atom != atoms.decls.end(); atom++) {
182 if (atom->uidField == 0) {
183 continue;
184 }
185 fprintf(out,
186 "\n // Adding uid field for atom "
187 "(%d)%s\n",
188 atom->code, atom->name.c_str());
189 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
190 make_constant_name(atom->name).c_str(), atom->uidField);
191 }
192
193 fprintf(out, " return uidField;\n");
194 fprintf(out, "};\n");
195
196 fprintf(out,
197 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
198 "getAtomUidField();\n");
199
200 fprintf(out,
201 "static std::map<int, StateAtomFieldOptions> "
202 "getStateAtomFieldOptions() {\n");
203 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
204 fprintf(out, " StateAtomFieldOptions opt;\n");
205 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
206 atom != atoms.decls.end(); atom++) {
207 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
208 continue;
209 }
210 fprintf(out,
211 "\n // Adding primary and exclusive fields for atom "
212 "(%d)%s\n",
213 atom->code, atom->name.c_str());
214 fprintf(out, " opt.primaryFields.clear();\n");
215 for (const auto& field : atom->primaryFields) {
216 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
217 }
218
219 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
220 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
221 make_constant_name(atom->name).c_str());
222 }
223
224 fprintf(out, " return options;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700225 fprintf(out, "}\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700226
227 fprintf(out,
228 "const std::map<int, StateAtomFieldOptions> "
229 "AtomsInfo::kStateAtomsFieldOptions = "
230 "getStateAtomFieldOptions();\n");
231
Yao Chenbbdd67d2018-10-24 12:15:56 -0700232 fprintf(out,
233 "static std::map<int, std::vector<int>> "
234 "getBinaryFieldAtoms() {\n");
235 fprintf(out, " std::map<int, std::vector<int>> options;\n");
236 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
237 atom != atoms.decls.end(); atom++) {
238 if (atom->binaryFields.size() == 0) {
239 continue;
240 }
241 fprintf(out,
242 "\n // Adding binary fields for atom "
243 "(%d)%s\n",
244 atom->code, atom->name.c_str());
245
246 for (const auto& field : atom->binaryFields) {
247 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
248 make_constant_name(atom->name).c_str(), field);
249 }
250 }
251
252 fprintf(out, " return options;\n");
253 fprintf(out, "}\n");
254
255 fprintf(out,
256 "const std::map<int, std::vector<int>> "
257 "AtomsInfo::kBytesFieldAtoms = "
258 "getBinaryFieldAtoms();\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700259
260 fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
261 fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
262 fprintf(out, "static std::mutex mLogdRetryMutex;\n");
263
Yao Chend54f9dd2017-10-17 17:37:48 +0000264 // Print write methods
265 fprintf(out, "\n");
266 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800267 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000268 int argIndex;
269
Yao Chen97e21ec2018-03-29 11:00:38 -0700270 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700271 fprintf(out, "try_stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000272 argIndex = 1;
273 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800274 arg != signature->end(); arg++) {
275 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
276 for (auto chainField : attributionDecl.fields) {
277 if (chainField.javaType == JAVA_TYPE_STRING) {
278 fprintf(out, ", const std::vector<%s>& %s",
279 cpp_type_name(chainField.javaType),
280 chainField.name.c_str());
281 } else {
282 fprintf(out, ", const %s* %s, size_t %s_length",
283 cpp_type_name(chainField.javaType),
284 chainField.name.c_str(), chainField.name.c_str());
285 }
286 }
Yangster-mace124e422018-08-16 10:30:28 -0700287 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700288 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
289 "const std::map<int, int64_t>& arg%d_2, "
290 "const std::map<int, char const*>& arg%d_3, "
291 "const std::map<int, float>& arg%d_4",
292 argIndex, argIndex, argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800293 } else {
294 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
295 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000296 argIndex++;
297 }
298 fprintf(out, ")\n");
299
300 fprintf(out, "{\n");
301 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700302 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700303 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800304 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800305 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000306 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800307 arg != signature->end(); arg++) {
308 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
309 for (const auto &chainField : attributionDecl.fields) {
310 if (chainField.javaType == JAVA_TYPE_STRING) {
311 fprintf(out, " if (%s_length != %s.size()) {\n",
312 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700313 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800314 fprintf(out, " }\n");
315 }
316 }
317 fprintf(out, "\n event.begin();\n");
318 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
319 attributionDecl.fields.front().name.c_str());
320 fprintf(out, " event.begin();\n");
321 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800322 if (chainField.javaType == JAVA_TYPE_STRING) {
323 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
324 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
325 fprintf(out, " } else {\n");
326 fprintf(out, " event << \"\";\n");
327 fprintf(out, " }\n");
328 } else {
329 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
330 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800331 }
332 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000333 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800334 fprintf(out, " event.end();\n\n");
Yangster-mace124e422018-08-16 10:30:28 -0700335 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
336 fprintf(out, " event.begin();\n\n");
337 fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
338 fprintf(out, " event.begin();\n");
339 fprintf(out, " event << it.first;\n");
340 fprintf(out, " event << it.second;\n");
341 fprintf(out, " event.end();\n");
342 fprintf(out, " }\n");
343
344 fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
345 fprintf(out, " event.begin();\n");
346 fprintf(out, " event << it.first;\n");
347 fprintf(out, " event << it.second;\n");
348 fprintf(out, " event.end();\n");
349 fprintf(out, " }\n");
350
351 fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
352 fprintf(out, " event.begin();\n");
353 fprintf(out, " event << it.first;\n");
354 fprintf(out, " event << it.second;\n");
355 fprintf(out, " event.end();\n");
356 fprintf(out, " }\n");
357
Howard Ro4078dd42018-09-27 17:41:08 -0700358 fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
359 fprintf(out, " event.begin();\n");
360 fprintf(out, " event << it.first;\n");
361 fprintf(out, " event << it.second;\n");
362 fprintf(out, " event.end();\n");
363 fprintf(out, " }\n");
364
Yangster-mace124e422018-08-16 10:30:28 -0700365 fprintf(out, " event.end();\n\n");
Yao Chen1fe9f592018-12-06 10:34:25 -0800366 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
367 fprintf(out,
Yao Chene89572c2019-01-09 15:41:50 -0800368 " event.AppendCharArray(arg%d.arg, "
369 "arg%d.arg_length);\n",
Yao Chen1fe9f592018-12-06 10:34:25 -0800370 argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800371 } else {
372 if (*arg == JAVA_TYPE_STRING) {
373 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
374 fprintf(out, " arg%d = \"\";\n", argIndex);
375 fprintf(out, " }\n");
376 }
377 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000378 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000379 argIndex++;
380 }
381
Yao Chen97e21ec2018-03-29 11:00:38 -0700382 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700383 fprintf(out, " } else {\n");
384 fprintf(out, " return 1;\n");
385 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000386 fprintf(out, "}\n");
387 fprintf(out, "\n");
388 }
389
Yangster-macb8382a12018-04-04 10:39:12 -0700390 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
391 signature != atoms.signatures.end(); signature++) {
392 int argIndex;
393
Yangster-mace124e422018-08-16 10:30:28 -0700394 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700395 fprintf(out, "stats_write(int32_t code");
396 argIndex = 1;
397 for (vector<java_type_t>::const_iterator arg = signature->begin();
398 arg != signature->end(); arg++) {
399 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
400 for (auto chainField : attributionDecl.fields) {
401 if (chainField.javaType == JAVA_TYPE_STRING) {
402 fprintf(out, ", const std::vector<%s>& %s",
403 cpp_type_name(chainField.javaType),
404 chainField.name.c_str());
405 } else {
406 fprintf(out, ", const %s* %s, size_t %s_length",
407 cpp_type_name(chainField.javaType),
408 chainField.name.c_str(), chainField.name.c_str());
409 }
410 }
Yangster-mace124e422018-08-16 10:30:28 -0700411 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Yao Chen1fe9f592018-12-06 10:34:25 -0800412 fprintf(out,
413 ", const std::map<int, int32_t>& arg%d_1, "
414 "const std::map<int, int64_t>& arg%d_2, "
415 "const std::map<int, char const*>& arg%d_3, "
416 "const std::map<int, float>& arg%d_4",
417 argIndex, argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700418 } else {
419 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
420 }
421 argIndex++;
422 }
423 fprintf(out, ")\n");
424
425 fprintf(out, "{\n");
426 fprintf(out, " int ret = 0;\n");
427
Yangster-macca5c0862018-04-09 22:39:53 -0700428 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700429 fprintf(out, " ret = try_stats_write(code");
430
431 argIndex = 1;
432 for (vector<java_type_t>::const_iterator arg = signature->begin();
433 arg != signature->end(); arg++) {
434 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
435 for (auto chainField : attributionDecl.fields) {
436 if (chainField.javaType == JAVA_TYPE_STRING) {
437 fprintf(out, ", %s",
438 chainField.name.c_str());
439 } else {
440 fprintf(out, ", %s, %s_length",
441 chainField.name.c_str(), chainField.name.c_str());
442 }
443 }
Yao Chen1fe9f592018-12-06 10:34:25 -0800444 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
445 fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex,
446 argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700447 } else {
448 fprintf(out, ", arg%d", argIndex);
449 }
450 argIndex++;
451 }
452 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700453 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700454
455 fprintf(out, " {\n");
456 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
457 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
458 "kMinRetryIntervalNs) break;\n");
459 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
460 fprintf(out, " }\n");
461 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700462 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700463 fprintf(out, " if (ret < 0) {\n");
Yao Chen39b67992018-11-08 15:32:17 -0800464 fprintf(out, " note_log_drop(ret);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700465 fprintf(out, " }\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700466 fprintf(out, " return ret;\n");
467 fprintf(out, "}\n");
468 fprintf(out, "\n");
469 }
470
Yangster-macba5b9e42018-01-10 21:31:59 -0800471 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
472 signature != atoms.non_chained_signatures.end(); signature++) {
473 int argIndex;
474
Yao Chen97e21ec2018-03-29 11:00:38 -0700475 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700476 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800477 argIndex = 1;
478 for (vector<java_type_t>::const_iterator arg = signature->begin();
479 arg != signature->end(); arg++) {
480 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
481 argIndex++;
482 }
483 fprintf(out, ")\n");
484
485 fprintf(out, "{\n");
486 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700487 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700488 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800489 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800490 fprintf(out, " event << code;\n\n");
491 for (vector<java_type_t>::const_iterator arg = signature->begin();
492 arg != signature->end(); arg++) {
493 if (argIndex == 1) {
494 fprintf(out, " event.begin();\n\n");
495 fprintf(out, " event.begin();\n");
496 }
497 if (*arg == JAVA_TYPE_STRING) {
498 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
499 fprintf(out, " arg%d = \"\";\n", argIndex);
500 fprintf(out, " }\n");
501 }
Yao Chene89572c2019-01-09 15:41:50 -0800502 if (*arg == JAVA_TYPE_BYTE_ARRAY) {
503 fprintf(out,
504 " event.AppendCharArray(arg%d.arg, "
505 "arg%d.arg_length);",
506 argIndex, argIndex);
507 } else {
508 fprintf(out, " event << arg%d;\n", argIndex);
509 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800510 if (argIndex == 2) {
511 fprintf(out, " event.end();\n\n");
512 fprintf(out, " event.end();\n\n");
513 }
514 argIndex++;
515 }
516
Yao Chen97e21ec2018-03-29 11:00:38 -0700517 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700518 fprintf(out, " } else {\n");
519 fprintf(out, " return 1;\n");
520 fprintf(out, " }\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800521 fprintf(out, "}\n");
522 fprintf(out, "\n");
523 }
Yangster-macb8382a12018-04-04 10:39:12 -0700524
525 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
526 signature != atoms.non_chained_signatures.end(); signature++) {
527 int argIndex;
528
529 fprintf(out, "int\n");
530 fprintf(out, "stats_write_non_chained(int32_t code");
531 argIndex = 1;
532 for (vector<java_type_t>::const_iterator arg = signature->begin();
533 arg != signature->end(); arg++) {
534 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
535 argIndex++;
536 }
537 fprintf(out, ")\n");
538
539 fprintf(out, "{\n");
540
541 fprintf(out, " int ret = 0;\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700542 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700543 fprintf(out, " ret = try_stats_write_non_chained(code");
544
545 argIndex = 1;
546 for (vector<java_type_t>::const_iterator arg = signature->begin();
547 arg != signature->end(); arg++) {
548 fprintf(out, ", arg%d", argIndex);
549 argIndex++;
550 }
551 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700552 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700553
554 fprintf(out, " {\n");
555 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
556 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
557 "kMinRetryIntervalNs) break;\n");
558 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
559 fprintf(out, " }\n");
560
561 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700562 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700563 fprintf(out, " if (ret < 0) {\n");
Yao Chen39b67992018-11-08 15:32:17 -0800564 fprintf(out, " note_log_drop(ret);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700565 fprintf(out, " }\n");
566 fprintf(out, " return ret;\n\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700567 fprintf(out, "}\n");
568
569 fprintf(out, "\n");
570 }
571
572
Yao Chend54f9dd2017-10-17 17:37:48 +0000573 // Print footer
574 fprintf(out, "\n");
575 fprintf(out, "} // namespace util\n");
576 fprintf(out, "} // namespace android\n");
577
578 return 0;
579}
580
Yangster-macba5b9e42018-01-10 21:31:59 -0800581void build_non_chained_decl_map(const Atoms& atoms,
582 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
583 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
584 atom != atoms.non_chained_decls.end(); atom++) {
585 decl_map->insert(std::make_pair(atom->code, atom));
586 }
587}
588
589static void write_cpp_usage(
590 FILE* out, const string& method_name, const string& atom_code_name,
591 const AtomDecl& atom, const AtomDecl &attributionDecl) {
Yao Chene89572c2019-01-09 15:41:50 -0800592 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
593 atom_code_name.c_str());
594
Yangster-macba5b9e42018-01-10 21:31:59 -0800595 for (vector<AtomField>::const_iterator field = atom.fields.begin();
596 field != atom.fields.end(); field++) {
597 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
598 for (auto chainField : attributionDecl.fields) {
599 if (chainField.javaType == JAVA_TYPE_STRING) {
600 fprintf(out, ", const std::vector<%s>& %s",
601 cpp_type_name(chainField.javaType),
602 chainField.name.c_str());
603 } else {
604 fprintf(out, ", const %s* %s, size_t %s_length",
605 cpp_type_name(chainField.javaType),
606 chainField.name.c_str(), chainField.name.c_str());
607 }
608 }
Yangster-mace124e422018-08-16 10:30:28 -0700609 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700610 fprintf(out, ", const std::map<int, int32_t>& %s_int"
611 ", const std::map<int, int64_t>& %s_long"
Yangster-mace124e422018-08-16 10:30:28 -0700612 ", const std::map<int, char const*>& %s_str"
613 ", const std::map<int, float>& %s_float",
Howard Ro4078dd42018-09-27 17:41:08 -0700614 field->name.c_str(),
615 field->name.c_str(),
616 field->name.c_str(),
617 field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800618 } else {
619 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
620 }
621 }
622 fprintf(out, ");\n");
623}
624
625static void write_cpp_method_header(
626 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
627 const AtomDecl &attributionDecl) {
628 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
629 signature != signatures.end(); signature++) {
Yangster-mace124e422018-08-16 10:30:28 -0700630 fprintf(out, "int %s(int32_t code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800631 int argIndex = 1;
632 for (vector<java_type_t>::const_iterator arg = signature->begin();
633 arg != signature->end(); arg++) {
634 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
635 for (auto chainField : attributionDecl.fields) {
636 if (chainField.javaType == JAVA_TYPE_STRING) {
637 fprintf(out, ", const std::vector<%s>& %s",
638 cpp_type_name(chainField.javaType), chainField.name.c_str());
639 } else {
640 fprintf(out, ", const %s* %s, size_t %s_length",
641 cpp_type_name(chainField.javaType),
642 chainField.name.c_str(), chainField.name.c_str());
643 }
644 }
Yangster-mace124e422018-08-16 10:30:28 -0700645 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700646 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
647 "const std::map<int, int64_t>& arg%d_2, "
648 "const std::map<int, char const*>& arg%d_3, "
649 "const std::map<int, float>& arg%d_4",
650 argIndex, argIndex, argIndex, argIndex);
Yangster-macba5b9e42018-01-10 21:31:59 -0800651 } else {
652 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
653 }
654 argIndex++;
655 }
656 fprintf(out, ");\n");
657
658 }
659}
Yao Chend54f9dd2017-10-17 17:37:48 +0000660
661static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800662write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000663{
Yao Chend54f9dd2017-10-17 17:37:48 +0000664 // Print prelude
665 fprintf(out, "// This file is autogenerated\n");
666 fprintf(out, "\n");
667 fprintf(out, "#pragma once\n");
668 fprintf(out, "\n");
669 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800670 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800671 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800672 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000673 fprintf(out, "\n");
674
675 fprintf(out, "namespace android {\n");
676 fprintf(out, "namespace util {\n");
677 fprintf(out, "\n");
678 fprintf(out, "/*\n");
679 fprintf(out, " * API For logging statistics events.\n");
680 fprintf(out, " */\n");
681 fprintf(out, "\n");
682 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700683 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000684 fprintf(out, " */\n");
685 fprintf(out, "enum {\n");
686
Yangster-macba5b9e42018-01-10 21:31:59 -0800687 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
688 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
689
Yao Chend54f9dd2017-10-17 17:37:48 +0000690 size_t i = 0;
691 // Print constants
692 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800693 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000694 string constant = make_constant_name(atom->name);
695 fprintf(out, "\n");
696 fprintf(out, " /**\n");
697 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800698 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
699
700 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
701 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
702 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
703 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000704 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000705 fprintf(out, " */\n");
706 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
707 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800708 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
709 maxPushedAtomId = atom->code;
710 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000711 i++;
712 }
713 fprintf(out, "\n");
714 fprintf(out, "};\n");
715 fprintf(out, "\n");
716
Yao Chene89572c2019-01-09 15:41:50 -0800717 fprintf(out, "struct BytesField {\n");
718 fprintf(out,
719 " BytesField(char const* array, size_t len) : arg(array), "
720 "arg_length(len) {}\n");
721 fprintf(out, " char const* arg;\n");
722 fprintf(out, " size_t arg_length;\n");
723 fprintf(out, "};\n");
724 fprintf(out, "\n");
725
Yao Chen9c1debe2018-02-19 14:39:19 -0800726 fprintf(out, "struct StateAtomFieldOptions {\n");
727 fprintf(out, " std::vector<int> primaryFields;\n");
728 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700729 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800730 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700731
732 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800733 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700734 " const static std::set<int> "
735 "kNotTruncatingTimestampAtomWhiteList;\n");
736 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
737 fprintf(out,
738 " const static std::set<int> kAtomsWithAttributionChain;\n");
739 fprintf(out,
740 " const static std::map<int, StateAtomFieldOptions> "
741 "kStateAtomsFieldOptions;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700742 fprintf(out,
743 " const static std::map<int, std::vector<int>> "
744 "kBytesFieldAtoms;");
Andrei Oneada01ea52019-01-30 15:28:36 +0000745 fprintf(out,
746 " const static std::set<int> kWhitelistedAtoms;\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800747 fprintf(out, "};\n");
748
Yao Chenc40a19d2018-03-15 16:48:25 -0700749 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
750 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800751
Yao Chend54f9dd2017-10-17 17:37:48 +0000752 // Print write methods
753 fprintf(out, "//\n");
754 fprintf(out, "// Write methods\n");
755 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800756 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
757
758 fprintf(out, "//\n");
759 fprintf(out, "// Write flattened methods\n");
760 fprintf(out, "//\n");
761 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
762 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000763
764 fprintf(out, "\n");
765 fprintf(out, "} // namespace util\n");
766 fprintf(out, "} // namespace android\n");
767
768 return 0;
769}
770
Bookatz0bd97202018-06-05 12:42:37 -0700771static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
772 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800773 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
774 method_name.c_str(), atom_code_name.c_str());
775 for (vector<AtomField>::const_iterator field = atom.fields.begin();
776 field != atom.fields.end(); field++) {
777 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700778 fprintf(out, ", android.os.WorkSource workSource");
Yangster-mace124e422018-08-16 10:30:28 -0700779 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
780 fprintf(out, ", SparseArray<Object> value_map");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700781 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
782 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800783 } else {
784 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
785 }
786 }
Bookatz0bd97202018-06-05 12:42:37 -0700787 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800788}
789
790static void write_java_method(
791 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
792 const AtomDecl &attributionDecl) {
793 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
794 signature != signatures.end(); signature++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800795 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700796 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800797 int argIndex = 1;
798 for (vector<java_type_t>::const_iterator arg = signature->begin();
799 arg != signature->end(); arg++) {
800 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
801 for (auto chainField : attributionDecl.fields) {
802 fprintf(out, ", %s[] %s",
803 java_type_name(chainField.javaType), chainField.name.c_str());
804 }
Yangster-mace124e422018-08-16 10:30:28 -0700805 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
806 fprintf(out, ", SparseArray<Object> value_map");
Yangster-macba5b9e42018-01-10 21:31:59 -0800807 } else {
808 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
809 }
810 argIndex++;
811 }
812 fprintf(out, ");\n");
813 }
814}
815
Bookatz0bd97202018-06-05 12:42:37 -0700816static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) {
817 fprintf(out, "\n // WorkSource methods.\n");
818 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
819 signature != signatures.end(); signature++) {
820 // Determine if there is Attribution in this signature.
821 int attributionArg = -1;
822 int argIndexMax = 0;
823 for (vector<java_type_t>::const_iterator arg = signature->begin();
824 arg != signature->end(); arg++) {
825 argIndexMax++;
826 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
827 if (attributionArg > -1) {
828 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
829 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
830 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
831 return;
832 }
833 attributionArg = argIndexMax;
834 }
835 }
836 if (attributionArg < 0) {
837 continue;
838 }
839
840 // Method header (signature)
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800841 fprintf(out, " /** @hide */\n");
Bookatz0bd97202018-06-05 12:42:37 -0700842 fprintf(out, " public static void write(int code");
843 int argIndex = 1;
844 for (vector<java_type_t>::const_iterator arg = signature->begin();
845 arg != signature->end(); arg++) {
846 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
847 fprintf(out, ", WorkSource ws");
848 } else {
849 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
850 }
851 argIndex++;
852 }
853 fprintf(out, ") {\n");
854
855 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
856 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
857 fprintf(out, " write_non_chained(code");
858 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
859 if (argIndex == attributionArg) {
860 fprintf(out, ", ws.get(i), ws.getName(i)");
861 } else {
862 fprintf(out, ", arg%d", argIndex);
863 }
864 }
865 fprintf(out, ");\n");
866 fprintf(out, " }\n"); // close flor-loop
867
868 // write() component.
869 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
870 fprintf(out, " if (workChains != null) {\n");
871 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
872 fprintf(out, " write(code");
873 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
874 if (argIndex == attributionArg) {
875 fprintf(out, ", wc.getUids(), wc.getTags()");
876 } else {
877 fprintf(out, ", arg%d", argIndex);
878 }
879 }
880 fprintf(out, ");\n");
881 fprintf(out, " }\n"); // close for-loop
882 fprintf(out, " }\n"); // close if
883 fprintf(out, " }\n"); // close method
884 }
885}
Yangster-macba5b9e42018-01-10 21:31:59 -0800886
Yao Chend54f9dd2017-10-17 17:37:48 +0000887static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800888write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000889{
Yao Chend54f9dd2017-10-17 17:37:48 +0000890 // Print prelude
891 fprintf(out, "// This file is autogenerated\n");
892 fprintf(out, "\n");
893 fprintf(out, "package android.util;\n");
894 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -0700895 fprintf(out, "import android.os.WorkSource;\n");
Yangster-mace124e422018-08-16 10:30:28 -0700896 fprintf(out, "import android.util.SparseArray;\n");
Bookatz0bd97202018-06-05 12:42:37 -0700897 fprintf(out, "import java.util.ArrayList;\n");
898 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000899 fprintf(out, "\n");
900 fprintf(out, "/**\n");
901 fprintf(out, " * API For logging statistics events.\n");
902 fprintf(out, " * @hide\n");
903 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800904 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700905 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000906
Yangster-macba5b9e42018-01-10 21:31:59 -0800907 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
908 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
909
Stefan Lafon9478f352017-10-30 21:20:20 -0700910 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000911 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
912 atom != atoms.decls.end(); atom++) {
913 string constant = make_constant_name(atom->name);
914 fprintf(out, "\n");
915 fprintf(out, " /**\n");
Bookatz0bd97202018-06-05 12:42:37 -0700916 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
917 write_java_usage(out, "write", constant, *atom);
Yangster-macba5b9e42018-01-10 21:31:59 -0800918 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
919 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
Bookatz0bd97202018-06-05 12:42:37 -0700920 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
Yao Chend54f9dd2017-10-17 17:37:48 +0000921 }
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800922 fprintf(out, " * @hide\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000923 fprintf(out, " */\n");
924 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
925 }
926 fprintf(out, "\n");
927
Stefan Lafon9478f352017-10-30 21:20:20 -0700928 // Print constants for the enum values.
929 fprintf(out, " // Constants for enum values.\n\n");
930 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800931 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700932 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800933 field != atom->fields.end(); field++) {
934 if (field->javaType == JAVA_TYPE_ENUM) {
935 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
936 field->name.c_str());
937 for (map<int, string>::const_iterator value = field->enumValues.begin();
938 value != field->enumValues.end(); value++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800939 fprintf(out, " /** @hide */\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800940 fprintf(out, " public static final int %s__%s__%s = %d;\n",
941 make_constant_name(atom->message).c_str(),
942 make_constant_name(field->name).c_str(),
943 make_constant_name(value->second).c_str(),
944 value->first);
945 }
946 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700947 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700948 }
949 }
950
Yao Chend54f9dd2017-10-17 17:37:48 +0000951 // Print write methods
952 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800953 write_java_method(out, "write", atoms.signatures, attributionDecl);
954 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Bookatz0bd97202018-06-05 12:42:37 -0700955 write_java_work_source_method(out, atoms.signatures);
Yao Chend54f9dd2017-10-17 17:37:48 +0000956
957 fprintf(out, "}\n");
958
959 return 0;
960}
961
962static const char*
963jni_type_name(java_type_t type)
964{
965 switch (type) {
966 case JAVA_TYPE_BOOLEAN:
967 return "jboolean";
968 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700969 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000970 return "jint";
971 case JAVA_TYPE_LONG:
972 return "jlong";
973 case JAVA_TYPE_FLOAT:
974 return "jfloat";
975 case JAVA_TYPE_DOUBLE:
976 return "jdouble";
977 case JAVA_TYPE_STRING:
978 return "jstring";
Yao Chenbbdd67d2018-10-24 12:15:56 -0700979 case JAVA_TYPE_BYTE_ARRAY:
980 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +0000981 default:
982 return "UNKNOWN";
983 }
984}
985
Yangster-mac7604aea2017-12-11 22:55:49 -0800986static const char*
987jni_array_type_name(java_type_t type)
988{
989 switch (type) {
990 case JAVA_TYPE_INT:
991 return "jintArray";
Yangster-mace124e422018-08-16 10:30:28 -0700992 case JAVA_TYPE_FLOAT:
993 return "jfloatArray";
Yangster-mac7604aea2017-12-11 22:55:49 -0800994 case JAVA_TYPE_STRING:
995 return "jobjectArray";
996 default:
997 return "UNKNOWN";
998 }
999}
1000
Yao Chend54f9dd2017-10-17 17:37:48 +00001001static string
Yangster-macba5b9e42018-01-10 21:31:59 -08001002jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +00001003{
Yangster-macba5b9e42018-01-10 21:31:59 -08001004 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +00001005 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001006 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001007 switch (*arg) {
1008 case JAVA_TYPE_BOOLEAN:
1009 result += "_boolean";
1010 break;
1011 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001012 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001013 result += "_int";
1014 break;
1015 case JAVA_TYPE_LONG:
1016 result += "_long";
1017 break;
1018 case JAVA_TYPE_FLOAT:
1019 result += "_float";
1020 break;
1021 case JAVA_TYPE_DOUBLE:
1022 result += "_double";
1023 break;
1024 case JAVA_TYPE_STRING:
1025 result += "_String";
1026 break;
Yangster-mac7604aea2017-12-11 22:55:49 -08001027 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1028 result += "_AttributionChain";
1029 break;
Yangster-mace124e422018-08-16 10:30:28 -07001030 case JAVA_TYPE_KEY_VALUE_PAIR:
1031 result += "_KeyValuePairs";
1032 break;
Yao Chenbbdd67d2018-10-24 12:15:56 -07001033 case JAVA_TYPE_BYTE_ARRAY:
1034 result += "_bytes";
1035 break;
Yao Chend54f9dd2017-10-17 17:37:48 +00001036 default:
1037 result += "_UNKNOWN";
1038 break;
1039 }
1040 }
1041 return result;
1042}
1043
1044static const char*
1045java_type_signature(java_type_t type)
1046{
1047 switch (type) {
1048 case JAVA_TYPE_BOOLEAN:
1049 return "Z";
1050 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001051 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001052 return "I";
1053 case JAVA_TYPE_LONG:
1054 return "J";
1055 case JAVA_TYPE_FLOAT:
1056 return "F";
1057 case JAVA_TYPE_DOUBLE:
1058 return "D";
1059 case JAVA_TYPE_STRING:
1060 return "Ljava/lang/String;";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001061 case JAVA_TYPE_BYTE_ARRAY:
1062 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +00001063 default:
1064 return "UNKNOWN";
1065 }
1066}
1067
1068static string
Yangster-mac7604aea2017-12-11 22:55:49 -08001069jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001070{
1071 string result("(I");
1072 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001073 arg != signature.end(); arg++) {
1074 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1075 for (auto chainField : attributionDecl.fields) {
1076 result += "[";
1077 result += java_type_signature(chainField.javaType);
1078 }
Yangster-mace124e422018-08-16 10:30:28 -07001079 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1080 result += "Landroid/util/SparseArray;";
Yangster-mac7604aea2017-12-11 22:55:49 -08001081 } else {
1082 result += java_type_signature(*arg);
1083 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001084 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001085 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +00001086 return result;
1087}
1088
Yangster-mace124e422018-08-16 10:30:28 -07001089static void write_key_value_map_jni(FILE* out) {
Howard Ro4078dd42018-09-27 17:41:08 -07001090 fprintf(out, " std::map<int, int32_t> int32_t_map;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001091 fprintf(out, " std::map<int, int64_t> int64_t_map;\n");
1092 fprintf(out, " std::map<int, float> float_map;\n");
1093 fprintf(out, " std::map<int, char const*> string_map;\n\n");
1094
1095 fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n");
1096
1097 fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n");
1098 fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n");
1099 fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n");
1100
1101
1102 fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n");
1103
Howard Ro4078dd42018-09-27 17:41:08 -07001104 fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001105 fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n");
1106 fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n");
1107 fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001108 fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001109 fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n");
1110 fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n");
1111
1112 fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n");
1113 fprintf(out, " for(int i = 0; i < jsize; i++) {\n");
1114 fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n");
1115 fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n");
1116 fprintf(out, " if (jvalue_obj == NULL) { continue; }\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001117 fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n");
1118 fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n");
1119 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n");
Yangster-mace124e422018-08-16 10:30:28 -07001120 fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n");
1121 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n");
1122 fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n");
1123 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n");
1124 fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n");
1125 fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n");
1126 fprintf(out, " scoped_ufs.push_back(std::move(utf));\n");
1127 fprintf(out, " }\n");
1128 fprintf(out, " }\n");
1129}
1130
Yao Chend54f9dd2017-10-17 17:37:48 +00001131static int
Yangster-macba5b9e42018-01-10 21:31:59 -08001132write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
1133 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001134{
Yao Chend54f9dd2017-10-17 17:37:48 +00001135 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -08001136 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1137 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001138 int argIndex;
1139
Yao Chen97e21ec2018-03-29 11:00:38 -07001140 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001141 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -08001142 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001143 argIndex = 1;
1144 for (vector<java_type_t>::const_iterator arg = signature->begin();
1145 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001146 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1147 for (auto chainField : attributionDecl.fields) {
1148 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1149 chainField.name.c_str());
1150 }
Yangster-mace124e422018-08-16 10:30:28 -07001151 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1152 fprintf(out, ", jobject value_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001153 } else {
1154 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1155 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001156 argIndex++;
1157 }
1158 fprintf(out, ")\n");
1159
1160 fprintf(out, "{\n");
1161
1162 // Prepare strings
1163 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001164 bool hadStringOrChain = false;
Yangster-mace124e422018-08-16 10:30:28 -07001165 bool isKeyValuePairAtom = false;
Yao Chend54f9dd2017-10-17 17:37:48 +00001166 for (vector<java_type_t>::const_iterator arg = signature->begin();
1167 arg != signature->end(); arg++) {
1168 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001169 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001170 fprintf(out, " const char* str%d;\n", argIndex);
1171 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1172 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1173 argIndex, argIndex);
1174 fprintf(out, " } else {\n");
1175 fprintf(out, " str%d = NULL;\n", argIndex);
1176 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001177 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1178 hadStringOrChain = true;
1179 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1180 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chen1fe9f592018-12-06 10:34:25 -08001181 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen8e6f9982018-11-29 09:39:45 -08001182 fprintf(out,
1183 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1184 "0) {\n",
1185 argIndex, argIndex);
Yao Chenbbdd67d2018-10-24 12:15:56 -07001186 fprintf(out,
1187 " jbyte_array%d = "
1188 "env->GetByteArrayElements(arg%d, NULL);\n",
1189 argIndex, argIndex);
1190 fprintf(out,
Yao Chen1fe9f592018-12-06 10:34:25 -08001191 " str%d_length = env->GetArrayLength(arg%d);\n",
1192 argIndex, argIndex);
1193 fprintf(out,
Yao Chenbbdd67d2018-10-24 12:15:56 -07001194 " str%d = "
1195 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1196 "d, NULL));\n",
1197 argIndex, argIndex);
1198 fprintf(out, " } else {\n");
1199 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1200 fprintf(out, " str%d = NULL;\n", argIndex);
1201 fprintf(out, " }\n");
1202
Yao Chene89572c2019-01-09 15:41:50 -08001203 fprintf(out,
1204 " android::util::BytesField bytesField%d(str%d, "
1205 "str%d_length);",
1206 argIndex, argIndex, argIndex);
1207
Yangster-mac7604aea2017-12-11 22:55:49 -08001208 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1209 hadStringOrChain = true;
1210 for (auto chainField : attributionDecl.fields) {
1211 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1212 chainField.name.c_str(), chainField.name.c_str());
1213 if (chainField.name != attributionDecl.fields.front().name) {
1214 fprintf(out, " if (%s_length != %s_length) {\n",
1215 chainField.name.c_str(),
1216 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001217 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001218 fprintf(out, " }\n");
1219 }
1220 if (chainField.javaType == JAVA_TYPE_INT) {
1221 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1222 chainField.name.c_str(), chainField.name.c_str());
1223 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1224 fprintf(out, " std::vector<%s> %s_vec;\n",
1225 cpp_type_name(chainField.javaType), chainField.name.c_str());
1226 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1227 chainField.name.c_str());
1228 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1229 chainField.name.c_str());
1230 fprintf(out, " jstring jstr = "
1231 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1232 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001233 fprintf(out, " if (jstr == NULL) {\n");
1234 fprintf(out, " %s_vec.push_back(NULL);\n",
1235 chainField.name.c_str());
1236 fprintf(out, " } else {\n");
1237 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001238 "new ScopedUtfChars(env, jstr);\n",
1239 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001240 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001241 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001242 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001243 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001244 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001245 fprintf(out, " }\n");
1246 }
1247 fprintf(out, "\n");
1248 }
Yangster-mace124e422018-08-16 10:30:28 -07001249 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1250 isKeyValuePairAtom = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001251 }
1252 argIndex++;
1253 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001254 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1255 // chains.
Yangster-mace124e422018-08-16 10:30:28 -07001256 if (!hadStringOrChain && !isKeyValuePairAtom) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001257 fprintf(out, " (void)env;\n");
1258 }
Yangster-mace124e422018-08-16 10:30:28 -07001259 if (isKeyValuePairAtom) {
1260 write_key_value_map_jni(out);
1261 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001262
1263 // stats_write call
1264 argIndex = 1;
Yao Chene89572c2019-01-09 15:41:50 -08001265 fprintf(out, "\n int ret = android::util::%s(code",
1266 cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001267 for (vector<java_type_t>::const_iterator arg = signature->begin();
1268 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001269 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1270 for (auto chainField : attributionDecl.fields) {
1271 if (chainField.javaType == JAVA_TYPE_INT) {
1272 fprintf(out, ", (const %s*)%s_array, %s_length",
1273 cpp_type_name(chainField.javaType),
1274 chainField.name.c_str(), chainField.name.c_str());
1275 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1276 fprintf(out, ", %s_vec", chainField.name.c_str());
1277 }
1278 }
Yangster-mace124e422018-08-16 10:30:28 -07001279 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -07001280 fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map");
Yao Chene89572c2019-01-09 15:41:50 -08001281 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1282 fprintf(out, ", bytesField%d", argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -08001283 } else {
Yao Chene89572c2019-01-09 15:41:50 -08001284 const char* argName =
1285 (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001286 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1287 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001288 argIndex++;
1289 }
1290 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001291 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001292
1293 // Clean up strings
1294 argIndex = 1;
1295 for (vector<java_type_t>::const_iterator arg = signature->begin();
1296 arg != signature->end(); arg++) {
1297 if (*arg == JAVA_TYPE_STRING) {
1298 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1299 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1300 argIndex, argIndex);
1301 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001302 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1303 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1304 fprintf(out,
1305 " env->ReleaseByteArrayElements(arg%d, "
1306 "jbyte_array%d, 0);\n",
1307 argIndex, argIndex);
1308 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001309 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1310 for (auto chainField : attributionDecl.fields) {
1311 if (chainField.javaType == JAVA_TYPE_INT) {
1312 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1313 chainField.name.c_str(), chainField.name.c_str());
1314 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001315 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001316 chainField.name.c_str());
1317 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1318 fprintf(out, " }\n");
1319 }
1320 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001321 }
1322 argIndex++;
1323 }
Yangster-mace124e422018-08-16 10:30:28 -07001324
Yao Chen97e21ec2018-03-29 11:00:38 -07001325 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001326
1327 fprintf(out, "}\n");
1328 fprintf(out, "\n");
1329 }
1330
Yangster-macba5b9e42018-01-10 21:31:59 -08001331
1332 return 0;
1333}
1334
1335void write_jni_registration(FILE* out, const string& java_method_name,
1336 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
1337 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1338 signature != signatures.end(); signature++) {
1339 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1340 java_method_name.c_str(),
1341 jni_function_signature(*signature, attributionDecl).c_str(),
1342 jni_function_name(java_method_name, *signature).c_str());
1343 }
1344}
1345
1346static int
1347write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1348{
1349 // Print prelude
1350 fprintf(out, "// This file is autogenerated\n");
1351 fprintf(out, "\n");
1352
1353 fprintf(out, "#include <statslog.h>\n");
1354 fprintf(out, "\n");
1355 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1356 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1357 fprintf(out, "#include <utils/Vector.h>\n");
1358 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1359 fprintf(out, "#include \"jni.h\"\n");
1360 fprintf(out, "\n");
1361 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1362 fprintf(out, "\n");
1363
1364 fprintf(out, "namespace android {\n");
1365 fprintf(out, "\n");
1366
1367 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
1368 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
1369 atoms.non_chained_signatures, attributionDecl);
1370
Yao Chend54f9dd2017-10-17 17:37:48 +00001371 // Print registration function table
1372 fprintf(out, "/*\n");
1373 fprintf(out, " * JNI registration.\n");
1374 fprintf(out, " */\n");
1375 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -08001376 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
1377 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001378 fprintf(out, "};\n");
1379 fprintf(out, "\n");
1380
1381 // Print registration function
1382 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
1383 fprintf(out, " return RegisterMethodsOrDie(\n");
1384 fprintf(out, " env,\n");
1385 fprintf(out, " \"android/util/StatsLog\",\n");
1386 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1387 fprintf(out, "}\n");
1388
1389 fprintf(out, "\n");
1390 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001391 return 0;
1392}
1393
Yao Chend54f9dd2017-10-17 17:37:48 +00001394static void
1395print_usage()
1396{
1397 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1398 fprintf(stderr, "\n");
1399 fprintf(stderr, "OPTIONS\n");
1400 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1401 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1402 fprintf(stderr, " --help this message\n");
1403 fprintf(stderr, " --java FILENAME the java file to output\n");
1404 fprintf(stderr, " --jni FILENAME the jni file to output\n");
1405}
1406
1407/**
1408 * Do the argument parsing and execute the tasks.
1409 */
1410static int
1411run(int argc, char const*const* argv)
1412{
1413 string cppFilename;
1414 string headerFilename;
1415 string javaFilename;
1416 string jniFilename;
1417
1418 int index = 1;
1419 while (index < argc) {
1420 if (0 == strcmp("--help", argv[index])) {
1421 print_usage();
1422 return 0;
1423 } else if (0 == strcmp("--cpp", argv[index])) {
1424 index++;
1425 if (index >= argc) {
1426 print_usage();
1427 return 1;
1428 }
1429 cppFilename = argv[index];
1430 } else if (0 == strcmp("--header", argv[index])) {
1431 index++;
1432 if (index >= argc) {
1433 print_usage();
1434 return 1;
1435 }
1436 headerFilename = argv[index];
1437 } else if (0 == strcmp("--java", argv[index])) {
1438 index++;
1439 if (index >= argc) {
1440 print_usage();
1441 return 1;
1442 }
1443 javaFilename = argv[index];
1444 } else if (0 == strcmp("--jni", argv[index])) {
1445 index++;
1446 if (index >= argc) {
1447 print_usage();
1448 return 1;
1449 }
1450 jniFilename = argv[index];
1451 }
1452 index++;
1453 }
1454
1455 if (cppFilename.size() == 0
1456 && headerFilename.size() == 0
1457 && javaFilename.size() == 0
1458 && jniFilename.size() == 0) {
1459 print_usage();
1460 return 1;
1461 }
1462
1463 // Collate the parameters
1464 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001465 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001466 if (errorCount != 0) {
1467 return 1;
1468 }
1469
Yangster-mac7604aea2017-12-11 22:55:49 -08001470 AtomDecl attributionDecl;
1471 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001472 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001473 &attributionDecl, &attributionSignature);
1474
Yao Chend54f9dd2017-10-17 17:37:48 +00001475 // Write the .cpp file
1476 if (cppFilename.size() != 0) {
1477 FILE* out = fopen(cppFilename.c_str(), "w");
1478 if (out == NULL) {
1479 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1480 return 1;
1481 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001482 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1483 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001484 fclose(out);
1485 }
1486
1487 // Write the .h file
1488 if (headerFilename.size() != 0) {
1489 FILE* out = fopen(headerFilename.c_str(), "w");
1490 if (out == NULL) {
1491 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1492 return 1;
1493 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001494 errorCount = android::stats_log_api_gen::write_stats_log_header(
1495 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001496 fclose(out);
1497 }
1498
1499 // Write the .java file
1500 if (javaFilename.size() != 0) {
1501 FILE* out = fopen(javaFilename.c_str(), "w");
1502 if (out == NULL) {
1503 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1504 return 1;
1505 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001506 errorCount = android::stats_log_api_gen::write_stats_log_java(
1507 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001508 fclose(out);
1509 }
1510
1511 // Write the jni file
1512 if (jniFilename.size() != 0) {
1513 FILE* out = fopen(jniFilename.c_str(), "w");
1514 if (out == NULL) {
1515 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1516 return 1;
1517 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001518 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1519 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001520 fclose(out);
1521 }
1522
1523 return 0;
1524}
1525
1526}
1527}
1528
1529/**
1530 * Main.
1531 */
1532int
1533main(int argc, char const*const* argv)
1534{
1535 GOOGLE_PROTOBUF_VERIFY_VERSION;
1536
1537 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07001538}