[PATCH] fio: add option for using threads
authorJens Axboe <axboe@suse.de>
Thu, 10 Nov 2005 10:54:51 +0000 (11:54 +0100)
committerJens Axboe <axboe@suse.de>
Thu, 10 Nov 2005 10:54:51 +0000 (11:54 +0100)
fio-ini.c
fio.c
fio.h

index 190e84e4869558f4d325cea7989dcd6c18e1e901..af939692665f47f625f2f7c2abfff6b314d0f0f8 100644 (file)
--- a/fio-ini.c
+++ b/fio-ini.c
@@ -28,6 +28,7 @@
 #define DEF_VERIFY     (0)
 #define DEF_STONEWALL  (0)
 #define DEF_NUMJOBS    (1)
+#define DEF_USE_THREAD (0)
 
 static int repeatable = DEF_RAND_REPEAT;
 static char *ini_file;
@@ -137,6 +138,7 @@ static struct thread_data *get_new_job(int global, struct thread_data *parent)
        td->verify = parent->verify;
        td->stonewall = parent->stonewall;
        td->numjobs = parent->numjobs;
+       td->use_thread = parent->use_thread;
        memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
 
        return td;
@@ -617,6 +619,12 @@ int parse_jobs_ini(char *file)
                                fgetpos(f, &off);
                                continue;
                        }
+                       if (!strncmp(p, "thread", 6)) {
+                               td->use_thread = 1;
+                               fgetpos(f, &off);
+                               continue;
+                       }
+
                        printf("Client%d: bad option %s\n",td->thread_number,p);
                }
                fsetpos(f, &off);
@@ -663,6 +671,7 @@ static int fill_def_thread(void)
        def_thread.verify = DEF_VERIFY;
        def_thread.stonewall = DEF_STONEWALL;
        def_thread.numjobs = DEF_NUMJOBS;
+       def_thread.use_thread = DEF_USE_THREAD;
 
        return 0;
 }
diff --git a/fio.c b/fio.c
index db7c41559aff1b2a5f383abc36a1fc097fc75138..8fc2f779392b66871943778dd1755de8c8a16d24 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -28,6 +28,7 @@
 #include <time.h>
 #include <math.h>
 #include <assert.h>
+#include <pthread.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
@@ -1320,21 +1321,12 @@ static void update_rusage_stat(struct thread_data *td)
        memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
 }
 
-static void *thread_main(int shm_id, int offset, char *argv[])
+static void *thread_main(void *data)
 {
-       struct thread_data *td;
+       struct thread_data *td = data;
        int ret = 1;
-       void *data;
 
        setsid();
-
-       data = shmat(shm_id, NULL, 0);
-       if (data == (void *) -1) {
-               perror("shmat");
-               return NULL;
-       }
-
-       td = data + offset * sizeof(struct thread_data);
        td->pid = getpid();
 
        if (init_io_u(td))
@@ -1345,8 +1337,6 @@ static void *thread_main(int shm_id, int offset, char *argv[])
                goto err;
        }
 
-       sprintf(argv[0], "fio%d", offset);
-
        if (td->use_aio && init_aio(td))
                goto err;
 
@@ -1425,6 +1415,23 @@ err:
                sem_wait(&td->mutex);
        }
        td_set_runstate(td, TD_EXITED);
+       return NULL;
+
+}
+
+static void *fork_main(int shm_id, int offset)
+{
+       struct thread_data *td;
+       void *data;
+
+       data = shmat(shm_id, NULL, 0);
+       if (data == (void *) -1) {
+               perror("shmat");
+               return NULL;
+       }
+
+       td = data + offset * sizeof(struct thread_data);
+       thread_main(td);
        shmdt(data);
        return NULL;
 }
@@ -1552,14 +1559,19 @@ static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
                        continue;
 
                td_set_runstate(td, TD_REAPED);
-               waitpid(td->pid, NULL, 0);
+
+               if (td->use_thread) {
+                       long ret;
+
+                       if (pthread_join(td->thread, (void *) &ret))
+                               perror("thread_join");
+               } else
+                       waitpid(td->pid, NULL, 0);
+
                (*nr_running)--;
                (*m_rate) -= td->ratemin;
                (*t_rate) -= td->rate;
                check_str_update(td, *nr_running, *t_rate, *m_rate);
-
-               if (td->terminate)
-                       continue;
        }
 }
 
@@ -1634,11 +1646,18 @@ static void run_threads(char *argv[])
                        todo--;
                        nr_started++;
 
-                       if (fork())
-                               sem_wait(&startup_sem);
-                       else {
-                               thread_main(shm_id, i, argv);
-                               exit(0);
+                       if (td->use_thread) {
+                               if (pthread_create(&td->thread, NULL, thread_main, td)) {
+                                       perror("thread_create");
+                                       nr_started--;
+                               }
+                       } else {
+                               if (fork())
+                                       sem_wait(&startup_sem);
+                               else {
+                                       fork_main(shm_id, i);
+                                       exit(0);
+                               }
                        }
                }
 
diff --git a/fio.h b/fio.h
index 8821812821e49519fb9816f9fdd9192aa920083d..ddbf7d7d93cadd0713d6d954989af63b3c649bf4 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -48,6 +48,7 @@ struct verify_header {
 struct thread_data {
        char file_name[256];
        char directory[256];
+       pthread_t thread;
        int thread_number;
        int groupid;
        int error;
@@ -83,6 +84,7 @@ struct thread_data {
        unsigned int verify;
        unsigned int stonewall;
        unsigned int numjobs;
+       unsigned int use_thread;
        cpu_set_t cpumask;
 
        struct drand48_data bsrange_state;