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)
55 #define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
57 static int str_rw_cb(void *, const char *);
58 static int str_ioengine_cb(void *, const char *);
59 static int str_mem_cb(void *, const char *);
60 static int str_verify_cb(void *, const char *);
61 static int str_lockmem_cb(void *, unsigned long *);
62 static int str_prio_cb(void *, unsigned int *);
63 static int str_prioclass_cb(void *, unsigned int *);
64 static int str_exitall_cb(void);
65 static int str_cpumask_cb(void *, unsigned int *);
68 * Map of job/command line options
70 static struct fio_option options[] = {
73 .type = FIO_OPT_STR_STORE,
74 .off1 = td_var_offset(name),
78 .type = FIO_OPT_STR_STORE,
79 .off1 = td_var_offset(directory),
83 .type = FIO_OPT_STR_STORE,
84 .off1 = td_var_offset(filename),
94 .cb = str_ioengine_cb,
107 .name = "write_iolog",
108 .type = FIO_OPT_STR_STORE,
109 .off1 = td_var_offset(write_iolog_file),
112 .name = "read_iolog",
113 .type = FIO_OPT_STR_STORE,
114 .off1 = td_var_offset(read_iolog_file),
117 .name = "exec_prerun",
118 .type = FIO_OPT_STR_STORE,
119 .off1 = td_var_offset(exec_prerun),
122 .name = "exec_postrun",
123 .type = FIO_OPT_STR_STORE,
124 .off1 = td_var_offset(exec_postrun),
126 #ifdef FIO_HAVE_IOSCHED_SWITCH
128 .name = "ioscheduler",
129 .type = FIO_OPT_STR_STORE,
130 .off1 = td_var_offset(ioscheduler),
135 .type = FIO_OPT_STR_VAL,
136 .off1 = td_var_offset(total_file_size),
140 .type = FIO_OPT_STR_VAL,
141 .off1 = td_var_offset(bs),
145 .type = FIO_OPT_STR_VAL,
146 .off1 = td_var_offset(start_offset),
150 .type = FIO_OPT_STR_VAL,
151 .off1 = td_var_offset(zone_size),
155 .type = FIO_OPT_STR_VAL,
156 .off1 = td_var_offset(zone_skip),
160 .type = FIO_OPT_STR_VAL,
161 .cb = str_lockmem_cb,
165 .type = FIO_OPT_RANGE,
166 .off1 = td_var_offset(min_bs),
167 .off2 = td_var_offset(max_bs),
172 .off1 = td_var_offset(nr_files),
177 .off1 = td_var_offset(iodepth),
182 .off1 = td_var_offset(fsync_blocks),
185 .name = "rwmixcycle",
187 .off1 = td_var_offset(rwmixcycle),
192 .off1 = td_var_offset(rwmixread),
196 .name = "rwmixwrite",
198 .off1 = td_var_offset(rwmixwrite),
204 .off1 = td_var_offset(nice),
206 #ifdef FIO_HAVE_IOPRIO
215 .cb = str_prioclass_cb,
221 .off1 = td_var_offset(thinktime)
226 .off1 = td_var_offset(rate)
231 .off1 = td_var_offset(ratemin)
236 .off1 = td_var_offset(ratecycle)
239 .name = "startdelay",
241 .off1 = td_var_offset(start_delay)
245 .type = FIO_OPT_STR_VAL_TIME,
246 .off1 = td_var_offset(timeout)
249 .name = "invalidate",
251 .off1 = td_var_offset(invalidate_cache)
256 .off1 = td_var_offset(sync_io)
261 .off1 = td_var_offset(bw_avg_time)
264 .name = "create_serialize",
266 .off1 = td_var_offset(create_serialize)
269 .name = "create_fsync",
271 .off1 = td_var_offset(create_fsync)
276 .off1 = td_var_offset(loops)
281 .off1 = td_var_offset(numjobs)
286 .off1 = td_var_offset(cpuload)
291 .off1 = td_var_offset(cpucycle)
296 .off1 = td_var_offset(odirect)
301 .off1 = td_var_offset(overwrite)
303 #ifdef FIO_HAVE_CPU_AFFINITY
307 .cb = str_cpumask_cb,
313 .off1 = td_var_offset(end_fsync)
317 .type = FIO_OPT_STR_SET,
318 .off1 = td_var_offset(unlink),
322 .type = FIO_OPT_STR_SET,
323 .cb = str_exitall_cb,
327 .type = FIO_OPT_STR_SET,
328 .off1 = td_var_offset(stonewall),
332 .type = FIO_OPT_STR_SET,
333 .off1 = td_var_offset(thread),
336 .name = "write_bw_log",
337 .type = FIO_OPT_STR_SET,
338 .off1 = td_var_offset(write_bw_log),
341 .name = "write_lat_log",
342 .type = FIO_OPT_STR_SET,
343 .off1 = td_var_offset(write_lat_log),
350 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
351 #define FIO_CMD_OPTS (16)
352 #define FIO_GETOPT_JOB (0x89988998)
355 * Command line options. These will contain the above, plus a few
356 * extra that only pertain to fio itself and not jobs.
358 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
361 .has_arg = required_argument,
366 .has_arg = required_argument,
370 .name = "latency-log",
371 .has_arg = required_argument,
375 .name = "bandwidth-log",
376 .has_arg = required_argument,
381 .has_arg = optional_argument,
386 .has_arg = no_argument,
394 static int def_timeout = DEF_TIMEOUT;
396 static char fio_version_string[] = "fio 1.7";
398 static char **ini_file;
399 static int max_jobs = MAX_JOBS;
401 struct thread_data def_thread;
402 struct thread_data *threads = NULL;
405 int exitall_on_terminate = 0;
406 int terse_output = 0;
407 unsigned long long mlock_size = 0;
411 static int write_lat_log = DEF_WRITE_LAT_LOG;
412 static int write_bw_log = DEF_WRITE_BW_LOG;
415 * Return a free job structure.
417 static struct thread_data *get_new_job(int global, struct thread_data *parent)
419 struct thread_data *td;
423 if (thread_number >= max_jobs)
426 td = &threads[thread_number++];
429 td->thread_number = thread_number;
433 static void put_job(struct thread_data *td)
435 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
440 * Lazy way of fixing up options that depend on each other. We could also
441 * define option callback handlers, but this is easier.
443 static void fixup_options(struct thread_data *td)
450 if (!td->rwmixread && td->rwmixwrite)
451 td->rwmixread = 100 - td->rwmixwrite;
453 if (td->write_iolog_file && td->read_iolog_file) {
454 log_err("fio: read iolog overrides write_iolog\n");
455 free(td->write_iolog_file);
456 td->write_iolog_file = NULL;
461 * Adds a job to the list of things todo. Sanitizes the various options
462 * to make sure we don't have conflicts, and initializes various
465 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
467 char *ddir_str[] = { "read", "write", "randread", "randwrite",
468 "rw", NULL, "randrw" };
470 int numjobs, ddir, i;
473 #ifndef FIO_HAVE_LIBAIO
474 if (td->io_engine == FIO_LIBAIO) {
475 log_err("Linux libaio not available\n");
479 #ifndef FIO_HAVE_POSIXAIO
480 if (td->io_engine == FIO_POSIXAIO) {
481 log_err("posix aio not available\n");
489 * the def_thread is just for options, it's not a real job
491 if (td == &def_thread)
495 * Set default io engine, if none set
498 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
500 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
505 if (td->io_ops->flags & FIO_SYNCIO)
509 td->iodepth = td->nr_files;
513 * only really works for sequential io for now, and with 1 file
515 if (td->zone_size && !td->sequential && td->nr_files == 1)
519 * Reads can do overwrites, we always need to pre-create the file
521 if (td_read(td) || td_rw(td))
524 td->filetype = FIO_TYPE_FILE;
525 if (!stat(jobname, &sb)) {
526 if (S_ISBLK(sb.st_mode))
527 td->filetype = FIO_TYPE_BD;
528 else if (S_ISCHR(sb.st_mode))
529 td->filetype = FIO_TYPE_CHAR;
533 td->io_ops->flags |= FIO_RAWIO;
536 td->nr_uniq_files = 1;
538 td->nr_uniq_files = td->nr_files;
540 if (td->filetype == FIO_TYPE_FILE || td->filename) {
545 if (td->directory && td->directory[0] != '\0')
546 sprintf(tmp, "%s/", td->directory);
548 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
550 for_each_file(td, f, i) {
551 memset(f, 0, sizeof(*f));
555 sprintf(tmp + len, "%s", td->filename);
557 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
558 f->file_name = strdup(tmp);
562 td->files = malloc(sizeof(struct fio_file));
565 memset(f, 0, sizeof(*f));
567 f->file_name = strdup(jobname);
570 for_each_file(td, f, i) {
571 f->file_size = td->total_file_size / td->nr_files;
572 f->file_offset = td->start_offset;
575 fio_sem_init(&td->mutex, 0);
577 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
578 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
579 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
581 if (td->min_bs == -1U)
583 if (td->max_bs == -1U)
585 if (td_read(td) && !td_rw(td))
588 if (td->stonewall && td->thread_number > 1)
591 td->groupid = groupid;
596 if (td->write_lat_log) {
597 setup_log(&td->slat_log);
598 setup_log(&td->clat_log);
600 if (td->write_bw_log)
601 setup_log(&td->bw_log);
604 td->name = strdup(jobname);
606 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
610 if (td->io_ops->flags & FIO_CPUIO)
611 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
613 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);
614 } else if (job_add_num == 1)
615 fprintf(f_out, "...\n");
619 * recurse add identical jobs, clear numjobs and stonewall options
620 * as they don't apply to sub-jobs
622 numjobs = td->numjobs;
624 struct thread_data *td_new = get_new_job(0, td);
630 td_new->stonewall = 0;
631 job_add_num = numjobs - 1;
633 if (add_job(td_new, jobname, job_add_num))
643 * Initialize the various random states we need (random io, block size ranges,
644 * read/write mix, etc).
646 int init_random_state(struct thread_data *td)
648 unsigned long seeds[4];
649 int fd, num_maps, blocks, i;
652 if (td->io_ops->flags & FIO_CPUIO)
655 fd = open("/dev/urandom", O_RDONLY);
657 td_verror(td, errno);
661 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
669 os_random_seed(seeds[0], &td->bsrange_state);
670 os_random_seed(seeds[1], &td->verify_state);
671 os_random_seed(seeds[2], &td->rwmix_state);
676 if (td->rand_repeatable)
677 seeds[3] = DEF_RANDSEED;
679 for_each_file(td, f, i) {
680 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
681 num_maps = blocks / BLOCKS_PER_MAP;
682 f->file_map = malloc(num_maps * sizeof(long));
683 f->num_maps = num_maps;
684 memset(f->file_map, 0, num_maps * sizeof(long));
687 os_random_seed(seeds[3], &td->random_state);
691 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
693 #ifdef FIO_HAVE_CPU_AFFINITY
698 for (i = 0; i < sizeof(int) * 8; i++) {
700 CPU_SET(i, &cpumask);
705 static int is_empty_or_comment(char *line)
709 for (i = 0; i < strlen(line); i++) {
712 if (!isspace(line[i]) && !iscntrl(line[i]))
719 static int str_rw_cb(void *data, const char *mem)
721 struct thread_data *td = data;
723 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
724 td->ddir = DDIR_READ;
727 } else if (!strncmp(mem, "randread", 8)) {
728 td->ddir = DDIR_READ;
731 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
732 td->ddir = DDIR_WRITE;
735 } else if (!strncmp(mem, "randwrite", 9)) {
736 td->ddir = DDIR_WRITE;
739 } else if (!strncmp(mem, "rw", 2)) {
744 } else if (!strncmp(mem, "randrw", 6)) {
751 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
755 static int str_verify_cb(void *data, const char *mem)
757 struct thread_data *td = data;
759 if (!strncmp(mem, "0", 1)) {
760 td->verify = VERIFY_NONE;
762 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
763 td->verify = VERIFY_MD5;
765 } else if (!strncmp(mem, "crc32", 5)) {
766 td->verify = VERIFY_CRC32;
770 log_err("fio: verify types: md5, crc32\n");
774 static int str_mem_cb(void *data, const char *mem)
776 struct thread_data *td = data;
778 if (!strncmp(mem, "malloc", 6)) {
779 td->mem_type = MEM_MALLOC;
781 } else if (!strncmp(mem, "shm", 3)) {
782 td->mem_type = MEM_SHM;
784 } else if (!strncmp(mem, "mmap", 4)) {
785 td->mem_type = MEM_MMAP;
789 log_err("fio: mem type: malloc, shm, mmap\n");
793 static int str_ioengine_cb(void *data, const char *str)
795 struct thread_data *td = data;
797 td->io_ops = load_ioengine(td, str);
801 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
805 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
811 static int str_prioclass_cb(void *data, unsigned int *val)
813 struct thread_data *td = data;
815 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
819 static int str_prio_cb(void *data, unsigned int *val)
821 struct thread_data *td = data;
827 static int str_exitall_cb(void)
829 exitall_on_terminate = 1;
833 static int str_cpumask_cb(void *data, unsigned int *val)
835 struct thread_data *td = data;
837 fill_cpu_mask(td->cpumask, *val);
842 * This is our [ini] type file parser.
844 int parse_jobs_ini(char *file, int stonewall_flag)
847 struct thread_data *td;
848 char *string, *name, *tmpbuf;
852 int ret = 0, stonewall;
854 f = fopen(file, "r");
856 perror("fopen job file");
860 string = malloc(4096);
862 tmpbuf = malloc(4096);
864 stonewall = stonewall_flag;
865 while ((p = fgets(string, 4096, f)) != NULL) {
868 if (is_empty_or_comment(p))
870 if (sscanf(p, "[%s]", name) != 1)
873 global = !strncmp(name, "global", 6);
875 name[strlen(name) - 1] = '\0';
877 td = get_new_job(global, &def_thread);
884 * Seperate multiple job files by a stonewall
886 if (!global && stonewall) {
887 td->stonewall = stonewall;
892 while ((p = fgets(string, 4096, f)) != NULL) {
893 if (is_empty_or_comment(p))
898 strip_blank_front(&p);
904 * Don't break here, continue parsing options so we
905 * dump all the bad ones. Makes trial/error fixups
906 * easier on the user.
908 ret = parse_option(p, options, td);
913 ret = add_job(td, name, 0);
926 static int fill_def_thread(void)
928 memset(&def_thread, 0, sizeof(def_thread));
930 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
931 perror("sched_getaffinity");
938 def_thread.ddir = DDIR_READ;
939 def_thread.iomix = 0;
940 def_thread.bs = DEF_BS;
941 def_thread.min_bs = -1;
942 def_thread.max_bs = -1;
943 def_thread.odirect = DEF_ODIRECT;
944 def_thread.ratecycle = DEF_RATE_CYCLE;
945 def_thread.sequential = DEF_SEQUENTIAL;
946 def_thread.timeout = def_timeout;
947 def_thread.overwrite = DEF_OVERWRITE;
948 def_thread.invalidate_cache = DEF_INVALIDATE;
949 def_thread.sync_io = DEF_SYNCIO;
950 def_thread.mem_type = MEM_MALLOC;
951 def_thread.bw_avg_time = DEF_BWAVGTIME;
952 def_thread.create_serialize = DEF_CREATE_SER;
953 def_thread.create_fsync = DEF_CREATE_FSYNC;
954 def_thread.loops = DEF_LOOPS;
955 def_thread.verify = DEF_VERIFY;
956 def_thread.stonewall = DEF_STONEWALL;
957 def_thread.numjobs = DEF_NUMJOBS;
958 def_thread.use_thread = DEF_USE_THREAD;
959 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
960 def_thread.rwmixread = DEF_RWMIX_READ;
961 def_thread.nice = DEF_NICE;
962 def_thread.rand_repeatable = DEF_RAND_REPEAT;
963 def_thread.nr_files = DEF_NR_FILES;
964 def_thread.unlink = DEF_UNLINK;
965 def_thread.write_bw_log = write_bw_log;
966 def_thread.write_lat_log = write_lat_log;
967 #ifdef FIO_HAVE_DISK_UTIL
968 def_thread.do_disk_util = 1;
974 static void usage(void)
976 printf("%s\n", fio_version_string);
977 printf("\t--output\tWrite output to file\n");
978 printf("\t--timeout\tRuntime in seconds\n");
979 printf("\t--latency-log\tGenerate per-job latency logs\n");
980 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
981 printf("\t--minimal\tMinimal (terse) output\n");
982 printf("\t--version\tPrint version info and exit\n");
985 static int parse_cmd_line(int argc, char *argv[])
987 struct thread_data *td = NULL;
988 int c, ini_idx = 0, lidx;
990 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
993 def_timeout = atoi(optarg);
1002 f_out = fopen(optarg, "w+");
1004 perror("fopen output");
1016 printf("%s\n", fio_version_string);
1018 case FIO_GETOPT_JOB: {
1019 const char *opt = long_options[lidx].name;
1023 td = get_new_job(0, &def_thread);
1027 if (parse_cmd_option(opt, val, options, td))
1032 printf("optarg <<%s>>\n", argv[optind]);
1038 const char *name = td->name;
1044 ret = add_job(td, name, 0);
1049 while (optind < argc) {
1051 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1052 ini_file[ini_idx - 1] = strdup(argv[optind]);
1059 static void free_shm(void)
1061 struct shmid_ds sbuf;
1064 shmdt((void *) threads);
1066 shmctl(shm_id, IPC_RMID, &sbuf);
1071 * The thread area is shared between the main process and the job
1072 * threads/processes. So setup a shared memory segment that will hold
1075 static int setup_thread_area(void)
1078 * 1024 is too much on some machines, scale max_jobs if
1079 * we get a failure that looks like too large a shm segment
1082 size_t size = max_jobs * sizeof(struct thread_data);
1084 shm_id = shmget(0, size, IPC_CREAT | 0600);
1087 if (errno != EINVAL) {
1098 threads = shmat(shm_id, NULL, 0);
1099 if (threads == (void *) -1) {
1109 * Copy the fio options into the long options map, so we mirror
1110 * job and cmd line options.
1112 static void dupe_job_options(void)
1114 struct fio_option *o;
1118 while (long_options[i].name)
1123 long_options[i].name = o->name;
1124 long_options[i].val = FIO_GETOPT_JOB;
1125 if (o->type == FIO_OPT_STR_SET)
1126 long_options[i].has_arg = no_argument;
1128 long_options[i].has_arg = required_argument;
1132 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1136 int parse_options(int argc, char *argv[])
1145 if (setup_thread_area())
1147 if (fill_def_thread())
1150 job_files = parse_cmd_line(argc, argv);
1152 for (i = 0; i < job_files; i++) {
1153 if (fill_def_thread())
1155 if (parse_jobs_ini(ini_file[i], i))
1162 if (!thread_number) {
1163 log_err("No jobs defined(s)\n");