perf script: Fix perf script -F +metric
PATCH 1/2 perf script: Fix perf script -F +metric
This fixes a regression with perf script -F +metric originally caused by :
commit 37cc8ad77cf81f3ffd226856c367b0e15333a738
Author: Ian Rogers <[email protected]> Date: Sun Feb 19 01:28:46 2023 -0800
perf metric: Directly use counts rather than saved_value
In the perf script environment the evsel wouldn't allocate an aggr values array, which led to a -1 reference because the metric evaluation would try to reference NULL - 1 (for aggr_idx)
Give the perf script evsels a single CPU aggr setup. That's enough because the groups are always contiguous, so no need to store more than one CPU's worth of values.
Before
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy % perf script -F +metric
Segmentation fault (core dumped)
After:
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy
...
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (90 samples) ] % perf script -F +metric
perf-exec 1847557 264658.180789: 3009 cycles: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms])
perf-exec 1847557 264658.180789: 382 instructions: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms])
perf-exec 1847557 264658.180789: metric: 0.13 insn per cycle...
--- tools/perf/builtin-script.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +4 -2
@@ -84,7 +84,7 @@ static bool system_wide;static bool print_flags;static const char *cpu_list; static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); -static struct perf_stat_config stat_config; +static struct perf_stat_config stat_config = { .aggr_map = &(struct cpu_aggr_map ){ .nr =1 } };static int max_blocks;static bool native_arch;static struct dlfilter *dlfilter;
@@ -2133,12 +2133,14 @@ static void perf_sample__fprint_metric(struct perf_script *script,if (evsel_script (leader)->gnum++ ==0 )perf_stat__reset_shadow_stats (); val = sample->period * evsel->scale; +/* Always use CPU 0 storage because the groups are contiguous. */ + evsel->stats->aggr[0 ].counts.val = val;evsel_script (evsel)->val = val;if (evsel_script (leader)->gnum == leader->core.nr_members) { for_each_group_member (ev2, leader) {perf_stat__print_shadow_stats (&stat_config, ev2,evsel_script (ev2)->val, - sample->cpu, +0 , &ctx,NULL ); }
Signature · 1 line
2.45.2
PATCH 2/2 Add a test case for perf script -F +metric
Just a simple test
--- tools/perf/tests/shell/script.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
tools/perf/tests/shell/script.sh +15 -1
@@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # perf script tests # SPDX-License-Identifier: GPL-2.0
@@ -7,6 +7,7 @@ set -e temp_dir=$(mktemp -d /tmp/perf-test-script.XXXXXXXXXX) perfdatafile="${temp_dir}/perf.data" +scriptoutput="${temp_dir}/script" db_test="${temp_dir}/db_test.py" err=0
@@ -88,8 +89,21 @@ test_parallel_perf() echo "parallel-perf test [Success]" } +test_metric() +{ + echo "script metric test" + if ! perf list | grep -q cycles ; then return ; fi + if ! perf list | grep -q instructions ; then return ; fi + perf record -e '{cycles,instructions}' -o "${perfdatafile}" bash -c 'for i in `seq 1000` ; do true ; done' + perf script -i "${perfdatafile}" -F +metric > $scriptoutput + test $(grep -c metric $scriptoutput) -gt 5 + grep metric $scriptoutput | head + echo "script metric test [Success]" +} + test_db test_parallel_perf +test_metric cleanup
Signature · 1 line
2.45.2
Namhyung Kim
maintainerRe: [PATCH 2/2] Add a test case for perf script -F +metric
Quoted 12 lines · Andi Kleen
Just a simple test
Signed-off-by Andi Kleen <[email protected]>--- tools/perf/tests/shell/script.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)tools/perf/tests/shell/script.sh +0
-0
tools/perf/tests/shell/script.sh line 1
Quoted 2 lines
tools/perf/tests/shell/script.sh +1
-1@@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash
Do we really need this?
Quoted 12 lines
tools/perf/tests/shell/script.sh +1
-0# perf script tests # SPDX-License-Identifier: GPL-2.0@@ -7,6 +7,7 @@ set -e temp_dir=$(mktemp -d /tmp/perf-test-script.XXXXXXXXXX) perfdatafile="${temp_dir}/perf.data" +scriptoutput="${temp_dir}/script" db_test="${temp_dir}/db_test.py" err=0
tools/perf/tests/shell/script.sh test_parallel_perf() · line 97
Quoted 9 lines
tools/perf/tests/shell/script.sh +6
-0@@ -88,8 +89,21 @@ test_parallel_perf() echo "parallel-perf test [Success]" } +test_metric() +{ + echo "script metric test" + if ! perf list | grep -q cycles ; then return ; fi + if ! perf list | grep -q instructions ; then return ; fi + perf record -e '{cycles,instructions}' -o "${perfdatafile}" bash -c 'for i in `seq 1000` ; do true ; done'
You can use built-in workloads like noploop or so instead of running an external command.
$ perf record -- perf test -w noploop
Thanks,
Namhyung
Quoted 15 lines
tools/perf/tests/shell/script.sh +7
-0+ perf script -i "${perfdatafile}" -F +metric > $scriptoutput + test $(grep -c metric $scriptoutput) -gt 5 + grep metric $scriptoutput | head + echo "script metric test [Success]" +} + test_db test_parallel_perf +test_metric cleanupSignature · 1 line
2.45.2
Quoted 29 lines · Andi Kleen
This fixes a regression with perf script -F +metric originally caused by :
commit
37cc8ad77cf81f3ffd226856c367b0e15333a738Author: Ian Rogers <[email protected]> Date: Sun Feb 19 01:28:46 2023 -0800perf metric: Directly use counts rather than
saved_valueIn the perf script environment the evsel wouldn't allocate an aggr values array, which led to a -1 reference because the metric evaluation would try to reference NULL - 1 (for
aggr_idx)Give the perf script evsels a single CPU aggr setup. That's enough because the groups are always contiguous, so no need to store more than one CPU's worth of values.
Before
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy % perf script -F +metric
Segmentation fault (core dumped)After:
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy
...
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (90 samples) ] % perf script -F +metric
Quoted 7 lines
perf-exec 1847557 264658.180789: 3009 cycles: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms]) perf-exec 1847557 264658.180789: 382 instructions: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms]) perf-exec 1847557 264658.180789: metric: 0.13 insn per cycle...
Fixes 37cc8ad77cf8 ("perf metric: Directly use counts rather ...")Signed-off-by Andi Kleen <[email protected]>
This works, however there seems to be yet another issue on perf-tools-next branch that breaks script metrics.
617824a7f0f73 (perf parse-events: Prefer sysfs/JSON hardware events over legacy, 2024-04-15) changes event priority so on my host "instructions" have type PERF_TYPE_RAW instead of PERF_TYPE_HARDWARE.
# event : name = instructions, , id = { 190969, 190970, 190971, 190972, 190973, 190974, 190975, 190976 }, type = 4 (cpu), size = 136, config = 0xc0 (instructions), sample_type = IP|TID|TIME|READ|CPU|PERIOD|IDENTIFIER, read_format = ID|GROUP|LOST, sample_id_all = 1, exclude_guest = 1This results in evsel__stat_type no longer recognizing these as STAT_INSTRUCTIONS.
Quoted 34 lines
--- tools/perf/builtin-script.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)tools/perf/builtin-script.c +4
-3@@ -84,7 +84,7 @@ static bool system_wide;static bool print_flags;static const char *cpu_list; static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); -static struct perf_stat_config stat_config; +static struct perf_stat_config stat_config = { .aggr_map = &(struct cpu_aggr_map ){ .nr =1 } };static int max_blocks;static bool native_arch;static struct dlfilter *dlfilter;@@ -2133,12 +2133,14 @@ static void perf_sample__fprint_metric(struct perf_script *script,if (evsel_script (leader)->gnum++ ==0 )perf_stat__reset_shadow_stats (); val = sample->period * evsel->scale; +/* Always use CPU 0 storage because the groups are contiguous. */ + evsel->stats->aggr[0 ].counts.val = val;evsel_script (evsel)->val = val;if (evsel_script (leader)->gnum == leader->core.nr_members) { for_each_group_member (ev2, leader) {perf_stat__print_shadow_stats (&stat_config, ev2,evsel_script (ev2)->val, - sample->cpu, +0 , &ctx,NULL ); } --
Quoted 2 lines
2.45.2
--
Artem
Namhyung Kim
maintainerQuoted 46 lines · Artem Savkov
Quoted 29 lines · Andi Kleen
This fixes a regression with perf script -F +metric originally caused by :
commit
37cc8ad77cf81f3ffd226856c367b0e15333a738Author: Ian Rogers <[email protected]> Date: Sun Feb 19 01:28:46 2023 -0800perf metric: Directly use counts rather than
saved_valueIn the perf script environment the evsel wouldn't allocate an aggr values array, which led to a -1 reference because the metric evaluation would try to reference NULL - 1 (for
aggr_idx)Give the perf script evsels a single CPU aggr setup. That's enough because the groups are always contiguous, so no need to store more than one CPU's worth of values.
Before
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy % perf script -F +metric
Segmentation fault (core dumped)After:
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy
...
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (90 samples) ] % perf script -F +metricQuoted 7 lines
perf-exec 1847557 264658.180789: 3009 cycles: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms]) perf-exec 1847557 264658.180789: 382 instructions: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms]) perf-exec 1847557 264658.180789: metric: 0.13 insn per cycle...
Fixes 37cc8ad77cf8 ("perf metric: Directly use counts rather ...")Signed-off-by Andi Kleen <[email protected]>This works, however there seems to be yet another issue on perf-tools-next branch that breaks script metrics.
617824a7f0f73(perf parse-events: Prefer sysfs/JSON hardware events over legacy, 2024-04-15) changes event priority so on my host "instructions" have typePERF_TYPE_RAWinstead ofPERF_TYPE_HARDWARE.
Quoted 4 lines
# event : name = instructions, , id = { 190969, 190970, 190971, 190972, 190973, 190974, 190975, 190976 }, type = 4 (cpu), size = 136, config = 0xc0 (instructions), sample_type = IP|TID|TIME|READ|CPU|PERIOD|IDENTIFIER, read_format = ID|GROUP|LOST, sample_id_all = 1, exclude_guest = 1This results in
evsel__stat_typeno longer recognizing these asSTAT_INSTRUCTIONS.
Hmm.. sounds like a problem. Ian, did you notice this?
Thanks,
Namhyung
Quoted 41 lines
Quoted 35 lines
--- tools/perf/builtin-script.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)tools/perf/builtin-script.c +4
-3@@ -84,7 +84,7 @@ static bool system_wide;static bool print_flags;static const char *cpu_list; static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); -static struct perf_stat_config stat_config; +static struct perf_stat_config stat_config = { .aggr_map = &(struct cpu_aggr_map ){ .nr =1 } };static int max_blocks;static bool native_arch;static struct dlfilter *dlfilter;@@ -2133,12 +2133,14 @@ static void perf_sample__fprint_metric(struct perf_script *script,if (evsel_script (leader)->gnum++ ==0 )perf_stat__reset_shadow_stats (); val = sample->period * evsel->scale; +/* Always use CPU 0 storage because the groups are contiguous. */ + evsel->stats->aggr[0 ].counts.val = val;evsel_script (evsel)->val = val;if (evsel_script (leader)->gnum == leader->core.nr_members) { for_each_group_member (ev2, leader) {perf_stat__print_shadow_stats (&stat_config, ev2,evsel_script (ev2)->val, - sample->cpu, +0 , &ctx,NULL ); } --Quoted 2 lines
2.45.2
--
Artem
Quoted 7 lines
This works, however there seems to be yet another issue on perf-tools-next branch that breaks script metrics.
617824a7f0f73(perf parse-events: Prefer sysfs/JSON hardware events over legacy, 2024-04-15) changes event priority so on my host "instructions" have typePERF_TYPE_RAWinstead ofPERF_TYPE_HARDWARE.
FWIW I don't see this on my build on Intel, even with that commit.
Quoted 2 lines
This results in
evsel__stat_typeno longer recognizing these asSTAT_INSTRUCTIONS.
I guess we could just fix evsel__stat_type to handle that case
Does this patch work?
perf: Workaround event priority problems
When instructions is not of HARDWARE types anymore, but raw, which breaks the shadow stat metrics detection for IPC.
tools/perf/util/stat-shadow.c +4 -2
@@ -97,9 +97,11 @@ static enum stat_type evsel__stat_type(const struct evsel *evsel)if (evsel__is_clock (evsel))return STAT_NSECS ; -else if (evsel__match (evsel,HARDWARE ,HW_CPU_CYCLES )) +else if (evsel__match (evsel,HARDWARE ,HW_CPU_CYCLES ) || + !strcmp (evsel__name ((struct evsel *)evsel),"cycles" ))return STAT_CYCLES ; -else if (evsel__match (evsel,HARDWARE ,HW_INSTRUCTIONS )) +else if (evsel__match (evsel,HARDWARE ,HW_INSTRUCTIONS ) || + !strcmp (evsel__name ((struct evsel *)evsel),"instructions" ))return STAT_INSTRUCTIONS ;else if (evsel__match (evsel,HARDWARE ,HW_STALLED_CYCLES_FRONTEND ))return STAT_STALLED_CYCLES_FRONT ;
Quoted 8 lines · Andi Kleen
Quoted 7 lines
This works, however there seems to be yet another issue on perf-tools-next branch that breaks script metrics.
617824a7f0f73(perf parse-events: Prefer sysfs/JSON hardware events over legacy, 2024-04-15) changes event priority so on my host "instructions" have typePERF_TYPE_RAWinstead ofPERF_TYPE_HARDWARE.
Quoted 8 lines
FWIW I don't see this on my build on Intel, even with that commit.
Quoted 2 lines
This results in
evsel__stat_typeno longer recognizing these asSTAT_INSTRUCTIONS.I guess we could just fix
evsel__stat_typeto handle that caseDoes this patch work?
It does for this specific example, but there are other events as well. From arch/x86/events/intel/core.c:
static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
{
[PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
[PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
[PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
[PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
[PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
[PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
[PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
};The list is probably different on other arches.
Quoted 14 lines
perf: Workaround event priority problems
When instructions is not of HARDWARE types anymore, but raw, which breaks the shadow stat metrics detection for IPC.
Reported-by Artem Savkov <[email protected]>Fixes 617824a7f0f73 (perf parse-events: Prefer sysfs/JSON hardwareSigned-off-by Andi Kleen <[email protected]>tools/perf/util/stat-shadow.c +0
-0
tools/perf/util/stat-shadow.c static enum stat_type evsel__stat_type(const struct evsel *evsel) · line 101
Quoted 6 lines
tools/perf/util/stat-shadow.c +2
-1@@ -97,9 +97,11 @@ static enum stat_type evsel__stat_type(const struct evsel *evsel)if (evsel__is_clock (evsel))return STAT_NSECS ; -else if (evsel__match (evsel,HARDWARE ,HW_CPU_CYCLES )) +else if (evsel__match (evsel,HARDWARE ,HW_CPU_CYCLES ) || + !strcmp (evsel__name ((struct evsel *)evsel),"cycles" ))
It is also probably worth to add a PERF_TYPE_RAW check.
tools/perf/util/stat-shadow.c static enum stat_type evsel__stat_type(const struct evsel *evsel) · line 107
Quoted 8 lines
tools/perf/util/stat-shadow.c +2
-1return STAT_CYCLES ; - else if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) + else if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS) || + !strcmp(evsel__name((struct evsel *)evsel), "instructions")) return STAT_INSTRUCTIONS; else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) return STAT_STALLED_CYCLES_FRONT;
--
Artem
Quoted 33 lines · Artem Savkov
Quoted 8 lines · Andi Kleen
Quoted 7 lines
This works, however there seems to be yet another issue on perf-tools-next branch that breaks script metrics.
617824a7f0f73(perf parse-events: Prefer sysfs/JSON hardware events over legacy, 2024-04-15) changes event priority so on my host "instructions" have typePERF_TYPE_RAWinstead ofPERF_TYPE_HARDWARE.Quoted 8 lines
FWIW I don't see this on my build on Intel, even with that commit.
Quoted 2 lines
This results in
evsel__stat_typeno longer recognizing these asSTAT_INSTRUCTIONS.I guess we could just fix
evsel__stat_typeto handle that caseDoes this patch work?
It does for this specific example, but there are other events as well. From
arch/x86/events/intel/core.c:static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly = { [PERF_COUNT_HW_CPU_CYCLES] = 0x003c, [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e, [PERF_COUNT_HW_CACHE_MISSES] = 0x412e, [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, [PERF_COUNT_HW_BUS_CYCLES] = 0x013c, [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */ };
Quoted 1 line
The list is probably different on other arches.
Most of them only use the fallback "xxx per second" metric, which should work even for raw I think. But yes some would be needed (like cache and branches)
Probably it needs a more generic solution then, it was somewhat of a hack anyways.
My proposal would be to revert the original patch. Clearly it was a bad idea.
-Andi
Namhyung Kim
maintainerHi Andi,
Quoted 29 lines · Andi Kleen
This fixes a regression with perf script -F +metric originally caused by :
commit
37cc8ad77cf81f3ffd226856c367b0e15333a738Author: Ian Rogers <[email protected]> Date: Sun Feb 19 01:28:46 2023 -0800perf metric: Directly use counts rather than
saved_valueIn the perf script environment the evsel wouldn't allocate an aggr values array, which led to a -1 reference because the metric evaluation would try to reference NULL - 1 (for
aggr_idx)Give the perf script evsels a single CPU aggr setup. That's enough because the groups are always contiguous, so no need to store more than one CPU's worth of values.
Before
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy % perf script -F +metric
Segmentation fault (core dumped)After:
% perf record -e '{cycles,instructions}:S' perf bench mem memcpy
...
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (90 samples) ] % perf script -F +metric
Quoted 4 lines
perf-exec 1847557 264658.180789: 3009 cycles: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms]) perf-exec 1847557 264658.180789: 382 instructions: ffffffff990a579a native_write_msr+0xa ([kernel.kallsyms]) perf-exec 1847557 264658.180789: metric: 0.13 insn per cycle...
Thanks for the fix!
Quoted 12 lines
Fixes 37cc8ad77cf8 ("perf metric: Directly use counts rather ...")Signed-off-by Andi Kleen <[email protected]>--- tools/perf/builtin-script.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)tools/perf/builtin-script.c +0
-0
tools/perf/builtin-script.c static bool system_wide; · line 87
Quoted 5 lines
tools/perf/builtin-script.c +1
-1@@ -84,7 +84,7 @@ static bool system_wide;static bool print_flags;static const char *cpu_list; static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); -static struct perf_stat_config stat_config; +static struct perf_stat_config stat_config = { .aggr_map = &(struct cpu_aggr_map ){ .nr =1 } };
Can you please follow the coding style and properly put the code?
Thanks,
Namhyung
Quoted 22 lines
tools/perf/builtin-script.c +3
-1static int max_blocks;static bool native_arch;static struct dlfilter *dlfilter;@@ -2133,12 +2133,14 @@ static void perf_sample__fprint_metric(struct perf_script *script,if (evsel_script (leader)->gnum++ ==0 )perf_stat__reset_shadow_stats (); val = sample->period * evsel->scale; +/* Always use CPU 0 storage because the groups are contiguous. */ + evsel->stats->aggr[0 ].counts.val = val;evsel_script (evsel)->val = val;if (evsel_script (leader)->gnum == leader->core.nr_members) { for_each_group_member (ev2, leader) {perf_stat__print_shadow_stats (&stat_config, ev2,evsel_script (ev2)->val, - sample->cpu, +0 , &ctx,NULL ); }Signature · 1 line
2.45.2