Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | #include "Collation.h" |
| 4 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 5 | #include "frameworks/base/cmds/statsd/src/atoms.pb.h" |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 6 | |
| 7 | #include <set> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <getopt.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | using namespace google::protobuf; |
| 16 | using namespace std; |
| 17 | |
| 18 | namespace android { |
| 19 | namespace stats_log_api_gen { |
| 20 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | int maxPushedAtomId = 2; |
| 22 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 23 | using android::os::statsd::Atom; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 24 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Turn lower and camel case into upper case with underscores. |
| 27 | */ |
| 28 | static string |
| 29 | make_constant_name(const string& str) |
| 30 | { |
| 31 | string result; |
| 32 | const int N = str.size(); |
| 33 | bool underscore_next = false; |
| 34 | for (int i=0; i<N; i++) { |
| 35 | char c = str[i]; |
| 36 | if (c >= 'A' && c <= 'Z') { |
| 37 | if (underscore_next) { |
| 38 | result += '_'; |
| 39 | underscore_next = false; |
| 40 | } |
| 41 | } else if (c >= 'a' && c <= 'z') { |
| 42 | c = 'A' + c - 'a'; |
| 43 | underscore_next = true; |
| 44 | } else if (c == '_') { |
| 45 | underscore_next = false; |
| 46 | } |
| 47 | result += c; |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | static const char* |
| 53 | cpp_type_name(java_type_t type) |
| 54 | { |
| 55 | switch (type) { |
| 56 | case JAVA_TYPE_BOOLEAN: |
| 57 | return "bool"; |
| 58 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 59 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 60 | return "int32_t"; |
| 61 | case JAVA_TYPE_LONG: |
| 62 | return "int64_t"; |
| 63 | case JAVA_TYPE_FLOAT: |
| 64 | return "float"; |
| 65 | case JAVA_TYPE_DOUBLE: |
| 66 | return "double"; |
| 67 | case JAVA_TYPE_STRING: |
| 68 | return "char const*"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 69 | case JAVA_TYPE_BYTE_ARRAY: |
| 70 | return "char const*"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 71 | default: |
| 72 | return "UNKNOWN"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static const char* |
| 77 | java_type_name(java_type_t type) |
| 78 | { |
| 79 | switch (type) { |
| 80 | case JAVA_TYPE_BOOLEAN: |
| 81 | return "boolean"; |
| 82 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 83 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 84 | return "int"; |
| 85 | case JAVA_TYPE_LONG: |
| 86 | return "long"; |
| 87 | case JAVA_TYPE_FLOAT: |
| 88 | return "float"; |
| 89 | case JAVA_TYPE_DOUBLE: |
| 90 | return "double"; |
| 91 | case JAVA_TYPE_STRING: |
| 92 | return "java.lang.String"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 93 | case JAVA_TYPE_BYTE_ARRAY: |
| 94 | return "byte[]"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 95 | default: |
| 96 | return "UNKNOWN"; |
| 97 | } |
| 98 | } |
| 99 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 100 | static int write_stats_log_cpp(FILE *out, const Atoms &atoms, |
| 101 | const AtomDecl &attributionDecl) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 102 | // Print prelude |
| 103 | fprintf(out, "// This file is autogenerated\n"); |
| 104 | fprintf(out, "\n"); |
| 105 | |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 106 | fprintf(out, "#include <mutex>\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 107 | fprintf(out, "#include <chrono>\n"); |
| 108 | fprintf(out, "#include <thread>\n"); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 109 | fprintf(out, "#include <cutils/properties.h>\n"); |
Yao Chen | f7bc6ab | 2018-04-18 13:45:48 -0700 | [diff] [blame] | 110 | fprintf(out, "#include <stats_event_list.h>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 111 | fprintf(out, "#include <log/log.h>\n"); |
| 112 | fprintf(out, "#include <statslog.h>\n"); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 113 | fprintf(out, "#include <utils/SystemClock.h>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 114 | fprintf(out, "\n"); |
| 115 | |
| 116 | fprintf(out, "namespace android {\n"); |
| 117 | fprintf(out, "namespace util {\n"); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 118 | fprintf(out, "// the single event tag id for all stats logs\n"); |
| 119 | fprintf(out, "const static int kStatsEventTag = 1937006964;\n"); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 120 | fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 121 | |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 122 | std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed", |
| 123 | "audio_state_changed", |
| 124 | "call_state_changed", |
| 125 | "phone_signal_strength_changed", |
| 126 | "mobile_bytes_transfer_by_fg_bg", |
| 127 | "mobile_bytes_transfer"}; |
| 128 | fprintf(out, |
| 129 | "const std::set<int> " |
| 130 | "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n"); |
| 131 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 132 | atom != atoms.decls.end(); atom++) { |
| 133 | if (kTruncatingAtomNames.find(atom->name) == |
| 134 | kTruncatingAtomNames.end()) { |
| 135 | string constant = make_constant_name(atom->name); |
| 136 | fprintf(out, " %s,\n", constant.c_str()); |
| 137 | } |
| 138 | } |
| 139 | fprintf(out, "};\n"); |
| 140 | fprintf(out, "\n"); |
| 141 | |
| 142 | fprintf(out, |
| 143 | "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n"); |
| 144 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 145 | atom != atoms.decls.end(); atom++) { |
| 146 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 147 | field != atom->fields.end(); field++) { |
| 148 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 149 | string constant = make_constant_name(atom->name); |
| 150 | fprintf(out, " %s,\n", constant.c_str()); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | fprintf(out, "};\n"); |
| 156 | fprintf(out, "\n"); |
| 157 | |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 158 | fprintf(out, "static std::map<int, int> getAtomUidField() {\n"); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 159 | fprintf(out, " std::map<int, int> uidField;\n"); |
| 160 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 161 | atom != atoms.decls.end(); atom++) { |
| 162 | if (atom->uidField == 0) { |
| 163 | continue; |
| 164 | } |
| 165 | fprintf(out, |
| 166 | "\n // Adding uid field for atom " |
| 167 | "(%d)%s\n", |
| 168 | atom->code, atom->name.c_str()); |
| 169 | fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n", |
| 170 | make_constant_name(atom->name).c_str(), atom->uidField); |
| 171 | } |
| 172 | |
| 173 | fprintf(out, " return uidField;\n"); |
| 174 | fprintf(out, "};\n"); |
| 175 | |
| 176 | fprintf(out, |
| 177 | "const std::map<int, int> AtomsInfo::kAtomsWithUidField = " |
| 178 | "getAtomUidField();\n"); |
| 179 | |
| 180 | fprintf(out, |
| 181 | "static std::map<int, StateAtomFieldOptions> " |
| 182 | "getStateAtomFieldOptions() {\n"); |
| 183 | fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n"); |
| 184 | fprintf(out, " StateAtomFieldOptions opt;\n"); |
| 185 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 186 | atom != atoms.decls.end(); atom++) { |
| 187 | if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) { |
| 188 | continue; |
| 189 | } |
| 190 | fprintf(out, |
| 191 | "\n // Adding primary and exclusive fields for atom " |
| 192 | "(%d)%s\n", |
| 193 | atom->code, atom->name.c_str()); |
| 194 | fprintf(out, " opt.primaryFields.clear();\n"); |
| 195 | for (const auto& field : atom->primaryFields) { |
| 196 | fprintf(out, " opt.primaryFields.push_back(%d);\n", field); |
| 197 | } |
| 198 | |
| 199 | fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField); |
| 200 | fprintf(out, " options[static_cast<int>(%s)] = opt;\n", |
| 201 | make_constant_name(atom->name).c_str()); |
| 202 | } |
| 203 | |
| 204 | fprintf(out, " return options;\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 205 | fprintf(out, "}\n"); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 206 | |
| 207 | fprintf(out, |
| 208 | "const std::map<int, StateAtomFieldOptions> " |
| 209 | "AtomsInfo::kStateAtomsFieldOptions = " |
| 210 | "getStateAtomFieldOptions();\n"); |
| 211 | |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 212 | fprintf(out, |
| 213 | "static std::map<int, std::vector<int>> " |
| 214 | "getBinaryFieldAtoms() {\n"); |
| 215 | fprintf(out, " std::map<int, std::vector<int>> options;\n"); |
| 216 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 217 | atom != atoms.decls.end(); atom++) { |
| 218 | if (atom->binaryFields.size() == 0) { |
| 219 | continue; |
| 220 | } |
| 221 | fprintf(out, |
| 222 | "\n // Adding binary fields for atom " |
| 223 | "(%d)%s\n", |
| 224 | atom->code, atom->name.c_str()); |
| 225 | |
| 226 | for (const auto& field : atom->binaryFields) { |
| 227 | fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n", |
| 228 | make_constant_name(atom->name).c_str(), field); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | fprintf(out, " return options;\n"); |
| 233 | fprintf(out, "}\n"); |
| 234 | |
| 235 | fprintf(out, |
| 236 | "const std::map<int, std::vector<int>> " |
| 237 | "AtomsInfo::kBytesFieldAtoms = " |
| 238 | "getBinaryFieldAtoms();\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 239 | |
| 240 | fprintf(out, "int64_t lastRetryTimestampNs = -1;\n"); |
| 241 | fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n"); |
| 242 | fprintf(out, "static std::mutex mLogdRetryMutex;\n"); |
| 243 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 244 | // Print write methods |
| 245 | fprintf(out, "\n"); |
| 246 | for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 247 | signature != atoms.signatures.end(); signature++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 248 | int argIndex; |
| 249 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 250 | fprintf(out, "int\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 251 | fprintf(out, "try_stats_write(int32_t code"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 252 | argIndex = 1; |
| 253 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 254 | arg != signature->end(); arg++) { |
| 255 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 256 | for (auto chainField : attributionDecl.fields) { |
| 257 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 258 | fprintf(out, ", const std::vector<%s>& %s", |
| 259 | cpp_type_name(chainField.javaType), |
| 260 | chainField.name.c_str()); |
| 261 | } else { |
| 262 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 263 | cpp_type_name(chainField.javaType), |
| 264 | chainField.name.c_str(), chainField.name.c_str()); |
| 265 | } |
| 266 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 267 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 268 | fprintf(out, ", const std::map<int, int32_t>& arg%d_1, " |
| 269 | "const std::map<int, int64_t>& arg%d_2, " |
| 270 | "const std::map<int, char const*>& arg%d_3, " |
| 271 | "const std::map<int, float>& arg%d_4", |
| 272 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 273 | } else { |
| 274 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 275 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 276 | argIndex++; |
| 277 | } |
| 278 | fprintf(out, ")\n"); |
| 279 | |
| 280 | fprintf(out, "{\n"); |
| 281 | argIndex = 1; |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 282 | fprintf(out, " if (kStatsdEnabled) {\n"); |
Yao Chen | f7bc6ab | 2018-04-18 13:45:48 -0700 | [diff] [blame] | 283 | fprintf(out, " stats_event_list event(kStatsEventTag);\n"); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 284 | fprintf(out, " event << android::elapsedRealtimeNano();\n\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 285 | fprintf(out, " event << code;\n\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 286 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 287 | arg != signature->end(); arg++) { |
| 288 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 289 | for (const auto &chainField : attributionDecl.fields) { |
| 290 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 291 | fprintf(out, " if (%s_length != %s.size()) {\n", |
| 292 | attributionDecl.fields.front().name.c_str(), chainField.name.c_str()); |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 293 | fprintf(out, " return -EINVAL;\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 294 | fprintf(out, " }\n"); |
| 295 | } |
| 296 | } |
| 297 | fprintf(out, "\n event.begin();\n"); |
| 298 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 299 | attributionDecl.fields.front().name.c_str()); |
| 300 | fprintf(out, " event.begin();\n"); |
| 301 | for (const auto &chainField : attributionDecl.fields) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 302 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 303 | fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str()); |
| 304 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 305 | fprintf(out, " } else {\n"); |
| 306 | fprintf(out, " event << \"\";\n"); |
| 307 | fprintf(out, " }\n"); |
| 308 | } else { |
| 309 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 310 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 311 | } |
| 312 | fprintf(out, " event.end();\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 313 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 314 | fprintf(out, " event.end();\n\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 315 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 316 | fprintf(out, " event.begin();\n\n"); |
| 317 | fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex); |
| 318 | fprintf(out, " event.begin();\n"); |
| 319 | fprintf(out, " event << it.first;\n"); |
| 320 | fprintf(out, " event << it.second;\n"); |
| 321 | fprintf(out, " event.end();\n"); |
| 322 | fprintf(out, " }\n"); |
| 323 | |
| 324 | fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex); |
| 325 | fprintf(out, " event.begin();\n"); |
| 326 | fprintf(out, " event << it.first;\n"); |
| 327 | fprintf(out, " event << it.second;\n"); |
| 328 | fprintf(out, " event.end();\n"); |
| 329 | fprintf(out, " }\n"); |
| 330 | |
| 331 | fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex); |
| 332 | fprintf(out, " event.begin();\n"); |
| 333 | fprintf(out, " event << it.first;\n"); |
| 334 | fprintf(out, " event << it.second;\n"); |
| 335 | fprintf(out, " event.end();\n"); |
| 336 | fprintf(out, " }\n"); |
| 337 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 338 | fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex); |
| 339 | fprintf(out, " event.begin();\n"); |
| 340 | fprintf(out, " event << it.first;\n"); |
| 341 | fprintf(out, " event << it.second;\n"); |
| 342 | fprintf(out, " event.end();\n"); |
| 343 | fprintf(out, " }\n"); |
| 344 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 345 | fprintf(out, " event.end();\n\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 346 | } else { |
| 347 | if (*arg == JAVA_TYPE_STRING) { |
| 348 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 349 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 350 | fprintf(out, " }\n"); |
| 351 | } |
| 352 | fprintf(out, " event << arg%d;\n", argIndex); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 353 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 354 | argIndex++; |
| 355 | } |
| 356 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 357 | fprintf(out, " return event.write(LOG_ID_STATS);\n"); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 358 | fprintf(out, " } else {\n"); |
| 359 | fprintf(out, " return 1;\n"); |
| 360 | fprintf(out, " }\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 361 | fprintf(out, "}\n"); |
| 362 | fprintf(out, "\n"); |
| 363 | } |
| 364 | |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 365 | for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin(); |
| 366 | signature != atoms.signatures.end(); signature++) { |
| 367 | int argIndex; |
| 368 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 369 | fprintf(out, "int\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 370 | fprintf(out, "stats_write(int32_t code"); |
| 371 | argIndex = 1; |
| 372 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 373 | arg != signature->end(); arg++) { |
| 374 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 375 | for (auto chainField : attributionDecl.fields) { |
| 376 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 377 | fprintf(out, ", const std::vector<%s>& %s", |
| 378 | cpp_type_name(chainField.javaType), |
| 379 | chainField.name.c_str()); |
| 380 | } else { |
| 381 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 382 | cpp_type_name(chainField.javaType), |
| 383 | chainField.name.c_str(), chainField.name.c_str()); |
| 384 | } |
| 385 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 386 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 387 | fprintf(out, ", const std::map<int, int32_t>& arg%d_1, " |
| 388 | "const std::map<int, int64_t>& arg%d_2, " |
| 389 | "const std::map<int, char const*>& arg%d_3, " |
| 390 | "const std::map<int, float>& arg%d_4", |
| 391 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 392 | } else { |
| 393 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 394 | } |
| 395 | argIndex++; |
| 396 | } |
| 397 | fprintf(out, ")\n"); |
| 398 | |
| 399 | fprintf(out, "{\n"); |
| 400 | fprintf(out, " int ret = 0;\n"); |
| 401 | |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 402 | fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 403 | fprintf(out, " ret = try_stats_write(code"); |
| 404 | |
| 405 | argIndex = 1; |
| 406 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 407 | arg != signature->end(); arg++) { |
| 408 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 409 | for (auto chainField : attributionDecl.fields) { |
| 410 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 411 | fprintf(out, ", %s", |
| 412 | chainField.name.c_str()); |
| 413 | } else { |
| 414 | fprintf(out, ", %s, %s_length", |
| 415 | chainField.name.c_str(), chainField.name.c_str()); |
| 416 | } |
| 417 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 418 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 419 | fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", |
| 420 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 421 | } else { |
| 422 | fprintf(out, ", arg%d", argIndex); |
| 423 | } |
| 424 | argIndex++; |
| 425 | } |
| 426 | fprintf(out, ");\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 427 | fprintf(out, " if (ret >= 0) { break; }\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 428 | |
| 429 | fprintf(out, " {\n"); |
| 430 | fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n"); |
| 431 | fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= " |
| 432 | "kMinRetryIntervalNs) break;\n"); |
| 433 | fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n"); |
| 434 | fprintf(out, " }\n"); |
| 435 | fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 436 | fprintf(out, " }\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 437 | fprintf(out, " if (ret < 0) {\n"); |
Yao Chen | 39b6799 | 2018-11-08 15:32:17 -0800 | [diff] [blame] | 438 | fprintf(out, " note_log_drop(ret);\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 439 | fprintf(out, " }\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 440 | fprintf(out, " return ret;\n"); |
| 441 | fprintf(out, "}\n"); |
| 442 | fprintf(out, "\n"); |
| 443 | } |
| 444 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 445 | for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin(); |
| 446 | signature != atoms.non_chained_signatures.end(); signature++) { |
| 447 | int argIndex; |
| 448 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 449 | fprintf(out, "int\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 450 | fprintf(out, "try_stats_write_non_chained(int32_t code"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 451 | argIndex = 1; |
| 452 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 453 | arg != signature->end(); arg++) { |
| 454 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 455 | argIndex++; |
| 456 | } |
| 457 | fprintf(out, ")\n"); |
| 458 | |
| 459 | fprintf(out, "{\n"); |
| 460 | argIndex = 1; |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 461 | fprintf(out, " if (kStatsdEnabled) {\n"); |
Yao Chen | f7bc6ab | 2018-04-18 13:45:48 -0700 | [diff] [blame] | 462 | fprintf(out, " stats_event_list event(kStatsEventTag);\n"); |
Yangster-mac | 31ddcde | 2018-02-14 16:09:35 -0800 | [diff] [blame] | 463 | fprintf(out, " event << android::elapsedRealtimeNano();\n\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 464 | fprintf(out, " event << code;\n\n"); |
| 465 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 466 | arg != signature->end(); arg++) { |
| 467 | if (argIndex == 1) { |
| 468 | fprintf(out, " event.begin();\n\n"); |
| 469 | fprintf(out, " event.begin();\n"); |
| 470 | } |
| 471 | if (*arg == JAVA_TYPE_STRING) { |
| 472 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 473 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 474 | fprintf(out, " }\n"); |
| 475 | } |
| 476 | fprintf(out, " event << arg%d;\n", argIndex); |
| 477 | if (argIndex == 2) { |
| 478 | fprintf(out, " event.end();\n\n"); |
| 479 | fprintf(out, " event.end();\n\n"); |
| 480 | } |
| 481 | argIndex++; |
| 482 | } |
| 483 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 484 | fprintf(out, " return event.write(LOG_ID_STATS);\n"); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 485 | fprintf(out, " } else {\n"); |
| 486 | fprintf(out, " return 1;\n"); |
| 487 | fprintf(out, " }\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 488 | fprintf(out, "}\n"); |
| 489 | fprintf(out, "\n"); |
| 490 | } |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 491 | |
| 492 | for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin(); |
| 493 | signature != atoms.non_chained_signatures.end(); signature++) { |
| 494 | int argIndex; |
| 495 | |
| 496 | fprintf(out, "int\n"); |
| 497 | fprintf(out, "stats_write_non_chained(int32_t code"); |
| 498 | argIndex = 1; |
| 499 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 500 | arg != signature->end(); arg++) { |
| 501 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 502 | argIndex++; |
| 503 | } |
| 504 | fprintf(out, ")\n"); |
| 505 | |
| 506 | fprintf(out, "{\n"); |
| 507 | |
| 508 | fprintf(out, " int ret = 0;\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 509 | fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 510 | fprintf(out, " ret = try_stats_write_non_chained(code"); |
| 511 | |
| 512 | argIndex = 1; |
| 513 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 514 | arg != signature->end(); arg++) { |
| 515 | fprintf(out, ", arg%d", argIndex); |
| 516 | argIndex++; |
| 517 | } |
| 518 | fprintf(out, ");\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 519 | fprintf(out, " if (ret >= 0) { break; }\n"); |
Yangster-mac | ca5c086 | 2018-04-09 22:39:53 -0700 | [diff] [blame] | 520 | |
| 521 | fprintf(out, " {\n"); |
| 522 | fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n"); |
| 523 | fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= " |
| 524 | "kMinRetryIntervalNs) break;\n"); |
| 525 | fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n"); |
| 526 | fprintf(out, " }\n"); |
| 527 | |
| 528 | fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 529 | fprintf(out, " }\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 530 | fprintf(out, " if (ret < 0) {\n"); |
Yao Chen | 39b6799 | 2018-11-08 15:32:17 -0800 | [diff] [blame] | 531 | fprintf(out, " note_log_drop(ret);\n"); |
Yao Chen | 0f5d661 | 2018-08-22 14:11:51 -0700 | [diff] [blame] | 532 | fprintf(out, " }\n"); |
| 533 | fprintf(out, " return ret;\n\n"); |
Yangster-mac | b8382a1 | 2018-04-04 10:39:12 -0700 | [diff] [blame] | 534 | fprintf(out, "}\n"); |
| 535 | |
| 536 | fprintf(out, "\n"); |
| 537 | } |
| 538 | |
| 539 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 540 | // Print footer |
| 541 | fprintf(out, "\n"); |
| 542 | fprintf(out, "} // namespace util\n"); |
| 543 | fprintf(out, "} // namespace android\n"); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 548 | void build_non_chained_decl_map(const Atoms& atoms, |
| 549 | std::map<int, set<AtomDecl>::const_iterator>* decl_map){ |
| 550 | for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin(); |
| 551 | atom != atoms.non_chained_decls.end(); atom++) { |
| 552 | decl_map->insert(std::make_pair(atom->code, atom)); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static void write_cpp_usage( |
| 557 | FILE* out, const string& method_name, const string& atom_code_name, |
| 558 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
| 559 | fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str()); |
| 560 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 561 | field != atom.fields.end(); field++) { |
| 562 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 563 | for (auto chainField : attributionDecl.fields) { |
| 564 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 565 | fprintf(out, ", const std::vector<%s>& %s", |
| 566 | cpp_type_name(chainField.javaType), |
| 567 | chainField.name.c_str()); |
| 568 | } else { |
| 569 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 570 | cpp_type_name(chainField.javaType), |
| 571 | chainField.name.c_str(), chainField.name.c_str()); |
| 572 | } |
| 573 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 574 | } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 575 | fprintf(out, ", const std::map<int, int32_t>& %s_int" |
| 576 | ", const std::map<int, int64_t>& %s_long" |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 577 | ", const std::map<int, char const*>& %s_str" |
| 578 | ", const std::map<int, float>& %s_float", |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 579 | field->name.c_str(), |
| 580 | field->name.c_str(), |
| 581 | field->name.c_str(), |
| 582 | field->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 583 | } else { |
| 584 | fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str()); |
| 585 | } |
| 586 | } |
| 587 | fprintf(out, ");\n"); |
| 588 | } |
| 589 | |
| 590 | static void write_cpp_method_header( |
| 591 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 592 | const AtomDecl &attributionDecl) { |
| 593 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 594 | signature != signatures.end(); signature++) { |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 595 | fprintf(out, "int %s(int32_t code", method_name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 596 | int argIndex = 1; |
| 597 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 598 | arg != signature->end(); arg++) { |
| 599 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 600 | for (auto chainField : attributionDecl.fields) { |
| 601 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 602 | fprintf(out, ", const std::vector<%s>& %s", |
| 603 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 604 | } else { |
| 605 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 606 | cpp_type_name(chainField.javaType), |
| 607 | chainField.name.c_str(), chainField.name.c_str()); |
| 608 | } |
| 609 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 610 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 611 | fprintf(out, ", const std::map<int, int32_t>& arg%d_1, " |
| 612 | "const std::map<int, int64_t>& arg%d_2, " |
| 613 | "const std::map<int, char const*>& arg%d_3, " |
| 614 | "const std::map<int, float>& arg%d_4", |
| 615 | argIndex, argIndex, argIndex, argIndex); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 616 | } else { |
| 617 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 618 | } |
| 619 | argIndex++; |
| 620 | } |
| 621 | fprintf(out, ");\n"); |
| 622 | |
| 623 | } |
| 624 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 625 | |
| 626 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 627 | write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 628 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 629 | // Print prelude |
| 630 | fprintf(out, "// This file is autogenerated\n"); |
| 631 | fprintf(out, "\n"); |
| 632 | fprintf(out, "#pragma once\n"); |
| 633 | fprintf(out, "\n"); |
| 634 | fprintf(out, "#include <stdint.h>\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 635 | fprintf(out, "#include <vector>\n"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 636 | fprintf(out, "#include <map>\n"); |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 637 | fprintf(out, "#include <set>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 638 | fprintf(out, "\n"); |
| 639 | |
| 640 | fprintf(out, "namespace android {\n"); |
| 641 | fprintf(out, "namespace util {\n"); |
| 642 | fprintf(out, "\n"); |
| 643 | fprintf(out, "/*\n"); |
| 644 | fprintf(out, " * API For logging statistics events.\n"); |
| 645 | fprintf(out, " */\n"); |
| 646 | fprintf(out, "\n"); |
| 647 | fprintf(out, "/**\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 648 | fprintf(out, " * Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 649 | fprintf(out, " */\n"); |
| 650 | fprintf(out, "enum {\n"); |
| 651 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 652 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 653 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 654 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 655 | size_t i = 0; |
| 656 | // Print constants |
| 657 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 658 | atom != atoms.decls.end(); atom++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 659 | string constant = make_constant_name(atom->name); |
| 660 | fprintf(out, "\n"); |
| 661 | fprintf(out, " /**\n"); |
| 662 | 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] | 663 | write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl); |
| 664 | |
| 665 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 666 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 667 | write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second, |
| 668 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 669 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 670 | fprintf(out, " */\n"); |
| 671 | char const* const comma = (i == atoms.decls.size() - 1) ? "" : ","; |
| 672 | fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 673 | if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) { |
| 674 | maxPushedAtomId = atom->code; |
| 675 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 676 | i++; |
| 677 | } |
| 678 | fprintf(out, "\n"); |
| 679 | fprintf(out, "};\n"); |
| 680 | fprintf(out, "\n"); |
| 681 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 682 | fprintf(out, "struct StateAtomFieldOptions {\n"); |
| 683 | fprintf(out, " std::vector<int> primaryFields;\n"); |
| 684 | fprintf(out, " int exclusiveField;\n"); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 685 | fprintf(out, "};\n"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 686 | fprintf(out, "\n"); |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 687 | |
| 688 | fprintf(out, "struct AtomsInfo {\n"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 689 | fprintf(out, |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 690 | " const static std::set<int> " |
| 691 | "kNotTruncatingTimestampAtomWhiteList;\n"); |
| 692 | fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n"); |
| 693 | fprintf(out, |
| 694 | " const static std::set<int> kAtomsWithAttributionChain;\n"); |
| 695 | fprintf(out, |
| 696 | " const static std::map<int, StateAtomFieldOptions> " |
| 697 | "kStateAtomsFieldOptions;\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 698 | fprintf(out, |
| 699 | " const static std::map<int, std::vector<int>> " |
| 700 | "kBytesFieldAtoms;"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 701 | fprintf(out, "};\n"); |
| 702 | |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 703 | fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", |
| 704 | maxPushedAtomId); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 705 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 706 | // Print write methods |
| 707 | fprintf(out, "//\n"); |
| 708 | fprintf(out, "// Write methods\n"); |
| 709 | fprintf(out, "//\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 710 | write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl); |
| 711 | |
| 712 | fprintf(out, "//\n"); |
| 713 | fprintf(out, "// Write flattened methods\n"); |
| 714 | fprintf(out, "//\n"); |
| 715 | write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures, |
| 716 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 717 | |
| 718 | fprintf(out, "\n"); |
| 719 | fprintf(out, "} // namespace util\n"); |
| 720 | fprintf(out, "} // namespace android\n"); |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 725 | static void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name, |
| 726 | const AtomDecl& atom) { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 727 | fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s", |
| 728 | method_name.c_str(), atom_code_name.c_str()); |
| 729 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 730 | field != atom.fields.end(); field++) { |
| 731 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 732 | fprintf(out, ", android.os.WorkSource workSource"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 733 | } else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 734 | fprintf(out, ", SparseArray<Object> value_map"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 735 | } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) { |
| 736 | fprintf(out, ", byte[] %s", field->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 737 | } else { |
| 738 | fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str()); |
| 739 | } |
| 740 | } |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 741 | fprintf(out, ");<br>\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static void write_java_method( |
| 745 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 746 | const AtomDecl &attributionDecl) { |
| 747 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 748 | signature != signatures.end(); signature++) { |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 749 | 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] | 750 | int argIndex = 1; |
| 751 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 752 | arg != signature->end(); arg++) { |
| 753 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 754 | for (auto chainField : attributionDecl.fields) { |
| 755 | fprintf(out, ", %s[] %s", |
| 756 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 757 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 758 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 759 | fprintf(out, ", SparseArray<Object> value_map"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 760 | } else { |
| 761 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 762 | } |
| 763 | argIndex++; |
| 764 | } |
| 765 | fprintf(out, ");\n"); |
| 766 | } |
| 767 | } |
| 768 | |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 769 | static void write_java_work_source_method(FILE* out, const set<vector<java_type_t>>& signatures) { |
| 770 | fprintf(out, "\n // WorkSource methods.\n"); |
| 771 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 772 | signature != signatures.end(); signature++) { |
| 773 | // Determine if there is Attribution in this signature. |
| 774 | int attributionArg = -1; |
| 775 | int argIndexMax = 0; |
| 776 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 777 | arg != signature->end(); arg++) { |
| 778 | argIndexMax++; |
| 779 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 780 | if (attributionArg > -1) { |
| 781 | fprintf(stderr, "An atom contains multiple AttributionNode fields.\n"); |
| 782 | fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n"); |
| 783 | fprintf(out, "\n// Invalid for WorkSource: more than one attribution chain.\n"); |
| 784 | return; |
| 785 | } |
| 786 | attributionArg = argIndexMax; |
| 787 | } |
| 788 | } |
| 789 | if (attributionArg < 0) { |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | // Method header (signature) |
| 794 | fprintf(out, " public static void write(int code"); |
| 795 | int argIndex = 1; |
| 796 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 797 | arg != signature->end(); arg++) { |
| 798 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 799 | fprintf(out, ", WorkSource ws"); |
| 800 | } else { |
| 801 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 802 | } |
| 803 | argIndex++; |
| 804 | } |
| 805 | fprintf(out, ") {\n"); |
| 806 | |
| 807 | // write_non_chained() component. TODO: Remove when flat uids are no longer needed. |
| 808 | fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n"); |
| 809 | fprintf(out, " write_non_chained(code"); |
| 810 | for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) { |
| 811 | if (argIndex == attributionArg) { |
| 812 | fprintf(out, ", ws.get(i), ws.getName(i)"); |
| 813 | } else { |
| 814 | fprintf(out, ", arg%d", argIndex); |
| 815 | } |
| 816 | } |
| 817 | fprintf(out, ");\n"); |
| 818 | fprintf(out, " }\n"); // close flor-loop |
| 819 | |
| 820 | // write() component. |
| 821 | fprintf(out, " ArrayList<WorkSource.WorkChain> workChains = ws.getWorkChains();\n"); |
| 822 | fprintf(out, " if (workChains != null) {\n"); |
| 823 | fprintf(out, " for (WorkSource.WorkChain wc : workChains) {\n"); |
| 824 | fprintf(out, " write(code"); |
| 825 | for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) { |
| 826 | if (argIndex == attributionArg) { |
| 827 | fprintf(out, ", wc.getUids(), wc.getTags()"); |
| 828 | } else { |
| 829 | fprintf(out, ", arg%d", argIndex); |
| 830 | } |
| 831 | } |
| 832 | fprintf(out, ");\n"); |
| 833 | fprintf(out, " }\n"); // close for-loop |
| 834 | fprintf(out, " }\n"); // close if |
| 835 | fprintf(out, " }\n"); // close method |
| 836 | } |
| 837 | } |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 838 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 839 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 840 | write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 841 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 842 | // Print prelude |
| 843 | fprintf(out, "// This file is autogenerated\n"); |
| 844 | fprintf(out, "\n"); |
| 845 | fprintf(out, "package android.util;\n"); |
| 846 | fprintf(out, "\n"); |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 847 | fprintf(out, "import android.os.WorkSource;\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 848 | fprintf(out, "import android.util.SparseArray;\n"); |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 849 | fprintf(out, "import java.util.ArrayList;\n"); |
| 850 | fprintf(out, "\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 851 | fprintf(out, "\n"); |
| 852 | fprintf(out, "/**\n"); |
| 853 | fprintf(out, " * API For logging statistics events.\n"); |
| 854 | fprintf(out, " * @hide\n"); |
| 855 | fprintf(out, " */\n"); |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 856 | fprintf(out, "public class StatsLogInternal {\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 857 | fprintf(out, " // Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 858 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 859 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 860 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 861 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 862 | // Print constants for the atom codes. |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 863 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 864 | atom != atoms.decls.end(); atom++) { |
| 865 | string constant = make_constant_name(atom->name); |
| 866 | fprintf(out, "\n"); |
| 867 | fprintf(out, " /**\n"); |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 868 | fprintf(out, " * %s %s<br>\n", atom->message.c_str(), atom->name.c_str()); |
| 869 | write_java_usage(out, "write", constant, *atom); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 870 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 871 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 872 | write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 873 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 874 | fprintf(out, " */\n"); |
| 875 | fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code); |
| 876 | } |
| 877 | fprintf(out, "\n"); |
| 878 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 879 | // Print constants for the enum values. |
| 880 | fprintf(out, " // Constants for enum values.\n\n"); |
| 881 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 882 | atom != atoms.decls.end(); atom++) { |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 883 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 884 | field != atom->fields.end(); field++) { |
| 885 | if (field->javaType == JAVA_TYPE_ENUM) { |
| 886 | fprintf(out, " // Values for %s.%s\n", atom->message.c_str(), |
| 887 | field->name.c_str()); |
| 888 | for (map<int, string>::const_iterator value = field->enumValues.begin(); |
| 889 | value != field->enumValues.end(); value++) { |
| 890 | fprintf(out, " public static final int %s__%s__%s = %d;\n", |
| 891 | make_constant_name(atom->message).c_str(), |
| 892 | make_constant_name(field->name).c_str(), |
| 893 | make_constant_name(value->second).c_str(), |
| 894 | value->first); |
| 895 | } |
| 896 | fprintf(out, "\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 897 | } |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 901 | // Print write methods |
| 902 | fprintf(out, " // Write methods\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 903 | write_java_method(out, "write", atoms.signatures, attributionDecl); |
| 904 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
Bookatz | 0bd9720 | 2018-06-05 12:42:37 -0700 | [diff] [blame] | 905 | write_java_work_source_method(out, atoms.signatures); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 906 | |
| 907 | fprintf(out, "}\n"); |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static const char* |
| 913 | jni_type_name(java_type_t type) |
| 914 | { |
| 915 | switch (type) { |
| 916 | case JAVA_TYPE_BOOLEAN: |
| 917 | return "jboolean"; |
| 918 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 919 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 920 | return "jint"; |
| 921 | case JAVA_TYPE_LONG: |
| 922 | return "jlong"; |
| 923 | case JAVA_TYPE_FLOAT: |
| 924 | return "jfloat"; |
| 925 | case JAVA_TYPE_DOUBLE: |
| 926 | return "jdouble"; |
| 927 | case JAVA_TYPE_STRING: |
| 928 | return "jstring"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 929 | case JAVA_TYPE_BYTE_ARRAY: |
| 930 | return "jbyteArray"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 931 | default: |
| 932 | return "UNKNOWN"; |
| 933 | } |
| 934 | } |
| 935 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 936 | static const char* |
| 937 | jni_array_type_name(java_type_t type) |
| 938 | { |
| 939 | switch (type) { |
| 940 | case JAVA_TYPE_INT: |
| 941 | return "jintArray"; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 942 | case JAVA_TYPE_FLOAT: |
| 943 | return "jfloatArray"; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 944 | case JAVA_TYPE_STRING: |
| 945 | return "jobjectArray"; |
| 946 | default: |
| 947 | return "UNKNOWN"; |
| 948 | } |
| 949 | } |
| 950 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 951 | static string |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 952 | 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] | 953 | { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 954 | string result("StatsLog_" + method_name); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 955 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 956 | arg != signature.end(); arg++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 957 | switch (*arg) { |
| 958 | case JAVA_TYPE_BOOLEAN: |
| 959 | result += "_boolean"; |
| 960 | break; |
| 961 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 962 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 963 | result += "_int"; |
| 964 | break; |
| 965 | case JAVA_TYPE_LONG: |
| 966 | result += "_long"; |
| 967 | break; |
| 968 | case JAVA_TYPE_FLOAT: |
| 969 | result += "_float"; |
| 970 | break; |
| 971 | case JAVA_TYPE_DOUBLE: |
| 972 | result += "_double"; |
| 973 | break; |
| 974 | case JAVA_TYPE_STRING: |
| 975 | result += "_String"; |
| 976 | break; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 977 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 978 | result += "_AttributionChain"; |
| 979 | break; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 980 | case JAVA_TYPE_KEY_VALUE_PAIR: |
| 981 | result += "_KeyValuePairs"; |
| 982 | break; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 983 | case JAVA_TYPE_BYTE_ARRAY: |
| 984 | result += "_bytes"; |
| 985 | break; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 986 | default: |
| 987 | result += "_UNKNOWN"; |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | return result; |
| 992 | } |
| 993 | |
| 994 | static const char* |
| 995 | java_type_signature(java_type_t type) |
| 996 | { |
| 997 | switch (type) { |
| 998 | case JAVA_TYPE_BOOLEAN: |
| 999 | return "Z"; |
| 1000 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 1001 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1002 | return "I"; |
| 1003 | case JAVA_TYPE_LONG: |
| 1004 | return "J"; |
| 1005 | case JAVA_TYPE_FLOAT: |
| 1006 | return "F"; |
| 1007 | case JAVA_TYPE_DOUBLE: |
| 1008 | return "D"; |
| 1009 | case JAVA_TYPE_STRING: |
| 1010 | return "Ljava/lang/String;"; |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1011 | case JAVA_TYPE_BYTE_ARRAY: |
| 1012 | return "[B"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1013 | default: |
| 1014 | return "UNKNOWN"; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | static string |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1019 | jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1020 | { |
| 1021 | string result("(I"); |
| 1022 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1023 | arg != signature.end(); arg++) { |
| 1024 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1025 | for (auto chainField : attributionDecl.fields) { |
| 1026 | result += "["; |
| 1027 | result += java_type_signature(chainField.javaType); |
| 1028 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1029 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1030 | result += "Landroid/util/SparseArray;"; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1031 | } else { |
| 1032 | result += java_type_signature(*arg); |
| 1033 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1034 | } |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1035 | result += ")I"; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1036 | return result; |
| 1037 | } |
| 1038 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1039 | static void write_key_value_map_jni(FILE* out) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1040 | fprintf(out, " std::map<int, int32_t> int32_t_map;\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1041 | fprintf(out, " std::map<int, int64_t> int64_t_map;\n"); |
| 1042 | fprintf(out, " std::map<int, float> float_map;\n"); |
| 1043 | fprintf(out, " std::map<int, char const*> string_map;\n\n"); |
| 1044 | |
| 1045 | fprintf(out, " jclass jmap_class = env->FindClass(\"android/util/SparseArray\");\n"); |
| 1046 | |
| 1047 | fprintf(out, " jmethodID jget_size_method = env->GetMethodID(jmap_class, \"size\", \"()I\");\n"); |
| 1048 | fprintf(out, " jmethodID jget_key_method = env->GetMethodID(jmap_class, \"keyAt\", \"(I)I\");\n"); |
| 1049 | fprintf(out, " jmethodID jget_value_method = env->GetMethodID(jmap_class, \"valueAt\", \"(I)Ljava/lang/Object;\");\n\n"); |
| 1050 | |
| 1051 | |
| 1052 | fprintf(out, " std::vector<std::unique_ptr<ScopedUtfChars>> scoped_ufs;\n\n"); |
| 1053 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1054 | fprintf(out, " jclass jint_class = env->FindClass(\"java/lang/Integer\");\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1055 | fprintf(out, " jclass jlong_class = env->FindClass(\"java/lang/Long\");\n"); |
| 1056 | fprintf(out, " jclass jfloat_class = env->FindClass(\"java/lang/Float\");\n"); |
| 1057 | fprintf(out, " jclass jstring_class = env->FindClass(\"java/lang/String\");\n"); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1058 | fprintf(out, " jmethodID jget_int_method = env->GetMethodID(jint_class, \"intValue\", \"()I\");\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1059 | fprintf(out, " jmethodID jget_long_method = env->GetMethodID(jlong_class, \"longValue\", \"()J\");\n"); |
| 1060 | fprintf(out, " jmethodID jget_float_method = env->GetMethodID(jfloat_class, \"floatValue\", \"()F\");\n\n"); |
| 1061 | |
| 1062 | fprintf(out, " jint jsize = env->CallIntMethod(value_map, jget_size_method);\n"); |
| 1063 | fprintf(out, " for(int i = 0; i < jsize; i++) {\n"); |
| 1064 | fprintf(out, " jint key = env->CallIntMethod(value_map, jget_key_method, i);\n"); |
| 1065 | fprintf(out, " jobject jvalue_obj = env->CallObjectMethod(value_map, jget_value_method, i);\n"); |
| 1066 | fprintf(out, " if (jvalue_obj == NULL) { continue; }\n"); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1067 | fprintf(out, " if (env->IsInstanceOf(jvalue_obj, jint_class)) {\n"); |
| 1068 | fprintf(out, " int32_t_map[key] = env->CallIntMethod(jvalue_obj, jget_int_method);\n"); |
| 1069 | fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jlong_class)) {\n"); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1070 | fprintf(out, " int64_t_map[key] = env->CallLongMethod(jvalue_obj, jget_long_method);\n"); |
| 1071 | fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jfloat_class)) {\n"); |
| 1072 | fprintf(out, " float_map[key] = env->CallFloatMethod(jvalue_obj, jget_float_method);\n"); |
| 1073 | fprintf(out, " } else if (env->IsInstanceOf(jvalue_obj, jstring_class)) {\n"); |
| 1074 | fprintf(out, " std::unique_ptr<ScopedUtfChars> utf(new ScopedUtfChars(env, (jstring)jvalue_obj));\n"); |
| 1075 | fprintf(out, " if (utf->c_str() != NULL) { string_map[key] = utf->c_str(); }\n"); |
| 1076 | fprintf(out, " scoped_ufs.push_back(std::move(utf));\n"); |
| 1077 | fprintf(out, " }\n"); |
| 1078 | fprintf(out, " }\n"); |
| 1079 | } |
| 1080 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1081 | static int |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1082 | write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name, |
| 1083 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1084 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1085 | // Print write methods |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1086 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 1087 | signature != signatures.end(); signature++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1088 | int argIndex; |
| 1089 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1090 | fprintf(out, "static int\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1091 | fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code", |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1092 | jni_function_name(java_method_name, *signature).c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1093 | argIndex = 1; |
| 1094 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 1095 | arg != signature->end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1096 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1097 | for (auto chainField : attributionDecl.fields) { |
| 1098 | fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType), |
| 1099 | chainField.name.c_str()); |
| 1100 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1101 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1102 | fprintf(out, ", jobject value_map"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1103 | } else { |
| 1104 | fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex); |
| 1105 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1106 | argIndex++; |
| 1107 | } |
| 1108 | fprintf(out, ")\n"); |
| 1109 | |
| 1110 | fprintf(out, "{\n"); |
| 1111 | |
| 1112 | // Prepare strings |
| 1113 | argIndex = 1; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1114 | bool hadStringOrChain = false; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1115 | bool isKeyValuePairAtom = false; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1116 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 1117 | arg != signature->end(); arg++) { |
| 1118 | if (*arg == JAVA_TYPE_STRING) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1119 | hadStringOrChain = true; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1120 | fprintf(out, " const char* str%d;\n", argIndex); |
| 1121 | fprintf(out, " if (arg%d != NULL) {\n", argIndex); |
| 1122 | fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n", |
| 1123 | argIndex, argIndex); |
| 1124 | fprintf(out, " } else {\n"); |
| 1125 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 1126 | fprintf(out, " }\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1127 | } else if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 1128 | hadStringOrChain = true; |
| 1129 | fprintf(out, " jbyte* jbyte_array%d;\n", argIndex); |
| 1130 | fprintf(out, " const char* str%d;\n", argIndex); |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame^] | 1131 | fprintf(out, |
| 1132 | " if (arg%d != NULL && env->GetArrayLength(arg%d) > " |
| 1133 | "0) {\n", |
| 1134 | argIndex, argIndex); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1135 | fprintf(out, |
| 1136 | " jbyte_array%d = " |
| 1137 | "env->GetByteArrayElements(arg%d, NULL);\n", |
| 1138 | argIndex, argIndex); |
| 1139 | fprintf(out, |
| 1140 | " str%d = " |
| 1141 | "reinterpret_cast<char*>(env->GetByteArrayElements(arg%" |
| 1142 | "d, NULL));\n", |
| 1143 | argIndex, argIndex); |
| 1144 | fprintf(out, " } else {\n"); |
| 1145 | fprintf(out, " jbyte_array%d = NULL;\n", argIndex); |
| 1146 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 1147 | fprintf(out, " }\n"); |
| 1148 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1149 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1150 | hadStringOrChain = true; |
| 1151 | for (auto chainField : attributionDecl.fields) { |
| 1152 | fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n", |
| 1153 | chainField.name.c_str(), chainField.name.c_str()); |
| 1154 | if (chainField.name != attributionDecl.fields.front().name) { |
| 1155 | fprintf(out, " if (%s_length != %s_length) {\n", |
| 1156 | chainField.name.c_str(), |
| 1157 | attributionDecl.fields.front().name.c_str()); |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1158 | fprintf(out, " return -EINVAL;\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1159 | fprintf(out, " }\n"); |
| 1160 | } |
| 1161 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 1162 | fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n", |
| 1163 | chainField.name.c_str(), chainField.name.c_str()); |
| 1164 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 1165 | fprintf(out, " std::vector<%s> %s_vec;\n", |
| 1166 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 1167 | fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n", |
| 1168 | chainField.name.c_str()); |
| 1169 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 1170 | chainField.name.c_str()); |
| 1171 | fprintf(out, " jstring jstr = " |
| 1172 | "(jstring)env->GetObjectArrayElement(%s, i);\n", |
| 1173 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1174 | fprintf(out, " if (jstr == NULL) {\n"); |
| 1175 | fprintf(out, " %s_vec.push_back(NULL);\n", |
| 1176 | chainField.name.c_str()); |
| 1177 | fprintf(out, " } else {\n"); |
| 1178 | fprintf(out, " ScopedUtfChars* scoped_%s = " |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1179 | "new ScopedUtfChars(env, jstr);\n", |
| 1180 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1181 | fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1182 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1183 | fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1184 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1185 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1186 | fprintf(out, " }\n"); |
| 1187 | } |
| 1188 | fprintf(out, "\n"); |
| 1189 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1190 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 1191 | isKeyValuePairAtom = true; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1192 | } |
| 1193 | argIndex++; |
| 1194 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1195 | // Emit this to quiet the unused parameter warning if there were no strings or attribution |
| 1196 | // chains. |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1197 | if (!hadStringOrChain && !isKeyValuePairAtom) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1198 | fprintf(out, " (void)env;\n"); |
| 1199 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1200 | if (isKeyValuePairAtom) { |
| 1201 | write_key_value_map_jni(out); |
| 1202 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1203 | |
| 1204 | // stats_write call |
| 1205 | argIndex = 1; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1206 | fprintf(out, "\n int ret = android::util::%s(code", cpp_method_name.c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1207 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 1208 | arg != signature->end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1209 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1210 | for (auto chainField : attributionDecl.fields) { |
| 1211 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 1212 | fprintf(out, ", (const %s*)%s_array, %s_length", |
| 1213 | cpp_type_name(chainField.javaType), |
| 1214 | chainField.name.c_str(), chainField.name.c_str()); |
| 1215 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 1216 | fprintf(out, ", %s_vec", chainField.name.c_str()); |
| 1217 | } |
| 1218 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1219 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 1220 | fprintf(out, ", int32_t_map, int64_t_map, string_map, float_map"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1221 | } else { |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1222 | const char* argName = (*arg == JAVA_TYPE_STRING || |
| 1223 | *arg == JAVA_TYPE_BYTE_ARRAY) |
| 1224 | ? "str" |
| 1225 | : "arg"; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1226 | fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex); |
| 1227 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1228 | argIndex++; |
| 1229 | } |
| 1230 | fprintf(out, ");\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1231 | fprintf(out, "\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Clean up strings |
| 1234 | argIndex = 1; |
| 1235 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 1236 | arg != signature->end(); arg++) { |
| 1237 | if (*arg == JAVA_TYPE_STRING) { |
| 1238 | fprintf(out, " if (str%d != NULL) {\n", argIndex); |
| 1239 | fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n", |
| 1240 | argIndex, argIndex); |
| 1241 | fprintf(out, " }\n"); |
Yao Chen | bbdd67d | 2018-10-24 12:15:56 -0700 | [diff] [blame] | 1242 | } else if (*arg == JAVA_TYPE_BYTE_ARRAY) { |
| 1243 | fprintf(out, " if (str%d != NULL) { \n", argIndex); |
| 1244 | fprintf(out, |
| 1245 | " env->ReleaseByteArrayElements(arg%d, " |
| 1246 | "jbyte_array%d, 0);\n", |
| 1247 | argIndex, argIndex); |
| 1248 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1249 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 1250 | for (auto chainField : attributionDecl.fields) { |
| 1251 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 1252 | fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n", |
| 1253 | chainField.name.c_str(), chainField.name.c_str()); |
| 1254 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 1255 | 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] | 1256 | chainField.name.c_str()); |
| 1257 | fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str()); |
| 1258 | fprintf(out, " }\n"); |
| 1259 | } |
| 1260 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1261 | } |
| 1262 | argIndex++; |
| 1263 | } |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 1264 | |
Yao Chen | 97e21ec | 2018-03-29 11:00:38 -0700 | [diff] [blame] | 1265 | fprintf(out, " return ret;\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1266 | |
| 1267 | fprintf(out, "}\n"); |
| 1268 | fprintf(out, "\n"); |
| 1269 | } |
| 1270 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1271 | |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | void write_jni_registration(FILE* out, const string& java_method_name, |
| 1276 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) { |
| 1277 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 1278 | signature != signatures.end(); signature++) { |
| 1279 | fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n", |
| 1280 | java_method_name.c_str(), |
| 1281 | jni_function_signature(*signature, attributionDecl).c_str(), |
| 1282 | jni_function_name(java_method_name, *signature).c_str()); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | static int |
| 1287 | write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
| 1288 | { |
| 1289 | // Print prelude |
| 1290 | fprintf(out, "// This file is autogenerated\n"); |
| 1291 | fprintf(out, "\n"); |
| 1292 | |
| 1293 | fprintf(out, "#include <statslog.h>\n"); |
| 1294 | fprintf(out, "\n"); |
| 1295 | fprintf(out, "#include <nativehelper/JNIHelp.h>\n"); |
| 1296 | fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n"); |
| 1297 | fprintf(out, "#include <utils/Vector.h>\n"); |
| 1298 | fprintf(out, "#include \"core_jni_helpers.h\"\n"); |
| 1299 | fprintf(out, "#include \"jni.h\"\n"); |
| 1300 | fprintf(out, "\n"); |
| 1301 | fprintf(out, "#define UNUSED __attribute__((__unused__))\n"); |
| 1302 | fprintf(out, "\n"); |
| 1303 | |
| 1304 | fprintf(out, "namespace android {\n"); |
| 1305 | fprintf(out, "\n"); |
| 1306 | |
| 1307 | write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl); |
| 1308 | write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained", |
| 1309 | atoms.non_chained_signatures, attributionDecl); |
| 1310 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1311 | // Print registration function table |
| 1312 | fprintf(out, "/*\n"); |
| 1313 | fprintf(out, " * JNI registration.\n"); |
| 1314 | fprintf(out, " */\n"); |
| 1315 | fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 1316 | write_jni_registration(out, "write", atoms.signatures, attributionDecl); |
| 1317 | write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1318 | fprintf(out, "};\n"); |
| 1319 | fprintf(out, "\n"); |
| 1320 | |
| 1321 | // Print registration function |
| 1322 | fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n"); |
| 1323 | fprintf(out, " return RegisterMethodsOrDie(\n"); |
| 1324 | fprintf(out, " env,\n"); |
| 1325 | fprintf(out, " \"android/util/StatsLog\",\n"); |
| 1326 | fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n"); |
| 1327 | fprintf(out, "}\n"); |
| 1328 | |
| 1329 | fprintf(out, "\n"); |
| 1330 | fprintf(out, "} // namespace android\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1334 | static void |
| 1335 | print_usage() |
| 1336 | { |
| 1337 | fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n"); |
| 1338 | fprintf(stderr, "\n"); |
| 1339 | fprintf(stderr, "OPTIONS\n"); |
| 1340 | fprintf(stderr, " --cpp FILENAME the header file to output\n"); |
| 1341 | fprintf(stderr, " --header FILENAME the cpp file to output\n"); |
| 1342 | fprintf(stderr, " --help this message\n"); |
| 1343 | fprintf(stderr, " --java FILENAME the java file to output\n"); |
| 1344 | fprintf(stderr, " --jni FILENAME the jni file to output\n"); |
| 1345 | } |
| 1346 | |
| 1347 | /** |
| 1348 | * Do the argument parsing and execute the tasks. |
| 1349 | */ |
| 1350 | static int |
| 1351 | run(int argc, char const*const* argv) |
| 1352 | { |
| 1353 | string cppFilename; |
| 1354 | string headerFilename; |
| 1355 | string javaFilename; |
| 1356 | string jniFilename; |
| 1357 | |
| 1358 | int index = 1; |
| 1359 | while (index < argc) { |
| 1360 | if (0 == strcmp("--help", argv[index])) { |
| 1361 | print_usage(); |
| 1362 | return 0; |
| 1363 | } else if (0 == strcmp("--cpp", argv[index])) { |
| 1364 | index++; |
| 1365 | if (index >= argc) { |
| 1366 | print_usage(); |
| 1367 | return 1; |
| 1368 | } |
| 1369 | cppFilename = argv[index]; |
| 1370 | } else if (0 == strcmp("--header", argv[index])) { |
| 1371 | index++; |
| 1372 | if (index >= argc) { |
| 1373 | print_usage(); |
| 1374 | return 1; |
| 1375 | } |
| 1376 | headerFilename = argv[index]; |
| 1377 | } else if (0 == strcmp("--java", argv[index])) { |
| 1378 | index++; |
| 1379 | if (index >= argc) { |
| 1380 | print_usage(); |
| 1381 | return 1; |
| 1382 | } |
| 1383 | javaFilename = argv[index]; |
| 1384 | } else if (0 == strcmp("--jni", argv[index])) { |
| 1385 | index++; |
| 1386 | if (index >= argc) { |
| 1387 | print_usage(); |
| 1388 | return 1; |
| 1389 | } |
| 1390 | jniFilename = argv[index]; |
| 1391 | } |
| 1392 | index++; |
| 1393 | } |
| 1394 | |
| 1395 | if (cppFilename.size() == 0 |
| 1396 | && headerFilename.size() == 0 |
| 1397 | && javaFilename.size() == 0 |
| 1398 | && jniFilename.size() == 0) { |
| 1399 | print_usage(); |
| 1400 | return 1; |
| 1401 | } |
| 1402 | |
| 1403 | // Collate the parameters |
| 1404 | Atoms atoms; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 1405 | int errorCount = collate_atoms(Atom::descriptor(), &atoms); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1406 | if (errorCount != 0) { |
| 1407 | return 1; |
| 1408 | } |
| 1409 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1410 | AtomDecl attributionDecl; |
| 1411 | vector<java_type_t> attributionSignature; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 1412 | collate_atom(android::os::statsd::AttributionNode::descriptor(), |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1413 | &attributionDecl, &attributionSignature); |
| 1414 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1415 | // Write the .cpp file |
| 1416 | if (cppFilename.size() != 0) { |
| 1417 | FILE* out = fopen(cppFilename.c_str(), "w"); |
| 1418 | if (out == NULL) { |
| 1419 | fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str()); |
| 1420 | return 1; |
| 1421 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1422 | errorCount = android::stats_log_api_gen::write_stats_log_cpp( |
| 1423 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1424 | fclose(out); |
| 1425 | } |
| 1426 | |
| 1427 | // Write the .h file |
| 1428 | if (headerFilename.size() != 0) { |
| 1429 | FILE* out = fopen(headerFilename.c_str(), "w"); |
| 1430 | if (out == NULL) { |
| 1431 | fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str()); |
| 1432 | return 1; |
| 1433 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1434 | errorCount = android::stats_log_api_gen::write_stats_log_header( |
| 1435 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1436 | fclose(out); |
| 1437 | } |
| 1438 | |
| 1439 | // Write the .java file |
| 1440 | if (javaFilename.size() != 0) { |
| 1441 | FILE* out = fopen(javaFilename.c_str(), "w"); |
| 1442 | if (out == NULL) { |
| 1443 | fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str()); |
| 1444 | return 1; |
| 1445 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1446 | errorCount = android::stats_log_api_gen::write_stats_log_java( |
| 1447 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1448 | fclose(out); |
| 1449 | } |
| 1450 | |
| 1451 | // Write the jni file |
| 1452 | if (jniFilename.size() != 0) { |
| 1453 | FILE* out = fopen(jniFilename.c_str(), "w"); |
| 1454 | if (out == NULL) { |
| 1455 | fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str()); |
| 1456 | return 1; |
| 1457 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 1458 | errorCount = android::stats_log_api_gen::write_stats_log_jni( |
| 1459 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1460 | fclose(out); |
| 1461 | } |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | /** |
| 1470 | * Main. |
| 1471 | */ |
| 1472 | int |
| 1473 | main(int argc, char const*const* argv) |
| 1474 | { |
| 1475 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 1476 | |
| 1477 | return android::stats_log_api_gen::run(argc, argv); |
Yao Chen | cf3829a | 2018-06-05 14:20:35 -0700 | [diff] [blame] | 1478 | } |