2 * This file contains job initialization and setup functions.
15 #include <sys/types.h>
25 #define DEF_TIMEOUT (0)
26 #define DEF_RATE_CYCLE (1000)
27 #define DEF_ODIRECT (1)
28 #define DEF_IO_ENGINE (FIO_SYNCIO)
29 #define DEF_IO_ENGINE_NAME "sync"
30 #define DEF_SEQUENTIAL (1)
31 #define DEF_RAND_REPEAT (1)
32 #define DEF_OVERWRITE (1)
33 #define DEF_INVALIDATE (1)
34 #define DEF_SYNCIO (0)
35 #define DEF_RANDSEED (0xb1899bedUL)
36 #define DEF_BWAVGTIME (500)
37 #define DEF_CREATE_SER (1)
38 #define DEF_CREATE_FSYNC (1)
40 #define DEF_VERIFY (0)
41 #define DEF_STONEWALL (0)
42 #define DEF_NUMJOBS (1)
43 #define DEF_USE_THREAD (0)
44 #define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
45 #define DEF_ZONE_SIZE (0)
46 #define DEF_ZONE_SKIP (0)
47 #define DEF_RWMIX_CYCLE (500)
48 #define DEF_RWMIX_READ (50)
50 #define DEF_NR_FILES (1)
51 #define DEF_UNLINK (0)
52 #define DEF_WRITE_BW_LOG (0)
53 #define DEF_WRITE_LAT_LOG (0)
54 #define DEF_NO_RAND_MAP (0)
56 #define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
58 static int str_rw_cb(void *, const char *);
59 static int str_ioengine_cb(void *, const char *);
60 static int str_mem_cb(void *, const char *);
61 static int str_verify_cb(void *, const char *);
62 static int str_lockmem_cb(void *, unsigned long *);
63 static int str_prio_cb(void *, unsigned int *);
64 static int str_prioclass_cb(void *, unsigned int *);
65 static int str_exitall_cb(void);
66 static int str_cpumask_cb(void *, unsigned int *);
69 * Map of job/command line options
71 static struct fio_option options[] = {
74 .type = FIO_OPT_STR_STORE,
75 .off1 = td_var_offset(name),
79 .type = FIO_OPT_STR_STORE,
80 .off1 = td_var_offset(directory),
84 .type = FIO_OPT_STR_STORE,
85 .off1 = td_var_offset(filename),
95 .cb = str_ioengine_cb,
108 .name = "write_iolog",
109 .type = FIO_OPT_STR_STORE,
110 .off1 = td_var_offset(write_iolog_file),
113 .name = "read_iolog",
114 .type = FIO_OPT_STR_STORE,
115 .off1 = td_var_offset(read_iolog_file),
118 .name = "exec_prerun",
119 .type = FIO_OPT_STR_STORE,
120 .off1 = td_var_offset(exec_prerun),
123 .name = "exec_postrun",
124 .type = FIO_OPT_STR_STORE,
125 .off1 = td_var_offset(exec_postrun),
127 #ifdef FIO_HAVE_IOSCHED_SWITCH
129 .name = "ioscheduler",
130 .type = FIO_OPT_STR_STORE,
131 .off1 = td_var_offset(ioscheduler),
136 .type = FIO_OPT_STR_VAL,
137 .off1 = td_var_offset(total_file_size),
141 .type = FIO_OPT_STR_VAL,
142 .off1 = td_var_offset(bs),
146 .type = FIO_OPT_STR_VAL,
147 .off1 = td_var_offset(start_offset),
151 .type = FIO_OPT_STR_VAL,
152 .off1 = td_var_offset(zone_size),
156 .type = FIO_OPT_STR_VAL,
157 .off1 = td_var_offset(zone_skip),
161 .type = FIO_OPT_STR_VAL,
162 .cb = str_lockmem_cb,
166 .type = FIO_OPT_RANGE,
167 .off1 = td_var_offset(min_bs),
168 .off2 = td_var_offset(max_bs),
173 .off1 = td_var_offset(nr_files),
178 .off1 = td_var_offset(iodepth),
183 .off1 = td_var_offset(fsync_blocks),
186 .name = "rwmixcycle",
188 .off1 = td_var_offset(rwmixcycle),
193 .off1 = td_var_offset(rwmixread),
197 .name = "rwmixwrite",
199 .off1 = td_var_offset(rwmixwrite),
205 .off1 = td_var_offset(nice),
207 #ifdef FIO_HAVE_IOPRIO
216 .cb = str_prioclass_cb,
222 .off1 = td_var_offset(thinktime)
227 .off1 = td_var_offset(rate)
232 .off1 = td_var_offset(ratemin)
237 .off1 = td_var_offset(ratecycle)
240 .name = "startdelay",
242 .off1 = td_var_offset(start_delay)
246 .type = FIO_OPT_STR_VAL_TIME,
247 .off1 = td_var_offset(timeout)
250 .name = "invalidate",
252 .off1 = td_var_offset(invalidate_cache)
257 .off1 = td_var_offset(sync_io)
262 .off1 = td_var_offset(bw_avg_time)
265 .name = "create_serialize",
267 .off1 = td_var_offset(create_serialize)
270 .name = "create_fsync",
272 .off1 = td_var_offset(create_fsync)
277 .off1 = td_var_offset(loops)
282 .off1 = td_var_offset(numjobs)
287 .off1 = td_var_offset(cpuload)
292 .off1 = td_var_offset(cpucycle)
297 .off1 = td_var_offset(odirect)
302 .off1 = td_var_offset(overwrite)
304 #ifdef FIO_HAVE_CPU_AFFINITY
308 .cb = str_cpumask_cb,
314 .off1 = td_var_offset(end_fsync)
318 .type = FIO_OPT_STR_SET,
319 .off1 = td_var_offset(unlink),
323 .type = FIO_OPT_STR_SET,
324 .cb = str_exitall_cb,
328 .type = FIO_OPT_STR_SET,
329 .off1 = td_var_offset(stonewall),
333 .type = FIO_OPT_STR_SET,
334 .off1 = td_var_offset(thread),
337 .name = "write_bw_log",
338 .type = FIO_OPT_STR_SET,
339 .off1 = td_var_offset(write_bw_log),
342 .name = "write_lat_log",
343 .type = FIO_OPT_STR_SET,
344 .off1 = td_var_offset(write_lat_log),
347 .name = "norandommap",
348 .type = FIO_OPT_STR_SET,
349 .off1 = td_var_offset(norandommap),
352 .name = "bs_unaligned",
353 .type = FIO_OPT_STR_SET,
354 .off1 = td_var_offset(bs_unaligned),
361 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
362 #define FIO_CMD_OPTS (16)
363 #define FIO_GETOPT_JOB (0x89988998)
366 * Command line options. These will contain the above, plus a few
367 * extra that only pertain to fio itself and not jobs.
369 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
372 .has_arg = required_argument,
377 .has_arg = required_argument,
381 .name = "latency-log",
382 .has_arg = required_argument,
386 .name = "bandwidth-log",
387 .has_arg = required_argument,
392 .has_arg = optional_argument,
397 .has_arg = no_argument,
405 static int def_timeout = DEF_TIMEOUT;
407 static char fio_version_string[] = "fio 1.7";
409 static char **ini_file;
410 static int max_jobs = MAX_JOBS;
412 struct thread_data def_thread;
413 struct thread_data *threads = NULL;
416 int exitall_on_terminate = 0;
417 int terse_output = 0;
418 unsigned long long mlock_size = 0;
422 static int write_lat_log = DEF_WRITE_LAT_LOG;
423 static int write_bw_log = DEF_WRITE_BW_LOG;
426 * Return a free job structure.
428 static struct thread_data *get_new_job(int global, struct thread_data *parent)
430 struct thread_data *td;
434 if (thread_number >= max_jobs)
437 td = &threads[thread_number++];
440 td->thread_number = thread_number;
444 static void put_job(struct thread_data *td)
446 if (td == &def_thread)
449 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
454 * Lazy way of fixing up options that depend on each other. We could also
455 * define option callback handlers, but this is easier.
457 static void fixup_options(struct thread_data *td)
459 if (!td->rwmixread && td->rwmixwrite)
460 td->rwmixread = 100 - td->rwmixwrite;
462 if (td->write_iolog_file && td->read_iolog_file) {
463 log_err("fio: read iolog overrides write_iolog\n");
464 free(td->write_iolog_file);
465 td->write_iolog_file = NULL;
468 if (td->io_ops->flags & FIO_SYNCIO)
472 td->iodepth = td->nr_files;
476 * only really works for sequential io for now, and with 1 file
478 if (td->zone_size && !td->sequential && td->nr_files == 1)
482 * Reads can do overwrites, we always need to pre-create the file
484 if (td_read(td) || td_rw(td))
491 if (td_read(td) && !td_rw(td))
494 if (td->norandommap && td->verify != VERIFY_NONE) {
495 log_err("fio: norandommap given, verify disabled\n");
496 td->verify = VERIFY_NONE;
498 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
499 log_err("fio: bs_unaligned may not work with raw io\n");
503 * Adds a job to the list of things todo. Sanitizes the various options
504 * to make sure we don't have conflicts, and initializes various
507 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
509 char *ddir_str[] = { "read", "write", "randread", "randwrite",
510 "rw", NULL, "randrw" };
512 int numjobs, ddir, i;
515 #ifndef FIO_HAVE_LIBAIO
516 if (td->io_engine == FIO_LIBAIO) {
517 log_err("Linux libaio not available\n");
521 #ifndef FIO_HAVE_POSIXAIO
522 if (td->io_engine == FIO_POSIXAIO) {
523 log_err("posix aio not available\n");
529 * the def_thread is just for options, it's not a real job
531 if (td == &def_thread)
535 * Set default io engine, if none set
538 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
540 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
546 td->io_ops->flags |= FIO_RAWIO;
550 td->filetype = FIO_TYPE_FILE;
551 if (!stat(jobname, &sb)) {
552 if (S_ISBLK(sb.st_mode))
553 td->filetype = FIO_TYPE_BD;
554 else if (S_ISCHR(sb.st_mode))
555 td->filetype = FIO_TYPE_CHAR;
559 td->nr_uniq_files = 1;
561 td->nr_uniq_files = td->nr_files;
563 if (td->filetype == FIO_TYPE_FILE || td->filename) {
568 if (td->directory && td->directory[0] != '\0')
569 sprintf(tmp, "%s/", td->directory);
571 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
573 for_each_file(td, f, i) {
574 memset(f, 0, sizeof(*f));
578 sprintf(tmp + len, "%s", td->filename);
580 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
581 f->file_name = strdup(tmp);
585 td->files = malloc(sizeof(struct fio_file));
588 memset(f, 0, sizeof(*f));
590 f->file_name = strdup(jobname);
593 for_each_file(td, f, i) {
594 f->file_size = td->total_file_size / td->nr_files;
595 f->file_offset = td->start_offset;
598 fio_sem_init(&td->mutex, 0);
600 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
601 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
602 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
604 if (td->stonewall && td->thread_number > 1)
607 td->groupid = groupid;
612 if (td->write_lat_log) {
613 setup_log(&td->slat_log);
614 setup_log(&td->clat_log);
616 if (td->write_bw_log)
617 setup_log(&td->bw_log);
620 td->name = strdup(jobname);
622 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
626 if (td->io_ops->flags & FIO_CPUIO)
627 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
629 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_ops->name, td->iodepth);
630 } else if (job_add_num == 1)
631 fprintf(f_out, "...\n");
635 * recurse add identical jobs, clear numjobs and stonewall options
636 * as they don't apply to sub-jobs
638 numjobs = td->numjobs;
640 struct thread_data *td_new = get_new_job(0, td);
646 td_new->stonewall = 0;
647 job_add_num = numjobs - 1;
649 if (add_job(td_new, jobname, job_add_num))
659 * Initialize the various random states we need (random io, block size ranges,
660 * read/write mix, etc).
662 int init_random_state(struct thread_data *td)
664 unsigned long seeds[4];
665 int fd, num_maps, blocks, i;
668 if (td->io_ops->flags & FIO_CPUIO)
671 fd = open("/dev/urandom", O_RDONLY);
673 td_verror(td, errno);
677 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
685 os_random_seed(seeds[0], &td->bsrange_state);
686 os_random_seed(seeds[1], &td->verify_state);
687 os_random_seed(seeds[2], &td->rwmix_state);
692 if (td->rand_repeatable)
693 seeds[3] = DEF_RANDSEED;
695 if (!td->norandommap) {
696 for_each_file(td, f, i) {
697 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
698 num_maps = blocks / BLOCKS_PER_MAP;
699 f->file_map = malloc(num_maps * sizeof(long));
700 f->num_maps = num_maps;
701 memset(f->file_map, 0, num_maps * sizeof(long));
705 os_random_seed(seeds[3], &td->random_state);
709 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
711 #ifdef FIO_HAVE_CPU_AFFINITY
716 for (i = 0; i < sizeof(int) * 8; i++) {
718 CPU_SET(i, &cpumask);
723 static int is_empty_or_comment(char *line)
727 for (i = 0; i < strlen(line); i++) {
730 if (!isspace(line[i]) && !iscntrl(line[i]))
737 static int str_rw_cb(void *data, const char *mem)
739 struct thread_data *td = data;
741 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
742 td->ddir = DDIR_READ;
745 } else if (!strncmp(mem, "randread", 8)) {
746 td->ddir = DDIR_READ;
749 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
750 td->ddir = DDIR_WRITE;
753 } else if (!strncmp(mem, "randwrite", 9)) {
754 td->ddir = DDIR_WRITE;
757 } else if (!strncmp(mem, "rw", 2)) {
762 } else if (!strncmp(mem, "randrw", 6)) {
769 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
773 static int str_verify_cb(void *data, const char *mem)
775 struct thread_data *td = data;
777 if (!strncmp(mem, "0", 1)) {
778 td->verify = VERIFY_NONE;
780 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
781 td->verify = VERIFY_MD5;
783 } else if (!strncmp(mem, "crc32", 5)) {
784 td->verify = VERIFY_CRC32;
788 log_err("fio: verify types: md5, crc32\n");
792 static int str_mem_cb(void *data, const char *mem)
794 struct thread_data *td = data;
796 if (!strncmp(mem, "malloc", 6)) {
797 td->mem_type = MEM_MALLOC;
799 } else if (!strncmp(mem, "shm", 3)) {
800 td->mem_type = MEM_SHM;
802 } else if (!strncmp(mem, "mmap", 4)) {
803 td->mem_type = MEM_MMAP;
807 log_err("fio: mem type: malloc, shm, mmap\n");
811 static int str_ioengine_cb(void *data, const char *str)
813 struct thread_data *td = data;
815 td->io_ops = load_ioengine(td, str);
819 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
823 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
829 static int str_prioclass_cb(void *data, unsigned int *val)
831 struct thread_data *td = data;
833 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
837 static int str_prio_cb(void *data, unsigned int *val)
839 struct thread_data *td = data;
845 static int str_exitall_cb(void)
847 exitall_on_terminate = 1;
851 static int str_cpumask_cb(void *data, unsigned int *val)
853 struct thread_data *td = data;
855 fill_cpu_mask(td->cpumask, *val);
860 * This is our [ini] type file parser.
862 int parse_jobs_ini(char *file, int stonewall_flag)
865 struct thread_data *td;
870 int ret = 0, stonewall;
872 f = fopen(file, "r");
874 perror("fopen job file");
878 string = malloc(4096);
880 memset(name, 0, 256);
882 stonewall = stonewall_flag;
884 p = fgets(string, 4095, f);
887 if (is_empty_or_comment(p))
889 if (sscanf(p, "[%255s]", name) != 1)
892 global = !strncmp(name, "global", 6);
894 name[strlen(name) - 1] = '\0';
896 td = get_new_job(global, &def_thread);
903 * Seperate multiple job files by a stonewall
905 if (!global && stonewall) {
906 td->stonewall = stonewall;
911 while ((p = fgets(string, 4096, f)) != NULL) {
912 if (is_empty_or_comment(p))
915 strip_blank_front(&p);
925 * Don't break here, continue parsing options so we
926 * dump all the bad ones. Makes trial/error fixups
927 * easier on the user.
929 ret |= parse_option(p, options, td);
934 ret = add_job(td, name, 0);
936 log_err("fio: job %s dropped\n", name);
947 static int fill_def_thread(void)
949 memset(&def_thread, 0, sizeof(def_thread));
951 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
952 perror("sched_getaffinity");
959 def_thread.ddir = DDIR_READ;
960 def_thread.iomix = 0;
961 def_thread.bs = DEF_BS;
962 def_thread.min_bs = 0;
963 def_thread.max_bs = 0;
964 def_thread.odirect = DEF_ODIRECT;
965 def_thread.ratecycle = DEF_RATE_CYCLE;
966 def_thread.sequential = DEF_SEQUENTIAL;
967 def_thread.timeout = def_timeout;
968 def_thread.overwrite = DEF_OVERWRITE;
969 def_thread.invalidate_cache = DEF_INVALIDATE;
970 def_thread.sync_io = DEF_SYNCIO;
971 def_thread.mem_type = MEM_MALLOC;
972 def_thread.bw_avg_time = DEF_BWAVGTIME;
973 def_thread.create_serialize = DEF_CREATE_SER;
974 def_thread.create_fsync = DEF_CREATE_FSYNC;
975 def_thread.loops = DEF_LOOPS;
976 def_thread.verify = DEF_VERIFY;
977 def_thread.stonewall = DEF_STONEWALL;
978 def_thread.numjobs = DEF_NUMJOBS;
979 def_thread.use_thread = DEF_USE_THREAD;
980 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
981 def_thread.rwmixread = DEF_RWMIX_READ;
982 def_thread.nice = DEF_NICE;
983 def_thread.rand_repeatable = DEF_RAND_REPEAT;
984 def_thread.nr_files = DEF_NR_FILES;
985 def_thread.unlink = DEF_UNLINK;
986 def_thread.write_bw_log = write_bw_log;
987 def_thread.write_lat_log = write_lat_log;
988 def_thread.norandommap = DEF_NO_RAND_MAP;
989 #ifdef FIO_HAVE_DISK_UTIL
990 def_thread.do_disk_util = 1;
996 static void usage(void)
998 printf("%s\n", fio_version_string);
999 printf("\t--output\tWrite output to file\n");
1000 printf("\t--timeout\tRuntime in seconds\n");
1001 printf("\t--latency-log\tGenerate per-job latency logs\n");
1002 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1003 printf("\t--minimal\tMinimal (terse) output\n");
1004 printf("\t--version\tPrint version info and exit\n");
1007 static int parse_cmd_line(int argc, char *argv[])
1009 struct thread_data *td = NULL;
1010 int c, ini_idx = 0, lidx, ret;
1012 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
1015 def_timeout = atoi(optarg);
1024 f_out = fopen(optarg, "w+");
1026 perror("fopen output");
1038 printf("%s\n", fio_version_string);
1040 case FIO_GETOPT_JOB: {
1041 const char *opt = long_options[lidx].name;
1044 if (!strncmp(opt, "name", 4) && td) {
1045 ret = add_job(td, td->name ?: "fio", 0);
1053 int global = !strncmp(val, "global", 6);
1055 td = get_new_job(global, &def_thread);
1060 ret = parse_cmd_option(opt, val, options, td);
1062 log_err("fio: job dropped\n");
1069 printf("optarg <<%s>>\n", argv[optind]);
1075 ret = add_job(td, td->name ?: "fio", 0);
1080 while (optind < argc) {
1082 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1083 ini_file[ini_idx - 1] = strdup(argv[optind]);
1090 static void free_shm(void)
1092 struct shmid_ds sbuf;
1095 shmdt((void *) threads);
1097 shmctl(shm_id, IPC_RMID, &sbuf);
1102 * The thread area is shared between the main process and the job
1103 * threads/processes. So setup a shared memory segment that will hold
1106 static int setup_thread_area(void)
1109 * 1024 is too much on some machines, scale max_jobs if
1110 * we get a failure that looks like too large a shm segment
1113 size_t size = max_jobs * sizeof(struct thread_data);
1115 shm_id = shmget(0, size, IPC_CREAT | 0600);
1118 if (errno != EINVAL) {
1129 threads = shmat(shm_id, NULL, 0);
1130 if (threads == (void *) -1) {
1140 * Copy the fio options into the long options map, so we mirror
1141 * job and cmd line options.
1143 static void dupe_job_options(void)
1145 struct fio_option *o;
1149 while (long_options[i].name)
1154 long_options[i].name = o->name;
1155 long_options[i].val = FIO_GETOPT_JOB;
1156 if (o->type == FIO_OPT_STR_SET)
1157 long_options[i].has_arg = no_argument;
1159 long_options[i].has_arg = required_argument;
1163 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1167 int parse_options(int argc, char *argv[])
1176 if (setup_thread_area())
1178 if (fill_def_thread())
1181 job_files = parse_cmd_line(argc, argv);
1183 for (i = 0; i < job_files; i++) {
1184 if (fill_def_thread())
1186 if (parse_jobs_ini(ini_file[i], i))
1193 if (!thread_number) {
1194 log_err("No jobs defined(s)\n");