Add support for absolute random zones
[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>
cd14cc10 14#include <inttypes.h>
7101d9c2 15#include <assert.h>
ebac4655 16
317b95d0 17#include "compiler/compiler.h"
8062f527 18#include "thread_options.h"
01743ee1 19#include "flist.h"
e2887563 20#include "fifo.h"
317b95d0
JA
21#include "arch/arch.h"
22#include "os/os.h"
07739b57 23#include "mutex.h"
a3d741fa
JA
24#include "log.h"
25#include "debug.h"
d6aed795
JA
26#include "file.h"
27#include "io_ddir.h"
618ee94c 28#include "ioengines.h"
5995a6a4 29#include "iolog.h"
c5c8bd5c 30#include "helpers.h"
07b3232d 31#include "options.h"
7eb36574 32#include "profile.h"
ef3c94b6 33#include "fio_time.h"
a7346816 34#include "gettime.h"
f87cc561 35#include "oslib/getopt.h"
2615cc4b 36#include "lib/rand.h"
7fb6d453 37#include "lib/rbtree.h"
5e746820 38#include "lib/num2str.h"
dd366728 39#include "client.h"
37db14fe 40#include "server.h"
a64e88da 41#include "stat.h"
9e684a49 42#include "flow.h"
8ef89e0b 43#include "io_u.h"
2ae0b204 44#include "io_u_queue.h"
a9da8ab2 45#include "workqueue.h"
0a70e050 46#include "steadystate.h"
ebac4655 47
4700b234 48#ifdef CONFIG_SOLARISAIO
417f0068
JA
49#include <sys/asynch.h>
50#endif
51
67bf9823 52#ifdef CONFIG_LIBNUMA
d0b937ed
YR
53#include <linux/mempolicy.h>
54#include <numa.h>
55
56/*
57 * "local" is pseudo-policy
58 */
59#define MPOL_LOCAL MPOL_MAX
60#endif
61
03553853
YR
62#ifdef CONFIG_CUDA
63#include <cuda.h>
64#endif
65
38dad62d
JA
66/*
67 * offset generator types
68 */
69enum {
70 RW_SEQ_SEQ = 0,
71 RW_SEQ_IDENT,
72};
73
d72be545 74enum {
a9f517ae
JA
75 __TD_F_VER_BACKLOG = 0,
76 __TD_F_TRIM_BACKLOG,
77 __TD_F_READ_IOLOG,
78 __TD_F_REFILL_BUFFERS,
79 __TD_F_SCRAMBLE_BUFFERS,
80 __TD_F_VER_NONE,
81 __TD_F_PROFILE_OPS,
82 __TD_F_COMPRESS,
83 __TD_F_COMPRESS_LOG,
84 __TD_F_VSTATE_SAVED,
85 __TD_F_NEED_LOCK,
86 __TD_F_CHILD,
87 __TD_F_NO_PROGRESS,
88 __TD_F_REGROW_LOGS,
89 __TD_F_MMAP_KEEP,
f1c71016 90 __TD_F_DIRS_CREATED,
9c6f1e75 91 __TD_F_LAST, /* not a real bit, keep last */
a9f517ae
JA
92};
93
94enum {
95 TD_F_VER_BACKLOG = 1U << __TD_F_VER_BACKLOG,
96 TD_F_TRIM_BACKLOG = 1U << __TD_F_TRIM_BACKLOG,
97 TD_F_READ_IOLOG = 1U << __TD_F_READ_IOLOG,
98 TD_F_REFILL_BUFFERS = 1U << __TD_F_REFILL_BUFFERS,
99 TD_F_SCRAMBLE_BUFFERS = 1U << __TD_F_SCRAMBLE_BUFFERS,
100 TD_F_VER_NONE = 1U << __TD_F_VER_NONE,
101 TD_F_PROFILE_OPS = 1U << __TD_F_PROFILE_OPS,
102 TD_F_COMPRESS = 1U << __TD_F_COMPRESS,
103 TD_F_COMPRESS_LOG = 1U << __TD_F_COMPRESS_LOG,
104 TD_F_VSTATE_SAVED = 1U << __TD_F_VSTATE_SAVED,
105 TD_F_NEED_LOCK = 1U << __TD_F_NEED_LOCK,
106 TD_F_CHILD = 1U << __TD_F_CHILD,
107 TD_F_NO_PROGRESS = 1U << __TD_F_NO_PROGRESS,
108 TD_F_REGROW_LOGS = 1U << __TD_F_REGROW_LOGS,
109 TD_F_MMAP_KEEP = 1U << __TD_F_MMAP_KEEP,
f1c71016 110 TD_F_DIRS_CREATED = 1U << __TD_F_DIRS_CREATED,
d72be545
JA
111};
112
d24945f0
JA
113enum {
114 FIO_RAND_BS_OFF = 0,
2f282cec
JA
115 FIO_RAND_BS1_OFF,
116 FIO_RAND_BS2_OFF,
d24945f0
JA
117 FIO_RAND_VER_OFF,
118 FIO_RAND_MIX_OFF,
119 FIO_RAND_FILE_OFF,
120 FIO_RAND_BLOCK_OFF,
121 FIO_RAND_FILE_SIZE_OFF,
122 FIO_RAND_TRIM_OFF,
123 FIO_RAND_BUF_OFF,
d9472271
JA
124 FIO_RAND_SEQ_RAND_READ_OFF,
125 FIO_RAND_SEQ_RAND_WRITE_OFF,
126 FIO_RAND_SEQ_RAND_TRIM_OFF,
23ed19b0 127 FIO_RAND_START_DELAY,
5c94b008 128 FIO_DEDUPE_OFF,
e7b24047 129 FIO_RAND_POISSON_OFF,
e0a04ac1 130 FIO_RAND_ZONE_OFF,
6c8611c7
JA
131 FIO_RAND_POISSON2_OFF,
132 FIO_RAND_POISSON3_OFF,
d24945f0
JA
133 FIO_RAND_NR_OFFS,
134};
135
a9da8ab2
JA
136enum {
137 IO_MODE_INLINE = 0,
6de65959
JA
138 IO_MODE_OFFLOAD = 1,
139
140 RATE_PROCESS_LINEAR = 0,
141 RATE_PROCESS_POISSON = 1,
a9da8ab2
JA
142};
143
ecb2083d
JA
144enum {
145 F_ADV_NONE = 0,
146 F_ADV_TYPE,
147 F_ADV_RANDOM,
148 F_ADV_SEQUENTIAL,
149};
150
2a2804c9
JA
151/*
152 * Per-thread/process specific data. Only used for the network client
153 * for now.
154 */
24660963
JA
155void sk_out_assign(struct sk_out *);
156void sk_out_drop(void);
2a2804c9 157
e0a04ac1
JA
158struct zone_split_index {
159 uint8_t size_perc;
160 uint8_t size_perc_prev;
59466396
JA
161 uint64_t size;
162 uint64_t size_prev;
e0a04ac1
JA
163};
164
2dc1bbeb
JA
165/*
166 * This describes a single thread/process executing a fio job.
167 */
168struct thread_data {
c2292325 169 struct flist_head opt_list;
d72be545 170 unsigned long flags;
d7e9266c 171 struct thread_options o;
de890a1e 172 void *eo;
2dc1bbeb 173 pthread_t thread;
2f122b13 174 unsigned int thread_number;
69bdd6ba 175 unsigned int subjob_number;
2f122b13 176 unsigned int groupid;
b2fcbe01 177 struct thread_stat ts __attribute__ ((aligned(8)));
7b9f733a 178
46bcd498
JA
179 int client_type;
180
7b9f733a
JA
181 struct io_log *slat_log;
182 struct io_log *clat_log;
1e613c9c 183 struct io_log *clat_hist_log;
7b9f733a
JA
184 struct io_log *lat_log;
185 struct io_log *bw_log;
c8eeb9df 186 struct io_log *iops_log;
7b9f733a 187
155f2f02 188 struct workqueue log_compress_wq;
aee2ab67 189
a9da8ab2
JA
190 struct thread_data *parent;
191
6eaf09d6 192 uint64_t stat_io_bytes[DDIR_RWDIR_CNT];
8b6a404c 193 struct timespec bw_sample_time;
c8eeb9df 194
6eaf09d6 195 uint64_t stat_io_blocks[DDIR_RWDIR_CNT];
8b6a404c 196 struct timespec iops_sample_time;
f0505a14 197
c97f1ad6
JA
198 volatile int update_rusage;
199 struct fio_mutex *rusage_sem;
c8aaba19
JA
200 struct rusage ru_start;
201 struct rusage ru_end;
202
126d65c6 203 struct fio_file **files;
d7df1d13 204 unsigned char *file_locks;
dd87b2c9 205 unsigned int files_size;
2dc1bbeb
JA
206 unsigned int files_index;
207 unsigned int nr_open_files;
1020a139 208 unsigned int nr_done_files;
2dc1bbeb
JA
209 unsigned int nr_normal_files;
210 union {
211 unsigned int next_file;
d6b72507 212 struct frand_state next_file_state;
2dc1bbeb 213 };
8c07860d
JA
214 union {
215 struct zipf_state next_file_zipf;
216 struct gauss_state next_file_gauss;
217 };
218 union {
219 double zipf_theta;
220 double pareto_h;
221 double gauss_dev;
222 };
2dc1bbeb 223 int error;
a5e371a6 224 int sig;
20e354ef 225 int done;
ca09be4b 226 int stop_io;
2dc1bbeb
JA
227 pid_t pid;
228 char *orig_buffer;
229 size_t orig_buffer_size;
230 volatile int terminate;
231 volatile int runstate;
2dc1bbeb 232 unsigned int last_was_sync;
9e144189 233 enum fio_ddir last_ddir;
2dc1bbeb 234
2dc1bbeb
JA
235 int mmapfd;
236
843a7413
JA
237 void *iolog_buf;
238 FILE *iolog_f;
ebac4655 239
d24945f0 240 unsigned long rand_seeds[FIO_RAND_NR_OFFS];
5bfc35d7 241
2f282cec 242 struct frand_state bsrange_state[DDIR_RWDIR_CNT];
d6b72507
JA
243 struct frand_state verify_state;
244 struct frand_state trim_state;
245 struct frand_state delay_state;
ebac4655 246
3545a109 247 struct frand_state buf_state;
5c94b008
JA
248 struct frand_state buf_state_prev;
249 struct frand_state dedupe_state;
e0a04ac1
JA
250 struct frand_state zone_state;
251
252 struct zone_split_index **zone_state_index;
3545a109 253
9e144189 254 unsigned int verify_batch;
0d29de83 255 unsigned int trim_batch;
9e144189 256
ca09be4b
JA
257 struct thread_io_list *vstate;
258
ebac4655
JA
259 int shm_id;
260
e9c047a0
JA
261 /*
262 * IO engine hooks, contains everything needed to submit an io_u
263 * to any of the available IO engines.
264 */
2866c82d 265 struct ioengine_ops *io_ops;
356ef1a1 266 int io_ops_init;
ebac4655 267
565e784d
TK
268 /*
269 * IO engine private data and dlhandle.
270 */
271 void *io_ops_data;
272 void *io_ops_dlhandle;
273
e9c047a0 274 /*
422f9e4b 275 * Queue depth of io_u's that fio MIGHT do
e9c047a0 276 */
ebac4655 277 unsigned int cur_depth;
422f9e4b
RM
278
279 /*
280 * io_u's about to be committed
281 */
d8005759 282 unsigned int io_u_queued;
422f9e4b
RM
283
284 /*
285 * io_u's submitted but not completed yet
286 */
287 unsigned int io_u_in_flight;
288
289 /*
290 * List of free and busy io_u's
291 */
2ae0b204
JA
292 struct io_u_ring io_u_requeues;
293 struct io_u_queue io_u_freelist;
294 struct io_u_queue io_u_all;
e8462bd8
JA
295 pthread_mutex_t io_u_lock;
296 pthread_cond_t free_cond;
297
298 /*
299 * async verify offload
300 */
301 struct flist_head verify_list;
302 pthread_t *verify_threads;
303 unsigned int nr_verify_threads;
304 pthread_cond_t verify_cond;
305 int verify_thread_exit;
ebac4655 306
e9c047a0
JA
307 /*
308 * Rate state
309 */
bdb6d20a 310 uint64_t rate_bps[DDIR_RWDIR_CNT];
90eff1c9 311 uint64_t rate_next_io_time[DDIR_RWDIR_CNT];
6eaf09d6
SL
312 unsigned long rate_bytes[DDIR_RWDIR_CNT];
313 unsigned long rate_blocks[DDIR_RWDIR_CNT];
90eff1c9 314 unsigned long long rate_io_issue_bytes[DDIR_RWDIR_CNT];
8b6a404c 315 struct timespec lastrate[DDIR_RWDIR_CNT];
6c8611c7
JA
316 int64_t last_usec[DDIR_RWDIR_CNT];
317 struct frand_state poisson_state[DDIR_RWDIR_CNT];
ebac4655 318
a9da8ab2
JA
319 /*
320 * Enforced rate submission/completion workqueue
321 */
322 struct workqueue io_wq;
323
bdb6d20a
JA
324 uint64_t total_io_size;
325 uint64_t fill_device_size;
ebac4655 326
74d6277f
JA
327 /*
328 * Issue side
329 */
ca09be4b 330 uint64_t io_issues[DDIR_RWDIR_CNT];
74d6277f 331 uint64_t io_issue_bytes[DDIR_RWDIR_CNT];
cf8a46a7 332 uint64_t loops;
74d6277f
JA
333
334 /*
335 * Completions
336 */
bdb6d20a
JA
337 uint64_t io_blocks[DDIR_RWDIR_CNT];
338 uint64_t this_io_blocks[DDIR_RWDIR_CNT];
339 uint64_t io_bytes[DDIR_RWDIR_CNT];
bdb6d20a 340 uint64_t this_io_bytes[DDIR_RWDIR_CNT];
55312f9f 341 uint64_t io_skip_bytes;
bdb6d20a 342 uint64_t zone_bytes;
cdd18ad8 343 struct fio_mutex *mutex;
55312f9f 344 uint64_t bytes_done[DDIR_RWDIR_CNT];
ebac4655 345
e9c047a0
JA
346 /*
347 * State for random io, a bitmap of blocks done vs not done
348 */
d6b72507 349 struct frand_state random_state;
ebac4655 350
8b6a404c
VF
351 struct timespec start; /* start of this loop */
352 struct timespec epoch; /* time job was started */
3aea75b1 353 unsigned long long unix_epoch; /* Time job was started, unix epoch based. */
8b6a404c 354 struct timespec last_issue;
df8fb609 355 long time_offset;
8b6a404c
VF
356 struct timespec ts_cache;
357 struct timespec terminate_time;
358 unsigned int ts_cache_nr;
359 unsigned int ts_cache_mask;
49f3f914 360 bool ramp_time_over;
ebac4655 361
3e260a46
JA
362 /*
363 * Time since last latency_window was started
364 */
8b6a404c 365 struct timespec latency_ts;
3e260a46
JA
366 unsigned int latency_qd;
367 unsigned int latency_qd_high;
368 unsigned int latency_qd_low;
369 unsigned int latency_failed;
370 uint64_t latency_ios;
6bb58215 371 int latency_end_run;
3e260a46 372
e9c047a0
JA
373 /*
374 * read/write mixed workload state
375 */
d6b72507 376 struct frand_state rwmix_state;
e4928662 377 unsigned long rwmix_issues;
e9c047a0 378 enum fio_ddir rwmix_ddir;
5736c10d 379 unsigned int ddir_seq_nr;
a6ccc7be 380
211c9b89
JA
381 /*
382 * rand/seq mixed workload state
383 */
d6b72507 384 struct frand_state seq_rand_state[DDIR_RWDIR_CNT];
211c9b89 385
e9c047a0 386 /*
8de8f047
JA
387 * IO history logs for verification. We use a tree for sorting,
388 * if we are overwriting. Otherwise just use a fifo.
e9c047a0 389 */
4b87898e 390 struct rb_root io_hist_tree;
01743ee1 391 struct flist_head io_hist_list;
9e144189 392 unsigned long io_hist_len;
8de8f047
JA
393
394 /*
395 * For IO replaying
396 */
01743ee1 397 struct flist_head io_log_list;
433afcb4 398
0d29de83
JA
399 /*
400 * For tracking/handling discards
401 */
402 struct flist_head trim_list;
403 unsigned long trim_entries;
404
1ae83d45
JA
405 struct flist_head next_rand_list;
406
1907dbc6
JA
407 /*
408 * for fileservice, how often to switch to a new file
409 */
410 unsigned int file_service_nr;
411 unsigned int file_service_left;
412 struct fio_file *file_service_file;
9c60ce64 413
44f29692
JA
414 unsigned int sync_file_range_nr;
415
9c60ce64
JA
416 /*
417 * For generating file sizes
418 */
d6b72507 419 struct frand_state file_size_state;
f2bba182
RR
420
421 /*
422 * Error counts
423 */
424 unsigned int total_err_count;
425 int first_error;
15dc1934 426
9e684a49
DE
427 struct fio_flow *flow;
428
15dc1934
JA
429 /*
430 * Can be overloaded by profiles
431 */
7eb36574 432 struct prof_io_ops prof_io_ops;
58c55ba0 433 void *prof_data;
9a3f1100
JA
434
435 void *pinned_mem;
d7e9266c 436
16e56d25
VF
437 struct steadystate_data ss;
438
d7e9266c 439 char verror[FIO_VERROR_SIZE];
03553853
YR
440
441#ifdef CONFIG_CUDA
442 /*
443 * for GPU memory management
444 */
445 int gpu_dev_cnt;
446 int gpu_dev_id;
447 CUdevice cu_dev;
448 CUcontext cu_ctx;
449 CUdeviceptr dev_mem_ptr;
450#endif
451
ebac4655
JA
452};
453
e592a06b
AC
454/*
455 * when should interactive ETA output be generated
456 */
457enum {
458 FIO_ETA_AUTO,
459 FIO_ETA_ALWAYS,
460 FIO_ETA_NEVER,
461};
462
e1161c32 463#define __td_verror(td, err, msg, func) \
ebac4655 464 do { \
21beb609 465 unsigned int ____e = (err); \
19abcd3d
JA
466 if ((td)->error) \
467 break; \
8271963e 468 (td)->error = ____e; \
f2bba182 469 if (!(td)->first_error) \
98ffb8f3 470 snprintf(td->verror, sizeof(td->verror), "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
ebac4655
JA
471 } while (0)
472
b990b5c0 473
a9da8ab2
JA
474#define td_clear_error(td) do { \
475 (td)->error = 0; \
476 if ((td)->parent) \
477 (td)->parent->error = 0; \
478} while (0)
479
480#define td_verror(td, err, func) do { \
481 __td_verror((td), (err), strerror((err)), (func)); \
482 if ((td)->parent) \
483 __td_verror((td)->parent, (err), strerror((err)), (func)); \
484} while (0)
485
486#define td_vmsg(td, err, msg, func) do { \
487 __td_verror((td), (err), (msg), (func)); \
488 if ((td)->parent) \
489 __td_verror((td)->parent, (err), (msg), (func)); \
490} while (0)
b990b5c0 491
81179eec
JA
492#define __fio_stringify_1(x) #x
493#define __fio_stringify(x) __fio_stringify_1(x)
494
ebac4655 495extern int exitall_on_terminate;
11e950bd 496extern unsigned int thread_number;
108fea77 497extern unsigned int stat_number;
ebac4655
JA
498extern int shm_id;
499extern int groupid;
f3afa57e 500extern int output_format;
2b8c71b0 501extern int append_terse_output;
53cdc686 502extern int temp_stall_ts;
7078106d 503extern uintptr_t page_mask, page_size;
4241ea8f 504extern int read_only;
e592a06b 505extern int eta_print;
e382e661 506extern int eta_new_line;
d3eeeabc 507extern unsigned long done_secs;
be4ecfdf
JA
508extern int fio_gtod_offload;
509extern int fio_gtod_cpu;
c223da83 510extern enum fio_cs fio_clock_source;
fa80feae 511extern int fio_clock_source_set;
a9523c6f 512extern int warnings_fatal;
f57a9c59 513extern int terse_version;
5c341e9a 514extern int is_backend;
a37f69b7 515extern int nr_clients;
e46d8091 516extern int log_syslog;
06464907 517extern int status_interval;
5e726d0a 518extern const char fio_version_string[];
ca09be4b
JA
519extern char *trigger_file;
520extern char *trigger_cmd;
b63efd30 521extern char *trigger_remote_cmd;
ca09be4b 522extern long long trigger_timeout;
d264264a 523extern char *aux_path;
ebac4655
JA
524
525extern struct thread_data *threads;
526
effd6ff0 527static inline void fio_ro_check(const struct thread_data *td, struct io_u *io_u)
7101d9c2
JA
528{
529 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
530}
531
cab2472e 532#define REAL_MAX_JOBS 4096
ebac4655 533
87dc1ab1
JA
534static inline int should_fsync(struct thread_data *td)
535{
536 if (td->last_was_sync)
537 return 0;
dcf9844e 538 if (td_write(td) || td->o.override_sync)
87dc1ab1
JA
539 return 1;
540
541 return 0;
542}
543
8914a9d8 544/*
214e1eca 545 * Init/option functions
8914a9d8 546 */
0420ba6a 547extern int __must_check fio_init_options(void);
72cb971b 548extern int __must_check parse_options(int, char **);
46bcd498
JA
549extern int parse_jobs_ini(char *, int, int, int);
550extern int parse_cmd_line(int, char **, int);
24660963 551extern int fio_backend(struct sk_out *);
50d16976 552extern void reset_fio_state(void);
ac28d905 553extern void clear_io_state(struct thread_data *, int);
c2292325 554extern int fio_options_parse(struct thread_data *, char **, int);
74929ac2 555extern void fio_keywords_init(void);
af1dc266 556extern void fio_keywords_exit(void);
214e1eca 557extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
de890a1e 558extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
214e1eca
JA
559extern void fio_fill_default_options(struct thread_data *);
560extern int fio_show_option_help(const char *);
de890a1e 561extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
214e1eca 562extern void fio_options_dup_and_init(struct option *);
de890a1e 563extern void fio_options_mem_dupe(struct thread_data *);
5bfc35d7 564extern void td_fill_rand_seeds(struct thread_data *);
8f380de2 565extern void td_fill_verify_state_seed(struct thread_data *);
46bcd498 566extern void add_job_opts(const char **, int);
de890a1e 567extern int ioengine_load(struct thread_data *);
8aa89d70 568extern bool parse_dryrun(void);
046395d7 569extern int fio_running_or_pending_io_threads(void);
3a35845f 570extern int fio_set_fd_nonblocking(int, const char *);
d2235e56 571extern void sig_show_status(int sig);
66e19a38 572extern struct thread_data *get_global_options(void);
1ec3d69b 573
a569b45f
JA
574extern uintptr_t page_mask;
575extern uintptr_t page_size;
576extern int initialize_fio(char *envp[]);
af1dc266 577extern void deinitialize_fio(void);
a569b45f 578
de890a1e
SL
579#define FIO_GETOPT_JOB 0x89000000
580#define FIO_GETOPT_IOENGINE 0x98000000
07b3232d 581#define FIO_NR_OPTIONS (FIO_MAX_OPTS + 128)
8914a9d8 582
263e529f
JA
583/*
584 * ETA/status stuff
585 */
586extern void print_thread_status(void);
587extern void print_status_init(int);
0a428b23 588extern char *fio_uint_to_kmg(unsigned int val);
263e529f
JA
589
590/*
591 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
592 * will never back again. It may cycle between running/verififying/fsyncing.
593 * Once the thread reaches TD_EXITED, it is just waiting for the core to
594 * reap it.
595 */
596enum {
597 TD_NOT_CREATED = 0,
598 TD_CREATED,
599 TD_INITIALIZED,
b29ee5b3 600 TD_RAMP,
36d80bc7 601 TD_SETTING_UP,
263e529f 602 TD_RUNNING,
b0f65863 603 TD_PRE_READING,
263e529f
JA
604 TD_VERIFYING,
605 TD_FSYNCING,
3d434057 606 TD_FINISHING,
263e529f
JA
607 TD_EXITED,
608 TD_REAPED,
6e760cf3 609 TD_LAST,
9b87f09b 610 TD_NR,
263e529f
JA
611};
612
9b87f09b
JA
613#define TD_ENG_FLAG_SHIFT 16
614#define TD_ENG_FLAG_MASK ((1U << 16) - 1)
615
9b87f09b
JA
616static inline void td_set_ioengine_flags(struct thread_data *td)
617{
3d92b849
SW
618 td->flags = (~(TD_ENG_FLAG_MASK << TD_ENG_FLAG_SHIFT) & td->flags) |
619 (td->io_ops->flags << TD_ENG_FLAG_SHIFT);
9b87f09b
JA
620}
621
8f6d1311
JA
622static inline bool td_ioengine_flagged(struct thread_data *td,
623 enum fio_ioengine_flags flags)
9b87f09b 624{
8f6d1311 625 return ((td->flags >> TD_ENG_FLAG_SHIFT) & flags) != 0;
9b87f09b
JA
626}
627
b29ee5b3 628extern void td_set_runstate(struct thread_data *, int);
8edd973d
JA
629extern int td_bump_runstate(struct thread_data *, int);
630extern void td_restore_runstate(struct thread_data *, int);
9cc8cb91 631extern const char *runstate_to_name(int runstate);
8edd973d 632
cba5460c
JA
633/*
634 * Allow 60 seconds for a job to quit on its own, otherwise reap with
635 * a vengeance.
636 */
8ecf6df1 637#define FIO_REAP_TIMEOUT 300
cba5460c 638
d883efbd
JA
639#define TERMINATE_ALL (-1U)
640extern void fio_terminate_threads(unsigned int);
ebea2133 641extern void fio_mark_td_terminate(struct thread_data *);
b29ee5b3 642
2f9ade3c
JA
643/*
644 * Memory helpers
645 */
9a3f1100
JA
646extern int __must_check fio_pin_memory(struct thread_data *);
647extern void fio_unpin_memory(struct thread_data *);
b2fdda43 648extern int __must_check allocate_io_mem(struct thread_data *);
2f9ade3c 649extern void free_io_mem(struct thread_data *);
2bb3f0a7 650extern void free_threads_shm(void);
2f9ade3c 651
248c9436
TK
652#ifdef FIO_INTERNAL
653#define PTR_ALIGN(ptr, mask) \
654 (char *) (((uintptr_t) (ptr) + (mask)) & ~(mask))
655#endif
656
b29ee5b3
JA
657/*
658 * Reset stats after ramp time completes
659 */
660extern void reset_all_stats(struct thread_data *);
661
40511301
JA
662extern int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret,
663 enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify,
8b6a404c 664 struct timespec *comp_time);
40511301 665
3e260a46
JA
666/*
667 * Latency target helpers
668 */
669extern void lat_target_check(struct thread_data *);
670extern void lat_target_init(struct thread_data *);
6bb58215 671extern void lat_target_reset(struct thread_data *);
3e260a46 672
79591fa9
TK
673/*
674 * Iterates all threads/processes within all the defined jobs
675 */
34572e28
JA
676#define for_each_td(td, i) \
677 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
53cdc686 678#define for_each_file(td, f, i) \
691c8fb0
JA
679 if ((td)->files_index) \
680 for ((i) = 0, (f) = (td)->files[0]; \
681 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
682 (i)++)
53cdc686 683
0032bf9f
JA
684#define fio_assert(td, cond) do { \
685 if (!(cond)) { \
340fd243 686 int *__foo = NULL; \
0032bf9f 687 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
ac18ea38 688 td_set_runstate((td), TD_EXITED); \
437c9b71 689 (td)->error = EFAULT; \
340fd243 690 *__foo = 0; \
0032bf9f
JA
691 } \
692} while (0)
693
8aa89d70 694static inline bool fio_fill_issue_time(struct thread_data *td)
12d9d841
JA
695{
696 if (td->o.read_iolog_file ||
697 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
8aa89d70 698 return true;
12d9d841 699
8aa89d70 700 return false;
12d9d841
JA
701}
702
8aa89d70
JA
703static inline bool __should_check_rate(struct thread_data *td,
704 enum fio_ddir ddir)
581e7141
JA
705{
706 struct thread_options *o = &td->o;
707
708 /*
709 * If some rate setting was given, we need to check it
710 */
711 if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
712 o->rate_iops_min[ddir])
8aa89d70 713 return true;
581e7141 714
8aa89d70 715 return false;
581e7141
JA
716}
717
8aa89d70 718static inline bool should_check_rate(struct thread_data *td)
581e7141 719{
8aa89d70
JA
720 if (td->bytes_done[DDIR_READ] && __should_check_rate(td, DDIR_READ))
721 return true;
722 if (td->bytes_done[DDIR_WRITE] && __should_check_rate(td, DDIR_WRITE))
723 return true;
724 if (td->bytes_done[DDIR_TRIM] && __should_check_rate(td, DDIR_TRIM))
725 return true;
726
727 return false;
581e7141
JA
728}
729
74f4b020
JA
730static inline unsigned int td_max_bs(struct thread_data *td)
731{
732 unsigned int max_bs;
733
734 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
735 return max(td->o.max_bs[DDIR_TRIM], max_bs);
736}
737
8d916c94
JA
738static inline unsigned int td_min_bs(struct thread_data *td)
739{
740 unsigned int min_bs;
741
742 min_bs = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
743 return min(td->o.min_bs[DDIR_TRIM], min_bs);
744}
745
1651e431 746static inline bool td_async_processing(struct thread_data *td)
a9da8ab2
JA
747{
748 return (td->flags & TD_F_NEED_LOCK) != 0;
749}
750
e8462bd8
JA
751/*
752 * We currently only need to do locking if we have verifier threads
753 * accessing our internal structures too
754 */
755static inline void td_io_u_lock(struct thread_data *td)
756{
a9da8ab2 757 if (td_async_processing(td))
e8462bd8
JA
758 pthread_mutex_lock(&td->io_u_lock);
759}
760
761static inline void td_io_u_unlock(struct thread_data *td)
762{
a9da8ab2 763 if (td_async_processing(td))
e8462bd8
JA
764 pthread_mutex_unlock(&td->io_u_lock);
765}
766
767static inline void td_io_u_free_notify(struct thread_data *td)
768{
a9da8ab2 769 if (td_async_processing(td))
e8462bd8
JA
770 pthread_cond_signal(&td->free_cond);
771}
772
1651e431
JA
773static inline void td_flags_clear(struct thread_data *td, unsigned int *flags,
774 unsigned int value)
775{
776 if (!td_async_processing(td))
777 *flags &= ~value;
778 else
779 __sync_fetch_and_and(flags, ~value);
780}
781
782static inline void td_flags_set(struct thread_data *td, unsigned int *flags,
783 unsigned int value)
784{
785 if (!td_async_processing(td))
786 *flags |= value;
787 else
788 __sync_fetch_and_or(flags, value);
789}
790
cca84643
JA
791extern const char *fio_get_arch_string(int);
792extern const char *fio_get_os_string(int);
793
e2c81040 794#ifdef FIO_INTERNAL
71deafc6
RP
795#define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0])))
796#define FIELD_SIZE(s, f) (sizeof(((typeof(s))0)->f))
e2c81040 797#endif
05775438 798
f3afa57e 799enum {
129fb2d4
JA
800 __FIO_OUTPUT_TERSE = 0,
801 __FIO_OUTPUT_JSON = 1,
802 __FIO_OUTPUT_NORMAL = 2,
513e37ee
VF
803 __FIO_OUTPUT_JSON_PLUS = 3,
804 FIO_OUTPUT_NR = 4,
129fb2d4
JA
805
806 FIO_OUTPUT_TERSE = 1U << __FIO_OUTPUT_TERSE,
807 FIO_OUTPUT_JSON = 1U << __FIO_OUTPUT_JSON,
808 FIO_OUTPUT_NORMAL = 1U << __FIO_OUTPUT_NORMAL,
513e37ee 809 FIO_OUTPUT_JSON_PLUS = 1U << __FIO_OUTPUT_JSON_PLUS,
f3afa57e
JA
810};
811
e25839d4
JA
812enum {
813 FIO_RAND_DIST_RANDOM = 0,
814 FIO_RAND_DIST_ZIPF,
925fee33 815 FIO_RAND_DIST_PARETO,
56d9fa4b 816 FIO_RAND_DIST_GAUSS,
e0a04ac1 817 FIO_RAND_DIST_ZONED,
59466396 818 FIO_RAND_DIST_ZONED_ABS,
e25839d4
JA
819};
820
c95f9404
JA
821#define FIO_DEF_ZIPF 1.1
822#define FIO_DEF_PARETO 0.2
823
8055e41d
JA
824enum {
825 FIO_RAND_GEN_TAUSWORTHE = 0,
826 FIO_RAND_GEN_LFSR,
c3546b53 827 FIO_RAND_GEN_TAUSWORTHE64,
8055e41d
JA
828};
829
c2acfbac
JA
830enum {
831 FIO_CPUS_SHARED = 0,
832 FIO_CPUS_SPLIT,
833};
834
ca09be4b
JA
835extern void exec_trigger(const char *);
836extern void check_trigger_file(void);
837
ebac4655 838#endif