blob: f62fef076f48b400db759b1b26e42931a1941de4 [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> "
Tej Singh29ac6012019-06-05 18:10:12 -0700134 "AtomsInfo::kTruncatingTimestampAtomBlackList = {\n");
135 for (set<string>::const_iterator blacklistedAtom = kTruncatingAtomNames.begin();
136 blacklistedAtom != kTruncatingAtomNames.end(); blacklistedAtom++) {
137 fprintf(out, " %s,\n", make_constant_name(*blacklistedAtom).c_str());
Yao Chenc40a19d2018-03-15 16:48:25 -0700138 }
139 fprintf(out, "};\n");
140 fprintf(out, "\n");
141
142 fprintf(out,
143 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
144 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
145 atom != atoms.decls.end(); atom++) {
146 for (vector<AtomField>::const_iterator field = atom->fields.begin();
147 field != atom->fields.end(); field++) {
148 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
149 string constant = make_constant_name(atom->name);
150 fprintf(out, " %s,\n", constant.c_str());
151 break;
152 }
153 }
154 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000155
156 fprintf(out, "};\n");
157 fprintf(out, "\n");
158
159 fprintf(out,
160 "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
161 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
162 atom != atoms.decls.end(); atom++) {
163 if (atom->whitelisted) {
164 string constant = make_constant_name(atom->name);
165 fprintf(out, " %s,\n", constant.c_str());
166 }
167 }
168
Yao Chenc40a19d2018-03-15 16:48:25 -0700169 fprintf(out, "};\n");
170 fprintf(out, "\n");
171
Yangster-macca5c0862018-04-09 22:39:53 -0700172 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700173 fprintf(out, " std::map<int, int> uidField;\n");
174 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
175 atom != atoms.decls.end(); atom++) {
176 if (atom->uidField == 0) {
177 continue;
178 }
179 fprintf(out,
180 "\n // Adding uid field for atom "
181 "(%d)%s\n",
182 atom->code, atom->name.c_str());
183 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
184 make_constant_name(atom->name).c_str(), atom->uidField);
185 }
186
187 fprintf(out, " return uidField;\n");
188 fprintf(out, "};\n");
189
190 fprintf(out,
191 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
192 "getAtomUidField();\n");
193
194 fprintf(out,
195 "static std::map<int, StateAtomFieldOptions> "
196 "getStateAtomFieldOptions() {\n");
197 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
198 fprintf(out, " StateAtomFieldOptions opt;\n");
199 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
200 atom != atoms.decls.end(); atom++) {
201 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
202 continue;
203 }
204 fprintf(out,
205 "\n // Adding primary and exclusive fields for atom "
206 "(%d)%s\n",
207 atom->code, atom->name.c_str());
208 fprintf(out, " opt.primaryFields.clear();\n");
209 for (const auto& field : atom->primaryFields) {
210 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
211 }
212
213 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
214 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
215 make_constant_name(atom->name).c_str());
216 }
217
218 fprintf(out, " return options;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700219 fprintf(out, "}\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700220
221 fprintf(out,
222 "const std::map<int, StateAtomFieldOptions> "
223 "AtomsInfo::kStateAtomsFieldOptions = "
224 "getStateAtomFieldOptions();\n");
225
Yao Chenbbdd67d2018-10-24 12:15:56 -0700226 fprintf(out,
227 "static std::map<int, std::vector<int>> "
228 "getBinaryFieldAtoms() {\n");
229 fprintf(out, " std::map<int, std::vector<int>> options;\n");
230 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
231 atom != atoms.decls.end(); atom++) {
232 if (atom->binaryFields.size() == 0) {
233 continue;
234 }
235 fprintf(out,
236 "\n // Adding binary fields for atom "
237 "(%d)%s\n",
238 atom->code, atom->name.c_str());
239
240 for (const auto& field : atom->binaryFields) {
241 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
242 make_constant_name(atom->name).c_str(), field);
243 }
244 }
245
246 fprintf(out, " return options;\n");
247 fprintf(out, "}\n");
248
249 fprintf(out,
250 "const std::map<int, std::vector<int>> "
251 "AtomsInfo::kBytesFieldAtoms = "
252 "getBinaryFieldAtoms();\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800253}
254
255// Writes namespaces for the cpp and header files, returning the number of namespaces written.
256void write_namespace(FILE* out, const string& cppNamespaces) {
257 vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
258 for (string cppNamespace : cppNamespaceVec) {
259 fprintf(out, "namespace %s {\n", cppNamespace.c_str());
260 }
261}
262
263// Writes namespace closing brackets for cpp and header files.
264void write_closing_namespace(FILE* out, const string& cppNamespaces) {
265 vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
266 for (auto it = cppNamespaceVec.rbegin(); it != cppNamespaceVec.rend(); ++it) {
267 fprintf(out, "} // namespace %s\n", it->c_str());
268 }
269}
270
271static int write_stats_log_cpp(FILE *out, const Atoms &atoms, const AtomDecl &attributionDecl,
272 const string& moduleName, const string& cppNamespace,
273 const string& importHeader) {
274 // Print prelude
275 fprintf(out, "// This file is autogenerated\n");
276 fprintf(out, "\n");
277
278 fprintf(out, "#include <mutex>\n");
279 fprintf(out, "#include <chrono>\n");
280 fprintf(out, "#include <thread>\n");
281 fprintf(out, "#ifdef __ANDROID__\n");
282 fprintf(out, "#include <cutils/properties.h>\n");
283 fprintf(out, "#endif\n");
284 fprintf(out, "#include <stats_event_list.h>\n");
285 fprintf(out, "#include <log/log.h>\n");
286 fprintf(out, "#include <%s>\n", importHeader.c_str());
287 fprintf(out, "#include <utils/SystemClock.h>\n");
288 fprintf(out, "\n");
289
290 write_namespace(out, cppNamespace);
291 fprintf(out, "// the single event tag id for all stats logs\n");
292 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
293 fprintf(out, "#ifdef __ANDROID__\n");
294 fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
295 fprintf(out, "#else\n");
296 fprintf(out, "const static bool kStatsdEnabled = false;\n");
297 fprintf(out, "#endif\n");
298
299 // AtomsInfo is only used by statsd internally and is not needed for other modules.
300 if (moduleName == DEFAULT_MODULE_NAME) {
301 write_atoms_info_cpp(out, atoms);
302 }
Yangster-macca5c0862018-04-09 22:39:53 -0700303
304 fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
305 fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
306 fprintf(out, "static std::mutex mLogdRetryMutex;\n");
307
Yao Chend54f9dd2017-10-17 17:37:48 +0000308 // Print write methods
309 fprintf(out, "\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800310 for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
311 signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
312 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
313 continue;
314 }
315 vector<java_type_t> signature = signature_to_modules_it->first;
Yao Chend54f9dd2017-10-17 17:37:48 +0000316 int argIndex;
317
Yao Chen97e21ec2018-03-29 11:00:38 -0700318 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700319 fprintf(out, "try_stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000320 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800321 for (vector<java_type_t>::const_iterator arg = signature.begin();
322 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800323 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
324 for (auto chainField : attributionDecl.fields) {
325 if (chainField.javaType == JAVA_TYPE_STRING) {
326 fprintf(out, ", const std::vector<%s>& %s",
327 cpp_type_name(chainField.javaType),
328 chainField.name.c_str());
329 } else {
330 fprintf(out, ", const %s* %s, size_t %s_length",
331 cpp_type_name(chainField.javaType),
332 chainField.name.c_str(), chainField.name.c_str());
333 }
334 }
Yangster-mace124e422018-08-16 10:30:28 -0700335 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700336 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
337 "const std::map<int, int64_t>& arg%d_2, "
338 "const std::map<int, char const*>& arg%d_3, "
339 "const std::map<int, float>& arg%d_4",
340 argIndex, argIndex, argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800341 } else {
342 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
343 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000344 argIndex++;
345 }
346 fprintf(out, ")\n");
347
348 fprintf(out, "{\n");
349 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700350 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700351 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800352 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800353 fprintf(out, " event << code;\n\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800354 for (vector<java_type_t>::const_iterator arg = signature.begin();
355 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800356 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
357 for (const auto &chainField : attributionDecl.fields) {
358 if (chainField.javaType == JAVA_TYPE_STRING) {
359 fprintf(out, " if (%s_length != %s.size()) {\n",
360 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700361 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800362 fprintf(out, " }\n");
363 }
364 }
365 fprintf(out, "\n event.begin();\n");
366 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
367 attributionDecl.fields.front().name.c_str());
368 fprintf(out, " event.begin();\n");
369 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800370 if (chainField.javaType == JAVA_TYPE_STRING) {
371 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
372 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
373 fprintf(out, " } else {\n");
374 fprintf(out, " event << \"\";\n");
375 fprintf(out, " }\n");
376 } else {
377 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
378 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800379 }
380 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000381 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800382 fprintf(out, " event.end();\n\n");
Yangster-mace124e422018-08-16 10:30:28 -0700383 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
384 fprintf(out, " event.begin();\n\n");
385 fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
386 fprintf(out, " event.begin();\n");
387 fprintf(out, " event << it.first;\n");
388 fprintf(out, " event << it.second;\n");
389 fprintf(out, " event.end();\n");
390 fprintf(out, " }\n");
391
392 fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
393 fprintf(out, " event.begin();\n");
394 fprintf(out, " event << it.first;\n");
395 fprintf(out, " event << it.second;\n");
396 fprintf(out, " event.end();\n");
397 fprintf(out, " }\n");
398
399 fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
400 fprintf(out, " event.begin();\n");
401 fprintf(out, " event << it.first;\n");
402 fprintf(out, " event << it.second;\n");
403 fprintf(out, " event.end();\n");
404 fprintf(out, " }\n");
405
Howard Ro4078dd42018-09-27 17:41:08 -0700406 fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
407 fprintf(out, " event.begin();\n");
408 fprintf(out, " event << it.first;\n");
409 fprintf(out, " event << it.second;\n");
410 fprintf(out, " event.end();\n");
411 fprintf(out, " }\n");
412
Yangster-mace124e422018-08-16 10:30:28 -0700413 fprintf(out, " event.end();\n\n");
Yao Chen1fe9f592018-12-06 10:34:25 -0800414 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
415 fprintf(out,
Yao Chene89572c2019-01-09 15:41:50 -0800416 " event.AppendCharArray(arg%d.arg, "
417 "arg%d.arg_length);\n",
Yao Chen1fe9f592018-12-06 10:34:25 -0800418 argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800419 } else {
420 if (*arg == JAVA_TYPE_STRING) {
421 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
422 fprintf(out, " arg%d = \"\";\n", argIndex);
423 fprintf(out, " }\n");
424 }
425 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000426 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000427 argIndex++;
428 }
429
Yao Chen97e21ec2018-03-29 11:00:38 -0700430 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700431 fprintf(out, " } else {\n");
432 fprintf(out, " return 1;\n");
433 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000434 fprintf(out, "}\n");
435 fprintf(out, "\n");
436 }
437
Tej Singh810eeb32019-03-07 19:08:52 -0800438 for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
439 signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
440 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
441 continue;
442 }
443 vector<java_type_t> signature = signature_to_modules_it->first;
Yangster-macb8382a12018-04-04 10:39:12 -0700444 int argIndex;
445
Yangster-mace124e422018-08-16 10:30:28 -0700446 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700447 fprintf(out, "stats_write(int32_t code");
448 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800449 for (vector<java_type_t>::const_iterator arg = signature.begin();
450 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700451 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
452 for (auto chainField : attributionDecl.fields) {
453 if (chainField.javaType == JAVA_TYPE_STRING) {
454 fprintf(out, ", const std::vector<%s>& %s",
455 cpp_type_name(chainField.javaType),
456 chainField.name.c_str());
457 } else {
458 fprintf(out, ", const %s* %s, size_t %s_length",
459 cpp_type_name(chainField.javaType),
460 chainField.name.c_str(), chainField.name.c_str());
461 }
462 }
Yangster-mace124e422018-08-16 10:30:28 -0700463 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Yao Chen1fe9f592018-12-06 10:34:25 -0800464 fprintf(out,
465 ", const std::map<int, int32_t>& arg%d_1, "
466 "const std::map<int, int64_t>& arg%d_2, "
467 "const std::map<int, char const*>& arg%d_3, "
468 "const std::map<int, float>& arg%d_4",
469 argIndex, argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700470 } else {
471 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
472 }
473 argIndex++;
474 }
475 fprintf(out, ")\n");
476
477 fprintf(out, "{\n");
478 fprintf(out, " int ret = 0;\n");
479
Yangster-macca5c0862018-04-09 22:39:53 -0700480 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700481 fprintf(out, " ret = try_stats_write(code");
482
483 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800484 for (vector<java_type_t>::const_iterator arg = signature.begin();
485 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700486 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
487 for (auto chainField : attributionDecl.fields) {
488 if (chainField.javaType == JAVA_TYPE_STRING) {
489 fprintf(out, ", %s",
490 chainField.name.c_str());
491 } else {
492 fprintf(out, ", %s, %s_length",
493 chainField.name.c_str(), chainField.name.c_str());
494 }
495 }
Yao Chen1fe9f592018-12-06 10:34:25 -0800496 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
497 fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex,
498 argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700499 } else {
500 fprintf(out, ", arg%d", argIndex);
501 }
502 argIndex++;
503 }
504 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700505 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700506
507 fprintf(out, " {\n");
508 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
509 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
510 "kMinRetryIntervalNs) break;\n");
511 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
512 fprintf(out, " }\n");
513 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700514 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700515 fprintf(out, " if (ret < 0) {\n");
Yao Chen49d7dd72019-03-26 14:02:11 -0700516 fprintf(out, " note_log_drop(ret, code);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700517 fprintf(out, " }\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700518 fprintf(out, " return ret;\n");
519 fprintf(out, "}\n");
520 fprintf(out, "\n");
521 }
522
Tej Singh810eeb32019-03-07 19:08:52 -0800523 for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
524 signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
525 if (!signature_needed_for_module(signature_it->second, moduleName)) {
526 continue;
527 }
528 vector<java_type_t> signature = signature_it->first;
Yangster-macba5b9e42018-01-10 21:31:59 -0800529 int argIndex;
530
Yao Chen97e21ec2018-03-29 11:00:38 -0700531 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700532 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800533 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800534 for (vector<java_type_t>::const_iterator arg = signature.begin();
535 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800536 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
537 argIndex++;
538 }
539 fprintf(out, ")\n");
540
541 fprintf(out, "{\n");
542 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700543 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700544 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800545 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800546 fprintf(out, " event << code;\n\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800547 for (vector<java_type_t>::const_iterator arg = signature.begin();
548 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800549 if (argIndex == 1) {
550 fprintf(out, " event.begin();\n\n");
551 fprintf(out, " event.begin();\n");
552 }
553 if (*arg == JAVA_TYPE_STRING) {
554 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
555 fprintf(out, " arg%d = \"\";\n", argIndex);
556 fprintf(out, " }\n");
557 }
Yao Chene89572c2019-01-09 15:41:50 -0800558 if (*arg == JAVA_TYPE_BYTE_ARRAY) {
559 fprintf(out,
560 " event.AppendCharArray(arg%d.arg, "
561 "arg%d.arg_length);",
562 argIndex, argIndex);
563 } else {
564 fprintf(out, " event << arg%d;\n", argIndex);
565 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800566 if (argIndex == 2) {
567 fprintf(out, " event.end();\n\n");
568 fprintf(out, " event.end();\n\n");
569 }
570 argIndex++;
571 }
572
Yao Chen97e21ec2018-03-29 11:00:38 -0700573 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700574 fprintf(out, " } else {\n");
575 fprintf(out, " return 1;\n");
576 fprintf(out, " }\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800577 fprintf(out, "}\n");
578 fprintf(out, "\n");
579 }
Yangster-macb8382a12018-04-04 10:39:12 -0700580
Tej Singh810eeb32019-03-07 19:08:52 -0800581 for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
582 signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
583 if (!signature_needed_for_module(signature_it->second, moduleName)) {
584 continue;
585 }
586 vector<java_type_t> signature = signature_it->first;
Yangster-macb8382a12018-04-04 10:39:12 -0700587 int argIndex;
588
589 fprintf(out, "int\n");
590 fprintf(out, "stats_write_non_chained(int32_t code");
591 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800592 for (vector<java_type_t>::const_iterator arg = signature.begin();
593 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700594 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
595 argIndex++;
596 }
597 fprintf(out, ")\n");
598
599 fprintf(out, "{\n");
600
601 fprintf(out, " int ret = 0;\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700602 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700603 fprintf(out, " ret = try_stats_write_non_chained(code");
604
605 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800606 for (vector<java_type_t>::const_iterator arg = signature.begin();
607 arg != signature.end(); arg++) {
Yangster-macb8382a12018-04-04 10:39:12 -0700608 fprintf(out, ", arg%d", argIndex);
609 argIndex++;
610 }
611 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700612 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700613
614 fprintf(out, " {\n");
615 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
616 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
617 "kMinRetryIntervalNs) break;\n");
618 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
619 fprintf(out, " }\n");
620
621 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700622 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700623 fprintf(out, " if (ret < 0) {\n");
Yao Chen49d7dd72019-03-26 14:02:11 -0700624 fprintf(out, " note_log_drop(ret, code);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700625 fprintf(out, " }\n");
626 fprintf(out, " return ret;\n\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700627 fprintf(out, "}\n");
628
629 fprintf(out, "\n");
630 }
631
632
Yao Chend54f9dd2017-10-17 17:37:48 +0000633 // Print footer
634 fprintf(out, "\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800635 write_closing_namespace(out, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000636
637 return 0;
638}
639
Yangster-macba5b9e42018-01-10 21:31:59 -0800640void build_non_chained_decl_map(const Atoms& atoms,
641 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
642 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
643 atom != atoms.non_chained_decls.end(); atom++) {
644 decl_map->insert(std::make_pair(atom->code, atom));
645 }
646}
647
648static void write_cpp_usage(
649 FILE* out, const string& method_name, const string& atom_code_name,
650 const AtomDecl& atom, const AtomDecl &attributionDecl) {
Yao Chene89572c2019-01-09 15:41:50 -0800651 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
652 atom_code_name.c_str());
653
Yangster-macba5b9e42018-01-10 21:31:59 -0800654 for (vector<AtomField>::const_iterator field = atom.fields.begin();
655 field != atom.fields.end(); field++) {
656 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
657 for (auto chainField : attributionDecl.fields) {
658 if (chainField.javaType == JAVA_TYPE_STRING) {
659 fprintf(out, ", const std::vector<%s>& %s",
660 cpp_type_name(chainField.javaType),
661 chainField.name.c_str());
662 } else {
663 fprintf(out, ", const %s* %s, size_t %s_length",
664 cpp_type_name(chainField.javaType),
665 chainField.name.c_str(), chainField.name.c_str());
666 }
667 }
Yangster-mace124e422018-08-16 10:30:28 -0700668 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700669 fprintf(out, ", const std::map<int, int32_t>& %s_int"
670 ", const std::map<int, int64_t>& %s_long"
Yangster-mace124e422018-08-16 10:30:28 -0700671 ", const std::map<int, char const*>& %s_str"
672 ", const std::map<int, float>& %s_float",
Howard Ro4078dd42018-09-27 17:41:08 -0700673 field->name.c_str(),
674 field->name.c_str(),
675 field->name.c_str(),
676 field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800677 } else {
678 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
679 }
680 }
681 fprintf(out, ");\n");
682}
683
684static void write_cpp_method_header(
Tej Singh810eeb32019-03-07 19:08:52 -0800685 FILE* out,
686 const string& method_name,
687 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
688 const AtomDecl &attributionDecl, const string& moduleName) {
689
690 for (auto signature_to_modules_it = signatures_to_modules.begin();
691 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
692 // Skip if this signature is not needed for the module.
693 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
694 continue;
695 }
696
697 vector<java_type_t> signature = signature_to_modules_it->first;
Yangster-mace124e422018-08-16 10:30:28 -0700698 fprintf(out, "int %s(int32_t code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800699 int argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800700 for (vector<java_type_t>::const_iterator arg = signature.begin();
701 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800702 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
703 for (auto chainField : attributionDecl.fields) {
704 if (chainField.javaType == JAVA_TYPE_STRING) {
705 fprintf(out, ", const std::vector<%s>& %s",
706 cpp_type_name(chainField.javaType), chainField.name.c_str());
707 } else {
708 fprintf(out, ", const %s* %s, size_t %s_length",
709 cpp_type_name(chainField.javaType),
710 chainField.name.c_str(), chainField.name.c_str());
711 }
712 }
Yangster-mace124e422018-08-16 10:30:28 -0700713 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700714 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
715 "const std::map<int, int64_t>& arg%d_2, "
716 "const std::map<int, char const*>& arg%d_3, "
717 "const std::map<int, float>& arg%d_4",
718 argIndex, argIndex, argIndex, argIndex);
Yangster-macba5b9e42018-01-10 21:31:59 -0800719 } else {
720 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
721 }
722 argIndex++;
723 }
724 fprintf(out, ");\n");
725
726 }
727}
Yao Chend54f9dd2017-10-17 17:37:48 +0000728
729static int
Tej Singh810eeb32019-03-07 19:08:52 -0800730write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
731 const string& moduleName, const string& cppNamespace)
Yao Chend54f9dd2017-10-17 17:37:48 +0000732{
Yao Chend54f9dd2017-10-17 17:37:48 +0000733 // Print prelude
734 fprintf(out, "// This file is autogenerated\n");
735 fprintf(out, "\n");
736 fprintf(out, "#pragma once\n");
737 fprintf(out, "\n");
738 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800739 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800740 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800741 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000742 fprintf(out, "\n");
743
Tej Singh810eeb32019-03-07 19:08:52 -0800744 write_namespace(out, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000745 fprintf(out, "\n");
746 fprintf(out, "/*\n");
747 fprintf(out, " * API For logging statistics events.\n");
748 fprintf(out, " */\n");
749 fprintf(out, "\n");
750 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700751 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000752 fprintf(out, " */\n");
753 fprintf(out, "enum {\n");
754
Yangster-macba5b9e42018-01-10 21:31:59 -0800755 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
756 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
757
Yao Chend54f9dd2017-10-17 17:37:48 +0000758 size_t i = 0;
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800759 // Print atom constants
Yao Chend54f9dd2017-10-17 17:37:48 +0000760 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800761 atom != atoms.decls.end(); atom++) {
Tej Singh810eeb32019-03-07 19:08:52 -0800762 // Skip if the atom is not needed for the module.
763 if (!atom_needed_for_module(*atom, moduleName)) {
764 continue;
765 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000766 string constant = make_constant_name(atom->name);
767 fprintf(out, "\n");
768 fprintf(out, " /**\n");
769 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800770 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
771
772 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
773 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
774 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
775 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000776 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000777 fprintf(out, " */\n");
778 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
779 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800780 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
781 maxPushedAtomId = atom->code;
782 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000783 i++;
784 }
785 fprintf(out, "\n");
786 fprintf(out, "};\n");
787 fprintf(out, "\n");
788
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800789 // Print constants for the enum values.
790 fprintf(out, "//\n");
791 fprintf(out, "// Constants for enum values\n");
792 fprintf(out, "//\n\n");
793 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
794 atom != atoms.decls.end(); atom++) {
Tej Singh810eeb32019-03-07 19:08:52 -0800795 // Skip if the atom is not needed for the module.
796 if (!atom_needed_for_module(*atom, moduleName)) {
797 continue;
798 }
799
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800800 for (vector<AtomField>::const_iterator field = atom->fields.begin();
801 field != atom->fields.end(); field++) {
802 if (field->javaType == JAVA_TYPE_ENUM) {
803 fprintf(out, "// Values for %s.%s\n", atom->message.c_str(),
804 field->name.c_str());
805 for (map<int, string>::const_iterator value = field->enumValues.begin();
806 value != field->enumValues.end(); value++) {
807 fprintf(out, "const int32_t %s__%s__%s = %d;\n",
808 make_constant_name(atom->message).c_str(),
809 make_constant_name(field->name).c_str(),
810 make_constant_name(value->second).c_str(),
811 value->first);
812 }
813 fprintf(out, "\n");
814 }
815 }
816 }
817
Yao Chene89572c2019-01-09 15:41:50 -0800818 fprintf(out, "struct BytesField {\n");
819 fprintf(out,
820 " BytesField(char const* array, size_t len) : arg(array), "
821 "arg_length(len) {}\n");
822 fprintf(out, " char const* arg;\n");
823 fprintf(out, " size_t arg_length;\n");
824 fprintf(out, "};\n");
825 fprintf(out, "\n");
826
Tej Singh810eeb32019-03-07 19:08:52 -0800827 // This metadata is only used by statsd, which uses the default libstatslog.
828 if (moduleName == DEFAULT_MODULE_NAME) {
Yao Chenc40a19d2018-03-15 16:48:25 -0700829
Tej Singh810eeb32019-03-07 19:08:52 -0800830 fprintf(out, "struct StateAtomFieldOptions {\n");
831 fprintf(out, " std::vector<int> primaryFields;\n");
832 fprintf(out, " int exclusiveField;\n");
833 fprintf(out, "};\n");
834 fprintf(out, "\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800835
Tej Singh810eeb32019-03-07 19:08:52 -0800836 fprintf(out, "struct AtomsInfo {\n");
837 fprintf(out,
838 " const static std::set<int> "
Tej Singh29ac6012019-06-05 18:10:12 -0700839 "kTruncatingTimestampAtomBlackList;\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800840 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
841 fprintf(out,
842 " const static std::set<int> kAtomsWithAttributionChain;\n");
843 fprintf(out,
844 " const static std::map<int, StateAtomFieldOptions> "
845 "kStateAtomsFieldOptions;\n");
846 fprintf(out,
847 " const static std::map<int, std::vector<int>> "
848 "kBytesFieldAtoms;");
849 fprintf(out,
850 " const static std::set<int> kWhitelistedAtoms;\n");
851 fprintf(out, "};\n");
852
853 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
854 maxPushedAtomId);
855 }
Yao Chen9c1debe2018-02-19 14:39:19 -0800856
Yao Chend54f9dd2017-10-17 17:37:48 +0000857 // Print write methods
858 fprintf(out, "//\n");
859 fprintf(out, "// Write methods\n");
860 fprintf(out, "//\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800861 write_cpp_method_header(out, "stats_write", atoms.signatures_to_modules, attributionDecl,
862 moduleName);
Yangster-macba5b9e42018-01-10 21:31:59 -0800863
864 fprintf(out, "//\n");
865 fprintf(out, "// Write flattened methods\n");
866 fprintf(out, "//\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800867 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures_to_modules,
868 attributionDecl, moduleName);
Yao Chend54f9dd2017-10-17 17:37:48 +0000869
870 fprintf(out, "\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800871 write_closing_namespace(out, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000872
873 return 0;
874}
875
Bookatz0bd97202018-06-05 12:42:37 -0700876static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
877 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800878 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
879 method_name.c_str(), atom_code_name.c_str());
880 for (vector<AtomField>::const_iterator field = atom.fields.begin();
881 field != atom.fields.end(); field++) {
882 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700883 fprintf(out, ", android.os.WorkSource workSource");
Yangster-mace124e422018-08-16 10:30:28 -0700884 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
885 fprintf(out, ", SparseArray<Object> value_map");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700886 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
887 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800888 } else {
889 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
890 }
891 }
Bookatz0bd97202018-06-05 12:42:37 -0700892 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800893}
894
895static void write_java_method(
Tej Singh810eeb32019-03-07 19:08:52 -0800896 FILE* out,
897 const string& method_name,
898 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
899 const AtomDecl &attributionDecl) {
900
901 for (auto signature_to_modules_it = signatures_to_modules.begin();
902 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
903 vector<java_type_t> signature = signature_to_modules_it->first;
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800904 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700905 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800906 int argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -0800907 for (vector<java_type_t>::const_iterator arg = signature.begin();
908 arg != signature.end(); arg++) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800909 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
910 for (auto chainField : attributionDecl.fields) {
911 fprintf(out, ", %s[] %s",
912 java_type_name(chainField.javaType), chainField.name.c_str());
913 }
Yangster-mace124e422018-08-16 10:30:28 -0700914 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
915 fprintf(out, ", SparseArray<Object> value_map");
Yangster-macba5b9e42018-01-10 21:31:59 -0800916 } else {
917 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
918 }
919 argIndex++;
920 }
921 fprintf(out, ");\n");
922 }
923}
924
Tej Singh2910d5a2019-03-23 17:26:32 -0700925static void write_java_helpers_for_module(
926 FILE * out,
927 const AtomDecl &attributionDecl,
928 const int requiredHelpers) {
929 fprintf(out, " private static void copyInt(byte[] buff, int pos, int val) {\n");
930 fprintf(out, " buff[pos] = (byte) (val);\n");
931 fprintf(out, " buff[pos + 1] = (byte) (val >> 8);\n");
932 fprintf(out, " buff[pos + 2] = (byte) (val >> 16);\n");
933 fprintf(out, " buff[pos + 3] = (byte) (val >> 24);\n");
934 fprintf(out, " return;\n");
935 fprintf(out, " }\n");
936 fprintf(out, "\n");
937
938 fprintf(out, " private static void copyLong(byte[] buff, int pos, long val) {\n");
939 fprintf(out, " buff[pos] = (byte) (val);\n");
940 fprintf(out, " buff[pos + 1] = (byte) (val >> 8);\n");
941 fprintf(out, " buff[pos + 2] = (byte) (val >> 16);\n");
942 fprintf(out, " buff[pos + 3] = (byte) (val >> 24);\n");
943 fprintf(out, " buff[pos + 4] = (byte) (val >> 32);\n");
944 fprintf(out, " buff[pos + 5] = (byte) (val >> 40);\n");
945 fprintf(out, " buff[pos + 6] = (byte) (val >> 48);\n");
946 fprintf(out, " buff[pos + 7] = (byte) (val >> 56);\n");
947 fprintf(out, " return;\n");
948 fprintf(out, " }\n");
949 fprintf(out, "\n");
950
951 if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) {
952 fprintf(out, " private static void copyFloat(byte[] buff, int pos, float val) {\n");
953 fprintf(out, " copyInt(buff, pos, Float.floatToIntBits(val));\n");
954 fprintf(out, " return;\n");
955 fprintf(out, " }\n");
956 fprintf(out, "\n");
957 }
958
959 if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) {
960 fprintf(out, " private static void writeAttributionChain(byte[] buff, int pos");
961 for (auto chainField : attributionDecl.fields) {
962 fprintf(out, ", %s[] %s",
963 java_type_name(chainField.javaType), chainField.name.c_str());
964 }
965 fprintf(out, ") {\n");
966
967 const char* uidName = attributionDecl.fields.front().name.c_str();
968 const char* tagName = attributionDecl.fields.back().name.c_str();
969
970 // Write the first list begin.
971 fprintf(out, " buff[pos] = LIST_TYPE;\n");
972 fprintf(out, " buff[pos + 1] = (byte) (%s.length);\n", tagName);
973 fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n");
974
975 // Iterate through the attribution chain and write the nodes.
976 fprintf(out, " for (int i = 0; i < %s.length; i++) {\n", tagName);
977 // Write the list begin.
978 fprintf(out, " buff[pos] = LIST_TYPE;\n");
979 fprintf(out, " buff[pos + 1] = %lu;\n", attributionDecl.fields.size());
980 fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n");
981
982 // Write the uid.
983 fprintf(out, " buff[pos] = INT_TYPE;\n");
984 fprintf(out, " copyInt(buff, pos + 1, %s[i]);\n", uidName);
985 fprintf(out, " pos += INT_TYPE_SIZE;\n");
986
987 // Write the tag.
988 fprintf(out, " String %sStr = (%s[i] == null) ? \"\" : %s[i];\n",
989 tagName, tagName, tagName);
990 fprintf(out, " byte[] %sByte = %sStr.getBytes(UTF_8);\n", tagName, tagName);
991 fprintf(out, " buff[pos] = STRING_TYPE;\n");
992 fprintf(out, " copyInt(buff, pos + 1, %sByte.length);\n", tagName);
993 fprintf(out, " System.arraycopy("
994 "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n",
995 tagName, tagName);
996 fprintf(out, " pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", tagName);
997 fprintf(out, " }\n");
998 fprintf(out, " }\n");
999 fprintf(out, "\n");
1000 }
1001}
1002
1003
1004static int write_java_non_chained_method_for_module(
1005 FILE* out,
1006 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1007 const string& moduleName
1008 ) {
1009 for (auto signature_to_modules_it = signatures_to_modules.begin();
1010 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1011 // Skip if this signature is not needed for the module.
1012 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
1013 continue;
1014 }
1015
1016 // Print method signature.
1017 vector<java_type_t> signature = signature_to_modules_it->first;
1018 fprintf(out, " public static void write_non_chained(int code");
1019 int argIndex = 1;
1020 for (vector<java_type_t>::const_iterator arg = signature.begin();
1021 arg != signature.end(); arg++) {
1022 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1023 // Non chained signatures should not have attribution chains.
1024 return 1;
1025 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1026 // Module logging does not yet support key value pair.
1027 return 1;
1028 } else {
1029 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
1030 }
1031 argIndex++;
1032 }
1033 fprintf(out, ") {\n");
1034
1035 fprintf(out, " write(code");
1036 argIndex = 1;
1037 for (vector<java_type_t>::const_iterator arg = signature.begin();
1038 arg != signature.end(); arg++) {
1039 // First two args are uid and tag of attribution chain.
1040 if (argIndex == 1) {
1041 fprintf(out, ", new int[] {arg%d}", argIndex);
1042 } else if (argIndex == 2) {
1043 fprintf(out, ", new java.lang.String[] {arg%d}", argIndex);
1044 } else {
1045 fprintf(out, ", arg%d", argIndex);
1046 }
1047 argIndex++;
1048 }
1049 fprintf(out, ");\n");
1050 fprintf(out, " }\n");
1051 fprintf(out, "\n");
1052 }
1053 return 0;
1054}
1055
1056static int write_java_method_for_module(
1057 FILE* out,
1058 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1059 const AtomDecl &attributionDecl,
1060 const string& moduleName,
1061 int* requiredHelpers
1062 ) {
1063
1064 for (auto signature_to_modules_it = signatures_to_modules.begin();
1065 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1066 // Skip if this signature is not needed for the module.
1067 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
1068 continue;
1069 }
1070
1071 // Print method signature.
1072 vector<java_type_t> signature = signature_to_modules_it->first;
1073 fprintf(out, " public static void write(int code");
1074 int argIndex = 1;
1075 for (vector<java_type_t>::const_iterator arg = signature.begin();
1076 arg != signature.end(); arg++) {
1077 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1078 for (auto chainField : attributionDecl.fields) {
1079 fprintf(out, ", %s[] %s",
1080 java_type_name(chainField.javaType), chainField.name.c_str());
1081 }
1082 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1083 // Module logging does not yet support key value pair.
1084 return 1;
1085 } else {
1086 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
1087 }
1088 argIndex++;
1089 }
1090 fprintf(out, ") {\n");
1091
1092 // Calculate the size of the buffer.
1093 fprintf(out, " // Initial overhead of the list, timestamp, and atom tag.\n");
1094 fprintf(out, " int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + INT_TYPE_SIZE;\n");
1095 argIndex = 1;
1096 for (vector<java_type_t>::const_iterator arg = signature.begin();
1097 arg != signature.end(); arg++) {
1098 switch (*arg) {
1099 case JAVA_TYPE_BOOLEAN:
1100 case JAVA_TYPE_INT:
1101 case JAVA_TYPE_FLOAT:
1102 case JAVA_TYPE_ENUM:
1103 fprintf(out, " needed += INT_TYPE_SIZE;\n");
1104 break;
1105 case JAVA_TYPE_LONG:
1106 // Longs take 9 bytes, 1 for the type and 8 for the value.
1107 fprintf(out, " needed += LONG_TYPE_SIZE;\n");
1108 break;
1109 case JAVA_TYPE_STRING:
1110 // Strings take 5 metadata bytes + length of byte encoded string.
1111 fprintf(out, " if (arg%d == null) {\n", argIndex);
1112 fprintf(out, " arg%d = \"\";\n", argIndex);
1113 fprintf(out, " }\n");
1114 fprintf(out, " byte[] arg%dBytes= arg%d.getBytes(UTF_8);\n",
1115 argIndex, argIndex);
1116 fprintf(out, " needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
1117 argIndex);
1118 break;
1119 case JAVA_TYPE_BYTE_ARRAY:
1120 // Byte arrays take 5 metadata bytes + length of byte array.
1121 fprintf(out, " if (arg%d == null) {\n", argIndex);
1122 fprintf(out, " arg%d = new byte[0];\n", argIndex);
1123 fprintf(out, " }\n");
1124 fprintf(out, " needed += STRING_TYPE_OVERHEAD + arg%d.length;\n", argIndex);
1125 break;
1126 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1127 {
1128 const char* uidName = attributionDecl.fields.front().name.c_str();
1129 const char* tagName = attributionDecl.fields.back().name.c_str();
1130 // Null checks on the params.
1131 fprintf(out, " if (%s == null) {\n", uidName);
1132 fprintf(out, " %s = new %s[0];\n", uidName,
1133 java_type_name(attributionDecl.fields.front().javaType));
1134 fprintf(out, " }\n");
1135 fprintf(out, " if (%s == null) {\n", tagName);
1136 fprintf(out, " %s = new %s[0];\n", tagName,
1137 java_type_name(attributionDecl.fields.back().javaType));
1138 fprintf(out, " }\n");
1139
1140 // First check that the lengths of the uid and tag arrays are the same.
1141 fprintf(out, " if (%s.length != %s.length) {\n", uidName, tagName);
1142 fprintf(out, " return;\n");
1143 fprintf(out, " }\n");
1144 fprintf(out, " int attrSize = LIST_TYPE_OVERHEAD;\n");
1145 fprintf(out, " for (int i = 0; i < %s.length; i++) {\n", tagName);
1146 fprintf(out, " String str%d = (%s[i] == null) ? \"\" : %s[i];\n",
1147 argIndex, tagName, tagName);
1148 fprintf(out, " int str%dlen = str%d.getBytes(UTF_8).length;\n",
1149 argIndex, argIndex);
1150 fprintf(out,
1151 " attrSize += "
1152 "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + str%dlen;\n",
1153 argIndex);
1154 fprintf(out, " }\n");
1155 fprintf(out, " needed += attrSize;\n");
1156 break;
1157 }
1158 default:
1159 // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR.
1160 return 1;
1161 }
1162 argIndex++;
1163 }
1164
1165 // Now we have the size that is needed. Check for overflow and return if needed.
1166 fprintf(out, " if (needed > MAX_EVENT_PAYLOAD) {\n");
1167 fprintf(out, " return;\n");
1168 fprintf(out, " }\n");
1169
1170 // Create new buffer, and associated data types.
1171 fprintf(out, " byte[] buff = new byte[needed];\n");
1172 fprintf(out, " int pos = 0;\n");
1173
1174 // Initialize the buffer with list data type.
1175 fprintf(out, " buff[pos] = LIST_TYPE;\n");
Greg Kaiser242c20e2019-04-08 06:25:21 -07001176 fprintf(out, " buff[pos + 1] = %zu;\n", signature.size() + 2);
Tej Singh2910d5a2019-03-23 17:26:32 -07001177 fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n");
1178
1179 // Write timestamp.
1180 fprintf(out, " long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n");
1181 fprintf(out, " buff[pos] = LONG_TYPE;\n");
1182 fprintf(out, " copyLong(buff, pos + 1, elapsedRealtime);\n");
1183 fprintf(out, " pos += LONG_TYPE_SIZE;\n");
1184
1185 // Write atom code.
1186 fprintf(out, " buff[pos] = INT_TYPE;\n");
1187 fprintf(out, " copyInt(buff, pos + 1, code);\n");
1188 fprintf(out, " pos += INT_TYPE_SIZE;\n");
1189
1190 // Write the args.
1191 argIndex = 1;
1192 for (vector<java_type_t>::const_iterator arg = signature.begin();
1193 arg != signature.end(); arg++) {
1194 switch (*arg) {
1195 case JAVA_TYPE_BOOLEAN:
1196 fprintf(out, " buff[pos] = INT_TYPE;\n");
1197 fprintf(out, " copyInt(buff, pos + 1, arg%d? 1 : 0);\n", argIndex);
1198 fprintf(out, " pos += INT_TYPE_SIZE;\n");
1199 break;
1200 case JAVA_TYPE_INT:
1201 case JAVA_TYPE_ENUM:
1202 fprintf(out, " buff[pos] = INT_TYPE;\n");
1203 fprintf(out, " copyInt(buff, pos + 1, arg%d);\n", argIndex);
1204 fprintf(out, " pos += INT_TYPE_SIZE;\n");
1205 break;
1206 case JAVA_TYPE_FLOAT:
1207 *requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT;
1208 fprintf(out, " buff[pos] = FLOAT_TYPE;\n");
1209 fprintf(out, " copyFloat(buff, pos + 1, arg%d);\n", argIndex);
1210 fprintf(out, " pos += FLOAT_TYPE_SIZE;\n");
1211 break;
1212 case JAVA_TYPE_LONG:
1213 fprintf(out, " buff[pos] = LONG_TYPE;\n");
1214 fprintf(out, " copyLong(buff, pos + 1, arg%d);\n", argIndex);
1215 fprintf(out, " pos += LONG_TYPE_SIZE;\n");
1216 break;
1217 case JAVA_TYPE_STRING:
1218 fprintf(out, " buff[pos] = STRING_TYPE;\n");
1219 fprintf(out, " copyInt(buff, pos + 1, arg%dBytes.length);\n", argIndex);
1220 fprintf(out, " System.arraycopy("
1221 "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%dBytes.length);\n",
1222 argIndex, argIndex);
1223 fprintf(out, " pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
1224 argIndex);
1225 break;
1226 case JAVA_TYPE_BYTE_ARRAY:
1227 fprintf(out, " buff[pos] = STRING_TYPE;\n");
1228 fprintf(out, " copyInt(buff, pos + 1, arg%d.length);\n", argIndex);
1229 fprintf(out, " System.arraycopy("
1230 "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n",
1231 argIndex, argIndex);
1232 fprintf(out, " pos += STRING_TYPE_OVERHEAD + arg%d.length;\n", argIndex);
1233 break;
1234 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1235 {
1236 *requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION;
1237 const char* uidName = attributionDecl.fields.front().name.c_str();
1238 const char* tagName = attributionDecl.fields.back().name.c_str();
1239
1240 fprintf(out, " writeAttributionChain(buff, pos, %s, %s);\n",
1241 uidName, tagName);
1242 fprintf(out, " pos += attrSize;\n");
1243 break;
1244 }
1245 default:
1246 // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR.
1247 return 1;
1248 }
1249 argIndex++;
1250 }
1251
1252 fprintf(out, " StatsLog.writeRaw(buff, pos);\n");
1253 fprintf(out, " }\n");
1254 fprintf(out, "\n");
1255 }
1256 return 0;
1257}
1258
Tej Singh810eeb32019-03-07 19:08:52 -08001259static void write_java_work_source_method(FILE* out,
Tej Singh2910d5a2019-03-23 17:26:32 -07001260 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1261 const string& moduleName) {
Bookatz0bd97202018-06-05 12:42:37 -07001262 fprintf(out, "\n // WorkSource methods.\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001263 for (auto signature_to_modules_it = signatures_to_modules.begin();
1264 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
Tej Singh2910d5a2019-03-23 17:26:32 -07001265 // Skip if this signature is not needed for the module.
1266 if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
1267 continue;
1268 }
Tej Singh810eeb32019-03-07 19:08:52 -08001269 vector<java_type_t> signature = signature_to_modules_it->first;
Bookatz0bd97202018-06-05 12:42:37 -07001270 // Determine if there is Attribution in this signature.
1271 int attributionArg = -1;
1272 int argIndexMax = 0;
Tej Singh810eeb32019-03-07 19:08:52 -08001273 for (vector<java_type_t>::const_iterator arg = signature.begin();
1274 arg != signature.end(); arg++) {
Bookatz0bd97202018-06-05 12:42:37 -07001275 argIndexMax++;
1276 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1277 if (attributionArg > -1) {
1278 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
1279 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
1280 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
1281 return;
1282 }
1283 attributionArg = argIndexMax;
1284 }
1285 }
1286 if (attributionArg < 0) {
1287 continue;
1288 }
1289
1290 // Method header (signature)
Tej Singh2910d5a2019-03-23 17:26:32 -07001291 if (moduleName == DEFAULT_MODULE_NAME) {
1292 fprintf(out, " /** @hide */\n");
1293 }
Bookatz0bd97202018-06-05 12:42:37 -07001294 fprintf(out, " public static void write(int code");
1295 int argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -08001296 for (vector<java_type_t>::const_iterator arg = signature.begin();
1297 arg != signature.end(); arg++) {
Bookatz0bd97202018-06-05 12:42:37 -07001298 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1299 fprintf(out, ", WorkSource ws");
1300 } else {
1301 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
1302 }
1303 argIndex++;
1304 }
1305 fprintf(out, ") {\n");
1306
1307 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
1308 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
1309 fprintf(out, " write_non_chained(code");
1310 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
1311 if (argIndex == attributionArg) {
1312 fprintf(out, ", ws.get(i), ws.getName(i)");
1313 } else {
1314 fprintf(out, ", arg%d", argIndex);
1315 }
1316 }
1317 fprintf(out, ");\n");
Tej Singh2910d5a2019-03-23 17:26:32 -07001318 fprintf(out, " }\n"); // close for-loop
Bookatz0bd97202018-06-05 12:42:37 -07001319
1320 // write() component.
1321 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
1322 fprintf(out, " if (workChains != null) {\n");
1323 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
1324 fprintf(out, " write(code");
1325 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
1326 if (argIndex == attributionArg) {
1327 fprintf(out, ", wc.getUids(), wc.getTags()");
1328 } else {
1329 fprintf(out, ", arg%d", argIndex);
1330 }
1331 }
1332 fprintf(out, ");\n");
1333 fprintf(out, " }\n"); // close for-loop
1334 fprintf(out, " }\n"); // close if
1335 fprintf(out, " }\n"); // close method
1336 }
1337}
Yangster-macba5b9e42018-01-10 21:31:59 -08001338
Tej Singh2910d5a2019-03-23 17:26:32 -07001339static void write_java_atom_codes(FILE* out, const Atoms& atoms, const string& moduleName) {
1340 fprintf(out, " // Constants for atom codes.\n");
1341
1342 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
1343 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
1344
1345 // Print constants for the atom codes.
1346 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
1347 atom != atoms.decls.end(); atom++) {
1348 // Skip if the atom is not needed for the module.
1349 if (!atom_needed_for_module(*atom, moduleName)) {
1350 continue;
1351 }
1352 string constant = make_constant_name(atom->name);
1353 fprintf(out, "\n");
1354 fprintf(out, " /**\n");
1355 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
1356 write_java_usage(out, "write", constant, *atom);
1357 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
1358 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
1359 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
1360 }
1361 if (moduleName == DEFAULT_MODULE_NAME) {
1362 fprintf(out, " * @hide\n");
1363 }
1364 fprintf(out, " */\n");
1365 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
1366 }
1367 fprintf(out, "\n");
1368}
1369
1370static void write_java_enum_values(FILE* out, const Atoms& atoms, const string& moduleName) {
1371 fprintf(out, " // Constants for enum values.\n\n");
1372 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
1373 atom != atoms.decls.end(); atom++) {
1374 // Skip if the atom is not needed for the module.
1375 if (!atom_needed_for_module(*atom, moduleName)) {
1376 continue;
1377 }
1378 for (vector<AtomField>::const_iterator field = atom->fields.begin();
1379 field != atom->fields.end(); field++) {
1380 if (field->javaType == JAVA_TYPE_ENUM) {
1381 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
1382 field->name.c_str());
1383 for (map<int, string>::const_iterator value = field->enumValues.begin();
1384 value != field->enumValues.end(); value++) {
1385 if (moduleName == DEFAULT_MODULE_NAME) {
1386 fprintf(out, " /** @hide */\n");
1387 }
1388 fprintf(out, " public static final int %s__%s__%s = %d;\n",
1389 make_constant_name(atom->message).c_str(),
1390 make_constant_name(field->name).c_str(),
1391 make_constant_name(value->second).c_str(),
1392 value->first);
1393 }
1394 fprintf(out, "\n");
1395 }
1396 }
1397 }
1398}
1399
Yao Chend54f9dd2017-10-17 17:37:48 +00001400static int
Yangster-mac7604aea2017-12-11 22:55:49 -08001401write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001402{
Yao Chend54f9dd2017-10-17 17:37:48 +00001403 // Print prelude
1404 fprintf(out, "// This file is autogenerated\n");
1405 fprintf(out, "\n");
1406 fprintf(out, "package android.util;\n");
1407 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -07001408 fprintf(out, "import android.os.WorkSource;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001409 fprintf(out, "import android.util.SparseArray;\n");
Bookatz0bd97202018-06-05 12:42:37 -07001410 fprintf(out, "import java.util.ArrayList;\n");
1411 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001412 fprintf(out, "\n");
1413 fprintf(out, "/**\n");
1414 fprintf(out, " * API For logging statistics events.\n");
1415 fprintf(out, " * @hide\n");
1416 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -08001417 fprintf(out, "public class StatsLogInternal {\n");
Tej Singh2910d5a2019-03-23 17:26:32 -07001418 write_java_atom_codes(out, atoms, DEFAULT_MODULE_NAME);
Yao Chend54f9dd2017-10-17 17:37:48 +00001419
Tej Singh2910d5a2019-03-23 17:26:32 -07001420 write_java_enum_values(out, atoms, DEFAULT_MODULE_NAME);
Stefan Lafon9478f352017-10-30 21:20:20 -07001421
Yao Chend54f9dd2017-10-17 17:37:48 +00001422 // Print write methods
1423 fprintf(out, " // Write methods\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001424 write_java_method(out, "write", atoms.signatures_to_modules, attributionDecl);
1425 write_java_method(out, "write_non_chained", atoms.non_chained_signatures_to_modules,
1426 attributionDecl);
Tej Singh2910d5a2019-03-23 17:26:32 -07001427 write_java_work_source_method(out, atoms.signatures_to_modules, DEFAULT_MODULE_NAME);
Yao Chend54f9dd2017-10-17 17:37:48 +00001428
1429 fprintf(out, "}\n");
1430
1431 return 0;
1432}
1433
Tej Singh2910d5a2019-03-23 17:26:32 -07001434// TODO: Merge this with write_stats_log_java so that we can get rid of StatsLogInternal JNI.
1435static int
1436write_stats_log_java_for_module(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
1437 const string& moduleName, const string& javaClass, const string& javaPackage)
1438{
1439 // Print prelude
1440 fprintf(out, "// This file is autogenerated\n");
1441 fprintf(out, "\n");
1442 fprintf(out, "package %s;\n", javaPackage.c_str());
1443 fprintf(out, "\n");
1444 fprintf(out, "import static java.nio.charset.StandardCharsets.UTF_8;\n");
1445 fprintf(out, "\n");
1446 fprintf(out, "import android.util.StatsLog;\n");
1447 fprintf(out, "import android.os.SystemClock;\n");
1448 fprintf(out, "\n");
1449 fprintf(out, "import java.util.ArrayList;\n");
1450 fprintf(out, "\n");
1451 fprintf(out, "\n");
1452 fprintf(out, "/**\n");
1453 fprintf(out, " * Utility class for logging statistics events.\n");
1454 fprintf(out, " */\n");
1455 fprintf(out, "public class %s {\n", javaClass.c_str());
1456
1457 // TODO: ideally these match with the native values (and automatically change if they change).
1458 fprintf(out, " private static final int LOGGER_ENTRY_MAX_PAYLOAD = 4068;\n");
1459 fprintf(out,
1460 " private static final int MAX_EVENT_PAYLOAD = LOGGER_ENTRY_MAX_PAYLOAD - 4;\n");
1461 // Value types. Must match with EventLog.java and log.h.
1462 fprintf(out, " private static final byte INT_TYPE = 0;\n");
1463 fprintf(out, " private static final byte LONG_TYPE = 1;\n");
1464 fprintf(out, " private static final byte STRING_TYPE = 2;\n");
1465 fprintf(out, " private static final byte LIST_TYPE = 3;\n");
1466 fprintf(out, " private static final byte FLOAT_TYPE = 4;\n");
1467
1468 // Size of each value type.
1469 // Booleans, ints, floats, and enums take 5 bytes, 1 for the type and 4 for the value.
1470 fprintf(out, " private static final int INT_TYPE_SIZE = 5;\n");
1471 fprintf(out, " private static final int FLOAT_TYPE_SIZE = 5;\n");
1472 // Longs take 9 bytes, 1 for the type and 8 for the value.
1473 fprintf(out, " private static final int LONG_TYPE_SIZE = 9;\n");
1474 // Strings take 5 metadata bytes: 1 byte is for the type, 4 are for the length.
1475 fprintf(out, " private static final int STRING_TYPE_OVERHEAD = 5;\n");
1476 fprintf(out, " private static final int LIST_TYPE_OVERHEAD = 2;\n");
1477
1478 write_java_atom_codes(out, atoms, moduleName);
1479
1480 write_java_enum_values(out, atoms, moduleName);
1481
1482 int errors = 0;
1483 int requiredHelpers = 0;
1484 // Print write methods
1485 fprintf(out, " // Write methods\n");
1486 errors += write_java_method_for_module(out, atoms.signatures_to_modules, attributionDecl,
1487 moduleName, &requiredHelpers);
1488 errors += write_java_non_chained_method_for_module(out, atoms.non_chained_signatures_to_modules,
1489 moduleName);
1490
1491 fprintf(out, " // Helper methods for copying primitives\n");
1492 write_java_helpers_for_module(out, attributionDecl, requiredHelpers);
1493
1494 fprintf(out, "}\n");
1495
1496 return errors;
1497}
1498
Yao Chend54f9dd2017-10-17 17:37:48 +00001499static const char*
1500jni_type_name(java_type_t type)
1501{
1502 switch (type) {
1503 case JAVA_TYPE_BOOLEAN:
1504 return "jboolean";
1505 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001506 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001507 return "jint";
1508 case JAVA_TYPE_LONG:
1509 return "jlong";
1510 case JAVA_TYPE_FLOAT:
1511 return "jfloat";
1512 case JAVA_TYPE_DOUBLE:
1513 return "jdouble";
1514 case JAVA_TYPE_STRING:
1515 return "jstring";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001516 case JAVA_TYPE_BYTE_ARRAY:
1517 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +00001518 default:
1519 return "UNKNOWN";
1520 }
1521}
1522
Yangster-mac7604aea2017-12-11 22:55:49 -08001523static const char*
1524jni_array_type_name(java_type_t type)
1525{
1526 switch (type) {
1527 case JAVA_TYPE_INT:
1528 return "jintArray";
Yangster-mace124e422018-08-16 10:30:28 -07001529 case JAVA_TYPE_FLOAT:
1530 return "jfloatArray";
Yangster-mac7604aea2017-12-11 22:55:49 -08001531 case JAVA_TYPE_STRING:
1532 return "jobjectArray";
1533 default:
1534 return "UNKNOWN";
1535 }
1536}
1537
Yao Chend54f9dd2017-10-17 17:37:48 +00001538static string
Yangster-macba5b9e42018-01-10 21:31:59 -08001539jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +00001540{
Yangster-macba5b9e42018-01-10 21:31:59 -08001541 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +00001542 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001543 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001544 switch (*arg) {
1545 case JAVA_TYPE_BOOLEAN:
1546 result += "_boolean";
1547 break;
1548 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001549 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001550 result += "_int";
1551 break;
1552 case JAVA_TYPE_LONG:
1553 result += "_long";
1554 break;
1555 case JAVA_TYPE_FLOAT:
1556 result += "_float";
1557 break;
1558 case JAVA_TYPE_DOUBLE:
1559 result += "_double";
1560 break;
1561 case JAVA_TYPE_STRING:
1562 result += "_String";
1563 break;
Yangster-mac7604aea2017-12-11 22:55:49 -08001564 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1565 result += "_AttributionChain";
1566 break;
Yangster-mace124e422018-08-16 10:30:28 -07001567 case JAVA_TYPE_KEY_VALUE_PAIR:
1568 result += "_KeyValuePairs";
1569 break;
Yao Chenbbdd67d2018-10-24 12:15:56 -07001570 case JAVA_TYPE_BYTE_ARRAY:
1571 result += "_bytes";
1572 break;
Yao Chend54f9dd2017-10-17 17:37:48 +00001573 default:
1574 result += "_UNKNOWN";
1575 break;
1576 }
1577 }
1578 return result;
1579}
1580
1581static const char*
1582java_type_signature(java_type_t type)
1583{
1584 switch (type) {
1585 case JAVA_TYPE_BOOLEAN:
1586 return "Z";
1587 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001588 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001589 return "I";
1590 case JAVA_TYPE_LONG:
1591 return "J";
1592 case JAVA_TYPE_FLOAT:
1593 return "F";
1594 case JAVA_TYPE_DOUBLE:
1595 return "D";
1596 case JAVA_TYPE_STRING:
1597 return "Ljava/lang/String;";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001598 case JAVA_TYPE_BYTE_ARRAY:
1599 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +00001600 default:
1601 return "UNKNOWN";
1602 }
1603}
1604
1605static string
Yangster-mac7604aea2017-12-11 22:55:49 -08001606jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001607{
1608 string result("(I");
1609 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001610 arg != signature.end(); arg++) {
1611 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1612 for (auto chainField : attributionDecl.fields) {
1613 result += "[";
1614 result += java_type_signature(chainField.javaType);
1615 }
Yangster-mace124e422018-08-16 10:30:28 -07001616 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1617 result += "Landroid/util/SparseArray;";
Yangster-mac7604aea2017-12-11 22:55:49 -08001618 } else {
1619 result += java_type_signature(*arg);
1620 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001621 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001622 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +00001623 return result;
1624}
1625
Yangster-mace124e422018-08-16 10:30:28 -07001626static void write_key_value_map_jni(FILE* out) {
Howard Ro4078dd42018-09-27 17:41:08 -07001627 fprintf(out, " std::map<int, int32_t> int32_t_map;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001628 fprintf(out, " std::map<int, int64_t> int64_t_map;\n");
1629 fprintf(out, " std::map<int, float> float_map;\n");
1630 fprintf(out, " std::map<int, char const*> string_map;\n\n");
1631
1632 fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n");
1633
1634 fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n");
1635 fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n");
1636 fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n");
1637
1638
1639 fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n");
1640
Howard Ro4078dd42018-09-27 17:41:08 -07001641 fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001642 fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n");
1643 fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n");
1644 fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001645 fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001646 fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n");
1647 fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n");
1648
1649 fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n");
1650 fprintf(out, " for(int i = 0; i < jsize; i++) {\n");
1651 fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n");
1652 fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n");
1653 fprintf(out, " if (jvalue_obj == NULL) { continue; }\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001654 fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n");
1655 fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n");
1656 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n");
Yangster-mace124e422018-08-16 10:30:28 -07001657 fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n");
1658 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n");
1659 fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n");
1660 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n");
1661 fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n");
1662 fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n");
1663 fprintf(out, " scoped_ufs.push_back(std::move(utf));\n");
1664 fprintf(out, " }\n");
1665 fprintf(out, " }\n");
1666}
1667
Yao Chend54f9dd2017-10-17 17:37:48 +00001668static int
Yangster-macba5b9e42018-01-10 21:31:59 -08001669write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
Tej Singh810eeb32019-03-07 19:08:52 -08001670 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1671 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001672 // Print write methods
Tej Singh810eeb32019-03-07 19:08:52 -08001673 for (auto signature_to_modules_it = signatures_to_modules.begin();
1674 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1675 vector<java_type_t> signature = signature_to_modules_it->first;
Yao Chend54f9dd2017-10-17 17:37:48 +00001676 int argIndex;
1677
Yao Chen97e21ec2018-03-29 11:00:38 -07001678 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001679 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Tej Singh810eeb32019-03-07 19:08:52 -08001680 jni_function_name(java_method_name, signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001681 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -08001682 for (vector<java_type_t>::const_iterator arg = signature.begin();
1683 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001684 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1685 for (auto chainField : attributionDecl.fields) {
1686 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1687 chainField.name.c_str());
1688 }
Yangster-mace124e422018-08-16 10:30:28 -07001689 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1690 fprintf(out, ", jobject value_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001691 } else {
1692 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1693 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001694 argIndex++;
1695 }
1696 fprintf(out, ")\n");
1697
1698 fprintf(out, "{\n");
1699
1700 // Prepare strings
1701 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001702 bool hadStringOrChain = false;
Yangster-mace124e422018-08-16 10:30:28 -07001703 bool isKeyValuePairAtom = false;
Tej Singh810eeb32019-03-07 19:08:52 -08001704 for (vector<java_type_t>::const_iterator arg = signature.begin();
1705 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001706 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001707 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001708 fprintf(out, " const char* str%d;\n", argIndex);
1709 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1710 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1711 argIndex, argIndex);
1712 fprintf(out, " } else {\n");
1713 fprintf(out, " str%d = NULL;\n", argIndex);
1714 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001715 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1716 hadStringOrChain = true;
1717 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1718 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chen1fe9f592018-12-06 10:34:25 -08001719 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen8e6f9982018-11-29 09:39:45 -08001720 fprintf(out,
1721 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1722 "0) {\n",
1723 argIndex, argIndex);
Yao Chenbbdd67d2018-10-24 12:15:56 -07001724 fprintf(out,
1725 " jbyte_array%d = "
1726 "env->GetByteArrayElements(arg%d, NULL);\n",
1727 argIndex, argIndex);
1728 fprintf(out,
Yao Chen1fe9f592018-12-06 10:34:25 -08001729 " str%d_length = env->GetArrayLength(arg%d);\n",
1730 argIndex, argIndex);
1731 fprintf(out,
Yao Chenbbdd67d2018-10-24 12:15:56 -07001732 " str%d = "
1733 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1734 "d, NULL));\n",
1735 argIndex, argIndex);
1736 fprintf(out, " } else {\n");
1737 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1738 fprintf(out, " str%d = NULL;\n", argIndex);
1739 fprintf(out, " }\n");
1740
Yao Chene89572c2019-01-09 15:41:50 -08001741 fprintf(out,
1742 " android::util::BytesField bytesField%d(str%d, "
1743 "str%d_length);",
1744 argIndex, argIndex, argIndex);
1745
Yangster-mac7604aea2017-12-11 22:55:49 -08001746 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1747 hadStringOrChain = true;
1748 for (auto chainField : attributionDecl.fields) {
1749 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1750 chainField.name.c_str(), chainField.name.c_str());
1751 if (chainField.name != attributionDecl.fields.front().name) {
1752 fprintf(out, " if (%s_length != %s_length) {\n",
1753 chainField.name.c_str(),
1754 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001755 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001756 fprintf(out, " }\n");
1757 }
1758 if (chainField.javaType == JAVA_TYPE_INT) {
1759 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1760 chainField.name.c_str(), chainField.name.c_str());
1761 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1762 fprintf(out, " std::vector<%s> %s_vec;\n",
1763 cpp_type_name(chainField.javaType), chainField.name.c_str());
1764 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1765 chainField.name.c_str());
1766 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1767 chainField.name.c_str());
1768 fprintf(out, " jstring jstr = "
1769 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1770 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001771 fprintf(out, " if (jstr == NULL) {\n");
1772 fprintf(out, " %s_vec.push_back(NULL);\n",
1773 chainField.name.c_str());
1774 fprintf(out, " } else {\n");
1775 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001776 "new ScopedUtfChars(env, jstr);\n",
1777 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001778 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001779 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001780 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001781 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001782 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001783 fprintf(out, " }\n");
1784 }
1785 fprintf(out, "\n");
1786 }
Yangster-mace124e422018-08-16 10:30:28 -07001787 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1788 isKeyValuePairAtom = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001789 }
1790 argIndex++;
1791 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001792 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1793 // chains.
Yangster-mace124e422018-08-16 10:30:28 -07001794 if (!hadStringOrChain && !isKeyValuePairAtom) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001795 fprintf(out, " (void)env;\n");
1796 }
Yangster-mace124e422018-08-16 10:30:28 -07001797 if (isKeyValuePairAtom) {
1798 write_key_value_map_jni(out);
1799 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001800
1801 // stats_write call
1802 argIndex = 1;
Yao Chene89572c2019-01-09 15:41:50 -08001803 fprintf(out, "\n int ret = android::util::%s(code",
1804 cpp_method_name.c_str());
Tej Singh810eeb32019-03-07 19:08:52 -08001805 for (vector<java_type_t>::const_iterator arg = signature.begin();
1806 arg != signature.end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001807 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1808 for (auto chainField : attributionDecl.fields) {
1809 if (chainField.javaType == JAVA_TYPE_INT) {
1810 fprintf(out, ", (const %s*)%s_array, %s_length",
1811 cpp_type_name(chainField.javaType),
1812 chainField.name.c_str(), chainField.name.c_str());
1813 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1814 fprintf(out, ", %s_vec", chainField.name.c_str());
1815 }
1816 }
Yangster-mace124e422018-08-16 10:30:28 -07001817 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -07001818 fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map");
Yao Chene89572c2019-01-09 15:41:50 -08001819 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1820 fprintf(out, ", bytesField%d", argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -08001821 } else {
Yao Chene89572c2019-01-09 15:41:50 -08001822 const char* argName =
1823 (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001824 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1825 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001826 argIndex++;
1827 }
1828 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001829 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001830
1831 // Clean up strings
1832 argIndex = 1;
Tej Singh810eeb32019-03-07 19:08:52 -08001833 for (vector<java_type_t>::const_iterator arg = signature.begin();
1834 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001835 if (*arg == JAVA_TYPE_STRING) {
1836 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1837 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1838 argIndex, argIndex);
1839 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001840 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1841 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1842 fprintf(out,
1843 " env->ReleaseByteArrayElements(arg%d, "
1844 "jbyte_array%d, 0);\n",
1845 argIndex, argIndex);
1846 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001847 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1848 for (auto chainField : attributionDecl.fields) {
1849 if (chainField.javaType == JAVA_TYPE_INT) {
1850 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1851 chainField.name.c_str(), chainField.name.c_str());
1852 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001853 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001854 chainField.name.c_str());
1855 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1856 fprintf(out, " }\n");
1857 }
1858 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001859 }
1860 argIndex++;
1861 }
Yangster-mace124e422018-08-16 10:30:28 -07001862
Yao Chen97e21ec2018-03-29 11:00:38 -07001863 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001864
1865 fprintf(out, "}\n");
1866 fprintf(out, "\n");
1867 }
1868
Yangster-macba5b9e42018-01-10 21:31:59 -08001869
1870 return 0;
1871}
1872
1873void write_jni_registration(FILE* out, const string& java_method_name,
Tej Singh810eeb32019-03-07 19:08:52 -08001874 const map<vector<java_type_t>, set<string>>& signatures_to_modules,
1875 const AtomDecl &attributionDecl) {
1876 for (auto signature_to_modules_it = signatures_to_modules.begin();
1877 signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
1878 vector<java_type_t> signature = signature_to_modules_it->first;
Yangster-macba5b9e42018-01-10 21:31:59 -08001879 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1880 java_method_name.c_str(),
Tej Singh810eeb32019-03-07 19:08:52 -08001881 jni_function_signature(signature, attributionDecl).c_str(),
1882 jni_function_name(java_method_name, signature).c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -08001883 }
1884}
1885
1886static int
1887write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1888{
1889 // Print prelude
1890 fprintf(out, "// This file is autogenerated\n");
1891 fprintf(out, "\n");
1892
1893 fprintf(out, "#include <statslog.h>\n");
1894 fprintf(out, "\n");
1895 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1896 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1897 fprintf(out, "#include <utils/Vector.h>\n");
1898 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1899 fprintf(out, "#include \"jni.h\"\n");
1900 fprintf(out, "\n");
1901 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1902 fprintf(out, "\n");
1903
1904 fprintf(out, "namespace android {\n");
1905 fprintf(out, "\n");
1906
Tej Singh810eeb32019-03-07 19:08:52 -08001907 write_stats_log_jni(out, "write", "stats_write", atoms.signatures_to_modules, attributionDecl);
Yangster-macba5b9e42018-01-10 21:31:59 -08001908 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
Tej Singh810eeb32019-03-07 19:08:52 -08001909 atoms.non_chained_signatures_to_modules, attributionDecl);
Yangster-macba5b9e42018-01-10 21:31:59 -08001910
Yao Chend54f9dd2017-10-17 17:37:48 +00001911 // Print registration function table
1912 fprintf(out, "/*\n");
1913 fprintf(out, " * JNI registration.\n");
1914 fprintf(out, " */\n");
1915 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001916 write_jni_registration(out, "write", atoms.signatures_to_modules, attributionDecl);
1917 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures_to_modules,
1918 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001919 fprintf(out, "};\n");
1920 fprintf(out, "\n");
1921
1922 // Print registration function
Tej Singhbe0482b2019-03-19 22:01:57 -07001923 fprintf(out, "int register_android_util_StatsLogInternal(JNIEnv* env) {\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001924 fprintf(out, " return RegisterMethodsOrDie(\n");
1925 fprintf(out, " env,\n");
Tej Singhbe0482b2019-03-19 22:01:57 -07001926 fprintf(out, " \"android/util/StatsLogInternal\",\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001927 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1928 fprintf(out, "}\n");
1929
1930 fprintf(out, "\n");
1931 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001932 return 0;
1933}
1934
Yao Chend54f9dd2017-10-17 17:37:48 +00001935static void
1936print_usage()
1937{
1938 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1939 fprintf(stderr, "\n");
1940 fprintf(stderr, "OPTIONS\n");
1941 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1942 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1943 fprintf(stderr, " --help this message\n");
1944 fprintf(stderr, " --java FILENAME the java file to output\n");
1945 fprintf(stderr, " --jni FILENAME the jni file to output\n");
Tej Singh810eeb32019-03-07 19:08:52 -08001946 fprintf(stderr, " --module NAME optional, module name to generate outputs for\n");
1947 fprintf(stderr, " --namespace COMMA,SEP,NAMESPACE required for cpp/header with module\n");
1948 fprintf(stderr, " comma separated namespace of the files\n");
1949 fprintf(stderr, " --importHeader NAME required for cpp/jni to say which header to import\n");
Tej Singh2910d5a2019-03-23 17:26:32 -07001950 fprintf(stderr, " --javaPackage PACKAGE the package for the java file.\n");
1951 fprintf(stderr, " required for java with module\n");
1952 fprintf(stderr, " --javaClass CLASS the class name of the java class.\n");
1953 fprintf(stderr, " Optional for Java with module.\n");
1954 fprintf(stderr, " Default is \"StatsLogInternal\"\n");}
Yao Chend54f9dd2017-10-17 17:37:48 +00001955
1956/**
1957 * Do the argument parsing and execute the tasks.
1958 */
1959static int
1960run(int argc, char const*const* argv)
1961{
1962 string cppFilename;
1963 string headerFilename;
1964 string javaFilename;
1965 string jniFilename;
1966
Tej Singh810eeb32019-03-07 19:08:52 -08001967 string moduleName = DEFAULT_MODULE_NAME;
1968 string cppNamespace = DEFAULT_CPP_NAMESPACE;
1969 string cppHeaderImport = DEFAULT_CPP_HEADER_IMPORT;
Tej Singh2910d5a2019-03-23 17:26:32 -07001970 string javaPackage = DEFAULT_JAVA_PACKAGE;
1971 string javaClass = DEFAULT_JAVA_CLASS;
Tej Singh810eeb32019-03-07 19:08:52 -08001972
Yao Chend54f9dd2017-10-17 17:37:48 +00001973 int index = 1;
1974 while (index < argc) {
1975 if (0 == strcmp("--help", argv[index])) {
1976 print_usage();
1977 return 0;
1978 } else if (0 == strcmp("--cpp", argv[index])) {
1979 index++;
1980 if (index >= argc) {
1981 print_usage();
1982 return 1;
1983 }
1984 cppFilename = argv[index];
1985 } else if (0 == strcmp("--header", argv[index])) {
1986 index++;
1987 if (index >= argc) {
1988 print_usage();
1989 return 1;
1990 }
1991 headerFilename = argv[index];
1992 } else if (0 == strcmp("--java", argv[index])) {
1993 index++;
1994 if (index >= argc) {
1995 print_usage();
1996 return 1;
1997 }
1998 javaFilename = argv[index];
1999 } else if (0 == strcmp("--jni", argv[index])) {
2000 index++;
2001 if (index >= argc) {
2002 print_usage();
2003 return 1;
2004 }
2005 jniFilename = argv[index];
Tej Singh810eeb32019-03-07 19:08:52 -08002006 } else if (0 == strcmp("--module", argv[index])) {
2007 index++;
2008 if (index >= argc) {
2009 print_usage();
2010 return 1;
2011 }
2012 moduleName = argv[index];
2013 } else if (0 == strcmp("--namespace", argv[index])) {
2014 index++;
2015 if (index >= argc) {
2016 print_usage();
2017 return 1;
2018 }
2019 cppNamespace = argv[index];
2020 } else if (0 == strcmp("--importHeader", argv[index])) {
2021 index++;
2022 if (index >= argc) {
2023 print_usage();
2024 return 1;
2025 }
2026 cppHeaderImport = argv[index];
Tej Singh2910d5a2019-03-23 17:26:32 -07002027 } else if (0 == strcmp("--javaPackage", argv[index])) {
2028 index++;
2029 if (index >= argc) {
2030 print_usage();
2031 return 1;
2032 }
2033 javaPackage = argv[index];
2034 } else if (0 == strcmp("--javaClass", argv[index])) {
2035 index++;
2036 if (index >= argc) {
2037 print_usage();
2038 return 1;
2039 }
2040 javaClass = argv[index];
Yao Chend54f9dd2017-10-17 17:37:48 +00002041 }
2042 index++;
2043 }
2044
2045 if (cppFilename.size() == 0
2046 && headerFilename.size() == 0
2047 && javaFilename.size() == 0
2048 && jniFilename.size() == 0) {
2049 print_usage();
2050 return 1;
2051 }
2052
2053 // Collate the parameters
2054 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08002055 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00002056 if (errorCount != 0) {
2057 return 1;
2058 }
2059
Yangster-mac7604aea2017-12-11 22:55:49 -08002060 AtomDecl attributionDecl;
2061 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08002062 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08002063 &attributionDecl, &attributionSignature);
2064
Yao Chend54f9dd2017-10-17 17:37:48 +00002065 // Write the .cpp file
2066 if (cppFilename.size() != 0) {
2067 FILE* out = fopen(cppFilename.c_str(), "w");
2068 if (out == NULL) {
2069 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
2070 return 1;
2071 }
Tej Singh810eeb32019-03-07 19:08:52 -08002072 // If this is for a specific module, the namespace must also be provided.
2073 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
2074 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
2075 return 1;
2076 }
2077 // If this is for a specific module, the header file to import must also be provided.
2078 if (moduleName != DEFAULT_MODULE_NAME && cppHeaderImport == DEFAULT_CPP_HEADER_IMPORT) {
2079 fprintf(stderr, "Must supply --headerImport if supplying a specific module\n");
2080 return 1;
2081 }
Yangster-mac7604aea2017-12-11 22:55:49 -08002082 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
Tej Singh810eeb32019-03-07 19:08:52 -08002083 out, atoms, attributionDecl, moduleName, cppNamespace, cppHeaderImport);
Yao Chend54f9dd2017-10-17 17:37:48 +00002084 fclose(out);
2085 }
2086
2087 // Write the .h file
2088 if (headerFilename.size() != 0) {
2089 FILE* out = fopen(headerFilename.c_str(), "w");
2090 if (out == NULL) {
2091 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
2092 return 1;
2093 }
Tej Singh810eeb32019-03-07 19:08:52 -08002094 // If this is for a specific module, the namespace must also be provided.
2095 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
2096 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
2097 }
Yangster-mac7604aea2017-12-11 22:55:49 -08002098 errorCount = android::stats_log_api_gen::write_stats_log_header(
Tej Singh810eeb32019-03-07 19:08:52 -08002099 out, atoms, attributionDecl, moduleName, cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +00002100 fclose(out);
2101 }
2102
2103 // Write the .java file
2104 if (javaFilename.size() != 0) {
2105 FILE* out = fopen(javaFilename.c_str(), "w");
2106 if (out == NULL) {
2107 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
2108 return 1;
2109 }
Tej Singh2910d5a2019-03-23 17:26:32 -07002110 // If this is for a specific module, the java package must also be provided.
2111 if (moduleName != DEFAULT_MODULE_NAME && javaPackage== DEFAULT_JAVA_PACKAGE) {
2112 fprintf(stderr, "Must supply --javaPackage if supplying a specific module\n");
2113 return 1;
2114 }
2115 if (moduleName == DEFAULT_MODULE_NAME) {
2116 errorCount = android::stats_log_api_gen::write_stats_log_java(
2117 out, atoms, attributionDecl);
2118 } else {
2119 errorCount = android::stats_log_api_gen::write_stats_log_java_for_module(
2120 out, atoms, attributionDecl, moduleName, javaClass, javaPackage);
2121 }
Yao Chend54f9dd2017-10-17 17:37:48 +00002122 fclose(out);
2123 }
2124
2125 // Write the jni file
2126 if (jniFilename.size() != 0) {
2127 FILE* out = fopen(jniFilename.c_str(), "w");
2128 if (out == NULL) {
2129 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
2130 return 1;
2131 }
Yangster-mac7604aea2017-12-11 22:55:49 -08002132 errorCount = android::stats_log_api_gen::write_stats_log_jni(
2133 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00002134 fclose(out);
2135 }
2136
Tej Singh2910d5a2019-03-23 17:26:32 -07002137 return errorCount;
Yao Chend54f9dd2017-10-17 17:37:48 +00002138}
2139
2140}
2141}
2142
2143/**
2144 * Main.
2145 */
2146int
2147main(int argc, char const*const* argv)
2148{
2149 GOOGLE_PROTOBUF_VERIFY_VERSION;
2150
2151 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07002152}