[PATCH] syslet: fix leak of ring and ahu
[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
15 #include "list.h"
16 #include "md5.h"
17 #include "crc32.h"
18 #include "arch.h"
19 #include "os.h"
20
21 #include "syslet.h"
22
23 enum fio_ddir {
24         DDIR_READ = 0,
25         DDIR_WRITE,
26         DDIR_SYNC,
27 };
28
29 /*
30  * Use for maintaining statistics
31  */
32 struct io_stat {
33         unsigned long max_val;
34         unsigned long min_val;
35         unsigned long samples;
36
37         double mean;
38         double S;
39 };
40
41 /*
42  * A single data sample
43  */
44 struct io_sample {
45         unsigned long time;
46         unsigned long val;
47         enum fio_ddir ddir;
48 };
49
50 /*
51  * Dynamically growing data sample log
52  */
53 struct io_log {
54         unsigned long nr_samples;
55         unsigned long max_samples;
56         struct io_sample *log;
57 };
58
59 /*
60  * When logging io actions, this matches a single sent io_u
61  */
62 struct io_piece {
63         struct list_head list;
64         struct fio_file *file;
65         unsigned long long offset;
66         unsigned long len;
67         enum fio_ddir ddir;
68 };
69
70 #ifdef FIO_HAVE_SYSLET
71 struct syslet_req {
72         struct syslet_uatom atom;
73         long ret;
74 };
75 #endif
76
77 /*
78  * The io unit
79  */
80 struct io_u {
81         union {
82 #ifdef FIO_HAVE_LIBAIO
83                 struct iocb iocb;
84 #endif
85 #ifdef FIO_HAVE_POSIXAIO
86                 struct aiocb aiocb;
87 #endif
88 #ifdef FIO_HAVE_SGIO
89                 struct sg_io_hdr hdr;
90 #endif
91 #ifdef FIO_HAVE_SYSLET
92                 struct syslet_req req;
93 #endif
94         };
95         struct timeval start_time;
96         struct timeval issue_time;
97
98         /*
99          * Allocated/set buffer and length
100          */
101         void *buf;
102         unsigned long buflen;
103         unsigned long long offset;
104
105         /*
106          * IO engine state, may be different from above when we get
107          * partial transfers / residual data counts
108          */
109         void *xfer_buf;
110         unsigned long xfer_buflen;
111
112         unsigned int resid;
113         unsigned int error;
114
115         enum fio_ddir ddir;
116
117         /*
118          * io engine private data
119          */
120         union {
121                 unsigned int index;
122                 unsigned int seen;
123         };
124
125         struct fio_file *file;
126
127         struct list_head list;
128 };
129
130 #define FIO_HDR_MAGIC   0xf00baaef
131
132 enum {
133         VERIFY_NONE = 0,                /* no verification */
134         VERIFY_MD5,                     /* md5 sum data blocks */
135         VERIFY_CRC32,                   /* crc32 sum data blocks */
136 };
137
138 /*
139  * A header structure associated with each checksummed data block
140  */
141 struct verify_header {
142         unsigned int fio_magic;
143         unsigned int len;
144         unsigned int verify_type;
145         union {
146                 char md5_digest[MD5_HASH_WORDS * 4];
147                 unsigned long crc32;
148         };
149 };
150
151 struct group_run_stats {
152         unsigned long long max_run[2], min_run[2];
153         unsigned long long max_bw[2], min_bw[2];
154         unsigned long long io_kb[2];
155         unsigned long long agg[2];
156 };
157
158 /*
159  * What type of allocation to use for io buffers
160  */
161 enum fio_memtype {
162         MEM_MALLOC = 0, /* ordinary malloc */
163         MEM_SHM,        /* use shared memory segments */
164         MEM_SHMHUGE,    /* use shared memory segments with huge pages */
165         MEM_MMAP,       /* use anonynomous mmap */
166         MEM_MMAPHUGE,   /* memory mapped huge file */
167 };
168
169 /*
170  * The type of object we are working on
171  */
172 enum fio_filetype {
173         FIO_TYPE_FILE = 1,              /* plain file */
174         FIO_TYPE_BD,                    /* block device */
175         FIO_TYPE_CHAR,                  /* character device */
176 };
177
178 enum fio_ioengine_flags {
179         FIO_SYNCIO      = 1 << 0,       /* io engine has synchronous ->queue */
180         FIO_CPUIO       = 1 << 1,       /* cpu burner, doesn't do real io */
181         FIO_MMAPIO      = 1 << 2,       /* uses memory mapped io */
182         FIO_RAWIO       = 1 << 3,       /* some sort of direct/raw io */
183         FIO_NETIO       = 1 << 4,       /* networked io */
184         FIO_NULLIO      = 1 << 5,       /* no real data transfer (cpu/null) */
185 };
186
187 /*
188  * Each thread_data structure has a number of files associated with it,
189  * this structure holds state information for a single file.
190  */
191 struct fio_file {
192         /*
193          * A file may not be a file descriptor, let the io engine decide
194          */
195         union {
196                 unsigned long file_data;
197                 long fd;
198         };
199         char *file_name;
200         void *mmap;
201         unsigned long long file_size;
202         unsigned long long real_file_size;
203         unsigned long long file_offset;
204         unsigned long long last_pos;
205         unsigned long long last_completed_pos;
206
207         /*
208          * block map for random io
209          */
210         unsigned long *file_map;
211         unsigned int num_maps;
212         unsigned int last_free_lookup;
213
214         unsigned int unlink;
215 };
216
217 /*
218  * How many depth levels to log
219  */
220 #define FIO_IO_U_MAP_NR 8
221
222 /*
223  * This describes a single thread/process executing a fio job.
224  */
225 struct thread_data {
226         char *description;
227         char *name;
228         char *directory;
229         char *filename;
230         char verror[80];
231         pthread_t thread;
232         int thread_number;
233         int groupid;
234         enum fio_filetype filetype;
235         struct fio_file *files;
236         unsigned int nr_files;
237         unsigned int nr_uniq_files;
238         unsigned int next_file;
239         int error;
240         pid_t pid;
241         char *orig_buffer;
242         size_t orig_buffer_size;
243         volatile int terminate;
244         volatile int runstate;
245         enum fio_ddir ddir;
246         unsigned int iomix;
247         unsigned int ioprio;
248         unsigned int last_was_sync;
249
250         unsigned int sequential;
251         unsigned int odirect;
252         unsigned int invalidate_cache;
253         unsigned int create_serialize;
254         unsigned int create_fsync;
255         unsigned int end_fsync;
256         unsigned int sync_io;
257         unsigned int verify;
258         unsigned int use_thread;
259         unsigned int unlink;
260         unsigned int do_disk_util;
261         unsigned int override_sync;
262         unsigned int rand_repeatable;
263         unsigned int write_lat_log;
264         unsigned int write_bw_log;
265         unsigned int norandommap;
266         unsigned int bs_unaligned;
267
268         unsigned int bs[2];
269         unsigned int min_bs[2];
270         unsigned int max_bs[2];
271         unsigned int hugepage_size;
272         unsigned int rw_min_bs;
273         unsigned int thinktime;
274         unsigned int thinktime_blocks;
275         unsigned int fsync_blocks;
276         unsigned int start_delay;
277         unsigned long timeout;
278         unsigned int overwrite;
279         unsigned int bw_avg_time;
280         unsigned int loops;
281         unsigned long long zone_size;
282         unsigned long long zone_skip;
283         enum fio_memtype mem_type;
284         char *mmapfile;
285         int mmapfd;
286         unsigned int stonewall;
287         unsigned int numjobs;
288         unsigned int iodepth;
289         os_cpu_mask_t cpumask;
290         unsigned int iolog;
291         unsigned int read_iolog;
292         unsigned int rwmixcycle;
293         unsigned int rwmixread;
294         unsigned int rwmixwrite;
295         unsigned int nice;
296
297         char *read_iolog_file;
298         char *write_iolog_file;
299         void *iolog_buf;
300         FILE *iolog_f;
301
302         char *sysfs_root;
303         char *ioscheduler;
304
305         os_random_state_t bsrange_state;
306         os_random_state_t verify_state;
307
308         int shm_id;
309
310         /*
311          * IO engine hooks, contains everything needed to submit an io_u
312          * to any of the available IO engines.
313          */
314         struct ioengine_ops *io_ops;
315
316         /*
317          * Current IO depth and list of free and busy io_u's.
318          */
319         unsigned int cur_depth;
320         unsigned int io_u_map[FIO_IO_U_MAP_NR];
321         unsigned long total_io_u;
322         struct list_head io_u_freelist;
323         struct list_head io_u_busylist;
324
325         /*
326          * Rate state
327          */
328         unsigned int rate;
329         unsigned int ratemin;
330         unsigned int ratecycle;
331         unsigned long rate_usec_cycle;
332         long rate_pending_usleep;
333         unsigned long rate_bytes;
334         struct timeval lastrate;
335
336         unsigned long runtime[2];               /* msec */
337         unsigned long long io_size;
338         unsigned long long total_file_size;
339         unsigned long long start_offset;
340         unsigned long long total_io_size;
341
342         unsigned long long io_blocks[2];
343         unsigned long long io_bytes[2];
344         unsigned long long zone_bytes;
345         unsigned long long this_io_bytes[2];
346         volatile int mutex;
347
348         /*
349          * State for random io, a bitmap of blocks done vs not done
350          */
351         os_random_state_t random_state;
352
353         /*
354          * CPU "io" cycle burner
355          */
356         unsigned int cpuload;
357         unsigned int cpucycle;
358
359         /*
360          * bandwidth and latency stats
361          */
362         struct io_stat clat_stat[2];            /* completion latency */
363         struct io_stat slat_stat[2];            /* submission latency */
364         struct io_stat bw_stat[2];              /* bandwidth stats */
365
366         unsigned long long stat_io_bytes[2];
367         struct timeval stat_sample_time[2];
368
369         struct io_log *slat_log;
370         struct io_log *clat_log;
371         struct io_log *bw_log;
372
373         struct timeval start;   /* start of this loop */
374         struct timeval epoch;   /* time job was started */
375         struct timeval end_time;/* time job ended */
376
377         /*
378          * fio system usage accounting
379          */
380         struct rusage ru_start;
381         struct rusage ru_end;
382         unsigned long usr_time;
383         unsigned long sys_time;
384         unsigned long ctx;
385
386         /*
387          * read/write mixed workload state
388          */
389         os_random_state_t rwmix_state;
390         struct timeval rwmix_switch;
391         enum fio_ddir rwmix_ddir;
392
393         /*
394          * Pre-run and post-run shell
395          */
396         char *exec_prerun;
397         char *exec_postrun;
398
399         /*
400          * IO historic logs
401          */
402         struct list_head io_hist_list;
403         struct list_head io_log_list;
404 };
405
406 #define __td_verror(td, err, msg)                                       \
407         do {                                                            \
408                 int e = (err);                                          \
409                 (td)->error = e;                                        \
410                 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, (msg));        \
411         } while (0)
412
413
414 #define td_verror(td, err)      __td_verror((td), (err), strerror((err)))
415 #define td_vmsg(td, err, msg)   __td_verror((td), (err), (msg))
416
417 extern int exitall_on_terminate;
418 extern int thread_number;
419 extern int shm_id;
420 extern int groupid;
421 extern int terse_output;
422 extern FILE *f_out;
423 extern FILE *f_err;
424 extern int temp_stall_ts;
425 extern unsigned long long mlock_size;
426
427 extern struct thread_data *threads;
428
429 #define td_read(td)             ((td)->ddir == DDIR_READ)
430 #define td_write(td)            ((td)->ddir == DDIR_WRITE)
431 #define td_rw(td)               ((td)->iomix != 0)
432
433 #define BLOCKS_PER_MAP          (8 * sizeof(long))
434 #define TO_MAP_BLOCK(td, f, b)  ((b) - ((f)->file_offset / (td)->rw_min_bs))
435 #define RAND_MAP_IDX(td, f, b)  (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
436 #define RAND_MAP_BIT(td, f, b)  (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
437
438 #define MAX_JOBS        (1024)
439
440 static inline int should_fsync(struct thread_data *td)
441 {
442         if (td->last_was_sync)
443                 return 0;
444         if (td->odirect)
445                 return 0;
446         if (td_write(td) || td_rw(td) || td->override_sync)
447                 return 1;
448
449         return 0;
450 }
451
452 /*
453  * Disk utils as read in /sys/block/<dev>/stat
454  */
455 struct disk_util_stat {
456         unsigned ios[2];
457         unsigned merges[2];
458         unsigned long long sectors[2];
459         unsigned ticks[2];
460         unsigned io_ticks;
461         unsigned time_in_queue;
462 };
463
464 /*
465  * Per-device disk util management
466  */
467 struct disk_util {
468         struct list_head list;
469
470         char *name;
471         char path[256];
472         dev_t dev;
473
474         struct disk_util_stat dus;
475         struct disk_util_stat last_dus;
476
477         unsigned long msec;
478         struct timeval time;
479 };
480
481 /*
482  * Used for passing io_u completion data
483  */
484 struct io_completion_data {
485         int nr;                         /* input */
486
487         int error;                      /* output */
488         unsigned long bytes_done[2];    /* output */
489         struct timeval time;            /* output */
490 };
491
492 #define DISK_UTIL_MSEC  (250)
493
494 #ifndef min
495 #define min(a, b)       ((a) < (b) ? (a) : (b))
496 #endif
497 #ifndef max
498 #define max(a, b)       ((a) > (b) ? (a) : (b))
499 #endif
500
501 /*
502  * Log exports
503  */
504 extern int read_iolog_get(struct thread_data *, struct io_u *);
505 extern void write_iolog_put(struct thread_data *, struct io_u *);
506 extern int init_iolog(struct thread_data *td);
507 extern void log_io_piece(struct thread_data *, struct io_u *);
508 extern void prune_io_piece_log(struct thread_data *);
509 extern void write_iolog_close(struct thread_data *);
510
511 /*
512  * Logging
513  */
514 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
515 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
516 extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
517 extern void show_run_stats(void);
518 extern void init_disk_util(struct thread_data *);
519 extern void update_rusage_stat(struct thread_data *);
520 extern void update_io_ticks(void);
521 extern void disk_util_timer_arm(void);
522 extern void setup_log(struct io_log **);
523 extern void finish_log(struct thread_data *, struct io_log *, const char *);
524 extern void __finish_log(struct io_log *, const char *);
525 extern int setup_rate(struct thread_data *);
526 extern struct io_log *agg_io_log[2];
527 extern int write_bw_log;
528 extern void add_agg_sample(unsigned long, enum fio_ddir);
529
530 /*
531  * Time functions
532  */
533 extern unsigned long utime_since(struct timeval *, struct timeval *);
534 extern unsigned long utime_since_now(struct timeval *);
535 extern unsigned long mtime_since(struct timeval *, struct timeval *);
536 extern unsigned long mtime_since_now(struct timeval *);
537 extern unsigned long time_since_now(struct timeval *);
538 extern unsigned long mtime_since_genesis(void);
539 extern void __usec_sleep(unsigned int);
540 extern void usec_sleep(struct thread_data *, unsigned long);
541 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int, int);
542 extern void fill_start_time(struct timeval *);
543 extern void fio_gettime(struct timeval *, void *);
544
545 /*
546  * Init functions
547  */
548 extern int parse_options(int, char **);
549 extern int init_random_state(struct thread_data *);
550
551 /*
552  * File setup/shutdown
553  */
554 extern void close_files(struct thread_data *);
555 extern int setup_files(struct thread_data *);
556 extern int open_files(struct thread_data *);
557 extern int file_invalidate_cache(struct thread_data *, struct fio_file *);
558
559 /*
560  * ETA/status stuff
561  */
562 extern void print_thread_status(void);
563 extern void print_status_init(int);
564
565 /*
566  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
567  * will never back again. It may cycle between running/verififying/fsyncing.
568  * Once the thread reaches TD_EXITED, it is just waiting for the core to
569  * reap it.
570  */
571 enum {
572         TD_NOT_CREATED = 0,
573         TD_CREATED,
574         TD_INITIALIZED,
575         TD_RUNNING,
576         TD_VERIFYING,
577         TD_FSYNCING,
578         TD_EXITED,
579         TD_REAPED,
580 };
581
582 /*
583  * Verify helpers
584  */
585 extern void populate_verify_io_u(struct thread_data *, struct io_u *);
586 extern int get_next_verify(struct thread_data *td, struct io_u *);
587 extern int do_io_u_verify(struct thread_data *, struct io_u **);
588
589 /*
590  * Memory helpers
591  */
592 extern int fio_pin_memory(void);
593 extern void fio_unpin_memory(void);
594 extern int allocate_io_mem(struct thread_data *);
595 extern void free_io_mem(struct thread_data *);
596
597 /*
598  * io unit handling
599  */
600 #define queue_full(td)  list_empty(&(td)->io_u_freelist)
601 extern struct io_u *__get_io_u(struct thread_data *);
602 extern struct io_u *get_io_u(struct thread_data *, struct fio_file *);
603 extern void put_io_u(struct thread_data *, struct io_u *);
604 extern void ios_completed(struct thread_data *, struct io_completion_data *);
605 extern void io_completed(struct thread_data *, struct io_u *, struct io_completion_data *);
606
607 /*
608  * io engine entry points
609  */
610 extern int td_io_init(struct thread_data *);
611 extern int td_io_prep(struct thread_data *, struct io_u *);
612 extern int td_io_queue(struct thread_data *, struct io_u *);
613 extern int td_io_sync(struct thread_data *, struct fio_file *);
614 extern int td_io_getevents(struct thread_data *, int, int, struct timespec *);
615
616 /*
617  * This is a pretty crappy semaphore implementation, but with the use that fio
618  * has (just signalling start/go conditions), it doesn't have to be better.
619  * Naturally this would not work for any type of contended semaphore or
620  * for real locking.
621  */
622 static inline void fio_sem_init(volatile int *sem, int val)
623 {
624         *sem = val;
625 }
626
627 static inline void fio_sem_down(volatile int *sem)
628 {
629         while (*sem == 0)
630                 usleep(10000);
631
632         (*sem)--;
633 }
634
635 static inline void fio_sem_up(volatile int *sem)
636 {
637         (*sem)++;
638 }
639
640 /*
641  * If logging output to a file, stderr should go to both stderr and f_err
642  */
643 #define log_err(args...)        do {            \
644         fprintf(f_err, ##args);                 \
645         if (f_err != stderr)                    \
646                 fprintf(stderr, ##args);        \
647         } while (0)
648
649 struct ioengine_ops {
650         struct list_head list;
651         char name[16];
652         int version;
653         int flags;
654         int (*setup)(struct thread_data *);
655         int (*init)(struct thread_data *);
656         int (*prep)(struct thread_data *, struct io_u *);
657         int (*queue)(struct thread_data *, struct io_u *);
658         int (*getevents)(struct thread_data *, int, int, struct timespec *);
659         struct io_u *(*event)(struct thread_data *, int);
660         int (*cancel)(struct thread_data *, struct io_u *);
661         void (*cleanup)(struct thread_data *);
662         void *data;
663         void *dlhandle;
664 };
665
666 #define FIO_IOOPS_VERSION       3
667
668 extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
669 extern int register_ioengine(struct ioengine_ops *);
670 extern void unregister_ioengine(struct ioengine_ops *);
671 extern void close_ioengine(struct thread_data *);
672
673 /*
674  * Mark unused variables passed to ops functions as unused, to silence gcc
675  */
676 #define fio_unused      __attribute((__unused__))
677 #define fio_init        __attribute__((constructor))
678 #define fio_exit        __attribute__((destructor))
679
680 #define for_each_td(td, i)      \
681         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
682 #define for_each_file(td, f, i) \
683         for ((i) = 0, (f) = &(td)->files[0]; (i) < (int) (td)->nr_files; (i)++, (f)++)
684
685 #define fio_assert(td, cond)    do {    \
686         if (!(cond)) {                  \
687                 int *__foo = NULL;      \
688                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
689                 (td)->runstate = TD_EXITED;     \
690                 (td)->error = EFAULT;           \
691                 *__foo = 0;                     \
692         }       \
693 } while (0)
694
695 #endif