Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | #include "Collation.h" |
| 4 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 5 | #include "frameworks/base/cmds/statsd/src/atoms.pb.h" |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 6 | |
| 7 | #include <set> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <getopt.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | using namespace google::protobuf; |
| 16 | using namespace std; |
| 17 | |
| 18 | namespace android { |
| 19 | namespace stats_log_api_gen { |
| 20 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | const int PULL_ATOM_START_ID = 1000; |
| 22 | |
| 23 | int maxPushedAtomId = 2; |
| 24 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 25 | using android::os::statsd::Atom; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 26 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Turn lower and camel case into upper case with underscores. |
| 29 | */ |
| 30 | static string |
| 31 | make_constant_name(const string& str) |
| 32 | { |
| 33 | string result; |
| 34 | const int N = str.size(); |
| 35 | bool underscore_next = false; |
| 36 | for (int i=0; i<N; i++) { |
| 37 | char c = str[i]; |
| 38 | if (c >= 'A' && c <= 'Z') { |
| 39 | if (underscore_next) { |
| 40 | result += '_'; |
| 41 | underscore_next = false; |
| 42 | } |
| 43 | } else if (c >= 'a' && c <= 'z') { |
| 44 | c = 'A' + c - 'a'; |
| 45 | underscore_next = true; |
| 46 | } else if (c == '_') { |
| 47 | underscore_next = false; |
| 48 | } |
| 49 | result += c; |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | static const char* |
| 55 | cpp_type_name(java_type_t type) |
| 56 | { |
| 57 | switch (type) { |
| 58 | case JAVA_TYPE_BOOLEAN: |
| 59 | return "bool"; |
| 60 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 61 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 62 | return "int32_t"; |
| 63 | case JAVA_TYPE_LONG: |
| 64 | return "int64_t"; |
| 65 | case JAVA_TYPE_FLOAT: |
| 66 | return "float"; |
| 67 | case JAVA_TYPE_DOUBLE: |
| 68 | return "double"; |
| 69 | case JAVA_TYPE_STRING: |
| 70 | return "char const*"; |
| 71 | default: |
| 72 | return "UNKNOWN"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static const char* |
| 77 | java_type_name(java_type_t type) |
| 78 | { |
| 79 | switch (type) { |
| 80 | case JAVA_TYPE_BOOLEAN: |
| 81 | return "boolean"; |
| 82 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 83 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 84 | 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"; |
| 93 | default: |
| 94 | return "UNKNOWN"; |
| 95 | } |
| 96 | } |
| 97 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 98 | static int write_stats_log_cpp(FILE *out, const Atoms &atoms, |
| 99 | const AtomDecl &attributionDecl) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 100 | // Print prelude |
| 101 | fprintf(out, "// This file is autogenerated\n"); |
| 102 | fprintf(out, "\n"); |
| 103 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 104 | fprintf(out, "#include <exception>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 105 | fprintf(out, "#include <log/log_event_list.h>\n"); |
| 106 | fprintf(out, "#include <log/log.h>\n"); |
| 107 | fprintf(out, "#include <statslog.h>\n"); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame^] | 108 | fprintf(out, "#include <utils/SystemClock.h>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 109 | fprintf(out, "\n"); |
| 110 | |
| 111 | fprintf(out, "namespace android {\n"); |
| 112 | fprintf(out, "namespace util {\n"); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 113 | fprintf(out, "// the single event tag id for all stats logs\n"); |
| 114 | fprintf(out, "const static int kStatsEventTag = 1937006964;\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 115 | |
| 116 | // Print write methods |
| 117 | fprintf(out, "\n"); |
| 118 | for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 119 | signature != atoms.signatures.end(); signature++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 120 | int argIndex; |
| 121 | |
| 122 | fprintf(out, "void\n"); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 123 | fprintf(out, "stats_write(int32_t code"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 124 | argIndex = 1; |
| 125 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 126 | arg != signature->end(); arg++) { |
| 127 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 128 | for (auto chainField : attributionDecl.fields) { |
| 129 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 130 | fprintf(out, ", const std::vector<%s>& %s", |
| 131 | cpp_type_name(chainField.javaType), |
| 132 | chainField.name.c_str()); |
| 133 | } else { |
| 134 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 135 | cpp_type_name(chainField.javaType), |
| 136 | chainField.name.c_str(), chainField.name.c_str()); |
| 137 | } |
| 138 | } |
| 139 | } else { |
| 140 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 141 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 142 | argIndex++; |
| 143 | } |
| 144 | fprintf(out, ")\n"); |
| 145 | |
| 146 | fprintf(out, "{\n"); |
| 147 | argIndex = 1; |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 148 | fprintf(out, " android_log_event_list event(kStatsEventTag);\n"); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame^] | 149 | fprintf(out, " event << android::elapsedRealtimeNano();\n\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 150 | fprintf(out, " event << code;\n\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 151 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 152 | arg != signature->end(); arg++) { |
| 153 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 154 | for (const auto &chainField : attributionDecl.fields) { |
| 155 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 156 | fprintf(out, " if (%s_length != %s.size()) {\n", |
| 157 | attributionDecl.fields.front().name.c_str(), chainField.name.c_str()); |
| 158 | fprintf(out, " throw std::invalid_argument(\"attribution fields with" |
| 159 | " diff length: %s vs %s\");\n", |
| 160 | attributionDecl.fields.front().name.c_str(), |
| 161 | chainField.name.c_str()); |
| 162 | fprintf(out, " return;\n"); |
| 163 | fprintf(out, " }\n"); |
| 164 | } |
| 165 | } |
| 166 | fprintf(out, "\n event.begin();\n"); |
| 167 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 168 | attributionDecl.fields.front().name.c_str()); |
| 169 | fprintf(out, " event.begin();\n"); |
| 170 | for (const auto &chainField : attributionDecl.fields) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 171 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 172 | fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str()); |
| 173 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 174 | fprintf(out, " } else {\n"); |
| 175 | fprintf(out, " event << \"\";\n"); |
| 176 | fprintf(out, " }\n"); |
| 177 | } else { |
| 178 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 179 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 180 | } |
| 181 | fprintf(out, " event.end();\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 182 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 183 | fprintf(out, " event.end();\n\n"); |
| 184 | } else { |
| 185 | if (*arg == JAVA_TYPE_STRING) { |
| 186 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 187 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 188 | fprintf(out, " }\n"); |
| 189 | } |
| 190 | fprintf(out, " event << arg%d;\n", argIndex); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 191 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 192 | argIndex++; |
| 193 | } |
| 194 | |
| 195 | fprintf(out, " event.write(LOG_ID_STATS);\n"); |
| 196 | fprintf(out, "}\n"); |
| 197 | fprintf(out, "\n"); |
| 198 | } |
| 199 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 200 | for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin(); |
| 201 | signature != atoms.non_chained_signatures.end(); signature++) { |
| 202 | int argIndex; |
| 203 | |
| 204 | fprintf(out, "void\n"); |
| 205 | fprintf(out, "stats_write_non_chained(int32_t code"); |
| 206 | argIndex = 1; |
| 207 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 208 | arg != signature->end(); arg++) { |
| 209 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 210 | argIndex++; |
| 211 | } |
| 212 | fprintf(out, ")\n"); |
| 213 | |
| 214 | fprintf(out, "{\n"); |
| 215 | argIndex = 1; |
| 216 | fprintf(out, " android_log_event_list event(kStatsEventTag);\n"); |
| 217 | fprintf(out, " event << code;\n\n"); |
| 218 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 219 | arg != signature->end(); arg++) { |
| 220 | if (argIndex == 1) { |
| 221 | fprintf(out, " event.begin();\n\n"); |
| 222 | fprintf(out, " event.begin();\n"); |
| 223 | } |
| 224 | if (*arg == JAVA_TYPE_STRING) { |
| 225 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 226 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 227 | fprintf(out, " }\n"); |
| 228 | } |
| 229 | fprintf(out, " event << arg%d;\n", argIndex); |
| 230 | if (argIndex == 2) { |
| 231 | fprintf(out, " event.end();\n\n"); |
| 232 | fprintf(out, " event.end();\n\n"); |
| 233 | } |
| 234 | argIndex++; |
| 235 | } |
| 236 | |
| 237 | fprintf(out, " event.write(LOG_ID_STATS);\n"); |
| 238 | fprintf(out, "}\n"); |
| 239 | fprintf(out, "\n"); |
| 240 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 241 | // Print footer |
| 242 | fprintf(out, "\n"); |
| 243 | fprintf(out, "} // namespace util\n"); |
| 244 | fprintf(out, "} // namespace android\n"); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 249 | void build_non_chained_decl_map(const Atoms& atoms, |
| 250 | std::map<int, set<AtomDecl>::const_iterator>* decl_map){ |
| 251 | for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin(); |
| 252 | atom != atoms.non_chained_decls.end(); atom++) { |
| 253 | decl_map->insert(std::make_pair(atom->code, atom)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static void write_cpp_usage( |
| 258 | FILE* out, const string& method_name, const string& atom_code_name, |
| 259 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
| 260 | fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str()); |
| 261 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 262 | field != atom.fields.end(); field++) { |
| 263 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 264 | for (auto chainField : attributionDecl.fields) { |
| 265 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 266 | fprintf(out, ", const std::vector<%s>& %s", |
| 267 | cpp_type_name(chainField.javaType), |
| 268 | chainField.name.c_str()); |
| 269 | } else { |
| 270 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 271 | cpp_type_name(chainField.javaType), |
| 272 | chainField.name.c_str(), chainField.name.c_str()); |
| 273 | } |
| 274 | } |
| 275 | } else { |
| 276 | fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str()); |
| 277 | } |
| 278 | } |
| 279 | fprintf(out, ");\n"); |
| 280 | } |
| 281 | |
| 282 | static void write_cpp_method_header( |
| 283 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 284 | const AtomDecl &attributionDecl) { |
| 285 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 286 | signature != signatures.end(); signature++) { |
| 287 | fprintf(out, "void %s(int32_t code ", method_name.c_str()); |
| 288 | int argIndex = 1; |
| 289 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 290 | arg != signature->end(); arg++) { |
| 291 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 292 | for (auto chainField : attributionDecl.fields) { |
| 293 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 294 | fprintf(out, ", const std::vector<%s>& %s", |
| 295 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 296 | } else { |
| 297 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 298 | cpp_type_name(chainField.javaType), |
| 299 | chainField.name.c_str(), chainField.name.c_str()); |
| 300 | } |
| 301 | } |
| 302 | } else { |
| 303 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 304 | } |
| 305 | argIndex++; |
| 306 | } |
| 307 | fprintf(out, ");\n"); |
| 308 | |
| 309 | } |
| 310 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 311 | |
| 312 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 313 | write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 314 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 315 | // Print prelude |
| 316 | fprintf(out, "// This file is autogenerated\n"); |
| 317 | fprintf(out, "\n"); |
| 318 | fprintf(out, "#pragma once\n"); |
| 319 | fprintf(out, "\n"); |
| 320 | fprintf(out, "#include <stdint.h>\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 321 | fprintf(out, "#include <vector>\n"); |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 322 | fprintf(out, "#include <set>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 323 | fprintf(out, "\n"); |
| 324 | |
| 325 | fprintf(out, "namespace android {\n"); |
| 326 | fprintf(out, "namespace util {\n"); |
| 327 | fprintf(out, "\n"); |
| 328 | fprintf(out, "/*\n"); |
| 329 | fprintf(out, " * API For logging statistics events.\n"); |
| 330 | fprintf(out, " */\n"); |
| 331 | fprintf(out, "\n"); |
| 332 | fprintf(out, "/**\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 333 | fprintf(out, " * Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 334 | fprintf(out, " */\n"); |
| 335 | fprintf(out, "enum {\n"); |
| 336 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 337 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 338 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 339 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 340 | size_t i = 0; |
| 341 | // Print constants |
| 342 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 343 | atom != atoms.decls.end(); atom++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 344 | string constant = make_constant_name(atom->name); |
| 345 | fprintf(out, "\n"); |
| 346 | fprintf(out, " /**\n"); |
| 347 | fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 348 | write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl); |
| 349 | |
| 350 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 351 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 352 | write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second, |
| 353 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 354 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 355 | fprintf(out, " */\n"); |
| 356 | char const* const comma = (i == atoms.decls.size() - 1) ? "" : ","; |
| 357 | fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 358 | if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) { |
| 359 | maxPushedAtomId = atom->code; |
| 360 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 361 | i++; |
| 362 | } |
| 363 | fprintf(out, "\n"); |
| 364 | fprintf(out, "};\n"); |
| 365 | fprintf(out, "\n"); |
| 366 | |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 367 | fprintf(out, "const static std::set<int> kAtomsWithUidField = {\n"); |
| 368 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 369 | atom != atoms.decls.end(); atom++) { |
| 370 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 371 | field != atom->fields.end(); field++) { |
| 372 | if (field->name == "uid") { |
| 373 | string constant = make_constant_name(atom->name); |
| 374 | fprintf(out, " %s,\n", constant.c_str()); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | fprintf(out, "};\n"); |
| 380 | fprintf(out, "\n"); |
| 381 | |
| 382 | fprintf(out, "const static std::set<int> kAtomsWithAttributionChain = {\n"); |
| 383 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 384 | atom != atoms.decls.end(); atom++) { |
| 385 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 386 | field != atom->fields.end(); field++) { |
| 387 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 388 | string constant = make_constant_name(atom->name); |
| 389 | fprintf(out, " %s,\n", constant.c_str()); |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | fprintf(out, "};\n"); |
| 395 | fprintf(out, "\n"); |
| 396 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 397 | fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 398 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 399 | // Print write methods |
| 400 | fprintf(out, "//\n"); |
| 401 | fprintf(out, "// Write methods\n"); |
| 402 | fprintf(out, "//\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 403 | write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl); |
| 404 | |
| 405 | fprintf(out, "//\n"); |
| 406 | fprintf(out, "// Write flattened methods\n"); |
| 407 | fprintf(out, "//\n"); |
| 408 | write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures, |
| 409 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 410 | |
| 411 | fprintf(out, "\n"); |
| 412 | fprintf(out, "} // namespace util\n"); |
| 413 | fprintf(out, "} // namespace android\n"); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 418 | static void write_java_usage( |
| 419 | FILE* out, const string& method_name, const string& atom_code_name, |
| 420 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
| 421 | fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s", |
| 422 | method_name.c_str(), atom_code_name.c_str()); |
| 423 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 424 | field != atom.fields.end(); field++) { |
| 425 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 426 | for (auto chainField : attributionDecl.fields) { |
| 427 | fprintf(out, ", %s[] %s", |
| 428 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 429 | } |
| 430 | } else { |
| 431 | fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str()); |
| 432 | } |
| 433 | } |
| 434 | fprintf(out, ");\n"); |
| 435 | } |
| 436 | |
| 437 | static void write_java_method( |
| 438 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 439 | const AtomDecl &attributionDecl) { |
| 440 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 441 | signature != signatures.end(); signature++) { |
| 442 | fprintf(out, " public static native void %s(int code", method_name.c_str()); |
| 443 | int argIndex = 1; |
| 444 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 445 | arg != signature->end(); arg++) { |
| 446 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 447 | for (auto chainField : attributionDecl.fields) { |
| 448 | fprintf(out, ", %s[] %s", |
| 449 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 450 | } |
| 451 | } else { |
| 452 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 453 | } |
| 454 | argIndex++; |
| 455 | } |
| 456 | fprintf(out, ");\n"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 461 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 462 | write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 463 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 464 | // Print prelude |
| 465 | fprintf(out, "// This file is autogenerated\n"); |
| 466 | fprintf(out, "\n"); |
| 467 | fprintf(out, "package android.util;\n"); |
| 468 | fprintf(out, "\n"); |
| 469 | fprintf(out, "\n"); |
| 470 | fprintf(out, "/**\n"); |
| 471 | fprintf(out, " * API For logging statistics events.\n"); |
| 472 | fprintf(out, " * @hide\n"); |
| 473 | fprintf(out, " */\n"); |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 474 | fprintf(out, "public class StatsLogInternal {\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 475 | fprintf(out, " // Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 476 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 477 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 478 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 479 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 480 | // Print constants for the atom codes. |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 481 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 482 | atom != atoms.decls.end(); atom++) { |
| 483 | string constant = make_constant_name(atom->name); |
| 484 | fprintf(out, "\n"); |
| 485 | fprintf(out, " /**\n"); |
| 486 | fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 487 | write_java_usage(out, "write", constant, *atom, attributionDecl); |
| 488 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 489 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 490 | write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second, |
| 491 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 492 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 493 | fprintf(out, " */\n"); |
| 494 | fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code); |
| 495 | } |
| 496 | fprintf(out, "\n"); |
| 497 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 498 | // Print constants for the enum values. |
| 499 | fprintf(out, " // Constants for enum values.\n\n"); |
| 500 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 501 | atom != atoms.decls.end(); atom++) { |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 502 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 503 | field != atom->fields.end(); field++) { |
| 504 | if (field->javaType == JAVA_TYPE_ENUM) { |
| 505 | fprintf(out, " // Values for %s.%s\n", atom->message.c_str(), |
| 506 | field->name.c_str()); |
| 507 | for (map<int, string>::const_iterator value = field->enumValues.begin(); |
| 508 | value != field->enumValues.end(); value++) { |
| 509 | fprintf(out, " public static final int %s__%s__%s = %d;\n", |
| 510 | make_constant_name(atom->message).c_str(), |
| 511 | make_constant_name(field->name).c_str(), |
| 512 | make_constant_name(value->second).c_str(), |
| 513 | value->first); |
| 514 | } |
| 515 | fprintf(out, "\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 516 | } |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 520 | // Print write methods |
| 521 | fprintf(out, " // Write methods\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 522 | write_java_method(out, "write", atoms.signatures, attributionDecl); |
| 523 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 524 | |
| 525 | fprintf(out, "}\n"); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static const char* |
| 531 | jni_type_name(java_type_t type) |
| 532 | { |
| 533 | switch (type) { |
| 534 | case JAVA_TYPE_BOOLEAN: |
| 535 | return "jboolean"; |
| 536 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 537 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 538 | return "jint"; |
| 539 | case JAVA_TYPE_LONG: |
| 540 | return "jlong"; |
| 541 | case JAVA_TYPE_FLOAT: |
| 542 | return "jfloat"; |
| 543 | case JAVA_TYPE_DOUBLE: |
| 544 | return "jdouble"; |
| 545 | case JAVA_TYPE_STRING: |
| 546 | return "jstring"; |
| 547 | default: |
| 548 | return "UNKNOWN"; |
| 549 | } |
| 550 | } |
| 551 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 552 | static const char* |
| 553 | jni_array_type_name(java_type_t type) |
| 554 | { |
| 555 | switch (type) { |
| 556 | case JAVA_TYPE_INT: |
| 557 | return "jintArray"; |
| 558 | case JAVA_TYPE_STRING: |
| 559 | return "jobjectArray"; |
| 560 | default: |
| 561 | return "UNKNOWN"; |
| 562 | } |
| 563 | } |
| 564 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 565 | static string |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 566 | jni_function_name(const string& method_name, const vector<java_type_t>& signature) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 567 | { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 568 | string result("StatsLog_" + method_name); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 569 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 570 | arg != signature.end(); arg++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 571 | switch (*arg) { |
| 572 | case JAVA_TYPE_BOOLEAN: |
| 573 | result += "_boolean"; |
| 574 | break; |
| 575 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 576 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 577 | result += "_int"; |
| 578 | break; |
| 579 | case JAVA_TYPE_LONG: |
| 580 | result += "_long"; |
| 581 | break; |
| 582 | case JAVA_TYPE_FLOAT: |
| 583 | result += "_float"; |
| 584 | break; |
| 585 | case JAVA_TYPE_DOUBLE: |
| 586 | result += "_double"; |
| 587 | break; |
| 588 | case JAVA_TYPE_STRING: |
| 589 | result += "_String"; |
| 590 | break; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 591 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 592 | result += "_AttributionChain"; |
| 593 | break; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 594 | default: |
| 595 | result += "_UNKNOWN"; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | static const char* |
| 603 | java_type_signature(java_type_t type) |
| 604 | { |
| 605 | switch (type) { |
| 606 | case JAVA_TYPE_BOOLEAN: |
| 607 | return "Z"; |
| 608 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 609 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 610 | return "I"; |
| 611 | case JAVA_TYPE_LONG: |
| 612 | return "J"; |
| 613 | case JAVA_TYPE_FLOAT: |
| 614 | return "F"; |
| 615 | case JAVA_TYPE_DOUBLE: |
| 616 | return "D"; |
| 617 | case JAVA_TYPE_STRING: |
| 618 | return "Ljava/lang/String;"; |
| 619 | default: |
| 620 | return "UNKNOWN"; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static string |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 625 | jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 626 | { |
| 627 | string result("(I"); |
| 628 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 629 | arg != signature.end(); arg++) { |
| 630 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 631 | for (auto chainField : attributionDecl.fields) { |
| 632 | result += "["; |
| 633 | result += java_type_signature(chainField.javaType); |
| 634 | } |
| 635 | } else { |
| 636 | result += java_type_signature(*arg); |
| 637 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 638 | } |
| 639 | result += ")V"; |
| 640 | return result; |
| 641 | } |
| 642 | |
| 643 | static int |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 644 | write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name, |
| 645 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 646 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 647 | // Print write methods |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 648 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 649 | signature != signatures.end(); signature++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 650 | int argIndex; |
| 651 | |
| 652 | fprintf(out, "static void\n"); |
| 653 | fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code", |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 654 | jni_function_name(java_method_name, *signature).c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 655 | argIndex = 1; |
| 656 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 657 | arg != signature->end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 658 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 659 | for (auto chainField : attributionDecl.fields) { |
| 660 | fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType), |
| 661 | chainField.name.c_str()); |
| 662 | } |
| 663 | } else { |
| 664 | fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex); |
| 665 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 666 | argIndex++; |
| 667 | } |
| 668 | fprintf(out, ")\n"); |
| 669 | |
| 670 | fprintf(out, "{\n"); |
| 671 | |
| 672 | // Prepare strings |
| 673 | argIndex = 1; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 674 | bool hadStringOrChain = false; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 675 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 676 | arg != signature->end(); arg++) { |
| 677 | if (*arg == JAVA_TYPE_STRING) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 678 | hadStringOrChain = true; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 679 | fprintf(out, " const char* str%d;\n", argIndex); |
| 680 | fprintf(out, " if (arg%d != NULL) {\n", argIndex); |
| 681 | fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n", |
| 682 | argIndex, argIndex); |
| 683 | fprintf(out, " } else {\n"); |
| 684 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 685 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 686 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 687 | hadStringOrChain = true; |
| 688 | for (auto chainField : attributionDecl.fields) { |
| 689 | fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n", |
| 690 | chainField.name.c_str(), chainField.name.c_str()); |
| 691 | if (chainField.name != attributionDecl.fields.front().name) { |
| 692 | fprintf(out, " if (%s_length != %s_length) {\n", |
| 693 | chainField.name.c_str(), |
| 694 | attributionDecl.fields.front().name.c_str()); |
| 695 | fprintf(out, " jniThrowException(env, " |
| 696 | "\"java/lang/IllegalArgumentException\", " |
| 697 | "\"invalid attribution field(%s) length.\");\n", |
| 698 | chainField.name.c_str()); |
| 699 | fprintf(out, " return;\n"); |
| 700 | fprintf(out, " }\n"); |
| 701 | } |
| 702 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 703 | fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n", |
| 704 | chainField.name.c_str(), chainField.name.c_str()); |
| 705 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 706 | fprintf(out, " std::vector<%s> %s_vec;\n", |
| 707 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 708 | fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n", |
| 709 | chainField.name.c_str()); |
| 710 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 711 | chainField.name.c_str()); |
| 712 | fprintf(out, " jstring jstr = " |
| 713 | "(jstring)env->GetObjectArrayElement(%s, i);\n", |
| 714 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 715 | fprintf(out, " if (jstr == NULL) {\n"); |
| 716 | fprintf(out, " %s_vec.push_back(NULL);\n", |
| 717 | chainField.name.c_str()); |
| 718 | fprintf(out, " } else {\n"); |
| 719 | fprintf(out, " ScopedUtfChars* scoped_%s = " |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 720 | "new ScopedUtfChars(env, jstr);\n", |
| 721 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 722 | fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 723 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 724 | fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 725 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 726 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 727 | fprintf(out, " }\n"); |
| 728 | } |
| 729 | fprintf(out, "\n"); |
| 730 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 731 | } |
| 732 | argIndex++; |
| 733 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 734 | // Emit this to quiet the unused parameter warning if there were no strings or attribution |
| 735 | // chains. |
| 736 | if (!hadStringOrChain) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 737 | fprintf(out, " (void)env;\n"); |
| 738 | } |
| 739 | |
| 740 | // stats_write call |
| 741 | argIndex = 1; |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 742 | fprintf(out, " android::util::%s(code", cpp_method_name.c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 743 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 744 | arg != signature->end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 745 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 746 | for (auto chainField : attributionDecl.fields) { |
| 747 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 748 | fprintf(out, ", (const %s*)%s_array, %s_length", |
| 749 | cpp_type_name(chainField.javaType), |
| 750 | chainField.name.c_str(), chainField.name.c_str()); |
| 751 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 752 | fprintf(out, ", %s_vec", chainField.name.c_str()); |
| 753 | } |
| 754 | } |
| 755 | } else { |
| 756 | const char *argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg"; |
| 757 | fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex); |
| 758 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 759 | argIndex++; |
| 760 | } |
| 761 | fprintf(out, ");\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 762 | fprintf(out, "\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 763 | |
| 764 | // Clean up strings |
| 765 | argIndex = 1; |
| 766 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 767 | arg != signature->end(); arg++) { |
| 768 | if (*arg == JAVA_TYPE_STRING) { |
| 769 | fprintf(out, " if (str%d != NULL) {\n", argIndex); |
| 770 | fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n", |
| 771 | argIndex, argIndex); |
| 772 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 773 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 774 | for (auto chainField : attributionDecl.fields) { |
| 775 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 776 | fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n", |
| 777 | chainField.name.c_str(), chainField.name.c_str()); |
| 778 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 779 | fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 780 | chainField.name.c_str()); |
| 781 | fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str()); |
| 782 | fprintf(out, " }\n"); |
| 783 | } |
| 784 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 785 | } |
| 786 | argIndex++; |
| 787 | } |
| 788 | |
| 789 | fprintf(out, "}\n"); |
| 790 | fprintf(out, "\n"); |
| 791 | } |
| 792 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | void write_jni_registration(FILE* out, const string& java_method_name, |
| 798 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) { |
| 799 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 800 | signature != signatures.end(); signature++) { |
| 801 | fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n", |
| 802 | java_method_name.c_str(), |
| 803 | jni_function_signature(*signature, attributionDecl).c_str(), |
| 804 | jni_function_name(java_method_name, *signature).c_str()); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | static int |
| 809 | write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
| 810 | { |
| 811 | // Print prelude |
| 812 | fprintf(out, "// This file is autogenerated\n"); |
| 813 | fprintf(out, "\n"); |
| 814 | |
| 815 | fprintf(out, "#include <statslog.h>\n"); |
| 816 | fprintf(out, "\n"); |
| 817 | fprintf(out, "#include <nativehelper/JNIHelp.h>\n"); |
| 818 | fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n"); |
| 819 | fprintf(out, "#include <utils/Vector.h>\n"); |
| 820 | fprintf(out, "#include \"core_jni_helpers.h\"\n"); |
| 821 | fprintf(out, "#include \"jni.h\"\n"); |
| 822 | fprintf(out, "\n"); |
| 823 | fprintf(out, "#define UNUSED __attribute__((__unused__))\n"); |
| 824 | fprintf(out, "\n"); |
| 825 | |
| 826 | fprintf(out, "namespace android {\n"); |
| 827 | fprintf(out, "\n"); |
| 828 | |
| 829 | write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl); |
| 830 | write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained", |
| 831 | atoms.non_chained_signatures, attributionDecl); |
| 832 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 833 | // Print registration function table |
| 834 | fprintf(out, "/*\n"); |
| 835 | fprintf(out, " * JNI registration.\n"); |
| 836 | fprintf(out, " */\n"); |
| 837 | fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 838 | write_jni_registration(out, "write", atoms.signatures, attributionDecl); |
| 839 | write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 840 | fprintf(out, "};\n"); |
| 841 | fprintf(out, "\n"); |
| 842 | |
| 843 | // Print registration function |
| 844 | fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n"); |
| 845 | fprintf(out, " return RegisterMethodsOrDie(\n"); |
| 846 | fprintf(out, " env,\n"); |
| 847 | fprintf(out, " \"android/util/StatsLog\",\n"); |
| 848 | fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n"); |
| 849 | fprintf(out, "}\n"); |
| 850 | |
| 851 | fprintf(out, "\n"); |
| 852 | fprintf(out, "} // namespace android\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 856 | static void |
| 857 | print_usage() |
| 858 | { |
| 859 | fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n"); |
| 860 | fprintf(stderr, "\n"); |
| 861 | fprintf(stderr, "OPTIONS\n"); |
| 862 | fprintf(stderr, " --cpp FILENAME the header file to output\n"); |
| 863 | fprintf(stderr, " --header FILENAME the cpp file to output\n"); |
| 864 | fprintf(stderr, " --help this message\n"); |
| 865 | fprintf(stderr, " --java FILENAME the java file to output\n"); |
| 866 | fprintf(stderr, " --jni FILENAME the jni file to output\n"); |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Do the argument parsing and execute the tasks. |
| 871 | */ |
| 872 | static int |
| 873 | run(int argc, char const*const* argv) |
| 874 | { |
| 875 | string cppFilename; |
| 876 | string headerFilename; |
| 877 | string javaFilename; |
| 878 | string jniFilename; |
| 879 | |
| 880 | int index = 1; |
| 881 | while (index < argc) { |
| 882 | if (0 == strcmp("--help", argv[index])) { |
| 883 | print_usage(); |
| 884 | return 0; |
| 885 | } else if (0 == strcmp("--cpp", argv[index])) { |
| 886 | index++; |
| 887 | if (index >= argc) { |
| 888 | print_usage(); |
| 889 | return 1; |
| 890 | } |
| 891 | cppFilename = argv[index]; |
| 892 | } else if (0 == strcmp("--header", argv[index])) { |
| 893 | index++; |
| 894 | if (index >= argc) { |
| 895 | print_usage(); |
| 896 | return 1; |
| 897 | } |
| 898 | headerFilename = argv[index]; |
| 899 | } else if (0 == strcmp("--java", argv[index])) { |
| 900 | index++; |
| 901 | if (index >= argc) { |
| 902 | print_usage(); |
| 903 | return 1; |
| 904 | } |
| 905 | javaFilename = argv[index]; |
| 906 | } else if (0 == strcmp("--jni", argv[index])) { |
| 907 | index++; |
| 908 | if (index >= argc) { |
| 909 | print_usage(); |
| 910 | return 1; |
| 911 | } |
| 912 | jniFilename = argv[index]; |
| 913 | } |
| 914 | index++; |
| 915 | } |
| 916 | |
| 917 | if (cppFilename.size() == 0 |
| 918 | && headerFilename.size() == 0 |
| 919 | && javaFilename.size() == 0 |
| 920 | && jniFilename.size() == 0) { |
| 921 | print_usage(); |
| 922 | return 1; |
| 923 | } |
| 924 | |
| 925 | // Collate the parameters |
| 926 | Atoms atoms; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 927 | int errorCount = collate_atoms(Atom::descriptor(), &atoms); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 928 | if (errorCount != 0) { |
| 929 | return 1; |
| 930 | } |
| 931 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 932 | AtomDecl attributionDecl; |
| 933 | vector<java_type_t> attributionSignature; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 934 | collate_atom(android::os::statsd::AttributionNode::descriptor(), |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 935 | &attributionDecl, &attributionSignature); |
| 936 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 937 | // Write the .cpp file |
| 938 | if (cppFilename.size() != 0) { |
| 939 | FILE* out = fopen(cppFilename.c_str(), "w"); |
| 940 | if (out == NULL) { |
| 941 | fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str()); |
| 942 | return 1; |
| 943 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 944 | errorCount = android::stats_log_api_gen::write_stats_log_cpp( |
| 945 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 946 | fclose(out); |
| 947 | } |
| 948 | |
| 949 | // Write the .h file |
| 950 | if (headerFilename.size() != 0) { |
| 951 | FILE* out = fopen(headerFilename.c_str(), "w"); |
| 952 | if (out == NULL) { |
| 953 | fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str()); |
| 954 | return 1; |
| 955 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 956 | errorCount = android::stats_log_api_gen::write_stats_log_header( |
| 957 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 958 | fclose(out); |
| 959 | } |
| 960 | |
| 961 | // Write the .java file |
| 962 | if (javaFilename.size() != 0) { |
| 963 | FILE* out = fopen(javaFilename.c_str(), "w"); |
| 964 | if (out == NULL) { |
| 965 | fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str()); |
| 966 | return 1; |
| 967 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 968 | errorCount = android::stats_log_api_gen::write_stats_log_java( |
| 969 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 970 | fclose(out); |
| 971 | } |
| 972 | |
| 973 | // Write the jni file |
| 974 | if (jniFilename.size() != 0) { |
| 975 | FILE* out = fopen(jniFilename.c_str(), "w"); |
| 976 | if (out == NULL) { |
| 977 | fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str()); |
| 978 | return 1; |
| 979 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 980 | errorCount = android::stats_log_api_gen::write_stats_log_jni( |
| 981 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 982 | fclose(out); |
| 983 | } |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * Main. |
| 993 | */ |
| 994 | int |
| 995 | main(int argc, char const*const* argv) |
| 996 | { |
| 997 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 998 | |
| 999 | return android::stats_log_api_gen::run(argc, argv); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1000 | } |