From: Namhyung Kim <namhyung@kernel.org>
To: Andi Kleen <ak@linux.intel.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v8 3/4] perf script: Fix perf script -F +metric
Date: Mon, 5 Aug 2024 12:23:22 -0700 [thread overview]
Message-ID: <ZrEmqk8yNzzGXd_4@google.com> (raw)
In-Reply-To: <20240801212844.677895-3-ak@linux.intel.com>
On Thu, Aug 01, 2024 at 02:28:43PM -0700, Andi Kleen wrote:
> This fixes a regression with perf script -F +metric originally caused by :
>
> commit 37cc8ad77cf81f3ffd226856c367b0e15333a738
> Author: Ian Rogers <irogers@google.com>
> 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
> ...
>
> Fixes: 37cc8ad77cf8 ("perf metric: Directly use counts rather ...")
> Signed-off-by: Andi Kleen <ak@linux.intel.com>
>
> ----
>
> v2: Reformat code
> v3: Work around bogus warning
> v4: Set up aggr map only for metrics case to keep perf stat record
> working
> v5: Broken version
> v6: Only set up limited aggregation mode with -F +metric. Add conflict
> checks with perf stat record files.
> v7: Remove some unnecessary conflict checks. Fix buffer overflow. Minor cleanups.
> v8: Add check for leader sampling. Update some comments.
> ---
> tools/perf/builtin-script.c | 50 +++++++++++++++++++++++++++++++++----
> 1 file changed, 45 insertions(+), 5 deletions(-)
>
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index c16224b1fef3..04378615b2ad 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -335,7 +335,6 @@ struct evsel_script {
> FILE *fp;
> u64 samples;
> /* For metric output */
> - u64 val;
> int gnum;
> };
>
> @@ -2127,18 +2126,30 @@ static void perf_sample__fprint_metric(struct perf_script *script,
> };
> struct evsel *ev2;
> u64 val;
> + static int printed;
>
> if (!evsel->stats)
> evlist__alloc_stats(&stat_config, script->session->evlist, /*alloc_raw=*/false);
> if (evsel_script(leader)->gnum++ == 0)
> perf_stat__reset_shadow_stats();
> - val = sample->period * evsel->scale;
> - evsel_script(evsel)->val = val;
> + val = sample->period;
> + if (!(evsel->core.attr.sample_type & PERF_SAMPLE_READ)) {
The correct check would need PERF_FORMAT_GROUP in attr.read_format for
the leader event, and no attr.sample_period for member events. But I
guess practically it's enough to check the leader only.
Thanks,
Namhyung
> + if (!printed)
> + fprintf(stderr, "perf script: -F metric requires {}:S for groups leader sampling\n");
> + printed = 1;
> + return;
> + }
> + /*
> + * Always use the first entry as storage because the leader sampling
> + * groups are contiguous and there's no need to handle multiple indexes
> + * for anything.
> + */
> + evsel->stats->aggr[0].counts.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,
> + evsel->stats->aggr[0].counts.val,
> + 0,
> &ctx,
> NULL);
> }
> @@ -2325,6 +2336,20 @@ static void process_event(struct perf_script *script,
> fflush(fp);
> }
>
> +static void check_metric_conflict(void)
> +{
> + int i;
> + /*
> + * Avoid conflict with the aggregation mode used for the metric printing.
> + */
> + for (i = 0; i < OUTPUT_TYPE_MAX; i++) {
> + if (output[i].fields & PERF_OUTPUT_METRIC) {
> + fprintf(stderr, "perf stat record files are not supported with -F metric\n");
> + exit(1);
> + }
> + }
> +}
> +
> static struct scripting_ops *scripting_ops;
>
> static void __process_stat(struct evsel *counter, u64 tstamp)
> @@ -2334,6 +2359,8 @@ static void __process_stat(struct evsel *counter, u64 tstamp)
> struct perf_cpu cpu;
> static int header_printed;
>
> + check_metric_conflict();
> +
> if (!header_printed) {
> printf("%3s %8s %15s %15s %15s %15s %s\n",
> "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
> @@ -3725,6 +3752,8 @@ static int process_stat_config_event(struct perf_session *session __maybe_unused
> {
> perf_event__read_stat_config(&stat_config, &event->stat_config);
>
> + check_metric_conflict();
> +
> /*
> * Aggregation modes are not used since post-processing scripts are
> * supposed to take care of such requirements
> @@ -4088,6 +4117,17 @@ int cmd_script(int argc, const char **argv)
>
> argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
> PARSE_OPT_STOP_AT_NON_OPTION);
> + for (i = 0; i < OUTPUT_TYPE_MAX; i++) {
> + if (output[i].fields & PERF_OUTPUT_METRIC) {
> + stat_config.aggr_map = cpu_aggr_map__empty_new(1);
> + err = -ENOMEM;
> + if (!stat_config.aggr_map)
> + goto out;
> + err = 0;
> + stat_config.aggr_map->nr = 1;
> + break;
> + }
> + }
>
> if (symbol_conf.guestmount ||
> symbol_conf.default_guest_vmlinux_name ||
> --
> 2.45.2
>
next prev parent reply other threads:[~2024-08-05 19:23 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-01 21:28 [PATCH v8 1/4] Create source symlink in perf object dir Andi Kleen
2024-08-01 21:28 ` [PATCH v8 2/4] perf test: Support external tests for separate objdir Andi Kleen
2024-08-05 19:02 ` Namhyung Kim
2024-08-01 21:28 ` [PATCH v8 3/4] perf script: Fix perf script -F +metric Andi Kleen
2024-08-05 19:23 ` Namhyung Kim [this message]
2024-08-01 21:28 ` [PATCH v8 4/4] Add a test case for " Andi Kleen
2024-08-05 19:25 ` Namhyung Kim
-- strict thread matches above, loose matches on Subject: below --
2024-08-07 3:30 [PATCH v8 1/4] Create source symlink in perf object dir Andi Kleen
2024-08-07 3:30 ` [PATCH v8 3/4] perf script: Fix perf script -F +metric Andi Kleen
2024-08-07 6:24 ` Namhyung Kim
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=ZrEmqk8yNzzGXd_4@google.com \
--to=namhyung@kernel.org \
--cc=ak@linux.intel.com \
--cc=linux-perf-users@vger.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).