Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "java_writer_q.h" |
| 18 | #include "utils.h" |
| 19 | |
| 20 | namespace android { |
| 21 | namespace stats_log_api_gen { |
| 22 | |
| 23 | void write_java_q_logging_constants(FILE* out, const string& indent) { |
| 24 | fprintf(out, "%s// Payload limits.\n", indent.c_str()); |
| 25 | fprintf(out, "%sprivate static final int LOGGER_ENTRY_MAX_PAYLOAD = 4068;\n", indent.c_str()); |
| 26 | fprintf(out, |
| 27 | "%sprivate static final int MAX_EVENT_PAYLOAD = LOGGER_ENTRY_MAX_PAYLOAD - 4;\n", |
| 28 | indent.c_str()); |
| 29 | |
| 30 | // Value types. Must match with EventLog.java and log.h. |
| 31 | fprintf(out, "\n"); |
| 32 | fprintf(out, "%s// Value types.\n", indent.c_str()); |
| 33 | fprintf(out, "%sprivate static final byte INT_TYPE = 0;\n", indent.c_str()); |
| 34 | fprintf(out, "%sprivate static final byte LONG_TYPE = 1;\n", indent.c_str()); |
| 35 | fprintf(out, "%sprivate static final byte STRING_TYPE = 2;\n", indent.c_str()); |
| 36 | fprintf(out, "%sprivate static final byte LIST_TYPE = 3;\n", indent.c_str()); |
| 37 | fprintf(out, "%sprivate static final byte FLOAT_TYPE = 4;\n", indent.c_str()); |
| 38 | |
| 39 | // Size of each value type. |
| 40 | // Booleans, ints, floats, and enums take 5 bytes, 1 for the type and 4 for the value. |
| 41 | fprintf(out, "\n"); |
| 42 | fprintf(out, "%s// Size of each value type.\n", indent.c_str()); |
| 43 | fprintf(out, "%sprivate static final int INT_TYPE_SIZE = 5;\n", indent.c_str()); |
| 44 | fprintf(out, "%sprivate static final int FLOAT_TYPE_SIZE = 5;\n", indent.c_str()); |
| 45 | // Longs take 9 bytes, 1 for the type and 8 for the value. |
| 46 | fprintf(out, "%sprivate static final int LONG_TYPE_SIZE = 9;\n", indent.c_str()); |
| 47 | // Strings take 5 metadata bytes: 1 byte is for the type, 4 are for the length. |
| 48 | fprintf(out, "%sprivate static final int STRING_TYPE_OVERHEAD = 5;\n", indent.c_str()); |
| 49 | fprintf(out, "%sprivate static final int LIST_TYPE_OVERHEAD = 2;\n", indent.c_str()); |
| 50 | } |
| 51 | |
| 52 | int write_java_methods_q_schema( |
| 53 | FILE* out, |
| 54 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 55 | const AtomDecl &attributionDecl, |
| 56 | const string& moduleName, |
| 57 | const string& indent) { |
| 58 | int requiredHelpers = 0; |
| 59 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 60 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 61 | // Skip if this signature is not needed for the module. |
| 62 | if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) { |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | // Print method signature. |
| 67 | vector<java_type_t> signature = signature_to_modules_it->first; |
| 68 | fprintf(out, "%spublic static void write(int code", indent.c_str()); |
| 69 | int argIndex = 1; |
| 70 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 71 | arg != signature.end(); arg++) { |
| 72 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 73 | for (auto chainField : attributionDecl.fields) { |
| 74 | fprintf(out, ", %s[] %s", |
| 75 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 76 | } |
| 77 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
| 78 | // Module logging does not yet support key value pair. |
| 79 | fprintf(stderr, "Module logging does not yet support key value pair.\n"); |
| 80 | continue; |
| 81 | } else { |
| 82 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 83 | } |
| 84 | argIndex++; |
| 85 | } |
| 86 | fprintf(out, ") {\n"); |
| 87 | |
| 88 | // Calculate the size of the buffer. |
| 89 | fprintf(out, "%s // Initial overhead of the list, timestamp, and atom tag.\n", |
| 90 | indent.c_str()); |
| 91 | fprintf(out, |
| 92 | "%s int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + INT_TYPE_SIZE;\n", |
| 93 | indent.c_str()); |
| 94 | argIndex = 1; |
| 95 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 96 | arg != signature.end(); arg++) { |
| 97 | switch (*arg) { |
| 98 | case JAVA_TYPE_BOOLEAN: |
| 99 | case JAVA_TYPE_INT: |
| 100 | case JAVA_TYPE_FLOAT: |
| 101 | case JAVA_TYPE_ENUM: |
| 102 | fprintf(out, "%s needed += INT_TYPE_SIZE;\n", indent.c_str()); |
| 103 | break; |
| 104 | case JAVA_TYPE_LONG: |
| 105 | // Longs take 9 bytes, 1 for the type and 8 for the value. |
| 106 | fprintf(out, "%s needed += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 107 | break; |
| 108 | case JAVA_TYPE_STRING: |
| 109 | // Strings take 5 metadata bytes + length of byte encoded string. |
| 110 | fprintf(out, "%s if (arg%d == null) {\n", indent.c_str(), argIndex); |
| 111 | fprintf(out, "%s arg%d = \"\";\n", indent.c_str(), argIndex); |
| 112 | fprintf(out, "%s }\n", indent.c_str()); |
| 113 | fprintf(out, |
| 114 | "%s byte[] arg%dBytes = " |
| 115 | "arg%d.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n", |
| 116 | indent.c_str(), argIndex, argIndex); |
| 117 | fprintf(out, "%s needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n", |
| 118 | indent.c_str(), argIndex); |
| 119 | break; |
| 120 | case JAVA_TYPE_BYTE_ARRAY: |
| 121 | // Byte arrays take 5 metadata bytes + length of byte array. |
| 122 | fprintf(out, "%s if (arg%d == null) {\n", indent.c_str(), argIndex); |
| 123 | fprintf(out, "%s arg%d = new byte[0];\n", indent.c_str(), argIndex); |
| 124 | fprintf(out, "%s }\n", indent.c_str()); |
| 125 | fprintf(out, "%s needed += STRING_TYPE_OVERHEAD + arg%d.length;\n", |
| 126 | indent.c_str(), argIndex); |
| 127 | break; |
| 128 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 129 | { |
| 130 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 131 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 132 | // Null checks on the params. |
| 133 | fprintf(out, "%s if (%s == null) {\n", indent.c_str(), uidName); |
| 134 | fprintf(out, "%s %s = new %s[0];\n", indent.c_str(), uidName, |
| 135 | java_type_name(attributionDecl.fields.front().javaType)); |
| 136 | fprintf(out, "%s }\n", indent.c_str()); |
| 137 | fprintf(out, "%s if (%s == null) {\n", indent.c_str(), tagName); |
| 138 | fprintf(out, "%s %s = new %s[0];\n", indent.c_str(), tagName, |
| 139 | java_type_name(attributionDecl.fields.back().javaType)); |
| 140 | fprintf(out, "%s }\n", indent.c_str()); |
| 141 | |
| 142 | // First check that the lengths of the uid and tag arrays are the same. |
| 143 | fprintf(out, "%s if (%s.length != %s.length) {\n", |
| 144 | indent.c_str(), uidName, tagName); |
| 145 | fprintf(out, "%s return;\n", indent.c_str()); |
| 146 | fprintf(out, "%s }\n", indent.c_str()); |
| 147 | fprintf(out, "%s int attrSize = LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 148 | fprintf(out, "%s for (int i = 0; i < %s.length; i++) {\n", |
| 149 | indent.c_str(), tagName); |
| 150 | fprintf(out, "%s String str%d = (%s[i] == null) ? \"\" : %s[i];\n", |
| 151 | indent.c_str(), argIndex, tagName, tagName); |
| 152 | fprintf(out, |
| 153 | "%s int str%dlen = " |
| 154 | "str%d.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;\n", |
| 155 | indent.c_str(), argIndex, argIndex); |
| 156 | fprintf(out, |
| 157 | "%s attrSize += " |
| 158 | "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + str%dlen;\n", |
| 159 | indent.c_str(), argIndex); |
| 160 | fprintf(out, "%s }\n", indent.c_str()); |
| 161 | fprintf(out, "%s needed += attrSize;\n", indent.c_str()); |
| 162 | break; |
| 163 | } |
| 164 | default: |
| 165 | // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR. |
| 166 | fprintf(stderr, "Module logging does not yet support key value pair.\n"); |
| 167 | return 1; |
| 168 | } |
| 169 | argIndex++; |
| 170 | } |
| 171 | |
| 172 | // Now we have the size that is needed. Check for overflow and return if needed. |
| 173 | fprintf(out, "%s if (needed > MAX_EVENT_PAYLOAD) {\n", indent.c_str()); |
| 174 | fprintf(out, "%s return;\n", indent.c_str()); |
| 175 | fprintf(out, "%s }\n", indent.c_str()); |
| 176 | |
| 177 | // Create new buffer, and associated data types. |
| 178 | fprintf(out, "%s byte[] buff = new byte[needed];\n", indent.c_str()); |
| 179 | fprintf(out, "%s int pos = 0;\n", indent.c_str()); |
| 180 | |
| 181 | // Initialize the buffer with list data type. |
| 182 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 183 | fprintf(out, "%s buff[pos + 1] = %zu;\n", indent.c_str(), signature.size() + 2); |
| 184 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 185 | |
| 186 | // Write timestamp. |
| 187 | fprintf(out, "%s long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n", indent.c_str()); |
| 188 | fprintf(out, "%s buff[pos] = LONG_TYPE;\n", indent.c_str()); |
| 189 | fprintf(out, "%s copyLong(buff, pos + 1, elapsedRealtime);\n", indent.c_str()); |
| 190 | fprintf(out, "%s pos += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 191 | |
| 192 | // Write atom code. |
| 193 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 194 | fprintf(out, "%s copyInt(buff, pos + 1, code);\n", indent.c_str()); |
| 195 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 196 | |
| 197 | // Write the args. |
| 198 | argIndex = 1; |
| 199 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 200 | arg != signature.end(); arg++) { |
| 201 | switch (*arg) { |
| 202 | case JAVA_TYPE_BOOLEAN: |
| 203 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 204 | fprintf(out, "%s copyInt(buff, pos + 1, arg%d? 1 : 0);\n", |
| 205 | indent.c_str(), argIndex); |
| 206 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 207 | break; |
| 208 | case JAVA_TYPE_INT: |
| 209 | case JAVA_TYPE_ENUM: |
| 210 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 211 | fprintf(out, "%s copyInt(buff, pos + 1, arg%d);\n", indent.c_str(), argIndex); |
| 212 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 213 | break; |
| 214 | case JAVA_TYPE_FLOAT: |
| 215 | requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT; |
| 216 | fprintf(out, "%s buff[pos] = FLOAT_TYPE;\n", indent.c_str()); |
| 217 | fprintf(out, "%s copyFloat(buff, pos + 1, arg%d);\n", indent.c_str(), argIndex); |
| 218 | fprintf(out, "%s pos += FLOAT_TYPE_SIZE;\n", indent.c_str()); |
| 219 | break; |
| 220 | case JAVA_TYPE_LONG: |
| 221 | fprintf(out, "%s buff[pos] = LONG_TYPE;\n", indent.c_str()); |
| 222 | fprintf(out, "%s copyLong(buff, pos + 1, arg%d);\n", indent.c_str(), argIndex); |
| 223 | fprintf(out, "%s pos += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 224 | break; |
| 225 | case JAVA_TYPE_STRING: |
| 226 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 227 | fprintf(out, "%s copyInt(buff, pos + 1, arg%dBytes.length);\n", |
| 228 | indent.c_str(), argIndex); |
| 229 | fprintf(out, "%s System.arraycopy(" |
| 230 | "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%dBytes.length);\n", |
| 231 | indent.c_str(), argIndex, argIndex); |
| 232 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n", |
| 233 | indent.c_str(), argIndex); |
| 234 | break; |
| 235 | case JAVA_TYPE_BYTE_ARRAY: |
| 236 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 237 | fprintf(out, "%s copyInt(buff, pos + 1, arg%d.length);\n", |
| 238 | indent.c_str(), argIndex); |
| 239 | fprintf(out, "%s System.arraycopy(" |
| 240 | "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n", |
| 241 | indent.c_str(), argIndex, argIndex); |
| 242 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + arg%d.length;\n", |
| 243 | indent.c_str(), argIndex); |
| 244 | break; |
| 245 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 246 | { |
| 247 | requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION; |
| 248 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 249 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 250 | |
| 251 | fprintf(out, "%s writeAttributionChain(buff, pos, %s, %s);\n", indent.c_str(), |
| 252 | uidName, tagName); |
| 253 | fprintf(out, "%s pos += attrSize;\n", indent.c_str()); |
| 254 | break; |
| 255 | } |
| 256 | default: |
| 257 | // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR. |
| 258 | fprintf(stderr, |
| 259 | "Object, Double, and KeyValuePairs are not supported in module logging"); |
| 260 | return 1; |
| 261 | } |
| 262 | argIndex++; |
| 263 | } |
| 264 | |
| 265 | fprintf(out, "%s StatsLog.writeRaw(buff, pos);\n", indent.c_str()); |
| 266 | fprintf(out, "%s}\n", indent.c_str()); |
| 267 | fprintf(out, "\n"); |
| 268 | } |
| 269 | |
| 270 | write_java_helpers_for_q_schema_methods(out, attributionDecl, requiredHelpers, indent); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | void write_java_helpers_for_q_schema_methods( |
| 276 | FILE* out, |
| 277 | const AtomDecl &attributionDecl, |
| 278 | const int requiredHelpers, |
| 279 | const string& indent) { |
| 280 | fprintf(out, "\n"); |
| 281 | fprintf(out, "%s// Helper methods for copying primitives\n", indent.c_str()); |
| 282 | fprintf(out, "%sprivate static void copyInt(byte[] buff, int pos, int val) {\n", |
| 283 | indent.c_str()); |
| 284 | fprintf(out, "%s buff[pos] = (byte) (val);\n", indent.c_str()); |
| 285 | fprintf(out, "%s buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str()); |
| 286 | fprintf(out, "%s buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str()); |
| 287 | fprintf(out, "%s buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str()); |
| 288 | fprintf(out, "%s return;\n", indent.c_str()); |
| 289 | fprintf(out, "%s}\n", indent.c_str()); |
| 290 | fprintf(out, "\n"); |
| 291 | |
| 292 | fprintf(out, "%sprivate static void copyLong(byte[] buff, int pos, long val) {\n", |
| 293 | indent.c_str()); |
| 294 | fprintf(out, "%s buff[pos] = (byte) (val);\n", indent.c_str()); |
| 295 | fprintf(out, "%s buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str()); |
| 296 | fprintf(out, "%s buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str()); |
| 297 | fprintf(out, "%s buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str()); |
| 298 | fprintf(out, "%s buff[pos + 4] = (byte) (val >> 32);\n", indent.c_str()); |
| 299 | fprintf(out, "%s buff[pos + 5] = (byte) (val >> 40);\n", indent.c_str()); |
| 300 | fprintf(out, "%s buff[pos + 6] = (byte) (val >> 48);\n", indent.c_str()); |
| 301 | fprintf(out, "%s buff[pos + 7] = (byte) (val >> 56);\n", indent.c_str()); |
| 302 | fprintf(out, "%s return;\n", indent.c_str()); |
| 303 | fprintf(out, "%s}\n", indent.c_str()); |
| 304 | fprintf(out, "\n"); |
| 305 | |
| 306 | if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) { |
| 307 | fprintf(out, "%sprivate static void copyFloat(byte[] buff, int pos, float val) {\n", |
| 308 | indent.c_str()); |
| 309 | fprintf(out, "%s copyInt(buff, pos, Float.floatToIntBits(val));\n", indent.c_str()); |
| 310 | fprintf(out, "%s return;\n", indent.c_str()); |
| 311 | fprintf(out, "%s}\n", indent.c_str()); |
| 312 | fprintf(out, "\n"); |
| 313 | } |
| 314 | |
| 315 | if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) { |
| 316 | fprintf(out, "%sprivate static void writeAttributionChain(byte[] buff, int pos", |
| 317 | indent.c_str()); |
| 318 | for (auto chainField : attributionDecl.fields) { |
| 319 | fprintf(out, ", %s[] %s", |
| 320 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 321 | } |
| 322 | fprintf(out, ") {\n"); |
| 323 | |
| 324 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 325 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 326 | |
| 327 | // Write the first list begin. |
| 328 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 329 | fprintf(out, "%s buff[pos + 1] = (byte) (%s.length);\n", indent.c_str(), tagName); |
| 330 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 331 | |
| 332 | // Iterate through the attribution chain and write the nodes. |
| 333 | fprintf(out, "%s for (int i = 0; i < %s.length; i++) {\n", indent.c_str(), tagName); |
| 334 | // Write the list begin. |
| 335 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 336 | fprintf(out, "%s buff[pos + 1] = %lu;\n", |
| 337 | indent.c_str(), attributionDecl.fields.size()); |
| 338 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 339 | |
| 340 | // Write the uid. |
| 341 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 342 | fprintf(out, "%s copyInt(buff, pos + 1, %s[i]);\n", indent.c_str(), uidName); |
| 343 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 344 | |
| 345 | // Write the tag. |
| 346 | fprintf(out, "%s String %sStr = (%s[i] == null) ? \"\" : %s[i];\n", |
| 347 | indent.c_str(), tagName, tagName, tagName); |
| 348 | fprintf(out, "%s byte[] %sByte = " |
| 349 | "%sStr.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n", |
| 350 | indent.c_str(), tagName, tagName); |
| 351 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 352 | fprintf(out, "%s copyInt(buff, pos + 1, %sByte.length);\n", indent.c_str(), tagName); |
| 353 | fprintf(out, "%s System.arraycopy(" |
| 354 | "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n", |
| 355 | indent.c_str(), tagName, tagName); |
| 356 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", |
| 357 | indent.c_str(), tagName); |
| 358 | fprintf(out, "%s }\n", indent.c_str()); |
| 359 | fprintf(out, "%s}\n", indent.c_str()); |
| 360 | fprintf(out, "\n"); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | #if defined(STATS_SCHEMA_LEGACY) |
| 365 | static void write_java_method( |
| 366 | FILE* out, |
| 367 | const string& method_name, |
| 368 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 369 | const AtomDecl &attributionDecl) { |
| 370 | |
| 371 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 372 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 373 | vector<java_type_t> signature = signature_to_modules_it->first; |
| 374 | fprintf(out, " /** @hide */\n"); |
| 375 | fprintf(out, " public static native int %s(int code", method_name.c_str()); |
| 376 | int argIndex = 1; |
| 377 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 378 | arg != signature.end(); arg++) { |
| 379 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 380 | for (auto chainField : attributionDecl.fields) { |
| 381 | fprintf(out, ", %s[] %s", |
| 382 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 383 | } |
| 384 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 385 | fprintf(out, ", android.util.SparseArray<Object> value_map"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 386 | } else { |
| 387 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 388 | } |
| 389 | argIndex++; |
| 390 | } |
| 391 | fprintf(out, ");\n"); |
| 392 | fprintf(out, "\n"); |
| 393 | } |
| 394 | } |
| 395 | |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 396 | int write_stats_log_java_q(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl, |
| 397 | const bool supportWorkSource) { |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 398 | // Print prelude |
| 399 | fprintf(out, "// This file is autogenerated\n"); |
| 400 | fprintf(out, "\n"); |
| 401 | fprintf(out, "package android.util;\n"); |
| 402 | fprintf(out, "\n"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 403 | fprintf(out, "\n"); |
| 404 | fprintf(out, "/**\n"); |
| 405 | fprintf(out, " * API For logging statistics events.\n"); |
| 406 | fprintf(out, " * @hide\n"); |
| 407 | fprintf(out, " */\n"); |
| 408 | fprintf(out, "public class StatsLogInternal {\n"); |
| 409 | write_java_atom_codes(out, atoms, DEFAULT_MODULE_NAME); |
| 410 | |
| 411 | write_java_enum_values(out, atoms, DEFAULT_MODULE_NAME); |
| 412 | |
| 413 | // Print write methods |
| 414 | fprintf(out, " // Write methods\n"); |
| 415 | write_java_method(out, "write", atoms.signatures_to_modules, attributionDecl); |
| 416 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures_to_modules, |
| 417 | attributionDecl); |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 418 | if (supportWorkSource) { |
| 419 | write_java_work_source_methods(out, atoms.signatures_to_modules, DEFAULT_MODULE_NAME); |
| 420 | } |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 421 | |
| 422 | fprintf(out, "}\n"); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 427 | int write_stats_log_java_q_for_module(FILE* out, const Atoms& atoms, |
| 428 | const AtomDecl &attributionDecl, const string& moduleName, |
| 429 | const string& javaClass, const string& javaPackage, |
| 430 | const bool supportWorkSource) { |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 431 | // Print prelude |
| 432 | fprintf(out, "// This file is autogenerated\n"); |
| 433 | fprintf(out, "\n"); |
| 434 | fprintf(out, "package %s;\n", javaPackage.c_str()); |
| 435 | fprintf(out, "\n"); |
| 436 | fprintf(out, "import static java.nio.charset.StandardCharsets.UTF_8;\n"); |
| 437 | fprintf(out, "\n"); |
| 438 | fprintf(out, "import android.util.StatsLog;\n"); |
| 439 | fprintf(out, "import android.os.SystemClock;\n"); |
| 440 | fprintf(out, "\n"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 441 | fprintf(out, "\n"); |
| 442 | fprintf(out, "/**\n"); |
| 443 | fprintf(out, " * Utility class for logging statistics events.\n"); |
| 444 | fprintf(out, " */\n"); |
| 445 | fprintf(out, "public class %s {\n", javaClass.c_str()); |
| 446 | |
| 447 | write_java_q_logging_constants(out, " "); |
| 448 | |
| 449 | write_java_atom_codes(out, atoms, moduleName); |
| 450 | |
| 451 | write_java_enum_values(out, atoms, moduleName); |
| 452 | |
| 453 | int errors = 0; |
| 454 | // Print write methods |
| 455 | fprintf(out, " // Write methods\n"); |
| 456 | errors += write_java_methods_q_schema(out, atoms.signatures_to_modules, attributionDecl, |
| 457 | moduleName, " "); |
| 458 | errors += write_java_non_chained_methods(out, atoms.non_chained_signatures_to_modules, |
| 459 | moduleName); |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 460 | if (supportWorkSource) { |
| 461 | errors += write_java_work_source_methods(out, atoms.signatures_to_modules, moduleName); |
| 462 | } |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 463 | |
| 464 | fprintf(out, "}\n"); |
| 465 | |
| 466 | return errors; |
| 467 | } |
| 468 | #endif |
| 469 | |
| 470 | } // namespace stats_log_api_gen |
| 471 | } // namespace android |