filesetup: assign fileno at setup time
[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 #include "compiler/compiler.h"
18 #include "thread_options.h"
19 #include "flist.h"
20 #include "fifo.h"
21 #include "arch/arch.h"
22 #include "os/os.h"
23 #include "mutex.h"
24 #include "log.h"
25 #include "debug.h"
26 #include "file.h"
27 #include "io_ddir.h"
28 #include "ioengine.h"
29 #include "iolog.h"
30 #include "helpers.h"
31 #include "options.h"
32 #include "profile.h"
33 #include "fio_time.h"
34 #include "gettime.h"
35 #include "oslib/getopt.h"
36 #include "lib/rand.h"
37 #include "lib/rbtree.h"
38 #include "client.h"
39 #include "server.h"
40 #include "stat.h"
41 #include "flow.h"
42 #include "io_u_queue.h"
43 #include "workqueue.h"
44
45 #ifdef CONFIG_SOLARISAIO
46 #include <sys/asynch.h>
47 #endif
48
49 #ifdef CONFIG_LIBNUMA
50 #include <linux/mempolicy.h>
51 #include <numa.h>
52
53 /*
54  * "local" is pseudo-policy
55  */
56 #define MPOL_LOCAL MPOL_MAX
57 #endif
58
59 /*
60  * offset generator types
61  */
62 enum {
63         RW_SEQ_SEQ      = 0,
64         RW_SEQ_IDENT,
65 };
66
67 enum {
68         TD_F_VER_BACKLOG        = 1U << 0,
69         TD_F_TRIM_BACKLOG       = 1U << 1,
70         TD_F_READ_IOLOG         = 1U << 2,
71         TD_F_REFILL_BUFFERS     = 1U << 3,
72         TD_F_SCRAMBLE_BUFFERS   = 1U << 4,
73         TD_F_VER_NONE           = 1U << 5,
74         TD_F_PROFILE_OPS        = 1U << 6,
75         TD_F_COMPRESS           = 1U << 7,
76         TD_F_NOIO               = 1U << 8,
77         TD_F_COMPRESS_LOG       = 1U << 9,
78         TD_F_VSTATE_SAVED       = 1U << 10,
79         TD_F_NEED_LOCK          = 1U << 11,
80         TD_F_CHILD              = 1U << 12,
81         TD_F_NO_PROGRESS        = 1U << 13,
82 };
83
84 enum {
85         FIO_RAND_BS_OFF         = 0,
86         FIO_RAND_VER_OFF,
87         FIO_RAND_MIX_OFF,
88         FIO_RAND_FILE_OFF,
89         FIO_RAND_BLOCK_OFF,
90         FIO_RAND_FILE_SIZE_OFF,
91         FIO_RAND_TRIM_OFF,
92         FIO_RAND_BUF_OFF,
93         FIO_RAND_SEQ_RAND_READ_OFF,
94         FIO_RAND_SEQ_RAND_WRITE_OFF,
95         FIO_RAND_SEQ_RAND_TRIM_OFF,
96         FIO_RAND_START_DELAY,
97         FIO_DEDUPE_OFF,
98         FIO_RAND_POISSON_OFF,
99         FIO_RAND_ZONE_OFF,
100         FIO_RAND_NR_OFFS,
101 };
102
103 enum {
104         IO_MODE_INLINE = 0,
105         IO_MODE_OFFLOAD = 1,
106
107         RATE_PROCESS_LINEAR = 0,
108         RATE_PROCESS_POISSON = 1,
109 };
110
111 /*
112  * Per-thread/process specific data. Only used for the network client
113  * for now.
114  */
115 struct sk_out;
116 void sk_out_assign(struct sk_out *);
117 void sk_out_drop(void);
118
119 struct zone_split_index {
120         uint8_t size_perc;
121         uint8_t size_perc_prev;
122 };
123
124 /*
125  * This describes a single thread/process executing a fio job.
126  */
127 struct thread_data {
128         struct thread_options o;
129         struct flist_head opt_list;
130         unsigned long flags;
131         void *eo;
132         char verror[FIO_VERROR_SIZE];
133         pthread_t thread;
134         unsigned int thread_number;
135         unsigned int subjob_number;
136         unsigned int groupid;
137         struct thread_stat ts;
138
139         int client_type;
140
141         struct io_log *slat_log;
142         struct io_log *clat_log;
143         struct io_log *lat_log;
144         struct io_log *bw_log;
145         struct io_log *iops_log;
146
147         struct workqueue log_compress_wq;
148
149         struct thread_data *parent;
150
151         uint64_t stat_io_bytes[DDIR_RWDIR_CNT];
152         struct timeval bw_sample_time;
153
154         uint64_t stat_io_blocks[DDIR_RWDIR_CNT];
155         struct timeval iops_sample_time;
156
157         volatile int update_rusage;
158         struct fio_mutex *rusage_sem;
159         struct rusage ru_start;
160         struct rusage ru_end;
161
162         struct fio_file **files;
163         unsigned char *file_locks;
164         unsigned int files_size;
165         unsigned int files_index;
166         unsigned int nr_open_files;
167         unsigned int nr_done_files;
168         unsigned int nr_normal_files;
169         union {
170                 unsigned int next_file;
171                 struct frand_state next_file_state;
172         };
173         int error;
174         int sig;
175         int done;
176         int stop_io;
177         pid_t pid;
178         char *orig_buffer;
179         size_t orig_buffer_size;
180         volatile int terminate;
181         volatile int runstate;
182         unsigned int last_was_sync;
183         enum fio_ddir last_ddir;
184
185         int mmapfd;
186
187         void *iolog_buf;
188         FILE *iolog_f;
189
190         char *sysfs_root;
191
192         unsigned long rand_seeds[FIO_RAND_NR_OFFS];
193
194         struct frand_state bsrange_state;
195         struct frand_state verify_state;
196         struct frand_state trim_state;
197         struct frand_state delay_state;
198
199         struct frand_state buf_state;
200         struct frand_state buf_state_prev;
201         struct frand_state dedupe_state;
202         struct frand_state zone_state;
203
204         struct zone_split_index **zone_state_index;
205
206         unsigned int verify_batch;
207         unsigned int trim_batch;
208
209         struct thread_io_list *vstate;
210
211         int shm_id;
212
213         /*
214          * IO engine hooks, contains everything needed to submit an io_u
215          * to any of the available IO engines.
216          */
217         struct ioengine_ops *io_ops;
218
219         /*
220          * Queue depth of io_u's that fio MIGHT do
221          */
222         unsigned int cur_depth;
223
224         /*
225          * io_u's about to be committed
226          */
227         unsigned int io_u_queued;
228
229         /*
230          * io_u's submitted but not completed yet
231          */
232         unsigned int io_u_in_flight;
233
234         /*
235          * List of free and busy io_u's
236          */
237         struct io_u_ring io_u_requeues;
238         struct io_u_queue io_u_freelist;
239         struct io_u_queue io_u_all;
240         pthread_mutex_t io_u_lock;
241         pthread_cond_t free_cond;
242
243         /*
244          * async verify offload
245          */
246         struct flist_head verify_list;
247         pthread_t *verify_threads;
248         unsigned int nr_verify_threads;
249         pthread_cond_t verify_cond;
250         int verify_thread_exit;
251
252         /*
253          * Rate state
254          */
255         uint64_t rate_bps[DDIR_RWDIR_CNT];
256         unsigned long rate_next_io_time[DDIR_RWDIR_CNT];
257         unsigned long rate_bytes[DDIR_RWDIR_CNT];
258         unsigned long rate_blocks[DDIR_RWDIR_CNT];
259         unsigned long rate_io_issue_bytes[DDIR_RWDIR_CNT];
260         struct timeval lastrate[DDIR_RWDIR_CNT];
261         int64_t last_usec;
262         struct frand_state poisson_state;
263
264         /*
265          * Enforced rate submission/completion workqueue
266          */
267         struct workqueue io_wq;
268
269         uint64_t total_io_size;
270         uint64_t fill_device_size;
271
272         /*
273          * Issue side
274          */
275         uint64_t io_issues[DDIR_RWDIR_CNT];
276         uint64_t io_issue_bytes[DDIR_RWDIR_CNT];
277         uint64_t loops;
278
279         /*
280          * Completions
281          */
282         uint64_t io_blocks[DDIR_RWDIR_CNT];
283         uint64_t this_io_blocks[DDIR_RWDIR_CNT];
284         uint64_t io_bytes[DDIR_RWDIR_CNT];
285         uint64_t this_io_bytes[DDIR_RWDIR_CNT];
286         uint64_t io_skip_bytes;
287         uint64_t zone_bytes;
288         struct fio_mutex *mutex;
289         uint64_t bytes_done[DDIR_RWDIR_CNT];
290
291         /*
292          * State for random io, a bitmap of blocks done vs not done
293          */
294         struct frand_state random_state;
295
296         struct timeval start;   /* start of this loop */
297         struct timeval epoch;   /* time job was started */
298         struct timeval last_issue;
299         long time_offset;
300         struct timeval tv_cache;
301         struct timeval terminate_time;
302         unsigned int tv_cache_nr;
303         unsigned int tv_cache_mask;
304         unsigned int ramp_time_over;
305
306         /*
307          * Time since last latency_window was started
308          */
309         struct timeval latency_ts;
310         unsigned int latency_qd;
311         unsigned int latency_qd_high;
312         unsigned int latency_qd_low;
313         unsigned int latency_failed;
314         uint64_t latency_ios;
315         int latency_end_run;
316
317         /*
318          * read/write mixed workload state
319          */
320         struct frand_state rwmix_state;
321         unsigned long rwmix_issues;
322         enum fio_ddir rwmix_ddir;
323         unsigned int ddir_seq_nr;
324
325         /*
326          * rand/seq mixed workload state
327          */
328         struct frand_state seq_rand_state[DDIR_RWDIR_CNT];
329
330         /*
331          * IO history logs for verification. We use a tree for sorting,
332          * if we are overwriting. Otherwise just use a fifo.
333          */
334         struct rb_root io_hist_tree;
335         struct flist_head io_hist_list;
336         unsigned long io_hist_len;
337
338         /*
339          * For IO replaying
340          */
341         struct flist_head io_log_list;
342
343         /*
344          * For tracking/handling discards
345          */
346         struct flist_head trim_list;
347         unsigned long trim_entries;
348
349         struct flist_head next_rand_list;
350
351         /*
352          * for fileservice, how often to switch to a new file
353          */
354         unsigned int file_service_nr;
355         unsigned int file_service_left;
356         struct fio_file *file_service_file;
357
358         unsigned int sync_file_range_nr;
359
360         /*
361          * For generating file sizes
362          */
363         struct frand_state file_size_state;
364
365         /*
366          * Error counts
367          */
368         unsigned int total_err_count;
369         int first_error;
370
371         struct fio_flow *flow;
372
373         /*
374          * Can be overloaded by profiles
375          */
376         struct prof_io_ops prof_io_ops;
377         void *prof_data;
378
379         void *pinned_mem;
380 };
381
382 /*
383  * when should interactive ETA output be generated
384  */
385 enum {
386         FIO_ETA_AUTO,
387         FIO_ETA_ALWAYS,
388         FIO_ETA_NEVER,
389 };
390
391 #define __td_verror(td, err, msg, func)                                 \
392         do {                                                            \
393                 unsigned int ____e = (err);                             \
394                 if ((td)->error)                                        \
395                         break;                                          \
396                 (td)->error = ____e;                                    \
397                 if (!(td)->first_error)                                 \
398                         snprintf(td->verror, sizeof(td->verror), "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));           \
399         } while (0)
400
401
402 #define td_clear_error(td)              do {            \
403         (td)->error = 0;                                \
404         if ((td)->parent)                               \
405                 (td)->parent->error = 0;                \
406 } while (0)
407
408 #define td_verror(td, err, func)        do {                    \
409         __td_verror((td), (err), strerror((err)), (func));      \
410         if ((td)->parent)                                       \
411                 __td_verror((td)->parent, (err), strerror((err)), (func)); \
412 } while (0)
413
414 #define td_vmsg(td, err, msg, func)     do {                    \
415         __td_verror((td), (err), (msg), (func));                \
416         if ((td)->parent)                                       \
417                 __td_verror((td)->parent, (err), (msg), (func));        \
418 } while (0)
419
420 #define __fio_stringify_1(x)    #x
421 #define __fio_stringify(x)      __fio_stringify_1(x)
422
423 extern int exitall_on_terminate;
424 extern unsigned int thread_number;
425 extern unsigned int stat_number;
426 extern int shm_id;
427 extern int groupid;
428 extern int output_format;
429 extern int append_terse_output;
430 extern int temp_stall_ts;
431 extern uintptr_t page_mask, page_size;
432 extern int read_only;
433 extern int eta_print;
434 extern int eta_new_line;
435 extern unsigned long done_secs;
436 extern char *job_section;
437 extern int fio_gtod_offload;
438 extern int fio_gtod_cpu;
439 extern enum fio_cs fio_clock_source;
440 extern int fio_clock_source_set;
441 extern int warnings_fatal;
442 extern int terse_version;
443 extern int is_backend;
444 extern int nr_clients;
445 extern int log_syslog;
446 extern int status_interval;
447 extern const char fio_version_string[];
448 extern int helper_do_stat;
449 extern pthread_cond_t helper_cond;
450 extern char *trigger_file;
451 extern char *trigger_cmd;
452 extern char *trigger_remote_cmd;
453 extern long long trigger_timeout;
454 extern char *aux_path;
455
456 extern struct thread_data *threads;
457
458 static inline void fio_ro_check(const struct thread_data *td, struct io_u *io_u)
459 {
460         assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
461 }
462
463 #define REAL_MAX_JOBS           2048
464
465 static inline int should_fsync(struct thread_data *td)
466 {
467         if (td->last_was_sync)
468                 return 0;
469         if (td_write(td) || td_rw(td) || td->o.override_sync)
470                 return 1;
471
472         return 0;
473 }
474
475 /*
476  * Init/option functions
477  */
478 extern int __must_check fio_init_options(void);
479 extern int __must_check parse_options(int, char **);
480 extern int parse_jobs_ini(char *, int, int, int);
481 extern int parse_cmd_line(int, char **, int);
482 extern int fio_backend(struct sk_out *);
483 extern void reset_fio_state(void);
484 extern void clear_io_state(struct thread_data *, int);
485 extern int fio_options_parse(struct thread_data *, char **, int);
486 extern void fio_keywords_init(void);
487 extern void fio_keywords_exit(void);
488 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
489 extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
490 extern void fio_fill_default_options(struct thread_data *);
491 extern int fio_show_option_help(const char *);
492 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
493 extern void fio_options_dup_and_init(struct option *);
494 extern void fio_options_mem_dupe(struct thread_data *);
495 extern void options_mem_dupe(void *data, struct fio_option *options);
496 extern void td_fill_rand_seeds(struct thread_data *);
497 extern void add_job_opts(const char **, int);
498 extern char *num2str(uint64_t, int, int, int, int);
499 extern int ioengine_load(struct thread_data *);
500 extern int parse_dryrun(void);
501 extern int fio_running_or_pending_io_threads(void);
502 extern int fio_set_fd_nonblocking(int, const char *);
503 extern void sig_show_status(int sig);
504 extern struct thread_data *get_global_options(void);
505
506 extern uintptr_t page_mask;
507 extern uintptr_t page_size;
508 extern int initialize_fio(char *envp[]);
509 extern void deinitialize_fio(void);
510
511 #define FIO_GETOPT_JOB          0x89000000
512 #define FIO_GETOPT_IOENGINE     0x98000000
513 #define FIO_NR_OPTIONS          (FIO_MAX_OPTS + 128)
514
515 /*
516  * ETA/status stuff
517  */
518 extern void print_thread_status(void);
519 extern void print_status_init(int);
520 extern char *fio_uint_to_kmg(unsigned int val);
521
522 /*
523  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
524  * will never back again. It may cycle between running/verififying/fsyncing.
525  * Once the thread reaches TD_EXITED, it is just waiting for the core to
526  * reap it.
527  */
528 enum {
529         TD_NOT_CREATED = 0,
530         TD_CREATED,
531         TD_INITIALIZED,
532         TD_RAMP,
533         TD_SETTING_UP,
534         TD_RUNNING,
535         TD_PRE_READING,
536         TD_VERIFYING,
537         TD_FSYNCING,
538         TD_FINISHING,
539         TD_EXITED,
540         TD_REAPED,
541         TD_LAST,
542 };
543
544 extern void td_set_runstate(struct thread_data *, int);
545 extern int td_bump_runstate(struct thread_data *, int);
546 extern void td_restore_runstate(struct thread_data *, int);
547 extern const char *runstate_to_name(int runstate);
548
549 /*
550  * Allow 60 seconds for a job to quit on its own, otherwise reap with
551  * a vengeance.
552  */
553 #define FIO_REAP_TIMEOUT        60
554
555 #define TERMINATE_ALL           (-1U)
556 extern void fio_terminate_threads(unsigned int);
557 extern void fio_mark_td_terminate(struct thread_data *);
558
559 /*
560  * Memory helpers
561  */
562 extern int __must_check fio_pin_memory(struct thread_data *);
563 extern void fio_unpin_memory(struct thread_data *);
564 extern int __must_check allocate_io_mem(struct thread_data *);
565 extern void free_io_mem(struct thread_data *);
566 extern void free_threads_shm(void);
567
568 /*
569  * Reset stats after ramp time completes
570  */
571 extern void reset_all_stats(struct thread_data *);
572
573 /*
574  * blktrace support
575  */
576 #ifdef FIO_HAVE_BLKTRACE
577 extern int is_blktrace(const char *, int *);
578 extern int load_blktrace(struct thread_data *, const char *, int);
579 #endif
580
581 extern int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret,
582                    enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify,
583                    struct timeval *comp_time);
584
585 /*
586  * Latency target helpers
587  */
588 extern void lat_target_check(struct thread_data *);
589 extern void lat_target_init(struct thread_data *);
590 extern void lat_target_reset(struct thread_data *);
591
592 #define for_each_td(td, i)      \
593         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
594 #define for_each_file(td, f, i) \
595         if ((td)->files_index)                                          \
596                 for ((i) = 0, (f) = (td)->files[0];                     \
597                  (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
598                  (i)++)
599
600 #define fio_assert(td, cond)    do {    \
601         if (!(cond)) {                  \
602                 int *__foo = NULL;      \
603                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
604                 td_set_runstate((td), TD_EXITED);       \
605                 (td)->error = EFAULT;           \
606                 *__foo = 0;                     \
607         }       \
608 } while (0)
609
610 static inline int fio_fill_issue_time(struct thread_data *td)
611 {
612         if (td->o.read_iolog_file ||
613             !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
614                 return 1;
615
616         return 0;
617 }
618
619 static inline int __should_check_rate(struct thread_data *td,
620                                       enum fio_ddir ddir)
621 {
622         struct thread_options *o = &td->o;
623
624         /*
625          * If some rate setting was given, we need to check it
626          */
627         if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
628             o->rate_iops_min[ddir])
629                 return 1;
630
631         return 0;
632 }
633
634 static inline int should_check_rate(struct thread_data *td)
635 {
636         int ret = 0;
637
638         if (td->bytes_done[DDIR_READ])
639                 ret |= __should_check_rate(td, DDIR_READ);
640         if (td->bytes_done[DDIR_WRITE])
641                 ret |= __should_check_rate(td, DDIR_WRITE);
642         if (td->bytes_done[DDIR_TRIM])
643                 ret |= __should_check_rate(td, DDIR_TRIM);
644
645         return ret;
646 }
647
648 static inline unsigned int td_max_bs(struct thread_data *td)
649 {
650         unsigned int max_bs;
651
652         max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
653         return max(td->o.max_bs[DDIR_TRIM], max_bs);
654 }
655
656 static inline unsigned int td_min_bs(struct thread_data *td)
657 {
658         unsigned int min_bs;
659
660         min_bs = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
661         return min(td->o.min_bs[DDIR_TRIM], min_bs);
662 }
663
664 static inline int td_async_processing(struct thread_data *td)
665 {
666         return (td->flags & TD_F_NEED_LOCK) != 0;
667 }
668
669 /*
670  * We currently only need to do locking if we have verifier threads
671  * accessing our internal structures too
672  */
673 static inline void td_io_u_lock(struct thread_data *td)
674 {
675         if (td_async_processing(td))
676                 pthread_mutex_lock(&td->io_u_lock);
677 }
678
679 static inline void td_io_u_unlock(struct thread_data *td)
680 {
681         if (td_async_processing(td))
682                 pthread_mutex_unlock(&td->io_u_lock);
683 }
684
685 static inline void td_io_u_free_notify(struct thread_data *td)
686 {
687         if (td_async_processing(td))
688                 pthread_cond_signal(&td->free_cond);
689 }
690
691 extern const char *fio_get_arch_string(int);
692 extern const char *fio_get_os_string(int);
693
694 #ifdef FIO_INTERNAL
695 #define ARRAY_SIZE(x)    (sizeof((x)) / (sizeof((x)[0])))
696 #define FIELD_SIZE(s, f) (sizeof(((typeof(s))0)->f))
697 #endif
698
699 enum {
700         __FIO_OUTPUT_TERSE      = 0,
701         __FIO_OUTPUT_JSON       = 1,
702         __FIO_OUTPUT_NORMAL     = 2,
703         __FIO_OUTPUT_JSON_PLUS  = 3,
704         FIO_OUTPUT_NR           = 4,
705
706         FIO_OUTPUT_TERSE        = 1U << __FIO_OUTPUT_TERSE,
707         FIO_OUTPUT_JSON         = 1U << __FIO_OUTPUT_JSON,
708         FIO_OUTPUT_NORMAL       = 1U << __FIO_OUTPUT_NORMAL,
709         FIO_OUTPUT_JSON_PLUS    = 1U << __FIO_OUTPUT_JSON_PLUS,
710 };
711
712 enum {
713         FIO_RAND_DIST_RANDOM    = 0,
714         FIO_RAND_DIST_ZIPF,
715         FIO_RAND_DIST_PARETO,
716         FIO_RAND_DIST_GAUSS,
717         FIO_RAND_DIST_ZONED,
718 };
719
720 #define FIO_DEF_ZIPF            1.1
721 #define FIO_DEF_PARETO          0.2
722
723 enum {
724         FIO_RAND_GEN_TAUSWORTHE = 0,
725         FIO_RAND_GEN_LFSR,
726         FIO_RAND_GEN_TAUSWORTHE64,
727 };
728
729 enum {
730         FIO_CPUS_SHARED         = 0,
731         FIO_CPUS_SPLIT,
732 };
733
734 extern void exec_trigger(const char *);
735 extern void check_trigger_file(void);
736
737 #endif