[PATCH v1 3/7] perf cs-etm: Calculate per CPU metadata array size

Leo Yan
2021-01-09T07:45:51Z

The metadata array can be extended over time and the tool, if using the
predefined macro (like CS_ETMV4_PRIV_MAX for ETMv4) as metadata array
size to copy data, it can cause compatible issue within different
versions of perf tool.

E.g. we recorded a data file with an old version tool, afterwards if
use the new version perf tool to parse the file, since the metadata
array has been extended and the macro CS_ETMV4_PRIV_MAX has been
altered, if use it to parse the perf data with old format, this will
lead to mismatch.

To maintain backward compatibility, this patch calculates per CPU
metadata array size on the runtime, the calculation is based on the
info stored in the data file so that it's reliable.

Signed-off-by: Leo Yan <leo.yan@linaro.org>
---
tools/perf/util/cs-etm.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index a2a369e2fbb6..5e284725dceb 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -2497,6 +2497,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
int i, j, k;
u64 *ptr, *hdr = NULL;
u64 **metadata = NULL;
+ int metadata_cpu_array_size;

/*
* sizeof(auxtrace_info_event::type) +
@@ -2544,6 +2545,19 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
goto err_free_traceid_list;
}

+ /*
+ * The metadata is a two dimensional array, the first dimension uses CPU
+ * number as index and the second dimension is the metadata array per
+ * CPU. Since the metadata array can be extended over time, the
+ * predefined macros (CS_ETM_PRIV_MAX or CS_ETMV4_PRIV_MAX) might
+ * mismatch within different versions of tool, this can lead to copy
+ * wrong data. To maintain backward compatibility, calculate CPU's
+ * metadata array size on the runtime.
+ */
+ metadata_cpu_array_size =
+ (auxtrace_info->header.size -
+ sizeof(struct perf_record_auxtrace_info)) / num_cpu / sizeof(u64);
+
/*
* The metadata is stored in the auxtrace_info section and encodes
* the configuration of the ARM embedded trace macrocell which is
@@ -2558,12 +2572,12 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
err = -ENOMEM;
goto err_free_metadata;
}
- for (k = 0; k < CS_ETM_PRIV_MAX; k++)
+ for (k = 0; k < metadata_cpu_array_size; k++)
metadata[j][k] = ptr[i + k];

/* The traceID is our handle */
idx = metadata[j][CS_ETM_ETMTRACEIDR];
- i += CS_ETM_PRIV_MAX;
+ i += metadata_cpu_array_size;
} else if (ptr[i] == __perf_cs_etmv4_magic) {
metadata[j] = zalloc(sizeof(*metadata[j]) *
CS_ETMV4_PRIV_MAX);
@@ -2571,12 +2585,12 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
err = -ENOMEM;
goto err_free_metadata;
}
- for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
+ for (k = 0; k < metadata_cpu_array_size; k++)
metadata[j][k] = ptr[i + k];

/* The traceID is our handle */
idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
- i += CS_ETMV4_PRIV_MAX;
+ i += metadata_cpu_array_size;
}

/* Get an RB node for this CPU */
--
2.25.1