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