[PATCH v5 02/14] perf bench mem: Defer type munging of size to float

Ankur Arora posted 14 patches 5 months, 1 week ago
[PATCH v5 02/14] perf bench mem: Defer type munging of size to float
Posted by Ankur Arora 5 months, 1 week ago
Do type conversion to double at the point of use.

Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
---
 tools/perf/bench/mem-functions.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
index 8599ed96ee1f..b8f020379197 100644
--- a/tools/perf/bench/mem-functions.c
+++ b/tools/perf/bench/mem-functions.c
@@ -139,7 +139,7 @@ struct bench_mem_info {
 	bool alloc_src;
 };
 
-static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, double size_total)
+static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, size_t size_total)
 {
 	const struct function *r = &info->functions[r_idx];
 	double result_bps = 0.0;
@@ -165,18 +165,18 @@ static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t
 	switch (bench_format) {
 	case BENCH_FORMAT_DEFAULT:
 		if (use_cycles) {
-			printf(" %14lf cycles/byte\n", (double)rt.cycles/size_total);
+			printf(" %14lf cycles/byte\n", (double)rt.cycles/(double)size_total);
 		} else {
-			result_bps = size_total/timeval2double(&rt.tv);
+			result_bps = (double)size_total/timeval2double(&rt.tv);
 			print_bps(result_bps);
 		}
 		break;
 
 	case BENCH_FORMAT_SIMPLE:
 		if (use_cycles) {
-			printf("%lf\n", (double)rt.cycles/size_total);
+			printf("%lf\n", (double)rt.cycles/(double)size_total);
 		} else {
-			result_bps = size_total/timeval2double(&rt.tv);
+			result_bps = (double)size_total/timeval2double(&rt.tv);
 			printf("%lf\n", result_bps);
 		}
 		break;
@@ -199,7 +199,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
 {
 	int i;
 	size_t size;
-	double size_total;
+	size_t size_total;
 
 	argc = parse_options(argc, argv, options, info->usage, 0);
 
@@ -212,7 +212,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
 	}
 
 	size = (size_t)perf_atoll((char *)size_str);
-	size_total = (double)size * nr_loops;
+	size_total = (size_t)size * nr_loops;
 
 	if ((s64)size <= 0) {
 		fprintf(stderr, "Invalid size:%s\n", size_str);
-- 
2.43.5
Re: [PATCH v5 02/14] perf bench mem: Defer type munging of size to float
Posted by Namhyung Kim 5 months ago
On Wed, Jul 09, 2025 at 05:59:14PM -0700, Ankur Arora wrote:
> Do type conversion to double at the point of use.
> 
> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>

Reviewed-by: Namhyung Kim <namhyung@kernel.org>

A nitpick below.

> ---
>  tools/perf/bench/mem-functions.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
> index 8599ed96ee1f..b8f020379197 100644
> --- a/tools/perf/bench/mem-functions.c
> +++ b/tools/perf/bench/mem-functions.c
> @@ -139,7 +139,7 @@ struct bench_mem_info {
>  	bool alloc_src;
>  };
>  
> -static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, double size_total)
> +static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, size_t size_total)
>  {
>  	const struct function *r = &info->functions[r_idx];
>  	double result_bps = 0.0;
> @@ -165,18 +165,18 @@ static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t
>  	switch (bench_format) {
>  	case BENCH_FORMAT_DEFAULT:
>  		if (use_cycles) {
> -			printf(" %14lf cycles/byte\n", (double)rt.cycles/size_total);
> +			printf(" %14lf cycles/byte\n", (double)rt.cycles/(double)size_total);
>  		} else {
> -			result_bps = size_total/timeval2double(&rt.tv);
> +			result_bps = (double)size_total/timeval2double(&rt.tv);
>  			print_bps(result_bps);
>  		}
>  		break;
>  
>  	case BENCH_FORMAT_SIMPLE:
>  		if (use_cycles) {
> -			printf("%lf\n", (double)rt.cycles/size_total);
> +			printf("%lf\n", (double)rt.cycles/(double)size_total);
>  		} else {
> -			result_bps = size_total/timeval2double(&rt.tv);
> +			result_bps = (double)size_total/timeval2double(&rt.tv);
>  			printf("%lf\n", result_bps);
>  		}
>  		break;
> @@ -199,7 +199,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
>  {
>  	int i;
>  	size_t size;
> -	double size_total;
> +	size_t size_total;
>  
>  	argc = parse_options(argc, argv, options, info->usage, 0);
>  
> @@ -212,7 +212,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
>  	}
>  
>  	size = (size_t)perf_atoll((char *)size_str);
> -	size_total = (double)size * nr_loops;
> +	size_total = (size_t)size * nr_loops;

No need to cast.

Thanks,
Namhyung

>  
>  	if ((s64)size <= 0) {
>  		fprintf(stderr, "Invalid size:%s\n", size_str);
> -- 
> 2.43.5
>
Re: [PATCH v5 02/14] perf bench mem: Defer type munging of size to float
Posted by Ankur Arora 5 months ago
Namhyung Kim <namhyung@kernel.org> writes:

> On Wed, Jul 09, 2025 at 05:59:14PM -0700, Ankur Arora wrote:
>> Do type conversion to double at the point of use.
>>
>> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
>
> Reviewed-by: Namhyung Kim <namhyung@kernel.org>
>
> A nitpick below.
>
>> ---
>>  tools/perf/bench/mem-functions.c | 14 +++++++-------
>>  1 file changed, 7 insertions(+), 7 deletions(-)
>>
>> diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
>> index 8599ed96ee1f..b8f020379197 100644
>> --- a/tools/perf/bench/mem-functions.c
>> +++ b/tools/perf/bench/mem-functions.c
>> @@ -139,7 +139,7 @@ struct bench_mem_info {
>>  	bool alloc_src;
>>  };
>>
>> -static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, double size_total)
>> +static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, size_t size_total)
>>  {
>>  	const struct function *r = &info->functions[r_idx];
>>  	double result_bps = 0.0;
>> @@ -165,18 +165,18 @@ static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t
>>  	switch (bench_format) {
>>  	case BENCH_FORMAT_DEFAULT:
>>  		if (use_cycles) {
>> -			printf(" %14lf cycles/byte\n", (double)rt.cycles/size_total);
>> +			printf(" %14lf cycles/byte\n", (double)rt.cycles/(double)size_total);
>>  		} else {
>> -			result_bps = size_total/timeval2double(&rt.tv);
>> +			result_bps = (double)size_total/timeval2double(&rt.tv);
>>  			print_bps(result_bps);
>>  		}
>>  		break;
>>
>>  	case BENCH_FORMAT_SIMPLE:
>>  		if (use_cycles) {
>> -			printf("%lf\n", (double)rt.cycles/size_total);
>> +			printf("%lf\n", (double)rt.cycles/(double)size_total);
>>  		} else {
>> -			result_bps = size_total/timeval2double(&rt.tv);
>> +			result_bps = (double)size_total/timeval2double(&rt.tv);
>>  			printf("%lf\n", result_bps);
>>  		}
>>  		break;
>> @@ -199,7 +199,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
>>  {
>>  	int i;
>>  	size_t size;
>> -	double size_total;
>> +	size_t size_total;
>>
>>  	argc = parse_options(argc, argv, options, info->usage, 0);
>>
>> @@ -212,7 +212,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
>>  	}
>>
>>  	size = (size_t)perf_atoll((char *)size_str);
>> -	size_total = (double)size * nr_loops;
>> +	size_total = (size_t)size * nr_loops;
>
> No need to cast.

Ah, yes.

--
ankur