client/server: send back nr_jobs and error exit code
[fio.git] / fio.h
1 #ifndef FIO_H
2 #define FIO_H
3
4 #include <sched.h>
5 #include <limits.h>
6 #include <pthread.h>
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <assert.h>
16
17 struct thread_data;
18
19 #include "compiler/compiler.h"
20 #include "flist.h"
21 #include "fifo.h"
22 #include "rbtree.h"
23 #include "arch/arch.h"
24 #include "os/os.h"
25 #include "mutex.h"
26 #include "log.h"
27 #include "debug.h"
28 #include "file.h"
29 #include "io_ddir.h"
30 #include "ioengine.h"
31 #include "iolog.h"
32 #include "helpers.h"
33 #include "options.h"
34 #include "profile.h"
35 #include "time.h"
36 #include "lib/getopt.h"
37 #include "lib/rand.h"
38 #include "server.h"
39 #include "stat.h"
40
41 #ifdef FIO_HAVE_GUASI
42 #include <guasi.h>
43 #endif
44
45 #ifdef FIO_HAVE_SOLARISAIO
46 #include <sys/asynch.h>
47 #endif
48
49 /*
50  * What type of allocation to use for io buffers
51  */
52 enum fio_memtype {
53         MEM_MALLOC = 0, /* ordinary malloc */
54         MEM_SHM,        /* use shared memory segments */
55         MEM_SHMHUGE,    /* use shared memory segments with huge pages */
56         MEM_MMAP,       /* use anonynomous mmap */
57         MEM_MMAPHUGE,   /* memory mapped huge file */
58 };
59
60 /*
61  * offset generator types
62  */
63 enum {
64         RW_SEQ_SEQ      = 0,
65         RW_SEQ_IDENT,
66 };
67
68 struct bssplit {
69         unsigned int bs;
70         unsigned char perc;
71 };
72
73 struct thread_options {
74         int pad;
75         char *description;
76         char *name;
77         char *directory;
78         char *filename;
79         char *opendir;
80         char *ioengine;
81         enum td_ddir td_ddir;
82         unsigned int rw_seq;
83         unsigned int kb_base;
84         unsigned int ddir_seq_nr;
85         long ddir_seq_add;
86         unsigned int iodepth;
87         unsigned int iodepth_low;
88         unsigned int iodepth_batch;
89         unsigned int iodepth_batch_complete;
90
91         unsigned long long size;
92         unsigned int size_percent;
93         unsigned int fill_device;
94         unsigned long long file_size_low;
95         unsigned long long file_size_high;
96         unsigned long long start_offset;
97
98         unsigned int bs[2];
99         unsigned int ba[2];
100         unsigned int min_bs[2];
101         unsigned int max_bs[2];
102         struct bssplit *bssplit[2];
103         unsigned int bssplit_nr[2];
104
105         unsigned int nr_files;
106         unsigned int open_files;
107         enum file_lock_mode file_lock_mode;
108         unsigned int lockfile_batch;
109
110         unsigned int odirect;
111         unsigned int invalidate_cache;
112         unsigned int create_serialize;
113         unsigned int create_fsync;
114         unsigned int create_on_open;
115         unsigned int end_fsync;
116         unsigned int pre_read;
117         unsigned int sync_io;
118         unsigned int verify;
119         unsigned int do_verify;
120         unsigned int verifysort;
121         unsigned int verify_interval;
122         unsigned int verify_offset;
123         char verify_pattern[MAX_PATTERN_SIZE];
124         unsigned int verify_pattern_bytes;
125         unsigned int verify_fatal;
126         unsigned int verify_dump;
127         unsigned int verify_async;
128         unsigned long long verify_backlog;
129         unsigned int verify_batch;
130         unsigned int use_thread;
131         unsigned int unlink;
132         unsigned int do_disk_util;
133         unsigned int override_sync;
134         unsigned int rand_repeatable;
135         unsigned int use_os_rand;
136         unsigned int write_lat_log;
137         unsigned int write_bw_log;
138         unsigned int write_iops_log;
139         unsigned int norandommap;
140         unsigned int softrandommap;
141         unsigned int bs_unaligned;
142         unsigned int fsync_on_close;
143
144         unsigned int hugepage_size;
145         unsigned int rw_min_bs;
146         unsigned int thinktime;
147         unsigned int thinktime_spin;
148         unsigned int thinktime_blocks;
149         unsigned int fsync_blocks;
150         unsigned int fdatasync_blocks;
151         unsigned int barrier_blocks;
152         unsigned long long start_delay;
153         unsigned long long timeout;
154         unsigned long long ramp_time;
155         unsigned int overwrite;
156         unsigned int bw_avg_time;
157         unsigned int iops_avg_time;
158         unsigned int loops;
159         unsigned long long zone_size;
160         unsigned long long zone_skip;
161         enum fio_memtype mem_type;
162         unsigned int mem_align;
163
164         unsigned int stonewall;
165         unsigned int new_group;
166         unsigned int numjobs;
167         os_cpu_mask_t cpumask;
168         unsigned int cpumask_set;
169         os_cpu_mask_t verify_cpumask;
170         unsigned int verify_cpumask_set;
171         unsigned int iolog;
172         unsigned int rwmixcycle;
173         unsigned int rwmix[2];
174         unsigned int nice;
175         unsigned int file_service_type;
176         unsigned int group_reporting;
177         unsigned int fadvise_hint;
178         enum fio_fallocate_mode fallocate_mode;
179         unsigned int zero_buffers;
180         unsigned int refill_buffers;
181         unsigned int scramble_buffers;
182         unsigned int time_based;
183         unsigned int disable_lat;
184         unsigned int disable_clat;
185         unsigned int disable_slat;
186         unsigned int disable_bw;
187         unsigned int gtod_reduce;
188         unsigned int gtod_cpu;
189         unsigned int gtod_offload;
190         enum fio_cs clocksource;
191         unsigned int no_stall;
192         unsigned int trim_percentage;
193         unsigned int trim_batch;
194         unsigned int trim_zero;
195         unsigned long long trim_backlog;
196         unsigned int clat_percentiles;
197         unsigned int overwrite_plist;
198         fio_fp64_t percentile_list[FIO_IO_U_LIST_MAX_LEN];
199
200         char *read_iolog_file;
201         char *write_iolog_file;
202         char *bw_log_file;
203         char *lat_log_file;
204         char *iops_log_file;
205         char *replay_redirect;
206
207         /*
208          * Pre-run and post-run shell
209          */
210         char *exec_prerun;
211         char *exec_postrun;
212
213         unsigned int rate[2];
214         unsigned int ratemin[2];
215         unsigned int ratecycle;
216         unsigned int rate_iops[2];
217         unsigned int rate_iops_min[2];
218
219         char *ioscheduler;
220
221         /*
222          * CPU "io" cycle burner
223          */
224         unsigned int cpuload;
225         unsigned int cpucycle;
226
227         /*
228          * I/O Error handling
229          */
230         unsigned int continue_on_error;
231
232         /*
233          * Benchmark profile type
234          */
235         char *profile;
236
237         /*
238          * blkio cgroup support
239          */
240         char *cgroup;
241         unsigned int cgroup_weight;
242         unsigned int cgroup_nodelete;
243
244         unsigned int uid;
245         unsigned int gid;
246
247         unsigned int sync_file_range;
248
249         unsigned int userspace_libaio_reap;
250 };
251
252 /*
253  * This describes a single thread/process executing a fio job.
254  */
255 struct thread_data {
256         struct thread_options o;
257         char verror[FIO_VERROR_SIZE];
258         pthread_t thread;
259         int thread_number;
260         int groupid;
261         struct thread_stat ts;
262
263         struct io_log *slat_log;
264         struct io_log *clat_log;
265         struct io_log *lat_log;
266         struct io_log *bw_log;
267         struct io_log *iops_log;
268
269         uint64_t stat_io_bytes[2];
270         struct timeval bw_sample_time;
271
272         uint64_t stat_io_blocks[2];
273         struct timeval iops_sample_time;
274
275         struct rusage ru_start;
276         struct rusage ru_end;
277
278         struct fio_file **files;
279         unsigned int files_size;
280         unsigned int files_index;
281         unsigned int nr_open_files;
282         unsigned int nr_done_files;
283         unsigned int nr_normal_files;
284         union {
285                 unsigned int next_file;
286                 os_random_state_t next_file_state;
287                 struct frand_state __next_file_state;
288         };
289         int error;
290         int done;
291         pid_t pid;
292         char *orig_buffer;
293         size_t orig_buffer_size;
294         volatile int terminate;
295         volatile int runstate;
296         unsigned int ioprio;
297         unsigned int ioprio_set;
298         unsigned int last_was_sync;
299         enum fio_ddir last_ddir;
300
301         char *mmapfile;
302         int mmapfd;
303
304         void *iolog_buf;
305         FILE *iolog_f;
306
307         char *sysfs_root;
308
309         unsigned long rand_seeds[8];
310
311         union {
312                 os_random_state_t bsrange_state;
313                 struct frand_state __bsrange_state;
314         };
315         union {
316                 os_random_state_t verify_state;
317                 struct frand_state __verify_state;
318         };
319         union {
320                 os_random_state_t trim_state;
321                 struct frand_state __trim_state;
322         };
323
324         struct frand_state buf_state;
325
326         unsigned int verify_batch;
327         unsigned int trim_batch;
328
329         int shm_id;
330
331         /*
332          * IO engine hooks, contains everything needed to submit an io_u
333          * to any of the available IO engines.
334          */
335         struct ioengine_ops *io_ops;
336
337         /*
338          * Current IO depth and list of free and busy io_u's.
339          */
340         unsigned int cur_depth;
341         unsigned int io_u_queued;
342         struct flist_head io_u_freelist;
343         struct flist_head io_u_busylist;
344         struct flist_head io_u_requeues;
345         pthread_mutex_t io_u_lock;
346         pthread_cond_t free_cond;
347
348         /*
349          * async verify offload
350          */
351         struct flist_head verify_list;
352         pthread_t *verify_threads;
353         unsigned int nr_verify_threads;
354         pthread_cond_t verify_cond;
355         int verify_thread_exit;
356
357         /*
358          * Rate state
359          */
360         unsigned long rate_nsec_cycle[2];
361         long rate_pending_usleep[2];
362         unsigned long rate_bytes[2];
363         unsigned long rate_blocks[2];
364         struct timeval lastrate[2];
365
366         unsigned long long total_io_size;
367         unsigned long long fill_device_size;
368
369         unsigned long io_issues[2];
370         unsigned long long io_blocks[2];
371         unsigned long long this_io_blocks[2];
372         unsigned long long io_bytes[2];
373         unsigned long long io_skip_bytes;
374         unsigned long long this_io_bytes[2];
375         unsigned long long zone_bytes;
376         struct fio_mutex *mutex;
377
378         /*
379          * State for random io, a bitmap of blocks done vs not done
380          */
381         union {
382                 os_random_state_t random_state;
383                 struct frand_state __random_state;
384         };
385
386         struct timeval start;   /* start of this loop */
387         struct timeval epoch;   /* time job was started */
388         struct timeval last_issue;
389         struct timeval tv_cache;
390         unsigned int tv_cache_nr;
391         unsigned int tv_cache_mask;
392         unsigned int ramp_time_over;
393
394         /*
395          * read/write mixed workload state
396          */
397         union {
398                 os_random_state_t rwmix_state;
399                 struct frand_state __rwmix_state;
400         };
401         unsigned long rwmix_issues;
402         enum fio_ddir rwmix_ddir;
403         unsigned int ddir_seq_nr;
404
405         /*
406          * IO history logs for verification. We use a tree for sorting,
407          * if we are overwriting. Otherwise just use a fifo.
408          */
409         struct rb_root io_hist_tree;
410         struct flist_head io_hist_list;
411         unsigned long io_hist_len;
412
413         /*
414          * For IO replaying
415          */
416         struct flist_head io_log_list;
417
418         /*
419          * For tracking/handling discards
420          */
421         struct flist_head trim_list;
422         unsigned long trim_entries;
423
424         /*
425          * for fileservice, how often to switch to a new file
426          */
427         unsigned int file_service_nr;
428         unsigned int file_service_left;
429         struct fio_file *file_service_file;
430
431         unsigned int sync_file_range_nr;
432
433         /*
434          * For generating file sizes
435          */
436         union {
437                 os_random_state_t file_size_state;
438                 struct frand_state __file_size_state;
439         };
440
441         /*
442          * Error counts
443          */
444         unsigned int total_err_count;
445         int first_error;
446
447         /*
448          * Can be overloaded by profiles
449          */
450         struct prof_io_ops prof_io_ops;
451         void *prof_data;
452 };
453
454 /*
455  * when should interactive ETA output be generated
456  */
457 enum {
458         FIO_ETA_AUTO,
459         FIO_ETA_ALWAYS,
460         FIO_ETA_NEVER,
461 };
462
463 #define __td_verror(td, err, msg, func)                                 \
464         do {                                                            \
465                 if ((td)->error)                                        \
466                         break;                                          \
467                 int e = (err);                                          \
468                 (td)->error = e;                                        \
469                 if (!(td)->first_error)                                 \
470                         snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));               \
471         } while (0)
472
473
474 #define td_clear_error(td)              \
475         (td)->error = 0;
476 #define td_verror(td, err, func)        \
477         __td_verror((td), (err), strerror((err)), (func))
478 #define td_vmsg(td, err, msg, func)     \
479         __td_verror((td), (err), (msg), (func))
480
481 #define __fio_stringify_1(x)    #x
482 #define __fio_stringify(x)      __fio_stringify_1(x)
483
484 extern int exitall_on_terminate;
485 extern unsigned int thread_number;
486 extern unsigned int nr_process, nr_thread;
487 extern int shm_id;
488 extern int groupid;
489 extern int terse_output;
490 extern int temp_stall_ts;
491 extern unsigned long long mlock_size;
492 extern unsigned long page_mask, page_size;
493 extern int read_only;
494 extern int eta_print;
495 extern unsigned long done_secs;
496 extern char *job_section;
497 extern int fio_gtod_offload;
498 extern int fio_gtod_cpu;
499 extern enum fio_cs fio_clock_source;
500 extern int warnings_fatal;
501 extern int terse_version;
502 extern int is_backend;
503 extern int nr_clients;
504 extern int log_syslog;
505 extern const char fio_version_string[];
506 extern const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN];
507
508 extern struct thread_data *threads;
509
510 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
511 {
512         assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
513 }
514
515 #define BLOCKS_PER_MAP          (8 * sizeof(unsigned long))
516 #define TO_MAP_BLOCK(f, b)      (b)
517 #define RAND_MAP_IDX(f, b)      (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
518 #define RAND_MAP_BIT(f, b)      (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
519
520 #define REAL_MAX_JOBS           2048
521
522 #define td_non_fatal_error(e)   ((e) == EIO || (e) == EILSEQ)
523
524 static inline void update_error_count(struct thread_data *td, int err)
525 {
526         td->total_err_count++;
527         if (td->total_err_count == 1)
528                 td->first_error = err;
529 }
530
531 static inline int should_fsync(struct thread_data *td)
532 {
533         if (td->last_was_sync)
534                 return 0;
535         if (td->o.odirect)
536                 return 0;
537         if (td_write(td) || td_rw(td) || td->o.override_sync)
538                 return 1;
539
540         return 0;
541 }
542
543 /*
544  * Init/option functions
545  */
546 extern int __must_check parse_options(int, char **);
547 extern int parse_jobs_ini(char *, int, int);
548 extern int parse_cmd_line(int, char **);
549 extern int exec_run(void);
550 extern void reset_fio_state(void);
551 extern int fio_options_parse(struct thread_data *, char **, int);
552 extern void fio_keywords_init(void);
553 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
554 extern void fio_fill_default_options(struct thread_data *);
555 extern int fio_show_option_help(const char *);
556 extern void fio_options_dup_and_init(struct option *);
557 extern void options_mem_dupe(struct thread_data *);
558 extern void options_mem_free(struct thread_data *);
559 extern void td_fill_rand_seeds(struct thread_data *);
560 extern void add_job_opts(const char **);
561 extern char *num2str(unsigned long, int, int, int);
562
563 #define FIO_GETOPT_JOB          0x89988998
564 #define FIO_NR_OPTIONS          (FIO_MAX_OPTS + 128)
565
566 /*
567  * ETA/status stuff
568  */
569 extern void print_thread_status(void);
570 extern void print_status_init(int);
571
572 /*
573  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
574  * will never back again. It may cycle between running/verififying/fsyncing.
575  * Once the thread reaches TD_EXITED, it is just waiting for the core to
576  * reap it.
577  */
578 enum {
579         TD_NOT_CREATED = 0,
580         TD_CREATED,
581         TD_INITIALIZED,
582         TD_RAMP,
583         TD_RUNNING,
584         TD_PRE_READING,
585         TD_VERIFYING,
586         TD_FSYNCING,
587         TD_EXITED,
588         TD_REAPED,
589 };
590
591 extern void td_set_runstate(struct thread_data *, int);
592 #define TERMINATE_ALL           (-1)
593 extern void fio_terminate_threads(int);
594
595 /*
596  * Memory helpers
597  */
598 extern int __must_check fio_pin_memory(void);
599 extern void fio_unpin_memory(void);
600 extern int __must_check allocate_io_mem(struct thread_data *);
601 extern void free_io_mem(struct thread_data *);
602
603 /*
604  * Reset stats after ramp time completes
605  */
606 extern void reset_all_stats(struct thread_data *);
607
608 /*
609  * blktrace support
610  */
611 #ifdef FIO_HAVE_BLKTRACE
612 extern int is_blktrace(const char *);
613 extern int load_blktrace(struct thread_data *, const char *);
614 #endif
615
616 /*
617  * Mark unused variables passed to ops functions as unused, to silence gcc
618  */
619 #define fio_unused      __attribute((__unused__))
620 #define fio_init        __attribute__((constructor))
621 #define fio_exit        __attribute__((destructor))
622
623 #define for_each_td(td, i)      \
624         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
625 #define for_each_file(td, f, i) \
626         if ((td)->files_index)                                          \
627                 for ((i) = 0, (f) = (td)->files[0];                     \
628                  (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
629                  (i)++)
630
631 #define fio_assert(td, cond)    do {    \
632         if (!(cond)) {                  \
633                 int *__foo = NULL;      \
634                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
635                 td_set_runstate((td), TD_EXITED);       \
636                 (td)->error = EFAULT;           \
637                 *__foo = 0;                     \
638         }       \
639 } while (0)
640
641 static inline int fio_fill_issue_time(struct thread_data *td)
642 {
643         if (td->o.read_iolog_file ||
644             !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
645                 return 1;
646
647         return 0;
648 }
649
650 static inline int __should_check_rate(struct thread_data *td,
651                                       enum fio_ddir ddir)
652 {
653         struct thread_options *o = &td->o;
654
655         /*
656          * If some rate setting was given, we need to check it
657          */
658         if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
659             o->rate_iops_min[ddir])
660                 return 1;
661
662         return 0;
663 }
664
665 static inline int should_check_rate(struct thread_data *td,
666                                     unsigned long *bytes_done)
667 {
668         int ret = 0;
669
670         if (bytes_done[0])
671                 ret |= __should_check_rate(td, 0);
672         if (bytes_done[1])
673                 ret |= __should_check_rate(td, 1);
674
675         return ret;
676 }
677
678 static inline int is_power_of_2(unsigned int val)
679 {
680         return (val != 0 && ((val & (val - 1)) == 0));
681 }
682
683 /*
684  * We currently only need to do locking if we have verifier threads
685  * accessing our internal structures too
686  */
687 static inline void td_io_u_lock(struct thread_data *td)
688 {
689         if (td->o.verify_async)
690                 pthread_mutex_lock(&td->io_u_lock);
691 }
692
693 static inline void td_io_u_unlock(struct thread_data *td)
694 {
695         if (td->o.verify_async)
696                 pthread_mutex_unlock(&td->io_u_lock);
697 }
698
699 static inline void td_io_u_free_notify(struct thread_data *td)
700 {
701         if (td->o.verify_async)
702                 pthread_cond_signal(&td->free_cond);
703 }
704
705 extern const char *fio_get_arch_string(int);
706 extern const char *fio_get_os_string(int);
707
708 #endif