blob: 88b7e2e9de21148f9d176cfb2db2b8220b35a0b9 [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:
70 return "char const*";
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");
Yao Chencf3829a2018-06-05 14:20:35 -0700109 fprintf(out, "#include <cutils/properties.h>\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700110 fprintf(out, "#include <stats_event_list.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000111 fprintf(out, "#include <log/log.h>\n");
112 fprintf(out, "#include <statslog.h>\n");
Yangster-mac330af582018-02-08 15:24:38 -0800113 fprintf(out, "#include <utils/SystemClock.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000114 fprintf(out, "\n");
115
116 fprintf(out, "namespace android {\n");
117 fprintf(out, "namespace util {\n");
Yao Chen80235402017-11-13 20:42:25 -0800118 fprintf(out, "// the single event tag id for all stats logs\n");
119 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700120 fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000121
Yao Chenc40a19d2018-03-15 16:48:25 -0700122 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
123 "audio_state_changed",
124 "call_state_changed",
125 "phone_signal_strength_changed",
126 "mobile_bytes_transfer_by_fg_bg",
127 "mobile_bytes_transfer"};
128 fprintf(out,
129 "const std::set<int> "
130 "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n");
131 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
132 atom != atoms.decls.end(); atom++) {
133 if (kTruncatingAtomNames.find(atom->name) ==
134 kTruncatingAtomNames.end()) {
135 string constant = make_constant_name(atom->name);
136 fprintf(out, " %s,\n", constant.c_str());
137 }
138 }
139 fprintf(out, "};\n");
140 fprintf(out, "\n");
141
142 fprintf(out,
143 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
144 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
145 atom != atoms.decls.end(); atom++) {
146 for (vector<AtomField>::const_iterator field = atom->fields.begin();
147 field != atom->fields.end(); field++) {
148 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
149 string constant = make_constant_name(atom->name);
150 fprintf(out, " %s,\n", constant.c_str());
151 break;
152 }
153 }
154 }
155 fprintf(out, "};\n");
156 fprintf(out, "\n");
157
Yangster-macca5c0862018-04-09 22:39:53 -0700158 fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700159 fprintf(out, " std::map<int, int> uidField;\n");
160 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
161 atom != atoms.decls.end(); atom++) {
162 if (atom->uidField == 0) {
163 continue;
164 }
165 fprintf(out,
166 "\n // Adding uid field for atom "
167 "(%d)%s\n",
168 atom->code, atom->name.c_str());
169 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
170 make_constant_name(atom->name).c_str(), atom->uidField);
171 }
172
173 fprintf(out, " return uidField;\n");
174 fprintf(out, "};\n");
175
176 fprintf(out,
177 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
178 "getAtomUidField();\n");
179
180 fprintf(out,
181 "static std::map<int, StateAtomFieldOptions> "
182 "getStateAtomFieldOptions() {\n");
183 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
184 fprintf(out, " StateAtomFieldOptions opt;\n");
185 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
186 atom != atoms.decls.end(); atom++) {
187 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
188 continue;
189 }
190 fprintf(out,
191 "\n // Adding primary and exclusive fields for atom "
192 "(%d)%s\n",
193 atom->code, atom->name.c_str());
194 fprintf(out, " opt.primaryFields.clear();\n");
195 for (const auto& field : atom->primaryFields) {
196 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
197 }
198
199 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
200 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
201 make_constant_name(atom->name).c_str());
202 }
203
204 fprintf(out, " return options;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700205 fprintf(out, "}\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700206
207 fprintf(out,
208 "const std::map<int, StateAtomFieldOptions> "
209 "AtomsInfo::kStateAtomsFieldOptions = "
210 "getStateAtomFieldOptions();\n");
211
Yao Chenbbdd67d2018-10-24 12:15:56 -0700212 fprintf(out,
213 "static std::map<int, std::vector<int>> "
214 "getBinaryFieldAtoms() {\n");
215 fprintf(out, " std::map<int, std::vector<int>> options;\n");
216 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
217 atom != atoms.decls.end(); atom++) {
218 if (atom->binaryFields.size() == 0) {
219 continue;
220 }
221 fprintf(out,
222 "\n // Adding binary fields for atom "
223 "(%d)%s\n",
224 atom->code, atom->name.c_str());
225
226 for (const auto& field : atom->binaryFields) {
227 fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
228 make_constant_name(atom->name).c_str(), field);
229 }
230 }
231
232 fprintf(out, " return options;\n");
233 fprintf(out, "}\n");
234
235 fprintf(out,
236 "const std::map<int, std::vector<int>> "
237 "AtomsInfo::kBytesFieldAtoms = "
238 "getBinaryFieldAtoms();\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700239
240 fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
241 fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
242 fprintf(out, "static std::mutex mLogdRetryMutex;\n");
243
Yao Chend54f9dd2017-10-17 17:37:48 +0000244 // Print write methods
245 fprintf(out, "\n");
246 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800247 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000248 int argIndex;
249
Yao Chen97e21ec2018-03-29 11:00:38 -0700250 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700251 fprintf(out, "try_stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000252 argIndex = 1;
253 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800254 arg != signature->end(); arg++) {
255 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
256 for (auto chainField : attributionDecl.fields) {
257 if (chainField.javaType == JAVA_TYPE_STRING) {
258 fprintf(out, ", const std::vector<%s>& %s",
259 cpp_type_name(chainField.javaType),
260 chainField.name.c_str());
261 } else {
262 fprintf(out, ", const %s* %s, size_t %s_length",
263 cpp_type_name(chainField.javaType),
264 chainField.name.c_str(), chainField.name.c_str());
265 }
266 }
Yangster-mace124e422018-08-16 10:30:28 -0700267 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700268 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
269 "const std::map<int, int64_t>& arg%d_2, "
270 "const std::map<int, char const*>& arg%d_3, "
271 "const std::map<int, float>& arg%d_4",
272 argIndex, argIndex, argIndex, argIndex);
Yangster-mac7604aea2017-12-11 22:55:49 -0800273 } else {
274 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
275 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000276 argIndex++;
277 }
278 fprintf(out, ")\n");
279
280 fprintf(out, "{\n");
281 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700282 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700283 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800284 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800285 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000286 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800287 arg != signature->end(); arg++) {
288 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
289 for (const auto &chainField : attributionDecl.fields) {
290 if (chainField.javaType == JAVA_TYPE_STRING) {
291 fprintf(out, " if (%s_length != %s.size()) {\n",
292 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700293 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800294 fprintf(out, " }\n");
295 }
296 }
297 fprintf(out, "\n event.begin();\n");
298 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
299 attributionDecl.fields.front().name.c_str());
300 fprintf(out, " event.begin();\n");
301 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800302 if (chainField.javaType == JAVA_TYPE_STRING) {
303 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
304 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
305 fprintf(out, " } else {\n");
306 fprintf(out, " event << \"\";\n");
307 fprintf(out, " }\n");
308 } else {
309 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
310 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800311 }
312 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000313 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800314 fprintf(out, " event.end();\n\n");
Yangster-mace124e422018-08-16 10:30:28 -0700315 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
316 fprintf(out, " event.begin();\n\n");
317 fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
318 fprintf(out, " event.begin();\n");
319 fprintf(out, " event << it.first;\n");
320 fprintf(out, " event << it.second;\n");
321 fprintf(out, " event.end();\n");
322 fprintf(out, " }\n");
323
324 fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
325 fprintf(out, " event.begin();\n");
326 fprintf(out, " event << it.first;\n");
327 fprintf(out, " event << it.second;\n");
328 fprintf(out, " event.end();\n");
329 fprintf(out, " }\n");
330
331 fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
332 fprintf(out, " event.begin();\n");
333 fprintf(out, " event << it.first;\n");
334 fprintf(out, " event << it.second;\n");
335 fprintf(out, " event.end();\n");
336 fprintf(out, " }\n");
337
Howard Ro4078dd42018-09-27 17:41:08 -0700338 fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
339 fprintf(out, " event.begin();\n");
340 fprintf(out, " event << it.first;\n");
341 fprintf(out, " event << it.second;\n");
342 fprintf(out, " event.end();\n");
343 fprintf(out, " }\n");
344
Yangster-mace124e422018-08-16 10:30:28 -0700345 fprintf(out, " event.end();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800346 } else {
347 if (*arg == JAVA_TYPE_STRING) {
348 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
349 fprintf(out, " arg%d = \"\";\n", argIndex);
350 fprintf(out, " }\n");
351 }
352 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000353 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000354 argIndex++;
355 }
356
Yao Chen97e21ec2018-03-29 11:00:38 -0700357 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700358 fprintf(out, " } else {\n");
359 fprintf(out, " return 1;\n");
360 fprintf(out, " }\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000361 fprintf(out, "}\n");
362 fprintf(out, "\n");
363 }
364
Yangster-macb8382a12018-04-04 10:39:12 -0700365 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
366 signature != atoms.signatures.end(); signature++) {
367 int argIndex;
368
Yangster-mace124e422018-08-16 10:30:28 -0700369 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700370 fprintf(out, "stats_write(int32_t code");
371 argIndex = 1;
372 for (vector<java_type_t>::const_iterator arg = signature->begin();
373 arg != signature->end(); arg++) {
374 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
375 for (auto chainField : attributionDecl.fields) {
376 if (chainField.javaType == JAVA_TYPE_STRING) {
377 fprintf(out, ", const std::vector<%s>& %s",
378 cpp_type_name(chainField.javaType),
379 chainField.name.c_str());
380 } else {
381 fprintf(out, ", const %s* %s, size_t %s_length",
382 cpp_type_name(chainField.javaType),
383 chainField.name.c_str(), chainField.name.c_str());
384 }
385 }
Yangster-mace124e422018-08-16 10:30:28 -0700386 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700387 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
388 "const std::map<int, int64_t>& arg%d_2, "
389 "const std::map<int, char const*>& arg%d_3, "
390 "const std::map<int, float>& arg%d_4",
391 argIndex, argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700392 } else {
393 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
394 }
395 argIndex++;
396 }
397 fprintf(out, ")\n");
398
399 fprintf(out, "{\n");
400 fprintf(out, " int ret = 0;\n");
401
Yangster-macca5c0862018-04-09 22:39:53 -0700402 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700403 fprintf(out, " ret = try_stats_write(code");
404
405 argIndex = 1;
406 for (vector<java_type_t>::const_iterator arg = signature->begin();
407 arg != signature->end(); arg++) {
408 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
409 for (auto chainField : attributionDecl.fields) {
410 if (chainField.javaType == JAVA_TYPE_STRING) {
411 fprintf(out, ", %s",
412 chainField.name.c_str());
413 } else {
414 fprintf(out, ", %s, %s_length",
415 chainField.name.c_str(), chainField.name.c_str());
416 }
417 }
Yangster-mace124e422018-08-16 10:30:28 -0700418 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700419 fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4",
420 argIndex, argIndex, argIndex, argIndex);
Yangster-macb8382a12018-04-04 10:39:12 -0700421 } else {
422 fprintf(out, ", arg%d", argIndex);
423 }
424 argIndex++;
425 }
426 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700427 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700428
429 fprintf(out, " {\n");
430 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
431 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
432 "kMinRetryIntervalNs) break;\n");
433 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
434 fprintf(out, " }\n");
435 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700436 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700437 fprintf(out, " if (ret < 0) {\n");
Yao Chen39b67992018-11-08 15:32:17 -0800438 fprintf(out, " note_log_drop(ret);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700439 fprintf(out, " }\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700440 fprintf(out, " return ret;\n");
441 fprintf(out, "}\n");
442 fprintf(out, "\n");
443 }
444
Yangster-macba5b9e42018-01-10 21:31:59 -0800445 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
446 signature != atoms.non_chained_signatures.end(); signature++) {
447 int argIndex;
448
Yao Chen97e21ec2018-03-29 11:00:38 -0700449 fprintf(out, "int\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700450 fprintf(out, "try_stats_write_non_chained(int32_t code");
Yangster-macba5b9e42018-01-10 21:31:59 -0800451 argIndex = 1;
452 for (vector<java_type_t>::const_iterator arg = signature->begin();
453 arg != signature->end(); arg++) {
454 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
455 argIndex++;
456 }
457 fprintf(out, ")\n");
458
459 fprintf(out, "{\n");
460 argIndex = 1;
Yao Chencf3829a2018-06-05 14:20:35 -0700461 fprintf(out, " if (kStatsdEnabled) {\n");
Yao Chenf7bc6ab2018-04-18 13:45:48 -0700462 fprintf(out, " stats_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800463 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800464 fprintf(out, " event << code;\n\n");
465 for (vector<java_type_t>::const_iterator arg = signature->begin();
466 arg != signature->end(); arg++) {
467 if (argIndex == 1) {
468 fprintf(out, " event.begin();\n\n");
469 fprintf(out, " event.begin();\n");
470 }
471 if (*arg == JAVA_TYPE_STRING) {
472 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
473 fprintf(out, " arg%d = \"\";\n", argIndex);
474 fprintf(out, " }\n");
475 }
476 fprintf(out, " event << arg%d;\n", argIndex);
477 if (argIndex == 2) {
478 fprintf(out, " event.end();\n\n");
479 fprintf(out, " event.end();\n\n");
480 }
481 argIndex++;
482 }
483
Yao Chen97e21ec2018-03-29 11:00:38 -0700484 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chencf3829a2018-06-05 14:20:35 -0700485 fprintf(out, " } else {\n");
486 fprintf(out, " return 1;\n");
487 fprintf(out, " }\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800488 fprintf(out, "}\n");
489 fprintf(out, "\n");
490 }
Yangster-macb8382a12018-04-04 10:39:12 -0700491
492 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
493 signature != atoms.non_chained_signatures.end(); signature++) {
494 int argIndex;
495
496 fprintf(out, "int\n");
497 fprintf(out, "stats_write_non_chained(int32_t code");
498 argIndex = 1;
499 for (vector<java_type_t>::const_iterator arg = signature->begin();
500 arg != signature->end(); arg++) {
501 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
502 argIndex++;
503 }
504 fprintf(out, ")\n");
505
506 fprintf(out, "{\n");
507
508 fprintf(out, " int ret = 0;\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700509 fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700510 fprintf(out, " ret = try_stats_write_non_chained(code");
511
512 argIndex = 1;
513 for (vector<java_type_t>::const_iterator arg = signature->begin();
514 arg != signature->end(); arg++) {
515 fprintf(out, ", arg%d", argIndex);
516 argIndex++;
517 }
518 fprintf(out, ");\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700519 fprintf(out, " if (ret >= 0) { break; }\n");
Yangster-macca5c0862018-04-09 22:39:53 -0700520
521 fprintf(out, " {\n");
522 fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
523 fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
524 "kMinRetryIntervalNs) break;\n");
525 fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
526 fprintf(out, " }\n");
527
528 fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700529 fprintf(out, " }\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700530 fprintf(out, " if (ret < 0) {\n");
Yao Chen39b67992018-11-08 15:32:17 -0800531 fprintf(out, " note_log_drop(ret);\n");
Yao Chen0f5d6612018-08-22 14:11:51 -0700532 fprintf(out, " }\n");
533 fprintf(out, " return ret;\n\n");
Yangster-macb8382a12018-04-04 10:39:12 -0700534 fprintf(out, "}\n");
535
536 fprintf(out, "\n");
537 }
538
539
Yao Chend54f9dd2017-10-17 17:37:48 +0000540 // Print footer
541 fprintf(out, "\n");
542 fprintf(out, "} // namespace util\n");
543 fprintf(out, "} // namespace android\n");
544
545 return 0;
546}
547
Yangster-macba5b9e42018-01-10 21:31:59 -0800548void build_non_chained_decl_map(const Atoms& atoms,
549 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
550 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
551 atom != atoms.non_chained_decls.end(); atom++) {
552 decl_map->insert(std::make_pair(atom->code, atom));
553 }
554}
555
556static void write_cpp_usage(
557 FILE* out, const string& method_name, const string& atom_code_name,
558 const AtomDecl& atom, const AtomDecl &attributionDecl) {
559 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str());
560 for (vector<AtomField>::const_iterator field = atom.fields.begin();
561 field != atom.fields.end(); field++) {
562 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
563 for (auto chainField : attributionDecl.fields) {
564 if (chainField.javaType == JAVA_TYPE_STRING) {
565 fprintf(out, ", const std::vector<%s>& %s",
566 cpp_type_name(chainField.javaType),
567 chainField.name.c_str());
568 } else {
569 fprintf(out, ", const %s* %s, size_t %s_length",
570 cpp_type_name(chainField.javaType),
571 chainField.name.c_str(), chainField.name.c_str());
572 }
573 }
Yangster-mace124e422018-08-16 10:30:28 -0700574 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700575 fprintf(out, ", const std::map<int, int32_t>& %s_int"
576 ", const std::map<int, int64_t>& %s_long"
Yangster-mace124e422018-08-16 10:30:28 -0700577 ", const std::map<int, char const*>& %s_str"
578 ", const std::map<int, float>& %s_float",
Howard Ro4078dd42018-09-27 17:41:08 -0700579 field->name.c_str(),
580 field->name.c_str(),
581 field->name.c_str(),
582 field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800583 } else {
584 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
585 }
586 }
587 fprintf(out, ");\n");
588}
589
590static void write_cpp_method_header(
591 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
592 const AtomDecl &attributionDecl) {
593 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
594 signature != signatures.end(); signature++) {
Yangster-mace124e422018-08-16 10:30:28 -0700595 fprintf(out, "int %s(int32_t code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800596 int argIndex = 1;
597 for (vector<java_type_t>::const_iterator arg = signature->begin();
598 arg != signature->end(); arg++) {
599 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
600 for (auto chainField : attributionDecl.fields) {
601 if (chainField.javaType == JAVA_TYPE_STRING) {
602 fprintf(out, ", const std::vector<%s>& %s",
603 cpp_type_name(chainField.javaType), chainField.name.c_str());
604 } else {
605 fprintf(out, ", const %s* %s, size_t %s_length",
606 cpp_type_name(chainField.javaType),
607 chainField.name.c_str(), chainField.name.c_str());
608 }
609 }
Yangster-mace124e422018-08-16 10:30:28 -0700610 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -0700611 fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
612 "const std::map<int, int64_t>& arg%d_2, "
613 "const std::map<int, char const*>& arg%d_3, "
614 "const std::map<int, float>& arg%d_4",
615 argIndex, argIndex, argIndex, argIndex);
Yangster-macba5b9e42018-01-10 21:31:59 -0800616 } else {
617 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
618 }
619 argIndex++;
620 }
621 fprintf(out, ");\n");
622
623 }
624}
Yao Chend54f9dd2017-10-17 17:37:48 +0000625
626static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800627write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000628{
Yao Chend54f9dd2017-10-17 17:37:48 +0000629 // Print prelude
630 fprintf(out, "// This file is autogenerated\n");
631 fprintf(out, "\n");
632 fprintf(out, "#pragma once\n");
633 fprintf(out, "\n");
634 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800635 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800636 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800637 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000638 fprintf(out, "\n");
639
640 fprintf(out, "namespace android {\n");
641 fprintf(out, "namespace util {\n");
642 fprintf(out, "\n");
643 fprintf(out, "/*\n");
644 fprintf(out, " * API For logging statistics events.\n");
645 fprintf(out, " */\n");
646 fprintf(out, "\n");
647 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700648 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000649 fprintf(out, " */\n");
650 fprintf(out, "enum {\n");
651
Yangster-macba5b9e42018-01-10 21:31:59 -0800652 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
653 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
654
Yao Chend54f9dd2017-10-17 17:37:48 +0000655 size_t i = 0;
656 // Print constants
657 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800658 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000659 string constant = make_constant_name(atom->name);
660 fprintf(out, "\n");
661 fprintf(out, " /**\n");
662 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800663 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
664
665 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
666 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
667 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
668 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000669 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000670 fprintf(out, " */\n");
671 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
672 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800673 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
674 maxPushedAtomId = atom->code;
675 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000676 i++;
677 }
678 fprintf(out, "\n");
679 fprintf(out, "};\n");
680 fprintf(out, "\n");
681
Yao Chen9c1debe2018-02-19 14:39:19 -0800682 fprintf(out, "struct StateAtomFieldOptions {\n");
683 fprintf(out, " std::vector<int> primaryFields;\n");
684 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700685 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800686 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700687
688 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800689 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700690 " const static std::set<int> "
691 "kNotTruncatingTimestampAtomWhiteList;\n");
692 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
693 fprintf(out,
694 " const static std::set<int> kAtomsWithAttributionChain;\n");
695 fprintf(out,
696 " const static std::map<int, StateAtomFieldOptions> "
697 "kStateAtomsFieldOptions;\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700698 fprintf(out,
699 " const static std::map<int, std::vector<int>> "
700 "kBytesFieldAtoms;");
Yao Chen9c1debe2018-02-19 14:39:19 -0800701 fprintf(out, "};\n");
702
Yao Chenc40a19d2018-03-15 16:48:25 -0700703 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
704 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800705
Yao Chend54f9dd2017-10-17 17:37:48 +0000706 // Print write methods
707 fprintf(out, "//\n");
708 fprintf(out, "// Write methods\n");
709 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800710 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
711
712 fprintf(out, "//\n");
713 fprintf(out, "// Write flattened methods\n");
714 fprintf(out, "//\n");
715 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
716 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000717
718 fprintf(out, "\n");
719 fprintf(out, "} // namespace util\n");
720 fprintf(out, "} // namespace android\n");
721
722 return 0;
723}
724
Bookatz0bd97202018-06-05 12:42:37 -0700725static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
726 const AtomDecl& atom) {
Yangster-macba5b9e42018-01-10 21:31:59 -0800727 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
728 method_name.c_str(), atom_code_name.c_str());
729 for (vector<AtomField>::const_iterator field = atom.fields.begin();
730 field != atom.fields.end(); field++) {
731 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Bookatz0bd97202018-06-05 12:42:37 -0700732 fprintf(out, ", android.os.WorkSource workSource");
Yangster-mace124e422018-08-16 10:30:28 -0700733 } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
734 fprintf(out, ", SparseArray<Object> value_map");
Yao Chenbbdd67d2018-10-24 12:15:56 -0700735 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
736 fprintf(out, ", byte[] %s", field->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800737 } else {
738 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
739 }
740 }
Bookatz0bd97202018-06-05 12:42:37 -0700741 fprintf(out, ");<br>\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800742}
743
744static void write_java_method(
745 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
746 const AtomDecl &attributionDecl) {
747 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
748 signature != signatures.end(); signature++) {
Yao Chen97e21ec2018-03-29 11:00:38 -0700749 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800750 int argIndex = 1;
751 for (vector<java_type_t>::const_iterator arg = signature->begin();
752 arg != signature->end(); arg++) {
753 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
754 for (auto chainField : attributionDecl.fields) {
755 fprintf(out, ", %s[] %s",
756 java_type_name(chainField.javaType), chainField.name.c_str());
757 }
Yangster-mace124e422018-08-16 10:30:28 -0700758 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
759 fprintf(out, ", SparseArray<Object> value_map");
Yangster-macba5b9e42018-01-10 21:31:59 -0800760 } else {
761 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
762 }
763 argIndex++;
764 }
765 fprintf(out, ");\n");
766 }
767}
768
Bookatz0bd97202018-06-05 12:42:37 -0700769static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) {
770 fprintf(out, "\n // WorkSource methods.\n");
771 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
772 signature != signatures.end(); signature++) {
773 // Determine if there is Attribution in this signature.
774 int attributionArg = -1;
775 int argIndexMax = 0;
776 for (vector<java_type_t>::const_iterator arg = signature->begin();
777 arg != signature->end(); arg++) {
778 argIndexMax++;
779 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
780 if (attributionArg > -1) {
781 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
782 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
783 fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n");
784 return;
785 }
786 attributionArg = argIndexMax;
787 }
788 }
789 if (attributionArg < 0) {
790 continue;
791 }
792
793 // Method header (signature)
794 fprintf(out, " public static void write(int code");
795 int argIndex = 1;
796 for (vector<java_type_t>::const_iterator arg = signature->begin();
797 arg != signature->end(); arg++) {
798 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
799 fprintf(out, ", WorkSource ws");
800 } else {
801 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
802 }
803 argIndex++;
804 }
805 fprintf(out, ") {\n");
806
807 // write_non_chained() component. TODO: Remove when flat uids are no longer needed.
808 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
809 fprintf(out, " write_non_chained(code");
810 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
811 if (argIndex == attributionArg) {
812 fprintf(out, ", ws.get(i), ws.getName(i)");
813 } else {
814 fprintf(out, ", arg%d", argIndex);
815 }
816 }
817 fprintf(out, ");\n");
818 fprintf(out, " }\n"); // close flor-loop
819
820 // write() component.
821 fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n");
822 fprintf(out, " if (workChains != null) {\n");
823 fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n");
824 fprintf(out, " write(code");
825 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
826 if (argIndex == attributionArg) {
827 fprintf(out, ", wc.getUids(), wc.getTags()");
828 } else {
829 fprintf(out, ", arg%d", argIndex);
830 }
831 }
832 fprintf(out, ");\n");
833 fprintf(out, " }\n"); // close for-loop
834 fprintf(out, " }\n"); // close if
835 fprintf(out, " }\n"); // close method
836 }
837}
Yangster-macba5b9e42018-01-10 21:31:59 -0800838
Yao Chend54f9dd2017-10-17 17:37:48 +0000839static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800840write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000841{
Yao Chend54f9dd2017-10-17 17:37:48 +0000842 // Print prelude
843 fprintf(out, "// This file is autogenerated\n");
844 fprintf(out, "\n");
845 fprintf(out, "package android.util;\n");
846 fprintf(out, "\n");
Bookatz0bd97202018-06-05 12:42:37 -0700847 fprintf(out, "import android.os.WorkSource;\n");
Yangster-mace124e422018-08-16 10:30:28 -0700848 fprintf(out, "import android.util.SparseArray;\n");
Bookatz0bd97202018-06-05 12:42:37 -0700849 fprintf(out, "import java.util.ArrayList;\n");
850 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000851 fprintf(out, "\n");
852 fprintf(out, "/**\n");
853 fprintf(out, " * API For logging statistics events.\n");
854 fprintf(out, " * @hide\n");
855 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800856 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700857 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000858
Yangster-macba5b9e42018-01-10 21:31:59 -0800859 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
860 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
861
Stefan Lafon9478f352017-10-30 21:20:20 -0700862 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000863 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
864 atom != atoms.decls.end(); atom++) {
865 string constant = make_constant_name(atom->name);
866 fprintf(out, "\n");
867 fprintf(out, " /**\n");
Bookatz0bd97202018-06-05 12:42:37 -0700868 fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str());
869 write_java_usage(out, "write", constant, *atom);
Yangster-macba5b9e42018-01-10 21:31:59 -0800870 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
871 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
Bookatz0bd97202018-06-05 12:42:37 -0700872 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second);
Yao Chend54f9dd2017-10-17 17:37:48 +0000873 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000874 fprintf(out, " */\n");
875 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
876 }
877 fprintf(out, "\n");
878
Stefan Lafon9478f352017-10-30 21:20:20 -0700879 // Print constants for the enum values.
880 fprintf(out, " // Constants for enum values.\n\n");
881 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800882 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700883 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800884 field != atom->fields.end(); field++) {
885 if (field->javaType == JAVA_TYPE_ENUM) {
886 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
887 field->name.c_str());
888 for (map<int, string>::const_iterator value = field->enumValues.begin();
889 value != field->enumValues.end(); value++) {
890 fprintf(out, " public static final int %s__%s__%s = %d;\n",
891 make_constant_name(atom->message).c_str(),
892 make_constant_name(field->name).c_str(),
893 make_constant_name(value->second).c_str(),
894 value->first);
895 }
896 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700897 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700898 }
899 }
900
Yao Chend54f9dd2017-10-17 17:37:48 +0000901 // Print write methods
902 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800903 write_java_method(out, "write", atoms.signatures, attributionDecl);
904 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Bookatz0bd97202018-06-05 12:42:37 -0700905 write_java_work_source_method(out, atoms.signatures);
Yao Chend54f9dd2017-10-17 17:37:48 +0000906
907 fprintf(out, "}\n");
908
909 return 0;
910}
911
912static const char*
913jni_type_name(java_type_t type)
914{
915 switch (type) {
916 case JAVA_TYPE_BOOLEAN:
917 return "jboolean";
918 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700919 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000920 return "jint";
921 case JAVA_TYPE_LONG:
922 return "jlong";
923 case JAVA_TYPE_FLOAT:
924 return "jfloat";
925 case JAVA_TYPE_DOUBLE:
926 return "jdouble";
927 case JAVA_TYPE_STRING:
928 return "jstring";
Yao Chenbbdd67d2018-10-24 12:15:56 -0700929 case JAVA_TYPE_BYTE_ARRAY:
930 return "jbyteArray";
Yao Chend54f9dd2017-10-17 17:37:48 +0000931 default:
932 return "UNKNOWN";
933 }
934}
935
Yangster-mac7604aea2017-12-11 22:55:49 -0800936static const char*
937jni_array_type_name(java_type_t type)
938{
939 switch (type) {
940 case JAVA_TYPE_INT:
941 return "jintArray";
Yangster-mace124e422018-08-16 10:30:28 -0700942 case JAVA_TYPE_FLOAT:
943 return "jfloatArray";
Yangster-mac7604aea2017-12-11 22:55:49 -0800944 case JAVA_TYPE_STRING:
945 return "jobjectArray";
946 default:
947 return "UNKNOWN";
948 }
949}
950
Yao Chend54f9dd2017-10-17 17:37:48 +0000951static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800952jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000953{
Yangster-macba5b9e42018-01-10 21:31:59 -0800954 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000955 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800956 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000957 switch (*arg) {
958 case JAVA_TYPE_BOOLEAN:
959 result += "_boolean";
960 break;
961 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700962 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000963 result += "_int";
964 break;
965 case JAVA_TYPE_LONG:
966 result += "_long";
967 break;
968 case JAVA_TYPE_FLOAT:
969 result += "_float";
970 break;
971 case JAVA_TYPE_DOUBLE:
972 result += "_double";
973 break;
974 case JAVA_TYPE_STRING:
975 result += "_String";
976 break;
Yangster-mac7604aea2017-12-11 22:55:49 -0800977 case JAVA_TYPE_ATTRIBUTION_CHAIN:
978 result += "_AttributionChain";
979 break;
Yangster-mace124e422018-08-16 10:30:28 -0700980 case JAVA_TYPE_KEY_VALUE_PAIR:
981 result += "_KeyValuePairs";
982 break;
Yao Chenbbdd67d2018-10-24 12:15:56 -0700983 case JAVA_TYPE_BYTE_ARRAY:
984 result += "_bytes";
985 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000986 default:
987 result += "_UNKNOWN";
988 break;
989 }
990 }
991 return result;
992}
993
994static const char*
995java_type_signature(java_type_t type)
996{
997 switch (type) {
998 case JAVA_TYPE_BOOLEAN:
999 return "Z";
1000 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -07001001 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +00001002 return "I";
1003 case JAVA_TYPE_LONG:
1004 return "J";
1005 case JAVA_TYPE_FLOAT:
1006 return "F";
1007 case JAVA_TYPE_DOUBLE:
1008 return "D";
1009 case JAVA_TYPE_STRING:
1010 return "Ljava/lang/String;";
Yao Chenbbdd67d2018-10-24 12:15:56 -07001011 case JAVA_TYPE_BYTE_ARRAY:
1012 return "[B";
Yao Chend54f9dd2017-10-17 17:37:48 +00001013 default:
1014 return "UNKNOWN";
1015 }
1016}
1017
1018static string
Yangster-mac7604aea2017-12-11 22:55:49 -08001019jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001020{
1021 string result("(I");
1022 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -08001023 arg != signature.end(); arg++) {
1024 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1025 for (auto chainField : attributionDecl.fields) {
1026 result += "[";
1027 result += java_type_signature(chainField.javaType);
1028 }
Yangster-mace124e422018-08-16 10:30:28 -07001029 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1030 result += "Landroid/util/SparseArray;";
Yangster-mac7604aea2017-12-11 22:55:49 -08001031 } else {
1032 result += java_type_signature(*arg);
1033 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001034 }
Yao Chen97e21ec2018-03-29 11:00:38 -07001035 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +00001036 return result;
1037}
1038
Yangster-mace124e422018-08-16 10:30:28 -07001039static void write_key_value_map_jni(FILE* out) {
Howard Ro4078dd42018-09-27 17:41:08 -07001040 fprintf(out, " std::map<int, int32_t> int32_t_map;\n");
Yangster-mace124e422018-08-16 10:30:28 -07001041 fprintf(out, " std::map<int, int64_t> int64_t_map;\n");
1042 fprintf(out, " std::map<int, float> float_map;\n");
1043 fprintf(out, " std::map<int, char const*> string_map;\n\n");
1044
1045 fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n");
1046
1047 fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n");
1048 fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n");
1049 fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n");
1050
1051
1052 fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n");
1053
Howard Ro4078dd42018-09-27 17:41:08 -07001054 fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001055 fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n");
1056 fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n");
1057 fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001058 fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n");
Yangster-mace124e422018-08-16 10:30:28 -07001059 fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n");
1060 fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n");
1061
1062 fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n");
1063 fprintf(out, " for(int i = 0; i < jsize; i++) {\n");
1064 fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n");
1065 fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n");
1066 fprintf(out, " if (jvalue_obj == NULL) { continue; }\n");
Howard Ro4078dd42018-09-27 17:41:08 -07001067 fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n");
1068 fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n");
1069 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n");
Yangster-mace124e422018-08-16 10:30:28 -07001070 fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n");
1071 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n");
1072 fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n");
1073 fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n");
1074 fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n");
1075 fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n");
1076 fprintf(out, " scoped_ufs.push_back(std::move(utf));\n");
1077 fprintf(out, " }\n");
1078 fprintf(out, " }\n");
1079}
1080
Yao Chend54f9dd2017-10-17 17:37:48 +00001081static int
Yangster-macba5b9e42018-01-10 21:31:59 -08001082write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
1083 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +00001084{
Yao Chend54f9dd2017-10-17 17:37:48 +00001085 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -08001086 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1087 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001088 int argIndex;
1089
Yao Chen97e21ec2018-03-29 11:00:38 -07001090 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001091 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -08001092 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001093 argIndex = 1;
1094 for (vector<java_type_t>::const_iterator arg = signature->begin();
1095 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001096 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1097 for (auto chainField : attributionDecl.fields) {
1098 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
1099 chainField.name.c_str());
1100 }
Yangster-mace124e422018-08-16 10:30:28 -07001101 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1102 fprintf(out, ", jobject value_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001103 } else {
1104 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
1105 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001106 argIndex++;
1107 }
1108 fprintf(out, ")\n");
1109
1110 fprintf(out, "{\n");
1111
1112 // Prepare strings
1113 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -08001114 bool hadStringOrChain = false;
Yangster-mace124e422018-08-16 10:30:28 -07001115 bool isKeyValuePairAtom = false;
Yao Chend54f9dd2017-10-17 17:37:48 +00001116 for (vector<java_type_t>::const_iterator arg = signature->begin();
1117 arg != signature->end(); arg++) {
1118 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001119 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001120 fprintf(out, " const char* str%d;\n", argIndex);
1121 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
1122 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
1123 argIndex, argIndex);
1124 fprintf(out, " } else {\n");
1125 fprintf(out, " str%d = NULL;\n", argIndex);
1126 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001127 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1128 hadStringOrChain = true;
1129 fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
1130 fprintf(out, " const char* str%d;\n", argIndex);
Yao Chen8e6f9982018-11-29 09:39:45 -08001131 fprintf(out,
1132 " if (arg%d != NULL && env->GetArrayLength(arg%d) > "
1133 "0) {\n",
1134 argIndex, argIndex);
Yao Chenbbdd67d2018-10-24 12:15:56 -07001135 fprintf(out,
1136 " jbyte_array%d = "
1137 "env->GetByteArrayElements(arg%d, NULL);\n",
1138 argIndex, argIndex);
1139 fprintf(out,
1140 " str%d = "
1141 "reinterpret_cast<char*>(env->GetByteArrayElements(arg%"
1142 "d, NULL));\n",
1143 argIndex, argIndex);
1144 fprintf(out, " } else {\n");
1145 fprintf(out, " jbyte_array%d = NULL;\n", argIndex);
1146 fprintf(out, " str%d = NULL;\n", argIndex);
1147 fprintf(out, " }\n");
1148
Yangster-mac7604aea2017-12-11 22:55:49 -08001149 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1150 hadStringOrChain = true;
1151 for (auto chainField : attributionDecl.fields) {
1152 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
1153 chainField.name.c_str(), chainField.name.c_str());
1154 if (chainField.name != attributionDecl.fields.front().name) {
1155 fprintf(out, " if (%s_length != %s_length) {\n",
1156 chainField.name.c_str(),
1157 attributionDecl.fields.front().name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -07001158 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001159 fprintf(out, " }\n");
1160 }
1161 if (chainField.javaType == JAVA_TYPE_INT) {
1162 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
1163 chainField.name.c_str(), chainField.name.c_str());
1164 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1165 fprintf(out, " std::vector<%s> %s_vec;\n",
1166 cpp_type_name(chainField.javaType), chainField.name.c_str());
1167 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
1168 chainField.name.c_str());
1169 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
1170 chainField.name.c_str());
1171 fprintf(out, " jstring jstr = "
1172 "(jstring)env->GetObjectArrayElement(%s, i);\n",
1173 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001174 fprintf(out, " if (jstr == NULL) {\n");
1175 fprintf(out, " %s_vec.push_back(NULL);\n",
1176 chainField.name.c_str());
1177 fprintf(out, " } else {\n");
1178 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -08001179 "new ScopedUtfChars(env, jstr);\n",
1180 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001181 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001182 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001183 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001184 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -08001185 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001186 fprintf(out, " }\n");
1187 }
1188 fprintf(out, "\n");
1189 }
Yangster-mace124e422018-08-16 10:30:28 -07001190 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
1191 isKeyValuePairAtom = true;
Yao Chend54f9dd2017-10-17 17:37:48 +00001192 }
1193 argIndex++;
1194 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001195 // Emit this to quiet the unused parameter warning if there were no strings or attribution
1196 // chains.
Yangster-mace124e422018-08-16 10:30:28 -07001197 if (!hadStringOrChain && !isKeyValuePairAtom) {
Yao Chend54f9dd2017-10-17 17:37:48 +00001198 fprintf(out, " (void)env;\n");
1199 }
Yangster-mace124e422018-08-16 10:30:28 -07001200 if (isKeyValuePairAtom) {
1201 write_key_value_map_jni(out);
1202 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001203
1204 // stats_write call
1205 argIndex = 1;
Yangster-mace124e422018-08-16 10:30:28 -07001206 fprintf(out, "\n int ret = android::util::%s(code", cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +00001207 for (vector<java_type_t>::const_iterator arg = signature->begin();
1208 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -08001209 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1210 for (auto chainField : attributionDecl.fields) {
1211 if (chainField.javaType == JAVA_TYPE_INT) {
1212 fprintf(out, ", (const %s*)%s_array, %s_length",
1213 cpp_type_name(chainField.javaType),
1214 chainField.name.c_str(), chainField.name.c_str());
1215 } else if (chainField.javaType == JAVA_TYPE_STRING) {
1216 fprintf(out, ", %s_vec", chainField.name.c_str());
1217 }
1218 }
Yangster-mace124e422018-08-16 10:30:28 -07001219 } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
Howard Ro4078dd42018-09-27 17:41:08 -07001220 fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map");
Yangster-mac7604aea2017-12-11 22:55:49 -08001221 } else {
Yao Chenbbdd67d2018-10-24 12:15:56 -07001222 const char* argName = (*arg == JAVA_TYPE_STRING ||
1223 *arg == JAVA_TYPE_BYTE_ARRAY)
1224 ? "str"
1225 : "arg";
Yangster-mac7604aea2017-12-11 22:55:49 -08001226 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
1227 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001228 argIndex++;
1229 }
1230 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001231 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001232
1233 // Clean up strings
1234 argIndex = 1;
1235 for (vector<java_type_t>::const_iterator arg = signature->begin();
1236 arg != signature->end(); arg++) {
1237 if (*arg == JAVA_TYPE_STRING) {
1238 fprintf(out, " if (str%d != NULL) {\n", argIndex);
1239 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
1240 argIndex, argIndex);
1241 fprintf(out, " }\n");
Yao Chenbbdd67d2018-10-24 12:15:56 -07001242 } else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
1243 fprintf(out, " if (str%d != NULL) { \n", argIndex);
1244 fprintf(out,
1245 " env->ReleaseByteArrayElements(arg%d, "
1246 "jbyte_array%d, 0);\n",
1247 argIndex, argIndex);
1248 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -08001249 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
1250 for (auto chainField : attributionDecl.fields) {
1251 if (chainField.javaType == JAVA_TYPE_INT) {
1252 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
1253 chainField.name.c_str(), chainField.name.c_str());
1254 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -08001255 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -08001256 chainField.name.c_str());
1257 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
1258 fprintf(out, " }\n");
1259 }
1260 }
Yao Chend54f9dd2017-10-17 17:37:48 +00001261 }
1262 argIndex++;
1263 }
Yangster-mace124e422018-08-16 10:30:28 -07001264
Yao Chen97e21ec2018-03-29 11:00:38 -07001265 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001266
1267 fprintf(out, "}\n");
1268 fprintf(out, "\n");
1269 }
1270
Yangster-macba5b9e42018-01-10 21:31:59 -08001271
1272 return 0;
1273}
1274
1275void write_jni_registration(FILE* out, const string& java_method_name,
1276 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
1277 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
1278 signature != signatures.end(); signature++) {
1279 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
1280 java_method_name.c_str(),
1281 jni_function_signature(*signature, attributionDecl).c_str(),
1282 jni_function_name(java_method_name, *signature).c_str());
1283 }
1284}
1285
1286static int
1287write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
1288{
1289 // Print prelude
1290 fprintf(out, "// This file is autogenerated\n");
1291 fprintf(out, "\n");
1292
1293 fprintf(out, "#include <statslog.h>\n");
1294 fprintf(out, "\n");
1295 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
1296 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
1297 fprintf(out, "#include <utils/Vector.h>\n");
1298 fprintf(out, "#include \"core_jni_helpers.h\"\n");
1299 fprintf(out, "#include \"jni.h\"\n");
1300 fprintf(out, "\n");
1301 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
1302 fprintf(out, "\n");
1303
1304 fprintf(out, "namespace android {\n");
1305 fprintf(out, "\n");
1306
1307 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
1308 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
1309 atoms.non_chained_signatures, attributionDecl);
1310
Yao Chend54f9dd2017-10-17 17:37:48 +00001311 // Print registration function table
1312 fprintf(out, "/*\n");
1313 fprintf(out, " * JNI registration.\n");
1314 fprintf(out, " */\n");
1315 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -08001316 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
1317 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001318 fprintf(out, "};\n");
1319 fprintf(out, "\n");
1320
1321 // Print registration function
1322 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
1323 fprintf(out, " return RegisterMethodsOrDie(\n");
1324 fprintf(out, " env,\n");
1325 fprintf(out, " \"android/util/StatsLog\",\n");
1326 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
1327 fprintf(out, "}\n");
1328
1329 fprintf(out, "\n");
1330 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +00001331 return 0;
1332}
1333
Yao Chend54f9dd2017-10-17 17:37:48 +00001334static void
1335print_usage()
1336{
1337 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
1338 fprintf(stderr, "\n");
1339 fprintf(stderr, "OPTIONS\n");
1340 fprintf(stderr, " --cpp FILENAME the header file to output\n");
1341 fprintf(stderr, " --header FILENAME the cpp file to output\n");
1342 fprintf(stderr, " --help this message\n");
1343 fprintf(stderr, " --java FILENAME the java file to output\n");
1344 fprintf(stderr, " --jni FILENAME the jni file to output\n");
1345}
1346
1347/**
1348 * Do the argument parsing and execute the tasks.
1349 */
1350static int
1351run(int argc, char const*const* argv)
1352{
1353 string cppFilename;
1354 string headerFilename;
1355 string javaFilename;
1356 string jniFilename;
1357
1358 int index = 1;
1359 while (index < argc) {
1360 if (0 == strcmp("--help", argv[index])) {
1361 print_usage();
1362 return 0;
1363 } else if (0 == strcmp("--cpp", argv[index])) {
1364 index++;
1365 if (index >= argc) {
1366 print_usage();
1367 return 1;
1368 }
1369 cppFilename = argv[index];
1370 } else if (0 == strcmp("--header", argv[index])) {
1371 index++;
1372 if (index >= argc) {
1373 print_usage();
1374 return 1;
1375 }
1376 headerFilename = argv[index];
1377 } else if (0 == strcmp("--java", argv[index])) {
1378 index++;
1379 if (index >= argc) {
1380 print_usage();
1381 return 1;
1382 }
1383 javaFilename = argv[index];
1384 } else if (0 == strcmp("--jni", argv[index])) {
1385 index++;
1386 if (index >= argc) {
1387 print_usage();
1388 return 1;
1389 }
1390 jniFilename = argv[index];
1391 }
1392 index++;
1393 }
1394
1395 if (cppFilename.size() == 0
1396 && headerFilename.size() == 0
1397 && javaFilename.size() == 0
1398 && jniFilename.size() == 0) {
1399 print_usage();
1400 return 1;
1401 }
1402
1403 // Collate the parameters
1404 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001405 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001406 if (errorCount != 0) {
1407 return 1;
1408 }
1409
Yangster-mac7604aea2017-12-11 22:55:49 -08001410 AtomDecl attributionDecl;
1411 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001412 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001413 &attributionDecl, &attributionSignature);
1414
Yao Chend54f9dd2017-10-17 17:37:48 +00001415 // Write the .cpp file
1416 if (cppFilename.size() != 0) {
1417 FILE* out = fopen(cppFilename.c_str(), "w");
1418 if (out == NULL) {
1419 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1420 return 1;
1421 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001422 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1423 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001424 fclose(out);
1425 }
1426
1427 // Write the .h file
1428 if (headerFilename.size() != 0) {
1429 FILE* out = fopen(headerFilename.c_str(), "w");
1430 if (out == NULL) {
1431 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1432 return 1;
1433 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001434 errorCount = android::stats_log_api_gen::write_stats_log_header(
1435 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001436 fclose(out);
1437 }
1438
1439 // Write the .java file
1440 if (javaFilename.size() != 0) {
1441 FILE* out = fopen(javaFilename.c_str(), "w");
1442 if (out == NULL) {
1443 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1444 return 1;
1445 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001446 errorCount = android::stats_log_api_gen::write_stats_log_java(
1447 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001448 fclose(out);
1449 }
1450
1451 // Write the jni file
1452 if (jniFilename.size() != 0) {
1453 FILE* out = fopen(jniFilename.c_str(), "w");
1454 if (out == NULL) {
1455 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1456 return 1;
1457 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001458 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1459 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001460 fclose(out);
1461 }
1462
1463 return 0;
1464}
1465
1466}
1467}
1468
1469/**
1470 * Main.
1471 */
1472int
1473main(int argc, char const*const* argv)
1474{
1475 GOOGLE_PROTOBUF_VERIFY_VERSION;
1476
1477 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -07001478}