gettime: Rework the clock thread starting mechanism
authorBart Van Assche <bart.vanassche@wdc.com>
Tue, 13 Mar 2018 17:43:02 +0000 (10:43 -0700)
committerBart Van Assche <bart.vanassche@wdc.com>
Thu, 15 Mar 2018 16:56:43 +0000 (09:56 -0700)
Replace two mutexes in struct clock_thread that are intended to make
the clock threads start simultaneously by a fio_mutex.

This patch avoids that drd reports the following:

==23831== Thread 2:
==23831== Mutex not locked by calling thread: mutex 0x6cb7bf8, recursion count 1, owner 1.
==23831==    at 0x4C38EC3: pthread_mutex_unlock_intercept (drd_pthread_intercepts.c:978)
==23831==    by 0x4C38EC3: pthread_mutex_unlock (drd_pthread_intercepts.c:991)
==23831==    by 0x420974: clock_thread_fn (gettime.c:604)
==23831==    by 0x4C34C44: vgDrd_thread_wrapper (drd_pthread_intercepts.c:444)
==23831==    by 0x5E4159A: start_thread (in /lib64/libpthread-2.27.so)
==23831==    by 0x6356A1E: clone (in /lib64/libc-2.27.so)

==23924== Mutex still locked at thread exit: mutex 0x6cb7bd0, recursion count 1, owner 2.
==23924==    at 0x4C35BF2: pthread_join_intercept (drd_pthread_intercepts.c:711)
==23924==    by 0x4C35BF2: pthread_join (drd_pthread_intercepts.c:718)
==23924==    by 0x420D7F: fio_monotonic_clocktest (gettime.c:722)
==23924==    by 0x420441: fio_clock_init (gettime.c:424)
==23924==    by 0x437526: fio_time_init (time.c:141)
==23924==    by 0x49BF41: main (fio.c:56)

Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com>
Makefile
gettime.c

index 9ac9a390e761dad5137f6c475ef30b3f4fde232f..eb3bddd6be095fc7a46bf8764e36eddb06d1a7bb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -231,7 +231,8 @@ T_AXMAP_OBJS += lib/lfsr.o lib/axmap.o
 T_AXMAP_PROGS = t/axmap
 
 T_LFSR_TEST_OBJS = t/lfsr-test.o
 T_AXMAP_PROGS = t/axmap
 
 T_LFSR_TEST_OBJS = t/lfsr-test.o
-T_LFSR_TEST_OBJS += lib/lfsr.o gettime.o t/log.o t/debug.o t/arch.o
+T_LFSR_TEST_OBJS += lib/lfsr.o gettime.o fio_sem.o pshared.o \
+                   t/log.o t/debug.o t/arch.o
 T_LFSR_TEST_PROGS = t/lfsr-test
 
 T_GEN_RAND_OBJS = t/gen-rand.o
 T_LFSR_TEST_PROGS = t/lfsr-test
 
 T_GEN_RAND_OBJS = t/gen-rand.o
index c256a96c16c8f26253a253c82d23d23f4a5d914c..57c66f7e6fbc6a1d8688cbc2d4b6ee0f370c8930 100644 (file)
--- a/gettime.c
+++ b/gettime.c
@@ -8,6 +8,7 @@
 #include <time.h>
 
 #include "fio.h"
 #include <time.h>
 
 #include "fio.h"
+#include "fio_sem.h"
 #include "smalloc.h"
 
 #include "hash.h"
 #include "smalloc.h"
 
 #include "hash.h"
@@ -563,8 +564,7 @@ struct clock_thread {
        pthread_t thread;
        int cpu;
        int debug;
        pthread_t thread;
        int cpu;
        int debug;
-       pthread_mutex_t lock;
-       pthread_mutex_t started;
+       struct fio_sem lock;
        unsigned long nr_entries;
        uint32_t *seq;
        struct clock_entry *entries;
        unsigned long nr_entries;
        uint32_t *seq;
        struct clock_entry *entries;
@@ -600,8 +600,7 @@ static void *clock_thread_fn(void *data)
                goto err;
        }
 
                goto err;
        }
 
-       pthread_mutex_lock(&t->lock);
-       pthread_mutex_unlock(&t->started);
+       fio_sem_down(&t->lock);
 
        first = get_cpu_clock();
        c = &t->entries[0];
 
        first = get_cpu_clock();
        c = &t->entries[0];
@@ -702,9 +701,7 @@ int fio_monotonic_clocktest(int debug)
                t->seq = &seq;
                t->nr_entries = nr_entries;
                t->entries = &entries[i * nr_entries];
                t->seq = &seq;
                t->nr_entries = nr_entries;
                t->entries = &entries[i * nr_entries];
-               pthread_mutex_init(&t->lock, NULL);
-               pthread_mutex_init(&t->started, NULL);
-               pthread_mutex_lock(&t->lock);
+               __fio_sem_init(&t->lock, FIO_SEM_LOCKED);
                if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
                        failed++;
                        nr_cpus = i;
                if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
                        failed++;
                        nr_cpus = i;
@@ -715,13 +712,7 @@ int fio_monotonic_clocktest(int debug)
        for (i = 0; i < nr_cpus; i++) {
                struct clock_thread *t = &cthreads[i];
 
        for (i = 0; i < nr_cpus; i++) {
                struct clock_thread *t = &cthreads[i];
 
-               pthread_mutex_lock(&t->started);
-       }
-
-       for (i = 0; i < nr_cpus; i++) {
-               struct clock_thread *t = &cthreads[i];
-
-               pthread_mutex_unlock(&t->lock);
+               fio_sem_up(&t->lock);
        }
 
        for (i = 0; i < nr_cpus; i++) {
        }
 
        for (i = 0; i < nr_cpus; i++) {
@@ -731,6 +722,7 @@ int fio_monotonic_clocktest(int debug)
                pthread_join(t->thread, &ret);
                if (ret)
                        failed++;
                pthread_join(t->thread, &ret);
                if (ret)
                        failed++;
+               __fio_sem_remove(&t->lock);
        }
        free(cthreads);
 
        }
        free(cthreads);