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 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 15 | #include "android-base/strings.h" |
| 16 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 17 | using namespace google::protobuf; |
| 18 | using namespace std; |
| 19 | |
| 20 | namespace android { |
| 21 | namespace stats_log_api_gen { |
| 22 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 23 | int maxPushedAtomId = 2; |
| 24 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 25 | const string DEFAULT_MODULE_NAME = "DEFAULT"; |
| 26 | const string DEFAULT_CPP_NAMESPACE = "android,util"; |
| 27 | const string DEFAULT_CPP_HEADER_IMPORT = "statslog.h"; |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 28 | const string DEFAULT_JAVA_PACKAGE = "android.util"; |
| 29 | const string DEFAULT_JAVA_CLASS = "StatsLogInternal"; |
| 30 | |
| 31 | const int JAVA_MODULE_REQUIRES_FLOAT = 0x01; |
| 32 | const int JAVA_MODULE_REQUIRES_ATTRIBUTION = 0x02; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 33 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 34 | using android::os::statsd::Atom; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 35 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Turn lower and camel case into upper case with underscores. |
| 38 | */ |
| 39 | static string |
| 40 | make_constant_name(const string& str) |
| 41 | { |
| 42 | string result; |
| 43 | const int N = str.size(); |
| 44 | bool underscore_next = false; |
| 45 | for (int i=0; i<N; i++) { |
| 46 | char c = str[i]; |
| 47 | if (c >= 'A' && c <= 'Z') { |
| 48 | if (underscore_next) { |
| 49 | result += '_'; |
| 50 | underscore_next = false; |
| 51 | } |
| 52 | } else if (c >= 'a' && c <= 'z') { |
| 53 | c = 'A' + c - 'a'; |
| 54 | underscore_next = true; |
| 55 | } else if (c == '_') { |
| 56 | underscore_next = false; |
| 57 | } |
| 58 | result += c; |
| 59 | } |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | static const char* |
| 64 | cpp_type_name(java_type_t type) |
| 65 | { |
| 66 | switch (type) { |
| 67 | case JAVA_TYPE_BOOLEAN: |
| 68 | return "bool"; |
| 69 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 70 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 71 | return "int32_t"; |
| 72 | case JAVA_TYPE_LONG: |
| 73 | return "int64_t"; |
| 74 | case JAVA_TYPE_FLOAT: |
| 75 | return "float"; |
| 76 | case JAVA_TYPE_DOUBLE: |
| 77 | return "double"; |
| 78 | case JAVA_TYPE_STRING: |
| 79 | return "char const*"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 80 | case JAVA_TYPE_BYTE_ARRAY: |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 81 | return "const BytesField&"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 82 | default: |
| 83 | return "UNKNOWN"; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static const char* |
| 88 | java_type_name(java_type_t type) |
| 89 | { |
| 90 | switch (type) { |
| 91 | case JAVA_TYPE_BOOLEAN: |
| 92 | return "boolean"; |
| 93 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 94 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 95 | return "int"; |
| 96 | case JAVA_TYPE_LONG: |
| 97 | return "long"; |
| 98 | case JAVA_TYPE_FLOAT: |
| 99 | return "float"; |
| 100 | case JAVA_TYPE_DOUBLE: |
| 101 | return "double"; |
| 102 | case JAVA_TYPE_STRING: |
| 103 | return "java.lang.String"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 104 | case JAVA_TYPE_BYTE_ARRAY: |
| 105 | return "byte[]"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 106 | default: |
| 107 | return "UNKNOWN"; |
| 108 | } |
| 109 | } |
| 110 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 111 | static bool atom_needed_for_module(const AtomDecl& atomDecl, const string& moduleName) { |
| 112 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 113 | return true; |
| 114 | } |
| 115 | return atomDecl.hasModule && (moduleName == atomDecl.moduleName); |
| 116 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 117 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 118 | static bool signature_needed_for_module(const set<string>& modules, const string& moduleName) { |
| 119 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 120 | return true; |
| 121 | } |
| 122 | return modules.find(moduleName) != modules.end(); |
| 123 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 124 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 125 | static void write_atoms_info_cpp(FILE *out, const Atoms &atoms) { |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 126 | std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed", |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 127 | "audio_state_changed", |
| 128 | "call_state_changed", |
| 129 | "phone_signal_strength_changed", |
| 130 | "mobile_bytes_transfer_by_fg_bg", |
| 131 | "mobile_bytes_transfer"}; |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 132 | fprintf(out, |
| 133 | "const std::set<int> " |
| 134 | "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n"); |
| 135 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 136 | atom != atoms.decls.end(); atom++) { |
| 137 | if (kTruncatingAtomNames.find(atom->name) == |
| 138 | kTruncatingAtomNames.end()) { |
| 139 | string constant = make_constant_name(atom->name); |
| 140 | fprintf(out, " %s,\n", constant.c_str()); |
| 141 | } |
| 142 | } |
| 143 | fprintf(out, "};\n"); |
| 144 | fprintf(out, "\n"); |
| 145 | |
| 146 | fprintf(out, |
| 147 | "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n"); |
| 148 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 149 | atom != atoms.decls.end(); atom++) { |
| 150 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 151 | field != atom->fields.end(); field++) { |
| 152 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 153 | string constant = make_constant_name(atom->name); |
| 154 | fprintf(out, " %s,\n", constant.c_str()); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | } |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 159 | |
| 160 | fprintf(out, "};\n"); |
| 161 | fprintf(out, "\n"); |
| 162 | |
| 163 | fprintf(out, |
| 164 | "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n"); |
| 165 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 166 | atom != atoms.decls.end(); atom++) { |
| 167 | if (atom->whitelisted) { |
| 168 | string constant = make_constant_name(atom->name); |
| 169 | fprintf(out, " %s,\n", constant.c_str()); |
| 170 | } |
| 171 | } |
| 172 | |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 173 | fprintf(out, "};\n"); |
| 174 | fprintf(out, "\n"); |
| 175 | |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 176 | fprintf(out, "static std::map<int, int> getAtomUidField() {\n"); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 177 | fprintf(out, " std::map<int, int> uidField;\n"); |
| 178 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 179 | atom != atoms.decls.end(); atom++) { |
| 180 | if (atom->uidField == 0) { |
| 181 | continue; |
| 182 | } |
| 183 | fprintf(out, |
| 184 | "\n // Adding uid field for atom " |
| 185 | "(%d)%s\n", |
| 186 | atom->code, atom->name.c_str()); |
| 187 | fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n", |
| 188 | make_constant_name(atom->name).c_str(), atom->uidField); |
| 189 | } |
| 190 | |
| 191 | fprintf(out, " return uidField;\n"); |
| 192 | fprintf(out, "};\n"); |
| 193 | |
| 194 | fprintf(out, |
| 195 | "const std::map<int, int> AtomsInfo::kAtomsWithUidField = " |
| 196 | "getAtomUidField();\n"); |
| 197 | |
| 198 | fprintf(out, |
| 199 | "static std::map<int, StateAtomFieldOptions> " |
| 200 | "getStateAtomFieldOptions() {\n"); |
| 201 | fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n"); |
| 202 | fprintf(out, " StateAtomFieldOptions opt;\n"); |
| 203 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 204 | atom != atoms.decls.end(); atom++) { |
| 205 | if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) { |
| 206 | continue; |
| 207 | } |
| 208 | fprintf(out, |
| 209 | "\n // Adding primary and exclusive fields for atom " |
| 210 | "(%d)%s\n", |
| 211 | atom->code, atom->name.c_str()); |
| 212 | fprintf(out, " opt.primaryFields.clear();\n"); |
| 213 | for (const auto& field : atom->primaryFields) { |
| 214 | fprintf(out, " opt.primaryFields.push_back(%d);\n", field); |
| 215 | } |
| 216 | |
| 217 | fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField); |
| 218 | fprintf(out, " options[static_cast<int>(%s)] = opt;\n", |
| 219 | make_constant_name(atom->name).c_str()); |
| 220 | } |
| 221 | |
| 222 | fprintf(out, " return options;\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 223 | fprintf(out, "}\n"); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 224 | |
| 225 | fprintf(out, |
| 226 | "const std::map<int, StateAtomFieldOptions> " |
| 227 | "AtomsInfo::kStateAtomsFieldOptions = " |
| 228 | "getStateAtomFieldOptions();\n"); |
| 229 | |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 230 | fprintf(out, |
| 231 | "static std::map<int, std::vector<int>> " |
| 232 | "getBinaryFieldAtoms() {\n"); |
| 233 | fprintf(out, " std::map<int, std::vector<int>> options;\n"); |
| 234 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 235 | atom != atoms.decls.end(); atom++) { |
| 236 | if (atom->binaryFields.size() == 0) { |
| 237 | continue; |
| 238 | } |
| 239 | fprintf(out, |
| 240 | "\n // Adding binary fields for atom " |
| 241 | "(%d)%s\n", |
| 242 | atom->code, atom->name.c_str()); |
| 243 | |
| 244 | for (const auto& field : atom->binaryFields) { |
| 245 | fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n", |
| 246 | make_constant_name(atom->name).c_str(), field); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | fprintf(out, " return options;\n"); |
| 251 | fprintf(out, "}\n"); |
| 252 | |
| 253 | fprintf(out, |
| 254 | "const std::map<int, std::vector<int>> " |
| 255 | "AtomsInfo::kBytesFieldAtoms = " |
| 256 | "getBinaryFieldAtoms();\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Writes namespaces for the cpp and header files, returning the number of namespaces written. |
| 260 | void write_namespace(FILE* out, const string& cppNamespaces) { |
| 261 | vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ","); |
| 262 | for (string cppNamespace : cppNamespaceVec) { |
| 263 | fprintf(out, "namespace %s {\n", cppNamespace.c_str()); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Writes namespace closing brackets for cpp and header files. |
| 268 | void write_closing_namespace(FILE* out, const string& cppNamespaces) { |
| 269 | vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ","); |
| 270 | for (auto it = cppNamespaceVec.rbegin(); it != cppNamespaceVec.rend(); ++it) { |
| 271 | fprintf(out, "} // namespace %s\n", it->c_str()); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static int write_stats_log_cpp(FILE *out, const Atoms &atoms, const AtomDecl &attributionDecl, |
| 276 | const string& moduleName, const string& cppNamespace, |
| 277 | const string& importHeader) { |
| 278 | // Print prelude |
| 279 | fprintf(out, "// This file is autogenerated\n"); |
| 280 | fprintf(out, "\n"); |
| 281 | |
| 282 | fprintf(out, "#include <mutex>\n"); |
| 283 | fprintf(out, "#include <chrono>\n"); |
| 284 | fprintf(out, "#include <thread>\n"); |
| 285 | fprintf(out, "#ifdef __ANDROID__\n"); |
| 286 | fprintf(out, "#include <cutils/properties.h>\n"); |
| 287 | fprintf(out, "#endif\n"); |
| 288 | fprintf(out, "#include <stats_event_list.h>\n"); |
| 289 | fprintf(out, "#include <log/log.h>\n"); |
| 290 | fprintf(out, "#include <%s>\n", importHeader.c_str()); |
| 291 | fprintf(out, "#include <utils/SystemClock.h>\n"); |
| 292 | fprintf(out, "\n"); |
| 293 | |
| 294 | write_namespace(out, cppNamespace); |
| 295 | fprintf(out, "// the single event tag id for all stats logs\n"); |
| 296 | fprintf(out, "const static int kStatsEventTag = 1937006964;\n"); |
| 297 | fprintf(out, "#ifdef __ANDROID__\n"); |
| 298 | fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n"); |
| 299 | fprintf(out, "#else\n"); |
| 300 | fprintf(out, "const static bool kStatsdEnabled = false;\n"); |
| 301 | fprintf(out, "#endif\n"); |
| 302 | |
| 303 | // AtomsInfo is only used by statsd internally and is not needed for other modules. |
| 304 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 305 | write_atoms_info_cpp(out, atoms); |
| 306 | } |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 307 | |
| 308 | fprintf(out, "int64_t lastRetryTimestampNs = -1;\n"); |
| 309 | fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n"); |
| 310 | fprintf(out, "static std::mutex mLogdRetryMutex;\n"); |
| 311 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 312 | // Print write methods |
| 313 | fprintf(out, "\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 314 | for (auto signature_to_modules_it = atoms.signatures_to_modules.begin(); |
| 315 | signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) { |
| 316 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 317 | continue; |
| 318 | } |
| 319 | vector<java_type_t> signature = signature_to_modules_it->first; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 320 | int argIndex; |
| 321 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 322 | fprintf(out, "int\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 323 | fprintf(out, "try_stats_write(int32_t code"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 324 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 325 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 326 | arg != signature.end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 327 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 328 | for (auto chainField : attributionDecl.fields) { |
| 329 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 330 | fprintf(out, ", const std::vector<%s>& %s", |
| 331 | cpp_type_name(chainField.javaType), |
| 332 | chainField.name.c_str()); |
| 333 | } else { |
| 334 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 335 | cpp_type_name(chainField.javaType), |
| 336 | chainField.name.c_str(), chainField.name.c_str()); |
| 337 | } |
| 338 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 339 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 340 | fprintf(out, ", const std::map<int, int32_t>& arg%d_1, " |
| 341 | "const std::map<int, int64_t>& arg%d_2, " |
| 342 | "const std::map<int, char const*>& arg%d_3, " |
| 343 | "const std::map<int, float>& arg%d_4", |
| 344 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 345 | } else { |
| 346 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 347 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 348 | argIndex++; |
| 349 | } |
| 350 | fprintf(out, ")\n"); |
| 351 | |
| 352 | fprintf(out, "{\n"); |
| 353 | argIndex = 1; |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 354 | fprintf(out, " if (kStatsdEnabled) {\n"); |
Yao Chen | f7bc6ab | 2018-04-18 13:45:48 -0700 | [diff] [blame] | 355 | fprintf(out, " stats_event_list event(kStatsEventTag);\n"); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 356 | fprintf(out, " event << android::elapsedRealtimeNano();\n\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 357 | fprintf(out, " event << code;\n\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 358 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 359 | arg != signature.end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 360 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 361 | for (const auto &chainField : attributionDecl.fields) { |
| 362 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 363 | fprintf(out, " if (%s_length != %s.size()) {\n", |
| 364 | attributionDecl.fields.front().name.c_str(), chainField.name.c_str()); |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 365 | fprintf(out, " return -EINVAL;\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 366 | fprintf(out, " }\n"); |
| 367 | } |
| 368 | } |
| 369 | fprintf(out, "\n event.begin();\n"); |
| 370 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 371 | attributionDecl.fields.front().name.c_str()); |
| 372 | fprintf(out, " event.begin();\n"); |
| 373 | for (const auto &chainField : attributionDecl.fields) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 374 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 375 | fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str()); |
| 376 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 377 | fprintf(out, " } else {\n"); |
| 378 | fprintf(out, " event << \"\";\n"); |
| 379 | fprintf(out, " }\n"); |
| 380 | } else { |
| 381 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 382 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 383 | } |
| 384 | fprintf(out, " event.end();\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 385 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 386 | fprintf(out, " event.end();\n\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 387 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 388 | fprintf(out, " event.begin();\n\n"); |
| 389 | fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex); |
| 390 | fprintf(out, " event.begin();\n"); |
| 391 | fprintf(out, " event << it.first;\n"); |
| 392 | fprintf(out, " event << it.second;\n"); |
| 393 | fprintf(out, " event.end();\n"); |
| 394 | fprintf(out, " }\n"); |
| 395 | |
| 396 | fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex); |
| 397 | fprintf(out, " event.begin();\n"); |
| 398 | fprintf(out, " event << it.first;\n"); |
| 399 | fprintf(out, " event << it.second;\n"); |
| 400 | fprintf(out, " event.end();\n"); |
| 401 | fprintf(out, " }\n"); |
| 402 | |
| 403 | fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex); |
| 404 | fprintf(out, " event.begin();\n"); |
| 405 | fprintf(out, " event << it.first;\n"); |
| 406 | fprintf(out, " event << it.second;\n"); |
| 407 | fprintf(out, " event.end();\n"); |
| 408 | fprintf(out, " }\n"); |
| 409 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 410 | fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex); |
| 411 | fprintf(out, " event.begin();\n"); |
| 412 | fprintf(out, " event << it.first;\n"); |
| 413 | fprintf(out, " event << it.second;\n"); |
| 414 | fprintf(out, " event.end();\n"); |
| 415 | fprintf(out, " }\n"); |
| 416 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 417 | fprintf(out, " event.end();\n\n"); |
Yao Chen | 1fe9f59 | 2018-12-06 10:34:25 -0800 | [diff] [blame] | 418 | } else if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 419 | fprintf(out, |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 420 | " event.AppendCharArray(arg%d.arg, " |
| 421 | "arg%d.arg_length);\n", |
Yao Chen | 1fe9f59 | 2018-12-06 10:34:25 -0800 | [diff] [blame] | 422 | argIndex, argIndex); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 423 | } else { |
| 424 | if (*arg == JAVA_TYPE_STRING) { |
| 425 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 426 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 427 | fprintf(out, " }\n"); |
| 428 | } |
| 429 | fprintf(out, " event << arg%d;\n", argIndex); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 430 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 431 | argIndex++; |
| 432 | } |
| 433 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 434 | fprintf(out, " return event.write(LOG_ID_STATS);\n"); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 435 | fprintf(out, " } else {\n"); |
| 436 | fprintf(out, " return 1;\n"); |
| 437 | fprintf(out, " }\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 438 | fprintf(out, "}\n"); |
| 439 | fprintf(out, "\n"); |
| 440 | } |
| 441 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 442 | for (auto signature_to_modules_it = atoms.signatures_to_modules.begin(); |
| 443 | signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) { |
| 444 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 445 | continue; |
| 446 | } |
| 447 | vector<java_type_t> signature = signature_to_modules_it->first; |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 448 | int argIndex; |
| 449 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 450 | fprintf(out, "int\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 451 | fprintf(out, "stats_write(int32_t code"); |
| 452 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 453 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 454 | arg != signature.end(); arg++) { |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 455 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 456 | for (auto chainField : attributionDecl.fields) { |
| 457 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 458 | fprintf(out, ", const std::vector<%s>& %s", |
| 459 | cpp_type_name(chainField.javaType), |
| 460 | chainField.name.c_str()); |
| 461 | } else { |
| 462 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 463 | cpp_type_name(chainField.javaType), |
| 464 | chainField.name.c_str(), chainField.name.c_str()); |
| 465 | } |
| 466 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 467 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Yao Chen | 1fe9f59 | 2018-12-06 10:34:25 -0800 | [diff] [blame] | 468 | fprintf(out, |
| 469 | ", const std::map<int, int32_t>& arg%d_1, " |
| 470 | "const std::map<int, int64_t>& arg%d_2, " |
| 471 | "const std::map<int, char const*>& arg%d_3, " |
| 472 | "const std::map<int, float>& arg%d_4", |
| 473 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 474 | } else { |
| 475 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 476 | } |
| 477 | argIndex++; |
| 478 | } |
| 479 | fprintf(out, ")\n"); |
| 480 | |
| 481 | fprintf(out, "{\n"); |
| 482 | fprintf(out, " int ret = 0;\n"); |
| 483 | |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 484 | fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 485 | fprintf(out, " ret = try_stats_write(code"); |
| 486 | |
| 487 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 488 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 489 | arg != signature.end(); arg++) { |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 490 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 491 | for (auto chainField : attributionDecl.fields) { |
| 492 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 493 | fprintf(out, ", %s", |
| 494 | chainField.name.c_str()); |
| 495 | } else { |
| 496 | fprintf(out, ", %s, %s_length", |
| 497 | chainField.name.c_str(), chainField.name.c_str()); |
| 498 | } |
| 499 | } |
Yao Chen | 1fe9f59 | 2018-12-06 10:34:25 -0800 | [diff] [blame] | 500 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 501 | fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex, |
| 502 | argIndex, argIndex, argIndex); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 503 | } else { |
| 504 | fprintf(out, ", arg%d", argIndex); |
| 505 | } |
| 506 | argIndex++; |
| 507 | } |
| 508 | fprintf(out, ");\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 509 | fprintf(out, " if (ret >= 0) { break; }\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 510 | |
| 511 | fprintf(out, " {\n"); |
| 512 | fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n"); |
| 513 | fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= " |
| 514 | "kMinRetryIntervalNs) break;\n"); |
| 515 | fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n"); |
| 516 | fprintf(out, " }\n"); |
| 517 | fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 518 | fprintf(out, " }\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 519 | fprintf(out, " if (ret < 0) {\n"); |
Yao Chen | 49d7dd7 | 2019-03-26 14:02:11 -0700 | [diff] [blame] | 520 | fprintf(out, " note_log_drop(ret, code);\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 521 | fprintf(out, " }\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 522 | fprintf(out, " return ret;\n"); |
| 523 | fprintf(out, "}\n"); |
| 524 | fprintf(out, "\n"); |
| 525 | } |
| 526 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 527 | for (auto signature_it = atoms.non_chained_signatures_to_modules.begin(); |
| 528 | signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) { |
| 529 | if (!signature_needed_for_module(signature_it->second, moduleName)) { |
| 530 | continue; |
| 531 | } |
| 532 | vector<java_type_t> signature = signature_it->first; |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 533 | int argIndex; |
| 534 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 535 | fprintf(out, "int\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 536 | fprintf(out, "try_stats_write_non_chained(int32_t code"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 537 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 538 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 539 | arg != signature.end(); arg++) { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 540 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 541 | argIndex++; |
| 542 | } |
| 543 | fprintf(out, ")\n"); |
| 544 | |
| 545 | fprintf(out, "{\n"); |
| 546 | argIndex = 1; |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 547 | fprintf(out, " if (kStatsdEnabled) {\n"); |
Yao Chen | f7bc6ab | 2018-04-18 13:45:48 -0700 | [diff] [blame] | 548 | fprintf(out, " stats_event_list event(kStatsEventTag);\n"); |
Yangster-mac | 31ddcde | 2018-02-14 16:09:35 -0800 | [diff] [blame] | 549 | fprintf(out, " event << android::elapsedRealtimeNano();\n\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 550 | fprintf(out, " event << code;\n\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 551 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 552 | arg != signature.end(); arg++) { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 553 | if (argIndex == 1) { |
| 554 | fprintf(out, " event.begin();\n\n"); |
| 555 | fprintf(out, " event.begin();\n"); |
| 556 | } |
| 557 | if (*arg == JAVA_TYPE_STRING) { |
| 558 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 559 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 560 | fprintf(out, " }\n"); |
| 561 | } |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 562 | if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 563 | fprintf(out, |
| 564 | " event.AppendCharArray(arg%d.arg, " |
| 565 | "arg%d.arg_length);", |
| 566 | argIndex, argIndex); |
| 567 | } else { |
| 568 | fprintf(out, " event << arg%d;\n", argIndex); |
| 569 | } |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 570 | if (argIndex == 2) { |
| 571 | fprintf(out, " event.end();\n\n"); |
| 572 | fprintf(out, " event.end();\n\n"); |
| 573 | } |
| 574 | argIndex++; |
| 575 | } |
| 576 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 577 | fprintf(out, " return event.write(LOG_ID_STATS);\n"); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 578 | fprintf(out, " } else {\n"); |
| 579 | fprintf(out, " return 1;\n"); |
| 580 | fprintf(out, " }\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 581 | fprintf(out, "}\n"); |
| 582 | fprintf(out, "\n"); |
| 583 | } |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 584 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 585 | for (auto signature_it = atoms.non_chained_signatures_to_modules.begin(); |
| 586 | signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) { |
| 587 | if (!signature_needed_for_module(signature_it->second, moduleName)) { |
| 588 | continue; |
| 589 | } |
| 590 | vector<java_type_t> signature = signature_it->first; |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 591 | int argIndex; |
| 592 | |
| 593 | fprintf(out, "int\n"); |
| 594 | fprintf(out, "stats_write_non_chained(int32_t code"); |
| 595 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 596 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 597 | arg != signature.end(); arg++) { |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 598 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 599 | argIndex++; |
| 600 | } |
| 601 | fprintf(out, ")\n"); |
| 602 | |
| 603 | fprintf(out, "{\n"); |
| 604 | |
| 605 | fprintf(out, " int ret = 0;\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 606 | fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 607 | fprintf(out, " ret = try_stats_write_non_chained(code"); |
| 608 | |
| 609 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 610 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 611 | arg != signature.end(); arg++) { |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 612 | fprintf(out, ", arg%d", argIndex); |
| 613 | argIndex++; |
| 614 | } |
| 615 | fprintf(out, ");\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 616 | fprintf(out, " if (ret >= 0) { break; }\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 617 | |
| 618 | fprintf(out, " {\n"); |
| 619 | fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n"); |
| 620 | fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= " |
| 621 | "kMinRetryIntervalNs) break;\n"); |
| 622 | fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n"); |
| 623 | fprintf(out, " }\n"); |
| 624 | |
| 625 | fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 626 | fprintf(out, " }\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 627 | fprintf(out, " if (ret < 0) {\n"); |
Yao Chen | 49d7dd7 | 2019-03-26 14:02:11 -0700 | [diff] [blame] | 628 | fprintf(out, " note_log_drop(ret, code);\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 629 | fprintf(out, " }\n"); |
| 630 | fprintf(out, " return ret;\n\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 631 | fprintf(out, "}\n"); |
| 632 | |
| 633 | fprintf(out, "\n"); |
| 634 | } |
| 635 | |
| 636 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 637 | // Print footer |
| 638 | fprintf(out, "\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 639 | write_closing_namespace(out, cppNamespace); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 644 | void build_non_chained_decl_map(const Atoms& atoms, |
| 645 | std::map<int, set<AtomDecl>::const_iterator>* decl_map){ |
| 646 | for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin(); |
| 647 | atom != atoms.non_chained_decls.end(); atom++) { |
| 648 | decl_map->insert(std::make_pair(atom->code, atom)); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | static void write_cpp_usage( |
| 653 | FILE* out, const string& method_name, const string& atom_code_name, |
| 654 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 655 | fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), |
| 656 | atom_code_name.c_str()); |
| 657 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 658 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 659 | field != atom.fields.end(); field++) { |
| 660 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 661 | for (auto chainField : attributionDecl.fields) { |
| 662 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 663 | fprintf(out, ", const std::vector<%s>& %s", |
| 664 | cpp_type_name(chainField.javaType), |
| 665 | chainField.name.c_str()); |
| 666 | } else { |
| 667 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 668 | cpp_type_name(chainField.javaType), |
| 669 | chainField.name.c_str(), chainField.name.c_str()); |
| 670 | } |
| 671 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 672 | } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 673 | fprintf(out, ", const std::map<int, int32_t>& %s_int" |
| 674 | ", const std::map<int, int64_t>& %s_long" |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 675 | ", const std::map<int, char const*>& %s_str" |
| 676 | ", const std::map<int, float>& %s_float", |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 677 | field->name.c_str(), |
| 678 | field->name.c_str(), |
| 679 | field->name.c_str(), |
| 680 | field->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 681 | } else { |
| 682 | fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str()); |
| 683 | } |
| 684 | } |
| 685 | fprintf(out, ");\n"); |
| 686 | } |
| 687 | |
| 688 | static void write_cpp_method_header( |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 689 | FILE* out, |
| 690 | const string& method_name, |
| 691 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 692 | const AtomDecl &attributionDecl, const string& moduleName) { |
| 693 | |
| 694 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 695 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 696 | // Skip if this signature is not needed for the module. |
| 697 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 698 | continue; |
| 699 | } |
| 700 | |
| 701 | vector<java_type_t> signature = signature_to_modules_it->first; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 702 | fprintf(out, "int %s(int32_t code", method_name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 703 | int argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 704 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 705 | arg != signature.end(); arg++) { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 706 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 707 | for (auto chainField : attributionDecl.fields) { |
| 708 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 709 | fprintf(out, ", const std::vector<%s>& %s", |
| 710 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 711 | } else { |
| 712 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 713 | cpp_type_name(chainField.javaType), |
| 714 | chainField.name.c_str(), chainField.name.c_str()); |
| 715 | } |
| 716 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 717 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 718 | fprintf(out, ", const std::map<int, int32_t>& arg%d_1, " |
| 719 | "const std::map<int, int64_t>& arg%d_2, " |
| 720 | "const std::map<int, char const*>& arg%d_3, " |
| 721 | "const std::map<int, float>& arg%d_4", |
| 722 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 723 | } else { |
| 724 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 725 | } |
| 726 | argIndex++; |
| 727 | } |
| 728 | fprintf(out, ");\n"); |
| 729 | |
| 730 | } |
| 731 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 732 | |
| 733 | static int |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 734 | write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl, |
| 735 | const string& moduleName, const string& cppNamespace) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 736 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 737 | // Print prelude |
| 738 | fprintf(out, "// This file is autogenerated\n"); |
| 739 | fprintf(out, "\n"); |
| 740 | fprintf(out, "#pragma once\n"); |
| 741 | fprintf(out, "\n"); |
| 742 | fprintf(out, "#include <stdint.h>\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 743 | fprintf(out, "#include <vector>\n"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 744 | fprintf(out, "#include <map>\n"); |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 745 | fprintf(out, "#include <set>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 746 | fprintf(out, "\n"); |
| 747 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 748 | write_namespace(out, cppNamespace); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 749 | fprintf(out, "\n"); |
| 750 | fprintf(out, "/*\n"); |
| 751 | fprintf(out, " * API For logging statistics events.\n"); |
| 752 | fprintf(out, " */\n"); |
| 753 | fprintf(out, "\n"); |
| 754 | fprintf(out, "/**\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 755 | fprintf(out, " * Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 756 | fprintf(out, " */\n"); |
| 757 | fprintf(out, "enum {\n"); |
| 758 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 759 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 760 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 761 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 762 | size_t i = 0; |
Muhammad Qureshi | dc132af | 2019-02-05 13:07:40 -0800 | [diff] [blame] | 763 | // Print atom constants |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 764 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 765 | atom != atoms.decls.end(); atom++) { |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 766 | // Skip if the atom is not needed for the module. |
| 767 | if (!atom_needed_for_module(*atom, moduleName)) { |
| 768 | continue; |
| 769 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 770 | string constant = make_constant_name(atom->name); |
| 771 | fprintf(out, "\n"); |
| 772 | fprintf(out, " /**\n"); |
| 773 | 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] | 774 | write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl); |
| 775 | |
| 776 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 777 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 778 | write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second, |
| 779 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 780 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 781 | fprintf(out, " */\n"); |
| 782 | char const* const comma = (i == atoms.decls.size() - 1) ? "" : ","; |
| 783 | fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 784 | if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) { |
| 785 | maxPushedAtomId = atom->code; |
| 786 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 787 | i++; |
| 788 | } |
| 789 | fprintf(out, "\n"); |
| 790 | fprintf(out, "};\n"); |
| 791 | fprintf(out, "\n"); |
| 792 | |
Muhammad Qureshi | dc132af | 2019-02-05 13:07:40 -0800 | [diff] [blame] | 793 | // Print constants for the enum values. |
| 794 | fprintf(out, "//\n"); |
| 795 | fprintf(out, "// Constants for enum values\n"); |
| 796 | fprintf(out, "//\n\n"); |
| 797 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 798 | atom != atoms.decls.end(); atom++) { |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 799 | // Skip if the atom is not needed for the module. |
| 800 | if (!atom_needed_for_module(*atom, moduleName)) { |
| 801 | continue; |
| 802 | } |
| 803 | |
Muhammad Qureshi | dc132af | 2019-02-05 13:07:40 -0800 | [diff] [blame] | 804 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 805 | field != atom->fields.end(); field++) { |
| 806 | if (field->javaType == JAVA_TYPE_ENUM) { |
| 807 | fprintf(out, "// Values for %s.%s\n", atom->message.c_str(), |
| 808 | field->name.c_str()); |
| 809 | for (map<int, string>::const_iterator value = field->enumValues.begin(); |
| 810 | value != field->enumValues.end(); value++) { |
| 811 | fprintf(out, "const int32_t %s__%s__%s = %d;\n", |
| 812 | make_constant_name(atom->message).c_str(), |
| 813 | make_constant_name(field->name).c_str(), |
| 814 | make_constant_name(value->second).c_str(), |
| 815 | value->first); |
| 816 | } |
| 817 | fprintf(out, "\n"); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 822 | fprintf(out, "struct BytesField {\n"); |
| 823 | fprintf(out, |
| 824 | " BytesField(char const* array, size_t len) : arg(array), " |
| 825 | "arg_length(len) {}\n"); |
| 826 | fprintf(out, " char const* arg;\n"); |
| 827 | fprintf(out, " size_t arg_length;\n"); |
| 828 | fprintf(out, "};\n"); |
| 829 | fprintf(out, "\n"); |
| 830 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 831 | // This metadata is only used by statsd, which uses the default libstatslog. |
| 832 | if (moduleName == DEFAULT_MODULE_NAME) { |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 833 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 834 | fprintf(out, "struct StateAtomFieldOptions {\n"); |
| 835 | fprintf(out, " std::vector<int> primaryFields;\n"); |
| 836 | fprintf(out, " int exclusiveField;\n"); |
| 837 | fprintf(out, "};\n"); |
| 838 | fprintf(out, "\n"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 839 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 840 | fprintf(out, "struct AtomsInfo {\n"); |
| 841 | fprintf(out, |
| 842 | " const static std::set<int> " |
| 843 | "kNotTruncatingTimestampAtomWhiteList;\n"); |
| 844 | fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n"); |
| 845 | fprintf(out, |
| 846 | " const static std::set<int> kAtomsWithAttributionChain;\n"); |
| 847 | fprintf(out, |
| 848 | " const static std::map<int, StateAtomFieldOptions> " |
| 849 | "kStateAtomsFieldOptions;\n"); |
| 850 | fprintf(out, |
| 851 | " const static std::map<int, std::vector<int>> " |
| 852 | "kBytesFieldAtoms;"); |
| 853 | fprintf(out, |
| 854 | " const static std::set<int> kWhitelistedAtoms;\n"); |
| 855 | fprintf(out, "};\n"); |
| 856 | |
| 857 | fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", |
| 858 | maxPushedAtomId); |
| 859 | } |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 860 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 861 | // Print write methods |
| 862 | fprintf(out, "//\n"); |
| 863 | fprintf(out, "// Write methods\n"); |
| 864 | fprintf(out, "//\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 865 | write_cpp_method_header(out, "stats_write", atoms.signatures_to_modules, attributionDecl, |
| 866 | moduleName); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 867 | |
| 868 | fprintf(out, "//\n"); |
| 869 | fprintf(out, "// Write flattened methods\n"); |
| 870 | fprintf(out, "//\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 871 | write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures_to_modules, |
| 872 | attributionDecl, moduleName); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 873 | |
| 874 | fprintf(out, "\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 875 | write_closing_namespace(out, cppNamespace); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 880 | static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name, |
| 881 | const AtomDecl& atom) { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 882 | fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s", |
| 883 | method_name.c_str(), atom_code_name.c_str()); |
| 884 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 885 | field != atom.fields.end(); field++) { |
| 886 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 887 | fprintf(out, ", android.os.WorkSource workSource"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 888 | } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 889 | fprintf(out, ", SparseArray<Object> value_map"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 890 | } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) { |
| 891 | fprintf(out, ", byte[] %s", field->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 892 | } else { |
| 893 | fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str()); |
| 894 | } |
| 895 | } |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 896 | fprintf(out, ");<br>\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static void write_java_method( |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 900 | FILE* out, |
| 901 | const string& method_name, |
| 902 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 903 | const AtomDecl &attributionDecl) { |
| 904 | |
| 905 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 906 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 907 | vector<java_type_t> signature = signature_to_modules_it->first; |
Tor Norbye | 0f2dc8d | 2019-01-08 12:07:15 -0800 | [diff] [blame] | 908 | fprintf(out, " /** @hide */\n"); |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 909 | fprintf(out, " public static native int %s(int code", method_name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 910 | int argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 911 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 912 | arg != signature.end(); arg++) { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 913 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 914 | for (auto chainField : attributionDecl.fields) { |
| 915 | fprintf(out, ", %s[] %s", |
| 916 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 917 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 918 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 919 | fprintf(out, ", SparseArray<Object> value_map"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 920 | } else { |
| 921 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 922 | } |
| 923 | argIndex++; |
| 924 | } |
| 925 | fprintf(out, ");\n"); |
| 926 | } |
| 927 | } |
| 928 | |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 929 | static void write_java_helpers_for_module( |
| 930 | FILE * out, |
| 931 | const AtomDecl &attributionDecl, |
| 932 | const int requiredHelpers) { |
| 933 | fprintf(out, " private static void copyInt(byte[] buff, int pos, int val) {\n"); |
| 934 | fprintf(out, " buff[pos] = (byte) (val);\n"); |
| 935 | fprintf(out, " buff[pos + 1] = (byte) (val >> 8);\n"); |
| 936 | fprintf(out, " buff[pos + 2] = (byte) (val >> 16);\n"); |
| 937 | fprintf(out, " buff[pos + 3] = (byte) (val >> 24);\n"); |
| 938 | fprintf(out, " return;\n"); |
| 939 | fprintf(out, " }\n"); |
| 940 | fprintf(out, "\n"); |
| 941 | |
| 942 | fprintf(out, " private static void copyLong(byte[] buff, int pos, long val) {\n"); |
| 943 | fprintf(out, " buff[pos] = (byte) (val);\n"); |
| 944 | fprintf(out, " buff[pos + 1] = (byte) (val >> 8);\n"); |
| 945 | fprintf(out, " buff[pos + 2] = (byte) (val >> 16);\n"); |
| 946 | fprintf(out, " buff[pos + 3] = (byte) (val >> 24);\n"); |
| 947 | fprintf(out, " buff[pos + 4] = (byte) (val >> 32);\n"); |
| 948 | fprintf(out, " buff[pos + 5] = (byte) (val >> 40);\n"); |
| 949 | fprintf(out, " buff[pos + 6] = (byte) (val >> 48);\n"); |
| 950 | fprintf(out, " buff[pos + 7] = (byte) (val >> 56);\n"); |
| 951 | fprintf(out, " return;\n"); |
| 952 | fprintf(out, " }\n"); |
| 953 | fprintf(out, "\n"); |
| 954 | |
| 955 | if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) { |
| 956 | fprintf(out, " private static void copyFloat(byte[] buff, int pos, float val) {\n"); |
| 957 | fprintf(out, " copyInt(buff, pos, Float.floatToIntBits(val));\n"); |
| 958 | fprintf(out, " return;\n"); |
| 959 | fprintf(out, " }\n"); |
| 960 | fprintf(out, "\n"); |
| 961 | } |
| 962 | |
| 963 | if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) { |
| 964 | fprintf(out, " private static void writeAttributionChain(byte[] buff, int pos"); |
| 965 | for (auto chainField : attributionDecl.fields) { |
| 966 | fprintf(out, ", %s[] %s", |
| 967 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 968 | } |
| 969 | fprintf(out, ") {\n"); |
| 970 | |
| 971 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 972 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 973 | |
| 974 | // Write the first list begin. |
| 975 | fprintf(out, " buff[pos] = LIST_TYPE;\n"); |
| 976 | fprintf(out, " buff[pos + 1] = (byte) (%s.length);\n", tagName); |
| 977 | fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n"); |
| 978 | |
| 979 | // Iterate through the attribution chain and write the nodes. |
| 980 | fprintf(out, " for (int i = 0; i < %s.length; i++) {\n", tagName); |
| 981 | // Write the list begin. |
| 982 | fprintf(out, " buff[pos] = LIST_TYPE;\n"); |
| 983 | fprintf(out, " buff[pos + 1] = %lu;\n", attributionDecl.fields.size()); |
| 984 | fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n"); |
| 985 | |
| 986 | // Write the uid. |
| 987 | fprintf(out, " buff[pos] = INT_TYPE;\n"); |
| 988 | fprintf(out, " copyInt(buff, pos + 1, %s[i]);\n", uidName); |
| 989 | fprintf(out, " pos += INT_TYPE_SIZE;\n"); |
| 990 | |
| 991 | // Write the tag. |
| 992 | fprintf(out, " String %sStr = (%s[i] == null) ? \"\" : %s[i];\n", |
| 993 | tagName, tagName, tagName); |
| 994 | fprintf(out, " byte[] %sByte = %sStr.getBytes(UTF_8);\n", tagName, tagName); |
| 995 | fprintf(out, " buff[pos] = STRING_TYPE;\n"); |
| 996 | fprintf(out, " copyInt(buff, pos + 1, %sByte.length);\n", tagName); |
| 997 | fprintf(out, " System.arraycopy(" |
| 998 | "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n", |
| 999 | tagName, tagName); |
| 1000 | fprintf(out, " pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", tagName); |
| 1001 | fprintf(out, " }\n"); |
| 1002 | fprintf(out, " }\n"); |
| 1003 | fprintf(out, "\n"); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | static int write_java_non_chained_method_for_module( |
| 1009 | FILE* out, |
| 1010 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 1011 | const string& moduleName |
| 1012 | ) { |
| 1013 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 1014 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 1015 | // Skip if this signature is not needed for the module. |
| 1016 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 1017 | continue; |
| 1018 | } |
| 1019 | |
| 1020 | // Print method signature. |
| 1021 | vector<java_type_t> signature = signature_to_modules_it->first; |
| 1022 | fprintf(out, " public static void write_non_chained(int code"); |
| 1023 | int argIndex = 1; |
| 1024 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1025 | arg != signature.end(); arg++) { |
| 1026 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1027 | // Non chained signatures should not have attribution chains. |
| 1028 | return 1; |
| 1029 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1030 | // Module logging does not yet support key value pair. |
| 1031 | return 1; |
| 1032 | } else { |
| 1033 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 1034 | } |
| 1035 | argIndex++; |
| 1036 | } |
| 1037 | fprintf(out, ") {\n"); |
| 1038 | |
| 1039 | fprintf(out, " write(code"); |
| 1040 | argIndex = 1; |
| 1041 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1042 | arg != signature.end(); arg++) { |
| 1043 | // First two args are uid and tag of attribution chain. |
| 1044 | if (argIndex == 1) { |
| 1045 | fprintf(out, ", new int[] {arg%d}", argIndex); |
| 1046 | } else if (argIndex == 2) { |
| 1047 | fprintf(out, ", new java.lang.String[] {arg%d}", argIndex); |
| 1048 | } else { |
| 1049 | fprintf(out, ", arg%d", argIndex); |
| 1050 | } |
| 1051 | argIndex++; |
| 1052 | } |
| 1053 | fprintf(out, ");\n"); |
| 1054 | fprintf(out, " }\n"); |
| 1055 | fprintf(out, "\n"); |
| 1056 | } |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | static int write_java_method_for_module( |
| 1061 | FILE* out, |
| 1062 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 1063 | const AtomDecl &attributionDecl, |
| 1064 | const string& moduleName, |
| 1065 | int* requiredHelpers |
| 1066 | ) { |
| 1067 | |
| 1068 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 1069 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 1070 | // Skip if this signature is not needed for the module. |
| 1071 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 1072 | continue; |
| 1073 | } |
| 1074 | |
| 1075 | // Print method signature. |
| 1076 | vector<java_type_t> signature = signature_to_modules_it->first; |
| 1077 | fprintf(out, " public static void write(int code"); |
| 1078 | int argIndex = 1; |
| 1079 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1080 | arg != signature.end(); arg++) { |
| 1081 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1082 | for (auto chainField : attributionDecl.fields) { |
| 1083 | fprintf(out, ", %s[] %s", |
| 1084 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 1085 | } |
| 1086 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1087 | // Module logging does not yet support key value pair. |
| 1088 | return 1; |
| 1089 | } else { |
| 1090 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 1091 | } |
| 1092 | argIndex++; |
| 1093 | } |
| 1094 | fprintf(out, ") {\n"); |
| 1095 | |
| 1096 | // Calculate the size of the buffer. |
| 1097 | fprintf(out, " // Initial overhead of the list, timestamp, and atom tag.\n"); |
| 1098 | fprintf(out, " int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + INT_TYPE_SIZE;\n"); |
| 1099 | argIndex = 1; |
| 1100 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1101 | arg != signature.end(); arg++) { |
| 1102 | switch (*arg) { |
| 1103 | case JAVA_TYPE_BOOLEAN: |
| 1104 | case JAVA_TYPE_INT: |
| 1105 | case JAVA_TYPE_FLOAT: |
| 1106 | case JAVA_TYPE_ENUM: |
| 1107 | fprintf(out, " needed += INT_TYPE_SIZE;\n"); |
| 1108 | break; |
| 1109 | case JAVA_TYPE_LONG: |
| 1110 | // Longs take 9 bytes, 1 for the type and 8 for the value. |
| 1111 | fprintf(out, " needed += LONG_TYPE_SIZE;\n"); |
| 1112 | break; |
| 1113 | case JAVA_TYPE_STRING: |
| 1114 | // Strings take 5 metadata bytes + length of byte encoded string. |
| 1115 | fprintf(out, " if (arg%d == null) {\n", argIndex); |
| 1116 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 1117 | fprintf(out, " }\n"); |
| 1118 | fprintf(out, " byte[] arg%dBytes= arg%d.getBytes(UTF_8);\n", |
| 1119 | argIndex, argIndex); |
| 1120 | fprintf(out, " needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n", |
| 1121 | argIndex); |
| 1122 | break; |
| 1123 | case JAVA_TYPE_BYTE_ARRAY: |
| 1124 | // Byte arrays take 5 metadata bytes + length of byte array. |
| 1125 | fprintf(out, " if (arg%d == null) {\n", argIndex); |
| 1126 | fprintf(out, " arg%d = new byte[0];\n", argIndex); |
| 1127 | fprintf(out, " }\n"); |
| 1128 | fprintf(out, " needed += STRING_TYPE_OVERHEAD + arg%d.length;\n", argIndex); |
| 1129 | break; |
| 1130 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 1131 | { |
| 1132 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 1133 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 1134 | // Null checks on the params. |
| 1135 | fprintf(out, " if (%s == null) {\n", uidName); |
| 1136 | fprintf(out, " %s = new %s[0];\n", uidName, |
| 1137 | java_type_name(attributionDecl.fields.front().javaType)); |
| 1138 | fprintf(out, " }\n"); |
| 1139 | fprintf(out, " if (%s == null) {\n", tagName); |
| 1140 | fprintf(out, " %s = new %s[0];\n", tagName, |
| 1141 | java_type_name(attributionDecl.fields.back().javaType)); |
| 1142 | fprintf(out, " }\n"); |
| 1143 | |
| 1144 | // First check that the lengths of the uid and tag arrays are the same. |
| 1145 | fprintf(out, " if (%s.length != %s.length) {\n", uidName, tagName); |
| 1146 | fprintf(out, " return;\n"); |
| 1147 | fprintf(out, " }\n"); |
| 1148 | fprintf(out, " int attrSize = LIST_TYPE_OVERHEAD;\n"); |
| 1149 | fprintf(out, " for (int i = 0; i < %s.length; i++) {\n", tagName); |
| 1150 | fprintf(out, " String str%d = (%s[i] == null) ? \"\" : %s[i];\n", |
| 1151 | argIndex, tagName, tagName); |
| 1152 | fprintf(out, " int str%dlen = str%d.getBytes(UTF_8).length;\n", |
| 1153 | argIndex, argIndex); |
| 1154 | fprintf(out, |
| 1155 | " attrSize += " |
| 1156 | "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + str%dlen;\n", |
| 1157 | argIndex); |
| 1158 | fprintf(out, " }\n"); |
| 1159 | fprintf(out, " needed += attrSize;\n"); |
| 1160 | break; |
| 1161 | } |
| 1162 | default: |
| 1163 | // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR. |
| 1164 | return 1; |
| 1165 | } |
| 1166 | argIndex++; |
| 1167 | } |
| 1168 | |
| 1169 | // Now we have the size that is needed. Check for overflow and return if needed. |
| 1170 | fprintf(out, " if (needed > MAX_EVENT_PAYLOAD) {\n"); |
| 1171 | fprintf(out, " return;\n"); |
| 1172 | fprintf(out, " }\n"); |
| 1173 | |
| 1174 | // Create new buffer, and associated data types. |
| 1175 | fprintf(out, " byte[] buff = new byte[needed];\n"); |
| 1176 | fprintf(out, " int pos = 0;\n"); |
| 1177 | |
| 1178 | // Initialize the buffer with list data type. |
| 1179 | fprintf(out, " buff[pos] = LIST_TYPE;\n"); |
| 1180 | fprintf(out, " buff[pos + 1] = %lu;\n", signature.size() + 2); |
| 1181 | fprintf(out, " pos += LIST_TYPE_OVERHEAD;\n"); |
| 1182 | |
| 1183 | // Write timestamp. |
| 1184 | fprintf(out, " long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n"); |
| 1185 | fprintf(out, " buff[pos] = LONG_TYPE;\n"); |
| 1186 | fprintf(out, " copyLong(buff, pos + 1, elapsedRealtime);\n"); |
| 1187 | fprintf(out, " pos += LONG_TYPE_SIZE;\n"); |
| 1188 | |
| 1189 | // Write atom code. |
| 1190 | fprintf(out, " buff[pos] = INT_TYPE;\n"); |
| 1191 | fprintf(out, " copyInt(buff, pos + 1, code);\n"); |
| 1192 | fprintf(out, " pos += INT_TYPE_SIZE;\n"); |
| 1193 | |
| 1194 | // Write the args. |
| 1195 | argIndex = 1; |
| 1196 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1197 | arg != signature.end(); arg++) { |
| 1198 | switch (*arg) { |
| 1199 | case JAVA_TYPE_BOOLEAN: |
| 1200 | fprintf(out, " buff[pos] = INT_TYPE;\n"); |
| 1201 | fprintf(out, " copyInt(buff, pos + 1, arg%d? 1 : 0);\n", argIndex); |
| 1202 | fprintf(out, " pos += INT_TYPE_SIZE;\n"); |
| 1203 | break; |
| 1204 | case JAVA_TYPE_INT: |
| 1205 | case JAVA_TYPE_ENUM: |
| 1206 | fprintf(out, " buff[pos] = INT_TYPE;\n"); |
| 1207 | fprintf(out, " copyInt(buff, pos + 1, arg%d);\n", argIndex); |
| 1208 | fprintf(out, " pos += INT_TYPE_SIZE;\n"); |
| 1209 | break; |
| 1210 | case JAVA_TYPE_FLOAT: |
| 1211 | *requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT; |
| 1212 | fprintf(out, " buff[pos] = FLOAT_TYPE;\n"); |
| 1213 | fprintf(out, " copyFloat(buff, pos + 1, arg%d);\n", argIndex); |
| 1214 | fprintf(out, " pos += FLOAT_TYPE_SIZE;\n"); |
| 1215 | break; |
| 1216 | case JAVA_TYPE_LONG: |
| 1217 | fprintf(out, " buff[pos] = LONG_TYPE;\n"); |
| 1218 | fprintf(out, " copyLong(buff, pos + 1, arg%d);\n", argIndex); |
| 1219 | fprintf(out, " pos += LONG_TYPE_SIZE;\n"); |
| 1220 | break; |
| 1221 | case JAVA_TYPE_STRING: |
| 1222 | fprintf(out, " buff[pos] = STRING_TYPE;\n"); |
| 1223 | fprintf(out, " copyInt(buff, pos + 1, arg%dBytes.length);\n", argIndex); |
| 1224 | fprintf(out, " System.arraycopy(" |
| 1225 | "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%dBytes.length);\n", |
| 1226 | argIndex, argIndex); |
| 1227 | fprintf(out, " pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n", |
| 1228 | argIndex); |
| 1229 | break; |
| 1230 | case JAVA_TYPE_BYTE_ARRAY: |
| 1231 | fprintf(out, " buff[pos] = STRING_TYPE;\n"); |
| 1232 | fprintf(out, " copyInt(buff, pos + 1, arg%d.length);\n", argIndex); |
| 1233 | fprintf(out, " System.arraycopy(" |
| 1234 | "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n", |
| 1235 | argIndex, argIndex); |
| 1236 | fprintf(out, " pos += STRING_TYPE_OVERHEAD + arg%d.length;\n", argIndex); |
| 1237 | break; |
| 1238 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 1239 | { |
| 1240 | *requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION; |
| 1241 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 1242 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 1243 | |
| 1244 | fprintf(out, " writeAttributionChain(buff, pos, %s, %s);\n", |
| 1245 | uidName, tagName); |
| 1246 | fprintf(out, " pos += attrSize;\n"); |
| 1247 | break; |
| 1248 | } |
| 1249 | default: |
| 1250 | // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR. |
| 1251 | return 1; |
| 1252 | } |
| 1253 | argIndex++; |
| 1254 | } |
| 1255 | |
| 1256 | fprintf(out, " StatsLog.writeRaw(buff, pos);\n"); |
| 1257 | fprintf(out, " }\n"); |
| 1258 | fprintf(out, "\n"); |
| 1259 | } |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1263 | static void write_java_work_source_method(FILE* out, |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1264 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 1265 | const string& moduleName) { |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1266 | fprintf(out, "\n // WorkSource methods.\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1267 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 1268 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1269 | // Skip if this signature is not needed for the module. |
| 1270 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 1271 | continue; |
| 1272 | } |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1273 | vector<java_type_t> signature = signature_to_modules_it->first; |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1274 | // Determine if there is Attribution in this signature. |
| 1275 | int attributionArg = -1; |
| 1276 | int argIndexMax = 0; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1277 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1278 | arg != signature.end(); arg++) { |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1279 | argIndexMax++; |
| 1280 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1281 | if (attributionArg > -1) { |
| 1282 | fprintf(stderr, "An atom contains multiple AttributionNode fields.\n"); |
| 1283 | fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n"); |
| 1284 | fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n"); |
| 1285 | return; |
| 1286 | } |
| 1287 | attributionArg = argIndexMax; |
| 1288 | } |
| 1289 | } |
| 1290 | if (attributionArg < 0) { |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
| 1294 | // Method header (signature) |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1295 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 1296 | fprintf(out, " /** @hide */\n"); |
| 1297 | } |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1298 | fprintf(out, " public static void write(int code"); |
| 1299 | int argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1300 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1301 | arg != signature.end(); arg++) { |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1302 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1303 | fprintf(out, ", WorkSource ws"); |
| 1304 | } else { |
| 1305 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 1306 | } |
| 1307 | argIndex++; |
| 1308 | } |
| 1309 | fprintf(out, ") {\n"); |
| 1310 | |
| 1311 | // write_non_chained() component. TODO: Remove when flat uids are no longer needed. |
| 1312 | fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n"); |
| 1313 | fprintf(out, " write_non_chained(code"); |
| 1314 | for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) { |
| 1315 | if (argIndex == attributionArg) { |
| 1316 | fprintf(out, ", ws.get(i), ws.getName(i)"); |
| 1317 | } else { |
| 1318 | fprintf(out, ", arg%d", argIndex); |
| 1319 | } |
| 1320 | } |
| 1321 | fprintf(out, ");\n"); |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1322 | fprintf(out, " }\n"); // close for-loop |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1323 | |
| 1324 | // write() component. |
| 1325 | fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n"); |
| 1326 | fprintf(out, " if (workChains != null) {\n"); |
| 1327 | fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n"); |
| 1328 | fprintf(out, " write(code"); |
| 1329 | for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) { |
| 1330 | if (argIndex == attributionArg) { |
| 1331 | fprintf(out, ", wc.getUids(), wc.getTags()"); |
| 1332 | } else { |
| 1333 | fprintf(out, ", arg%d", argIndex); |
| 1334 | } |
| 1335 | } |
| 1336 | fprintf(out, ");\n"); |
| 1337 | fprintf(out, " }\n"); // close for-loop |
| 1338 | fprintf(out, " }\n"); // close if |
| 1339 | fprintf(out, " }\n"); // close method |
| 1340 | } |
| 1341 | } |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1342 | |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1343 | static void write_java_atom_codes(FILE* out, const Atoms& atoms, const string& moduleName) { |
| 1344 | fprintf(out, " // Constants for atom codes.\n"); |
| 1345 | |
| 1346 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 1347 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 1348 | |
| 1349 | // Print constants for the atom codes. |
| 1350 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 1351 | atom != atoms.decls.end(); atom++) { |
| 1352 | // Skip if the atom is not needed for the module. |
| 1353 | if (!atom_needed_for_module(*atom, moduleName)) { |
| 1354 | continue; |
| 1355 | } |
| 1356 | string constant = make_constant_name(atom->name); |
| 1357 | fprintf(out, "\n"); |
| 1358 | fprintf(out, " /**\n"); |
| 1359 | fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str()); |
| 1360 | write_java_usage(out, "write", constant, *atom); |
| 1361 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 1362 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 1363 | write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second); |
| 1364 | } |
| 1365 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 1366 | fprintf(out, " * @hide\n"); |
| 1367 | } |
| 1368 | fprintf(out, " */\n"); |
| 1369 | fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code); |
| 1370 | } |
| 1371 | fprintf(out, "\n"); |
| 1372 | } |
| 1373 | |
| 1374 | static void write_java_enum_values(FILE* out, const Atoms& atoms, const string& moduleName) { |
| 1375 | fprintf(out, " // Constants for enum values.\n\n"); |
| 1376 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 1377 | atom != atoms.decls.end(); atom++) { |
| 1378 | // Skip if the atom is not needed for the module. |
| 1379 | if (!atom_needed_for_module(*atom, moduleName)) { |
| 1380 | continue; |
| 1381 | } |
| 1382 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 1383 | field != atom->fields.end(); field++) { |
| 1384 | if (field->javaType == JAVA_TYPE_ENUM) { |
| 1385 | fprintf(out, " // Values for %s.%s\n", atom->message.c_str(), |
| 1386 | field->name.c_str()); |
| 1387 | for (map<int, string>::const_iterator value = field->enumValues.begin(); |
| 1388 | value != field->enumValues.end(); value++) { |
| 1389 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 1390 | fprintf(out, " /** @hide */\n"); |
| 1391 | } |
| 1392 | fprintf(out, " public static final int %s__%s__%s = %d;\n", |
| 1393 | make_constant_name(atom->message).c_str(), |
| 1394 | make_constant_name(field->name).c_str(), |
| 1395 | make_constant_name(value->second).c_str(), |
| 1396 | value->first); |
| 1397 | } |
| 1398 | fprintf(out, "\n"); |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1404 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1405 | write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1406 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1407 | // Print prelude |
| 1408 | fprintf(out, "// This file is autogenerated\n"); |
| 1409 | fprintf(out, "\n"); |
| 1410 | fprintf(out, "package android.util;\n"); |
| 1411 | fprintf(out, "\n"); |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1412 | fprintf(out, "import android.os.WorkSource;\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1413 | fprintf(out, "import android.util.SparseArray;\n"); |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 1414 | fprintf(out, "import java.util.ArrayList;\n"); |
| 1415 | fprintf(out, "\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1416 | fprintf(out, "\n"); |
| 1417 | fprintf(out, "/**\n"); |
| 1418 | fprintf(out, " * API For logging statistics events.\n"); |
| 1419 | fprintf(out, " * @hide\n"); |
| 1420 | fprintf(out, " */\n"); |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 1421 | fprintf(out, "public class StatsLogInternal {\n"); |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1422 | write_java_atom_codes(out, atoms, DEFAULT_MODULE_NAME); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1423 | |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1424 | write_java_enum_values(out, atoms, DEFAULT_MODULE_NAME); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 1425 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1426 | // Print write methods |
| 1427 | fprintf(out, " // Write methods\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1428 | write_java_method(out, "write", atoms.signatures_to_modules, attributionDecl); |
| 1429 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures_to_modules, |
| 1430 | attributionDecl); |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1431 | write_java_work_source_method(out, atoms.signatures_to_modules, DEFAULT_MODULE_NAME); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1432 | |
| 1433 | fprintf(out, "}\n"); |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1438 | // TODO: Merge this with write_stats_log_java so that we can get rid of StatsLogInternal JNI. |
| 1439 | static int |
| 1440 | write_stats_log_java_for_module(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl, |
| 1441 | const string& moduleName, const string& javaClass, const string& javaPackage) |
| 1442 | { |
| 1443 | // Print prelude |
| 1444 | fprintf(out, "// This file is autogenerated\n"); |
| 1445 | fprintf(out, "\n"); |
| 1446 | fprintf(out, "package %s;\n", javaPackage.c_str()); |
| 1447 | fprintf(out, "\n"); |
| 1448 | fprintf(out, "import static java.nio.charset.StandardCharsets.UTF_8;\n"); |
| 1449 | fprintf(out, "\n"); |
| 1450 | fprintf(out, "import android.util.StatsLog;\n"); |
| 1451 | fprintf(out, "import android.os.SystemClock;\n"); |
| 1452 | fprintf(out, "\n"); |
| 1453 | fprintf(out, "import java.util.ArrayList;\n"); |
| 1454 | fprintf(out, "\n"); |
| 1455 | fprintf(out, "\n"); |
| 1456 | fprintf(out, "/**\n"); |
| 1457 | fprintf(out, " * Utility class for logging statistics events.\n"); |
| 1458 | fprintf(out, " */\n"); |
| 1459 | fprintf(out, "public class %s {\n", javaClass.c_str()); |
| 1460 | |
| 1461 | // TODO: ideally these match with the native values (and automatically change if they change). |
| 1462 | fprintf(out, " private static final int LOGGER_ENTRY_MAX_PAYLOAD = 4068;\n"); |
| 1463 | fprintf(out, |
| 1464 | " private static final int MAX_EVENT_PAYLOAD = LOGGER_ENTRY_MAX_PAYLOAD - 4;\n"); |
| 1465 | // Value types. Must match with EventLog.java and log.h. |
| 1466 | fprintf(out, " private static final byte INT_TYPE = 0;\n"); |
| 1467 | fprintf(out, " private static final byte LONG_TYPE = 1;\n"); |
| 1468 | fprintf(out, " private static final byte STRING_TYPE = 2;\n"); |
| 1469 | fprintf(out, " private static final byte LIST_TYPE = 3;\n"); |
| 1470 | fprintf(out, " private static final byte FLOAT_TYPE = 4;\n"); |
| 1471 | |
| 1472 | // Size of each value type. |
| 1473 | // Booleans, ints, floats, and enums take 5 bytes, 1 for the type and 4 for the value. |
| 1474 | fprintf(out, " private static final int INT_TYPE_SIZE = 5;\n"); |
| 1475 | fprintf(out, " private static final int FLOAT_TYPE_SIZE = 5;\n"); |
| 1476 | // Longs take 9 bytes, 1 for the type and 8 for the value. |
| 1477 | fprintf(out, " private static final int LONG_TYPE_SIZE = 9;\n"); |
| 1478 | // Strings take 5 metadata bytes: 1 byte is for the type, 4 are for the length. |
| 1479 | fprintf(out, " private static final int STRING_TYPE_OVERHEAD = 5;\n"); |
| 1480 | fprintf(out, " private static final int LIST_TYPE_OVERHEAD = 2;\n"); |
| 1481 | |
| 1482 | write_java_atom_codes(out, atoms, moduleName); |
| 1483 | |
| 1484 | write_java_enum_values(out, atoms, moduleName); |
| 1485 | |
| 1486 | int errors = 0; |
| 1487 | int requiredHelpers = 0; |
| 1488 | // Print write methods |
| 1489 | fprintf(out, " // Write methods\n"); |
| 1490 | errors += write_java_method_for_module(out, atoms.signatures_to_modules, attributionDecl, |
| 1491 | moduleName, &requiredHelpers); |
| 1492 | errors += write_java_non_chained_method_for_module(out, atoms.non_chained_signatures_to_modules, |
| 1493 | moduleName); |
| 1494 | |
| 1495 | fprintf(out, " // Helper methods for copying primitives\n"); |
| 1496 | write_java_helpers_for_module(out, attributionDecl, requiredHelpers); |
| 1497 | |
| 1498 | fprintf(out, "}\n"); |
| 1499 | |
| 1500 | return errors; |
| 1501 | } |
| 1502 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1503 | static const char* |
| 1504 | jni_type_name(java_type_t type) |
| 1505 | { |
| 1506 | switch (type) { |
| 1507 | case JAVA_TYPE_BOOLEAN: |
| 1508 | return "jboolean"; |
| 1509 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 1510 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1511 | return "jint"; |
| 1512 | case JAVA_TYPE_LONG: |
| 1513 | return "jlong"; |
| 1514 | case JAVA_TYPE_FLOAT: |
| 1515 | return "jfloat"; |
| 1516 | case JAVA_TYPE_DOUBLE: |
| 1517 | return "jdouble"; |
| 1518 | case JAVA_TYPE_STRING: |
| 1519 | return "jstring"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1520 | case JAVA_TYPE_BYTE_ARRAY: |
| 1521 | return "jbyteArray"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1522 | default: |
| 1523 | return "UNKNOWN"; |
| 1524 | } |
| 1525 | } |
| 1526 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1527 | static const char* |
| 1528 | jni_array_type_name(java_type_t type) |
| 1529 | { |
| 1530 | switch (type) { |
| 1531 | case JAVA_TYPE_INT: |
| 1532 | return "jintArray"; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1533 | case JAVA_TYPE_FLOAT: |
| 1534 | return "jfloatArray"; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1535 | case JAVA_TYPE_STRING: |
| 1536 | return "jobjectArray"; |
| 1537 | default: |
| 1538 | return "UNKNOWN"; |
| 1539 | } |
| 1540 | } |
| 1541 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1542 | static string |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1543 | 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] | 1544 | { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1545 | string result("StatsLog_" + method_name); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1546 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1547 | arg != signature.end(); arg++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1548 | switch (*arg) { |
| 1549 | case JAVA_TYPE_BOOLEAN: |
| 1550 | result += "_boolean"; |
| 1551 | break; |
| 1552 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 1553 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1554 | result += "_int"; |
| 1555 | break; |
| 1556 | case JAVA_TYPE_LONG: |
| 1557 | result += "_long"; |
| 1558 | break; |
| 1559 | case JAVA_TYPE_FLOAT: |
| 1560 | result += "_float"; |
| 1561 | break; |
| 1562 | case JAVA_TYPE_DOUBLE: |
| 1563 | result += "_double"; |
| 1564 | break; |
| 1565 | case JAVA_TYPE_STRING: |
| 1566 | result += "_String"; |
| 1567 | break; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1568 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 1569 | result += "_AttributionChain"; |
| 1570 | break; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1571 | case JAVA_TYPE_KEY_VALUE_PAIR: |
| 1572 | result += "_KeyValuePairs"; |
| 1573 | break; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1574 | case JAVA_TYPE_BYTE_ARRAY: |
| 1575 | result += "_bytes"; |
| 1576 | break; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1577 | default: |
| 1578 | result += "_UNKNOWN"; |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | return result; |
| 1583 | } |
| 1584 | |
| 1585 | static const char* |
| 1586 | java_type_signature(java_type_t type) |
| 1587 | { |
| 1588 | switch (type) { |
| 1589 | case JAVA_TYPE_BOOLEAN: |
| 1590 | return "Z"; |
| 1591 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 1592 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1593 | return "I"; |
| 1594 | case JAVA_TYPE_LONG: |
| 1595 | return "J"; |
| 1596 | case JAVA_TYPE_FLOAT: |
| 1597 | return "F"; |
| 1598 | case JAVA_TYPE_DOUBLE: |
| 1599 | return "D"; |
| 1600 | case JAVA_TYPE_STRING: |
| 1601 | return "Ljava/lang/String;"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1602 | case JAVA_TYPE_BYTE_ARRAY: |
| 1603 | return "[B"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1604 | default: |
| 1605 | return "UNKNOWN"; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | static string |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1610 | jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1611 | { |
| 1612 | string result("(I"); |
| 1613 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1614 | arg != signature.end(); arg++) { |
| 1615 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1616 | for (auto chainField : attributionDecl.fields) { |
| 1617 | result += "["; |
| 1618 | result += java_type_signature(chainField.javaType); |
| 1619 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1620 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1621 | result += "Landroid/util/SparseArray;"; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1622 | } else { |
| 1623 | result += java_type_signature(*arg); |
| 1624 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1625 | } |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1626 | result += ")I"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1627 | return result; |
| 1628 | } |
| 1629 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1630 | static void write_key_value_map_jni(FILE* out) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1631 | fprintf(out, " std::map<int, int32_t> int32_t_map;\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1632 | fprintf(out, " std::map<int, int64_t> int64_t_map;\n"); |
| 1633 | fprintf(out, " std::map<int, float> float_map;\n"); |
| 1634 | fprintf(out, " std::map<int, char const*> string_map;\n\n"); |
| 1635 | |
| 1636 | fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n"); |
| 1637 | |
| 1638 | fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n"); |
| 1639 | fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n"); |
| 1640 | fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n"); |
| 1641 | |
| 1642 | |
| 1643 | fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n"); |
| 1644 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1645 | fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1646 | fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n"); |
| 1647 | fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n"); |
| 1648 | fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n"); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1649 | fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1650 | fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n"); |
| 1651 | fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n"); |
| 1652 | |
| 1653 | fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n"); |
| 1654 | fprintf(out, " for(int i = 0; i < jsize; i++) {\n"); |
| 1655 | fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n"); |
| 1656 | fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n"); |
| 1657 | fprintf(out, " if (jvalue_obj == NULL) { continue; }\n"); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1658 | fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n"); |
| 1659 | fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n"); |
| 1660 | fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1661 | fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n"); |
| 1662 | fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n"); |
| 1663 | fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n"); |
| 1664 | fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n"); |
| 1665 | fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n"); |
| 1666 | fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n"); |
| 1667 | fprintf(out, " scoped_ufs.push_back(std::move(utf));\n"); |
| 1668 | fprintf(out, " }\n"); |
| 1669 | fprintf(out, " }\n"); |
| 1670 | } |
| 1671 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1672 | static int |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1673 | write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name, |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1674 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 1675 | const AtomDecl &attributionDecl) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1676 | // Print write methods |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1677 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 1678 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 1679 | vector<java_type_t> signature = signature_to_modules_it->first; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1680 | int argIndex; |
| 1681 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1682 | fprintf(out, "static int\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1683 | fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code", |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1684 | jni_function_name(java_method_name, signature).c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1685 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1686 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1687 | arg != signature.end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1688 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1689 | for (auto chainField : attributionDecl.fields) { |
| 1690 | fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType), |
| 1691 | chainField.name.c_str()); |
| 1692 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1693 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1694 | fprintf(out, ", jobject value_map"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1695 | } else { |
| 1696 | fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex); |
| 1697 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1698 | argIndex++; |
| 1699 | } |
| 1700 | fprintf(out, ")\n"); |
| 1701 | |
| 1702 | fprintf(out, "{\n"); |
| 1703 | |
| 1704 | // Prepare strings |
| 1705 | argIndex = 1; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1706 | bool hadStringOrChain = false; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1707 | bool isKeyValuePairAtom = false; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1708 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1709 | arg != signature.end(); arg++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1710 | if (*arg == JAVA_TYPE_STRING) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1711 | hadStringOrChain = true; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1712 | fprintf(out, " const char* str%d;\n", argIndex); |
| 1713 | fprintf(out, " if (arg%d != NULL) {\n", argIndex); |
| 1714 | fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n", |
| 1715 | argIndex, argIndex); |
| 1716 | fprintf(out, " } else {\n"); |
| 1717 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 1718 | fprintf(out, " }\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1719 | } else if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 1720 | hadStringOrChain = true; |
| 1721 | fprintf(out, " jbyte* jbyte_array%d;\n", argIndex); |
| 1722 | fprintf(out, " const char* str%d;\n", argIndex); |
Yao Chen | 1fe9f59 | 2018-12-06 10:34:25 -0800 | [diff] [blame] | 1723 | fprintf(out, " int str%d_length = 0;\n", argIndex); |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame] | 1724 | fprintf(out, |
| 1725 | " if (arg%d != NULL && env->GetArrayLength(arg%d) > " |
| 1726 | "0) {\n", |
| 1727 | argIndex, argIndex); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1728 | fprintf(out, |
| 1729 | " jbyte_array%d = " |
| 1730 | "env->GetByteArrayElements(arg%d, NULL);\n", |
| 1731 | argIndex, argIndex); |
| 1732 | fprintf(out, |
Yao Chen | 1fe9f59 | 2018-12-06 10:34:25 -0800 | [diff] [blame] | 1733 | " str%d_length = env->GetArrayLength(arg%d);\n", |
| 1734 | argIndex, argIndex); |
| 1735 | fprintf(out, |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1736 | " str%d = " |
| 1737 | "reinterpret_cast<char*>(env->GetByteArrayElements(arg%" |
| 1738 | "d, NULL));\n", |
| 1739 | argIndex, argIndex); |
| 1740 | fprintf(out, " } else {\n"); |
| 1741 | fprintf(out, " jbyte_array%d = NULL;\n", argIndex); |
| 1742 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 1743 | fprintf(out, " }\n"); |
| 1744 | |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 1745 | fprintf(out, |
| 1746 | " android::util::BytesField bytesField%d(str%d, " |
| 1747 | "str%d_length);", |
| 1748 | argIndex, argIndex, argIndex); |
| 1749 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1750 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1751 | hadStringOrChain = true; |
| 1752 | for (auto chainField : attributionDecl.fields) { |
| 1753 | fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n", |
| 1754 | chainField.name.c_str(), chainField.name.c_str()); |
| 1755 | if (chainField.name != attributionDecl.fields.front().name) { |
| 1756 | fprintf(out, " if (%s_length != %s_length) {\n", |
| 1757 | chainField.name.c_str(), |
| 1758 | attributionDecl.fields.front().name.c_str()); |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1759 | fprintf(out, " return -EINVAL;\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1760 | fprintf(out, " }\n"); |
| 1761 | } |
| 1762 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 1763 | fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n", |
| 1764 | chainField.name.c_str(), chainField.name.c_str()); |
| 1765 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 1766 | fprintf(out, " std::vector<%s> %s_vec;\n", |
| 1767 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 1768 | fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n", |
| 1769 | chainField.name.c_str()); |
| 1770 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 1771 | chainField.name.c_str()); |
| 1772 | fprintf(out, " jstring jstr = " |
| 1773 | "(jstring)env->GetObjectArrayElement(%s, i);\n", |
| 1774 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1775 | fprintf(out, " if (jstr == NULL) {\n"); |
| 1776 | fprintf(out, " %s_vec.push_back(NULL);\n", |
| 1777 | chainField.name.c_str()); |
| 1778 | fprintf(out, " } else {\n"); |
| 1779 | fprintf(out, " ScopedUtfChars* scoped_%s = " |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1780 | "new ScopedUtfChars(env, jstr);\n", |
| 1781 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1782 | fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1783 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1784 | fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1785 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1786 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1787 | fprintf(out, " }\n"); |
| 1788 | } |
| 1789 | fprintf(out, "\n"); |
| 1790 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1791 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1792 | isKeyValuePairAtom = true; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1793 | } |
| 1794 | argIndex++; |
| 1795 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1796 | // Emit this to quiet the unused parameter warning if there were no strings or attribution |
| 1797 | // chains. |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1798 | if (!hadStringOrChain && !isKeyValuePairAtom) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1799 | fprintf(out, " (void)env;\n"); |
| 1800 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1801 | if (isKeyValuePairAtom) { |
| 1802 | write_key_value_map_jni(out); |
| 1803 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1804 | |
| 1805 | // stats_write call |
| 1806 | argIndex = 1; |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 1807 | fprintf(out, "\n int ret = android::util::%s(code", |
| 1808 | cpp_method_name.c_str()); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1809 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1810 | arg != signature.end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1811 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1812 | for (auto chainField : attributionDecl.fields) { |
| 1813 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 1814 | fprintf(out, ", (const %s*)%s_array, %s_length", |
| 1815 | cpp_type_name(chainField.javaType), |
| 1816 | chainField.name.c_str(), chainField.name.c_str()); |
| 1817 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 1818 | fprintf(out, ", %s_vec", chainField.name.c_str()); |
| 1819 | } |
| 1820 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1821 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1822 | fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map"); |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 1823 | } else if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 1824 | fprintf(out, ", bytesField%d", argIndex); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1825 | } else { |
Yao Chen | e89572c | 2019-01-09 15:41:50 -0800 | [diff] [blame] | 1826 | const char* argName = |
| 1827 | (*arg == JAVA_TYPE_STRING) ? "str" : "arg"; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1828 | fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex); |
| 1829 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1830 | argIndex++; |
| 1831 | } |
| 1832 | fprintf(out, ");\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1833 | fprintf(out, "\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Clean up strings |
| 1836 | argIndex = 1; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1837 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 1838 | arg != signature.end(); arg++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1839 | if (*arg == JAVA_TYPE_STRING) { |
| 1840 | fprintf(out, " if (str%d != NULL) {\n", argIndex); |
| 1841 | fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n", |
| 1842 | argIndex, argIndex); |
| 1843 | fprintf(out, " }\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1844 | } else if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 1845 | fprintf(out, " if (str%d != NULL) { \n", argIndex); |
| 1846 | fprintf(out, |
| 1847 | " env->ReleaseByteArrayElements(arg%d, " |
| 1848 | "jbyte_array%d, 0);\n", |
| 1849 | argIndex, argIndex); |
| 1850 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1851 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1852 | for (auto chainField : attributionDecl.fields) { |
| 1853 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 1854 | fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n", |
| 1855 | chainField.name.c_str(), chainField.name.c_str()); |
| 1856 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1857 | 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] | 1858 | chainField.name.c_str()); |
| 1859 | fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str()); |
| 1860 | fprintf(out, " }\n"); |
| 1861 | } |
| 1862 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1863 | } |
| 1864 | argIndex++; |
| 1865 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1866 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1867 | fprintf(out, " return ret;\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1868 | |
| 1869 | fprintf(out, "}\n"); |
| 1870 | fprintf(out, "\n"); |
| 1871 | } |
| 1872 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1873 | |
| 1874 | return 0; |
| 1875 | } |
| 1876 | |
| 1877 | void write_jni_registration(FILE* out, const string& java_method_name, |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1878 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 1879 | const AtomDecl &attributionDecl) { |
| 1880 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 1881 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 1882 | vector<java_type_t> signature = signature_to_modules_it->first; |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1883 | fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n", |
| 1884 | java_method_name.c_str(), |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1885 | jni_function_signature(signature, attributionDecl).c_str(), |
| 1886 | jni_function_name(java_method_name, signature).c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | static int |
| 1891 | write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
| 1892 | { |
| 1893 | // Print prelude |
| 1894 | fprintf(out, "// This file is autogenerated\n"); |
| 1895 | fprintf(out, "\n"); |
| 1896 | |
| 1897 | fprintf(out, "#include <statslog.h>\n"); |
| 1898 | fprintf(out, "\n"); |
| 1899 | fprintf(out, "#include <nativehelper/JNIHelp.h>\n"); |
| 1900 | fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n"); |
| 1901 | fprintf(out, "#include <utils/Vector.h>\n"); |
| 1902 | fprintf(out, "#include \"core_jni_helpers.h\"\n"); |
| 1903 | fprintf(out, "#include \"jni.h\"\n"); |
| 1904 | fprintf(out, "\n"); |
| 1905 | fprintf(out, "#define UNUSED __attribute__((__unused__))\n"); |
| 1906 | fprintf(out, "\n"); |
| 1907 | |
| 1908 | fprintf(out, "namespace android {\n"); |
| 1909 | fprintf(out, "\n"); |
| 1910 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1911 | write_stats_log_jni(out, "write", "stats_write", atoms.signatures_to_modules, attributionDecl); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1912 | write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained", |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1913 | atoms.non_chained_signatures_to_modules, attributionDecl); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1914 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1915 | // Print registration function table |
| 1916 | fprintf(out, "/*\n"); |
| 1917 | fprintf(out, " * JNI registration.\n"); |
| 1918 | fprintf(out, " */\n"); |
| 1919 | fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1920 | write_jni_registration(out, "write", atoms.signatures_to_modules, attributionDecl); |
| 1921 | write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures_to_modules, |
| 1922 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1923 | fprintf(out, "};\n"); |
| 1924 | fprintf(out, "\n"); |
| 1925 | |
| 1926 | // Print registration function |
Tej Singh | be0482b | 2019-03-19 22:01:57 -0700 | [diff] [blame] | 1927 | fprintf(out, "int register_android_util_StatsLogInternal(JNIEnv* env) {\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1928 | fprintf(out, " return RegisterMethodsOrDie(\n"); |
| 1929 | fprintf(out, " env,\n"); |
Tej Singh | be0482b | 2019-03-19 22:01:57 -0700 | [diff] [blame] | 1930 | fprintf(out, " \"android/util/StatsLogInternal\",\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1931 | fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n"); |
| 1932 | fprintf(out, "}\n"); |
| 1933 | |
| 1934 | fprintf(out, "\n"); |
| 1935 | fprintf(out, "} // namespace android\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1939 | static void |
| 1940 | print_usage() |
| 1941 | { |
| 1942 | fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n"); |
| 1943 | fprintf(stderr, "\n"); |
| 1944 | fprintf(stderr, "OPTIONS\n"); |
| 1945 | fprintf(stderr, " --cpp FILENAME the header file to output\n"); |
| 1946 | fprintf(stderr, " --header FILENAME the cpp file to output\n"); |
| 1947 | fprintf(stderr, " --help this message\n"); |
| 1948 | fprintf(stderr, " --java FILENAME the java file to output\n"); |
| 1949 | fprintf(stderr, " --jni FILENAME the jni file to output\n"); |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1950 | fprintf(stderr, " --module NAME optional, module name to generate outputs for\n"); |
| 1951 | fprintf(stderr, " --namespace COMMA,SEP,NAMESPACE required for cpp/header with module\n"); |
| 1952 | fprintf(stderr, " comma separated namespace of the files\n"); |
| 1953 | fprintf(stderr, " --importHeader NAME required for cpp/jni to say which header to import\n"); |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1954 | fprintf(stderr, " --javaPackage PACKAGE the package for the java file.\n"); |
| 1955 | fprintf(stderr, " required for java with module\n"); |
| 1956 | fprintf(stderr, " --javaClass CLASS the class name of the java class.\n"); |
| 1957 | fprintf(stderr, " Optional for Java with module.\n"); |
| 1958 | fprintf(stderr, " Default is \"StatsLogInternal\"\n");} |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1959 | |
| 1960 | /** |
| 1961 | * Do the argument parsing and execute the tasks. |
| 1962 | */ |
| 1963 | static int |
| 1964 | run(int argc, char const*const* argv) |
| 1965 | { |
| 1966 | string cppFilename; |
| 1967 | string headerFilename; |
| 1968 | string javaFilename; |
| 1969 | string jniFilename; |
| 1970 | |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1971 | string moduleName = DEFAULT_MODULE_NAME; |
| 1972 | string cppNamespace = DEFAULT_CPP_NAMESPACE; |
| 1973 | string cppHeaderImport = DEFAULT_CPP_HEADER_IMPORT; |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 1974 | string javaPackage = DEFAULT_JAVA_PACKAGE; |
| 1975 | string javaClass = DEFAULT_JAVA_CLASS; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 1976 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1977 | int index = 1; |
| 1978 | while (index < argc) { |
| 1979 | if (0 == strcmp("--help", argv[index])) { |
| 1980 | print_usage(); |
| 1981 | return 0; |
| 1982 | } else if (0 == strcmp("--cpp", argv[index])) { |
| 1983 | index++; |
| 1984 | if (index >= argc) { |
| 1985 | print_usage(); |
| 1986 | return 1; |
| 1987 | } |
| 1988 | cppFilename = argv[index]; |
| 1989 | } else if (0 == strcmp("--header", argv[index])) { |
| 1990 | index++; |
| 1991 | if (index >= argc) { |
| 1992 | print_usage(); |
| 1993 | return 1; |
| 1994 | } |
| 1995 | headerFilename = argv[index]; |
| 1996 | } else if (0 == strcmp("--java", argv[index])) { |
| 1997 | index++; |
| 1998 | if (index >= argc) { |
| 1999 | print_usage(); |
| 2000 | return 1; |
| 2001 | } |
| 2002 | javaFilename = argv[index]; |
| 2003 | } else if (0 == strcmp("--jni", argv[index])) { |
| 2004 | index++; |
| 2005 | if (index >= argc) { |
| 2006 | print_usage(); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | jniFilename = argv[index]; |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 2010 | } else if (0 == strcmp("--module", argv[index])) { |
| 2011 | index++; |
| 2012 | if (index >= argc) { |
| 2013 | print_usage(); |
| 2014 | return 1; |
| 2015 | } |
| 2016 | moduleName = argv[index]; |
| 2017 | } else if (0 == strcmp("--namespace", argv[index])) { |
| 2018 | index++; |
| 2019 | if (index >= argc) { |
| 2020 | print_usage(); |
| 2021 | return 1; |
| 2022 | } |
| 2023 | cppNamespace = argv[index]; |
| 2024 | } else if (0 == strcmp("--importHeader", argv[index])) { |
| 2025 | index++; |
| 2026 | if (index >= argc) { |
| 2027 | print_usage(); |
| 2028 | return 1; |
| 2029 | } |
| 2030 | cppHeaderImport = argv[index]; |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 2031 | } else if (0 == strcmp("--javaPackage", argv[index])) { |
| 2032 | index++; |
| 2033 | if (index >= argc) { |
| 2034 | print_usage(); |
| 2035 | return 1; |
| 2036 | } |
| 2037 | javaPackage = argv[index]; |
| 2038 | } else if (0 == strcmp("--javaClass", argv[index])) { |
| 2039 | index++; |
| 2040 | if (index >= argc) { |
| 2041 | print_usage(); |
| 2042 | return 1; |
| 2043 | } |
| 2044 | javaClass = argv[index]; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2045 | } |
| 2046 | index++; |
| 2047 | } |
| 2048 | |
| 2049 | if (cppFilename.size() == 0 |
| 2050 | && headerFilename.size() == 0 |
| 2051 | && javaFilename.size() == 0 |
| 2052 | && jniFilename.size() == 0) { |
| 2053 | print_usage(); |
| 2054 | return 1; |
| 2055 | } |
| 2056 | |
| 2057 | // Collate the parameters |
| 2058 | Atoms atoms; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 2059 | int errorCount = collate_atoms(Atom::descriptor(), &atoms); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2060 | if (errorCount != 0) { |
| 2061 | return 1; |
| 2062 | } |
| 2063 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 2064 | AtomDecl attributionDecl; |
| 2065 | vector<java_type_t> attributionSignature; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 2066 | collate_atom(android::os::statsd::AttributionNode::descriptor(), |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 2067 | &attributionDecl, &attributionSignature); |
| 2068 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2069 | // Write the .cpp file |
| 2070 | if (cppFilename.size() != 0) { |
| 2071 | FILE* out = fopen(cppFilename.c_str(), "w"); |
| 2072 | if (out == NULL) { |
| 2073 | fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str()); |
| 2074 | return 1; |
| 2075 | } |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 2076 | // If this is for a specific module, the namespace must also be provided. |
| 2077 | if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) { |
| 2078 | fprintf(stderr, "Must supply --namespace if supplying a specific module\n"); |
| 2079 | return 1; |
| 2080 | } |
| 2081 | // If this is for a specific module, the header file to import must also be provided. |
| 2082 | if (moduleName != DEFAULT_MODULE_NAME && cppHeaderImport == DEFAULT_CPP_HEADER_IMPORT) { |
| 2083 | fprintf(stderr, "Must supply --headerImport if supplying a specific module\n"); |
| 2084 | return 1; |
| 2085 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 2086 | errorCount = android::stats_log_api_gen::write_stats_log_cpp( |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 2087 | out, atoms, attributionDecl, moduleName, cppNamespace, cppHeaderImport); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2088 | fclose(out); |
| 2089 | } |
| 2090 | |
| 2091 | // Write the .h file |
| 2092 | if (headerFilename.size() != 0) { |
| 2093 | FILE* out = fopen(headerFilename.c_str(), "w"); |
| 2094 | if (out == NULL) { |
| 2095 | fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str()); |
| 2096 | return 1; |
| 2097 | } |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 2098 | // If this is for a specific module, the namespace must also be provided. |
| 2099 | if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) { |
| 2100 | fprintf(stderr, "Must supply --namespace if supplying a specific module\n"); |
| 2101 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 2102 | errorCount = android::stats_log_api_gen::write_stats_log_header( |
Tej Singh | 810eeb3 | 2019-03-07 19:08:52 -0800 | [diff] [blame] | 2103 | out, atoms, attributionDecl, moduleName, cppNamespace); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2104 | fclose(out); |
| 2105 | } |
| 2106 | |
| 2107 | // Write the .java file |
| 2108 | if (javaFilename.size() != 0) { |
| 2109 | FILE* out = fopen(javaFilename.c_str(), "w"); |
| 2110 | if (out == NULL) { |
| 2111 | fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str()); |
| 2112 | return 1; |
| 2113 | } |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 2114 | // If this is for a specific module, the java package must also be provided. |
| 2115 | if (moduleName != DEFAULT_MODULE_NAME && javaPackage== DEFAULT_JAVA_PACKAGE) { |
| 2116 | fprintf(stderr, "Must supply --javaPackage if supplying a specific module\n"); |
| 2117 | return 1; |
| 2118 | } |
| 2119 | if (moduleName == DEFAULT_MODULE_NAME) { |
| 2120 | errorCount = android::stats_log_api_gen::write_stats_log_java( |
| 2121 | out, atoms, attributionDecl); |
| 2122 | } else { |
| 2123 | errorCount = android::stats_log_api_gen::write_stats_log_java_for_module( |
| 2124 | out, atoms, attributionDecl, moduleName, javaClass, javaPackage); |
| 2125 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2126 | fclose(out); |
| 2127 | } |
| 2128 | |
| 2129 | // Write the jni file |
| 2130 | if (jniFilename.size() != 0) { |
| 2131 | FILE* out = fopen(jniFilename.c_str(), "w"); |
| 2132 | if (out == NULL) { |
| 2133 | fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str()); |
| 2134 | return 1; |
| 2135 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 2136 | errorCount = android::stats_log_api_gen::write_stats_log_jni( |
| 2137 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2138 | fclose(out); |
| 2139 | } |
| 2140 | |
Tej Singh | 2910d5a | 2019-03-23 17:26:32 -0700 | [diff] [blame^] | 2141 | return errorCount; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * Main. |
| 2149 | */ |
| 2150 | int |
| 2151 | main(int argc, char const*const* argv) |
| 2152 | { |
| 2153 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 2154 | |
| 2155 | return android::stats_log_api_gen::run(argc, argv); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 2156 | } |