[v1,10/14] perf top: Avoid repeated function calls

Message ID 20231129060211.1890454-11-irogers@google.com
State New
Headers
Series Clean up libperf cpumap's empty function |

Commit Message

Ian Rogers Nov. 29, 2023, 6:02 a.m. UTC
  Add a local variable to avoid repeated calls to perf_cpu_map__nr.

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

Comments

James Clark Dec. 12, 2023, 3:11 p.m. UTC | #1
On 29/11/2023 06:02, Ian Rogers wrote:
> Add a local variable to avoid repeated calls to perf_cpu_map__nr.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

Reviewed-by: James Clark <james.clark@arm.com>

> ---
>  tools/perf/util/top.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c
> index be7157de0451..4db3d1bd686c 100644
> --- a/tools/perf/util/top.c
> +++ b/tools/perf/util/top.c
> @@ -28,6 +28,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
>  	struct record_opts *opts = &top->record_opts;
>  	struct target *target = &opts->target;
>  	size_t ret = 0;
> +	int nr_cpus;
>  
>  	if (top->samples) {
>  		samples_per_sec = top->samples / top->delay_secs;
> @@ -93,19 +94,17 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
>  	else
>  		ret += SNPRINTF(bf + ret, size - ret, " (all");
>  
> +	nr_cpus = perf_cpu_map__nr(top->evlist->core.user_requested_cpus);
>  	if (target->cpu_list)
>  		ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
> -				perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
> -				? "s" : "",
> +				nr_cpus > 1 ? "s" : "",
>  				target->cpu_list);
>  	else {
>  		if (target->tid)
>  			ret += SNPRINTF(bf + ret, size - ret, ")");
>  		else
>  			ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
> -					perf_cpu_map__nr(top->evlist->core.user_requested_cpus),
> -					perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
> -					? "s" : "");
> +					nr_cpus, nr_cpus > 1 ? "s" : "");
>  	}
>  
>  	perf_top__reset_sample_counters(top);
  
Arnaldo Carvalho de Melo Dec. 18, 2023, 8:34 p.m. UTC | #2
Em Tue, Dec 12, 2023 at 03:11:53PM +0000, James Clark escreveu:
> 
> 
> On 29/11/2023 06:02, Ian Rogers wrote:
> > Add a local variable to avoid repeated calls to perf_cpu_map__nr.
> > 
> > Signed-off-by: Ian Rogers <irogers@google.com>
> 
> Reviewed-by: James Clark <james.clark@arm.com>

Thanks, applied to perf-tools-next.

- Arnaldo

 
> > ---
> >  tools/perf/util/top.c | 9 ++++-----
> >  1 file changed, 4 insertions(+), 5 deletions(-)
> > 
> > diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c
> > index be7157de0451..4db3d1bd686c 100644
> > --- a/tools/perf/util/top.c
> > +++ b/tools/perf/util/top.c
> > @@ -28,6 +28,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
> >  	struct record_opts *opts = &top->record_opts;
> >  	struct target *target = &opts->target;
> >  	size_t ret = 0;
> > +	int nr_cpus;
> >  
> >  	if (top->samples) {
> >  		samples_per_sec = top->samples / top->delay_secs;
> > @@ -93,19 +94,17 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
> >  	else
> >  		ret += SNPRINTF(bf + ret, size - ret, " (all");
> >  
> > +	nr_cpus = perf_cpu_map__nr(top->evlist->core.user_requested_cpus);
> >  	if (target->cpu_list)
> >  		ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
> > -				perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
> > -				? "s" : "",
> > +				nr_cpus > 1 ? "s" : "",
> >  				target->cpu_list);
> >  	else {
> >  		if (target->tid)
> >  			ret += SNPRINTF(bf + ret, size - ret, ")");
> >  		else
> >  			ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
> > -					perf_cpu_map__nr(top->evlist->core.user_requested_cpus),
> > -					perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
> > -					? "s" : "");
> > +					nr_cpus, nr_cpus > 1 ? "s" : "");
> >  	}
> >  
> >  	perf_top__reset_sample_counters(top);
>
  

Patch

diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c
index be7157de0451..4db3d1bd686c 100644
--- a/tools/perf/util/top.c
+++ b/tools/perf/util/top.c
@@ -28,6 +28,7 @@  size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	struct record_opts *opts = &top->record_opts;
 	struct target *target = &opts->target;
 	size_t ret = 0;
+	int nr_cpus;
 
 	if (top->samples) {
 		samples_per_sec = top->samples / top->delay_secs;
@@ -93,19 +94,17 @@  size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	else
 		ret += SNPRINTF(bf + ret, size - ret, " (all");
 
+	nr_cpus = perf_cpu_map__nr(top->evlist->core.user_requested_cpus);
 	if (target->cpu_list)
 		ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
-				perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-				? "s" : "",
+				nr_cpus > 1 ? "s" : "",
 				target->cpu_list);
 	else {
 		if (target->tid)
 			ret += SNPRINTF(bf + ret, size - ret, ")");
 		else
 			ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus),
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-					? "s" : "");
+					nr_cpus, nr_cpus > 1 ? "s" : "");
 	}
 
 	perf_top__reset_sample_counters(top);