blob: bd6c3e3a5672938968c9a69b6255569415ebdacb [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;
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800677 // Print atom constants
Yao Chend54f9dd2017-10-17 17:37:48 +0000678 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
Muhammad Qureshidc132af2019-02-05 13:07:40 -0800703 // Print constants for the enum values.
704 fprintf(out, "//\n");
705 fprintf(out, "// Constants for enum values\n");
706 fprintf(out, "//\n\n");
707 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
708 atom != atoms.decls.end(); atom++) {
709 for (vector<AtomField>::const_iterator field = atom->fields.begin();
710 field != atom->fields.end(); field++) {
711 if (field->javaType == JAVA_TYPE_ENUM) {
712 fprintf(out, "// Values for %s.%s\n", atom->message.c_str(),
713 field->name.c_str());
714 for (map<int, string>::const_iterator value = field->enumValues.begin();
715 value != field->enumValues.end(); value++) {
716 fprintf(out, "const int32_t %s__%s__%s = %d;\n",
717 make_constant_name(atom->message).c_str(),
718 make_constant_name(field->name).c_str(),
719 make_constant_name(value->second).c_str(),
720 value->first);
721 }
722 fprintf(out, "\n");
723 }
724 }
725 }
726
Yao Chene89572c2019-01-09 15:41:50 -0800727 fprintf(out, "struct BytesField {\n");
728 fprintf(out,
729 " BytesField(char const* array, size_t len) : arg(array), "
730 "arg_length(len) {}\n");
731 fprintf(out, " char const* arg;\n");
732 fprintf(out, " size_t arg_length;\n");
733 fprintf(out, "};\n");
734 fprintf(out, "\n");
735
Yao Chen9c1debe2018-02-19 14:39:19 -0800736 fprintf(out, "struct StateAtomFieldOptions {\n");
737 fprintf(out, " std::vector<int> primaryFields;\n");
738 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700739 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800740 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700741
742 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800743 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700744 " const static std::set<int> "
745 "kNotTruncatingTimestampAtomWhiteList;\n");
746 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
747 fprintf(out,
748 " const static std::set<int> kAtomsWithAttributionChain;\n");
749 fprintf(out,
750 " const static std::map<int, StateAtomFieldOptions> "
751 "kStateAtomsFieldOptions;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700752 fprintf(out,
753 " const static std::map<int, std::vector<int>> "
754 "kBytesFieldAtoms;");
Yao Chen9c1debe2018-02-19 14:39:19 -0800755 fprintf(out, "};\n");
756
Yao Chenc40a19d2018-03-15 16:48:25 -0700757 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
758 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800759
Yao Chend54f9dd2017-10-17 17:37:48 +0000760 // Print write methods
761 fprintf(out, "//\n");
762 fprintf(out, "// Write methods\n");
763 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800764 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
765
766 fprintf(out, "//\n");
767 fprintf(out, "// Write flattened methods\n");
768 fprintf(out, "//\n");
769 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
770 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000771
772 fprintf(out, "\n");
773 fprintf(out, "} // namespace util\n");
774 fprintf(out, "} // namespace android\n");
775
776 return 0;
777}
778
Bookatz0bd97202018-06-05 12:42:37 -0700779static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
780 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800781 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
782 method_name.c_str(), atom_code_name.c_str());
783 for (vector<AtomField>::const_iterator field = atom.fields.begin();
784 field != atom.fields.end(); field++) {
785 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700786 fprintf(out, ", android.os.WorkSource workSource");
Yangster-mace124e422018-08-16 10:30:28 -0700787 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
788 fprintf(out, ", SparseArray<Object> value_map");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700789 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
790 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800791 } else {
792 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
793 }
794 }
Bookatz0bd97202018-06-05 12:42:37 -0700795 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800796}
797
798static void write_java_method(
799 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
800 const AtomDecl &attributionDecl) {
801 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
802 signature != signatures.end(); signature++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800803 fprintf(out, " /** @hide */\n");
Yao Chen97e21ec2018-03-29 11:00:38 -0700804 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800805 int argIndex = 1;
806 for (vector<java_type_t>::const_iterator arg = signature->begin();
807 arg != signature->end(); arg++) {
808 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
809 for (auto chainField : attributionDecl.fields) {
810 fprintf(out, ", %s[] %s",
811 java_type_name(chainField.javaType), chainField.name.c_str());
812 }
Yangster-mace124e422018-08-16 10:30:28 -0700813 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
814 fprintf(out, ", SparseArray<Object> value_map");
Yangster-macba5b9e42018-01-10 21:31:59 -0800815 } else {
816 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
817 }
818 argIndex++;
819 }
820 fprintf(out, ");\n");
821 }
822}
823
Bookatz0bd97202018-06-05 12:42:37 -0700824static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) {
825 fprintf(out, "\n // WorkSource methods.\n");
826 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
827 signature != signatures.end(); signature++) {
828 // Determine if there is Attribution in this signature.
829 int attributionArg = -1;
830 int argIndexMax = 0;
831 for (vector<java_type_t>::const_iterator arg = signature->begin();
832 arg != signature->end(); arg++) {
833 argIndexMax++;
834 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
835 if (attributionArg > -1) {
836 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
837 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
838 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
839 return;
840 }
841 attributionArg = argIndexMax;
842 }
843 }
844 if (attributionArg < 0) {
845 continue;
846 }
847
848 // Method header (signature)
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800849 fprintf(out, " /** @hide */\n");
Bookatz0bd97202018-06-05 12:42:37 -0700850 fprintf(out, " public static void write(int code");
851 int argIndex = 1;
852 for (vector<java_type_t>::const_iterator arg = signature->begin();
853 arg != signature->end(); arg++) {
854 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
855 fprintf(out, ", WorkSource ws");
856 } else {
857 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
858 }
859 argIndex++;
860 }
861 fprintf(out, ") {\n");
862
863 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
864 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
865 fprintf(out, " write_non_chained(code");
866 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
867 if (argIndex == attributionArg) {
868 fprintf(out, ", ws.get(i), ws.getName(i)");
869 } else {
870 fprintf(out, ", arg%d", argIndex);
871 }
872 }
873 fprintf(out, ");\n");
874 fprintf(out, " }\n"); // close flor-loop
875
876 // write() component.
877 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
878 fprintf(out, " if (workChains != null) {\n");
879 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
880 fprintf(out, " write(code");
881 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
882 if (argIndex == attributionArg) {
883 fprintf(out, ", wc.getUids(), wc.getTags()");
884 } else {
885 fprintf(out, ", arg%d", argIndex);
886 }
887 }
888 fprintf(out, ");\n");
889 fprintf(out, " }\n"); // close for-loop
890 fprintf(out, " }\n"); // close if
891 fprintf(out, " }\n"); // close method
892 }
893}
Yangster-macba5b9e42018-01-10 21:31:59 -0800894
Yao Chend54f9dd2017-10-17 17:37:48 +0000895static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800896write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000897{
Yao Chend54f9dd2017-10-17 17:37:48 +0000898 // Print prelude
899 fprintf(out, "// This file is autogenerated\n");
900 fprintf(out, "\n");
901 fprintf(out, "package android.util;\n");
902 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -0700903 fprintf(out, "import android.os.WorkSource;\n");
Yangster-mace124e422018-08-16 10:30:28 -0700904 fprintf(out, "import android.util.SparseArray;\n");
Bookatz0bd97202018-06-05 12:42:37 -0700905 fprintf(out, "import java.util.ArrayList;\n");
906 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000907 fprintf(out, "\n");
908 fprintf(out, "/**\n");
909 fprintf(out, " * API For logging statistics events.\n");
910 fprintf(out, " * @hide\n");
911 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800912 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700913 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000914
Yangster-macba5b9e42018-01-10 21:31:59 -0800915 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
916 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
917
Stefan Lafon9478f352017-10-30 21:20:20 -0700918 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000919 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
920 atom != atoms.decls.end(); atom++) {
921 string constant = make_constant_name(atom->name);
922 fprintf(out, "\n");
923 fprintf(out, " /**\n");
Bookatz0bd97202018-06-05 12:42:37 -0700924 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
925 write_java_usage(out, "write", constant, *atom);
Yangster-macba5b9e42018-01-10 21:31:59 -0800926 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
927 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
Bookatz0bd97202018-06-05 12:42:37 -0700928 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
Yao Chend54f9dd2017-10-17 17:37:48 +0000929 }
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800930 fprintf(out, " * @hide\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000931 fprintf(out, " */\n");
932 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
933 }
934 fprintf(out, "\n");
935
Stefan Lafon9478f352017-10-30 21:20:20 -0700936 // Print constants for the enum values.
937 fprintf(out, " // Constants for enum values.\n\n");
938 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800939 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700940 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800941 field != atom->fields.end(); field++) {
942 if (field->javaType == JAVA_TYPE_ENUM) {
943 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
944 field->name.c_str());
945 for (map<int, string>::const_iterator value = field->enumValues.begin();
946 value != field->enumValues.end(); value++) {
Tor Norbye0f2dc8d2019-01-08 12:07:15 -0800947 fprintf(out, " /** @hide */\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800948 fprintf(out, " public static final int %s__%s__%s = %d;\n",
949 make_constant_name(atom->message).c_str(),
950 make_constant_name(field->name).c_str(),
951 make_constant_name(value->second).c_str(),
952 value->first);
953 }
954 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700955 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700956 }
957 }
958
Yao Chend54f9dd2017-10-17 17:37:48 +0000959 // Print write methods
960 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800961 write_java_method(out, "write", atoms.signatures, attributionDecl);
962 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Bookatz0bd97202018-06-05 12:42:37 -0700963 write_java_work_source_method(out, atoms.signatures);
Yao Chend54f9dd2017-10-17 17:37:48 +0000964
965 fprintf(out, "}\n");
966
967 return 0;
968}
969
970static const char*
971jni_type_name(java_type_t type)
972{
973 switch (type) {
974 case JAVA_TYPE_BOOLEAN:
975 return "jboolean";
976 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700977 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000978 return "jint";
979 case JAVA_TYPE_LONG:
980 return "jlong";
981 case JAVA_TYPE_FLOAT:
982 return "jfloat";
983 case JAVA_TYPE_DOUBLE:
984 return "jdouble";
985 case JAVA_TYPE_STRING:
986 return "jstring";
Yao Chenbbdd67d2018-10-24 12:15:56 -0700987 case JAVA_TYPE_BYTE_ARRAY:
988 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +0000989 default:
990 return "UNKNOWN";
991 }
992}
993
Yangster-mac7604aea2017-12-11 22:55:49 -0800994static const char*
995jni_array_type_name(java_type_t type)
996{
997 switch (type) {
998 case JAVA_TYPE_INT:
999 return "jintArray";
Yangster-mace124e422018-08-16 10:30:28 -07001000 case JAVA_TYPE_FLOAT:
1001 return "jfloatArray";
Yangster-mac7604aea2017-12-11 22:55:49 -08001002 case JAVA_TYPE_STRING:
1003 return "jobjectArray";
1004 default:
1005 return "UNKNOWN";
1006 }
1007}
1008
Yao Chend54f9dd2017-10-17 17:37:48 +00001009static string
Yangster-macba5b9e42018-01-10 21:31:59 -08001010jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +00001011{
Yangster-macba5b9e42018-01-10 21:31:59 -08001012 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +00001013 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001014 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001015 switch (*arg) {
1016 case JAVA_TYPE_BOOLEAN:
1017 result += "_boolean";
1018 break;
1019 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001020 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001021 result += "_int";
1022 break;
1023 case JAVA_TYPE_LONG:
1024 result += "_long";
1025 break;
1026 case JAVA_TYPE_FLOAT:
1027 result += "_float";
1028 break;
1029 case JAVA_TYPE_DOUBLE:
1030 result += "_double";
1031 break;
1032 case JAVA_TYPE_STRING:
1033 result += "_String";
1034 break;
Yangster-mac7604aea2017-12-11 22:55:49 -08001035 case JAVA_TYPE_ATTRIBUTION_CHAIN:
1036 result += "_AttributionChain";
1037 break;
Yangster-mace124e422018-08-16 10:30:28 -07001038 case JAVA_TYPE_KEY_VALUE_PAIR:
1039 result += "_KeyValuePairs";
1040 break;
Yao Chenbbdd67d2018-10-24 12:15:56 -07001041 case JAVA_TYPE_BYTE_ARRAY:
1042 result += "_bytes";
1043 break;
Yao Chend54f9dd2017-10-17 17:37:48 +00001044 default:
1045 result += "_UNKNOWN";
1046 break;
1047 }
1048 }
1049 return result;
1050}
1051
1052static const char*
1053java_type_signature(java_type_t type)
1054{
1055 switch (type) {
1056 case JAVA_TYPE_BOOLEAN:
1057 return "Z";
1058 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001059 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001060 return "I";
1061 case JAVA_TYPE_LONG:
1062 return "J";
1063 case JAVA_TYPE_FLOAT:
1064 return "F";
1065 case JAVA_TYPE_DOUBLE:
1066 return "D";
1067 case JAVA_TYPE_STRING:
1068 return "Ljava/lang/String;";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001069 case JAVA_TYPE_BYTE_ARRAY:
1070 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +00001071 default:
1072 return "UNKNOWN";
1073 }
1074}
1075
1076static string
Yangster-mac7604aea2017-12-11 22:55:49 -08001077jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001078{
1079 string result("(I");
1080 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001081 arg != signature.end(); arg++) {
1082 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1083 for (auto chainField : attributionDecl.fields) {
1084 result += "[";
1085 result += java_type_signature(chainField.javaType);
1086 }
Yangster-mace124e422018-08-16 10:30:28 -07001087 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1088 result += "Landroid/util/SparseArray;";
Yangster-mac7604aea2017-12-11 22:55:49 -08001089 } else {
1090 result += java_type_signature(*arg);
1091 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001092 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001093 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +00001094 return result;
1095}
1096
Yangster-mace124e422018-08-16 10:30:28 -07001097static void write_key_value_map_jni(FILE* out) {
Howard Ro4078dd42018-09-27 17:41:08 -07001098 fprintf(out, " std::map<int, int32_t> int32_t_map;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001099 fprintf(out, " std::map<int, int64_t> int64_t_map;\n");
1100 fprintf(out, " std::map<int, float> float_map;\n");
1101 fprintf(out, " std::map<int, char const*> string_map;\n\n");
1102
1103 fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n");
1104
1105 fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n");
1106 fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n");
1107 fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n");
1108
1109
1110 fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n");
1111
Howard Ro4078dd42018-09-27 17:41:08 -07001112 fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001113 fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n");
1114 fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n");
1115 fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001116 fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001117 fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n");
1118 fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n");
1119
1120 fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n");
1121 fprintf(out, " for(int i = 0; i < jsize; i++) {\n");
1122 fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n");
1123 fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n");
1124 fprintf(out, " if (jvalue_obj == NULL) { continue; }\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001125 fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n");
1126 fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n");
1127 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n");
Yangster-mace124e422018-08-16 10:30:28 -07001128 fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n");
1129 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n");
1130 fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n");
1131 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n");
1132 fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n");
1133 fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n");
1134 fprintf(out, " scoped_ufs.push_back(std::move(utf));\n");
1135 fprintf(out, " }\n");
1136 fprintf(out, " }\n");
1137}
1138
Yao Chend54f9dd2017-10-17 17:37:48 +00001139static int
Yangster-macba5b9e42018-01-10 21:31:59 -08001140write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
1141 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001142{
Yao Chend54f9dd2017-10-17 17:37:48 +00001143 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -08001144 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1145 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001146 int argIndex;
1147
Yao Chen97e21ec2018-03-29 11:00:38 -07001148 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001149 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -08001150 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001151 argIndex = 1;
1152 for (vector<java_type_t>::const_iterator arg = signature->begin();
1153 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001154 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1155 for (auto chainField : attributionDecl.fields) {
1156 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1157 chainField.name.c_str());
1158 }
Yangster-mace124e422018-08-16 10:30:28 -07001159 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1160 fprintf(out, ", jobject value_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001161 } else {
1162 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1163 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001164 argIndex++;
1165 }
1166 fprintf(out, ")\n");
1167
1168 fprintf(out, "{\n");
1169
1170 // Prepare strings
1171 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001172 bool hadStringOrChain = false;
Yangster-mace124e422018-08-16 10:30:28 -07001173 bool isKeyValuePairAtom = false;
Yao Chend54f9dd2017-10-17 17:37:48 +00001174 for (vector<java_type_t>::const_iterator arg = signature->begin();
1175 arg != signature->end(); arg++) {
1176 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001177 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001178 fprintf(out, " const char* str%d;\n", argIndex);
1179 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1180 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1181 argIndex, argIndex);
1182 fprintf(out, " } else {\n");
1183 fprintf(out, " str%d = NULL;\n", argIndex);
1184 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001185 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1186 hadStringOrChain = true;
1187 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1188 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chen1fe9f592018-12-06 10:34:25 -08001189 fprintf(out, " int str%d_length = 0;\n", argIndex);
Yao Chen8e6f9982018-11-29 09:39:45 -08001190 fprintf(out,
1191 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1192 "0) {\n",
1193 argIndex, argIndex);
Yao Chenbbdd67d2018-10-24 12:15:56 -07001194 fprintf(out,
1195 " jbyte_array%d = "
1196 "env->GetByteArrayElements(arg%d, NULL);\n",
1197 argIndex, argIndex);
1198 fprintf(out,
Yao Chen1fe9f592018-12-06 10:34:25 -08001199 " str%d_length = env->GetArrayLength(arg%d);\n",
1200 argIndex, argIndex);
1201 fprintf(out,
Yao Chenbbdd67d2018-10-24 12:15:56 -07001202 " str%d = "
1203 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1204 "d, NULL));\n",
1205 argIndex, argIndex);
1206 fprintf(out, " } else {\n");
1207 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1208 fprintf(out, " str%d = NULL;\n", argIndex);
1209 fprintf(out, " }\n");
1210
Yao Chene89572c2019-01-09 15:41:50 -08001211 fprintf(out,
1212 " android::util::BytesField bytesField%d(str%d, "
1213 "str%d_length);",
1214 argIndex, argIndex, argIndex);
1215
Yangster-mac7604aea2017-12-11 22:55:49 -08001216 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1217 hadStringOrChain = true;
1218 for (auto chainField : attributionDecl.fields) {
1219 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1220 chainField.name.c_str(), chainField.name.c_str());
1221 if (chainField.name != attributionDecl.fields.front().name) {
1222 fprintf(out, " if (%s_length != %s_length) {\n",
1223 chainField.name.c_str(),
1224 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001225 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001226 fprintf(out, " }\n");
1227 }
1228 if (chainField.javaType == JAVA_TYPE_INT) {
1229 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1230 chainField.name.c_str(), chainField.name.c_str());
1231 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1232 fprintf(out, " std::vector<%s> %s_vec;\n",
1233 cpp_type_name(chainField.javaType), chainField.name.c_str());
1234 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1235 chainField.name.c_str());
1236 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1237 chainField.name.c_str());
1238 fprintf(out, " jstring jstr = "
1239 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1240 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001241 fprintf(out, " if (jstr == NULL) {\n");
1242 fprintf(out, " %s_vec.push_back(NULL);\n",
1243 chainField.name.c_str());
1244 fprintf(out, " } else {\n");
1245 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001246 "new ScopedUtfChars(env, jstr);\n",
1247 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001248 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001249 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001250 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001251 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001252 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001253 fprintf(out, " }\n");
1254 }
1255 fprintf(out, "\n");
1256 }
Yangster-mace124e422018-08-16 10:30:28 -07001257 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1258 isKeyValuePairAtom = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001259 }
1260 argIndex++;
1261 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001262 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1263 // chains.
Yangster-mace124e422018-08-16 10:30:28 -07001264 if (!hadStringOrChain && !isKeyValuePairAtom) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001265 fprintf(out, " (void)env;\n");
1266 }
Yangster-mace124e422018-08-16 10:30:28 -07001267 if (isKeyValuePairAtom) {
1268 write_key_value_map_jni(out);
1269 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001270
1271 // stats_write call
1272 argIndex = 1;
Yao Chene89572c2019-01-09 15:41:50 -08001273 fprintf(out, "\n int ret = android::util::%s(code",
1274 cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001275 for (vector<java_type_t>::const_iterator arg = signature->begin();
1276 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001277 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1278 for (auto chainField : attributionDecl.fields) {
1279 if (chainField.javaType == JAVA_TYPE_INT) {
1280 fprintf(out, ", (const %s*)%s_array, %s_length",
1281 cpp_type_name(chainField.javaType),
1282 chainField.name.c_str(), chainField.name.c_str());
1283 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1284 fprintf(out, ", %s_vec", chainField.name.c_str());
1285 }
1286 }
Yangster-mace124e422018-08-16 10:30:28 -07001287 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -07001288 fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map");
Yao Chene89572c2019-01-09 15:41:50 -08001289 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1290 fprintf(out, ", bytesField%d", argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -08001291 } else {
Yao Chene89572c2019-01-09 15:41:50 -08001292 const char* argName =
1293 (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001294 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1295 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001296 argIndex++;
1297 }
1298 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001299 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001300
1301 // Clean up strings
1302 argIndex = 1;
1303 for (vector<java_type_t>::const_iterator arg = signature->begin();
1304 arg != signature->end(); arg++) {
1305 if (*arg == JAVA_TYPE_STRING) {
1306 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1307 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1308 argIndex, argIndex);
1309 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001310 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1311 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1312 fprintf(out,
1313 " env->ReleaseByteArrayElements(arg%d, "
1314 "jbyte_array%d, 0);\n",
1315 argIndex, argIndex);
1316 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001317 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1318 for (auto chainField : attributionDecl.fields) {
1319 if (chainField.javaType == JAVA_TYPE_INT) {
1320 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1321 chainField.name.c_str(), chainField.name.c_str());
1322 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001323 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001324 chainField.name.c_str());
1325 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1326 fprintf(out, " }\n");
1327 }
1328 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001329 }
1330 argIndex++;
1331 }
Yangster-mace124e422018-08-16 10:30:28 -07001332
Yao Chen97e21ec2018-03-29 11:00:38 -07001333 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001334
1335 fprintf(out, "}\n");
1336 fprintf(out, "\n");
1337 }
1338
Yangster-macba5b9e42018-01-10 21:31:59 -08001339
1340 return 0;
1341}
1342
1343void write_jni_registration(FILE* out, const string& java_method_name,
1344 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
1345 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1346 signature != signatures.end(); signature++) {
1347 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1348 java_method_name.c_str(),
1349 jni_function_signature(*signature, attributionDecl).c_str(),
1350 jni_function_name(java_method_name, *signature).c_str());
1351 }
1352}
1353
1354static int
1355write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1356{
1357 // Print prelude
1358 fprintf(out, "// This file is autogenerated\n");
1359 fprintf(out, "\n");
1360
1361 fprintf(out, "#include <statslog.h>\n");
1362 fprintf(out, "\n");
1363 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1364 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1365 fprintf(out, "#include <utils/Vector.h>\n");
1366 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1367 fprintf(out, "#include \"jni.h\"\n");
1368 fprintf(out, "\n");
1369 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1370 fprintf(out, "\n");
1371
1372 fprintf(out, "namespace android {\n");
1373 fprintf(out, "\n");
1374
1375 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
1376 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
1377 atoms.non_chained_signatures, attributionDecl);
1378
Yao Chend54f9dd2017-10-17 17:37:48 +00001379 // Print registration function table
1380 fprintf(out, "/*\n");
1381 fprintf(out, " * JNI registration.\n");
1382 fprintf(out, " */\n");
1383 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -08001384 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
1385 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001386 fprintf(out, "};\n");
1387 fprintf(out, "\n");
1388
1389 // Print registration function
1390 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
1391 fprintf(out, " return RegisterMethodsOrDie(\n");
1392 fprintf(out, " env,\n");
1393 fprintf(out, " \"android/util/StatsLog\",\n");
1394 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1395 fprintf(out, "}\n");
1396
1397 fprintf(out, "\n");
1398 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001399 return 0;
1400}
1401
Yao Chend54f9dd2017-10-17 17:37:48 +00001402static void
1403print_usage()
1404{
1405 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1406 fprintf(stderr, "\n");
1407 fprintf(stderr, "OPTIONS\n");
1408 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1409 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1410 fprintf(stderr, " --help this message\n");
1411 fprintf(stderr, " --java FILENAME the java file to output\n");
1412 fprintf(stderr, " --jni FILENAME the jni file to output\n");
1413}
1414
1415/**
1416 * Do the argument parsing and execute the tasks.
1417 */
1418static int
1419run(int argc, char const*const* argv)
1420{
1421 string cppFilename;
1422 string headerFilename;
1423 string javaFilename;
1424 string jniFilename;
1425
1426 int index = 1;
1427 while (index < argc) {
1428 if (0 == strcmp("--help", argv[index])) {
1429 print_usage();
1430 return 0;
1431 } else if (0 == strcmp("--cpp", argv[index])) {
1432 index++;
1433 if (index >= argc) {
1434 print_usage();
1435 return 1;
1436 }
1437 cppFilename = argv[index];
1438 } else if (0 == strcmp("--header", argv[index])) {
1439 index++;
1440 if (index >= argc) {
1441 print_usage();
1442 return 1;
1443 }
1444 headerFilename = argv[index];
1445 } else if (0 == strcmp("--java", argv[index])) {
1446 index++;
1447 if (index >= argc) {
1448 print_usage();
1449 return 1;
1450 }
1451 javaFilename = argv[index];
1452 } else if (0 == strcmp("--jni", argv[index])) {
1453 index++;
1454 if (index >= argc) {
1455 print_usage();
1456 return 1;
1457 }
1458 jniFilename = argv[index];
1459 }
1460 index++;
1461 }
1462
1463 if (cppFilename.size() == 0
1464 && headerFilename.size() == 0
1465 && javaFilename.size() == 0
1466 && jniFilename.size() == 0) {
1467 print_usage();
1468 return 1;
1469 }
1470
1471 // Collate the parameters
1472 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001473 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001474 if (errorCount != 0) {
1475 return 1;
1476 }
1477
Yangster-mac7604aea2017-12-11 22:55:49 -08001478 AtomDecl attributionDecl;
1479 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001480 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001481 &attributionDecl, &attributionSignature);
1482
Yao Chend54f9dd2017-10-17 17:37:48 +00001483 // Write the .cpp file
1484 if (cppFilename.size() != 0) {
1485 FILE* out = fopen(cppFilename.c_str(), "w");
1486 if (out == NULL) {
1487 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1488 return 1;
1489 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001490 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1491 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001492 fclose(out);
1493 }
1494
1495 // Write the .h file
1496 if (headerFilename.size() != 0) {
1497 FILE* out = fopen(headerFilename.c_str(), "w");
1498 if (out == NULL) {
1499 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1500 return 1;
1501 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001502 errorCount = android::stats_log_api_gen::write_stats_log_header(
1503 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001504 fclose(out);
1505 }
1506
1507 // Write the .java file
1508 if (javaFilename.size() != 0) {
1509 FILE* out = fopen(javaFilename.c_str(), "w");
1510 if (out == NULL) {
1511 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1512 return 1;
1513 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001514 errorCount = android::stats_log_api_gen::write_stats_log_java(
1515 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001516 fclose(out);
1517 }
1518
1519 // Write the jni file
1520 if (jniFilename.size() != 0) {
1521 FILE* out = fopen(jniFilename.c_str(), "w");
1522 if (out == NULL) {
1523 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1524 return 1;
1525 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001526 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1527 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001528 fclose(out);
1529 }
1530
1531 return 0;
1532}
1533
1534}
1535}
1536
1537/**
1538 * Main.
1539 */
1540int
1541main(int argc, char const*const* argv)
1542{
1543 GOOGLE_PROTOBUF_VERIFY_VERSION;
1544
1545 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07001546}