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) { |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 78 | fprintf(out, ", android.util.SparseArray<Object> valueMap"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 79 | } else { |
| 80 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 81 | } |
| 82 | argIndex++; |
| 83 | } |
| 84 | fprintf(out, ") {\n"); |
| 85 | |
| 86 | // Calculate the size of the buffer. |
| 87 | fprintf(out, "%s // Initial overhead of the list, timestamp, and atom tag.\n", |
| 88 | indent.c_str()); |
| 89 | fprintf(out, |
| 90 | "%s int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + INT_TYPE_SIZE;\n", |
| 91 | indent.c_str()); |
| 92 | argIndex = 1; |
| 93 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 94 | arg != signature.end(); arg++) { |
| 95 | switch (*arg) { |
| 96 | case JAVA_TYPE_BOOLEAN: |
| 97 | case JAVA_TYPE_INT: |
| 98 | case JAVA_TYPE_FLOAT: |
| 99 | case JAVA_TYPE_ENUM: |
| 100 | fprintf(out, "%s needed += INT_TYPE_SIZE;\n", indent.c_str()); |
| 101 | break; |
| 102 | case JAVA_TYPE_LONG: |
| 103 | // Longs take 9 bytes, 1 for the type and 8 for the value. |
| 104 | fprintf(out, "%s needed += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 105 | break; |
| 106 | case JAVA_TYPE_STRING: |
| 107 | // Strings take 5 metadata bytes + length of byte encoded string. |
| 108 | fprintf(out, "%s if (arg%d == null) {\n", indent.c_str(), argIndex); |
| 109 | fprintf(out, "%s arg%d = \"\";\n", indent.c_str(), argIndex); |
| 110 | fprintf(out, "%s }\n", indent.c_str()); |
| 111 | fprintf(out, |
| 112 | "%s byte[] arg%dBytes = " |
| 113 | "arg%d.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n", |
| 114 | indent.c_str(), argIndex, argIndex); |
| 115 | fprintf(out, "%s needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n", |
| 116 | indent.c_str(), argIndex); |
| 117 | break; |
| 118 | case JAVA_TYPE_BYTE_ARRAY: |
| 119 | // Byte arrays take 5 metadata bytes + length of byte array. |
| 120 | fprintf(out, "%s if (arg%d == null) {\n", indent.c_str(), argIndex); |
| 121 | fprintf(out, "%s arg%d = new byte[0];\n", indent.c_str(), argIndex); |
| 122 | fprintf(out, "%s }\n", indent.c_str()); |
| 123 | fprintf(out, "%s needed += STRING_TYPE_OVERHEAD + arg%d.length;\n", |
| 124 | indent.c_str(), argIndex); |
| 125 | break; |
| 126 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 127 | { |
| 128 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 129 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 130 | // Null checks on the params. |
| 131 | fprintf(out, "%s if (%s == null) {\n", indent.c_str(), uidName); |
| 132 | fprintf(out, "%s %s = new %s[0];\n", indent.c_str(), uidName, |
| 133 | java_type_name(attributionDecl.fields.front().javaType)); |
| 134 | fprintf(out, "%s }\n", indent.c_str()); |
| 135 | fprintf(out, "%s if (%s == null) {\n", indent.c_str(), tagName); |
| 136 | fprintf(out, "%s %s = new %s[0];\n", indent.c_str(), tagName, |
| 137 | java_type_name(attributionDecl.fields.back().javaType)); |
| 138 | fprintf(out, "%s }\n", indent.c_str()); |
| 139 | |
| 140 | // First check that the lengths of the uid and tag arrays are the same. |
| 141 | fprintf(out, "%s if (%s.length != %s.length) {\n", |
| 142 | indent.c_str(), uidName, tagName); |
| 143 | fprintf(out, "%s return;\n", indent.c_str()); |
| 144 | fprintf(out, "%s }\n", indent.c_str()); |
| 145 | fprintf(out, "%s int attrSize = LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 146 | fprintf(out, "%s for (int i = 0; i < %s.length; i++) {\n", |
| 147 | indent.c_str(), tagName); |
| 148 | fprintf(out, "%s String str%d = (%s[i] == null) ? \"\" : %s[i];\n", |
| 149 | indent.c_str(), argIndex, tagName, tagName); |
| 150 | fprintf(out, |
| 151 | "%s int str%dlen = " |
| 152 | "str%d.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;\n", |
| 153 | indent.c_str(), argIndex, argIndex); |
| 154 | fprintf(out, |
| 155 | "%s attrSize += " |
| 156 | "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + str%dlen;\n", |
| 157 | indent.c_str(), argIndex); |
| 158 | fprintf(out, "%s }\n", indent.c_str()); |
| 159 | fprintf(out, "%s needed += attrSize;\n", indent.c_str()); |
| 160 | break; |
| 161 | } |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 162 | case JAVA_TYPE_KEY_VALUE_PAIR: |
| 163 | { |
| 164 | fprintf(out, |
| 165 | "%s // Calculate bytes needed by Key Value Pairs.\n", |
| 166 | indent.c_str()); |
| 167 | fprintf(out, |
| 168 | "%s final int count = valueMap.size();\n", indent.c_str()); |
| 169 | fprintf(out, |
| 170 | "%s android.util.SparseIntArray intMap = null;\n", indent.c_str()); |
| 171 | fprintf(out, |
| 172 | "%s android.util.SparseLongArray longMap = null;\n", indent.c_str()); |
| 173 | fprintf(out, |
| 174 | "%s android.util.SparseArray<String> stringMap = null;\n", |
| 175 | indent.c_str()); |
| 176 | fprintf(out, |
| 177 | "%s android.util.SparseArray<Float> floatMap = null;\n", indent.c_str()); |
| 178 | fprintf(out, |
| 179 | "%s int keyValuePairSize = LIST_TYPE_OVERHEAD * 5;\n", |
| 180 | indent.c_str()); |
| 181 | fprintf(out, |
| 182 | "%s for (int i = 0; i < count; i++) {\n", indent.c_str()); |
| 183 | fprintf(out, |
| 184 | "%s final int key = valueMap.keyAt(i);\n", indent.c_str()); |
| 185 | fprintf(out, |
| 186 | "%s final Object value = valueMap.valueAt(i);\n", |
| 187 | indent.c_str()); |
| 188 | fprintf(out, |
| 189 | "%s if (value instanceof Integer) {\n", indent.c_str()); |
| 190 | fprintf(out, |
| 191 | "%s keyValuePairSize += LIST_TYPE_OVERHEAD\n", |
| 192 | indent.c_str()); |
| 193 | fprintf(out, |
| 194 | "%s + INT_TYPE_SIZE + INT_TYPE_SIZE;\n", |
| 195 | indent.c_str()); |
| 196 | fprintf(out, |
| 197 | "%s if (null == intMap) {\n", indent.c_str()); |
| 198 | fprintf(out, |
| 199 | "%s intMap = new android.util.SparseIntArray();\n", indent.c_str()); |
| 200 | fprintf(out, |
| 201 | "%s }\n", indent.c_str()); |
| 202 | fprintf(out, |
| 203 | "%s intMap.put(key, (Integer) value);\n", indent.c_str()); |
| 204 | fprintf(out, |
| 205 | "%s } else if (value instanceof Long) {\n", indent.c_str()); |
| 206 | fprintf(out, |
| 207 | "%s keyValuePairSize += LIST_TYPE_OVERHEAD\n", |
| 208 | indent.c_str()); |
| 209 | fprintf(out, |
| 210 | "%s + INT_TYPE_SIZE + LONG_TYPE_SIZE;\n", |
| 211 | indent.c_str()); |
| 212 | fprintf(out, |
| 213 | "%s if (null == longMap) {\n", indent.c_str()); |
| 214 | fprintf(out, |
| 215 | "%s longMap = new android.util.SparseLongArray();\n", indent.c_str()); |
| 216 | fprintf(out, |
| 217 | "%s }\n", indent.c_str()); |
| 218 | fprintf(out, |
| 219 | "%s longMap.put(key, (Long) value);\n", indent.c_str()); |
| 220 | fprintf(out, |
| 221 | "%s } else if (value instanceof String) {\n", indent.c_str()); |
| 222 | fprintf(out, |
| 223 | "%s final String str = (value == null) ? \"\" : " |
| 224 | "(String) value;\n", |
| 225 | indent.c_str()); |
| 226 | fprintf(out, |
| 227 | "%s final int len = " |
| 228 | "str.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;\n", |
| 229 | indent.c_str()); |
| 230 | fprintf(out, |
| 231 | "%s keyValuePairSize += LIST_TYPE_OVERHEAD + INT_TYPE_SIZE\n", |
| 232 | indent.c_str()); |
| 233 | fprintf(out, |
| 234 | "%s + STRING_TYPE_OVERHEAD + len;\n", |
| 235 | indent.c_str()); |
| 236 | fprintf(out, |
| 237 | "%s if (null == stringMap) {\n", indent.c_str()); |
| 238 | fprintf(out, |
| 239 | "%s stringMap = new android.util.SparseArray<>();\n", indent.c_str()); |
| 240 | fprintf(out, |
| 241 | "%s }\n", indent.c_str()); |
| 242 | fprintf(out, |
| 243 | "%s stringMap.put(key, str);\n", indent.c_str()); |
| 244 | fprintf(out, |
| 245 | "%s } else if (value instanceof Float) {\n", indent.c_str()); |
| 246 | fprintf(out, |
| 247 | "%s keyValuePairSize += LIST_TYPE_OVERHEAD\n", |
| 248 | indent.c_str()); |
| 249 | fprintf(out, |
| 250 | "%s + INT_TYPE_SIZE + FLOAT_TYPE_SIZE;\n", |
| 251 | indent.c_str()); |
| 252 | fprintf(out, |
| 253 | "%s if (null == floatMap) {\n", indent.c_str()); |
| 254 | fprintf(out, |
| 255 | "%s floatMap = new android.util.SparseArray<>();\n", indent.c_str()); |
| 256 | fprintf(out, |
| 257 | "%s }\n", indent.c_str()); |
| 258 | fprintf(out, |
| 259 | "%s floatMap.put(key, (Float) value);\n", indent.c_str()); |
| 260 | fprintf(out, |
| 261 | "%s }\n", indent.c_str()); |
| 262 | fprintf(out, |
| 263 | "%s }\n", indent.c_str()); |
| 264 | fprintf(out, "%s needed += keyValuePairSize;\n", indent.c_str()); |
| 265 | break; |
| 266 | } |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 267 | default: |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 268 | // Unsupported types: OBJECT, DOUBLE. |
| 269 | fprintf(stderr, "Module logging does not yet support Object and Double.\n"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 270 | return 1; |
| 271 | } |
| 272 | argIndex++; |
| 273 | } |
| 274 | |
| 275 | // Now we have the size that is needed. Check for overflow and return if needed. |
| 276 | fprintf(out, "%s if (needed > MAX_EVENT_PAYLOAD) {\n", indent.c_str()); |
| 277 | fprintf(out, "%s return;\n", indent.c_str()); |
| 278 | fprintf(out, "%s }\n", indent.c_str()); |
| 279 | |
| 280 | // Create new buffer, and associated data types. |
| 281 | fprintf(out, "%s byte[] buff = new byte[needed];\n", indent.c_str()); |
| 282 | fprintf(out, "%s int pos = 0;\n", indent.c_str()); |
| 283 | |
| 284 | // Initialize the buffer with list data type. |
| 285 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 286 | fprintf(out, "%s buff[pos + 1] = %zu;\n", indent.c_str(), signature.size() + 2); |
| 287 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 288 | |
| 289 | // Write timestamp. |
| 290 | fprintf(out, "%s long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n", indent.c_str()); |
| 291 | fprintf(out, "%s buff[pos] = LONG_TYPE;\n", indent.c_str()); |
| 292 | fprintf(out, "%s copyLong(buff, pos + 1, elapsedRealtime);\n", indent.c_str()); |
| 293 | fprintf(out, "%s pos += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 294 | |
| 295 | // Write atom code. |
| 296 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 297 | fprintf(out, "%s copyInt(buff, pos + 1, code);\n", indent.c_str()); |
| 298 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 299 | |
| 300 | // Write the args. |
| 301 | argIndex = 1; |
| 302 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 303 | arg != signature.end(); arg++) { |
| 304 | switch (*arg) { |
| 305 | case JAVA_TYPE_BOOLEAN: |
| 306 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 307 | fprintf(out, "%s copyInt(buff, pos + 1, arg%d? 1 : 0);\n", |
| 308 | indent.c_str(), argIndex); |
| 309 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 310 | break; |
| 311 | case JAVA_TYPE_INT: |
| 312 | case JAVA_TYPE_ENUM: |
| 313 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 314 | fprintf(out, "%s copyInt(buff, pos + 1, arg%d);\n", indent.c_str(), argIndex); |
| 315 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 316 | break; |
| 317 | case JAVA_TYPE_FLOAT: |
| 318 | requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT; |
| 319 | fprintf(out, "%s buff[pos] = FLOAT_TYPE;\n", indent.c_str()); |
| 320 | fprintf(out, "%s copyFloat(buff, pos + 1, arg%d);\n", indent.c_str(), argIndex); |
| 321 | fprintf(out, "%s pos += FLOAT_TYPE_SIZE;\n", indent.c_str()); |
| 322 | break; |
| 323 | case JAVA_TYPE_LONG: |
| 324 | fprintf(out, "%s buff[pos] = LONG_TYPE;\n", indent.c_str()); |
| 325 | fprintf(out, "%s copyLong(buff, pos + 1, arg%d);\n", indent.c_str(), argIndex); |
| 326 | fprintf(out, "%s pos += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 327 | break; |
| 328 | case JAVA_TYPE_STRING: |
| 329 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 330 | fprintf(out, "%s copyInt(buff, pos + 1, arg%dBytes.length);\n", |
| 331 | indent.c_str(), argIndex); |
| 332 | fprintf(out, "%s System.arraycopy(" |
| 333 | "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%dBytes.length);\n", |
| 334 | indent.c_str(), argIndex, argIndex); |
| 335 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n", |
| 336 | indent.c_str(), argIndex); |
| 337 | break; |
| 338 | case JAVA_TYPE_BYTE_ARRAY: |
| 339 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 340 | fprintf(out, "%s copyInt(buff, pos + 1, arg%d.length);\n", |
| 341 | indent.c_str(), argIndex); |
| 342 | fprintf(out, "%s System.arraycopy(" |
| 343 | "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n", |
| 344 | indent.c_str(), argIndex, argIndex); |
| 345 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + arg%d.length;\n", |
| 346 | indent.c_str(), argIndex); |
| 347 | break; |
| 348 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 349 | { |
| 350 | requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION; |
| 351 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 352 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 353 | |
| 354 | fprintf(out, "%s writeAttributionChain(buff, pos, %s, %s);\n", indent.c_str(), |
| 355 | uidName, tagName); |
| 356 | fprintf(out, "%s pos += attrSize;\n", indent.c_str()); |
| 357 | break; |
| 358 | } |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 359 | case JAVA_TYPE_KEY_VALUE_PAIR: |
| 360 | requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT; |
| 361 | requiredHelpers |= JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS; |
| 362 | fprintf(out, |
| 363 | "%s writeKeyValuePairs(buff, pos, intMap, longMap, stringMap, " |
| 364 | "floatMap);\n", indent.c_str()); |
| 365 | fprintf(out, "%s pos += keyValuePairSize;\n", indent.c_str()); |
| 366 | break; |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 367 | default: |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 368 | // Unsupported types: OBJECT, DOUBLE. |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 369 | fprintf(stderr, |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 370 | "Object and Double are not supported in module logging"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 371 | return 1; |
| 372 | } |
| 373 | argIndex++; |
| 374 | } |
| 375 | |
| 376 | fprintf(out, "%s StatsLog.writeRaw(buff, pos);\n", indent.c_str()); |
| 377 | fprintf(out, "%s}\n", indent.c_str()); |
| 378 | fprintf(out, "\n"); |
| 379 | } |
| 380 | |
| 381 | write_java_helpers_for_q_schema_methods(out, attributionDecl, requiredHelpers, indent); |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | void write_java_helpers_for_q_schema_methods( |
| 387 | FILE* out, |
| 388 | const AtomDecl &attributionDecl, |
| 389 | const int requiredHelpers, |
| 390 | const string& indent) { |
| 391 | fprintf(out, "\n"); |
| 392 | fprintf(out, "%s// Helper methods for copying primitives\n", indent.c_str()); |
| 393 | fprintf(out, "%sprivate static void copyInt(byte[] buff, int pos, int val) {\n", |
| 394 | indent.c_str()); |
| 395 | fprintf(out, "%s buff[pos] = (byte) (val);\n", indent.c_str()); |
| 396 | fprintf(out, "%s buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str()); |
| 397 | fprintf(out, "%s buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str()); |
| 398 | fprintf(out, "%s buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str()); |
| 399 | fprintf(out, "%s return;\n", indent.c_str()); |
| 400 | fprintf(out, "%s}\n", indent.c_str()); |
| 401 | fprintf(out, "\n"); |
| 402 | |
| 403 | fprintf(out, "%sprivate static void copyLong(byte[] buff, int pos, long val) {\n", |
| 404 | indent.c_str()); |
| 405 | fprintf(out, "%s buff[pos] = (byte) (val);\n", indent.c_str()); |
| 406 | fprintf(out, "%s buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str()); |
| 407 | fprintf(out, "%s buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str()); |
| 408 | fprintf(out, "%s buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str()); |
| 409 | fprintf(out, "%s buff[pos + 4] = (byte) (val >> 32);\n", indent.c_str()); |
| 410 | fprintf(out, "%s buff[pos + 5] = (byte) (val >> 40);\n", indent.c_str()); |
| 411 | fprintf(out, "%s buff[pos + 6] = (byte) (val >> 48);\n", indent.c_str()); |
| 412 | fprintf(out, "%s buff[pos + 7] = (byte) (val >> 56);\n", indent.c_str()); |
| 413 | fprintf(out, "%s return;\n", indent.c_str()); |
| 414 | fprintf(out, "%s}\n", indent.c_str()); |
| 415 | fprintf(out, "\n"); |
| 416 | |
| 417 | if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) { |
| 418 | fprintf(out, "%sprivate static void copyFloat(byte[] buff, int pos, float val) {\n", |
| 419 | indent.c_str()); |
| 420 | fprintf(out, "%s copyInt(buff, pos, Float.floatToIntBits(val));\n", indent.c_str()); |
| 421 | fprintf(out, "%s return;\n", indent.c_str()); |
| 422 | fprintf(out, "%s}\n", indent.c_str()); |
| 423 | fprintf(out, "\n"); |
| 424 | } |
| 425 | |
| 426 | if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) { |
| 427 | fprintf(out, "%sprivate static void writeAttributionChain(byte[] buff, int pos", |
| 428 | indent.c_str()); |
| 429 | for (auto chainField : attributionDecl.fields) { |
| 430 | fprintf(out, ", %s[] %s", |
| 431 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 432 | } |
| 433 | fprintf(out, ") {\n"); |
| 434 | |
| 435 | const char* uidName = attributionDecl.fields.front().name.c_str(); |
| 436 | const char* tagName = attributionDecl.fields.back().name.c_str(); |
| 437 | |
| 438 | // Write the first list begin. |
| 439 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 440 | fprintf(out, "%s buff[pos + 1] = (byte) (%s.length);\n", indent.c_str(), tagName); |
| 441 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 442 | |
| 443 | // Iterate through the attribution chain and write the nodes. |
| 444 | fprintf(out, "%s for (int i = 0; i < %s.length; i++) {\n", indent.c_str(), tagName); |
| 445 | // Write the list begin. |
| 446 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 447 | fprintf(out, "%s buff[pos + 1] = %lu;\n", |
| 448 | indent.c_str(), attributionDecl.fields.size()); |
| 449 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 450 | |
| 451 | // Write the uid. |
| 452 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 453 | fprintf(out, "%s copyInt(buff, pos + 1, %s[i]);\n", indent.c_str(), uidName); |
| 454 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 455 | |
| 456 | // Write the tag. |
| 457 | fprintf(out, "%s String %sStr = (%s[i] == null) ? \"\" : %s[i];\n", |
| 458 | indent.c_str(), tagName, tagName, tagName); |
| 459 | fprintf(out, "%s byte[] %sByte = " |
| 460 | "%sStr.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n", |
| 461 | indent.c_str(), tagName, tagName); |
| 462 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 463 | fprintf(out, "%s copyInt(buff, pos + 1, %sByte.length);\n", indent.c_str(), tagName); |
| 464 | fprintf(out, "%s System.arraycopy(" |
| 465 | "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n", |
| 466 | indent.c_str(), tagName, tagName); |
| 467 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", |
| 468 | indent.c_str(), tagName); |
| 469 | fprintf(out, "%s }\n", indent.c_str()); |
| 470 | fprintf(out, "%s}\n", indent.c_str()); |
| 471 | fprintf(out, "\n"); |
| 472 | } |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 473 | |
| 474 | if (requiredHelpers & JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS) { |
| 475 | fprintf(out, "%sprivate static void writeKeyValuePairs(byte[] buff, int pos,\n", |
| 476 | indent.c_str()); |
| 477 | fprintf(out, "%s final android.util.SparseIntArray intMap,\n", indent.c_str()); |
| 478 | fprintf(out, "%s final android.util.SparseLongArray longMap,\n", indent.c_str()); |
| 479 | fprintf(out, "%s final android.util.SparseArray<String> stringMap,\n", |
| 480 | indent.c_str()); |
| 481 | fprintf(out, "%s final android.util.SparseArray<Float> floatMap) {\n", |
| 482 | indent.c_str()); |
| 483 | |
| 484 | // Start list of lists. |
| 485 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 486 | fprintf(out, "%s buff[pos + 1] = (byte) 4;\n", indent.c_str()); |
| 487 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 488 | |
| 489 | // Write integers. |
| 490 | fprintf(out, "%s final int intMapSize = null == intMap ? 0 : intMap.size();\n", |
| 491 | indent.c_str()); |
| 492 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 493 | fprintf(out, "%s buff[pos + 1] = (byte) intMapSize;\n", indent.c_str()); |
| 494 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 495 | fprintf(out, "%s for (int i = 0; i < intMapSize; i++) {\n", indent.c_str()); |
| 496 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 497 | fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); |
| 498 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 499 | fprintf(out, "%s final int key = intMap.keyAt(i);\n", indent.c_str()); |
| 500 | fprintf(out, "%s final int value = intMap.valueAt(i);\n", indent.c_str()); |
| 501 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 502 | fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); |
| 503 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 504 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 505 | fprintf(out, "%s copyInt(buff, pos + 1, value);\n", indent.c_str()); |
| 506 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 507 | fprintf(out, "%s }\n", indent.c_str()); |
| 508 | |
| 509 | // Write longs. |
| 510 | fprintf(out, "%s final int longMapSize = null == longMap ? 0 : longMap.size();\n", |
| 511 | indent.c_str()); |
| 512 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 513 | fprintf(out, "%s buff[pos + 1] = (byte) longMapSize;\n", indent.c_str()); |
| 514 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 515 | fprintf(out, "%s for (int i = 0; i < longMapSize; i++) {\n", indent.c_str()); |
| 516 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 517 | fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); |
| 518 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 519 | fprintf(out, "%s final int key = longMap.keyAt(i);\n", indent.c_str()); |
| 520 | fprintf(out, "%s final long value = longMap.valueAt(i);\n", indent.c_str()); |
| 521 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 522 | fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); |
| 523 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 524 | fprintf(out, "%s buff[pos] = LONG_TYPE;\n", indent.c_str()); |
| 525 | fprintf(out, "%s copyLong(buff, pos + 1, value);\n", indent.c_str()); |
| 526 | fprintf(out, "%s pos += LONG_TYPE_SIZE;\n", indent.c_str()); |
| 527 | fprintf(out, "%s }\n", indent.c_str()); |
| 528 | |
| 529 | // Write Strings. |
| 530 | fprintf(out, "%s final int stringMapSize = null == stringMap ? 0 : stringMap.size();\n", |
| 531 | indent.c_str()); |
| 532 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 533 | fprintf(out, "%s buff[pos + 1] = (byte) stringMapSize;\n", indent.c_str()); |
| 534 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 535 | fprintf(out, "%s for (int i = 0; i < stringMapSize; i++) {\n", indent.c_str()); |
| 536 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 537 | fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); |
| 538 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 539 | fprintf(out, "%s final int key = stringMap.keyAt(i);\n", indent.c_str()); |
| 540 | fprintf(out, "%s final String value = stringMap.valueAt(i);\n", indent.c_str()); |
| 541 | fprintf(out, "%s final byte[] valueBytes = " |
| 542 | "value.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n", |
| 543 | indent.c_str()); |
| 544 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 545 | fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); |
| 546 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 547 | fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); |
| 548 | fprintf(out, "%s copyInt(buff, pos + 1, valueBytes.length);\n", indent.c_str()); |
| 549 | fprintf(out, "%s System.arraycopy(" |
| 550 | "valueBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, valueBytes.length);\n", |
| 551 | indent.c_str()); |
| 552 | fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + valueBytes.length;\n", |
| 553 | indent.c_str()); |
| 554 | fprintf(out, "%s }\n", indent.c_str()); |
| 555 | |
| 556 | // Write floats. |
| 557 | fprintf(out, "%s final int floatMapSize = null == floatMap ? 0 : floatMap.size();\n", |
| 558 | indent.c_str()); |
| 559 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 560 | fprintf(out, "%s buff[pos + 1] = (byte) floatMapSize;\n", indent.c_str()); |
| 561 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 562 | fprintf(out, "%s for (int i = 0; i < floatMapSize; i++) {\n", indent.c_str()); |
| 563 | fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); |
| 564 | fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); |
| 565 | fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); |
| 566 | fprintf(out, "%s final int key = floatMap.keyAt(i);\n", indent.c_str()); |
| 567 | fprintf(out, "%s final float value = floatMap.valueAt(i);\n", indent.c_str()); |
| 568 | fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); |
| 569 | fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); |
| 570 | fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); |
| 571 | fprintf(out, "%s buff[pos] = FLOAT_TYPE;\n", indent.c_str()); |
| 572 | fprintf(out, "%s copyFloat(buff, pos + 1, value);\n", indent.c_str()); |
| 573 | fprintf(out, "%s pos += FLOAT_TYPE_SIZE;\n", indent.c_str()); |
| 574 | fprintf(out, "%s }\n", indent.c_str()); |
| 575 | fprintf(out, "%s}\n", indent.c_str()); |
| 576 | fprintf(out, "\n"); |
| 577 | } |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | #if defined(STATS_SCHEMA_LEGACY) |
| 581 | static void write_java_method( |
| 582 | FILE* out, |
| 583 | const string& method_name, |
| 584 | const map<vector<java_type_t>, set<string>>& signatures_to_modules, |
| 585 | const AtomDecl &attributionDecl) { |
| 586 | |
| 587 | for (auto signature_to_modules_it = signatures_to_modules.begin(); |
| 588 | signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) { |
| 589 | vector<java_type_t> signature = signature_to_modules_it->first; |
| 590 | fprintf(out, " /** @hide */\n"); |
| 591 | fprintf(out, " public static native int %s(int code", method_name.c_str()); |
| 592 | int argIndex = 1; |
| 593 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| 594 | arg != signature.end(); arg++) { |
| 595 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 596 | for (auto chainField : attributionDecl.fields) { |
| 597 | fprintf(out, ", %s[] %s", |
| 598 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 599 | } |
| 600 | } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { |
Muhammad Qureshi | b6b3995 | 2020-01-14 14:42:13 -0800 | [diff] [blame] | 601 | fprintf(out, ", android.util.SparseArray<Object> valueMap"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 602 | } else { |
| 603 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 604 | } |
| 605 | argIndex++; |
| 606 | } |
| 607 | fprintf(out, ");\n"); |
| 608 | fprintf(out, "\n"); |
| 609 | } |
| 610 | } |
| 611 | |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 612 | int write_stats_log_java_q(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl, |
| 613 | const bool supportWorkSource) { |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 614 | // Print prelude |
| 615 | fprintf(out, "// This file is autogenerated\n"); |
| 616 | fprintf(out, "\n"); |
| 617 | fprintf(out, "package android.util;\n"); |
| 618 | fprintf(out, "\n"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 619 | fprintf(out, "\n"); |
| 620 | fprintf(out, "/**\n"); |
| 621 | fprintf(out, " * API For logging statistics events.\n"); |
| 622 | fprintf(out, " * @hide\n"); |
| 623 | fprintf(out, " */\n"); |
| 624 | fprintf(out, "public class StatsLogInternal {\n"); |
| 625 | write_java_atom_codes(out, atoms, DEFAULT_MODULE_NAME); |
| 626 | |
| 627 | write_java_enum_values(out, atoms, DEFAULT_MODULE_NAME); |
| 628 | |
| 629 | // Print write methods |
| 630 | fprintf(out, " // Write methods\n"); |
| 631 | write_java_method(out, "write", atoms.signatures_to_modules, attributionDecl); |
| 632 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures_to_modules, |
| 633 | attributionDecl); |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 634 | if (supportWorkSource) { |
| 635 | write_java_work_source_methods(out, atoms.signatures_to_modules, DEFAULT_MODULE_NAME); |
| 636 | } |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 637 | |
| 638 | fprintf(out, "}\n"); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 643 | int write_stats_log_java_q_for_module(FILE* out, const Atoms& atoms, |
| 644 | const AtomDecl &attributionDecl, const string& moduleName, |
| 645 | const string& javaClass, const string& javaPackage, |
| 646 | const bool supportWorkSource) { |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 647 | // Print prelude |
| 648 | fprintf(out, "// This file is autogenerated\n"); |
| 649 | fprintf(out, "\n"); |
| 650 | fprintf(out, "package %s;\n", javaPackage.c_str()); |
| 651 | fprintf(out, "\n"); |
| 652 | fprintf(out, "import static java.nio.charset.StandardCharsets.UTF_8;\n"); |
| 653 | fprintf(out, "\n"); |
| 654 | fprintf(out, "import android.util.StatsLog;\n"); |
| 655 | fprintf(out, "import android.os.SystemClock;\n"); |
| 656 | fprintf(out, "\n"); |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 657 | fprintf(out, "\n"); |
| 658 | fprintf(out, "/**\n"); |
| 659 | fprintf(out, " * Utility class for logging statistics events.\n"); |
| 660 | fprintf(out, " */\n"); |
| 661 | fprintf(out, "public class %s {\n", javaClass.c_str()); |
| 662 | |
| 663 | write_java_q_logging_constants(out, " "); |
| 664 | |
| 665 | write_java_atom_codes(out, atoms, moduleName); |
| 666 | |
| 667 | write_java_enum_values(out, atoms, moduleName); |
| 668 | |
| 669 | int errors = 0; |
| 670 | // Print write methods |
| 671 | fprintf(out, " // Write methods\n"); |
| 672 | errors += write_java_methods_q_schema(out, atoms.signatures_to_modules, attributionDecl, |
| 673 | moduleName, " "); |
| 674 | errors += write_java_non_chained_methods(out, atoms.non_chained_signatures_to_modules, |
| 675 | moduleName); |
Muhammad Qureshi | ad94425 | 2020-01-10 14:03:13 -0800 | [diff] [blame] | 676 | if (supportWorkSource) { |
| 677 | errors += write_java_work_source_methods(out, atoms.signatures_to_modules, moduleName); |
| 678 | } |
Muhammad Qureshi | 9effe48 | 2019-10-29 15:36:44 -0700 | [diff] [blame] | 679 | |
| 680 | fprintf(out, "}\n"); |
| 681 | |
| 682 | return errors; |
| 683 | } |
| 684 | #endif |
| 685 | |
| 686 | } // namespace stats_log_api_gen |
| 687 | } // namespace android |