blob: 4e6d07303b0218270121ccb2d08d1a72d2e71dae [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
Tej Singh810eeb32019-03-07 19:08:52 -080015#include "android-base/strings.h"
16
Yao Chend54f9dd2017-10-17 17:37:48 +000017using namespace google::protobuf;
18using namespace std;
19
20namespace android {
21namespace stats_log_api_gen {
22
Yao Chenb3561512017-11-21 18:07:17 -080023int maxPushedAtomId = 2;
24
Tej Singh810eeb32019-03-07 19:08:52 -080025const string DEFAULT_MODULE_NAME = "DEFAULT";
26const string DEFAULT_CPP_NAMESPACE = "android,util";
27const string DEFAULT_CPP_HEADER_IMPORT = "statslog.h";
Tej Singh2910d5a2019-03-23 17:26:32 -070028const string DEFAULT_JAVA_PACKAGE = "android.util";
29const string DEFAULT_JAVA_CLASS = "StatsLogInternal";
30
31const int JAVA_MODULE_REQUIRES_FLOAT = 0x01;
32const int JAVA_MODULE_REQUIRES_ATTRIBUTION = 0x02;
Tej Singh810eeb32019-03-07 19:08:52 -080033
Stefan Lafonae2df012017-11-14 09:17:21 -080034using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000035
Yao Chend54f9dd2017-10-17 17:37:48 +000036/**
37 * Turn lower and camel case into upper case with underscores.
38 */
39static string
40make_constant_name(const string& str)
41{
42 string result;
43 const int N = str.size();
44 bool underscore_next = false;
45 for (int i=0; i<N; i++) {
46 char c = str[i];
47 if (c >= 'A' && c <= 'Z') {
48 if (underscore_next) {
49 result += '_';
50 underscore_next = false;
51 }
52 } else if (c >= 'a' && c <= 'z') {
53 c = 'A' + c - 'a';
54 underscore_next = true;
55 } else if (c == '_') {
56 underscore_next = false;
57 }
58 result += c;
59 }
60 return result;
61}
62
63static const char*
64cpp_type_name(java_type_t type)
65{
66 switch (type) {
67 case JAVA_TYPE_BOOLEAN:
68 return "bool";
69 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070070 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000071 return "int32_t";
72 case JAVA_TYPE_LONG:
73 return "int64_t";
74 case JAVA_TYPE_FLOAT:
75 return "float";
76 case JAVA_TYPE_DOUBLE:
77 return "double";
78 case JAVA_TYPE_STRING:
79 return "char const*";
Yao Chenbbdd67d2018-10-24 12:15:56 -070080 case JAVA_TYPE_BYTE_ARRAY:
Yao Chene89572c2019-01-09 15:41:50 -080081 return "const BytesField&";
Yao Chend54f9dd2017-10-17 17:37:48 +000082 default:
83 return "UNKNOWN";
84 }
85}
86
87static const char*
88java_type_name(java_type_t type)
89{
90 switch (type) {
91 case JAVA_TYPE_BOOLEAN:
92 return "boolean";
93 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070094 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000095 return "int";
96 case JAVA_TYPE_LONG:
97 return "long";
98 case JAVA_TYPE_FLOAT:
99 return "float";
100 case JAVA_TYPE_DOUBLE:
101 return "double";
102 case JAVA_TYPE_STRING:
103 return "java.lang.String";
Yao Chenbbdd67d2018-10-24 12:15:56 -0700104 case JAVA_TYPE_BYTE_ARRAY:
105 return "byte[]";
Yao Chend54f9dd2017-10-17 17:37:48 +0000106 default:
107 return "UNKNOWN";
108 }
109}
110
Tej Singh810eeb32019-03-07 19:08:52 -0800111static bool atom_needed_for_module(const AtomDecl& atomDecl, const string& moduleName) {
112 if (moduleName == DEFAULT_MODULE_NAME) {
113 return true;
114 }
115 return atomDecl.hasModule && (moduleName == atomDecl.moduleName);
116}
Yao Chend54f9dd2017-10-17 17:37:48 +0000117
Tej Singh810eeb32019-03-07 19:08:52 -0800118static bool signature_needed_for_module(const set<string>& modules, const string& moduleName) {
119 if (moduleName == DEFAULT_MODULE_NAME) {
120 return true;
121 }
122 return modules.find(moduleName) != modules.end();
123}
Yao Chend54f9dd2017-10-17 17:37:48 +0000124
Tej Singh810eeb32019-03-07 19:08:52 -0800125static void write_atoms_info_cpp(FILE *out, const Atoms &atoms) {
Yao Chenc40a19d2018-03-15 16:48:25 -0700126 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
Tej Singh810eeb32019-03-07 19:08:52 -0800127 "audio_state_changed",
128 "call_state_changed",
129 "phone_signal_strength_changed",
130 "mobile_bytes_transfer_by_fg_bg",
131 "mobile_bytes_transfer"};
Yao Chenc40a19d2018-03-15 16:48:25 -0700132 fprintf(out,
133 "const std::set<int> "
134 "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n");
135 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
136 atom != atoms.decls.end(); atom++) {
137 if (kTruncatingAtomNames.find(atom->name) ==
138 kTruncatingAtomNames.end()) {
139 string constant = make_constant_name(atom->name);
140 fprintf(out, " %s,\n", constant.c_str());
141 }
142 }
143 fprintf(out, "};\n");
144 fprintf(out, "\n");
145
146 fprintf(out,
147 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
148 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
149 atom != atoms.decls.end(); atom++) {
150 for (vector<AtomField>::const_iterator field = atom->fields.begin();
151 field != atom->fields.end(); field++) {
152 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
153 string constant = make_constant_name(atom->name);
154 fprintf(out, " %s,\n", constant.c_str());
155 break;
156 }
157 }
158 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000159
160 fprintf(out, "};\n");
161 fprintf(out, "\n");
162
163 fprintf(out,
164 "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
165 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
166 atom != atoms.decls.end(); atom++) {
167 if (atom->whitelisted) {
168 string constant = make_constant_name(atom->name);
169 fprintf(out, " %s,\n", constant.c_str());
170 }
171 }
172
Yao Chenc40a19d2018-03-15 16:48:25 -0700173 fprintf(out, "};\n");
174 fprintf(out, "\n");
175
Yangster-macca5c0862018-04-09 22:39:53 -0700176 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700177 fprintf(out, " std::map<int, int> uidField;\n");
178 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
179 atom != atoms.decls.end(); atom++) {
180 if (atom->uidField == 0) {
181 continue;
182 }
183 fprintf(out,
184 "\n // Adding uid field for atom "
185 "(%d)%s\n",
186 atom->code, atom->name.c_str());
187 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
188 make_constant_name(atom->name).c_str(), atom->uidField);
189 }
190
191 fprintf(out, " return uidField;\n");
192 fprintf(out, "};\n");
193
194 fprintf(out,
195 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
196 "getAtomUidField();\n");
197
198 fprintf(out,
199 "static std::map<int, StateAtomFieldOptions> "
200 "getStateAtomFieldOptions() {\n");
201 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
202 fprintf(out, " StateAtomFieldOptions opt;\n");
203 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
204 atom != atoms.decls.end(); atom++) {
205 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
206 continue;
207 }
208 fprintf(out,
209 "\n // Adding primary and exclusive fields for atom "
210 "(%d)%s\n",
211 atom->code, atom->name.c_str());
212 fprintf(out, " opt.primaryFields.clear();\n");
213 for (const auto& field : atom->primaryFields) {
214 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
215 }
216
217 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
218 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
219 make_constant_name(atom->name).c_str());
220 }
221
222 fprintf(out, " return options;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700223 fprintf(out, "}\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700224
225 fprintf(out,
226 "const std::map<int, StateAtomFieldOptions> "
227 "AtomsInfo::kStateAtomsFieldOptions = "
228 "getStateAtomFieldOptions();\n");
229
Yao Chenbbdd67d2018-10-24 12:15:56 -0700230 fprintf(out,
231 "static std::map<int, std::vector<int>> "
232 "getBinaryFieldAtoms() {\n");
233 fprintf(out, " std::map<int, std::vector<int>> options;\n");
234 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
235 atom != atoms.decls.end(); atom++) {
236 if (atom->binaryFields.size() == 0) {
237 continue;
238 }
239 fprintf(out,
240 "\n // Adding binary fields for atom "
241 "(%d)%s\n",
242 atom->code, atom->name.c_str());
243
244 for (const auto& field : atom->binaryFields) {
245 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
246 make_constant_name(atom->name).c_str(), field);
247 }
248 }
249
250 fprintf(out, " return options;\n");
251 fprintf(out, "}\n");
252
253 fprintf(out,
254 "const std::map<int, std::vector<int>> "
255 "AtomsInfo::kBytesFieldAtoms = "
256 "getBinaryFieldAtoms();\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800257}
258
259// Writes namespaces for the cpp and header files, returning the number of namespaces written.
260void write_namespace(FILE* out, const string& cppNamespaces) {
261 vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
262 for (string cppNamespace : cppNamespaceVec) {
263 fprintf(out, "namespace %s {\n", cppNamespace.c_str());
264 }
265}
266
267// Writes namespace closing brackets for cpp and header files.
268void write_closing_namespace(FILE* out, const string& cppNamespaces) {
269 vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
270 for (auto it = cppNamespaceVec.rbegin(); it != cppNamespaceVec.rend(); ++it) {
271 fprintf(out, "} // namespace %s\n", it->c_str());
272 }
273}
274
275static int write_stats_log_cpp(FILE *out, const Atoms &atoms, const AtomDecl &attributionDecl,
276 const string& moduleName, const string& cppNamespace,
277 const string& importHeader) {
278 // Print prelude
279 fprintf(out, "// This file is autogenerated\n");
280 fprintf(out, "\n");
281
282 fprintf(out, "#include <mutex>\n");
283 fprintf(out, "#include <chrono>\n");
284 fprintf(out, "#include <thread>\n");
285 fprintf(out, "#ifdef __ANDROID__\n");
286 fprintf(out, "#include <cutils/properties.h>\n");
287 fprintf(out, "#endif\n");
288 fprintf(out, "#include <stats_event_list.h>\n");
289 fprintf(out, "#include <log/log.h>\n");
290 fprintf(out, "#include <%s>\n", importHeader.c_str());
291 fprintf(out, "#include <utils/SystemClock.h>\n");
292 fprintf(out, "\n");
293
294 write_namespace(out, cppNamespace);
295 fprintf(out, "// the single event tag id for all stats logs\n");
296 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
297 fprintf(out, "#ifdef __ANDROID__\n");
298 fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
299 fprintf(out, "#else\n");
300 fprintf(out, "const static bool kStatsdEnabled = false;\n");
301 fprintf(out, "#endif\n");
302
303 // AtomsInfo is only used by statsd internally and is not needed for other modules.
304 if (moduleName == DEFAULT_MODULE_NAME) {
305 write_atoms_info_cpp(out, atoms);
306 }
Yangster-macca5c0862018-04-09 22:39:53 -0700307
308 fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
309 fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
310 fprintf(out, "static std::mutex mLogdRetryMutex;\n");
311
Yao Chend54f9dd2017-10-17 17:37:48 +0000312 // Print write methods
313 fprintf(out, "\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800314 for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
315 signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
316 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
317 continue;
318 }
319 vector<java_type_t> signature = signature_to_modules_it->first;
Yao Chend54f9dd2017-10-17 17:37:48 +0000320 int argIndex;
321
Yao Chen97e21ec2018-03-29 11:00:38 -0700322 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700323 fprintf(out, "try_stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000324 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800325 for (vector<java_type_t>::const_iterator arg = signature.begin();
326 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800327 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
328 for (auto chainField : attributionDecl.fields) {
329 if (chainField.javaType == JAVA_TYPE_STRING) {
330 fprintf(out, ", const std::vector<%s>& %s",
331 cpp_type_name(chainField.javaType),
332 chainField.name.c_str());
333 } else {
334 fprintf(out, ", const %s* %s, size_t %s_length",
335 cpp_type_name(chainField.javaType),
336 chainField.name.c_str(), chainField.name.c_str());
337 }
338 }
Yangster-mace124e422018-08-16 10:30:28 -0700339 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700340 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
341 "const std::map<int, int64_t>& arg%d_2, "
342 "const std::map<int, char const*>& arg%d_3, "
343 "const std::map<int, float>& arg%d_4",
344 argIndex, argIndex, argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800345 } else {
346 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
347 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000348 argIndex++;
349 }
350 fprintf(out, ")\n");
351
352 fprintf(out, "{\n");
353 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700354 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700355 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800356 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800357 fprintf(out, " event << code;\n\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800358 for (vector<java_type_t>::const_iterator arg = signature.begin();
359 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800360 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
361 for (const auto &chainField : attributionDecl.fields) {
362 if (chainField.javaType == JAVA_TYPE_STRING) {
363 fprintf(out, " if (%s_length != %s.size()) {\n",
364 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700365 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800366 fprintf(out, " }\n");
367 }
368 }
369 fprintf(out, "\n event.begin();\n");
370 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
371 attributionDecl.fields.front().name.c_str());
372 fprintf(out, " event.begin();\n");
373 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800374 if (chainField.javaType == JAVA_TYPE_STRING) {
375 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
376 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
377 fprintf(out, " } else {\n");
378 fprintf(out, " event << \"\";\n");
379 fprintf(out, " }\n");
380 } else {
381 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
382 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800383 }
384 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000385 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800386 fprintf(out, " event.end();\n\n");
Yangster-mace124e422018-08-16 10:30:28 -0700387 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
388 fprintf(out, " event.begin();\n\n");
389 fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
390 fprintf(out, " event.begin();\n");
391 fprintf(out, " event << it.first;\n");
392 fprintf(out, " event << it.second;\n");
393 fprintf(out, " event.end();\n");
394 fprintf(out, " }\n");
395
396 fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
397 fprintf(out, " event.begin();\n");
398 fprintf(out, " event << it.first;\n");
399 fprintf(out, " event << it.second;\n");
400 fprintf(out, " event.end();\n");
401 fprintf(out, " }\n");
402
403 fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
404 fprintf(out, " event.begin();\n");
405 fprintf(out, " event << it.first;\n");
406 fprintf(out, " event << it.second;\n");
407 fprintf(out, " event.end();\n");
408 fprintf(out, " }\n");
409
Howard Ro4078dd42018-09-27 17:41:08 -0700410 fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
411 fprintf(out, " event.begin();\n");
412 fprintf(out, " event << it.first;\n");
413 fprintf(out, " event << it.second;\n");
414 fprintf(out, " event.end();\n");
415 fprintf(out, " }\n");
416
Yangster-mace124e422018-08-16 10:30:28 -0700417 fprintf(out, " event.end();\n\n");
Yao Chen1fe9f592018-12-06 10:34:25 -0800418 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
419 fprintf(out,
Yao Chene89572c2019-01-09 15:41:50 -0800420 " event.AppendCharArray(arg%d.arg, "
421 "arg%d.arg_length);\n",
Yao Chen1fe9f592018-12-06 10:34:25 -0800422 argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800423 } else {
424 if (*arg == JAVA_TYPE_STRING) {
425 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
426 fprintf(out, " arg%d = \"\";\n", argIndex);
427 fprintf(out, " }\n");
428 }
429 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000430 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000431 argIndex++;
432 }
433
Yao Chen97e21ec2018-03-29 11:00:38 -0700434 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700435 fprintf(out, " } else {\n");
436 fprintf(out, " return 1;\n");
437 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000438 fprintf(out, "}\n");
439 fprintf(out, "\n");
440 }
441
Tej Singh810eeb32019-03-07 19:08:52 -0800442 for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
443 signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
444 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
445 continue;
446 }
447 vector<java_type_t> signature = signature_to_modules_it->first;
Yangster-macb8382a12018-04-04 10:39:12 -0700448 int argIndex;
449
Yangster-mace124e422018-08-16 10:30:28 -0700450 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700451 fprintf(out, "stats_write(int32_t code");
452 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800453 for (vector<java_type_t>::const_iterator arg = signature.begin();
454 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700455 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
456 for (auto chainField : attributionDecl.fields) {
457 if (chainField.javaType == JAVA_TYPE_STRING) {
458 fprintf(out, ", const std::vector<%s>& %s",
459 cpp_type_name(chainField.javaType),
460 chainField.name.c_str());
461 } else {
462 fprintf(out, ", const %s* %s, size_t %s_length",
463 cpp_type_name(chainField.javaType),
464 chainField.name.c_str(), chainField.name.c_str());
465 }
466 }
Yangster-mace124e422018-08-16 10:30:28 -0700467 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Yao Chen1fe9f592018-12-06 10:34:25 -0800468 fprintf(out,
469 ", const std::map<int, int32_t>& arg%d_1, "
470 "const std::map<int, int64_t>& arg%d_2, "
471 "const std::map<int, char const*>& arg%d_3, "
472 "const std::map<int, float>& arg%d_4",
473 argIndex, argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700474 } else {
475 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
476 }
477 argIndex++;
478 }
479 fprintf(out, ")\n");
480
481 fprintf(out, "{\n");
482 fprintf(out, " int ret = 0;\n");
483
Yangster-macca5c0862018-04-09 22:39:53 -0700484 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700485 fprintf(out, " ret = try_stats_write(code");
486
487 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800488 for (vector<java_type_t>::const_iterator arg = signature.begin();
489 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700490 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
491 for (auto chainField : attributionDecl.fields) {
492 if (chainField.javaType == JAVA_TYPE_STRING) {
493 fprintf(out, ", %s",
494 chainField.name.c_str());
495 } else {
496 fprintf(out, ", %s, %s_length",
497 chainField.name.c_str(), chainField.name.c_str());
498 }
499 }
Yao Chen1fe9f592018-12-06 10:34:25 -0800500 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
501 fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex,
502 argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700503 } else {
504 fprintf(out, ", arg%d", argIndex);
505 }
506 argIndex++;
507 }
508 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700509 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700510
511 fprintf(out, " {\n");
512 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
513 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
514 "kMinRetryIntervalNs) break;\n");
515 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
516 fprintf(out, " }\n");
517 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700518 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700519 fprintf(out, " if (ret < 0) {\n");
Yao Chen49d7dd72019-03-26 14:02:11 -0700520 fprintf(out, " note_log_drop(ret, code);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700521 fprintf(out, " }\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700522 fprintf(out, " return ret;\n");
523 fprintf(out, "}\n");
524 fprintf(out, "\n");
525 }
526
Tej Singh810eeb32019-03-07 19:08:52 -0800527 for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
528 signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
529 if (!signature_needed_for_module(signature_it->second, moduleName)) {
530 continue;
531 }
532 vector<java_type_t> signature = signature_it->first;
Yangster-macba5b9e42018-01-10 21:31:59 -0800533 int argIndex;
534
Yao Chen97e21ec2018-03-29 11:00:38 -0700535 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700536 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800537 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800538 for (vector<java_type_t>::const_iterator arg = signature.begin();
539 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800540 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
541 argIndex++;
542 }
543 fprintf(out, ")\n");
544
545 fprintf(out, "{\n");
546 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700547 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700548 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800549 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800550 fprintf(out, " event << code;\n\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800551 for (vector<java_type_t>::const_iterator arg = signature.begin();
552 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800553 if (argIndex == 1) {
554 fprintf(out, " event.begin();\n\n");
555 fprintf(out, " event.begin();\n");
556 }
557 if (*arg == JAVA_TYPE_STRING) {
558 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
559 fprintf(out, " arg%d = \"\";\n", argIndex);
560 fprintf(out, " }\n");
561 }
Yao Chene89572c2019-01-09 15:41:50 -0800562 if (*arg == JAVA_TYPE_BYTE_ARRAY) {
563 fprintf(out,
564 " event.AppendCharArray(arg%d.arg, "
565 "arg%d.arg_length);",
566 argIndex, argIndex);
567 } else {
568 fprintf(out, " event << arg%d;\n", argIndex);
569 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800570 if (argIndex == 2) {
571 fprintf(out, " event.end();\n\n");
572 fprintf(out, " event.end();\n\n");
573 }
574 argIndex++;
575 }
576
Yao Chen97e21ec2018-03-29 11:00:38 -0700577 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700578 fprintf(out, " } else {\n");
579 fprintf(out, " return 1;\n");
580 fprintf(out, " }\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800581 fprintf(out, "}\n");
582 fprintf(out, "\n");
583 }
Yangster-macb8382a12018-04-04 10:39:12 -0700584
Tej Singh810eeb32019-03-07 19:08:52 -0800585 for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
586 signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
587 if (!signature_needed_for_module(signature_it->second, moduleName)) {
588 continue;
589 }
590 vector<java_type_t> signature = signature_it->first;
Yangster-macb8382a12018-04-04 10:39:12 -0700591 int argIndex;
592
593 fprintf(out, "int\n");
594 fprintf(out, "stats_write_non_chained(int32_t code");
595 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800596 for (vector<java_type_t>::const_iterator arg = signature.begin();
597 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700598 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
599 argIndex++;
600 }
601 fprintf(out, ")\n");
602
603 fprintf(out, "{\n");
604
605 fprintf(out, " int ret = 0;\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700606 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700607 fprintf(out, " ret = try_stats_write_non_chained(code");
608
609 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800610 for (vector<java_type_t>::const_iterator arg = signature.begin();
611 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700612 fprintf(out, ", arg%d", argIndex);
613 argIndex++;
614 }
615 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700616 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700617
618 fprintf(out, " {\n");
619 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
620 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
621 "kMinRetryIntervalNs) break;\n");
622 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
623 fprintf(out, " }\n");
624
625 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700626 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700627 fprintf(out, " if (ret < 0) {\n");
Yao Chen49d7dd72019-03-26 14:02:11 -0700628 fprintf(out, " note_log_drop(ret, code);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700629 fprintf(out, " }\n");
630 fprintf(out, " return ret;\n\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700631 fprintf(out, "}\n");
632
633 fprintf(out, "\n");
634 }
635
636
Yao Chend54f9dd2017-10-17 17:37:48 +0000637 // Print footer
638 fprintf(out, "\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800639 write_closing_namespace(out, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000640
641 return 0;
642}
643
Yangster-macba5b9e42018-01-10 21:31:59 -0800644void build_non_chained_decl_map(const Atoms& atoms,
645 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
646 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
647 atom != atoms.non_chained_decls.end(); atom++) {
648 decl_map->insert(std::make_pair(atom->code, atom));
649 }
650}
651
652static void write_cpp_usage(
653 FILE* out, const string& method_name, const string& atom_code_name,
654 const AtomDecl& atom, const AtomDecl &attributionDecl) {
Yao Chene89572c2019-01-09 15:41:50 -0800655 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
656 atom_code_name.c_str());
657
Yangster-macba5b9e42018-01-10 21:31:59 -0800658 for (vector<AtomField>::const_iterator field = atom.fields.begin();
659 field != atom.fields.end(); field++) {
660 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
661 for (auto chainField : attributionDecl.fields) {
662 if (chainField.javaType == JAVA_TYPE_STRING) {
663 fprintf(out, ", const std::vector<%s>& %s",
664 cpp_type_name(chainField.javaType),
665 chainField.name.c_str());
666 } else {
667 fprintf(out, ", const %s* %s, size_t %s_length",
668 cpp_type_name(chainField.javaType),
669 chainField.name.c_str(), chainField.name.c_str());
670 }
671 }
Yangster-mace124e422018-08-16 10:30:28 -0700672 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700673 fprintf(out, ", const std::map<int, int32_t>& %s_int"
674 ", const std::map<int, int64_t>& %s_long"
Yangster-mace124e422018-08-16 10:30:28 -0700675 ", const std::map<int, char const*>& %s_str"
676 ", const std::map<int, float>& %s_float",
Howard Ro4078dd42018-09-27 17:41:08 -0700677 field->name.c_str(),
678 field->name.c_str(),
679 field->name.c_str(),
680 field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800681 } else {
682 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
683 }
684 }
685 fprintf(out, ");\n");
686}
687
688static void write_cpp_method_header(
Tej Singh810eeb32019-03-07 19:08:52 -0800689 FILE* out,
690 const string& method_name,
691 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
692 const AtomDecl &attributionDecl, const string& moduleName) {
693
694 for (auto signature_to_modules_it = signatures_to_modules.begin();
695 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
696 // Skip if this signature is not needed for the module.
697 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
698 continue;
699 }
700
701 vector<java_type_t> signature = signature_to_modules_it->first;
Yangster-mace124e422018-08-16 10:30:28 -0700702 fprintf(out, "int %s(int32_t code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800703 int argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800704 for (vector<java_type_t>::const_iterator arg = signature.begin();
705 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800706 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
707 for (auto chainField : attributionDecl.fields) {
708 if (chainField.javaType == JAVA_TYPE_STRING) {
709 fprintf(out, ", const std::vector<%s>& %s",
710 cpp_type_name(chainField.javaType), chainField.name.c_str());
711 } else {
712 fprintf(out, ", const %s* %s, size_t %s_length",
713 cpp_type_name(chainField.javaType),
714 chainField.name.c_str(), chainField.name.c_str());
715 }
716 }
Yangster-mace124e422018-08-16 10:30:28 -0700717 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700718 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
719 "const std::map<int, int64_t>& arg%d_2, "
720 "const std::map<int, char const*>& arg%d_3, "
721 "const std::map<int, float>& arg%d_4",
722 argIndex, argIndex, argIndex, argIndex);
Yangster-macba5b9e42018-01-10 21:31:59 -0800723 } else {
724 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
725 }
726 argIndex++;
727 }
728 fprintf(out, ");\n");
729
730 }
731}
Yao Chend54f9dd2017-10-17 17:37:48 +0000732
733static int
Tej Singh810eeb32019-03-07 19:08:52 -0800734write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
735 const string& moduleName, const string& cppNamespace)
Yao Chend54f9dd2017-10-17 17:37:48 +0000736{
Yao Chend54f9dd2017-10-17 17:37:48 +0000737 // Print prelude
738 fprintf(out, "// This file is autogenerated\n");
739 fprintf(out, "\n");
740 fprintf(out, "#pragma once\n");
741 fprintf(out, "\n");
742 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800743 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800744 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800745 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000746 fprintf(out, "\n");
747
Tej Singh810eeb32019-03-07 19:08:52 -0800748 write_namespace(out, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000749 fprintf(out, "\n");
750 fprintf(out, "/*\n");
751 fprintf(out, " * API For logging statistics events.\n");
752 fprintf(out, " */\n");
753 fprintf(out, "\n");
754 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700755 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000756 fprintf(out, " */\n");
757 fprintf(out, "enum {\n");
758
Yangster-macba5b9e42018-01-10 21:31:59 -0800759 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
760 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
761
Yao Chend54f9dd2017-10-17 17:37:48 +0000762 size_t i = 0;
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800763 // Print atom constants
Yao Chend54f9dd2017-10-17 17:37:48 +0000764 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800765 atom != atoms.decls.end(); atom++) {
Tej Singh810eeb32019-03-07 19:08:52 -0800766 // Skip if the atom is not needed for the module.
767 if (!atom_needed_for_module(*atom, moduleName)) {
768 continue;
769 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000770 string constant = make_constant_name(atom->name);
771 fprintf(out, "\n");
772 fprintf(out, " /**\n");
773 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800774 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
775
776 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
777 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
778 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
779 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000780 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000781 fprintf(out, " */\n");
782 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
783 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800784 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
785 maxPushedAtomId = atom->code;
786 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000787 i++;
788 }
789 fprintf(out, "\n");
790 fprintf(out, "};\n");
791 fprintf(out, "\n");
792
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800793 // Print constants for the enum values.
794 fprintf(out, "//\n");
795 fprintf(out, "// Constants for enum values\n");
796 fprintf(out, "//\n\n");
797 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
798 atom != atoms.decls.end(); atom++) {
Tej Singh810eeb32019-03-07 19:08:52 -0800799 // Skip if the atom is not needed for the module.
800 if (!atom_needed_for_module(*atom, moduleName)) {
801 continue;
802 }
803
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800804 for (vector<AtomField>::const_iterator field = atom->fields.begin();
805 field != atom->fields.end(); field++) {
806 if (field->javaType == JAVA_TYPE_ENUM) {
807 fprintf(out, "// Values for %s.%s\n", atom->message.c_str(),
808 field->name.c_str());
809 for (map<int, string>::const_iterator value = field->enumValues.begin();
810 value != field->enumValues.end(); value++) {
811 fprintf(out, "const int32_t %s__%s__%s = %d;\n",
812 make_constant_name(atom->message).c_str(),
813 make_constant_name(field->name).c_str(),
814 make_constant_name(value->second).c_str(),
815 value->first);
816 }
817 fprintf(out, "\n");
818 }
819 }
820 }
821
Yao Chene89572c2019-01-09 15:41:50 -0800822 fprintf(out, "struct BytesField {\n");
823 fprintf(out,
824 " BytesField(char const* array, size_t len) : arg(array), "
825 "arg_length(len) {}\n");
826 fprintf(out, " char const* arg;\n");
827 fprintf(out, " size_t arg_length;\n");
828 fprintf(out, "};\n");
829 fprintf(out, "\n");
830
Tej Singh810eeb32019-03-07 19:08:52 -0800831 // This metadata is only used by statsd, which uses the default libstatslog.
832 if (moduleName == DEFAULT_MODULE_NAME) {
Yao Chenc40a19d2018-03-15 16:48:25 -0700833
Tej Singh810eeb32019-03-07 19:08:52 -0800834 fprintf(out, "struct StateAtomFieldOptions {\n");
835 fprintf(out, " std::vector<int> primaryFields;\n");
836 fprintf(out, " int exclusiveField;\n");
837 fprintf(out, "};\n");
838 fprintf(out, "\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800839
Tej Singh810eeb32019-03-07 19:08:52 -0800840 fprintf(out, "struct AtomsInfo {\n");
841 fprintf(out,
842 " const static std::set<int> "
843 "kNotTruncatingTimestampAtomWhiteList;\n");
844 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
845 fprintf(out,
846 " const static std::set<int> kAtomsWithAttributionChain;\n");
847 fprintf(out,
848 " const static std::map<int, StateAtomFieldOptions> "
849 "kStateAtomsFieldOptions;\n");
850 fprintf(out,
851 " const static std::map<int, std::vector<int>> "
852 "kBytesFieldAtoms;");
853 fprintf(out,
854 " const static std::set<int> kWhitelistedAtoms;\n");
855 fprintf(out, "};\n");
856
857 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
858 maxPushedAtomId);
859 }
Yao Chen9c1debe2018-02-19 14:39:19 -0800860
Yao Chend54f9dd2017-10-17 17:37:48 +0000861 // Print write methods
862 fprintf(out, "//\n");
863 fprintf(out, "// Write methods\n");
864 fprintf(out, "//\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800865 write_cpp_method_header(out, "stats_write", atoms.signatures_to_modules, attributionDecl,
866 moduleName);
Yangster-macba5b9e42018-01-10 21:31:59 -0800867
868 fprintf(out, "//\n");
869 fprintf(out, "// Write flattened methods\n");
870 fprintf(out, "//\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800871 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures_to_modules,
872 attributionDecl, moduleName);
Yao Chend54f9dd2017-10-17 17:37:48 +0000873
874 fprintf(out, "\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800875 write_closing_namespace(out, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000876
877 return 0;
878}
879
Bookatz0bd97202018-06-05 12:42:37 -0700880static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
881 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800882 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
883 method_name.c_str(), atom_code_name.c_str());
884 for (vector<AtomField>::const_iterator field = atom.fields.begin();
885 field != atom.fields.end(); field++) {
886 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700887 fprintf(out, ", android.os.WorkSource workSource");
Yangster-mace124e422018-08-16 10:30:28 -0700888 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
889 fprintf(out, ", SparseArray<Object> value_map");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700890 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
891 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800892 } else {
893 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
894 }
895 }
Bookatz0bd97202018-06-05 12:42:37 -0700896 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800897}
898
899static void write_java_method(
Tej Singh810eeb32019-03-07 19:08:52 -0800900 FILE* out,
901 const string& method_name,
902 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
903 const AtomDecl &attributionDecl) {
904
905 for (auto signature_to_modules_it = signatures_to_modules.begin();
906 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
907 vector<java_type_t> signature = signature_to_modules_it->first;
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800908 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700909 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800910 int argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800911 for (vector<java_type_t>::const_iterator arg = signature.begin();
912 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800913 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
914 for (auto chainField : attributionDecl.fields) {
915 fprintf(out, ", %s[] %s",
916 java_type_name(chainField.javaType), chainField.name.c_str());
917 }
Yangster-mace124e422018-08-16 10:30:28 -0700918 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
919 fprintf(out, ", SparseArray<Object> value_map");
Yangster-macba5b9e42018-01-10 21:31:59 -0800920 } else {
921 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
922 }
923 argIndex++;
924 }
925 fprintf(out, ");\n");
926 }
927}
928
Tej Singh2910d5a2019-03-23 17:26:32 -0700929static void write_java_helpers_for_module(
930 FILE * out,
931 const AtomDecl &attributionDecl,
932 const int requiredHelpers) {
933 fprintf(out, " private static void copyInt(byte[] buff, int pos, int val) {\n");
934 fprintf(out, " buff[pos] = (byte) (val);\n");
935 fprintf(out, " buff[pos + 1] = (byte) (val >> 8);\n");
936 fprintf(out, " buff[pos + 2] = (byte) (val >> 16);\n");
937 fprintf(out, " buff[pos + 3] = (byte) (val >> 24);\n");
938 fprintf(out, " return;\n");
939 fprintf(out, " }\n");
940 fprintf(out, "\n");
941
942 fprintf(out, " private static void copyLong(byte[] buff, int pos, long val) {\n");
943 fprintf(out, " buff[pos] = (byte) (val);\n");
944 fprintf(out, " buff[pos + 1] = (byte) (val >> 8);\n");
945 fprintf(out, " buff[pos + 2] = (byte) (val >> 16);\n");
946 fprintf(out, " buff[pos + 3] = (byte) (val >> 24);\n");
947 fprintf(out, " buff[pos + 4] = (byte) (val >> 32);\n");
948 fprintf(out, " buff[pos + 5] = (byte) (val >> 40);\n");
949 fprintf(out, " buff[pos + 6] = (byte) (val >> 48);\n");
950 fprintf(out, " buff[pos + 7] = (byte) (val >> 56);\n");
951 fprintf(out, " return;\n");
952 fprintf(out, " }\n");
953 fprintf(out, "\n");
954
955 if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) {
956 fprintf(out, " private static void copyFloat(byte[] buff, int pos, float val) {\n");
957 fprintf(out, " copyInt(buff, pos, Float.floatToIntBits(val));\n");
958 fprintf(out, " return;\n");
959 fprintf(out, " }\n");
960 fprintf(out, "\n");
961 }
962
963 if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) {
964 fprintf(out, " private static void writeAttributionChain(byte[] buff, int pos");
965 for (auto chainField : attributionDecl.fields) {
966 fprintf(out, ", %s[] %s",
967 java_type_name(chainField.javaType), chainField.name.c_str());
968 }
969 fprintf(out, ") {\n");
970
971 const char* uidName = attributionDecl.fields.front().name.c_str();
972 const char* tagName = attributionDecl.fields.back().name.c_str();
973
974 // Write the first list begin.
975 fprintf(out, " buff[pos] = LIST_TYPE;\n");
976 fprintf(out, " buff[pos + 1] = (byte) (%s.length);\n", tagName);
977 fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n");
978
979 // Iterate through the attribution chain and write the nodes.
980 fprintf(out, " for (int i = 0; i < %s.length; i++) {\n", tagName);
981 // Write the list begin.
982 fprintf(out, " buff[pos] = LIST_TYPE;\n");
983 fprintf(out, " buff[pos + 1] = %lu;\n", attributionDecl.fields.size());
984 fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n");
985
986 // Write the uid.
987 fprintf(out, " buff[pos] = INT_TYPE;\n");
988 fprintf(out, " copyInt(buff, pos + 1, %s[i]);\n", uidName);
989 fprintf(out, " pos += INT_TYPE_SIZE;\n");
990
991 // Write the tag.
992 fprintf(out, " String %sStr = (%s[i] == null) ? \"\" : %s[i];\n",
993 tagName, tagName, tagName);
994 fprintf(out, " byte[] %sByte = %sStr.getBytes(UTF_8);\n", tagName, tagName);
995 fprintf(out, " buff[pos] = STRING_TYPE;\n");
996 fprintf(out, " copyInt(buff, pos + 1, %sByte.length);\n", tagName);
997 fprintf(out, " System.arraycopy("
998 "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n",
999 tagName, tagName);
1000 fprintf(out, " pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", tagName);
1001 fprintf(out, " }\n");
1002 fprintf(out, " }\n");
1003 fprintf(out, "\n");
1004 }
1005}
1006
1007
1008static int write_java_non_chained_method_for_module(
1009 FILE* out,
1010 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1011 const string& moduleName
1012 ) {
1013 for (auto signature_to_modules_it = signatures_to_modules.begin();
1014 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1015 // Skip if this signature is not needed for the module.
1016 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
1017 continue;
1018 }
1019
1020 // Print method signature.
1021 vector<java_type_t> signature = signature_to_modules_it->first;
1022 fprintf(out, " public static void write_non_chained(int code");
1023 int argIndex = 1;
1024 for (vector<java_type_t>::const_iterator arg = signature.begin();
1025 arg != signature.end(); arg++) {
1026 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1027 // Non chained signatures should not have attribution chains.
1028 return 1;
1029 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1030 // Module logging does not yet support key value pair.
1031 return 1;
1032 } else {
1033 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
1034 }
1035 argIndex++;
1036 }
1037 fprintf(out, ") {\n");
1038
1039 fprintf(out, " write(code");
1040 argIndex = 1;
1041 for (vector<java_type_t>::const_iterator arg = signature.begin();
1042 arg != signature.end(); arg++) {
1043 // First two args are uid and tag of attribution chain.
1044 if (argIndex == 1) {
1045 fprintf(out, ", new int[] {arg%d}", argIndex);
1046 } else if (argIndex == 2) {
1047 fprintf(out, ", new java.lang.String[] {arg%d}", argIndex);
1048 } else {
1049 fprintf(out, ", arg%d", argIndex);
1050 }
1051 argIndex++;
1052 }
1053 fprintf(out, ");\n");
1054 fprintf(out, " }\n");
1055 fprintf(out, "\n");
1056 }
1057 return 0;
1058}
1059
1060static int write_java_method_for_module(
1061 FILE* out,
1062 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1063 const AtomDecl &attributionDecl,
1064 const string& moduleName,
1065 int* requiredHelpers
1066 ) {
1067
1068 for (auto signature_to_modules_it = signatures_to_modules.begin();
1069 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1070 // Skip if this signature is not needed for the module.
1071 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
1072 continue;
1073 }
1074
1075 // Print method signature.
1076 vector<java_type_t> signature = signature_to_modules_it->first;
1077 fprintf(out, " public static void write(int code");
1078 int argIndex = 1;
1079 for (vector<java_type_t>::const_iterator arg = signature.begin();
1080 arg != signature.end(); arg++) {
1081 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1082 for (auto chainField : attributionDecl.fields) {
1083 fprintf(out, ", %s[] %s",
1084 java_type_name(chainField.javaType), chainField.name.c_str());
1085 }
1086 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1087 // Module logging does not yet support key value pair.
1088 return 1;
1089 } else {
1090 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
1091 }
1092 argIndex++;
1093 }
1094 fprintf(out, ") {\n");
1095
1096 // Calculate the size of the buffer.
1097 fprintf(out, " // Initial overhead of the list, timestamp, and atom tag.\n");
1098 fprintf(out, " int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + INT_TYPE_SIZE;\n");
1099 argIndex = 1;
1100 for (vector<java_type_t>::const_iterator arg = signature.begin();
1101 arg != signature.end(); arg++) {
1102 switch (*arg) {
1103 case JAVA_TYPE_BOOLEAN:
1104 case JAVA_TYPE_INT:
1105 case JAVA_TYPE_FLOAT:
1106 case JAVA_TYPE_ENUM:
1107 fprintf(out, " needed += INT_TYPE_SIZE;\n");
1108 break;
1109 case JAVA_TYPE_LONG:
1110 // Longs take 9 bytes, 1 for the type and 8 for the value.
1111 fprintf(out, " needed += LONG_TYPE_SIZE;\n");
1112 break;
1113 case JAVA_TYPE_STRING:
1114 // Strings take 5 metadata bytes + length of byte encoded string.
1115 fprintf(out, " if (arg%d == null) {\n", argIndex);
1116 fprintf(out, " arg%d = \"\";\n", argIndex);
1117 fprintf(out, " }\n");
1118 fprintf(out, " byte[] arg%dBytes= arg%d.getBytes(UTF_8);\n",
1119 argIndex, argIndex);
1120 fprintf(out, " needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
1121 argIndex);
1122 break;
1123 case JAVA_TYPE_BYTE_ARRAY:
1124 // Byte arrays take 5 metadata bytes + length of byte array.
1125 fprintf(out, " if (arg%d == null) {\n", argIndex);
1126 fprintf(out, " arg%d = new byte[0];\n", argIndex);
1127 fprintf(out, " }\n");
1128 fprintf(out, " needed += STRING_TYPE_OVERHEAD + arg%d.length;\n", argIndex);
1129 break;
1130 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1131 {
1132 const char* uidName = attributionDecl.fields.front().name.c_str();
1133 const char* tagName = attributionDecl.fields.back().name.c_str();
1134 // Null checks on the params.
1135 fprintf(out, " if (%s == null) {\n", uidName);
1136 fprintf(out, " %s = new %s[0];\n", uidName,
1137 java_type_name(attributionDecl.fields.front().javaType));
1138 fprintf(out, " }\n");
1139 fprintf(out, " if (%s == null) {\n", tagName);
1140 fprintf(out, " %s = new %s[0];\n", tagName,
1141 java_type_name(attributionDecl.fields.back().javaType));
1142 fprintf(out, " }\n");
1143
1144 // First check that the lengths of the uid and tag arrays are the same.
1145 fprintf(out, " if (%s.length != %s.length) {\n", uidName, tagName);
1146 fprintf(out, " return;\n");
1147 fprintf(out, " }\n");
1148 fprintf(out, " int attrSize = LIST_TYPE_OVERHEAD;\n");
1149 fprintf(out, " for (int i = 0; i < %s.length; i++) {\n", tagName);
1150 fprintf(out, " String str%d = (%s[i] == null) ? \"\" : %s[i];\n",
1151 argIndex, tagName, tagName);
1152 fprintf(out, " int str%dlen = str%d.getBytes(UTF_8).length;\n",
1153 argIndex, argIndex);
1154 fprintf(out,
1155 " attrSize += "
1156 "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + str%dlen;\n",
1157 argIndex);
1158 fprintf(out, " }\n");
1159 fprintf(out, " needed += attrSize;\n");
1160 break;
1161 }
1162 default:
1163 // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR.
1164 return 1;
1165 }
1166 argIndex++;
1167 }
1168
1169 // Now we have the size that is needed. Check for overflow and return if needed.
1170 fprintf(out, " if (needed > MAX_EVENT_PAYLOAD) {\n");
1171 fprintf(out, " return;\n");
1172 fprintf(out, " }\n");
1173
1174 // Create new buffer, and associated data types.
1175 fprintf(out, " byte[] buff = new byte[needed];\n");
1176 fprintf(out, " int pos = 0;\n");
1177
1178 // Initialize the buffer with list data type.
1179 fprintf(out, " buff[pos] = LIST_TYPE;\n");
1180 fprintf(out, " buff[pos + 1] = %lu;\n", signature.size() + 2);
1181 fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n");
1182
1183 // Write timestamp.
1184 fprintf(out, " long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n");
1185 fprintf(out, " buff[pos] = LONG_TYPE;\n");
1186 fprintf(out, " copyLong(buff, pos + 1, elapsedRealtime);\n");
1187 fprintf(out, " pos += LONG_TYPE_SIZE;\n");
1188
1189 // Write atom code.
1190 fprintf(out, " buff[pos] = INT_TYPE;\n");
1191 fprintf(out, " copyInt(buff, pos + 1, code);\n");
1192 fprintf(out, " pos += INT_TYPE_SIZE;\n");
1193
1194 // Write the args.
1195 argIndex = 1;
1196 for (vector<java_type_t>::const_iterator arg = signature.begin();
1197 arg != signature.end(); arg++) {
1198 switch (*arg) {
1199 case JAVA_TYPE_BOOLEAN:
1200 fprintf(out, " buff[pos] = INT_TYPE;\n");
1201 fprintf(out, " copyInt(buff, pos + 1, arg%d? 1 : 0);\n", argIndex);
1202 fprintf(out, " pos += INT_TYPE_SIZE;\n");
1203 break;
1204 case JAVA_TYPE_INT:
1205 case JAVA_TYPE_ENUM:
1206 fprintf(out, " buff[pos] = INT_TYPE;\n");
1207 fprintf(out, " copyInt(buff, pos + 1, arg%d);\n", argIndex);
1208 fprintf(out, " pos += INT_TYPE_SIZE;\n");
1209 break;
1210 case JAVA_TYPE_FLOAT:
1211 *requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT;
1212 fprintf(out, " buff[pos] = FLOAT_TYPE;\n");
1213 fprintf(out, " copyFloat(buff, pos + 1, arg%d);\n", argIndex);
1214 fprintf(out, " pos += FLOAT_TYPE_SIZE;\n");
1215 break;
1216 case JAVA_TYPE_LONG:
1217 fprintf(out, " buff[pos] = LONG_TYPE;\n");
1218 fprintf(out, " copyLong(buff, pos + 1, arg%d);\n", argIndex);
1219 fprintf(out, " pos += LONG_TYPE_SIZE;\n");
1220 break;
1221 case JAVA_TYPE_STRING:
1222 fprintf(out, " buff[pos] = STRING_TYPE;\n");
1223 fprintf(out, " copyInt(buff, pos + 1, arg%dBytes.length);\n", argIndex);
1224 fprintf(out, " System.arraycopy("
1225 "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%dBytes.length);\n",
1226 argIndex, argIndex);
1227 fprintf(out, " pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
1228 argIndex);
1229 break;
1230 case JAVA_TYPE_BYTE_ARRAY:
1231 fprintf(out, " buff[pos] = STRING_TYPE;\n");
1232 fprintf(out, " copyInt(buff, pos + 1, arg%d.length);\n", argIndex);
1233 fprintf(out, " System.arraycopy("
1234 "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n",
1235 argIndex, argIndex);
1236 fprintf(out, " pos += STRING_TYPE_OVERHEAD + arg%d.length;\n", argIndex);
1237 break;
1238 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1239 {
1240 *requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION;
1241 const char* uidName = attributionDecl.fields.front().name.c_str();
1242 const char* tagName = attributionDecl.fields.back().name.c_str();
1243
1244 fprintf(out, " writeAttributionChain(buff, pos, %s, %s);\n",
1245 uidName, tagName);
1246 fprintf(out, " pos += attrSize;\n");
1247 break;
1248 }
1249 default:
1250 // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR.
1251 return 1;
1252 }
1253 argIndex++;
1254 }
1255
1256 fprintf(out, " StatsLog.writeRaw(buff, pos);\n");
1257 fprintf(out, " }\n");
1258 fprintf(out, "\n");
1259 }
1260 return 0;
1261}
1262
Tej Singh810eeb32019-03-07 19:08:52 -08001263static void write_java_work_source_method(FILE* out,
Tej Singh2910d5a2019-03-23 17:26:32 -07001264 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1265 const string& moduleName) {
Bookatz0bd97202018-06-05 12:42:37 -07001266 fprintf(out, "\n // WorkSource methods.\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001267 for (auto signature_to_modules_it = signatures_to_modules.begin();
1268 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
Tej Singh2910d5a2019-03-23 17:26:32 -07001269 // Skip if this signature is not needed for the module.
1270 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
1271 continue;
1272 }
Tej Singh810eeb32019-03-07 19:08:52 -08001273 vector<java_type_t> signature = signature_to_modules_it->first;
Bookatz0bd97202018-06-05 12:42:37 -07001274 // Determine if there is Attribution in this signature.
1275 int attributionArg = -1;
1276 int argIndexMax = 0;
Tej Singh810eeb32019-03-07 19:08:52 -08001277 for (vector<java_type_t>::const_iterator arg = signature.begin();
1278 arg != signature.end(); arg++) {
Bookatz0bd97202018-06-05 12:42:37 -07001279 argIndexMax++;
1280 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1281 if (attributionArg > -1) {
1282 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
1283 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
1284 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
1285 return;
1286 }
1287 attributionArg = argIndexMax;
1288 }
1289 }
1290 if (attributionArg < 0) {
1291 continue;
1292 }
1293
1294 // Method header (signature)
Tej Singh2910d5a2019-03-23 17:26:32 -07001295 if (moduleName == DEFAULT_MODULE_NAME) {
1296 fprintf(out, " /** @hide */\n");
1297 }
Bookatz0bd97202018-06-05 12:42:37 -07001298 fprintf(out, " public static void write(int code");
1299 int argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -08001300 for (vector<java_type_t>::const_iterator arg = signature.begin();
1301 arg != signature.end(); arg++) {
Bookatz0bd97202018-06-05 12:42:37 -07001302 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1303 fprintf(out, ", WorkSource ws");
1304 } else {
1305 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
1306 }
1307 argIndex++;
1308 }
1309 fprintf(out, ") {\n");
1310
1311 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
1312 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
1313 fprintf(out, " write_non_chained(code");
1314 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
1315 if (argIndex == attributionArg) {
1316 fprintf(out, ", ws.get(i), ws.getName(i)");
1317 } else {
1318 fprintf(out, ", arg%d", argIndex);
1319 }
1320 }
1321 fprintf(out, ");\n");
Tej Singh2910d5a2019-03-23 17:26:32 -07001322 fprintf(out, " }\n"); // close for-loop
Bookatz0bd97202018-06-05 12:42:37 -07001323
1324 // write() component.
1325 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
1326 fprintf(out, " if (workChains != null) {\n");
1327 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
1328 fprintf(out, " write(code");
1329 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
1330 if (argIndex == attributionArg) {
1331 fprintf(out, ", wc.getUids(), wc.getTags()");
1332 } else {
1333 fprintf(out, ", arg%d", argIndex);
1334 }
1335 }
1336 fprintf(out, ");\n");
1337 fprintf(out, " }\n"); // close for-loop
1338 fprintf(out, " }\n"); // close if
1339 fprintf(out, " }\n"); // close method
1340 }
1341}
Yangster-macba5b9e42018-01-10 21:31:59 -08001342
Tej Singh2910d5a2019-03-23 17:26:32 -07001343static void write_java_atom_codes(FILE* out, const Atoms& atoms, const string& moduleName) {
1344 fprintf(out, " // Constants for atom codes.\n");
1345
1346 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
1347 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
1348
1349 // Print constants for the atom codes.
1350 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
1351 atom != atoms.decls.end(); atom++) {
1352 // Skip if the atom is not needed for the module.
1353 if (!atom_needed_for_module(*atom, moduleName)) {
1354 continue;
1355 }
1356 string constant = make_constant_name(atom->name);
1357 fprintf(out, "\n");
1358 fprintf(out, " /**\n");
1359 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
1360 write_java_usage(out, "write", constant, *atom);
1361 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
1362 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
1363 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
1364 }
1365 if (moduleName == DEFAULT_MODULE_NAME) {
1366 fprintf(out, " * @hide\n");
1367 }
1368 fprintf(out, " */\n");
1369 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
1370 }
1371 fprintf(out, "\n");
1372}
1373
1374static void write_java_enum_values(FILE* out, const Atoms& atoms, const string& moduleName) {
1375 fprintf(out, " // Constants for enum values.\n\n");
1376 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
1377 atom != atoms.decls.end(); atom++) {
1378 // Skip if the atom is not needed for the module.
1379 if (!atom_needed_for_module(*atom, moduleName)) {
1380 continue;
1381 }
1382 for (vector<AtomField>::const_iterator field = atom->fields.begin();
1383 field != atom->fields.end(); field++) {
1384 if (field->javaType == JAVA_TYPE_ENUM) {
1385 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
1386 field->name.c_str());
1387 for (map<int, string>::const_iterator value = field->enumValues.begin();
1388 value != field->enumValues.end(); value++) {
1389 if (moduleName == DEFAULT_MODULE_NAME) {
1390 fprintf(out, " /** @hide */\n");
1391 }
1392 fprintf(out, " public static final int %s__%s__%s = %d;\n",
1393 make_constant_name(atom->message).c_str(),
1394 make_constant_name(field->name).c_str(),
1395 make_constant_name(value->second).c_str(),
1396 value->first);
1397 }
1398 fprintf(out, "\n");
1399 }
1400 }
1401 }
1402}
1403
Yao Chend54f9dd2017-10-17 17:37:48 +00001404static int
Yangster-mac7604aea2017-12-11 22:55:49 -08001405write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001406{
Yao Chend54f9dd2017-10-17 17:37:48 +00001407 // Print prelude
1408 fprintf(out, "// This file is autogenerated\n");
1409 fprintf(out, "\n");
1410 fprintf(out, "package android.util;\n");
1411 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -07001412 fprintf(out, "import android.os.WorkSource;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001413 fprintf(out, "import android.util.SparseArray;\n");
Bookatz0bd97202018-06-05 12:42:37 -07001414 fprintf(out, "import java.util.ArrayList;\n");
1415 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001416 fprintf(out, "\n");
1417 fprintf(out, "/**\n");
1418 fprintf(out, " * API For logging statistics events.\n");
1419 fprintf(out, " * @hide\n");
1420 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -08001421 fprintf(out, "public class StatsLogInternal {\n");
Tej Singh2910d5a2019-03-23 17:26:32 -07001422 write_java_atom_codes(out, atoms, DEFAULT_MODULE_NAME);
Yao Chend54f9dd2017-10-17 17:37:48 +00001423
Tej Singh2910d5a2019-03-23 17:26:32 -07001424 write_java_enum_values(out, atoms, DEFAULT_MODULE_NAME);
Stefan Lafon9478f352017-10-30 21:20:20 -07001425
Yao Chend54f9dd2017-10-17 17:37:48 +00001426 // Print write methods
1427 fprintf(out, " // Write methods\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001428 write_java_method(out, "write", atoms.signatures_to_modules, attributionDecl);
1429 write_java_method(out, "write_non_chained", atoms.non_chained_signatures_to_modules,
1430 attributionDecl);
Tej Singh2910d5a2019-03-23 17:26:32 -07001431 write_java_work_source_method(out, atoms.signatures_to_modules, DEFAULT_MODULE_NAME);
Yao Chend54f9dd2017-10-17 17:37:48 +00001432
1433 fprintf(out, "}\n");
1434
1435 return 0;
1436}
1437
Tej Singh2910d5a2019-03-23 17:26:32 -07001438// TODO: Merge this with write_stats_log_java so that we can get rid of StatsLogInternal JNI.
1439static int
1440write_stats_log_java_for_module(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
1441 const string& moduleName, const string& javaClass, const string& javaPackage)
1442{
1443 // Print prelude
1444 fprintf(out, "// This file is autogenerated\n");
1445 fprintf(out, "\n");
1446 fprintf(out, "package %s;\n", javaPackage.c_str());
1447 fprintf(out, "\n");
1448 fprintf(out, "import static java.nio.charset.StandardCharsets.UTF_8;\n");
1449 fprintf(out, "\n");
1450 fprintf(out, "import android.util.StatsLog;\n");
1451 fprintf(out, "import android.os.SystemClock;\n");
1452 fprintf(out, "\n");
1453 fprintf(out, "import java.util.ArrayList;\n");
1454 fprintf(out, "\n");
1455 fprintf(out, "\n");
1456 fprintf(out, "/**\n");
1457 fprintf(out, " * Utility class for logging statistics events.\n");
1458 fprintf(out, " */\n");
1459 fprintf(out, "public class %s {\n", javaClass.c_str());
1460
1461 // TODO: ideally these match with the native values (and automatically change if they change).
1462 fprintf(out, " private static final int LOGGER_ENTRY_MAX_PAYLOAD = 4068;\n");
1463 fprintf(out,
1464 " private static final int MAX_EVENT_PAYLOAD = LOGGER_ENTRY_MAX_PAYLOAD - 4;\n");
1465 // Value types. Must match with EventLog.java and log.h.
1466 fprintf(out, " private static final byte INT_TYPE = 0;\n");
1467 fprintf(out, " private static final byte LONG_TYPE = 1;\n");
1468 fprintf(out, " private static final byte STRING_TYPE = 2;\n");
1469 fprintf(out, " private static final byte LIST_TYPE = 3;\n");
1470 fprintf(out, " private static final byte FLOAT_TYPE = 4;\n");
1471
1472 // Size of each value type.
1473 // Booleans, ints, floats, and enums take 5 bytes, 1 for the type and 4 for the value.
1474 fprintf(out, " private static final int INT_TYPE_SIZE = 5;\n");
1475 fprintf(out, " private static final int FLOAT_TYPE_SIZE = 5;\n");
1476 // Longs take 9 bytes, 1 for the type and 8 for the value.
1477 fprintf(out, " private static final int LONG_TYPE_SIZE = 9;\n");
1478 // Strings take 5 metadata bytes: 1 byte is for the type, 4 are for the length.
1479 fprintf(out, " private static final int STRING_TYPE_OVERHEAD = 5;\n");
1480 fprintf(out, " private static final int LIST_TYPE_OVERHEAD = 2;\n");
1481
1482 write_java_atom_codes(out, atoms, moduleName);
1483
1484 write_java_enum_values(out, atoms, moduleName);
1485
1486 int errors = 0;
1487 int requiredHelpers = 0;
1488 // Print write methods
1489 fprintf(out, " // Write methods\n");
1490 errors += write_java_method_for_module(out, atoms.signatures_to_modules, attributionDecl,
1491 moduleName, &requiredHelpers);
1492 errors += write_java_non_chained_method_for_module(out, atoms.non_chained_signatures_to_modules,
1493 moduleName);
1494
1495 fprintf(out, " // Helper methods for copying primitives\n");
1496 write_java_helpers_for_module(out, attributionDecl, requiredHelpers);
1497
1498 fprintf(out, "}\n");
1499
1500 return errors;
1501}
1502
Yao Chend54f9dd2017-10-17 17:37:48 +00001503static const char*
1504jni_type_name(java_type_t type)
1505{
1506 switch (type) {
1507 case JAVA_TYPE_BOOLEAN:
1508 return "jboolean";
1509 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001510 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001511 return "jint";
1512 case JAVA_TYPE_LONG:
1513 return "jlong";
1514 case JAVA_TYPE_FLOAT:
1515 return "jfloat";
1516 case JAVA_TYPE_DOUBLE:
1517 return "jdouble";
1518 case JAVA_TYPE_STRING:
1519 return "jstring";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001520 case JAVA_TYPE_BYTE_ARRAY:
1521 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +00001522 default:
1523 return "UNKNOWN";
1524 }
1525}
1526
Yangster-mac7604aea2017-12-11 22:55:49 -08001527static const char*
1528jni_array_type_name(java_type_t type)
1529{
1530 switch (type) {
1531 case JAVA_TYPE_INT:
1532 return "jintArray";
Yangster-mace124e422018-08-16 10:30:28 -07001533 case JAVA_TYPE_FLOAT:
1534 return "jfloatArray";
Yangster-mac7604aea2017-12-11 22:55:49 -08001535 case JAVA_TYPE_STRING:
1536 return "jobjectArray";
1537 default:
1538 return "UNKNOWN";
1539 }
1540}
1541
Yao Chend54f9dd2017-10-17 17:37:48 +00001542static string
Yangster-macba5b9e42018-01-10 21:31:59 -08001543jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +00001544{
Yangster-macba5b9e42018-01-10 21:31:59 -08001545 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +00001546 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001547 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001548 switch (*arg) {
1549 case JAVA_TYPE_BOOLEAN:
1550 result += "_boolean";
1551 break;
1552 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001553 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001554 result += "_int";
1555 break;
1556 case JAVA_TYPE_LONG:
1557 result += "_long";
1558 break;
1559 case JAVA_TYPE_FLOAT:
1560 result += "_float";
1561 break;
1562 case JAVA_TYPE_DOUBLE:
1563 result += "_double";
1564 break;
1565 case JAVA_TYPE_STRING:
1566 result += "_String";
1567 break;
Yangster-mac7604aea2017-12-11 22:55:49 -08001568 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1569 result += "_AttributionChain";
1570 break;
Yangster-mace124e422018-08-16 10:30:28 -07001571 case JAVA_TYPE_KEY_VALUE_PAIR:
1572 result += "_KeyValuePairs";
1573 break;
Yao Chenbbdd67d2018-10-24 12:15:56 -07001574 case JAVA_TYPE_BYTE_ARRAY:
1575 result += "_bytes";
1576 break;
Yao Chend54f9dd2017-10-17 17:37:48 +00001577 default:
1578 result += "_UNKNOWN";
1579 break;
1580 }
1581 }
1582 return result;
1583}
1584
1585static const char*
1586java_type_signature(java_type_t type)
1587{
1588 switch (type) {
1589 case JAVA_TYPE_BOOLEAN:
1590 return "Z";
1591 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001592 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001593 return "I";
1594 case JAVA_TYPE_LONG:
1595 return "J";
1596 case JAVA_TYPE_FLOAT:
1597 return "F";
1598 case JAVA_TYPE_DOUBLE:
1599 return "D";
1600 case JAVA_TYPE_STRING:
1601 return "Ljava/lang/String;";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001602 case JAVA_TYPE_BYTE_ARRAY:
1603 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +00001604 default:
1605 return "UNKNOWN";
1606 }
1607}
1608
1609static string
Yangster-mac7604aea2017-12-11 22:55:49 -08001610jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001611{
1612 string result("(I");
1613 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001614 arg != signature.end(); arg++) {
1615 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1616 for (auto chainField : attributionDecl.fields) {
1617 result += "[";
1618 result += java_type_signature(chainField.javaType);
1619 }
Yangster-mace124e422018-08-16 10:30:28 -07001620 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1621 result += "Landroid/util/SparseArray;";
Yangster-mac7604aea2017-12-11 22:55:49 -08001622 } else {
1623 result += java_type_signature(*arg);
1624 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001625 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001626 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +00001627 return result;
1628}
1629
Yangster-mace124e422018-08-16 10:30:28 -07001630static void write_key_value_map_jni(FILE* out) {
Howard Ro4078dd42018-09-27 17:41:08 -07001631 fprintf(out, " std::map<int, int32_t> int32_t_map;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001632 fprintf(out, " std::map<int, int64_t> int64_t_map;\n");
1633 fprintf(out, " std::map<int, float> float_map;\n");
1634 fprintf(out, " std::map<int, char const*> string_map;\n\n");
1635
1636 fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n");
1637
1638 fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n");
1639 fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n");
1640 fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n");
1641
1642
1643 fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n");
1644
Howard Ro4078dd42018-09-27 17:41:08 -07001645 fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001646 fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n");
1647 fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n");
1648 fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001649 fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001650 fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n");
1651 fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n");
1652
1653 fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n");
1654 fprintf(out, " for(int i = 0; i < jsize; i++) {\n");
1655 fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n");
1656 fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n");
1657 fprintf(out, " if (jvalue_obj == NULL) { continue; }\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001658 fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n");
1659 fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n");
1660 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n");
Yangster-mace124e422018-08-16 10:30:28 -07001661 fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n");
1662 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n");
1663 fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n");
1664 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n");
1665 fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n");
1666 fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n");
1667 fprintf(out, " scoped_ufs.push_back(std::move(utf));\n");
1668 fprintf(out, " }\n");
1669 fprintf(out, " }\n");
1670}
1671
Yao Chend54f9dd2017-10-17 17:37:48 +00001672static int
Yangster-macba5b9e42018-01-10 21:31:59 -08001673write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
Tej Singh810eeb32019-03-07 19:08:52 -08001674 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1675 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001676 // Print write methods
Tej Singh810eeb32019-03-07 19:08:52 -08001677 for (auto signature_to_modules_it = signatures_to_modules.begin();
1678 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1679 vector<java_type_t> signature = signature_to_modules_it->first;
Yao Chend54f9dd2017-10-17 17:37:48 +00001680 int argIndex;
1681
Yao Chen97e21ec2018-03-29 11:00:38 -07001682 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001683 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Tej Singh810eeb32019-03-07 19:08:52 -08001684 jni_function_name(java_method_name, signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001685 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -08001686 for (vector<java_type_t>::const_iterator arg = signature.begin();
1687 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001688 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1689 for (auto chainField : attributionDecl.fields) {
1690 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1691 chainField.name.c_str());
1692 }
Yangster-mace124e422018-08-16 10:30:28 -07001693 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1694 fprintf(out, ", jobject value_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001695 } else {
1696 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1697 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001698 argIndex++;
1699 }
1700 fprintf(out, ")\n");
1701
1702 fprintf(out, "{\n");
1703
1704 // Prepare strings
1705 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001706 bool hadStringOrChain = false;
Yangster-mace124e422018-08-16 10:30:28 -07001707 bool isKeyValuePairAtom = false;
Tej Singh810eeb32019-03-07 19:08:52 -08001708 for (vector<java_type_t>::const_iterator arg = signature.begin();
1709 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001710 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001711 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001712 fprintf(out, " const char* str%d;\n", argIndex);
1713 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1714 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1715 argIndex, argIndex);
1716 fprintf(out, " } else {\n");
1717 fprintf(out, " str%d = NULL;\n", argIndex);
1718 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001719 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1720 hadStringOrChain = true;
1721 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1722 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chen1fe9f592018-12-06 10:34:25 -08001723 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen8e6f9982018-11-29 09:39:45 -08001724 fprintf(out,
1725 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1726 "0) {\n",
1727 argIndex, argIndex);
Yao Chenbbdd67d2018-10-24 12:15:56 -07001728 fprintf(out,
1729 " jbyte_array%d = "
1730 "env->GetByteArrayElements(arg%d, NULL);\n",
1731 argIndex, argIndex);
1732 fprintf(out,
Yao Chen1fe9f592018-12-06 10:34:25 -08001733 " str%d_length = env->GetArrayLength(arg%d);\n",
1734 argIndex, argIndex);
1735 fprintf(out,
Yao Chenbbdd67d2018-10-24 12:15:56 -07001736 " str%d = "
1737 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1738 "d, NULL));\n",
1739 argIndex, argIndex);
1740 fprintf(out, " } else {\n");
1741 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1742 fprintf(out, " str%d = NULL;\n", argIndex);
1743 fprintf(out, " }\n");
1744
Yao Chene89572c2019-01-09 15:41:50 -08001745 fprintf(out,
1746 " android::util::BytesField bytesField%d(str%d, "
1747 "str%d_length);",
1748 argIndex, argIndex, argIndex);
1749
Yangster-mac7604aea2017-12-11 22:55:49 -08001750 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1751 hadStringOrChain = true;
1752 for (auto chainField : attributionDecl.fields) {
1753 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1754 chainField.name.c_str(), chainField.name.c_str());
1755 if (chainField.name != attributionDecl.fields.front().name) {
1756 fprintf(out, " if (%s_length != %s_length) {\n",
1757 chainField.name.c_str(),
1758 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001759 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001760 fprintf(out, " }\n");
1761 }
1762 if (chainField.javaType == JAVA_TYPE_INT) {
1763 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1764 chainField.name.c_str(), chainField.name.c_str());
1765 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1766 fprintf(out, " std::vector<%s> %s_vec;\n",
1767 cpp_type_name(chainField.javaType), chainField.name.c_str());
1768 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1769 chainField.name.c_str());
1770 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1771 chainField.name.c_str());
1772 fprintf(out, " jstring jstr = "
1773 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1774 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001775 fprintf(out, " if (jstr == NULL) {\n");
1776 fprintf(out, " %s_vec.push_back(NULL);\n",
1777 chainField.name.c_str());
1778 fprintf(out, " } else {\n");
1779 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001780 "new ScopedUtfChars(env, jstr);\n",
1781 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001782 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001783 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001784 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001785 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001786 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001787 fprintf(out, " }\n");
1788 }
1789 fprintf(out, "\n");
1790 }
Yangster-mace124e422018-08-16 10:30:28 -07001791 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1792 isKeyValuePairAtom = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001793 }
1794 argIndex++;
1795 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001796 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1797 // chains.
Yangster-mace124e422018-08-16 10:30:28 -07001798 if (!hadStringOrChain && !isKeyValuePairAtom) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001799 fprintf(out, " (void)env;\n");
1800 }
Yangster-mace124e422018-08-16 10:30:28 -07001801 if (isKeyValuePairAtom) {
1802 write_key_value_map_jni(out);
1803 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001804
1805 // stats_write call
1806 argIndex = 1;
Yao Chene89572c2019-01-09 15:41:50 -08001807 fprintf(out, "\n int ret = android::util::%s(code",
1808 cpp_method_name.c_str());
Tej Singh810eeb32019-03-07 19:08:52 -08001809 for (vector<java_type_t>::const_iterator arg = signature.begin();
1810 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001811 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1812 for (auto chainField : attributionDecl.fields) {
1813 if (chainField.javaType == JAVA_TYPE_INT) {
1814 fprintf(out, ", (const %s*)%s_array, %s_length",
1815 cpp_type_name(chainField.javaType),
1816 chainField.name.c_str(), chainField.name.c_str());
1817 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1818 fprintf(out, ", %s_vec", chainField.name.c_str());
1819 }
1820 }
Yangster-mace124e422018-08-16 10:30:28 -07001821 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -07001822 fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map");
Yao Chene89572c2019-01-09 15:41:50 -08001823 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1824 fprintf(out, ", bytesField%d", argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -08001825 } else {
Yao Chene89572c2019-01-09 15:41:50 -08001826 const char* argName =
1827 (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001828 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1829 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001830 argIndex++;
1831 }
1832 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001833 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001834
1835 // Clean up strings
1836 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -08001837 for (vector<java_type_t>::const_iterator arg = signature.begin();
1838 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001839 if (*arg == JAVA_TYPE_STRING) {
1840 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1841 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1842 argIndex, argIndex);
1843 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001844 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1845 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1846 fprintf(out,
1847 " env->ReleaseByteArrayElements(arg%d, "
1848 "jbyte_array%d, 0);\n",
1849 argIndex, argIndex);
1850 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001851 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1852 for (auto chainField : attributionDecl.fields) {
1853 if (chainField.javaType == JAVA_TYPE_INT) {
1854 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1855 chainField.name.c_str(), chainField.name.c_str());
1856 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001857 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001858 chainField.name.c_str());
1859 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1860 fprintf(out, " }\n");
1861 }
1862 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001863 }
1864 argIndex++;
1865 }
Yangster-mace124e422018-08-16 10:30:28 -07001866
Yao Chen97e21ec2018-03-29 11:00:38 -07001867 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001868
1869 fprintf(out, "}\n");
1870 fprintf(out, "\n");
1871 }
1872
Yangster-macba5b9e42018-01-10 21:31:59 -08001873
1874 return 0;
1875}
1876
1877void write_jni_registration(FILE* out, const string& java_method_name,
Tej Singh810eeb32019-03-07 19:08:52 -08001878 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1879 const AtomDecl &attributionDecl) {
1880 for (auto signature_to_modules_it = signatures_to_modules.begin();
1881 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1882 vector<java_type_t> signature = signature_to_modules_it->first;
Yangster-macba5b9e42018-01-10 21:31:59 -08001883 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1884 java_method_name.c_str(),
Tej Singh810eeb32019-03-07 19:08:52 -08001885 jni_function_signature(signature, attributionDecl).c_str(),
1886 jni_function_name(java_method_name, signature).c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -08001887 }
1888}
1889
1890static int
1891write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1892{
1893 // Print prelude
1894 fprintf(out, "// This file is autogenerated\n");
1895 fprintf(out, "\n");
1896
1897 fprintf(out, "#include <statslog.h>\n");
1898 fprintf(out, "\n");
1899 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1900 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1901 fprintf(out, "#include <utils/Vector.h>\n");
1902 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1903 fprintf(out, "#include \"jni.h\"\n");
1904 fprintf(out, "\n");
1905 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1906 fprintf(out, "\n");
1907
1908 fprintf(out, "namespace android {\n");
1909 fprintf(out, "\n");
1910
Tej Singh810eeb32019-03-07 19:08:52 -08001911 write_stats_log_jni(out, "write", "stats_write", atoms.signatures_to_modules, attributionDecl);
Yangster-macba5b9e42018-01-10 21:31:59 -08001912 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
Tej Singh810eeb32019-03-07 19:08:52 -08001913 atoms.non_chained_signatures_to_modules, attributionDecl);
Yangster-macba5b9e42018-01-10 21:31:59 -08001914
Yao Chend54f9dd2017-10-17 17:37:48 +00001915 // Print registration function table
1916 fprintf(out, "/*\n");
1917 fprintf(out, " * JNI registration.\n");
1918 fprintf(out, " */\n");
1919 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001920 write_jni_registration(out, "write", atoms.signatures_to_modules, attributionDecl);
1921 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures_to_modules,
1922 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001923 fprintf(out, "};\n");
1924 fprintf(out, "\n");
1925
1926 // Print registration function
Tej Singhbe0482b2019-03-19 22:01:57 -07001927 fprintf(out, "int register_android_util_StatsLogInternal(JNIEnv* env) {\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001928 fprintf(out, " return RegisterMethodsOrDie(\n");
1929 fprintf(out, " env,\n");
Tej Singhbe0482b2019-03-19 22:01:57 -07001930 fprintf(out, " \"android/util/StatsLogInternal\",\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001931 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1932 fprintf(out, "}\n");
1933
1934 fprintf(out, "\n");
1935 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001936 return 0;
1937}
1938
Yao Chend54f9dd2017-10-17 17:37:48 +00001939static void
1940print_usage()
1941{
1942 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1943 fprintf(stderr, "\n");
1944 fprintf(stderr, "OPTIONS\n");
1945 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1946 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1947 fprintf(stderr, " --help this message\n");
1948 fprintf(stderr, " --java FILENAME the java file to output\n");
1949 fprintf(stderr, " --jni FILENAME the jni file to output\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001950 fprintf(stderr, " --module NAME optional, module name to generate outputs for\n");
1951 fprintf(stderr, " --namespace COMMA,SEP,NAMESPACE required for cpp/header with module\n");
1952 fprintf(stderr, " comma separated namespace of the files\n");
1953 fprintf(stderr, " --importHeader NAME required for cpp/jni to say which header to import\n");
Tej Singh2910d5a2019-03-23 17:26:32 -07001954 fprintf(stderr, " --javaPackage PACKAGE the package for the java file.\n");
1955 fprintf(stderr, " required for java with module\n");
1956 fprintf(stderr, " --javaClass CLASS the class name of the java class.\n");
1957 fprintf(stderr, " Optional for Java with module.\n");
1958 fprintf(stderr, " Default is \"StatsLogInternal\"\n");}
Yao Chend54f9dd2017-10-17 17:37:48 +00001959
1960/**
1961 * Do the argument parsing and execute the tasks.
1962 */
1963static int
1964run(int argc, char const*const* argv)
1965{
1966 string cppFilename;
1967 string headerFilename;
1968 string javaFilename;
1969 string jniFilename;
1970
Tej Singh810eeb32019-03-07 19:08:52 -08001971 string moduleName = DEFAULT_MODULE_NAME;
1972 string cppNamespace = DEFAULT_CPP_NAMESPACE;
1973 string cppHeaderImport = DEFAULT_CPP_HEADER_IMPORT;
Tej Singh2910d5a2019-03-23 17:26:32 -07001974 string javaPackage = DEFAULT_JAVA_PACKAGE;
1975 string javaClass = DEFAULT_JAVA_CLASS;
Tej Singh810eeb32019-03-07 19:08:52 -08001976
Yao Chend54f9dd2017-10-17 17:37:48 +00001977 int index = 1;
1978 while (index < argc) {
1979 if (0 == strcmp("--help", argv[index])) {
1980 print_usage();
1981 return 0;
1982 } else if (0 == strcmp("--cpp", argv[index])) {
1983 index++;
1984 if (index >= argc) {
1985 print_usage();
1986 return 1;
1987 }
1988 cppFilename = argv[index];
1989 } else if (0 == strcmp("--header", argv[index])) {
1990 index++;
1991 if (index >= argc) {
1992 print_usage();
1993 return 1;
1994 }
1995 headerFilename = argv[index];
1996 } else if (0 == strcmp("--java", argv[index])) {
1997 index++;
1998 if (index >= argc) {
1999 print_usage();
2000 return 1;
2001 }
2002 javaFilename = argv[index];
2003 } else if (0 == strcmp("--jni", argv[index])) {
2004 index++;
2005 if (index >= argc) {
2006 print_usage();
2007 return 1;
2008 }
2009 jniFilename = argv[index];
Tej Singh810eeb32019-03-07 19:08:52 -08002010 } else if (0 == strcmp("--module", argv[index])) {
2011 index++;
2012 if (index >= argc) {
2013 print_usage();
2014 return 1;
2015 }
2016 moduleName = argv[index];
2017 } else if (0 == strcmp("--namespace", argv[index])) {
2018 index++;
2019 if (index >= argc) {
2020 print_usage();
2021 return 1;
2022 }
2023 cppNamespace = argv[index];
2024 } else if (0 == strcmp("--importHeader", argv[index])) {
2025 index++;
2026 if (index >= argc) {
2027 print_usage();
2028 return 1;
2029 }
2030 cppHeaderImport = argv[index];
Tej Singh2910d5a2019-03-23 17:26:32 -07002031 } else if (0 == strcmp("--javaPackage", argv[index])) {
2032 index++;
2033 if (index >= argc) {
2034 print_usage();
2035 return 1;
2036 }
2037 javaPackage = argv[index];
2038 } else if (0 == strcmp("--javaClass", argv[index])) {
2039 index++;
2040 if (index >= argc) {
2041 print_usage();
2042 return 1;
2043 }
2044 javaClass = argv[index];
Yao Chend54f9dd2017-10-17 17:37:48 +00002045 }
2046 index++;
2047 }
2048
2049 if (cppFilename.size() == 0
2050 && headerFilename.size() == 0
2051 && javaFilename.size() == 0
2052 && jniFilename.size() == 0) {
2053 print_usage();
2054 return 1;
2055 }
2056
2057 // Collate the parameters
2058 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08002059 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00002060 if (errorCount != 0) {
2061 return 1;
2062 }
2063
Yangster-mac7604aea2017-12-11 22:55:49 -08002064 AtomDecl attributionDecl;
2065 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08002066 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08002067 &attributionDecl, &attributionSignature);
2068
Yao Chend54f9dd2017-10-17 17:37:48 +00002069 // Write the .cpp file
2070 if (cppFilename.size() != 0) {
2071 FILE* out = fopen(cppFilename.c_str(), "w");
2072 if (out == NULL) {
2073 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
2074 return 1;
2075 }
Tej Singh810eeb32019-03-07 19:08:52 -08002076 // If this is for a specific module, the namespace must also be provided.
2077 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
2078 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
2079 return 1;
2080 }
2081 // If this is for a specific module, the header file to import must also be provided.
2082 if (moduleName != DEFAULT_MODULE_NAME && cppHeaderImport == DEFAULT_CPP_HEADER_IMPORT) {
2083 fprintf(stderr, "Must supply --headerImport if supplying a specific module\n");
2084 return 1;
2085 }
Yangster-mac7604aea2017-12-11 22:55:49 -08002086 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
Tej Singh810eeb32019-03-07 19:08:52 -08002087 out, atoms, attributionDecl, moduleName, cppNamespace, cppHeaderImport);
Yao Chend54f9dd2017-10-17 17:37:48 +00002088 fclose(out);
2089 }
2090
2091 // Write the .h file
2092 if (headerFilename.size() != 0) {
2093 FILE* out = fopen(headerFilename.c_str(), "w");
2094 if (out == NULL) {
2095 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
2096 return 1;
2097 }
Tej Singh810eeb32019-03-07 19:08:52 -08002098 // If this is for a specific module, the namespace must also be provided.
2099 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
2100 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
2101 }
Yangster-mac7604aea2017-12-11 22:55:49 -08002102 errorCount = android::stats_log_api_gen::write_stats_log_header(
Tej Singh810eeb32019-03-07 19:08:52 -08002103 out, atoms, attributionDecl, moduleName, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +00002104 fclose(out);
2105 }
2106
2107 // Write the .java file
2108 if (javaFilename.size() != 0) {
2109 FILE* out = fopen(javaFilename.c_str(), "w");
2110 if (out == NULL) {
2111 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
2112 return 1;
2113 }
Tej Singh2910d5a2019-03-23 17:26:32 -07002114 // If this is for a specific module, the java package must also be provided.
2115 if (moduleName != DEFAULT_MODULE_NAME && javaPackage== DEFAULT_JAVA_PACKAGE) {
2116 fprintf(stderr, "Must supply --javaPackage if supplying a specific module\n");
2117 return 1;
2118 }
2119 if (moduleName == DEFAULT_MODULE_NAME) {
2120 errorCount = android::stats_log_api_gen::write_stats_log_java(
2121 out, atoms, attributionDecl);
2122 } else {
2123 errorCount = android::stats_log_api_gen::write_stats_log_java_for_module(
2124 out, atoms, attributionDecl, moduleName, javaClass, javaPackage);
2125 }
Yao Chend54f9dd2017-10-17 17:37:48 +00002126 fclose(out);
2127 }
2128
2129 // Write the jni file
2130 if (jniFilename.size() != 0) {
2131 FILE* out = fopen(jniFilename.c_str(), "w");
2132 if (out == NULL) {
2133 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
2134 return 1;
2135 }
Yangster-mac7604aea2017-12-11 22:55:49 -08002136 errorCount = android::stats_log_api_gen::write_stats_log_jni(
2137 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00002138 fclose(out);
2139 }
2140
Tej Singh2910d5a2019-03-23 17:26:32 -07002141 return errorCount;
Yao Chend54f9dd2017-10-17 17:37:48 +00002142}
2143
2144}
2145}
2146
2147/**
2148 * Main.
2149 */
2150int
2151main(int argc, char const*const* argv)
2152{
2153 GOOGLE_PROTOBUF_VERIFY_VERSION;
2154
2155 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07002156}