blob: 4491a8567441fd7f8a030baec58f96a9cfe59015 [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001
2
3#include "Collation.h"
4
Stefan Lafonae2df012017-11-14 09:17:21 -08005#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +00006
7#include <set>
8#include <vector>
9
10#include <getopt.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15using namespace google::protobuf;
16using namespace std;
17
18namespace android {
19namespace stats_log_api_gen {
20
Yao Chenb3561512017-11-21 18:07:17 -080021int maxPushedAtomId = 2;
22
Stefan Lafonae2df012017-11-14 09:17:21 -080023using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000024
Yao Chend54f9dd2017-10-17 17:37:48 +000025/**
26 * Turn lower and camel case into upper case with underscores.
27 */
28static string
29make_constant_name(const string& str)
30{
31 string result;
32 const int N = str.size();
33 bool underscore_next = false;
34 for (int i=0; i<N; i++) {
35 char c = str[i];
36 if (c >= 'A' && c <= 'Z') {
37 if (underscore_next) {
38 result += '_';
39 underscore_next = false;
40 }
41 } else if (c >= 'a' && c <= 'z') {
42 c = 'A' + c - 'a';
43 underscore_next = true;
44 } else if (c == '_') {
45 underscore_next = false;
46 }
47 result += c;
48 }
49 return result;
50}
51
52static const char*
53cpp_type_name(java_type_t type)
54{
55 switch (type) {
56 case JAVA_TYPE_BOOLEAN:
57 return "bool";
58 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070059 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000060 return "int32_t";
61 case JAVA_TYPE_LONG:
62 return "int64_t";
63 case JAVA_TYPE_FLOAT:
64 return "float";
65 case JAVA_TYPE_DOUBLE:
66 return "double";
67 case JAVA_TYPE_STRING:
68 return "char const*";
Yao Chenbbdd67d2018-10-24 12:15:56 -070069 case JAVA_TYPE_BYTE_ARRAY:
Yao Chene89572c2019-01-09 15:41:50 -080070 return "const BytesField&";
Yao Chend54f9dd2017-10-17 17:37:48 +000071 default:
72 return "UNKNOWN";
73 }
74}
75
76static const char*
77java_type_name(java_type_t type)
78{
79 switch (type) {
80 case JAVA_TYPE_BOOLEAN:
81 return "boolean";
82 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070083 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000084 return "int";
85 case JAVA_TYPE_LONG:
86 return "long";
87 case JAVA_TYPE_FLOAT:
88 return "float";
89 case JAVA_TYPE_DOUBLE:
90 return "double";
91 case JAVA_TYPE_STRING:
92 return "java.lang.String";
Yao Chenbbdd67d2018-10-24 12:15:56 -070093 case JAVA_TYPE_BYTE_ARRAY:
94 return "byte[]";
Yao Chend54f9dd2017-10-17 17:37:48 +000095 default:
96 return "UNKNOWN";
97 }
98}
99
Yangster-mac7604aea2017-12-11 22:55:49 -0800100static int write_stats_log_cpp(FILE *out, const Atoms &atoms,
101 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000102 // Print prelude
103 fprintf(out, "// This file is autogenerated\n");
104 fprintf(out, "\n");
105
Yangster-macca5c0862018-04-09 22:39:53 -0700106 fprintf(out, "#include <mutex>\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700107 fprintf(out, "#include <chrono>\n");
108 fprintf(out, "#include <thread>\n");
Jack He34a892d2018-12-20 00:42:31 -0800109 fprintf(out, "#ifdef __ANDROID__\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700110 fprintf(out, "#include <cutils/properties.h>\n");
Jack He34a892d2018-12-20 00:42:31 -0800111 fprintf(out, "#endif\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700112 fprintf(out, "#include <stats_event_list.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000113 fprintf(out, "#include <log/log.h>\n");
114 fprintf(out, "#include <statslog.h>\n");
Yangster-mac330af582018-02-08 15:24:38 -0800115 fprintf(out, "#include <utils/SystemClock.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000116 fprintf(out, "\n");
117
118 fprintf(out, "namespace android {\n");
119 fprintf(out, "namespace util {\n");
Yao Chen80235402017-11-13 20:42:25 -0800120 fprintf(out, "// the single event tag id for all stats logs\n");
121 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Jack He34a892d2018-12-20 00:42:31 -0800122 fprintf(out, "#ifdef __ANDROID__\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700123 fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
Jack He34a892d2018-12-20 00:42:31 -0800124 fprintf(out, "#else\n");
125 fprintf(out, "const static bool kStatsdEnabled = false;\n");
126 fprintf(out, "#endif\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000127
Yao Chenc40a19d2018-03-15 16:48:25 -0700128 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
129 "audio_state_changed",
130 "call_state_changed",
131 "phone_signal_strength_changed",
132 "mobile_bytes_transfer_by_fg_bg",
133 "mobile_bytes_transfer"};
134 fprintf(out,
135 "const std::set<int> "
136 "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n");
137 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
138 atom != atoms.decls.end(); atom++) {
139 if (kTruncatingAtomNames.find(atom->name) ==
140 kTruncatingAtomNames.end()) {
141 string constant = make_constant_name(atom->name);
142 fprintf(out, " %s,\n", constant.c_str());
143 }
144 }
145 fprintf(out, "};\n");
146 fprintf(out, "\n");
147
148 fprintf(out,
149 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
150 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
151 atom != atoms.decls.end(); atom++) {
152 for (vector<AtomField>::const_iterator field = atom->fields.begin();
153 field != atom->fields.end(); field++) {
154 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
155 string constant = make_constant_name(atom->name);
156 fprintf(out, " %s,\n", constant.c_str());
157 break;
158 }
159 }
160 }
161 fprintf(out, "};\n");
162 fprintf(out, "\n");
163
Yangster-macca5c0862018-04-09 22:39:53 -0700164 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700165 fprintf(out, " std::map<int, int> uidField;\n");
166 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
167 atom != atoms.decls.end(); atom++) {
168 if (atom->uidField == 0) {
169 continue;
170 }
171 fprintf(out,
172 "\n // Adding uid field for atom "
173 "(%d)%s\n",
174 atom->code, atom->name.c_str());
175 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
176 make_constant_name(atom->name).c_str(), atom->uidField);
177 }
178
179 fprintf(out, " return uidField;\n");
180 fprintf(out, "};\n");
181
182 fprintf(out,
183 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
184 "getAtomUidField();\n");
185
186 fprintf(out,
187 "static std::map<int, StateAtomFieldOptions> "
188 "getStateAtomFieldOptions() {\n");
189 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
190 fprintf(out, " StateAtomFieldOptions opt;\n");
191 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
192 atom != atoms.decls.end(); atom++) {
193 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
194 continue;
195 }
196 fprintf(out,
197 "\n // Adding primary and exclusive fields for atom "
198 "(%d)%s\n",
199 atom->code, atom->name.c_str());
200 fprintf(out, " opt.primaryFields.clear();\n");
201 for (const auto& field : atom->primaryFields) {
202 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
203 }
204
205 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
206 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
207 make_constant_name(atom->name).c_str());
208 }
209
210 fprintf(out, " return options;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700211 fprintf(out, "}\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700212
213 fprintf(out,
214 "const std::map<int, StateAtomFieldOptions> "
215 "AtomsInfo::kStateAtomsFieldOptions = "
216 "getStateAtomFieldOptions();\n");
217
Yao Chenbbdd67d2018-10-24 12:15:56 -0700218 fprintf(out,
219 "static std::map<int, std::vector<int>> "
220 "getBinaryFieldAtoms() {\n");
221 fprintf(out, " std::map<int, std::vector<int>> options;\n");
222 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
223 atom != atoms.decls.end(); atom++) {
224 if (atom->binaryFields.size() == 0) {
225 continue;
226 }
227 fprintf(out,
228 "\n // Adding binary fields for atom "
229 "(%d)%s\n",
230 atom->code, atom->name.c_str());
231
232 for (const auto& field : atom->binaryFields) {
233 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
234 make_constant_name(atom->name).c_str(), field);
235 }
236 }
237
238 fprintf(out, " return options;\n");
239 fprintf(out, "}\n");
240
241 fprintf(out,
242 "const std::map<int, std::vector<int>> "
243 "AtomsInfo::kBytesFieldAtoms = "
244 "getBinaryFieldAtoms();\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700245
246 fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
247 fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
248 fprintf(out, "static std::mutex mLogdRetryMutex;\n");
249
Yao Chend54f9dd2017-10-17 17:37:48 +0000250 // Print write methods
251 fprintf(out, "\n");
252 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800253 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000254 int argIndex;
255
Yao Chen97e21ec2018-03-29 11:00:38 -0700256 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700257 fprintf(out, "try_stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000258 argIndex = 1;
259 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800260 arg != signature->end(); arg++) {
261 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
262 for (auto chainField : attributionDecl.fields) {
263 if (chainField.javaType == JAVA_TYPE_STRING) {
264 fprintf(out, ", const std::vector<%s>& %s",
265 cpp_type_name(chainField.javaType),
266 chainField.name.c_str());
267 } else {
268 fprintf(out, ", const %s* %s, size_t %s_length",
269 cpp_type_name(chainField.javaType),
270 chainField.name.c_str(), chainField.name.c_str());
271 }
272 }
Yangster-mace124e422018-08-16 10:30:28 -0700273 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700274 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
275 "const std::map<int, int64_t>& arg%d_2, "
276 "const std::map<int, char const*>& arg%d_3, "
277 "const std::map<int, float>& arg%d_4",
278 argIndex, argIndex, argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800279 } else {
280 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
281 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000282 argIndex++;
283 }
284 fprintf(out, ")\n");
285
286 fprintf(out, "{\n");
287 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700288 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700289 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800290 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800291 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000292 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800293 arg != signature->end(); arg++) {
294 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
295 for (const auto &chainField : attributionDecl.fields) {
296 if (chainField.javaType == JAVA_TYPE_STRING) {
297 fprintf(out, " if (%s_length != %s.size()) {\n",
298 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700299 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800300 fprintf(out, " }\n");
301 }
302 }
303 fprintf(out, "\n event.begin();\n");
304 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
305 attributionDecl.fields.front().name.c_str());
306 fprintf(out, " event.begin();\n");
307 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800308 if (chainField.javaType == JAVA_TYPE_STRING) {
309 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
310 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
311 fprintf(out, " } else {\n");
312 fprintf(out, " event << \"\";\n");
313 fprintf(out, " }\n");
314 } else {
315 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
316 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800317 }
318 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000319 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800320 fprintf(out, " event.end();\n\n");
Yangster-mace124e422018-08-16 10:30:28 -0700321 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
322 fprintf(out, " event.begin();\n\n");
323 fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
324 fprintf(out, " event.begin();\n");
325 fprintf(out, " event << it.first;\n");
326 fprintf(out, " event << it.second;\n");
327 fprintf(out, " event.end();\n");
328 fprintf(out, " }\n");
329
330 fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
331 fprintf(out, " event.begin();\n");
332 fprintf(out, " event << it.first;\n");
333 fprintf(out, " event << it.second;\n");
334 fprintf(out, " event.end();\n");
335 fprintf(out, " }\n");
336
337 fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
338 fprintf(out, " event.begin();\n");
339 fprintf(out, " event << it.first;\n");
340 fprintf(out, " event << it.second;\n");
341 fprintf(out, " event.end();\n");
342 fprintf(out, " }\n");
343
Howard Ro4078dd42018-09-27 17:41:08 -0700344 fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
345 fprintf(out, " event.begin();\n");
346 fprintf(out, " event << it.first;\n");
347 fprintf(out, " event << it.second;\n");
348 fprintf(out, " event.end();\n");
349 fprintf(out, " }\n");
350
Yangster-mace124e422018-08-16 10:30:28 -0700351 fprintf(out, " event.end();\n\n");
Yao Chen1fe9f592018-12-06 10:34:25 -0800352 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
353 fprintf(out,
Yao Chene89572c2019-01-09 15:41:50 -0800354 " event.AppendCharArray(arg%d.arg, "
355 "arg%d.arg_length);\n",
Yao Chen1fe9f592018-12-06 10:34:25 -0800356 argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800357 } else {
358 if (*arg == JAVA_TYPE_STRING) {
359 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
360 fprintf(out, " arg%d = \"\";\n", argIndex);
361 fprintf(out, " }\n");
362 }
363 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000364 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000365 argIndex++;
366 }
367
Yao Chen97e21ec2018-03-29 11:00:38 -0700368 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700369 fprintf(out, " } else {\n");
370 fprintf(out, " return 1;\n");
371 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000372 fprintf(out, "}\n");
373 fprintf(out, "\n");
374 }
375
Yangster-macb8382a12018-04-04 10:39:12 -0700376 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
377 signature != atoms.signatures.end(); signature++) {
378 int argIndex;
379
Yangster-mace124e422018-08-16 10:30:28 -0700380 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700381 fprintf(out, "stats_write(int32_t code");
382 argIndex = 1;
383 for (vector<java_type_t>::const_iterator arg = signature->begin();
384 arg != signature->end(); arg++) {
385 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
386 for (auto chainField : attributionDecl.fields) {
387 if (chainField.javaType == JAVA_TYPE_STRING) {
388 fprintf(out, ", const std::vector<%s>& %s",
389 cpp_type_name(chainField.javaType),
390 chainField.name.c_str());
391 } else {
392 fprintf(out, ", const %s* %s, size_t %s_length",
393 cpp_type_name(chainField.javaType),
394 chainField.name.c_str(), chainField.name.c_str());
395 }
396 }
Yangster-mace124e422018-08-16 10:30:28 -0700397 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Yao Chen1fe9f592018-12-06 10:34:25 -0800398 fprintf(out,
399 ", const std::map<int, int32_t>& arg%d_1, "
400 "const std::map<int, int64_t>& arg%d_2, "
401 "const std::map<int, char const*>& arg%d_3, "
402 "const std::map<int, float>& arg%d_4",
403 argIndex, argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700404 } else {
405 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
406 }
407 argIndex++;
408 }
409 fprintf(out, ")\n");
410
411 fprintf(out, "{\n");
412 fprintf(out, " int ret = 0;\n");
413
Yangster-macca5c0862018-04-09 22:39:53 -0700414 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700415 fprintf(out, " ret = try_stats_write(code");
416
417 argIndex = 1;
418 for (vector<java_type_t>::const_iterator arg = signature->begin();
419 arg != signature->end(); arg++) {
420 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
421 for (auto chainField : attributionDecl.fields) {
422 if (chainField.javaType == JAVA_TYPE_STRING) {
423 fprintf(out, ", %s",
424 chainField.name.c_str());
425 } else {
426 fprintf(out, ", %s, %s_length",
427 chainField.name.c_str(), chainField.name.c_str());
428 }
429 }
Yao Chen1fe9f592018-12-06 10:34:25 -0800430 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
431 fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex,
432 argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700433 } else {
434 fprintf(out, ", arg%d", argIndex);
435 }
436 argIndex++;
437 }
438 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700439 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700440
441 fprintf(out, " {\n");
442 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
443 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
444 "kMinRetryIntervalNs) break;\n");
445 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
446 fprintf(out, " }\n");
447 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700448 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700449 fprintf(out, " if (ret < 0) {\n");
Yao Chen39b67992018-11-08 15:32:17 -0800450 fprintf(out, " note_log_drop(ret);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700451 fprintf(out, " }\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700452 fprintf(out, " return ret;\n");
453 fprintf(out, "}\n");
454 fprintf(out, "\n");
455 }
456
Yangster-macba5b9e42018-01-10 21:31:59 -0800457 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
458 signature != atoms.non_chained_signatures.end(); signature++) {
459 int argIndex;
460
Yao Chen97e21ec2018-03-29 11:00:38 -0700461 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700462 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800463 argIndex = 1;
464 for (vector<java_type_t>::const_iterator arg = signature->begin();
465 arg != signature->end(); arg++) {
466 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
467 argIndex++;
468 }
469 fprintf(out, ")\n");
470
471 fprintf(out, "{\n");
472 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700473 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700474 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800475 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800476 fprintf(out, " event << code;\n\n");
477 for (vector<java_type_t>::const_iterator arg = signature->begin();
478 arg != signature->end(); arg++) {
479 if (argIndex == 1) {
480 fprintf(out, " event.begin();\n\n");
481 fprintf(out, " event.begin();\n");
482 }
483 if (*arg == JAVA_TYPE_STRING) {
484 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
485 fprintf(out, " arg%d = \"\";\n", argIndex);
486 fprintf(out, " }\n");
487 }
Yao Chene89572c2019-01-09 15:41:50 -0800488 if (*arg == JAVA_TYPE_BYTE_ARRAY) {
489 fprintf(out,
490 " event.AppendCharArray(arg%d.arg, "
491 "arg%d.arg_length);",
492 argIndex, argIndex);
493 } else {
494 fprintf(out, " event << arg%d;\n", argIndex);
495 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800496 if (argIndex == 2) {
497 fprintf(out, " event.end();\n\n");
498 fprintf(out, " event.end();\n\n");
499 }
500 argIndex++;
501 }
502
Yao Chen97e21ec2018-03-29 11:00:38 -0700503 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700504 fprintf(out, " } else {\n");
505 fprintf(out, " return 1;\n");
506 fprintf(out, " }\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800507 fprintf(out, "}\n");
508 fprintf(out, "\n");
509 }
Yangster-macb8382a12018-04-04 10:39:12 -0700510
511 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
512 signature != atoms.non_chained_signatures.end(); signature++) {
513 int argIndex;
514
515 fprintf(out, "int\n");
516 fprintf(out, "stats_write_non_chained(int32_t code");
517 argIndex = 1;
518 for (vector<java_type_t>::const_iterator arg = signature->begin();
519 arg != signature->end(); arg++) {
520 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
521 argIndex++;
522 }
523 fprintf(out, ")\n");
524
525 fprintf(out, "{\n");
526
527 fprintf(out, " int ret = 0;\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700528 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700529 fprintf(out, " ret = try_stats_write_non_chained(code");
530
531 argIndex = 1;
532 for (vector<java_type_t>::const_iterator arg = signature->begin();
533 arg != signature->end(); arg++) {
534 fprintf(out, ", arg%d", argIndex);
535 argIndex++;
536 }
537 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700538 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700539
540 fprintf(out, " {\n");
541 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
542 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
543 "kMinRetryIntervalNs) break;\n");
544 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
545 fprintf(out, " }\n");
546
547 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700548 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700549 fprintf(out, " if (ret < 0) {\n");
Yao Chen39b67992018-11-08 15:32:17 -0800550 fprintf(out, " note_log_drop(ret);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700551 fprintf(out, " }\n");
552 fprintf(out, " return ret;\n\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700553 fprintf(out, "}\n");
554
555 fprintf(out, "\n");
556 }
557
558
Yao Chend54f9dd2017-10-17 17:37:48 +0000559 // Print footer
560 fprintf(out, "\n");
561 fprintf(out, "} // namespace util\n");
562 fprintf(out, "} // namespace android\n");
563
564 return 0;
565}
566
Yangster-macba5b9e42018-01-10 21:31:59 -0800567void build_non_chained_decl_map(const Atoms& atoms,
568 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
569 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
570 atom != atoms.non_chained_decls.end(); atom++) {
571 decl_map->insert(std::make_pair(atom->code, atom));
572 }
573}
574
575static void write_cpp_usage(
576 FILE* out, const string& method_name, const string& atom_code_name,
577 const AtomDecl& atom, const AtomDecl &attributionDecl) {
Yao Chene89572c2019-01-09 15:41:50 -0800578 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
579 atom_code_name.c_str());
580
Yangster-macba5b9e42018-01-10 21:31:59 -0800581 for (vector<AtomField>::const_iterator field = atom.fields.begin();
582 field != atom.fields.end(); field++) {
583 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
584 for (auto chainField : attributionDecl.fields) {
585 if (chainField.javaType == JAVA_TYPE_STRING) {
586 fprintf(out, ", const std::vector<%s>& %s",
587 cpp_type_name(chainField.javaType),
588 chainField.name.c_str());
589 } else {
590 fprintf(out, ", const %s* %s, size_t %s_length",
591 cpp_type_name(chainField.javaType),
592 chainField.name.c_str(), chainField.name.c_str());
593 }
594 }
Yangster-mace124e422018-08-16 10:30:28 -0700595 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700596 fprintf(out, ", const std::map<int, int32_t>& %s_int"
597 ", const std::map<int, int64_t>& %s_long"
Yangster-mace124e422018-08-16 10:30:28 -0700598 ", const std::map<int, char const*>& %s_str"
599 ", const std::map<int, float>& %s_float",
Howard Ro4078dd42018-09-27 17:41:08 -0700600 field->name.c_str(),
601 field->name.c_str(),
602 field->name.c_str(),
603 field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800604 } else {
605 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
606 }
607 }
608 fprintf(out, ");\n");
609}
610
611static void write_cpp_method_header(
612 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
613 const AtomDecl &attributionDecl) {
614 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
615 signature != signatures.end(); signature++) {
Yangster-mace124e422018-08-16 10:30:28 -0700616 fprintf(out, "int %s(int32_t code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800617 int argIndex = 1;
618 for (vector<java_type_t>::const_iterator arg = signature->begin();
619 arg != signature->end(); arg++) {
620 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
621 for (auto chainField : attributionDecl.fields) {
622 if (chainField.javaType == JAVA_TYPE_STRING) {
623 fprintf(out, ", const std::vector<%s>& %s",
624 cpp_type_name(chainField.javaType), chainField.name.c_str());
625 } else {
626 fprintf(out, ", const %s* %s, size_t %s_length",
627 cpp_type_name(chainField.javaType),
628 chainField.name.c_str(), chainField.name.c_str());
629 }
630 }
Yangster-mace124e422018-08-16 10:30:28 -0700631 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700632 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
633 "const std::map<int, int64_t>& arg%d_2, "
634 "const std::map<int, char const*>& arg%d_3, "
635 "const std::map<int, float>& arg%d_4",
636 argIndex, argIndex, argIndex, argIndex);
Yangster-macba5b9e42018-01-10 21:31:59 -0800637 } else {
638 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
639 }
640 argIndex++;
641 }
642 fprintf(out, ");\n");
643
644 }
645}
Yao Chend54f9dd2017-10-17 17:37:48 +0000646
647static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800648write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000649{
Yao Chend54f9dd2017-10-17 17:37:48 +0000650 // Print prelude
651 fprintf(out, "// This file is autogenerated\n");
652 fprintf(out, "\n");
653 fprintf(out, "#pragma once\n");
654 fprintf(out, "\n");
655 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800656 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800657 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800658 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000659 fprintf(out, "\n");
660
661 fprintf(out, "namespace android {\n");
662 fprintf(out, "namespace util {\n");
663 fprintf(out, "\n");
664 fprintf(out, "/*\n");
665 fprintf(out, " * API For logging statistics events.\n");
666 fprintf(out, " */\n");
667 fprintf(out, "\n");
668 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700669 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000670 fprintf(out, " */\n");
671 fprintf(out, "enum {\n");
672
Yangster-macba5b9e42018-01-10 21:31:59 -0800673 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
674 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
675
Yao Chend54f9dd2017-10-17 17:37:48 +0000676 size_t i = 0;
677 // Print constants
678 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800679 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000680 string constant = make_constant_name(atom->name);
681 fprintf(out, "\n");
682 fprintf(out, " /**\n");
683 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800684 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
685
686 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
687 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
688 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
689 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000690 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000691 fprintf(out, " */\n");
692 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
693 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800694 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
695 maxPushedAtomId = atom->code;
696 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000697 i++;
698 }
699 fprintf(out, "\n");
700 fprintf(out, "};\n");
701 fprintf(out, "\n");
702
Yao Chene89572c2019-01-09 15:41:50 -0800703 fprintf(out, "struct BytesField {\n");
704 fprintf(out,
705 " BytesField(char const* array, size_t len) : arg(array), "
706 "arg_length(len) {}\n");
707 fprintf(out, " char const* arg;\n");
708 fprintf(out, " size_t arg_length;\n");
709 fprintf(out, "};\n");
710 fprintf(out, "\n");
711
Yao Chen9c1debe2018-02-19 14:39:19 -0800712 fprintf(out, "struct StateAtomFieldOptions {\n");
713 fprintf(out, " std::vector<int> primaryFields;\n");
714 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700715 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800716 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700717
718 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800719 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700720 " const static std::set<int> "
721 "kNotTruncatingTimestampAtomWhiteList;\n");
722 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
723 fprintf(out,
724 " const static std::set<int> kAtomsWithAttributionChain;\n");
725 fprintf(out,
726 " const static std::map<int, StateAtomFieldOptions> "
727 "kStateAtomsFieldOptions;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700728 fprintf(out,
729 " const static std::map<int, std::vector<int>> "
730 "kBytesFieldAtoms;");
Yao Chen9c1debe2018-02-19 14:39:19 -0800731 fprintf(out, "};\n");
732
Yao Chenc40a19d2018-03-15 16:48:25 -0700733 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
734 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800735
Yao Chend54f9dd2017-10-17 17:37:48 +0000736 // Print write methods
737 fprintf(out, "//\n");
738 fprintf(out, "// Write methods\n");
739 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800740 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
741
742 fprintf(out, "//\n");
743 fprintf(out, "// Write flattened methods\n");
744 fprintf(out, "//\n");
745 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
746 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000747
748 fprintf(out, "\n");
749 fprintf(out, "} // namespace util\n");
750 fprintf(out, "} // namespace android\n");
751
752 return 0;
753}
754
Bookatz0bd97202018-06-05 12:42:37 -0700755static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
756 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800757 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
758 method_name.c_str(), atom_code_name.c_str());
759 for (vector<AtomField>::const_iterator field = atom.fields.begin();
760 field != atom.fields.end(); field++) {
761 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700762 fprintf(out, ", android.os.WorkSource workSource");
Yangster-mace124e422018-08-16 10:30:28 -0700763 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
764 fprintf(out, ", SparseArray<Object> value_map");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700765 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
766 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800767 } else {
768 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
769 }
770 }
Bookatz0bd97202018-06-05 12:42:37 -0700771 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800772}
773
774static void write_java_method(
775 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
776 const AtomDecl &attributionDecl) {
777 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
778 signature != signatures.end(); signature++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800779 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700780 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800781 int argIndex = 1;
782 for (vector<java_type_t>::const_iterator arg = signature->begin();
783 arg != signature->end(); arg++) {
784 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
785 for (auto chainField : attributionDecl.fields) {
786 fprintf(out, ", %s[] %s",
787 java_type_name(chainField.javaType), chainField.name.c_str());
788 }
Yangster-mace124e422018-08-16 10:30:28 -0700789 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
790 fprintf(out, ", SparseArray<Object> value_map");
Yangster-macba5b9e42018-01-10 21:31:59 -0800791 } else {
792 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
793 }
794 argIndex++;
795 }
796 fprintf(out, ");\n");
797 }
798}
799
Bookatz0bd97202018-06-05 12:42:37 -0700800static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) {
801 fprintf(out, "\n // WorkSource methods.\n");
802 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
803 signature != signatures.end(); signature++) {
804 // Determine if there is Attribution in this signature.
805 int attributionArg = -1;
806 int argIndexMax = 0;
807 for (vector<java_type_t>::const_iterator arg = signature->begin();
808 arg != signature->end(); arg++) {
809 argIndexMax++;
810 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
811 if (attributionArg > -1) {
812 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
813 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
814 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
815 return;
816 }
817 attributionArg = argIndexMax;
818 }
819 }
820 if (attributionArg < 0) {
821 continue;
822 }
823
824 // Method header (signature)
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800825 fprintf(out, " /** @hide */\n");
Bookatz0bd97202018-06-05 12:42:37 -0700826 fprintf(out, " public static void write(int code");
827 int argIndex = 1;
828 for (vector<java_type_t>::const_iterator arg = signature->begin();
829 arg != signature->end(); arg++) {
830 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
831 fprintf(out, ", WorkSource ws");
832 } else {
833 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
834 }
835 argIndex++;
836 }
837 fprintf(out, ") {\n");
838
839 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
840 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
841 fprintf(out, " write_non_chained(code");
842 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
843 if (argIndex == attributionArg) {
844 fprintf(out, ", ws.get(i), ws.getName(i)");
845 } else {
846 fprintf(out, ", arg%d", argIndex);
847 }
848 }
849 fprintf(out, ");\n");
850 fprintf(out, " }\n"); // close flor-loop
851
852 // write() component.
853 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
854 fprintf(out, " if (workChains != null) {\n");
855 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
856 fprintf(out, " write(code");
857 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
858 if (argIndex == attributionArg) {
859 fprintf(out, ", wc.getUids(), wc.getTags()");
860 } else {
861 fprintf(out, ", arg%d", argIndex);
862 }
863 }
864 fprintf(out, ");\n");
865 fprintf(out, " }\n"); // close for-loop
866 fprintf(out, " }\n"); // close if
867 fprintf(out, " }\n"); // close method
868 }
869}
Yangster-macba5b9e42018-01-10 21:31:59 -0800870
Yao Chend54f9dd2017-10-17 17:37:48 +0000871static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800872write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000873{
Yao Chend54f9dd2017-10-17 17:37:48 +0000874 // Print prelude
875 fprintf(out, "// This file is autogenerated\n");
876 fprintf(out, "\n");
877 fprintf(out, "package android.util;\n");
878 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -0700879 fprintf(out, "import android.os.WorkSource;\n");
Yangster-mace124e422018-08-16 10:30:28 -0700880 fprintf(out, "import android.util.SparseArray;\n");
Bookatz0bd97202018-06-05 12:42:37 -0700881 fprintf(out, "import java.util.ArrayList;\n");
882 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000883 fprintf(out, "\n");
884 fprintf(out, "/**\n");
885 fprintf(out, " * API For logging statistics events.\n");
886 fprintf(out, " * @hide\n");
887 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800888 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700889 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000890
Yangster-macba5b9e42018-01-10 21:31:59 -0800891 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
892 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
893
Stefan Lafon9478f352017-10-30 21:20:20 -0700894 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000895 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
896 atom != atoms.decls.end(); atom++) {
897 string constant = make_constant_name(atom->name);
898 fprintf(out, "\n");
899 fprintf(out, " /**\n");
Bookatz0bd97202018-06-05 12:42:37 -0700900 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
901 write_java_usage(out, "write", constant, *atom);
Yangster-macba5b9e42018-01-10 21:31:59 -0800902 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
903 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
Bookatz0bd97202018-06-05 12:42:37 -0700904 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
Yao Chend54f9dd2017-10-17 17:37:48 +0000905 }
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800906 fprintf(out, " * @hide\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000907 fprintf(out, " */\n");
908 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
909 }
910 fprintf(out, "\n");
911
Stefan Lafon9478f352017-10-30 21:20:20 -0700912 // Print constants for the enum values.
913 fprintf(out, " // Constants for enum values.\n\n");
914 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800915 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700916 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800917 field != atom->fields.end(); field++) {
918 if (field->javaType == JAVA_TYPE_ENUM) {
919 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
920 field->name.c_str());
921 for (map<int, string>::const_iterator value = field->enumValues.begin();
922 value != field->enumValues.end(); value++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800923 fprintf(out, " /** @hide */\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800924 fprintf(out, " public static final int %s__%s__%s = %d;\n",
925 make_constant_name(atom->message).c_str(),
926 make_constant_name(field->name).c_str(),
927 make_constant_name(value->second).c_str(),
928 value->first);
929 }
930 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700931 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700932 }
933 }
934
Yao Chend54f9dd2017-10-17 17:37:48 +0000935 // Print write methods
936 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800937 write_java_method(out, "write", atoms.signatures, attributionDecl);
938 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Bookatz0bd97202018-06-05 12:42:37 -0700939 write_java_work_source_method(out, atoms.signatures);
Yao Chend54f9dd2017-10-17 17:37:48 +0000940
941 fprintf(out, "}\n");
942
943 return 0;
944}
945
946static const char*
947jni_type_name(java_type_t type)
948{
949 switch (type) {
950 case JAVA_TYPE_BOOLEAN:
951 return "jboolean";
952 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700953 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000954 return "jint";
955 case JAVA_TYPE_LONG:
956 return "jlong";
957 case JAVA_TYPE_FLOAT:
958 return "jfloat";
959 case JAVA_TYPE_DOUBLE:
960 return "jdouble";
961 case JAVA_TYPE_STRING:
962 return "jstring";
Yao Chenbbdd67d2018-10-24 12:15:56 -0700963 case JAVA_TYPE_BYTE_ARRAY:
964 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +0000965 default:
966 return "UNKNOWN";
967 }
968}
969
Yangster-mac7604aea2017-12-11 22:55:49 -0800970static const char*
971jni_array_type_name(java_type_t type)
972{
973 switch (type) {
974 case JAVA_TYPE_INT:
975 return "jintArray";
Yangster-mace124e422018-08-16 10:30:28 -0700976 case JAVA_TYPE_FLOAT:
977 return "jfloatArray";
Yangster-mac7604aea2017-12-11 22:55:49 -0800978 case JAVA_TYPE_STRING:
979 return "jobjectArray";
980 default:
981 return "UNKNOWN";
982 }
983}
984
Yao Chend54f9dd2017-10-17 17:37:48 +0000985static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800986jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000987{
Yangster-macba5b9e42018-01-10 21:31:59 -0800988 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000989 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800990 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000991 switch (*arg) {
992 case JAVA_TYPE_BOOLEAN:
993 result += "_boolean";
994 break;
995 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700996 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000997 result += "_int";
998 break;
999 case JAVA_TYPE_LONG:
1000 result += "_long";
1001 break;
1002 case JAVA_TYPE_FLOAT:
1003 result += "_float";
1004 break;
1005 case JAVA_TYPE_DOUBLE:
1006 result += "_double";
1007 break;
1008 case JAVA_TYPE_STRING:
1009 result += "_String";
1010 break;
Yangster-mac7604aea2017-12-11 22:55:49 -08001011 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1012 result += "_AttributionChain";
1013 break;
Yangster-mace124e422018-08-16 10:30:28 -07001014 case JAVA_TYPE_KEY_VALUE_PAIR:
1015 result += "_KeyValuePairs";
1016 break;
Yao Chenbbdd67d2018-10-24 12:15:56 -07001017 case JAVA_TYPE_BYTE_ARRAY:
1018 result += "_bytes";
1019 break;
Yao Chend54f9dd2017-10-17 17:37:48 +00001020 default:
1021 result += "_UNKNOWN";
1022 break;
1023 }
1024 }
1025 return result;
1026}
1027
1028static const char*
1029java_type_signature(java_type_t type)
1030{
1031 switch (type) {
1032 case JAVA_TYPE_BOOLEAN:
1033 return "Z";
1034 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001035 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001036 return "I";
1037 case JAVA_TYPE_LONG:
1038 return "J";
1039 case JAVA_TYPE_FLOAT:
1040 return "F";
1041 case JAVA_TYPE_DOUBLE:
1042 return "D";
1043 case JAVA_TYPE_STRING:
1044 return "Ljava/lang/String;";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001045 case JAVA_TYPE_BYTE_ARRAY:
1046 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +00001047 default:
1048 return "UNKNOWN";
1049 }
1050}
1051
1052static string
Yangster-mac7604aea2017-12-11 22:55:49 -08001053jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001054{
1055 string result("(I");
1056 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001057 arg != signature.end(); arg++) {
1058 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1059 for (auto chainField : attributionDecl.fields) {
1060 result += "[";
1061 result += java_type_signature(chainField.javaType);
1062 }
Yangster-mace124e422018-08-16 10:30:28 -07001063 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1064 result += "Landroid/util/SparseArray;";
Yangster-mac7604aea2017-12-11 22:55:49 -08001065 } else {
1066 result += java_type_signature(*arg);
1067 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001068 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001069 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +00001070 return result;
1071}
1072
Yangster-mace124e422018-08-16 10:30:28 -07001073static void write_key_value_map_jni(FILE* out) {
Howard Ro4078dd42018-09-27 17:41:08 -07001074 fprintf(out, " std::map<int, int32_t> int32_t_map;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001075 fprintf(out, " std::map<int, int64_t> int64_t_map;\n");
1076 fprintf(out, " std::map<int, float> float_map;\n");
1077 fprintf(out, " std::map<int, char const*> string_map;\n\n");
1078
1079 fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n");
1080
1081 fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n");
1082 fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n");
1083 fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n");
1084
1085
1086 fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n");
1087
Howard Ro4078dd42018-09-27 17:41:08 -07001088 fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001089 fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n");
1090 fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n");
1091 fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001092 fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001093 fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n");
1094 fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n");
1095
1096 fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n");
1097 fprintf(out, " for(int i = 0; i < jsize; i++) {\n");
1098 fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n");
1099 fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n");
1100 fprintf(out, " if (jvalue_obj == NULL) { continue; }\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001101 fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n");
1102 fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n");
1103 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n");
Yangster-mace124e422018-08-16 10:30:28 -07001104 fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n");
1105 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n");
1106 fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n");
1107 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n");
1108 fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n");
1109 fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n");
1110 fprintf(out, " scoped_ufs.push_back(std::move(utf));\n");
1111 fprintf(out, " }\n");
1112 fprintf(out, " }\n");
1113}
1114
Yao Chend54f9dd2017-10-17 17:37:48 +00001115static int
Yangster-macba5b9e42018-01-10 21:31:59 -08001116write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
1117 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001118{
Yao Chend54f9dd2017-10-17 17:37:48 +00001119 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -08001120 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1121 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001122 int argIndex;
1123
Yao Chen97e21ec2018-03-29 11:00:38 -07001124 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001125 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -08001126 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001127 argIndex = 1;
1128 for (vector<java_type_t>::const_iterator arg = signature->begin();
1129 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001130 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1131 for (auto chainField : attributionDecl.fields) {
1132 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1133 chainField.name.c_str());
1134 }
Yangster-mace124e422018-08-16 10:30:28 -07001135 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1136 fprintf(out, ", jobject value_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001137 } else {
1138 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1139 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001140 argIndex++;
1141 }
1142 fprintf(out, ")\n");
1143
1144 fprintf(out, "{\n");
1145
1146 // Prepare strings
1147 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001148 bool hadStringOrChain = false;
Yangster-mace124e422018-08-16 10:30:28 -07001149 bool isKeyValuePairAtom = false;
Yao Chend54f9dd2017-10-17 17:37:48 +00001150 for (vector<java_type_t>::const_iterator arg = signature->begin();
1151 arg != signature->end(); arg++) {
1152 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001153 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001154 fprintf(out, " const char* str%d;\n", argIndex);
1155 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1156 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1157 argIndex, argIndex);
1158 fprintf(out, " } else {\n");
1159 fprintf(out, " str%d = NULL;\n", argIndex);
1160 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001161 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1162 hadStringOrChain = true;
1163 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1164 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chen1fe9f592018-12-06 10:34:25 -08001165 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen8e6f9982018-11-29 09:39:45 -08001166 fprintf(out,
1167 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1168 "0) {\n",
1169 argIndex, argIndex);
Yao Chenbbdd67d2018-10-24 12:15:56 -07001170 fprintf(out,
1171 " jbyte_array%d = "
1172 "env->GetByteArrayElements(arg%d, NULL);\n",
1173 argIndex, argIndex);
1174 fprintf(out,
Yao Chen1fe9f592018-12-06 10:34:25 -08001175 " str%d_length = env->GetArrayLength(arg%d);\n",
1176 argIndex, argIndex);
1177 fprintf(out,
Yao Chenbbdd67d2018-10-24 12:15:56 -07001178 " str%d = "
1179 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1180 "d, NULL));\n",
1181 argIndex, argIndex);
1182 fprintf(out, " } else {\n");
1183 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1184 fprintf(out, " str%d = NULL;\n", argIndex);
1185 fprintf(out, " }\n");
1186
Yao Chene89572c2019-01-09 15:41:50 -08001187 fprintf(out,
1188 " android::util::BytesField bytesField%d(str%d, "
1189 "str%d_length);",
1190 argIndex, argIndex, argIndex);
1191
Yangster-mac7604aea2017-12-11 22:55:49 -08001192 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1193 hadStringOrChain = true;
1194 for (auto chainField : attributionDecl.fields) {
1195 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1196 chainField.name.c_str(), chainField.name.c_str());
1197 if (chainField.name != attributionDecl.fields.front().name) {
1198 fprintf(out, " if (%s_length != %s_length) {\n",
1199 chainField.name.c_str(),
1200 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001201 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001202 fprintf(out, " }\n");
1203 }
1204 if (chainField.javaType == JAVA_TYPE_INT) {
1205 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1206 chainField.name.c_str(), chainField.name.c_str());
1207 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1208 fprintf(out, " std::vector<%s> %s_vec;\n",
1209 cpp_type_name(chainField.javaType), chainField.name.c_str());
1210 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1211 chainField.name.c_str());
1212 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1213 chainField.name.c_str());
1214 fprintf(out, " jstring jstr = "
1215 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1216 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001217 fprintf(out, " if (jstr == NULL) {\n");
1218 fprintf(out, " %s_vec.push_back(NULL);\n",
1219 chainField.name.c_str());
1220 fprintf(out, " } else {\n");
1221 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001222 "new ScopedUtfChars(env, jstr);\n",
1223 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001224 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001225 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001226 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001227 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001228 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001229 fprintf(out, " }\n");
1230 }
1231 fprintf(out, "\n");
1232 }
Yangster-mace124e422018-08-16 10:30:28 -07001233 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1234 isKeyValuePairAtom = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001235 }
1236 argIndex++;
1237 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001238 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1239 // chains.
Yangster-mace124e422018-08-16 10:30:28 -07001240 if (!hadStringOrChain && !isKeyValuePairAtom) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001241 fprintf(out, " (void)env;\n");
1242 }
Yangster-mace124e422018-08-16 10:30:28 -07001243 if (isKeyValuePairAtom) {
1244 write_key_value_map_jni(out);
1245 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001246
1247 // stats_write call
1248 argIndex = 1;
Yao Chene89572c2019-01-09 15:41:50 -08001249 fprintf(out, "\n int ret = android::util::%s(code",
1250 cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001251 for (vector<java_type_t>::const_iterator arg = signature->begin();
1252 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001253 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1254 for (auto chainField : attributionDecl.fields) {
1255 if (chainField.javaType == JAVA_TYPE_INT) {
1256 fprintf(out, ", (const %s*)%s_array, %s_length",
1257 cpp_type_name(chainField.javaType),
1258 chainField.name.c_str(), chainField.name.c_str());
1259 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1260 fprintf(out, ", %s_vec", chainField.name.c_str());
1261 }
1262 }
Yangster-mace124e422018-08-16 10:30:28 -07001263 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -07001264 fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map");
Yao Chene89572c2019-01-09 15:41:50 -08001265 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1266 fprintf(out, ", bytesField%d", argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -08001267 } else {
Yao Chene89572c2019-01-09 15:41:50 -08001268 const char* argName =
1269 (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001270 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1271 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001272 argIndex++;
1273 }
1274 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001275 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001276
1277 // Clean up strings
1278 argIndex = 1;
1279 for (vector<java_type_t>::const_iterator arg = signature->begin();
1280 arg != signature->end(); arg++) {
1281 if (*arg == JAVA_TYPE_STRING) {
1282 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1283 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1284 argIndex, argIndex);
1285 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001286 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1287 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1288 fprintf(out,
1289 " env->ReleaseByteArrayElements(arg%d, "
1290 "jbyte_array%d, 0);\n",
1291 argIndex, argIndex);
1292 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001293 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1294 for (auto chainField : attributionDecl.fields) {
1295 if (chainField.javaType == JAVA_TYPE_INT) {
1296 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1297 chainField.name.c_str(), chainField.name.c_str());
1298 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001299 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001300 chainField.name.c_str());
1301 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1302 fprintf(out, " }\n");
1303 }
1304 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001305 }
1306 argIndex++;
1307 }
Yangster-mace124e422018-08-16 10:30:28 -07001308
Yao Chen97e21ec2018-03-29 11:00:38 -07001309 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001310
1311 fprintf(out, "}\n");
1312 fprintf(out, "\n");
1313 }
1314
Yangster-macba5b9e42018-01-10 21:31:59 -08001315
1316 return 0;
1317}
1318
1319void write_jni_registration(FILE* out, const string& java_method_name,
1320 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
1321 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1322 signature != signatures.end(); signature++) {
1323 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1324 java_method_name.c_str(),
1325 jni_function_signature(*signature, attributionDecl).c_str(),
1326 jni_function_name(java_method_name, *signature).c_str());
1327 }
1328}
1329
1330static int
1331write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1332{
1333 // Print prelude
1334 fprintf(out, "// This file is autogenerated\n");
1335 fprintf(out, "\n");
1336
1337 fprintf(out, "#include <statslog.h>\n");
1338 fprintf(out, "\n");
1339 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1340 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1341 fprintf(out, "#include <utils/Vector.h>\n");
1342 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1343 fprintf(out, "#include \"jni.h\"\n");
1344 fprintf(out, "\n");
1345 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1346 fprintf(out, "\n");
1347
1348 fprintf(out, "namespace android {\n");
1349 fprintf(out, "\n");
1350
1351 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
1352 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
1353 atoms.non_chained_signatures, attributionDecl);
1354
Yao Chend54f9dd2017-10-17 17:37:48 +00001355 // Print registration function table
1356 fprintf(out, "/*\n");
1357 fprintf(out, " * JNI registration.\n");
1358 fprintf(out, " */\n");
1359 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -08001360 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
1361 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001362 fprintf(out, "};\n");
1363 fprintf(out, "\n");
1364
1365 // Print registration function
1366 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
1367 fprintf(out, " return RegisterMethodsOrDie(\n");
1368 fprintf(out, " env,\n");
1369 fprintf(out, " \"android/util/StatsLog\",\n");
1370 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1371 fprintf(out, "}\n");
1372
1373 fprintf(out, "\n");
1374 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001375 return 0;
1376}
1377
Yao Chend54f9dd2017-10-17 17:37:48 +00001378static void
1379print_usage()
1380{
1381 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1382 fprintf(stderr, "\n");
1383 fprintf(stderr, "OPTIONS\n");
1384 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1385 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1386 fprintf(stderr, " --help this message\n");
1387 fprintf(stderr, " --java FILENAME the java file to output\n");
1388 fprintf(stderr, " --jni FILENAME the jni file to output\n");
1389}
1390
1391/**
1392 * Do the argument parsing and execute the tasks.
1393 */
1394static int
1395run(int argc, char const*const* argv)
1396{
1397 string cppFilename;
1398 string headerFilename;
1399 string javaFilename;
1400 string jniFilename;
1401
1402 int index = 1;
1403 while (index < argc) {
1404 if (0 == strcmp("--help", argv[index])) {
1405 print_usage();
1406 return 0;
1407 } else if (0 == strcmp("--cpp", argv[index])) {
1408 index++;
1409 if (index >= argc) {
1410 print_usage();
1411 return 1;
1412 }
1413 cppFilename = argv[index];
1414 } else if (0 == strcmp("--header", argv[index])) {
1415 index++;
1416 if (index >= argc) {
1417 print_usage();
1418 return 1;
1419 }
1420 headerFilename = argv[index];
1421 } else if (0 == strcmp("--java", argv[index])) {
1422 index++;
1423 if (index >= argc) {
1424 print_usage();
1425 return 1;
1426 }
1427 javaFilename = argv[index];
1428 } else if (0 == strcmp("--jni", argv[index])) {
1429 index++;
1430 if (index >= argc) {
1431 print_usage();
1432 return 1;
1433 }
1434 jniFilename = argv[index];
1435 }
1436 index++;
1437 }
1438
1439 if (cppFilename.size() == 0
1440 && headerFilename.size() == 0
1441 && javaFilename.size() == 0
1442 && jniFilename.size() == 0) {
1443 print_usage();
1444 return 1;
1445 }
1446
1447 // Collate the parameters
1448 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001449 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001450 if (errorCount != 0) {
1451 return 1;
1452 }
1453
Yangster-mac7604aea2017-12-11 22:55:49 -08001454 AtomDecl attributionDecl;
1455 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001456 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001457 &attributionDecl, &attributionSignature);
1458
Yao Chend54f9dd2017-10-17 17:37:48 +00001459 // Write the .cpp file
1460 if (cppFilename.size() != 0) {
1461 FILE* out = fopen(cppFilename.c_str(), "w");
1462 if (out == NULL) {
1463 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1464 return 1;
1465 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001466 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1467 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001468 fclose(out);
1469 }
1470
1471 // Write the .h file
1472 if (headerFilename.size() != 0) {
1473 FILE* out = fopen(headerFilename.c_str(), "w");
1474 if (out == NULL) {
1475 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1476 return 1;
1477 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001478 errorCount = android::stats_log_api_gen::write_stats_log_header(
1479 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001480 fclose(out);
1481 }
1482
1483 // Write the .java file
1484 if (javaFilename.size() != 0) {
1485 FILE* out = fopen(javaFilename.c_str(), "w");
1486 if (out == NULL) {
1487 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1488 return 1;
1489 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001490 errorCount = android::stats_log_api_gen::write_stats_log_java(
1491 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001492 fclose(out);
1493 }
1494
1495 // Write the jni file
1496 if (jniFilename.size() != 0) {
1497 FILE* out = fopen(jniFilename.c_str(), "w");
1498 if (out == NULL) {
1499 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1500 return 1;
1501 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001502 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1503 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001504 fclose(out);
1505 }
1506
1507 return 0;
1508}
1509
1510}
1511}
1512
1513/**
1514 * Main.
1515 */
1516int
1517main(int argc, char const*const* argv)
1518{
1519 GOOGLE_PROTOBUF_VERIFY_VERSION;
1520
1521 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07001522}