From b6b3995f17c97e806e70198db180cc65783d49d1 Mon Sep 17 00:00:00 2001 From: Muhammad Qureshi Date: Tue, 14 Jan 2020 14:42:13 -0800 Subject: [PATCH] Add KeyValuePairs support for modules. Add KeyValuePairs support to StatsLog autogen for modules. Bug: 147674597 Test: m Test: fastboot flashall Test: adb logcat "*:S statsd:*" and inspect KeyValuePair atoms. Test: statsd_testdrive 83 Change-Id: Ie40ea52fde494ec11da6b73f430c7a112711b55b --- tools/stats_log_api_gen/java_writer_q.cpp | 232 +++++++++++++++++++++- tools/stats_log_api_gen/utils.h | 1 + 2 files changed, 225 insertions(+), 8 deletions(-) diff --git a/tools/stats_log_api_gen/java_writer_q.cpp b/tools/stats_log_api_gen/java_writer_q.cpp index f8661294821eb..1259a68e96247 100644 --- a/tools/stats_log_api_gen/java_writer_q.cpp +++ b/tools/stats_log_api_gen/java_writer_q.cpp @@ -75,9 +75,7 @@ int write_java_methods_q_schema( java_type_name(chainField.javaType), chainField.name.c_str()); } } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { - // Module logging does not yet support key value pair. - fprintf(stderr, "Module logging does not yet support key value pair.\n"); - continue; + fprintf(out, ", android.util.SparseArray valueMap"); } else { fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); } @@ -161,9 +159,114 @@ int write_java_methods_q_schema( fprintf(out, "%s needed += attrSize;\n", indent.c_str()); break; } + case JAVA_TYPE_KEY_VALUE_PAIR: + { + fprintf(out, + "%s // Calculate bytes needed by Key Value Pairs.\n", + indent.c_str()); + fprintf(out, + "%s final int count = valueMap.size();\n", indent.c_str()); + fprintf(out, + "%s android.util.SparseIntArray intMap = null;\n", indent.c_str()); + fprintf(out, + "%s android.util.SparseLongArray longMap = null;\n", indent.c_str()); + fprintf(out, + "%s android.util.SparseArray stringMap = null;\n", + indent.c_str()); + fprintf(out, + "%s android.util.SparseArray floatMap = null;\n", indent.c_str()); + fprintf(out, + "%s int keyValuePairSize = LIST_TYPE_OVERHEAD * 5;\n", + indent.c_str()); + fprintf(out, + "%s for (int i = 0; i < count; i++) {\n", indent.c_str()); + fprintf(out, + "%s final int key = valueMap.keyAt(i);\n", indent.c_str()); + fprintf(out, + "%s final Object value = valueMap.valueAt(i);\n", + indent.c_str()); + fprintf(out, + "%s if (value instanceof Integer) {\n", indent.c_str()); + fprintf(out, + "%s keyValuePairSize += LIST_TYPE_OVERHEAD\n", + indent.c_str()); + fprintf(out, + "%s + INT_TYPE_SIZE + INT_TYPE_SIZE;\n", + indent.c_str()); + fprintf(out, + "%s if (null == intMap) {\n", indent.c_str()); + fprintf(out, + "%s intMap = new android.util.SparseIntArray();\n", indent.c_str()); + fprintf(out, + "%s }\n", indent.c_str()); + fprintf(out, + "%s intMap.put(key, (Integer) value);\n", indent.c_str()); + fprintf(out, + "%s } else if (value instanceof Long) {\n", indent.c_str()); + fprintf(out, + "%s keyValuePairSize += LIST_TYPE_OVERHEAD\n", + indent.c_str()); + fprintf(out, + "%s + INT_TYPE_SIZE + LONG_TYPE_SIZE;\n", + indent.c_str()); + fprintf(out, + "%s if (null == longMap) {\n", indent.c_str()); + fprintf(out, + "%s longMap = new android.util.SparseLongArray();\n", indent.c_str()); + fprintf(out, + "%s }\n", indent.c_str()); + fprintf(out, + "%s longMap.put(key, (Long) value);\n", indent.c_str()); + fprintf(out, + "%s } else if (value instanceof String) {\n", indent.c_str()); + fprintf(out, + "%s final String str = (value == null) ? \"\" : " + "(String) value;\n", + indent.c_str()); + fprintf(out, + "%s final int len = " + "str.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;\n", + indent.c_str()); + fprintf(out, + "%s keyValuePairSize += LIST_TYPE_OVERHEAD + INT_TYPE_SIZE\n", + indent.c_str()); + fprintf(out, + "%s + STRING_TYPE_OVERHEAD + len;\n", + indent.c_str()); + fprintf(out, + "%s if (null == stringMap) {\n", indent.c_str()); + fprintf(out, + "%s stringMap = new android.util.SparseArray<>();\n", indent.c_str()); + fprintf(out, + "%s }\n", indent.c_str()); + fprintf(out, + "%s stringMap.put(key, str);\n", indent.c_str()); + fprintf(out, + "%s } else if (value instanceof Float) {\n", indent.c_str()); + fprintf(out, + "%s keyValuePairSize += LIST_TYPE_OVERHEAD\n", + indent.c_str()); + fprintf(out, + "%s + INT_TYPE_SIZE + FLOAT_TYPE_SIZE;\n", + indent.c_str()); + fprintf(out, + "%s if (null == floatMap) {\n", indent.c_str()); + fprintf(out, + "%s floatMap = new android.util.SparseArray<>();\n", indent.c_str()); + fprintf(out, + "%s }\n", indent.c_str()); + fprintf(out, + "%s floatMap.put(key, (Float) value);\n", indent.c_str()); + fprintf(out, + "%s }\n", indent.c_str()); + fprintf(out, + "%s }\n", indent.c_str()); + fprintf(out, "%s needed += keyValuePairSize;\n", indent.c_str()); + break; + } default: - // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR. - fprintf(stderr, "Module logging does not yet support key value pair.\n"); + // Unsupported types: OBJECT, DOUBLE. + fprintf(stderr, "Module logging does not yet support Object and Double.\n"); return 1; } argIndex++; @@ -253,10 +356,18 @@ int write_java_methods_q_schema( fprintf(out, "%s pos += attrSize;\n", indent.c_str()); break; } + case JAVA_TYPE_KEY_VALUE_PAIR: + requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT; + requiredHelpers |= JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS; + fprintf(out, + "%s writeKeyValuePairs(buff, pos, intMap, longMap, stringMap, " + "floatMap);\n", indent.c_str()); + fprintf(out, "%s pos += keyValuePairSize;\n", indent.c_str()); + break; default: - // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR. + // Unsupported types: OBJECT, DOUBLE. fprintf(stderr, - "Object, Double, and KeyValuePairs are not supported in module logging"); + "Object and Double are not supported in module logging"); return 1; } argIndex++; @@ -359,6 +470,111 @@ void write_java_helpers_for_q_schema_methods( fprintf(out, "%s}\n", indent.c_str()); fprintf(out, "\n"); } + + if (requiredHelpers & JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS) { + fprintf(out, "%sprivate static void writeKeyValuePairs(byte[] buff, int pos,\n", + indent.c_str()); + fprintf(out, "%s final android.util.SparseIntArray intMap,\n", indent.c_str()); + fprintf(out, "%s final android.util.SparseLongArray longMap,\n", indent.c_str()); + fprintf(out, "%s final android.util.SparseArray stringMap,\n", + indent.c_str()); + fprintf(out, "%s final android.util.SparseArray floatMap) {\n", + indent.c_str()); + + // Start list of lists. + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) 4;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + + // Write integers. + fprintf(out, "%s final int intMapSize = null == intMap ? 0 : intMap.size();\n", + indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) intMapSize;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s for (int i = 0; i < intMapSize; i++) {\n", indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s final int key = intMap.keyAt(i);\n", indent.c_str()); + fprintf(out, "%s final int value = intMap.valueAt(i);\n", indent.c_str()); + fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); + fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyInt(buff, pos + 1, value);\n", indent.c_str()); + fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + + // Write longs. + fprintf(out, "%s final int longMapSize = null == longMap ? 0 : longMap.size();\n", + indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) longMapSize;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s for (int i = 0; i < longMapSize; i++) {\n", indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s final int key = longMap.keyAt(i);\n", indent.c_str()); + fprintf(out, "%s final long value = longMap.valueAt(i);\n", indent.c_str()); + fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); + fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s buff[pos] = LONG_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyLong(buff, pos + 1, value);\n", indent.c_str()); + fprintf(out, "%s pos += LONG_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + + // Write Strings. + fprintf(out, "%s final int stringMapSize = null == stringMap ? 0 : stringMap.size();\n", + indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) stringMapSize;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s for (int i = 0; i < stringMapSize; i++) {\n", indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s final int key = stringMap.keyAt(i);\n", indent.c_str()); + fprintf(out, "%s final String value = stringMap.valueAt(i);\n", indent.c_str()); + fprintf(out, "%s final byte[] valueBytes = " + "value.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n", + indent.c_str()); + fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); + fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s buff[pos] = STRING_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyInt(buff, pos + 1, valueBytes.length);\n", indent.c_str()); + fprintf(out, "%s System.arraycopy(" + "valueBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, valueBytes.length);\n", + indent.c_str()); + fprintf(out, "%s pos += STRING_TYPE_OVERHEAD + valueBytes.length;\n", + indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + + // Write floats. + fprintf(out, "%s final int floatMapSize = null == floatMap ? 0 : floatMap.size();\n", + indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) floatMapSize;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s for (int i = 0; i < floatMapSize; i++) {\n", indent.c_str()); + fprintf(out, "%s buff[pos] = LIST_TYPE;\n", indent.c_str()); + fprintf(out, "%s buff[pos + 1] = (byte) 2;\n", indent.c_str()); + fprintf(out, "%s pos += LIST_TYPE_OVERHEAD;\n", indent.c_str()); + fprintf(out, "%s final int key = floatMap.keyAt(i);\n", indent.c_str()); + fprintf(out, "%s final float value = floatMap.valueAt(i);\n", indent.c_str()); + fprintf(out, "%s buff[pos] = INT_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyInt(buff, pos + 1, key);\n", indent.c_str()); + fprintf(out, "%s pos += INT_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s buff[pos] = FLOAT_TYPE;\n", indent.c_str()); + fprintf(out, "%s copyFloat(buff, pos + 1, value);\n", indent.c_str()); + fprintf(out, "%s pos += FLOAT_TYPE_SIZE;\n", indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, "%s}\n", indent.c_str()); + fprintf(out, "\n"); + } } #if defined(STATS_SCHEMA_LEGACY) @@ -382,7 +598,7 @@ static void write_java_method( java_type_name(chainField.javaType), chainField.name.c_str()); } } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { - fprintf(out, ", android.util.SparseArray value_map"); + fprintf(out, ", android.util.SparseArray valueMap"); } else { fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); } diff --git a/tools/stats_log_api_gen/utils.h b/tools/stats_log_api_gen/utils.h index 50737a68bf89a..cd602e53359ad 100644 --- a/tools/stats_log_api_gen/utils.h +++ b/tools/stats_log_api_gen/utils.h @@ -39,6 +39,7 @@ const string DEFAULT_JAVA_CLASS = "StatsLogInternal"; const int JAVA_MODULE_REQUIRES_FLOAT = 0x01; const int JAVA_MODULE_REQUIRES_ATTRIBUTION = 0x02; +const int JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS = 0x04; string make_constant_name(const string& str);