linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 0/4] Fixes/improvements to metric output
@ 2024-02-09 20:49 Ian Rogers
  2024-02-09 20:49 ` [PATCH v1 1/4] perf expr: Allow NaN to be a valid number Ian Rogers
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Ian Rogers @ 2024-02-09 20:49 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, John Garry, Kan Liang, James Clark,
	K Prateek Nayak, Kaige Ye, linux-perf-users, linux-kernel

A small addition to allow NaNs to be encoded in metrics and then 3
fixes to various metric related issues.

Ian Rogers (4):
  perf expr: Allow NaN to be a valid number
  perf expr: Fix "has_event" function for metric style events
  perf stat: Avoid metric-only segv
  perf metric: Don't remove scale from counts

 tools/perf/util/expr.c         | 20 +++++++++++++++++++-
 tools/perf/util/expr.l         |  9 +++++++++
 tools/perf/util/stat-display.c |  2 +-
 tools/perf/util/stat-shadow.c  |  7 +------
 4 files changed, 30 insertions(+), 8 deletions(-)

-- 
2.43.0.687.g38aa6559b0-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 1/4] perf expr: Allow NaN to be a valid number
  2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
@ 2024-02-09 20:49 ` Ian Rogers
  2024-02-09 20:49 ` [PATCH v1 2/4] perf expr: Fix "has_event" function for metric style events Ian Rogers
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Ian Rogers @ 2024-02-09 20:49 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, John Garry, Kan Liang, James Clark,
	K Prateek Nayak, Kaige Ye, linux-perf-users, linux-kernel

Currently only floating point numbers can be parsed, add a special
case for NaN.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/expr.l | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/tools/perf/util/expr.l b/tools/perf/util/expr.l
index 0feef0726c48..a2fc43159ee9 100644
--- a/tools/perf/util/expr.l
+++ b/tools/perf/util/expr.l
@@ -94,6 +94,14 @@ static int literal(yyscan_t scanner, const struct expr_scanner_ctx *sctx)
 	}
 	return LITERAL;
 }
+
+static int nan_value(yyscan_t scanner)
+{
+	YYSTYPE *yylval = expr_get_lval(scanner);
+
+	yylval->num = NAN;
+	return NUMBER;
+}
 %}
 
 number		([0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)(e-?[0-9]+)?
@@ -115,6 +123,7 @@ else		{ return ELSE; }
 source_count	{ return SOURCE_COUNT; }
 has_event	{ return HAS_EVENT; }
 strcmp_cpuid_str	{ return STRCMP_CPUID_STR; }
+NaN		{ return nan_value(yyscanner); }
 {literal}	{ return literal(yyscanner, sctx); }
 {number}	{ return value(yyscanner); }
 {symbol}	{ return str(yyscanner, ID, sctx->runtime); }
-- 
2.43.0.687.g38aa6559b0-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 2/4] perf expr: Fix "has_event" function for metric style events
  2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
  2024-02-09 20:49 ` [PATCH v1 1/4] perf expr: Allow NaN to be a valid number Ian Rogers
@ 2024-02-09 20:49 ` Ian Rogers
  2024-02-09 20:49 ` [PATCH v1 3/4] perf stat: Avoid metric-only segv Ian Rogers
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Ian Rogers @ 2024-02-09 20:49 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, John Garry, Kan Liang, James Clark,
	K Prateek Nayak, Kaige Ye, linux-perf-users, linux-kernel

Events in metrics cannot use '/' as a separator, it would be
recognized as a divide, so they use '@'. The '@' is recognized in the
metricgroups code and changed to '/', do the same in the has_event
function so that the parsing is only tried without the @s.

Fixes: 4a4a9bf9075f ("perf expr: Add has_event function")
Signed-off-by: Ian Rogers <irogers@google.com>
---
Note, this fixes a has_event issue but as no metrics currently use
metric style events like cpu@inst_retired.any@ no current metrics are
broken.
---
 tools/perf/util/expr.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
index 7be23b3ac082..b8875aac8f87 100644
--- a/tools/perf/util/expr.c
+++ b/tools/perf/util/expr.c
@@ -500,7 +500,25 @@ double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const
 	tmp = evlist__new();
 	if (!tmp)
 		return NAN;
-	ret = parse_event(tmp, id) ? 0 : 1;
+
+	if (strchr(id, '@')) {
+		char *tmp_id, *p;
+
+		tmp_id = strdup(id);
+		if (!tmp_id) {
+			ret = NAN;
+			goto out;
+		}
+		p = strchr(tmp_id, '@');
+		*p = '/';
+		p = strrchr(tmp_id, '@');
+		*p = '/';
+		ret = parse_event(tmp, tmp_id) ? 0 : 1;
+		free(tmp_id);
+	} else {
+		ret = parse_event(tmp, id) ? 0 : 1;
+	}
+out:
 	evlist__delete(tmp);
 	return ret;
 }
-- 
2.43.0.687.g38aa6559b0-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 3/4] perf stat: Avoid metric-only segv
  2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
  2024-02-09 20:49 ` [PATCH v1 1/4] perf expr: Allow NaN to be a valid number Ian Rogers
  2024-02-09 20:49 ` [PATCH v1 2/4] perf expr: Fix "has_event" function for metric style events Ian Rogers
@ 2024-02-09 20:49 ` Ian Rogers
  2024-02-13 21:45   ` Namhyung Kim
  2024-02-09 20:49 ` [PATCH v1 4/4] perf metric: Don't remove scale from counts Ian Rogers
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Ian Rogers @ 2024-02-09 20:49 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, John Garry, Kan Liang, James Clark,
	K Prateek Nayak, Kaige Ye, linux-perf-users, linux-kernel

Cycles is recognized as part of a hard coded metric in stat-shadow.c,
it may call print_metric_only with a NULL fmt string leading to a
segfault. Handle the NULL fmt explicitly.

Fixes: 088519f318be ("perf stat: Move the display functions to stat-display.c")
Signed-off-by: Ian Rogers <irogers@google.com>
---
Note, the fixes tag is to a refactor that moved the function. The bug
existed before this.
---
 tools/perf/util/stat-display.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
index 8c61f8627ebc..b7d00a538d70 100644
--- a/tools/perf/util/stat-display.c
+++ b/tools/perf/util/stat-display.c
@@ -560,7 +560,7 @@ static void print_metric_only(struct perf_stat_config *config,
 	if (color)
 		mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
 
-	color_snprintf(str, sizeof(str), color ?: "", fmt, val);
+	color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);
 	fprintf(out, "%*s ", mlen, str);
 	os->first = false;
 }
-- 
2.43.0.687.g38aa6559b0-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 4/4] perf metric: Don't remove scale from counts
  2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
                   ` (2 preceding siblings ...)
  2024-02-09 20:49 ` [PATCH v1 3/4] perf stat: Avoid metric-only segv Ian Rogers
@ 2024-02-09 20:49 ` Ian Rogers
  2024-02-13 14:28 ` [PATCH v1 0/4] Fixes/improvements to metric output Liang, Kan
  2024-02-14 19:07 ` Namhyung Kim
  5 siblings, 0 replies; 8+ messages in thread
From: Ian Rogers @ 2024-02-09 20:49 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, John Garry, Kan Liang, James Clark,
	K Prateek Nayak, Kaige Ye, linux-perf-users, linux-kernel

Counts were switched from the scaled saved value form to the
aggregated count to avoid double accounting. When this happened the
removing of scaling for a count should have been removed, however, it
wasn't and this wasn't observed as it normally doesn't matter because
a counter's scale is 1. A problem was observed with RAPL events that
are scaled.

Fixes: 37cc8ad77cf8 ("perf metric: Directly use counts rather than saved_value")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/stat-shadow.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index e31426167852..cf573ff3fa84 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -414,12 +414,7 @@ static int prepare_metric(struct evsel **metric_events,
 				val = NAN;
 				source_count = 0;
 			} else {
-				/*
-				 * If an event was scaled during stat gathering,
-				 * reverse the scale before computing the
-				 * metric.
-				 */
-				val = aggr->counts.val * (1.0 / metric_events[i]->scale);
+				val = aggr->counts.val;
 				source_count = evsel__source_count(metric_events[i]);
 			}
 		}
-- 
2.43.0.687.g38aa6559b0-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 0/4] Fixes/improvements to metric output
  2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
                   ` (3 preceding siblings ...)
  2024-02-09 20:49 ` [PATCH v1 4/4] perf metric: Don't remove scale from counts Ian Rogers
@ 2024-02-13 14:28 ` Liang, Kan
  2024-02-14 19:07 ` Namhyung Kim
  5 siblings, 0 replies; 8+ messages in thread
From: Liang, Kan @ 2024-02-13 14:28 UTC (permalink / raw)
  To: Ian Rogers, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Adrian Hunter, John Garry, James Clark, K Prateek Nayak, Kaige Ye,
	linux-perf-users, linux-kernel



On 2024-02-09 3:49 p.m., Ian Rogers wrote:
> A small addition to allow NaNs to be encoded in metrics and then 3
> fixes to various metric related issues.
> 
> Ian Rogers (4):
>   perf expr: Allow NaN to be a valid number
>   perf expr: Fix "has_event" function for metric style events
>   perf stat: Avoid metric-only segv
>   perf metric: Don't remove scale from counts
>

Reviewed-by: Kan Liang <kan.liang@linux.intel.com>

Thanks,
Kan


>  tools/perf/util/expr.c         | 20 +++++++++++++++++++-
>  tools/perf/util/expr.l         |  9 +++++++++
>  tools/perf/util/stat-display.c |  2 +-
>  tools/perf/util/stat-shadow.c  |  7 +------
>  4 files changed, 30 insertions(+), 8 deletions(-)
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 3/4] perf stat: Avoid metric-only segv
  2024-02-09 20:49 ` [PATCH v1 3/4] perf stat: Avoid metric-only segv Ian Rogers
@ 2024-02-13 21:45   ` Namhyung Kim
  0 siblings, 0 replies; 8+ messages in thread
From: Namhyung Kim @ 2024-02-13 21:45 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
	John Garry, Kan Liang, James Clark, K Prateek Nayak, Kaige Ye,
	linux-perf-users, linux-kernel

On Fri, Feb 9, 2024 at 12:50 PM Ian Rogers <irogers@google.com> wrote:
>
> Cycles is recognized as part of a hard coded metric in stat-shadow.c,
> it may call print_metric_only with a NULL fmt string leading to a
> segfault. Handle the NULL fmt explicitly.
>
> Fixes: 088519f318be ("perf stat: Move the display functions to stat-display.c")
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> Note, the fixes tag is to a refactor that moved the function. The bug
> existed before this.

Yeah I noticed that.

> ---
>  tools/perf/util/stat-display.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
> index 8c61f8627ebc..b7d00a538d70 100644
> --- a/tools/perf/util/stat-display.c
> +++ b/tools/perf/util/stat-display.c
> @@ -560,7 +560,7 @@ static void print_metric_only(struct perf_stat_config *config,
>         if (color)
>                 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
>
> -       color_snprintf(str, sizeof(str), color ?: "", fmt, val);
> +       color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);

I was thinking about fixing the callers to pass valid format strings
but it seems they don't care about the output string anyway so
I think it's fine.

Thanks,
Namhyung


>         fprintf(out, "%*s ", mlen, str);
>         os->first = false;
>  }
> --
> 2.43.0.687.g38aa6559b0-goog
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 0/4] Fixes/improvements to metric output
  2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
                   ` (4 preceding siblings ...)
  2024-02-13 14:28 ` [PATCH v1 0/4] Fixes/improvements to metric output Liang, Kan
@ 2024-02-14 19:07 ` Namhyung Kim
  5 siblings, 0 replies; 8+ messages in thread
From: Namhyung Kim @ 2024-02-14 19:07 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Ian Rogers, Mark Rutland, Ingo Molnar,
	Kaige Ye, Peter Zijlstra, Alexander Shishkin, Jiri Olsa,
	K Prateek Nayak, Kan Liang, John Garry, James Clark,
	Adrian Hunter, linux-kernel, linux-perf-users

On Fri, 9 Feb 2024 12:49:43 -0800, Ian Rogers wrote:
> A small addition to allow NaNs to be encoded in metrics and then 3
> fixes to various metric related issues.
> 
> Ian Rogers (4):
>   perf expr: Allow NaN to be a valid number
>   perf expr: Fix "has_event" function for metric style events
>   perf stat: Avoid metric-only segv
>   perf metric: Don't remove scale from counts
> 
> [...]

Applied to perf-tools-next, thanks!

Best regards,
-- 
Namhyung Kim <namhyung@kernel.org>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-02-14 19:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-09 20:49 [PATCH v1 0/4] Fixes/improvements to metric output Ian Rogers
2024-02-09 20:49 ` [PATCH v1 1/4] perf expr: Allow NaN to be a valid number Ian Rogers
2024-02-09 20:49 ` [PATCH v1 2/4] perf expr: Fix "has_event" function for metric style events Ian Rogers
2024-02-09 20:49 ` [PATCH v1 3/4] perf stat: Avoid metric-only segv Ian Rogers
2024-02-13 21:45   ` Namhyung Kim
2024-02-09 20:49 ` [PATCH v1 4/4] perf metric: Don't remove scale from counts Ian Rogers
2024-02-13 14:28 ` [PATCH v1 0/4] Fixes/improvements to metric output Liang, Kan
2024-02-14 19:07 ` Namhyung Kim

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).