[4/9] perf stat: Move common code in print_metric_headers()

Message ID 20221107213314.3239159-5-namhyung@kernel.org
State New
Headers
Series perf stat: Cleanup perf stat output display (v1) |

Commit Message

Namhyung Kim Nov. 7, 2022, 9:33 p.m. UTC
  The struct perf_stat_output_ctx is set in a loop with the same values.
Move the code out of the loop and keep the loop minimal.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/stat-display.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
  

Comments

Ian Rogers Nov. 8, 2022, 11:19 p.m. UTC | #1
On Mon, Nov 7, 2022 at 1:33 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> The struct perf_stat_output_ctx is set in a loop with the same values.
> Move the code out of the loop and keep the loop minimal.
>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Acked-by: Ian Rogers <irogers@google.com>
Could also potentially make it const, but functions it is passed to
would also need changing.

Thanks,
Ian

> ---
>  tools/perf/util/stat-display.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
> index d4936a502560..115477461224 100644
> --- a/tools/perf/util/stat-display.c
> +++ b/tools/perf/util/stat-display.c
> @@ -837,11 +837,16 @@ static void print_metric_headers(struct perf_stat_config *config,
>                                  struct evlist *evlist,
>                                  const char *prefix, bool no_indent)
>  {
> -       struct perf_stat_output_ctx out;
>         struct evsel *counter;
>         struct outstate os = {
>                 .fh = config->output
>         };
> +       struct perf_stat_output_ctx out = {
> +               .ctx = &os,
> +               .print_metric = print_metric_header,
> +               .new_line = new_line_metric,
> +               .force_header = true,
> +       };
>         bool first = true;
>
>         if (config->json_output && !config->interval)
> @@ -865,13 +870,11 @@ static void print_metric_headers(struct perf_stat_config *config,
>         /* Print metrics headers only */
>         evlist__for_each_entry(evlist, counter) {
>                 os.evsel = counter;
> -               out.ctx = &os;
> -               out.print_metric = print_metric_header;
> +
>                 if (!first && config->json_output)
>                         fprintf(config->output, ", ");
>                 first = false;
> -               out.new_line = new_line_metric;
> -               out.force_header = true;
> +
>                 perf_stat__print_shadow_stats(config, counter, 0,
>                                               0,
>                                               &out,
> --
> 2.38.1.431.g37b22c650d-goog
>
  
Namhyung Kim Nov. 9, 2022, 6:04 p.m. UTC | #2
On Tue, Nov 8, 2022 at 3:19 PM Ian Rogers <irogers@google.com> wrote:
>
> On Mon, Nov 7, 2022 at 1:33 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > The struct perf_stat_output_ctx is set in a loop with the same values.
> > Move the code out of the loop and keep the loop minimal.
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>
> Acked-by: Ian Rogers <irogers@google.com>
> Could also potentially make it const, but functions it is passed to
> would also need changing.

I'll consider when I work on it later.

Thanks,
Namhyung
  

Patch

diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
index d4936a502560..115477461224 100644
--- a/tools/perf/util/stat-display.c
+++ b/tools/perf/util/stat-display.c
@@ -837,11 +837,16 @@  static void print_metric_headers(struct perf_stat_config *config,
 				 struct evlist *evlist,
 				 const char *prefix, bool no_indent)
 {
-	struct perf_stat_output_ctx out;
 	struct evsel *counter;
 	struct outstate os = {
 		.fh = config->output
 	};
+	struct perf_stat_output_ctx out = {
+		.ctx = &os,
+		.print_metric = print_metric_header,
+		.new_line = new_line_metric,
+		.force_header = true,
+	};
 	bool first = true;
 
 	if (config->json_output && !config->interval)
@@ -865,13 +870,11 @@  static void print_metric_headers(struct perf_stat_config *config,
 	/* Print metrics headers only */
 	evlist__for_each_entry(evlist, counter) {
 		os.evsel = counter;
-		out.ctx = &os;
-		out.print_metric = print_metric_header;
+
 		if (!first && config->json_output)
 			fprintf(config->output, ", ");
 		first = false;
-		out.new_line = new_line_metric;
-		out.force_header = true;
+
 		perf_stat__print_shadow_stats(config, counter, 0,
 					      0,
 					      &out,