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