[LTP] [PATCH v3] ebizzy: prevent integer overflow in 64-bit systems

Vishal Chourasia vishalc@linux.ibm.com
Tue Aug 29 12:13:27 CEST 2023


By handling the rate computation within the thread joining loop, we've optimized
our storage, ensuring we don't risk integer overflow, particularly in 64-bit
systems. Given that `uintptr_t` can handle values up to `(2^64)-1`, to create an
overflow with the current implementation on a system with 9,000,000,000
records/sec values, one would need to run more than (2^64-1)/9 billion threads,
which is astronomically high and practically impossible.

This change reinforces the reliability of our code on high-performance, 64-bit
systems, ensuring accurate record counting without potential overflows.

In the `start_threads` function of the `ebizzy` benchmark utility, the variable
previously named `records_read` has been refactored to `records_per_sec` to more
accurately reflect its purpose, which is to store the rate of records per
second.

Reviewed-by: Shrikanth Hegde <sshegde@linux.vnet.ibm.com>
Reviewed-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>

---
 utils/benchmark/ebizzy-0.3/ebizzy.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/utils/benchmark/ebizzy-0.3/ebizzy.c b/utils/benchmark/ebizzy-0.3/ebizzy.c
index ae0981fbd..ffc439a24 100644
--- a/utils/benchmark/ebizzy-0.3/ebizzy.c
+++ b/utils/benchmark/ebizzy-0.3/ebizzy.c
@@ -456,7 +456,7 @@ static void start_threads(void)
 	unsigned int i;
 	struct rusage start_ru, end_ru;
 	struct timeval usr_time, sys_time;
-	uintptr_t records_read = 0;
+	double records_per_sec = 0.0; 
 	int err;
 
 	if (verbose)
@@ -493,14 +493,13 @@ static void start_threads(void)
 			fprintf(stderr, "Error joining thread %d\n", i);
 			exit(1);
 		}
-		records_read += record_thread;
+		records_per_sec += ((double)record_thread / elapsed);
 	}
 
 	if (verbose)
 		printf("Threads finished\n");
 
-	printf("%tu records/s\n",
-	       (uintptr_t)(((double)records_read) / elapsed));
+	printf("%tu records/s\n", (uintptr_t) records_per_sec); 
 
 	usr_time = difftimeval(&end_ru.ru_utime, &start_ru.ru_utime);
 	sys_time = difftimeval(&end_ru.ru_stime, &start_ru.ru_stime);
-- 
2.39.3



More information about the ltp mailing list