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),
356 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
357 #define FIO_CMD_OPTS (16)
358 #define FIO_GETOPT_JOB (0x89988998)
361 * Command line options. These will contain the above, plus a few
362 * extra that only pertain to fio itself and not jobs.
364 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
367 .has_arg = required_argument,
372 .has_arg = required_argument,
376 .name = "latency-log",
377 .has_arg = required_argument,
381 .name = "bandwidth-log",
382 .has_arg = required_argument,
387 .has_arg = optional_argument,
392 .has_arg = no_argument,
400 static int def_timeout = DEF_TIMEOUT;
402 static char fio_version_string[] = "fio 1.7";
404 static char **ini_file;
405 static int max_jobs = MAX_JOBS;
407 struct thread_data def_thread;
408 struct thread_data *threads = NULL;
411 int exitall_on_terminate = 0;
412 int terse_output = 0;
413 unsigned long long mlock_size = 0;
417 static int write_lat_log = DEF_WRITE_LAT_LOG;
418 static int write_bw_log = DEF_WRITE_BW_LOG;
421 * Return a free job structure.
423 static struct thread_data *get_new_job(int global, struct thread_data *parent)
425 struct thread_data *td;
429 if (thread_number >= max_jobs)
432 td = &threads[thread_number++];
435 td->thread_number = thread_number;
439 static void put_job(struct thread_data *td)
441 if (td == &def_thread)
444 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
449 * Lazy way of fixing up options that depend on each other. We could also
450 * define option callback handlers, but this is easier.
452 static void fixup_options(struct thread_data *td)
454 if (!td->rwmixread && td->rwmixwrite)
455 td->rwmixread = 100 - td->rwmixwrite;
457 if (td->write_iolog_file && td->read_iolog_file) {
458 log_err("fio: read iolog overrides write_iolog\n");
459 free(td->write_iolog_file);
460 td->write_iolog_file = NULL;
463 if (td->io_ops->flags & FIO_SYNCIO)
467 td->iodepth = td->nr_files;
471 * only really works for sequential io for now, and with 1 file
473 if (td->zone_size && !td->sequential && td->nr_files == 1)
477 * Reads can do overwrites, we always need to pre-create the file
479 if (td_read(td) || td_rw(td))
486 if (td_read(td) && !td_rw(td))
489 if (td->norandommap && td->verify != VERIFY_NONE) {
490 log_err("fio: norandommap given, verify disabled\n");
491 td->verify = VERIFY_NONE;
496 * Adds a job to the list of things todo. Sanitizes the various options
497 * to make sure we don't have conflicts, and initializes various
500 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
502 char *ddir_str[] = { "read", "write", "randread", "randwrite",
503 "rw", NULL, "randrw" };
505 int numjobs, ddir, i;
508 #ifndef FIO_HAVE_LIBAIO
509 if (td->io_engine == FIO_LIBAIO) {
510 log_err("Linux libaio not available\n");
514 #ifndef FIO_HAVE_POSIXAIO
515 if (td->io_engine == FIO_POSIXAIO) {
516 log_err("posix aio not available\n");
522 * the def_thread is just for options, it's not a real job
524 if (td == &def_thread)
528 * Set default io engine, if none set
531 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
533 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
540 td->filetype = FIO_TYPE_FILE;
541 if (!stat(jobname, &sb)) {
542 if (S_ISBLK(sb.st_mode))
543 td->filetype = FIO_TYPE_BD;
544 else if (S_ISCHR(sb.st_mode))
545 td->filetype = FIO_TYPE_CHAR;
549 td->io_ops->flags |= FIO_RAWIO;
552 td->nr_uniq_files = 1;
554 td->nr_uniq_files = td->nr_files;
556 if (td->filetype == FIO_TYPE_FILE || td->filename) {
561 if (td->directory && td->directory[0] != '\0')
562 sprintf(tmp, "%s/", td->directory);
564 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
566 for_each_file(td, f, i) {
567 memset(f, 0, sizeof(*f));
571 sprintf(tmp + len, "%s", td->filename);
573 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
574 f->file_name = strdup(tmp);
578 td->files = malloc(sizeof(struct fio_file));
581 memset(f, 0, sizeof(*f));
583 f->file_name = strdup(jobname);
586 for_each_file(td, f, i) {
587 f->file_size = td->total_file_size / td->nr_files;
588 f->file_offset = td->start_offset;
591 fio_sem_init(&td->mutex, 0);
593 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
594 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
595 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
597 if (td->stonewall && td->thread_number > 1)
600 td->groupid = groupid;
605 if (td->write_lat_log) {
606 setup_log(&td->slat_log);
607 setup_log(&td->clat_log);
609 if (td->write_bw_log)
610 setup_log(&td->bw_log);
613 td->name = strdup(jobname);
615 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
619 if (td->io_ops->flags & FIO_CPUIO)
620 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
622 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);
623 } else if (job_add_num == 1)
624 fprintf(f_out, "...\n");
628 * recurse add identical jobs, clear numjobs and stonewall options
629 * as they don't apply to sub-jobs
631 numjobs = td->numjobs;
633 struct thread_data *td_new = get_new_job(0, td);
639 td_new->stonewall = 0;
640 job_add_num = numjobs - 1;
642 if (add_job(td_new, jobname, job_add_num))
652 * Initialize the various random states we need (random io, block size ranges,
653 * read/write mix, etc).
655 int init_random_state(struct thread_data *td)
657 unsigned long seeds[4];
658 int fd, num_maps, blocks, i;
661 if (td->io_ops->flags & FIO_CPUIO)
664 fd = open("/dev/urandom", O_RDONLY);
666 td_verror(td, errno);
670 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
678 os_random_seed(seeds[0], &td->bsrange_state);
679 os_random_seed(seeds[1], &td->verify_state);
680 os_random_seed(seeds[2], &td->rwmix_state);
685 if (td->rand_repeatable)
686 seeds[3] = DEF_RANDSEED;
688 if (!td->norandommap) {
689 for_each_file(td, f, i) {
690 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
691 num_maps = blocks / BLOCKS_PER_MAP;
692 f->file_map = malloc(num_maps * sizeof(long));
693 f->num_maps = num_maps;
694 memset(f->file_map, 0, num_maps * sizeof(long));
698 os_random_seed(seeds[3], &td->random_state);
702 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
704 #ifdef FIO_HAVE_CPU_AFFINITY
709 for (i = 0; i < sizeof(int) * 8; i++) {
711 CPU_SET(i, &cpumask);
716 static int is_empty_or_comment(char *line)
720 for (i = 0; i < strlen(line); i++) {
723 if (!isspace(line[i]) && !iscntrl(line[i]))
730 static int str_rw_cb(void *data, const char *mem)
732 struct thread_data *td = data;
734 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
735 td->ddir = DDIR_READ;
738 } else if (!strncmp(mem, "randread", 8)) {
739 td->ddir = DDIR_READ;
742 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
743 td->ddir = DDIR_WRITE;
746 } else if (!strncmp(mem, "randwrite", 9)) {
747 td->ddir = DDIR_WRITE;
750 } else if (!strncmp(mem, "rw", 2)) {
755 } else if (!strncmp(mem, "randrw", 6)) {
762 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
766 static int str_verify_cb(void *data, const char *mem)
768 struct thread_data *td = data;
770 if (!strncmp(mem, "0", 1)) {
771 td->verify = VERIFY_NONE;
773 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
774 td->verify = VERIFY_MD5;
776 } else if (!strncmp(mem, "crc32", 5)) {
777 td->verify = VERIFY_CRC32;
781 log_err("fio: verify types: md5, crc32\n");
785 static int str_mem_cb(void *data, const char *mem)
787 struct thread_data *td = data;
789 if (!strncmp(mem, "malloc", 6)) {
790 td->mem_type = MEM_MALLOC;
792 } else if (!strncmp(mem, "shm", 3)) {
793 td->mem_type = MEM_SHM;
795 } else if (!strncmp(mem, "mmap", 4)) {
796 td->mem_type = MEM_MMAP;
800 log_err("fio: mem type: malloc, shm, mmap\n");
804 static int str_ioengine_cb(void *data, const char *str)
806 struct thread_data *td = data;
808 td->io_ops = load_ioengine(td, str);
812 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
816 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
822 static int str_prioclass_cb(void *data, unsigned int *val)
824 struct thread_data *td = data;
826 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
830 static int str_prio_cb(void *data, unsigned int *val)
832 struct thread_data *td = data;
838 static int str_exitall_cb(void)
840 exitall_on_terminate = 1;
844 static int str_cpumask_cb(void *data, unsigned int *val)
846 struct thread_data *td = data;
848 fill_cpu_mask(td->cpumask, *val);
853 * This is our [ini] type file parser.
855 int parse_jobs_ini(char *file, int stonewall_flag)
858 struct thread_data *td;
863 int ret = 0, stonewall;
865 f = fopen(file, "r");
867 perror("fopen job file");
871 string = malloc(4096);
873 memset(name, 0, 256);
875 stonewall = stonewall_flag;
877 p = fgets(string, 4095, f);
880 if (is_empty_or_comment(p))
882 if (sscanf(p, "[%255s]", name) != 1)
885 global = !strncmp(name, "global", 6);
887 name[strlen(name) - 1] = '\0';
889 td = get_new_job(global, &def_thread);
896 * Seperate multiple job files by a stonewall
898 if (!global && stonewall) {
899 td->stonewall = stonewall;
904 while ((p = fgets(string, 4096, f)) != NULL) {
905 if (is_empty_or_comment(p))
908 strip_blank_front(&p);
918 * Don't break here, continue parsing options so we
919 * dump all the bad ones. Makes trial/error fixups
920 * easier on the user.
922 ret |= parse_option(p, options, td);
927 ret = add_job(td, name, 0);
937 static int fill_def_thread(void)
939 memset(&def_thread, 0, sizeof(def_thread));
941 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
942 perror("sched_getaffinity");
949 def_thread.ddir = DDIR_READ;
950 def_thread.iomix = 0;
951 def_thread.bs = DEF_BS;
952 def_thread.min_bs = 0;
953 def_thread.max_bs = 0;
954 def_thread.odirect = DEF_ODIRECT;
955 def_thread.ratecycle = DEF_RATE_CYCLE;
956 def_thread.sequential = DEF_SEQUENTIAL;
957 def_thread.timeout = def_timeout;
958 def_thread.overwrite = DEF_OVERWRITE;
959 def_thread.invalidate_cache = DEF_INVALIDATE;
960 def_thread.sync_io = DEF_SYNCIO;
961 def_thread.mem_type = MEM_MALLOC;
962 def_thread.bw_avg_time = DEF_BWAVGTIME;
963 def_thread.create_serialize = DEF_CREATE_SER;
964 def_thread.create_fsync = DEF_CREATE_FSYNC;
965 def_thread.loops = DEF_LOOPS;
966 def_thread.verify = DEF_VERIFY;
967 def_thread.stonewall = DEF_STONEWALL;
968 def_thread.numjobs = DEF_NUMJOBS;
969 def_thread.use_thread = DEF_USE_THREAD;
970 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
971 def_thread.rwmixread = DEF_RWMIX_READ;
972 def_thread.nice = DEF_NICE;
973 def_thread.rand_repeatable = DEF_RAND_REPEAT;
974 def_thread.nr_files = DEF_NR_FILES;
975 def_thread.unlink = DEF_UNLINK;
976 def_thread.write_bw_log = write_bw_log;
977 def_thread.write_lat_log = write_lat_log;
978 def_thread.norandommap = DEF_NO_RAND_MAP;
979 #ifdef FIO_HAVE_DISK_UTIL
980 def_thread.do_disk_util = 1;
986 static void usage(void)
988 printf("%s\n", fio_version_string);
989 printf("\t--output\tWrite output to file\n");
990 printf("\t--timeout\tRuntime in seconds\n");
991 printf("\t--latency-log\tGenerate per-job latency logs\n");
992 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
993 printf("\t--minimal\tMinimal (terse) output\n");
994 printf("\t--version\tPrint version info and exit\n");
997 static int parse_cmd_line(int argc, char *argv[])
999 struct thread_data *td = NULL;
1000 int c, ini_idx = 0, lidx, ret;
1002 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
1005 def_timeout = atoi(optarg);
1014 f_out = fopen(optarg, "w+");
1016 perror("fopen output");
1028 printf("%s\n", fio_version_string);
1030 case FIO_GETOPT_JOB: {
1031 const char *opt = long_options[lidx].name;
1034 if (!strncmp(opt, "name", 4) && td) {
1035 ret = add_job(td, td->name ?: "fio", 0);
1043 int global = !strncmp(val, "global", 6);
1045 td = get_new_job(global, &def_thread);
1050 parse_cmd_option(opt, val, options, td);
1054 printf("optarg <<%s>>\n", argv[optind]);
1060 ret = add_job(td, td->name ?: "fio", 0);
1065 while (optind < argc) {
1067 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1068 ini_file[ini_idx - 1] = strdup(argv[optind]);
1075 static void free_shm(void)
1077 struct shmid_ds sbuf;
1080 shmdt((void *) threads);
1082 shmctl(shm_id, IPC_RMID, &sbuf);
1087 * The thread area is shared between the main process and the job
1088 * threads/processes. So setup a shared memory segment that will hold
1091 static int setup_thread_area(void)
1094 * 1024 is too much on some machines, scale max_jobs if
1095 * we get a failure that looks like too large a shm segment
1098 size_t size = max_jobs * sizeof(struct thread_data);
1100 shm_id = shmget(0, size, IPC_CREAT | 0600);
1103 if (errno != EINVAL) {
1114 threads = shmat(shm_id, NULL, 0);
1115 if (threads == (void *) -1) {
1125 * Copy the fio options into the long options map, so we mirror
1126 * job and cmd line options.
1128 static void dupe_job_options(void)
1130 struct fio_option *o;
1134 while (long_options[i].name)
1139 long_options[i].name = o->name;
1140 long_options[i].val = FIO_GETOPT_JOB;
1141 if (o->type == FIO_OPT_STR_SET)
1142 long_options[i].has_arg = no_argument;
1144 long_options[i].has_arg = required_argument;
1148 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1152 int parse_options(int argc, char *argv[])
1161 if (setup_thread_area())
1163 if (fill_def_thread())
1166 job_files = parse_cmd_line(argc, argv);
1168 for (i = 0; i < job_files; i++) {
1169 if (fill_def_thread())
1171 if (parse_jobs_ini(ini_file[i], i))
1178 if (!thread_number) {
1179 log_err("No jobs defined(s)\n");