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