Clean up file flags
[fio.git] / fio.h
CommitLineData
ebac4655
JA
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>
3c39a379
JA
9#include <errno.h>
10#include <stdlib.h>
11#include <stdio.h>
6d6f031f 12#include <unistd.h>
34cfcdaf 13#include <string.h>
214e1eca 14#include <getopt.h>
cd14cc10 15#include <inttypes.h>
7101d9c2 16#include <assert.h>
ebac4655 17
317b95d0 18#include "compiler/compiler.h"
01743ee1 19#include "flist.h"
e2887563 20#include "fifo.h"
4b87898e 21#include "rbtree.h"
317b95d0
JA
22#include "arch/arch.h"
23#include "os/os.h"
07739b57 24#include "mutex.h"
a3d741fa
JA
25#include "log.h"
26#include "debug.h"
d6aed795
JA
27#include "file.h"
28#include "io_ddir.h"
ebac4655 29
609342ff
DL
30#ifdef FIO_HAVE_GUASI
31#include <guasi.h>
32#endif
33
417f0068
JA
34#ifdef FIO_HAVE_SOLARISAIO
35#include <sys/asynch.h>
36#endif
37
1c9e06ee
JA
38/*
39 * Use for maintaining statistics
40 */
ebac4655 41struct io_stat {
ebac4655
JA
42 unsigned long max_val;
43 unsigned long min_val;
44 unsigned long samples;
68704084
JA
45
46 double mean;
47 double S;
ebac4655
JA
48};
49
1c9e06ee
JA
50/*
51 * A single data sample
52 */
ebac4655
JA
53struct io_sample {
54 unsigned long time;
55 unsigned long val;
1e97cce9 56 enum fio_ddir ddir;
306ddc97 57 unsigned int bs;
ebac4655
JA
58};
59
1c9e06ee
JA
60/*
61 * Dynamically growing data sample log
62 */
ebac4655
JA
63struct io_log {
64 unsigned long nr_samples;
65 unsigned long max_samples;
66 struct io_sample *log;
67};
68
1c9e06ee
JA
69/*
70 * When logging io actions, this matches a single sent io_u
71 */
ebac4655 72struct io_piece {
4b87898e
JA
73 union {
74 struct rb_node rb_node;
01743ee1 75 struct flist_head list;
4b87898e 76 };
f29b25a3
JA
77 union {
78 int fileno;
79 struct fio_file *file;
80 };
ebac4655 81 unsigned long long offset;
a4f4fdd7 82 unsigned long len;
1e97cce9 83 enum fio_ddir ddir;
f29b25a3
JA
84 union {
85 unsigned long delay;
86 unsigned int file_action;
87 };
ebac4655
JA
88};
89
0c6e7517
JA
90enum {
91 IO_U_F_FREE = 1 << 0,
92 IO_U_F_FLIGHT = 1 << 1,
93};
94
36690c9b
JA
95struct thread_data;
96
ebac4655
JA
97/*
98 * The io unit
99 */
100struct io_u {
101 union {
102#ifdef FIO_HAVE_LIBAIO
103 struct iocb iocb;
104#endif
105#ifdef FIO_HAVE_POSIXAIO
106 struct aiocb aiocb;
107#endif
108#ifdef FIO_HAVE_SGIO
109 struct sg_io_hdr hdr;
a4f4fdd7 110#endif
609342ff
DL
111#ifdef FIO_HAVE_GUASI
112 guasi_req_t greq;
417f0068
JA
113#endif
114#ifdef FIO_HAVE_SOLARISAIO
115 aio_result_t resultp;
ebac4655 116#endif
ac893112 117 void *mmap_data;
ebac4655
JA
118 };
119 struct timeval start_time;
120 struct timeval issue_time;
121
1c9e06ee
JA
122 /*
123 * Allocated/set buffer and length
124 */
6b9cea23 125 void *buf;
a4f4fdd7 126 unsigned long buflen;
ebac4655
JA
127 unsigned long long offset;
128
1c9e06ee
JA
129 /*
130 * IO engine state, may be different from above when we get
131 * partial transfers / residual data counts
132 */
cec6b55d 133 void *xfer_buf;
a4f4fdd7 134 unsigned long xfer_buflen;
cec6b55d 135
ebac4655
JA
136 unsigned int resid;
137 unsigned int error;
138
1e97cce9 139 enum fio_ddir ddir;
ebac4655 140
dfd7bc2c
JA
141 /*
142 * io engine private data
143 */
144 union {
145 unsigned int index;
146 unsigned int seen;
556e831d 147 void *engine_data;
dfd7bc2c
JA
148 };
149
0c6e7517
JA
150 unsigned int flags;
151
53cdc686
JA
152 struct fio_file *file;
153
01743ee1 154 struct flist_head list;
d7762cf8
JA
155
156 /*
157 * Callback for io completion
158 */
36690c9b 159 int (*end_io)(struct thread_data *, struct io_u *);
ebac4655
JA
160};
161
36167d82
JA
162/*
163 * io_ops->queue() return values
164 */
165enum {
166 FIO_Q_COMPLETED = 0, /* completed sync */
167 FIO_Q_QUEUED = 1, /* queued, will complete async */
755200a3 168 FIO_Q_BUSY = 2, /* no more room, call ->commit() */
36167d82
JA
169};
170
ebac4655
JA
171#define FIO_HDR_MAGIC 0xf00baaef
172
173enum {
1c9e06ee
JA
174 VERIFY_NONE = 0, /* no verification */
175 VERIFY_MD5, /* md5 sum data blocks */
d77a7af3 176 VERIFY_CRC64, /* crc64 sum data blocks */
1c9e06ee 177 VERIFY_CRC32, /* crc32 sum data blocks */
bac39e0e 178 VERIFY_CRC32C, /* crc32c sum data blocks */
3845591f 179 VERIFY_CRC32C_INTEL, /* crc32c sum data blocks with hw */
969f7ed3 180 VERIFY_CRC16, /* crc16 sum data blocks */
1e154bdb 181 VERIFY_CRC7, /* crc7 sum data blocks */
cd14cc10
JA
182 VERIFY_SHA256, /* sha256 sum data blocks */
183 VERIFY_SHA512, /* sha512 sum data blocks */
7437ee87 184 VERIFY_META, /* block_num, timestamp etc. */
36690c9b 185 VERIFY_NULL, /* pretend to verify */
ebac4655
JA
186};
187
1c9e06ee 188/*
546dfd9f
JA
189 * A header structure associated with each checksummed data block. It is
190 * followed by a checksum specific header that contains the verification
191 * data.
1c9e06ee 192 */
ebac4655
JA
193struct verify_header {
194 unsigned int fio_magic;
195 unsigned int len;
196 unsigned int verify_type;
546dfd9f
JA
197};
198
199struct vhdr_md5 {
200 uint32_t md5_digest[16];
201};
202struct vhdr_sha512 {
203 uint8_t sha512[128];
204};
205struct vhdr_sha256 {
206 uint8_t sha256[128];
207};
208struct vhdr_crc64 {
209 uint64_t crc64;
210};
211struct vhdr_crc32 {
212 uint32_t crc32;
213};
214struct vhdr_crc16 {
215 uint16_t crc16;
216};
217struct vhdr_crc7 {
218 uint8_t crc7;
ebac4655 219};
7437ee87
SL
220struct vhdr_meta {
221 uint64_t offset;
222 unsigned char thread;
223 unsigned short numberio;
224 unsigned long time_sec;
225 unsigned long time_usec;
226};
ebac4655
JA
227
228struct group_run_stats {
9104f874
JA
229 unsigned long long max_run[2], min_run[2];
230 unsigned long long max_bw[2], min_bw[2];
e9b2a3fa 231 unsigned long long io_kb[2];
9104f874 232 unsigned long long agg[2];
ebac4655
JA
233};
234
e9c047a0
JA
235/*
236 * What type of allocation to use for io buffers
237 */
238enum fio_memtype {
239 MEM_MALLOC = 0, /* ordinary malloc */
240 MEM_SHM, /* use shared memory segments */
74b025b0 241 MEM_SHMHUGE, /* use shared memory segments with huge pages */
e9c047a0 242 MEM_MMAP, /* use anonynomous mmap */
d0bdaf49 243 MEM_MMAPHUGE, /* memory mapped huge file */
e9c047a0
JA
244};
245
2866c82d 246enum fio_ioengine_flags {
1c9e06ee 247 FIO_SYNCIO = 1 << 0, /* io engine has synchronous ->queue */
ba0fbe10
JA
248 FIO_RAWIO = 1 << 1, /* some sort of direct/raw io */
249 FIO_DISKLESSIO = 1 << 2, /* no disk involved */
0263882a 250 FIO_NOEXTEND = 1 << 3, /* engine can't extend file */
62d984e2 251 FIO_NODISKUTIL = 1 << 4, /* diskutil can't handle filename */
b67740d3 252 FIO_UNIDIR = 1 << 5, /* engine is uni-directional */
1f809d15 253 FIO_NOIO = 1 << 6, /* thread does only pseudo IO */
ad830ed7 254 FIO_SIGQUIT = 1 << 7, /* needs SIGQUIT to exit */
e9c047a0
JA
255};
256
b2560f3c
JA
257/*
258 * How many depth levels to log
259 */
260#define FIO_IO_U_MAP_NR 8
a9a18a3f
JA
261#define FIO_IO_U_LAT_U_NR 10
262#define FIO_IO_U_LAT_M_NR 12
b2560f3c 263
079ad09b 264struct thread_stat {
756867bd
JA
265 char *name;
266 char *verror;
267 int error;
268 int groupid;
269 pid_t pid;
270 char *description;
6586ee89 271 int members;
756867bd 272
079ad09b
JA
273 struct io_log *slat_log;
274 struct io_log *clat_log;
275 struct io_log *bw_log;
276
277 /*
278 * bandwidth and latency stats
279 */
280 struct io_stat clat_stat[2]; /* completion latency */
281 struct io_stat slat_stat[2]; /* submission latency */
282 struct io_stat bw_stat[2]; /* bandwidth stats */
283
284 unsigned long long stat_io_bytes[2];
285 struct timeval stat_sample_time[2];
286
287 /*
288 * fio system usage accounting
289 */
290 struct rusage ru_start;
291 struct rusage ru_end;
292 unsigned long usr_time;
293 unsigned long sys_time;
294 unsigned long ctx;
81887d5d 295 unsigned long minf, majf;
079ad09b 296
b2560f3c
JA
297 /*
298 * IO depth and latency stats
299 */
300 unsigned int io_u_map[FIO_IO_U_MAP_NR];
838bc709
JA
301 unsigned int io_u_submit[FIO_IO_U_MAP_NR];
302 unsigned int io_u_complete[FIO_IO_U_MAP_NR];
04a0feae
JA
303 unsigned int io_u_lat_u[FIO_IO_U_LAT_U_NR];
304 unsigned int io_u_lat_m[FIO_IO_U_LAT_M_NR];
b3605062 305 unsigned long total_io_u[2];
30061b97 306 unsigned long short_io_u[2];
838bc709
JA
307 unsigned long total_submit;
308 unsigned long total_complete;
756867bd
JA
309
310 unsigned long long io_bytes[2];
311 unsigned long runtime[2];
312 unsigned long total_run_time;
b2560f3c 313};
71619dc2 314
564ca972
JA
315struct bssplit {
316 unsigned int bs;
317 unsigned char perc;
318};
319
2dc1bbeb 320struct thread_options {
b1ec1da6 321 int pad;
61697c37 322 char *description;
b4692828 323 char *name;
ef899b63 324 char *directory;
13f8e2d2 325 char *filename;
2dc1bbeb 326 char *opendir;
09629a90 327 char *ioengine;
413dd459 328 enum td_ddir td_ddir;
211097b2 329 unsigned int ddir_nr;
2dc1bbeb
JA
330 unsigned int iodepth;
331 unsigned int iodepth_low;
332 unsigned int iodepth_batch;
4950421a 333 unsigned int iodepth_batch_complete;
2dc1bbeb
JA
334
335 unsigned long long size;
aa31f1f1 336 unsigned int fill_device;
2dc1bbeb
JA
337 unsigned long long file_size_low;
338 unsigned long long file_size_high;
339 unsigned long long start_offset;
340
341 unsigned int bs[2];
2b7a01d0 342 unsigned int ba[2];
2dc1bbeb
JA
343 unsigned int min_bs[2];
344 unsigned int max_bs[2];
720e84ad
JA
345 struct bssplit *bssplit[2];
346 unsigned int bssplit_nr[2];
2dc1bbeb
JA
347
348 unsigned int nr_files;
349 unsigned int open_files;
4d4e80f2 350 enum file_lock_mode file_lock_mode;
29c1349f 351 unsigned int lockfile_batch;
e9c047a0 352
9158d2f7
JA
353 unsigned int odirect;
354 unsigned int invalidate_cache;
355 unsigned int create_serialize;
356 unsigned int create_fsync;
814452bd 357 unsigned int create_on_open;
9158d2f7 358 unsigned int end_fsync;
afad68f7 359 unsigned int pre_read;
9158d2f7
JA
360 unsigned int sync_io;
361 unsigned int verify;
e84c73a8 362 unsigned int do_verify;
160b966d 363 unsigned int verifysort;
a59e170d
JA
364 unsigned int verify_interval;
365 unsigned int verify_offset;
90059d65
JA
366 unsigned int verify_pattern;
367 unsigned int verify_pattern_bytes;
a12a3b4d 368 unsigned int verify_fatal;
9158d2f7
JA
369 unsigned int use_thread;
370 unsigned int unlink;
371 unsigned int do_disk_util;
372 unsigned int override_sync;
373 unsigned int rand_repeatable;
374 unsigned int write_lat_log;
375 unsigned int write_bw_log;
bb8895e0 376 unsigned int norandommap;
2b386d25 377 unsigned int softrandommap;
690adba3 378 unsigned int bs_unaligned;
ebb1415f 379 unsigned int fsync_on_close;
e9c047a0 380
56bb17f2 381 unsigned int hugepage_size;
a00735e6 382 unsigned int rw_min_bs;
ebac4655 383 unsigned int thinktime;
48097d5c 384 unsigned int thinktime_spin;
9c1f7434 385 unsigned int thinktime_blocks;
ebac4655
JA
386 unsigned int fsync_blocks;
387 unsigned int start_delay;
0db26797 388 unsigned long long timeout;
721938ae 389 unsigned long long ramp_time;
ebac4655 390 unsigned int overwrite;
ebac4655 391 unsigned int bw_avg_time;
ebac4655 392 unsigned int loops;
20dc95c4
JA
393 unsigned long long zone_size;
394 unsigned long long zone_skip;
e9c047a0 395 enum fio_memtype mem_type;
2dc1bbeb 396
ebac4655 397 unsigned int stonewall;
b3d62a75 398 unsigned int new_group;
ebac4655 399 unsigned int numjobs;
ebac4655 400 os_cpu_mask_t cpumask;
375b2695 401 unsigned int cpumask_set;
aea47d44 402 unsigned int iolog;
a6ccc7be 403 unsigned int rwmixcycle;
e47f799f 404 unsigned int rwmix[2];
b6f4d880 405 unsigned int nice;
0aabe160 406 unsigned int file_service_type;
b2560f3c 407 unsigned int group_reporting;
d2f3ac35 408 unsigned int fadvise_hint;
e9459e5a 409 unsigned int zero_buffers;
5973cafb 410 unsigned int refill_buffers;
cf4464ca 411 unsigned int time_based;
9520ebb9
JA
412 unsigned int disable_clat;
413 unsigned int disable_slat;
414 unsigned int disable_bw;
993bf48b 415 unsigned int gtod_reduce;
be4ecfdf
JA
416 unsigned int gtod_cpu;
417 unsigned int gtod_offload;
aea47d44 418
076efc7c
JA
419 char *read_iolog_file;
420 char *write_iolog_file;
e3cedca7
JA
421 char *bw_log_file;
422 char *lat_log_file;
2dc1bbeb
JA
423
424 /*
425 * Pre-run and post-run shell
426 */
427 char *exec_prerun;
428 char *exec_postrun;
429
430 unsigned int rate;
431 unsigned int ratemin;
432 unsigned int ratecycle;
433 unsigned int rate_iops;
434 unsigned int rate_iops_min;
435
436 char *ioscheduler;
437
438 /*
439 * CPU "io" cycle burner
440 */
441 unsigned int cpuload;
442 unsigned int cpucycle;
443};
444
e4e33258
JA
445#define FIO_VERROR_SIZE 128
446
2dc1bbeb
JA
447/*
448 * This describes a single thread/process executing a fio job.
449 */
450struct thread_data {
451 struct thread_options o;
e4e33258 452 char verror[FIO_VERROR_SIZE];
2dc1bbeb
JA
453 pthread_t thread;
454 int thread_number;
455 int groupid;
456 struct thread_stat ts;
126d65c6 457 struct fio_file **files;
dd87b2c9 458 unsigned int files_size;
2dc1bbeb
JA
459 unsigned int files_index;
460 unsigned int nr_open_files;
1020a139 461 unsigned int nr_done_files;
2dc1bbeb
JA
462 unsigned int nr_normal_files;
463 union {
464 unsigned int next_file;
465 os_random_state_t next_file_state;
466 };
467 int error;
20e354ef 468 int done;
2dc1bbeb
JA
469 pid_t pid;
470 char *orig_buffer;
471 size_t orig_buffer_size;
472 volatile int terminate;
473 volatile int runstate;
474 unsigned int ioprio;
ac684785 475 unsigned int ioprio_set;
2dc1bbeb
JA
476 unsigned int last_was_sync;
477
478 char *mmapfile;
479 int mmapfd;
480
843a7413
JA
481 void *iolog_buf;
482 FILE *iolog_f;
ebac4655 483
da86774e 484 char *sysfs_root;
da86774e 485
5bfc35d7
JA
486 unsigned long rand_seeds[6];
487
6dfd46b9
JA
488 os_random_state_t bsrange_state;
489 os_random_state_t verify_state;
ebac4655
JA
490
491 int shm_id;
492
e9c047a0
JA
493 /*
494 * IO engine hooks, contains everything needed to submit an io_u
495 * to any of the available IO engines.
496 */
2866c82d 497 struct ioengine_ops *io_ops;
ebac4655 498
e9c047a0
JA
499 /*
500 * Current IO depth and list of free and busy io_u's.
501 */
ebac4655 502 unsigned int cur_depth;
d8005759 503 unsigned int io_u_queued;
01743ee1
JA
504 struct flist_head io_u_freelist;
505 struct flist_head io_u_busylist;
506 struct flist_head io_u_requeues;
ebac4655 507
e9c047a0
JA
508 /*
509 * Rate state
510 */
ebac4655
JA
511 unsigned long rate_usec_cycle;
512 long rate_pending_usleep;
513 unsigned long rate_bytes;
4e991c23 514 unsigned long rate_blocks;
ebac4655
JA
515 struct timeval lastrate;
516
ebac4655
JA
517 unsigned long long total_io_size;
518
755200a3 519 unsigned long io_issues[2];
9104f874
JA
520 unsigned long long io_blocks[2];
521 unsigned long long io_bytes[2];
48f5abd3 522 unsigned long long io_skip_bytes;
9104f874 523 unsigned long long this_io_bytes[2];
079ad09b 524 unsigned long long zone_bytes;
cdd18ad8 525 struct fio_mutex *mutex;
ebac4655 526
e9c047a0
JA
527 /*
528 * State for random io, a bitmap of blocks done vs not done
529 */
6dfd46b9 530 os_random_state_t random_state;
ebac4655 531
ebac4655
JA
532 struct timeval start; /* start of this loop */
533 struct timeval epoch; /* time job was started */
38d77cae 534 struct timeval rw_end[2];
a61eddec 535 struct timeval last_issue;
993bf48b
JA
536 struct timeval tv_cache;
537 unsigned int tv_cache_nr;
538 unsigned int tv_cache_mask;
38d77cae 539 unsigned int rw_end_set[2];
721938ae 540 unsigned int ramp_time_over;
ebac4655 541
e9c047a0
JA
542 /*
543 * read/write mixed workload state
544 */
6dfd46b9 545 os_random_state_t rwmix_state;
e4928662 546 unsigned long rwmix_issues;
e9c047a0 547 enum fio_ddir rwmix_ddir;
211097b2 548 unsigned int ddir_nr;
a6ccc7be 549
e9c047a0 550 /*
8de8f047
JA
551 * IO history logs for verification. We use a tree for sorting,
552 * if we are overwriting. Otherwise just use a fifo.
e9c047a0 553 */
4b87898e 554 struct rb_root io_hist_tree;
01743ee1 555 struct flist_head io_hist_list;
8de8f047
JA
556
557 /*
558 * For IO replaying
559 */
01743ee1 560 struct flist_head io_log_list;
433afcb4 561
1907dbc6
JA
562 /*
563 * for fileservice, how often to switch to a new file
564 */
565 unsigned int file_service_nr;
566 unsigned int file_service_left;
567 struct fio_file *file_service_file;
9c60ce64
JA
568
569 /*
570 * For generating file sizes
571 */
572 os_random_state_t file_size_state;
ebac4655
JA
573};
574
0aabe160 575/*
a086c257
JA
576 * roundrobin available files, or choose one at random, or do each one
577 * serially.
0aabe160
JA
578 */
579enum {
580 FIO_FSERVICE_RANDOM = 1,
581 FIO_FSERVICE_RR = 2,
a086c257 582 FIO_FSERVICE_SEQ = 3,
0aabe160
JA
583};
584
e592a06b
AC
585/*
586 * when should interactive ETA output be generated
587 */
588enum {
589 FIO_ETA_AUTO,
590 FIO_ETA_ALWAYS,
591 FIO_ETA_NEVER,
592};
593
e1161c32 594#define __td_verror(td, err, msg, func) \
ebac4655 595 do { \
19abcd3d
JA
596 if ((td)->error) \
597 break; \
ebac4655
JA
598 int e = (err); \
599 (td)->error = e; \
e1161c32 600 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
ebac4655
JA
601 } while (0)
602
b990b5c0 603
e1161c32
JA
604#define td_verror(td, err, func) \
605 __td_verror((td), (err), strerror((err)), (func))
606#define td_vmsg(td, err, msg, func) \
607 __td_verror((td), (err), (msg), (func))
b990b5c0 608
ebac4655
JA
609extern int exitall_on_terminate;
610extern int thread_number;
9cedf167 611extern int nr_process, nr_thread;
ebac4655
JA
612extern int shm_id;
613extern int groupid;
c6ae0a5b 614extern int terse_output;
53cdc686 615extern int temp_stall_ts;
1e97cce9 616extern unsigned long long mlock_size;
cfc99db7 617extern unsigned long page_mask, page_size;
4241ea8f 618extern int read_only;
e592a06b 619extern int eta_print;
d3eeeabc 620extern unsigned long done_secs;
01f06b63 621extern char *job_section;
be4ecfdf
JA
622extern int fio_gtod_offload;
623extern int fio_gtod_cpu;
ebac4655
JA
624
625extern struct thread_data *threads;
626
2dc1bbeb
JA
627#define td_read(td) ((td)->o.td_ddir & TD_DDIR_READ)
628#define td_write(td) ((td)->o.td_ddir & TD_DDIR_WRITE)
629#define td_rw(td) (((td)->o.td_ddir & TD_DDIR_RW) == TD_DDIR_RW)
630#define td_random(td) ((td)->o.td_ddir & TD_DDIR_RAND)
303032ae 631#define file_randommap(td, f) (!(td)->o.norandommap && (f)->file_map)
ebac4655 632
7101d9c2
JA
633static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
634{
635 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
636}
637
de605666 638#define BLOCKS_PER_MAP (8 * sizeof(int))
aec2de20
JA
639#define TO_MAP_BLOCK(f, b) (b)
640#define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
641#define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
ebac4655
JA
642
643#define MAX_JOBS (1024)
644
87dc1ab1
JA
645static inline int should_fsync(struct thread_data *td)
646{
647 if (td->last_was_sync)
648 return 0;
2dc1bbeb 649 if (td->o.odirect)
87dc1ab1 650 return 0;
2dc1bbeb 651 if (td_write(td) || td_rw(td) || td->o.override_sync)
87dc1ab1
JA
652 return 1;
653
654 return 0;
655}
656
1c9e06ee
JA
657/*
658 * Disk utils as read in /sys/block/<dev>/stat
659 */
ebac4655
JA
660struct disk_util_stat {
661 unsigned ios[2];
662 unsigned merges[2];
663 unsigned long long sectors[2];
664 unsigned ticks[2];
665 unsigned io_ticks;
666 unsigned time_in_queue;
667};
668
1c9e06ee
JA
669/*
670 * Per-device disk util management
671 */
ebac4655 672struct disk_util {
01743ee1 673 struct flist_head list;
67423013
ST
674 /* If this disk is a slave, hook it into the master's
675 * list using this head.
676 */
677 struct flist_head slavelist;
ebac4655
JA
678
679 char *name;
e11c410c 680 char *sysfs_root;
ebac4655 681 char path[256];
07e5b264 682 int major, minor;
ebac4655
JA
683
684 struct disk_util_stat dus;
685 struct disk_util_stat last_dus;
686
67423013
ST
687 /* For software raids, this entry maintains pointers to the
688 * entries for the slave devices. The disk_util entries for
689 * the slaves devices should primarily be maintained through
690 * the disk_list list, i.e. for memory allocation and
691 * de-allocation, etc. Whereas this list should be used only
692 * for aggregating a software RAID's disk util figures.
693 */
694 struct flist_head slaves;
695
ebac4655
JA
696 unsigned long msec;
697 struct timeval time;
c97bd0fa
JA
698
699 struct fio_mutex *lock;
700 unsigned long users;
ebac4655
JA
701};
702
c97bd0fa
JA
703static inline void disk_util_inc(struct disk_util *du)
704{
705 if (du) {
706 fio_mutex_down(du->lock);
707 du->users++;
708 fio_mutex_up(du->lock);
709 }
710}
711
712static inline void disk_util_dec(struct disk_util *du)
713{
714 if (du) {
715 fio_mutex_down(du->lock);
716 du->users--;
717 fio_mutex_up(du->lock);
718 }
719}
720
ebac4655
JA
721#define DISK_UTIL_MSEC (250)
722
6796209a
JA
723/*
724 * Log exports
725 */
f29b25a3
JA
726enum file_log_act {
727 FIO_LOG_ADD_FILE,
728 FIO_LOG_OPEN_FILE,
729 FIO_LOG_CLOSE_FILE,
f718273e 730 FIO_LOG_UNLINK_FILE,
f29b25a3
JA
731};
732
72cb971b 733extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
f29b25a3
JA
734extern void log_io_u(struct thread_data *, struct io_u *);
735extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
72cb971b 736extern int __must_check init_iolog(struct thread_data *td);
6796209a 737extern void log_io_piece(struct thread_data *, struct io_u *);
691c8fb0 738extern void queue_io_piece(struct thread_data *, struct io_piece *);
6796209a
JA
739extern void prune_io_piece_log(struct thread_data *);
740extern void write_iolog_close(struct thread_data *);
741
742/*
743 * Logging
744 */
306ddc97
JA
745extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long,
746 unsigned int);
747extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long,
748 unsigned int);
749extern void add_bw_sample(struct thread_data *, enum fio_ddir, unsigned int,
750 struct timeval *);
6796209a
JA
751extern void show_run_stats(void);
752extern void init_disk_util(struct thread_data *);
753extern void update_rusage_stat(struct thread_data *);
754extern void update_io_ticks(void);
8914a9d8
JA
755extern void setup_log(struct io_log **);
756extern void finish_log(struct thread_data *, struct io_log *, const char *);
e3cedca7 757extern void finish_log_named(struct thread_data *, struct io_log *, const char *, const char *);
bb3884d8 758extern void __finish_log(struct io_log *, const char *);
bb3884d8
JA
759extern struct io_log *agg_io_log[2];
760extern int write_bw_log;
306ddc97 761extern void add_agg_sample(unsigned long, enum fio_ddir, unsigned int);
6796209a
JA
762
763/*
764 * Time functions
765 */
10927316
SL
766extern unsigned long long utime_since(struct timeval *, struct timeval *);
767extern unsigned long long utime_since_now(struct timeval *);
6796209a
JA
768extern unsigned long mtime_since(struct timeval *, struct timeval *);
769extern unsigned long mtime_since_now(struct timeval *);
770extern unsigned long time_since_now(struct timeval *);
263e529f 771extern unsigned long mtime_since_genesis(void);
6dd6f2cd 772extern void usec_spin(unsigned int);
6796209a 773extern void usec_sleep(struct thread_data *, unsigned long);
413dd459 774extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
6043c579 775extern void fill_start_time(struct timeval *);
02bcaa8c 776extern void fio_gettime(struct timeval *, void *);
be4ecfdf
JA
777extern void fio_gtod_init(void);
778extern void fio_gtod_update(void);
a2f77c9f 779extern void set_genesis_time(void);
721938ae 780extern int ramp_time_over(struct thread_data *);
b29ee5b3 781extern int in_ramp_time(struct thread_data *);
6796209a 782
8914a9d8 783/*
214e1eca 784 * Init/option functions
8914a9d8 785 */
72cb971b 786extern int __must_check parse_options(int, char **);
3b8b7135 787extern int fio_options_parse(struct thread_data *, char **, int);
214e1eca
JA
788extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
789extern void fio_fill_default_options(struct thread_data *);
790extern int fio_show_option_help(const char *);
791extern void fio_options_dup_and_init(struct option *);
d23bb327
JA
792extern void options_mem_dupe(struct thread_data *);
793extern void options_mem_free(struct thread_data *);
5bfc35d7 794extern void td_fill_rand_seeds(struct thread_data *);
214e1eca
JA
795#define FIO_GETOPT_JOB 0x89988998
796#define FIO_NR_OPTIONS 128
8914a9d8 797
53cdc686
JA
798/*
799 * File setup/shutdown
800 */
801extern void close_files(struct thread_data *);
24ffd2c2 802extern void close_and_free_files(struct thread_data *);
b2fdda43 803extern int __must_check setup_files(struct thread_data *);
b2fdda43 804extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
b5af8293 805extern int __must_check generic_open_file(struct thread_data *, struct fio_file *);
6977bcd0 806extern int __must_check generic_close_file(struct thread_data *, struct fio_file *);
df9c26b1 807extern int __must_check generic_get_file_size(struct thread_data *, struct fio_file *);
afad68f7 808extern int __must_check pre_read_files(struct thread_data *);
f29b25a3 809extern int add_file(struct thread_data *, const char *);
0ad920e7 810extern void get_file(struct fio_file *);
6977bcd0 811extern int __must_check put_file(struct thread_data *, struct fio_file *);
4d4e80f2
JA
812extern void lock_file(struct thread_data *, struct fio_file *, enum fio_ddir);
813extern void unlock_file(struct thread_data *, struct fio_file *);
814extern void unlock_file_all(struct thread_data *, struct fio_file *);
bbf6b540 815extern int add_dir_files(struct thread_data *, const char *);
68727076 816extern int init_random_map(struct thread_data *);
cade3ef4 817extern void dup_files(struct thread_data *, struct thread_data *);
f29b25a3
JA
818extern int get_fileno(struct thread_data *, const char *);
819extern void free_release_files(struct thread_data *);
53cdc686 820
263e529f
JA
821/*
822 * ETA/status stuff
823 */
824extern void print_thread_status(void);
825extern void print_status_init(int);
826
9f8f2064
JA
827/*
828 * disk util stuff
829 */
830#ifdef FIO_HAVE_DISK_UTIL
831extern void show_disk_util(void);
9f8f2064
JA
832extern void init_disk_util(struct thread_data *);
833extern void update_io_ticks(void);
834#else
835#define show_disk_util()
9f8f2064
JA
836#define init_disk_util(td)
837#define update_io_ticks()
838#endif
839
263e529f
JA
840/*
841 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
842 * will never back again. It may cycle between running/verififying/fsyncing.
843 * Once the thread reaches TD_EXITED, it is just waiting for the core to
844 * reap it.
845 */
846enum {
847 TD_NOT_CREATED = 0,
848 TD_CREATED,
849 TD_INITIALIZED,
b29ee5b3 850 TD_RAMP,
263e529f 851 TD_RUNNING,
b0f65863 852 TD_PRE_READING,
263e529f
JA
853 TD_VERIFYING,
854 TD_FSYNCING,
855 TD_EXITED,
856 TD_REAPED,
857};
858
b29ee5b3
JA
859extern void td_set_runstate(struct thread_data *, int);
860
e29d1b70
JA
861/*
862 * Verify helpers
863 */
864extern void populate_verify_io_u(struct thread_data *, struct io_u *);
b2fdda43 865extern int __must_check get_next_verify(struct thread_data *td, struct io_u *);
36690c9b 866extern int __must_check verify_io_u(struct thread_data *, struct io_u *);
e29d1b70 867
2f9ade3c
JA
868/*
869 * Memory helpers
870 */
b2fdda43 871extern int __must_check fio_pin_memory(void);
2f9ade3c 872extern void fio_unpin_memory(void);
b2fdda43 873extern int __must_check allocate_io_mem(struct thread_data *);
2f9ade3c
JA
874extern void free_io_mem(struct thread_data *);
875
10ba535a
JA
876/*
877 * io unit handling
878 */
01743ee1 879#define queue_full(td) flist_empty(&(td)->io_u_freelist)
10ba535a 880extern struct io_u *__get_io_u(struct thread_data *);
3d7c391d 881extern struct io_u *get_io_u(struct thread_data *);
10ba535a 882extern void put_io_u(struct thread_data *, struct io_u *);
755200a3 883extern void requeue_io_u(struct thread_data *, struct io_u **);
d7762cf8
JA
884extern long __must_check io_u_sync_complete(struct thread_data *, struct io_u *);
885extern long __must_check io_u_queued_complete(struct thread_data *, int);
7e77dd02 886extern void io_u_queued(struct thread_data *, struct io_u *);
5451792e 887extern void io_u_log_error(struct thread_data *, struct io_u *);
d8005759 888extern void io_u_mark_depth(struct thread_data *, unsigned int);
5973cafb 889extern void io_u_fill_buffer(struct thread_data *td, struct io_u *, unsigned int);
838bc709
JA
890void io_u_mark_complete(struct thread_data *, unsigned int);
891void io_u_mark_submit(struct thread_data *, unsigned int);
10ba535a 892
b29ee5b3
JA
893/*
894 * Reset stats after ramp time completes
895 */
896extern void reset_all_stats(struct thread_data *);
897
10ba535a
JA
898/*
899 * io engine entry points
900 */
b2fdda43
JA
901extern int __must_check td_io_init(struct thread_data *);
902extern int __must_check td_io_prep(struct thread_data *, struct io_u *);
903extern int __must_check td_io_queue(struct thread_data *, struct io_u *);
904extern int __must_check td_io_sync(struct thread_data *, struct fio_file *);
e7d2e616 905extern int __must_check td_io_getevents(struct thread_data *, unsigned int, unsigned int, struct timespec *);
b2fdda43 906extern int __must_check td_io_commit(struct thread_data *);
b5af8293 907extern int __must_check td_io_open_file(struct thread_data *, struct fio_file *);
6977bcd0 908extern int td_io_close_file(struct thread_data *, struct fio_file *);
df9c26b1 909extern int __must_check td_io_get_file_size(struct thread_data *, struct fio_file *);
10ba535a 910
fb7b71a3
JA
911/*
912 * blktrace support
913 */
5e62c22a 914#ifdef FIO_HAVE_BLKTRACE
fb7b71a3
JA
915extern int is_blktrace(const char *);
916extern int load_blktrace(struct thread_data *, const char *);
5e62c22a 917#endif
fb7b71a3 918
2866c82d 919struct ioengine_ops {
01743ee1 920 struct flist_head list;
2866c82d
JA
921 char name[16];
922 int version;
923 int flags;
ea2877a4 924 int (*setup)(struct thread_data *);
2866c82d
JA
925 int (*init)(struct thread_data *);
926 int (*prep)(struct thread_data *, struct io_u *);
927 int (*queue)(struct thread_data *, struct io_u *);
755200a3 928 int (*commit)(struct thread_data *);
e7d2e616 929 int (*getevents)(struct thread_data *, unsigned int, unsigned int, struct timespec *);
2866c82d
JA
930 struct io_u *(*event)(struct thread_data *, int);
931 int (*cancel)(struct thread_data *, struct io_u *);
932 void (*cleanup)(struct thread_data *);
b5af8293 933 int (*open_file)(struct thread_data *, struct fio_file *);
6977bcd0 934 int (*close_file)(struct thread_data *, struct fio_file *);
df9c26b1 935 int (*get_file_size)(struct thread_data *, struct fio_file *);
2866c82d
JA
936 void *data;
937 void *dlhandle;
938};
939
df9c26b1 940#define FIO_IOOPS_VERSION 10
2866c82d 941
b4692828 942extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
b2fdda43 943extern void register_ioengine(struct ioengine_ops *);
5f350952 944extern void unregister_ioengine(struct ioengine_ops *);
2866c82d
JA
945extern void close_ioengine(struct thread_data *);
946
947/*
948 * Mark unused variables passed to ops functions as unused, to silence gcc
949 */
950#define fio_unused __attribute((__unused__))
5f350952
JA
951#define fio_init __attribute__((constructor))
952#define fio_exit __attribute__((destructor))
2866c82d 953
34572e28
JA
954#define for_each_td(td, i) \
955 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
53cdc686 956#define for_each_file(td, f, i) \
691c8fb0
JA
957 if ((td)->files_index) \
958 for ((i) = 0, (f) = (td)->files[0]; \
959 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
960 (i)++)
53cdc686 961
0032bf9f
JA
962#define fio_assert(td, cond) do { \
963 if (!(cond)) { \
340fd243 964 int *__foo = NULL; \
0032bf9f
JA
965 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
966 (td)->runstate = TD_EXITED; \
437c9b71 967 (td)->error = EFAULT; \
340fd243 968 *__foo = 0; \
0032bf9f
JA
969 } \
970} while (0)
971
009bd847
JA
972static inline void fio_file_reset(struct fio_file *f)
973{
974 f->last_free_lookup = 0;
975 f->last_pos = f->file_offset;
eaf9b417
JA
976 if (f->file_map)
977 memset(f->file_map, 0, f->num_maps * sizeof(int));
009bd847
JA
978}
979
9bf27b45
JA
980static inline void clear_error(struct thread_data *td)
981{
982 td->error = 0;
983 td->verror[0] = '\0';
984}
985
79e48f72 986#ifdef FIO_INC_DEBUG
2ba1c290
JA
987static inline void dprint_io_u(struct io_u *io_u, const char *p)
988{
989 struct fio_file *f = io_u->file;
990
991 dprint(FD_IO, "%s: io_u %p: off=%llu/len=%lu/ddir=%d", p, io_u,
5921e80c
JA
992 (unsigned long long) io_u->offset,
993 io_u->buflen, io_u->ddir);
8172fe97
JA
994 if (fio_debug & (1 << FD_IO)) {
995 if (f)
996 log_info("/%s", f->file_name);
997
998 log_info("\n");
999 }
2ba1c290 1000}
79e48f72 1001#else
79e48f72
JA
1002#define dprint_io_u(io_u, p)
1003#endif
2ba1c290 1004
12d9d841
JA
1005static inline int fio_fill_issue_time(struct thread_data *td)
1006{
1007 if (td->o.read_iolog_file ||
1008 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
1009 return 1;
1010
1011 return 0;
1012}
1013
ebac4655 1014#endif