log: fix averaged latency logging
[fio.git] / backend.c
index 45ee156948ed03e9ba63055e20f82c20a44fc6c1..58c77cbc6fa3c2862b29eba289efe7ca2bb2bed5 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -603,6 +603,15 @@ static void do_verify(struct thread_data *td, uint64_t verify_bytes)
        if (td->error)
                return;
 
+       /*
+        * verify_state needs to be reset before verification
+        * proceeds so that expected random seeds match actual
+        * random seeds in headers. The main loop will reset
+        * all random number generators if randrepeat is set.
+        */
+       if (!td->o.rand_repeatable)
+               td_fill_verify_state_seed(td);
+
        td_set_runstate(td, TD_VERIFYING);
 
        io_u = NULL;
@@ -1428,8 +1437,6 @@ static void *thread_main(void *data)
        struct thread_data *td = fd->td;
        struct thread_options *o = &td->o;
        struct sk_out *sk_out = fd->sk_out;
-       pthread_condattr_t attr;
-       pthread_mutexattr_t mattr;
        int clear_state;
        int ret;
 
@@ -1456,34 +1463,16 @@ static void *thread_main(void *data)
        INIT_FLIST_HEAD(&td->next_rand_list);
        td->io_hist_tree = RB_ROOT;
 
-       ret = pthread_mutexattr_init(&mattr);
-       if (ret) {
-               td_verror(td, ret, "pthread_mutexattr_init");
-               goto err;
-       }
-#ifdef FIO_HAVE_PSHARED_MUTEX
-       ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
-       if (ret) {
-               td_verror(td, ret, "pthread_mutexattr_setpshared");
-               goto err;
-       }
-#endif
-       pthread_mutex_init(&td->io_u_lock, &mattr);
-
-       ret = pthread_condattr_init(&attr);
+       ret = mutex_cond_init_pshared(&td->io_u_lock, &td->free_cond);
        if (ret) {
-               td_verror(td, ret, "pthread_condattr_init");
+               td_verror(td, ret, "mutex_cond_init_pshared");
                goto err;
        }
-#ifdef FIO_HAVE_PSHARED_MUTEX
-       ret = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+       ret = cond_init_pshared(&td->verify_cond);
        if (ret) {
-               td_verror(td, ret, "pthread_condattr_setpshared");
+               td_verror(td, ret, "mutex_cond_pshared");
                goto err;
        }
-#endif
-       pthread_cond_init(&td->verify_cond, &attr);
-       pthread_cond_init(&td->free_cond, &attr);
 
        td_set_runstate(td, TD_INITIALIZED);
        dprint(FD_MUTEX, "up startup_mutex\n");
@@ -1819,6 +1808,39 @@ err:
        return (void *) (uintptr_t) td->error;
 }
 
+
+/*
+ * We cannot pass the td data into a forked process, so attach the td and
+ * pass it to the thread worker.
+ */
+static int fork_main(struct sk_out *sk_out, int shmid, int offset)
+{
+       struct fork_data *fd;
+       void *data, *ret;
+
+#if !defined(__hpux) && !defined(CONFIG_NO_SHM)
+       data = shmat(shmid, NULL, 0);
+       if (data == (void *) -1) {
+               int __err = errno;
+
+               perror("shmat");
+               return __err;
+       }
+#else
+       /*
+        * HP-UX inherits shm mappings?
+        */
+       data = threads;
+#endif
+
+       fd = calloc(1, sizeof(*fd));
+       fd->td = data + offset * sizeof(struct thread_data);
+       fd->sk_out = sk_out;
+       ret = thread_main(fd);
+       shmdt(data);
+       return (int) (uintptr_t) ret;
+}
+
 static void dump_td_info(struct thread_data *td)
 {
        log_err("fio: job '%s' (state=%d) hasn't exited in %lu seconds, it "
@@ -2156,7 +2178,6 @@ reap:
                struct thread_data *map[REAL_MAX_JOBS];
                struct timeval this_start;
                int this_jobs = 0, left;
-               struct fork_data *fd;
 
                /*
                 * create threads (TD_NOT_CREATED -> TD_CREATED)
@@ -2206,13 +2227,14 @@ reap:
                        map[this_jobs++] = td;
                        nr_started++;
 
-                       fd = calloc(1, sizeof(*fd));
-                       fd->td = td;
-                       fd->sk_out = sk_out;
-
                        if (td->o.use_thread) {
+                               struct fork_data *fd;
                                int ret;
 
+                               fd = calloc(1, sizeof(*fd));
+                               fd->td = td;
+                               fd->sk_out = sk_out;
+
                                dprint(FD_PROCESS, "will pthread_create\n");
                                ret = pthread_create(&td->thread, NULL,
                                                        thread_main, fd);
@@ -2232,9 +2254,8 @@ reap:
                                dprint(FD_PROCESS, "will fork\n");
                                pid = fork();
                                if (!pid) {
-                                       int ret;
+                                       int ret = fork_main(sk_out, shm_id, i);
 
-                                       ret = (int)(uintptr_t)thread_main(fd);
                                        _exit(ret);
                                } else if (i == fio_debug_jobno)
                                        *fio_debug_jobp = pid;
@@ -2376,7 +2397,7 @@ int fio_backend(struct sk_out *sk_out)
                        for (i = 0; i < DDIR_RWDIR_CNT; i++) {
                                struct io_log *log = agg_io_log[i];
 
-                               flush_log(log, 0);
+                               flush_log(log, false);
                                free_log(log);
                        }
                }