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