[LTP] [PATCH 1/2] Enhanced thread safety in ebizzy benchmark tool

Vishal Chourasia vishalc@linux.ibm.com
Mon Aug 14 08:18:09 CEST 2023


Modified ebizzy.c to improve thread safety by introducing a mutex for
'records_read' shared variable.

Reviewed-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Reviewed-by: Shrikanth Hegde <sshegde@linux.vnet.ibm.com>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
---
 utils/benchmark/ebizzy-0.3/ebizzy.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/utils/benchmark/ebizzy-0.3/ebizzy.c b/utils/benchmark/ebizzy-0.3/ebizzy.c
index 54b047130..1af004d9d 100644
--- a/utils/benchmark/ebizzy-0.3/ebizzy.c
+++ b/utils/benchmark/ebizzy-0.3/ebizzy.c
@@ -85,6 +85,8 @@ static time_t start_time;
 static volatile int threads_go;
 static unsigned int records_read;
 
+pthread_mutex_t records_read_lock;
+
 static void usage(void)
 {
 	fprintf(stderr, "Usage: %s [options]\n"
@@ -430,7 +432,9 @@ static void *thread_run(void *arg __attribute__((unused)))
 
 	while (threads_go == 0) ;
 
+	pthread_mutex_lock(&records_read_lock);
 	records_read += search_mem();
+	pthread_mutex_unlock(&records_read_lock);
 
 	if (verbose > 1)
 		printf("Thread finished, %f seconds\n",
@@ -456,6 +460,12 @@ static void start_threads(void)
 	struct timeval usr_time, sys_time;
 	int err;
 
+	/* Initialize the mutex before starting the threads */
+	if (pthread_mutex_init(&records_read_lock, NULL) != 0) {
+		fprintf(stderr, "Failed to initialize mutex\n");
+		exit(1);
+	}
+
 	if (verbose)
 		printf("Threads starting\n");
 
@@ -491,6 +501,8 @@ static void start_threads(void)
 		}
 	}
 
+	pthread_mutex_destroy(&records_read_lock);
+
 	if (verbose)
 		printf("Threads finished\n");
 
-- 
2.39.3



More information about the ltp mailing list