From: James Clark <james.clark@arm.com>
To: Ian Rogers <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org, renyu.zj@linux.alibaba.com,
john.g.garry@oracle.com, namhyung@kernel.org, acme@kernel.org,
Will Deacon <will@kernel.org>, Mike Leach <mike.leach@linaro.org>,
Leo Yan <leo.yan@linaro.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Nick Forrington <nick.forrington@arm.com>,
Ilkka Koskinen <ilkka@os.amperecomputing.com>,
Kajol Jain <kjain@linux.ibm.com>,
Thomas Richter <tmricht@linux.ibm.com>,
Kan Liang <kan.liang@linux.intel.com>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/5] perf jevents: Match on highest version of Arm json file available
Date: Wed, 12 Jul 2023 14:38:15 +0100 [thread overview]
Message-ID: <516face0-fc2f-453c-df9a-f6ebe47c687f@arm.com> (raw)
In-Reply-To: <CAP-5=fUJavikRkhm601ws+=yB3As1X0Au9Fr-5Z=BemGW9xjyw@mail.gmail.com>
On 12/07/2023 01:39, Ian Rogers wrote:
> On Tue, Jul 11, 2023 at 3:02 AM James Clark <james.clark@arm.com> wrote:
>>
>> Currently variant and revision fields are masked out of the MIDR so
>> there can only be one set of jsons per CPU. In a later commit multiple
>> revisions of Neoverse N2 json files will be provided. This can be used
>> when a change is made to a new CPU version that fixes an event or metric
>> formula.
>>
>> The highest valid version of json files should be used. For example if
>> r0p0 and r1p0 JSON files are both provided, then r0p0 files will be used
>> for all CPU versions up to (not including) r1p0, and then from r1p0
>> onwards the r1p0 files will be used. To make this work the mapfile has
>> to be reverse sorted on the CPUID field so that the highest is found
>> first. It's possible, but error prone, to do this manually so instead
>> add an explicit sort into jevents.py. If the CPUID is a string then the
>> rows are string sorted rather than numerically.
>>
>> Signed-off-by: James Clark <james.clark@arm.com>
>> ---
>> tools/perf/arch/arm64/util/header.c | 61 +++++++++++++++-----
>> tools/perf/pmu-events/arch/arm64/mapfile.csv | 12 +++-
>> tools/perf/pmu-events/jevents.py | 49 +++++++++-------
>> tools/perf/tests/pmu-events.c | 34 +++++++++++
>> 4 files changed, 119 insertions(+), 37 deletions(-)
>>
>> diff --git a/tools/perf/arch/arm64/util/header.c b/tools/perf/arch/arm64/util/header.c
>> index 80b9f6287fe2..637ad21721c2 100644
>> --- a/tools/perf/arch/arm64/util/header.c
>> +++ b/tools/perf/arch/arm64/util/header.c
>> @@ -1,3 +1,6 @@
>> +#include <linux/kernel.h>
>> +#include <linux/bits.h>
>> +#include <linux/bitfield.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <perf/cpumap.h>
>> @@ -10,14 +13,12 @@
>>
>> #define MIDR "/regs/identification/midr_el1"
>> #define MIDR_SIZE 19
>> -#define MIDR_REVISION_MASK 0xf
>> -#define MIDR_VARIANT_SHIFT 20
>> -#define MIDR_VARIANT_MASK (0xf << MIDR_VARIANT_SHIFT)
>> +#define MIDR_REVISION_MASK GENMASK(3, 0)
>> +#define MIDR_VARIANT_MASK GENMASK(23, 20)
>>
>> static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
>> {
>> const char *sysfs = sysfs__mountpoint();
>> - u64 midr = 0;
>> int cpu;
>>
>> if (!sysfs || sz < MIDR_SIZE)
>> @@ -44,21 +45,11 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
>> }
>> fclose(file);
>>
>> - /* Ignore/clear Variant[23:20] and
>> - * Revision[3:0] of MIDR
>> - */
>> - midr = strtoul(buf, NULL, 16);
>> - midr &= (~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK));
>> - scnprintf(buf, MIDR_SIZE, "0x%016lx", midr);
>> /* got midr break loop */
>> break;
>> }
>>
>> perf_cpu_map__put(cpus);
>> -
>> - if (!midr)
>> - return EINVAL;
>> -
>> return 0;
>> }
>>
>> @@ -99,3 +90,45 @@ char *get_cpuid_str(struct perf_pmu *pmu)
>>
>> return buf;
>> }
>> +
>> +
>> +int strcmp_cpuid_str(const char *mapcpuid, const char *idstr)
>> +{
>> + u64 map_id = strtoull(mapcpuid, NULL, 16);
>> + char map_id_variant = FIELD_GET(MIDR_VARIANT_MASK, map_id);
>> + char map_id_revision = FIELD_GET(MIDR_REVISION_MASK, map_id);
>> + u64 id = strtoull(idstr, NULL, 16);
>> + char id_variant = FIELD_GET(MIDR_VARIANT_MASK, id);
>> + char id_revision = FIELD_GET(MIDR_REVISION_MASK, id);
>> + u64 id_fields = ~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK);
>> +
>> + /* Compare without version first */
>> + if ((map_id & id_fields) != (id & id_fields))
>> + return 1;
>> +
>> + /*
>> + * ID matches, now compare version.
>> + *
>> + * Arm revisions (like r0p0) are compared here like two digit semver
>> + * values eg. 1.3 < 2.0 < 2.1 < 2.2. The events json file with the
>> + * highest matching version is used.
>> + *
>> + * r = high value = 'Variant' field in MIDR
>> + * p = low value = 'Revision' field in MIDR
>> + *
>> + * Because the Variant field is further to the left, iterating through a
>> + * reverse sorted mapfile.csv gives the correct comparison behavior.
>> + * This relies on jevents.py sorting the list in print_mapping_table().
>> + */
>> + if (id_variant > map_id_variant)
>> + return 0;
>> +
>> + if (id_variant == map_id_variant && id_revision >= map_id_revision)
>> + return 0;
>> +
>> + /*
>> + * variant is less than mapfile variant or variants are the same but
>> + * the revision doesn't match. Return no match.
>> + */
>> + return 1;
>> +}
>> diff --git a/tools/perf/pmu-events/arch/arm64/mapfile.csv b/tools/perf/pmu-events/arch/arm64/mapfile.csv
>> index 32674ddd2b63..3a90fe650863 100644
>> --- a/tools/perf/pmu-events/arch/arm64/mapfile.csv
>> +++ b/tools/perf/pmu-events/arch/arm64/mapfile.csv
>> @@ -3,7 +3,17 @@
>> #
>> # where
>> # MIDR Processor version
>> -# Variant[23:20] and Revision [3:0] should be zero.
>> +# Variant[23:20] and Revision [3:0] should be set to the
>> +# lowest supported version for that set of JSON files.
>> +# Multiple versions of the same CPU can be provided and
>> +# the highest version JSON files available will be used.
>> +#
>> +# For example for a single set of JSONs, if variant and
>> +# revision are both set to 0 then the JSONs will match all
>> +# CPU versions. If another set is provided with variant
>> +# set to 1, the previous JSONs will match all versions up
>> +# to r1p0, and then r1p0 CPUs and above will start to
>> +# match the next set of JSONs provided.
>> # Version could be used to track version of of JSON file
>> # but currently unused.
>> # JSON/file/pathname is the path to JSON file, relative
>> diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
>> index 12e80bb7939b..c6a848f8d93a 100755
>> --- a/tools/perf/pmu-events/jevents.py
>> +++ b/tools/perf/pmu-events/jevents.py
>> @@ -620,28 +620,34 @@ const struct pmu_events_map pmu_events_map[] = {
>> },
>> """)
>> else:
>> + def int_or_string_key(row):
>> + try:
>> + return int(row[0], 0)
>> + except:
>> + return row[0]
>> with open(f'{_args.starting_dir}/{arch}/mapfile.csv') as csvfile:
>> - table = csv.reader(csvfile)
>> - first = True
>> - for row in table:
>> - # Skip the first row or any row beginning with #.
>> - if not first and len(row) > 0 and not row[0].startswith('#'):
>> - event_tblname = file_name_to_table_name('pmu_events_', [], row[2].replace('/', '_'))
>> - if event_tblname in _event_tables:
>> - event_size = f'ARRAY_SIZE({event_tblname})'
>> - else:
>> - event_tblname = 'NULL'
>> - event_size = '0'
>> - metric_tblname = file_name_to_table_name('pmu_metrics_', [], row[2].replace('/', '_'))
>> - if metric_tblname in _metric_tables:
>> - metric_size = f'ARRAY_SIZE({metric_tblname})'
>> - else:
>> - metric_tblname = 'NULL'
>> - metric_size = '0'
>> - if event_size == '0' and metric_size == '0':
>> - continue
>> - cpuid = row[0].replace('\\', '\\\\')
>> - _args.output_file.write(f"""{{
>> + table = [row for row in csv.reader(csvfile)]
>> + # Strip the first row or any row beginning with #.
>> + table = [row for row in table[1:] if len(row) > 0 and not row[0].startswith('#')]
>> + # Sort on CPUID field for predictable >= version comparisons later on
>> + table = sorted(table, key=int_or_string_key, reverse=True)
>> + for row in table:
>
> nit: kind of weird diff is seeing this as changed, the rest is
> explainable by the switch to the list comprehension.
>
It's just because I copy the whole table first so technically everything
doesn't need to be inside the 'with' block anymore. But it doesn't
really matter to leave it in the block and the diff will be much smaller
so I can undo that change.
>> + event_tblname = file_name_to_table_name('pmu_events_', [], row[2].replace('/', '_'))
>> + if event_tblname in _event_tables:
>> + event_size = f'ARRAY_SIZE({event_tblname})'
>> + else:
>> + event_tblname = 'NULL'
>> + event_size = '0'
>> + metric_tblname = file_name_to_table_name('pmu_metrics_', [], row[2].replace('/', '_'))
>> + if metric_tblname in _metric_tables:
>> + metric_size = f'ARRAY_SIZE({metric_tblname})'
>> + else:
>> + metric_tblname = 'NULL'
>> + metric_size = '0'
>> + if event_size == '0' and metric_size == '0':
>> + continue
>> + cpuid = row[0].replace('\\', '\\\\')
>> + _args.output_file.write(f"""{{
>> \t.arch = "{arch}",
>> \t.cpuid = "{cpuid}",
>> \t.event_table = {{
>> @@ -654,7 +660,6 @@ const struct pmu_events_map pmu_events_map[] = {
>> \t}}
>> }},
>> """)
>> - first = False
>>
>> _args.output_file.write("""{
>> \t.arch = 0,
>> diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c
>> index 64383fc34ef1..e730d4792bbe 100644
>> --- a/tools/perf/tests/pmu-events.c
>> +++ b/tools/perf/tests/pmu-events.c
>> @@ -14,6 +14,7 @@
>> #include "util/evlist.h"
>> #include "util/expr.h"
>> #include "util/hashmap.h"
>> +#include "util/header.h"
>> #include "util/parse-events.h"
>> #include "metricgroup.h"
>> #include "stat.h"
>> @@ -1027,6 +1028,38 @@ static int test__parsing_threshold(struct test_suite *test __maybe_unused,
>> return pmu_for_each_sys_metric(test__parsing_threshold_callback, NULL);
>> }
>>
>> +static int test__cpuid_match(struct test_suite *test __maybe_unused,
>> + int subtest __maybe_unused)
>> +{
>> +#ifdef __aarch64__
>
> This feels like it should be in: tools/perf/arch/arm64/tests/
>
> Thanks,
> Ian
>
Yep that works, I will move it there.
James
>> + /* midr with no leading zeros matches */
>> + if (strcmp_cpuid_str("0x410fd0c0", "0x00000000410fd0c0"))
>> + return -1;
>> + /* Upper case matches */
>> + if (strcmp_cpuid_str("0x410fd0c0", "0x00000000410FD0C0"))
>> + return -1;
>> + /* r0p0 = r0p0 matches */
>> + if (strcmp_cpuid_str("0x00000000410fd480", "0x00000000410fd480"))
>> + return -1;
>> + /* r0p1 > r0p0 matches */
>> + if (strcmp_cpuid_str("0x00000000410fd480", "0x00000000410fd481"))
>> + return -1;
>> + /* r1p0 > r0p0 matches*/
>> + if (strcmp_cpuid_str("0x00000000410fd480", "0x00000000411fd480"))
>> + return -1;
>> + /* r0p0 < r0p1 doesn't match */
>> + if (!strcmp_cpuid_str("0x00000000410fd481", "0x00000000410fd480"))
>> + return -1;
>> + /* r0p0 < r1p0 doesn't match */
>> + if (!strcmp_cpuid_str("0x00000000411fd480", "0x00000000410fd480"))
>> + return -1;
>> + /* Different CPU doesn't match */
>> + if (!strcmp_cpuid_str("0x00000000410fd4c0", "0x00000000430f0af0"))
>> + return -1;
>> +#endif
>> + return 0;
>> +}
>> +
>> static struct test_case pmu_events_tests[] = {
>> TEST_CASE("PMU event table sanity", pmu_event_table),
>> TEST_CASE("PMU event map aliases", aliases),
>> @@ -1034,6 +1067,7 @@ static struct test_case pmu_events_tests[] = {
>> "some metrics failed"),
>> TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
>> TEST_CASE("Parsing of metric thresholds with fake PMUs", parsing_threshold),
>> + TEST_CASE("CPUID matching", cpuid_match),
>> { .name = NULL, }
>> };
>>
>> --
>> 2.34.1
>>
next prev parent reply other threads:[~2023-07-12 13:38 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-11 10:02 [PATCH v3 0/5] perf vendor events arm64: Update N2 and V2 metrics and events using Arm telemetry repo James Clark
2023-07-11 10:02 ` [PATCH v3 1/5] perf: cs-etm: Don't duplicate FIELD_GET() James Clark
2023-07-11 10:02 ` [PATCH v3 2/5] perf jevents: Match on highest version of Arm json file available James Clark
2023-07-12 0:39 ` Ian Rogers
2023-07-12 13:38 ` James Clark [this message]
2023-07-11 10:02 ` [PATCH v3 3/5] perf vendor events arm64: Update scale units and descriptions of common topdown metrics James Clark
2023-07-12 0:43 ` Ian Rogers
2023-07-11 10:02 ` [PATCH v3 4/5] perf vendor events arm64: Update N2-r0p3 and V2 metrics and events using Arm telemetry repo James Clark
2023-07-12 1:04 ` Ian Rogers
2023-07-12 8:33 ` James Clark
2023-07-12 9:20 ` James Clark
2023-08-01 10:55 ` James Clark
2023-08-01 15:56 ` Ian Rogers
2023-08-01 18:20 ` Arnaldo Carvalho de Melo
2023-08-02 9:21 ` James Clark
2023-07-11 10:02 ` [PATCH v3 5/5] perf vendor events arm64: Update N2-r0p0 " James Clark
2023-07-12 1:08 ` Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=516face0-fc2f-453c-df9a-f6ebe47c687f@arm.com \
--to=james.clark@arm.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=ilkka@os.amperecomputing.com \
--cc=irogers@google.com \
--cc=john.g.garry@oracle.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=kjain@linux.ibm.com \
--cc=leo.yan@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mike.leach@linaro.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=nick.forrington@arm.com \
--cc=peterz@infradead.org \
--cc=renyu.zj@linux.alibaba.com \
--cc=tmricht@linux.ibm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).