From: Jens Axboe Date: Thu, 10 Nov 2005 10:54:51 +0000 (+0100) Subject: [PATCH] fio: add option for using threads X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=189873de9184b5064816cba487645caaf94d67f7;p=disktools.git [PATCH] fio: add option for using threads --- diff --git a/fio-ini.c b/fio-ini.c index 190e84e..af93969 100644 --- 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 db7c415..8fc2f77 100644 --- a/fio.c +++ b/fio.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -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 8821812..ddbf7d7 100644 --- 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;