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