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