All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Vishal Chourasia <vishalc@linux.ibm.com>
Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>,
	Shrikanth Hegde <sshegde@linux.vnet.ibm.com>,
	raj.khem@gmail.com, chris@mips.com, ltp@lists.linux.it,
	tdavies@darkphysics.net, gaowanlong@cn.fujitsu.com
Subject: Re: [LTP] [PATCH 1/2] Enhanced thread safety in ebizzy benchmark tool
Date: Mon, 14 Aug 2023 17:15:11 +0200	[thread overview]
Message-ID: <ZNpE_7LKnB6ZGFbK@yuki> (raw)
In-Reply-To: <20230814061810.2297146-2-vishalc@linux.ibm.com>

Hi!
> 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;

Can't we just initialze the mutex statically?

i.e.:

static pthread_mutex_t record_read_lock =  PTHREAD_MUTEX_INITIALIZER;


Then we don't have to intialize/destroy it.

Or even better we can rework the code so that the value is passed up to
the pthread_join() function which would serialize the code naturally,
something as:

diff --git a/utils/benchmark/ebizzy-0.3/ebizzy.c b/utils/benchmark/ebizzy-0.3/ebizzy.c
index 54b047130..841bf0a1c 100644
--- a/utils/benchmark/ebizzy-0.3/ebizzy.c
+++ b/utils/benchmark/ebizzy-0.3/ebizzy.c
@@ -50,6 +50,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <stdint.h>

 #include "ebizzy.h"

@@ -83,7 +84,6 @@ static char **hole_mem;
 static unsigned int page_size;
 static time_t start_time;
 static volatile int threads_go;
-static unsigned int records_read;

 static void usage(void)
 {
@@ -423,6 +423,8 @@ static unsigned int search_mem(void)

 static void *thread_run(void *arg __attribute__((unused)))
 {
+       uintptr_t records_thread;
+
        if (verbose > 1)
                printf("Thread started\n");

@@ -430,13 +432,13 @@ static void *thread_run(void *arg __attribute__((unused)))

        while (threads_go == 0) ;

-       records_read += search_mem();
+       records_thread = search_mem();

        if (verbose > 1)
                printf("Thread finished, %f seconds\n",
                       difftime(time(NULL), start_time));

-       return NULL;
+       return (void *)records_thread;
 }

 static struct timeval difftimeval(struct timeval *end, struct timeval *start)
@@ -454,6 +456,7 @@ static void start_threads(void)
        unsigned int i;
        struct rusage start_ru, end_ru;
        struct timeval usr_time, sys_time;
+       unsigned int records_read = 0;
        int err;

        if (verbose)
@@ -484,11 +487,13 @@ static void start_threads(void)
         */

        for (i = 0; i < threads; i++) {
-               err = pthread_join(thread_array[i], NULL);
+               uintptr_t record_thread;
+               err = pthread_join(thread_array[i], (void *)&record_thread);
                if (err) {
                        fprintf(stderr, "Error joining thread %d\n", i);
                        exit(1);
                }
+               records_read += record_thread;
        }

        if (verbose)


>  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
> 

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2023-08-14 15:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-14  6:18 [LTP] [PATCH 0/2] Ebizzy Benchmark Tool: Enhancements for Thread Safety and Record Count Handling Vishal Chourasia
2023-08-14  6:18 ` [LTP] [PATCH 1/2] Enhanced thread safety in ebizzy benchmark tool Vishal Chourasia
2023-08-14 15:15   ` Cyril Hrubis [this message]
2023-08-14 19:42     ` Vishal Chourasia
2023-08-14  6:18 ` [LTP] [PATCH 2/2] Handle high record counts and prevent integer overflow Vishal Chourasia
  -- strict thread matches above, loose matches on Subject: below --
2023-08-11 10:03 [LTP] [PATCH 0/2] Ebizzy Benchmark Tool: Enhancements for Thread Safety and Record Count Handling Vishal Chourasia
2023-08-11 10:03 ` [LTP] [PATCH 1/2] Enhanced thread safety in ebizzy benchmark tool Vishal Chourasia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZNpE_7LKnB6ZGFbK@yuki \
    --to=chrubis@suse.cz \
    --cc=chris@mips.com \
    --cc=gaowanlong@cn.fujitsu.com \
    --cc=ltp@lists.linux.it \
    --cc=raj.khem@gmail.com \
    --cc=srikar@linux.vnet.ibm.com \
    --cc=sshegde@linux.vnet.ibm.com \
    --cc=tdavies@darkphysics.net \
    --cc=vishalc@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.