Add a real semaphore implemtation
[fio.git] / fio.c
diff --git a/fio.c b/fio.c
index bf903d92c0c912aa05e8a3ecbe4e47bbc53f712c..3cf2a9b13220a2e6eece254d98547667f04963e5 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -46,7 +46,7 @@ int thread_number = 0;
 int shm_id = 0;
 int temp_stall_ts;
 
-static volatile int startup_sem;
+static struct fio_sem *startup_sem;
 static volatile int fio_abort;
 static int exit_value;
 
@@ -67,7 +67,11 @@ static void terminate_threads(int group_id)
 
        for_each_td(td, i) {
                if (group_id == TERMINATE_ALL || groupid == td->groupid) {
-                       kill(td->pid, SIGQUIT);
+                       /*
+                        * if the thread is running, just let it exit
+                        */
+                       if (td->runstate < TD_RUNNING)
+                               kill(td->pid, SIGQUIT);
                        td->terminate = 1;
                        td->start_delay = 0;
                }
@@ -291,9 +295,8 @@ static void do_verify(struct thread_data *td)
                }
 
                io_u->end_io = verify_io_u;
-requeue:
-               ret = td_io_queue(td, io_u);
 
+               ret = td_io_queue(td, io_u);
                switch (ret) {
                case FIO_Q_COMPLETED:
                        if (io_u->error)
@@ -303,11 +306,12 @@ requeue:
 
                                io_u->xfer_buflen = io_u->resid;
                                io_u->xfer_buf += bytes;
-                               goto requeue;
+                               requeue_io_u(td, &io_u);
+                       } else {
+                               ret = io_u_sync_complete(td, io_u);
+                               if (ret < 0)
+                                       break;
                        }
-                       ret = io_u_sync_complete(td, io_u);
-                       if (ret < 0)
-                               break;
                        continue;
                case FIO_Q_QUEUED:
                        break;
@@ -414,9 +418,8 @@ static void do_io(struct thread_data *td)
                        put_io_u(td, io_u);
                        break;
                }
-requeue:
-               ret = td_io_queue(td, io_u);
 
+               ret = td_io_queue(td, io_u);
                switch (ret) {
                case FIO_Q_COMPLETED:
                        if (io_u->error)
@@ -426,12 +429,13 @@ requeue:
 
                                io_u->xfer_buflen = io_u->resid;
                                io_u->xfer_buf += bytes;
-                               goto requeue;
+                               requeue_io_u(td, &io_u);
+                       } else {
+                               fio_gettime(&comp_time, NULL);
+                               bytes_done = io_u_sync_complete(td, io_u);
+                               if (bytes_done < 0)
+                                       ret = bytes_done;
                        }
-                       fio_gettime(&comp_time, NULL);
-                       bytes_done = io_u_sync_complete(td, io_u);
-                       if (bytes_done < 0)
-                               ret = bytes_done;
                        break;
                case FIO_Q_QUEUED:
                        /*
@@ -652,10 +656,10 @@ static int switch_ioscheduler(struct thread_data *td)
        return 0;
 }
 
-static void clear_io_state(struct thread_data *td)
+static int clear_io_state(struct thread_data *td)
 {
        struct fio_file *f;
-       int i;
+       int i, ret;
 
        td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
        td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
@@ -663,16 +667,17 @@ static void clear_io_state(struct thread_data *td)
 
        td->last_was_sync = 0;
 
-       for_each_file(td, f, i) {
-               f->last_completed_pos = 0;
-
-               f->last_pos = 0;
-               if (td->io_ops->flags & FIO_SYNCIO)
-                       lseek(f->fd, SEEK_SET, 0);
+       for_each_file(td, f, i)
+               td_io_close_file(td, f);
 
-               if (f->file_map)
-                       memset(f->file_map, 0, f->num_maps * sizeof(long));
+       ret = 0;
+       for_each_file(td, f, i) {
+               ret = td_io_open_file(td, f);
+               if (ret)
+                       break;
        }
+
+       return ret;
 }
 
 /*
@@ -683,6 +688,7 @@ static void *thread_main(void *data)
 {
        unsigned long long runtime[2];
        struct thread_data *td = data;
+       int clear_state;
 
        if (!td->use_thread)
                setsid();
@@ -725,21 +731,18 @@ static void *thread_main(void *data)
                goto err;
 
        td_set_runstate(td, TD_INITIALIZED);
-       fio_sem_up(&startup_sem);
-       fio_sem_down(&td->mutex);
+       fio_sem_up(startup_sem);
+       fio_sem_down(td->mutex);
 
        if (!td->create_serialize && setup_files(td))
                goto err;
-       if (open_files(td))
-               goto err;
 
-       /*
-        * Do this late, as some IO engines would like to have the
-        * files setup prior to initializing structures.
-        */
        if (td_io_init(td))
                goto err;
 
+       if (open_files(td))
+               goto err;
+
        if (td->exec_prerun) {
                if (system(td->exec_prerun) < 0)
                        goto err;
@@ -750,6 +753,7 @@ static void *thread_main(void *data)
        getrusage(RUSAGE_SELF, &td->ts.ru_start);
 
        runtime[0] = runtime[1] = 0;
+       clear_state = 0;
        while (td->loops--) {
                fio_gettime(&td->start, NULL);
                memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
@@ -757,7 +761,9 @@ static void *thread_main(void *data)
                if (td->ratemin)
                        memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
 
-               clear_io_state(td);
+               if (clear_state && clear_io_state(td))
+                       break;
+
                prune_io_piece_log(td);
 
                if (td->io_ops->flags & FIO_CPUIO)
@@ -765,6 +771,8 @@ static void *thread_main(void *data)
                else
                        do_io(td);
 
+               clear_state = 1;
+
                if (td_read(td) && td->io_bytes[DDIR_READ])
                        runtime[DDIR_READ] += utime_since_now(&td->start);
                if (td_write(td) && td->io_bytes[DDIR_WRITE])
@@ -776,7 +784,9 @@ static void *thread_main(void *data)
                if (td->verify == VERIFY_NONE)
                        continue;
 
-               clear_io_state(td);
+               if (clear_io_state(td))
+                       break;
+
                fio_gettime(&td->start, NULL);
 
                do_verify(td);
@@ -788,9 +798,11 @@ static void *thread_main(void *data)
        }
 
        update_rusage_stat(td);
-       fio_gettime(&td->end_time, NULL);
-       td->runtime[0] = runtime[0] / 1000;
-       td->runtime[1] = runtime[1] / 1000;
+       td->ts.runtime[0] = runtime[0] / 1000;
+       td->ts.runtime[1] = runtime[1] / 1000;
+       td->ts.total_run_time = mtime_since_now(&td->epoch);
+       td->ts.io_bytes[0] = td->io_bytes[0];
+       td->ts.io_bytes[1] = td->io_bytes[1];
 
        if (td->ts.bw_log)
                finish_log(td, td->ts.bw_log, "bw");
@@ -918,6 +930,8 @@ reaped:
                                perror("pthread_join");
                }
 
+               fio_sem_remove(td->mutex);
+
                (*nr_running)--;
                (*m_rate) -= td->ratemin;
                (*t_rate) -= td->rate;
@@ -1018,7 +1032,6 @@ static void run_threads(void)
                         */
                        td_set_runstate(td, TD_CREATED);
                        map[this_jobs++] = td;
-                       fio_sem_init(&startup_sem, 1);
                        nr_started++;
 
                        if (td->use_thread) {
@@ -1027,14 +1040,13 @@ static void run_threads(void)
                                        nr_started--;
                                }
                        } else {
-                               if (fork())
-                                       fio_sem_down(&startup_sem);
-                               else {
+                               if (!fork()) {
                                        int ret = fork_main(shm_id, i);
 
                                        exit(ret);
                                }
                        }
+                       fio_sem_down(startup_sem);
                }
 
                /*
@@ -1089,7 +1101,7 @@ static void run_threads(void)
                        m_rate += td->ratemin;
                        t_rate += td->rate;
                        todo--;
-                       fio_sem_up(&td->mutex);
+                       fio_sem_up(td->mutex);
                }
 
                reap_threads(&nr_running, &t_rate, &m_rate);
@@ -1139,6 +1151,8 @@ int main(int argc, char *argv[])
                setup_log(&agg_io_log[DDIR_WRITE]);
        }
 
+       startup_sem = fio_sem_init(0);
+
        set_genesis_time();
 
        disk_util_timer_arm();
@@ -1153,5 +1167,6 @@ int main(int argc, char *argv[])
                }
        }
 
+       fio_sem_remove(startup_sem);
        return exit_value;
 }