backend: privatize nr_thread/nr_process
[fio.git] / fio.h
1 #ifndef FIO_H
2 #define FIO_H
3
4 #include <sched.h>
5 #include <limits.h>
6 #include <pthread.h>
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <assert.h>
16
17 struct thread_data;
18
19 #include "compiler/compiler.h"
20 #include "thread_options.h"
21 #include "flist.h"
22 #include "fifo.h"
23 #include "rbtree.h"
24 #include "arch/arch.h"
25 #include "os/os.h"
26 #include "mutex.h"
27 #include "log.h"
28 #include "debug.h"
29 #include "file.h"
30 #include "io_ddir.h"
31 #include "ioengine.h"
32 #include "iolog.h"
33 #include "helpers.h"
34 #include "options.h"
35 #include "profile.h"
36 #include "time.h"
37 #include "gettime.h"
38 #include "lib/getopt.h"
39 #include "lib/rand.h"
40 #include "client.h"
41 #include "server.h"
42 #include "stat.h"
43 #include "flow.h"
44
45 #ifdef FIO_HAVE_GUASI
46 #include <guasi.h>
47 #endif
48
49 #ifdef FIO_HAVE_SOLARISAIO
50 #include <sys/asynch.h>
51 #endif
52
53 /*
54  * offset generator types
55  */
56 enum {
57         RW_SEQ_SEQ      = 0,
58         RW_SEQ_IDENT,
59 };
60
61 /*
62  * This describes a single thread/process executing a fio job.
63  */
64 struct thread_data {
65         struct thread_options o;
66         void *eo;
67         char verror[FIO_VERROR_SIZE];
68         pthread_t thread;
69         unsigned int thread_number;
70         unsigned int groupid;
71         struct thread_stat ts;
72
73         int client_type;
74
75         struct io_log *slat_log;
76         struct io_log *clat_log;
77         struct io_log *lat_log;
78         struct io_log *bw_log;
79         struct io_log *iops_log;
80
81         uint64_t stat_io_bytes[2];
82         struct timeval bw_sample_time;
83
84         uint64_t stat_io_blocks[2];
85         struct timeval iops_sample_time;
86
87         struct rusage ru_start;
88         struct rusage ru_end;
89
90         struct fio_file **files;
91         unsigned int files_size;
92         unsigned int files_index;
93         unsigned int nr_open_files;
94         unsigned int nr_done_files;
95         unsigned int nr_normal_files;
96         union {
97                 unsigned int next_file;
98                 os_random_state_t next_file_state;
99                 struct frand_state __next_file_state;
100         };
101         int error;
102         int done;
103         pid_t pid;
104         char *orig_buffer;
105         size_t orig_buffer_size;
106         volatile int terminate;
107         volatile int runstate;
108         unsigned int ioprio;
109         unsigned int ioprio_set;
110         unsigned int last_was_sync;
111         enum fio_ddir last_ddir;
112
113         char *mmapfile;
114         int mmapfd;
115
116         void *iolog_buf;
117         FILE *iolog_f;
118
119         char *sysfs_root;
120
121         unsigned long rand_seeds[8];
122
123         union {
124                 os_random_state_t bsrange_state;
125                 struct frand_state __bsrange_state;
126         };
127         union {
128                 os_random_state_t verify_state;
129                 struct frand_state __verify_state;
130         };
131         union {
132                 os_random_state_t trim_state;
133                 struct frand_state __trim_state;
134         };
135
136         struct frand_state buf_state;
137
138         unsigned int verify_batch;
139         unsigned int trim_batch;
140
141         int shm_id;
142
143         /*
144          * IO engine hooks, contains everything needed to submit an io_u
145          * to any of the available IO engines.
146          */
147         struct ioengine_ops *io_ops;
148
149         /*
150          * Queue depth of io_u's that fio MIGHT do
151          */
152         unsigned int cur_depth;
153
154         /*
155          * io_u's about to be committed
156          */
157         unsigned int io_u_queued;
158
159         /*
160          * io_u's submitted but not completed yet
161          */
162         unsigned int io_u_in_flight;
163
164         /*
165          * List of free and busy io_u's
166          */
167         struct flist_head io_u_freelist;
168         struct flist_head io_u_busylist;
169         struct flist_head io_u_requeues;
170         pthread_mutex_t io_u_lock;
171         pthread_cond_t free_cond;
172
173         /*
174          * async verify offload
175          */
176         struct flist_head verify_list;
177         pthread_t *verify_threads;
178         unsigned int nr_verify_threads;
179         pthread_cond_t verify_cond;
180         int verify_thread_exit;
181
182         /*
183          * Rate state
184          */
185         unsigned long long rate_bps[2];
186         long rate_pending_usleep[2];
187         unsigned long rate_bytes[2];
188         unsigned long rate_blocks[2];
189         struct timeval lastrate[2];
190
191         unsigned long long total_io_size;
192         unsigned long long fill_device_size;
193
194         unsigned long io_issues[2];
195         unsigned long long io_blocks[2];
196         unsigned long long this_io_blocks[2];
197         unsigned long long io_bytes[2];
198         unsigned long long io_skip_bytes;
199         unsigned long long this_io_bytes[2];
200         unsigned long long zone_bytes;
201         struct fio_mutex *mutex;
202
203         /*
204          * State for random io, a bitmap of blocks done vs not done
205          */
206         union {
207                 os_random_state_t random_state;
208                 struct frand_state __random_state;
209         };
210
211         struct timeval start;   /* start of this loop */
212         struct timeval epoch;   /* time job was started */
213         struct timeval last_issue;
214         struct timeval tv_cache;
215         unsigned int tv_cache_nr;
216         unsigned int tv_cache_mask;
217         unsigned int ramp_time_over;
218
219         /*
220          * read/write mixed workload state
221          */
222         union {
223                 os_random_state_t rwmix_state;
224                 struct frand_state __rwmix_state;
225         };
226         unsigned long rwmix_issues;
227         enum fio_ddir rwmix_ddir;
228         unsigned int ddir_seq_nr;
229
230         /*
231          * IO history logs for verification. We use a tree for sorting,
232          * if we are overwriting. Otherwise just use a fifo.
233          */
234         struct rb_root io_hist_tree;
235         struct flist_head io_hist_list;
236         unsigned long io_hist_len;
237
238         /*
239          * For IO replaying
240          */
241         struct flist_head io_log_list;
242
243         /*
244          * For tracking/handling discards
245          */
246         struct flist_head trim_list;
247         unsigned long trim_entries;
248
249         /*
250          * for fileservice, how often to switch to a new file
251          */
252         unsigned int file_service_nr;
253         unsigned int file_service_left;
254         struct fio_file *file_service_file;
255
256         unsigned int sync_file_range_nr;
257
258         /*
259          * For generating file sizes
260          */
261         union {
262                 os_random_state_t file_size_state;
263                 struct frand_state __file_size_state;
264         };
265
266         /*
267          * Error counts
268          */
269         unsigned int total_err_count;
270         int first_error;
271
272         struct fio_flow *flow;
273
274         /*
275          * Can be overloaded by profiles
276          */
277         struct prof_io_ops prof_io_ops;
278         void *prof_data;
279 };
280
281 /*
282  * when should interactive ETA output be generated
283  */
284 enum {
285         FIO_ETA_AUTO,
286         FIO_ETA_ALWAYS,
287         FIO_ETA_NEVER,
288 };
289
290 #define __td_verror(td, err, msg, func)                                 \
291         do {                                                            \
292                 if ((td)->error)                                        \
293                         break;                                          \
294                 int e = (err);                                          \
295                 (td)->error = e;                                        \
296                 if (!(td)->first_error)                                 \
297                         snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));               \
298         } while (0)
299
300
301 #define td_clear_error(td)              \
302         (td)->error = 0;
303 #define td_verror(td, err, func)        \
304         __td_verror((td), (err), strerror((err)), (func))
305 #define td_vmsg(td, err, msg, func)     \
306         __td_verror((td), (err), (msg), (func))
307
308 #define __fio_stringify_1(x)    #x
309 #define __fio_stringify(x)      __fio_stringify_1(x)
310
311 extern int exitall_on_terminate;
312 extern unsigned int thread_number;
313 extern int shm_id;
314 extern int groupid;
315 extern int terse_output;
316 extern int temp_stall_ts;
317 extern unsigned long long mlock_size;
318 extern unsigned long page_mask, page_size;
319 extern int read_only;
320 extern int eta_print;
321 extern unsigned long done_secs;
322 extern char *job_section;
323 extern int fio_gtod_offload;
324 extern int fio_gtod_cpu;
325 extern enum fio_cs fio_clock_source;
326 extern int warnings_fatal;
327 extern int terse_version;
328 extern int is_backend;
329 extern int nr_clients;
330 extern int log_syslog;
331 extern const char fio_version_string[];
332 extern const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN];
333
334 extern struct thread_data *threads;
335
336 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
337 {
338         assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
339 }
340
341 #define BLOCKS_PER_MAP          (8 * sizeof(unsigned long))
342 #define TO_MAP_BLOCK(f, b)      (b)
343 #define RAND_MAP_IDX(f, b)      (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
344 #define RAND_MAP_BIT(f, b)      (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
345
346 #define REAL_MAX_JOBS           2048
347
348 #define td_non_fatal_error(e)   ((e) == EIO || (e) == EILSEQ)
349
350 static inline enum error_type td_error_type(enum fio_ddir ddir, int err)
351 {
352         if (err == EILSEQ)
353                 return ERROR_TYPE_VERIFY;
354         if (ddir == DDIR_READ)
355                 return ERROR_TYPE_READ;
356         return ERROR_TYPE_WRITE;
357 }
358
359 static inline void update_error_count(struct thread_data *td, int err)
360 {
361         td->total_err_count++;
362         if (td->total_err_count == 1)
363                 td->first_error = err;
364 }
365
366 static inline int should_fsync(struct thread_data *td)
367 {
368         if (td->last_was_sync)
369                 return 0;
370         if (td->o.odirect)
371                 return 0;
372         if (td_write(td) || td_rw(td) || td->o.override_sync)
373                 return 1;
374
375         return 0;
376 }
377
378 /*
379  * Init/option functions
380  */
381 extern int __must_check fio_init_options(void);
382 extern int __must_check parse_options(int, char **);
383 extern int parse_jobs_ini(char *, int, int, int);
384 extern int parse_cmd_line(int, char **, int);
385 extern int fio_backend(void);
386 extern void reset_fio_state(void);
387 extern void clear_io_state(struct thread_data *);
388 extern int fio_options_parse(struct thread_data *, char **, int);
389 extern void fio_keywords_init(void);
390 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
391 extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
392 extern void fio_fill_default_options(struct thread_data *);
393 extern int fio_show_option_help(const char *);
394 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
395 extern void fio_options_dup_and_init(struct option *);
396 extern void fio_options_mem_dupe(struct thread_data *);
397 extern void options_mem_dupe(void *data, struct fio_option *options);
398 extern void td_fill_rand_seeds(struct thread_data *);
399 extern void add_job_opts(const char **, int);
400 extern char *num2str(unsigned long, int, int, int);
401 extern int ioengine_load(struct thread_data *);
402
403 extern unsigned long page_mask;
404 extern unsigned long page_size;
405 extern int initialize_fio(char *envp[]);
406
407 #define FIO_GETOPT_JOB          0x89000000
408 #define FIO_GETOPT_IOENGINE     0x98000000
409 #define FIO_NR_OPTIONS          (FIO_MAX_OPTS + 128)
410
411 /*
412  * ETA/status stuff
413  */
414 extern void print_thread_status(void);
415 extern void print_status_init(int);
416 extern char *fio_uint_to_kmg(unsigned int val);
417
418 /*
419  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
420  * will never back again. It may cycle between running/verififying/fsyncing.
421  * Once the thread reaches TD_EXITED, it is just waiting for the core to
422  * reap it.
423  */
424 enum {
425         TD_NOT_CREATED = 0,
426         TD_CREATED,
427         TD_INITIALIZED,
428         TD_RAMP,
429         TD_RUNNING,
430         TD_PRE_READING,
431         TD_VERIFYING,
432         TD_FSYNCING,
433         TD_EXITED,
434         TD_REAPED,
435 };
436
437 extern void td_set_runstate(struct thread_data *, int);
438 #define TERMINATE_ALL           (-1)
439 extern void fio_terminate_threads(int);
440
441 /*
442  * Memory helpers
443  */
444 extern int __must_check fio_pin_memory(void);
445 extern void fio_unpin_memory(void);
446 extern int __must_check allocate_io_mem(struct thread_data *);
447 extern void free_io_mem(struct thread_data *);
448
449 /*
450  * Reset stats after ramp time completes
451  */
452 extern void reset_all_stats(struct thread_data *);
453
454 /*
455  * blktrace support
456  */
457 #ifdef FIO_HAVE_BLKTRACE
458 extern int is_blktrace(const char *);
459 extern int load_blktrace(struct thread_data *, const char *);
460 #endif
461
462 /*
463  * Mark unused variables passed to ops functions as unused, to silence gcc
464  */
465 #define fio_unused      __attribute((__unused__))
466 #define fio_init        __attribute__((constructor))
467 #define fio_exit        __attribute__((destructor))
468
469 #define for_each_td(td, i)      \
470         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
471 #define for_each_file(td, f, i) \
472         if ((td)->files_index)                                          \
473                 for ((i) = 0, (f) = (td)->files[0];                     \
474                  (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
475                  (i)++)
476
477 #define fio_assert(td, cond)    do {    \
478         if (!(cond)) {                  \
479                 int *__foo = NULL;      \
480                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
481                 td_set_runstate((td), TD_EXITED);       \
482                 (td)->error = EFAULT;           \
483                 *__foo = 0;                     \
484         }       \
485 } while (0)
486
487 static inline int fio_fill_issue_time(struct thread_data *td)
488 {
489         if (td->o.read_iolog_file ||
490             !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
491                 return 1;
492
493         return 0;
494 }
495
496 static inline int __should_check_rate(struct thread_data *td,
497                                       enum fio_ddir ddir)
498 {
499         struct thread_options *o = &td->o;
500
501         /*
502          * If some rate setting was given, we need to check it
503          */
504         if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
505             o->rate_iops_min[ddir])
506                 return 1;
507
508         return 0;
509 }
510
511 static inline int should_check_rate(struct thread_data *td,
512                                     unsigned long *bytes_done)
513 {
514         int ret = 0;
515
516         if (bytes_done[0])
517                 ret |= __should_check_rate(td, 0);
518         if (bytes_done[1])
519                 ret |= __should_check_rate(td, 1);
520
521         return ret;
522 }
523
524 static inline int is_power_of_2(unsigned int val)
525 {
526         return (val != 0 && ((val & (val - 1)) == 0));
527 }
528
529 /*
530  * We currently only need to do locking if we have verifier threads
531  * accessing our internal structures too
532  */
533 static inline void td_io_u_lock(struct thread_data *td)
534 {
535         if (td->o.verify_async)
536                 pthread_mutex_lock(&td->io_u_lock);
537 }
538
539 static inline void td_io_u_unlock(struct thread_data *td)
540 {
541         if (td->o.verify_async)
542                 pthread_mutex_unlock(&td->io_u_lock);
543 }
544
545 static inline void td_io_u_free_notify(struct thread_data *td)
546 {
547         if (td->o.verify_async)
548                 pthread_cond_signal(&td->free_cond);
549 }
550
551 extern const char *fio_get_arch_string(int);
552 extern const char *fio_get_os_string(int);
553
554 #endif