blob: 38e9d0d43003c466ae13defebea219db8057b7c1 [file] [log] [blame]
Jens Axboed6aed792009-06-03 08:41:15 +02001#ifndef FIO_FILE_H
2#define FIO_FILE_H
3
Jens Axboee2e58882011-01-04 08:36:06 +01004#include <string.h>
Bruce Cranecc314b2011-01-04 10:59:30 +01005#include "compiler/compiler.h"
Jens Axboed6aed792009-06-03 08:41:15 +02006#include "io_ddir.h"
Bruce Cranecc314b2011-01-04 10:59:30 +01007#include "flist.h"
Jens Axboe9c6f6312012-11-07 09:15:45 +01008#include "lib/zipf.h"
Jens Axboed6aed792009-06-03 08:41:15 +02009
10/*
11 * The type of object we are working on
12 */
13enum fio_filetype {
14 FIO_TYPE_FILE = 1, /* plain file */
15 FIO_TYPE_BD, /* block device */
16 FIO_TYPE_CHAR, /* character device */
17 FIO_TYPE_PIPE, /* pipe */
18};
19
20enum fio_file_flags {
21 FIO_FILE_open = 1 << 0, /* file is open */
22 FIO_FILE_closing = 1 << 1, /* file being closed */
23 FIO_FILE_extend = 1 << 2, /* needs extend */
24 FIO_FILE_done = 1 << 3, /* io completed to this file */
25 FIO_FILE_size_known = 1 << 4, /* size has been set */
26 FIO_FILE_hashed = 1 << 5, /* file is on hash */
Jens Axboeed47cbf2009-07-03 22:52:38 +020027 FIO_FILE_partial_mmap = 1 << 6, /* can't do full mmap */
Jens Axboed6aed792009-06-03 08:41:15 +020028};
29
30enum file_lock_mode {
31 FILE_LOCK_NONE,
32 FILE_LOCK_EXCLUSIVE,
33 FILE_LOCK_READWRITE,
34};
35
36/*
Jens Axboe590aebd2009-06-03 08:54:00 +020037 * roundrobin available files, or choose one at random, or do each one
38 * serially.
39 */
40enum {
41 FIO_FSERVICE_RANDOM = 1,
42 FIO_FSERVICE_RR = 2,
43 FIO_FSERVICE_SEQ = 3,
44};
45
46/*
Eric Gourioua596f042011-06-17 09:11:45 +020047 * No pre-allocation when laying down files, or call posix_fallocate(), or
48 * call fallocate() with FALLOC_FL_KEEP_SIZE set.
49 */
50enum fio_fallocate_mode {
51 FIO_FALLOCATE_NONE = 1,
52 FIO_FALLOCATE_POSIX = 2,
53 FIO_FALLOCATE_KEEP_SIZE = 3,
54};
55
56/*
Jens Axboed6aed792009-06-03 08:41:15 +020057 * Each thread_data structure has a number of files associated with it,
58 * this structure holds state information for a single file.
59 */
60struct fio_file {
61 struct flist_head hash_list;
62 enum fio_filetype filetype;
63
Jens Axboe9e700a72010-10-08 15:06:33 +020064 void *file_data;
Jens Axboe0e238572010-10-08 11:26:43 +020065 int fd;
Bruce Cran93bcfd22012-02-20 20:18:19 +010066#ifdef WIN32
Bruce Cran03e20d62011-01-02 20:14:54 +010067 HANDLE hFile;
68 HANDLE ioCP;
69#endif
Jens Axboed6aed792009-06-03 08:41:15 +020070
71 /*
72 * filename and possible memory mapping
73 */
74 char *file_name;
75 unsigned int major, minor;
Shaohua Li89ac1d42012-06-11 08:56:32 +020076 int fileno;
Jens Axboed6aed792009-06-03 08:41:15 +020077
78 void *mmap_ptr;
79 size_t mmap_sz;
80 off_t mmap_off;
81
82 /*
83 * size of the file, offset into file, and io size from that offset
84 */
85 unsigned long long real_file_size;
86 unsigned long long file_offset;
87 unsigned long long io_size;
88
89 unsigned long long last_pos;
Jens Axboe38dad622010-07-20 14:46:00 -060090 unsigned long long last_start;
Jens Axboed6aed792009-06-03 08:41:15 +020091
Jens Axboe44f29692010-03-09 20:09:44 +010092 unsigned long long first_write;
93 unsigned long long last_write;
94
Jens Axboed6aed792009-06-03 08:41:15 +020095 /*
Jens Axboee943b872010-02-02 09:48:13 +010096 * For use by the io engine
97 */
98 unsigned long long file_pos;
99
100 /*
Jens Axboed6aed792009-06-03 08:41:15 +0200101 * if io is protected by a semaphore, this is set
102 */
103 struct fio_mutex *lock;
104 void *lock_owner;
105 unsigned int lock_batch;
106 enum fio_ddir lock_ddir;
107
108 /*
109 * block map for random io
110 */
Jens Axboe0ce8b112011-01-27 22:25:29 +0100111 unsigned long *file_map;
Jens Axboe0e280ee2012-02-01 20:05:34 +0100112 unsigned long num_maps;
113 unsigned long last_free_lookup;
114 unsigned failed_rands;
Jens Axboed6aed792009-06-03 08:41:15 +0200115
Jens Axboe9c6f6312012-11-07 09:15:45 +0100116 /*
117 * Used for zipf random distribution
118 */
119 struct zipf_state zipf;
120
Jens Axboed6aed792009-06-03 08:41:15 +0200121 int references;
122 enum fio_file_flags flags;
123
124 struct disk_util *du;
125};
126
127#define FILE_FLAG_FNS(name) \
128static inline void fio_file_set_##name(struct fio_file *f) \
129{ \
130 (f)->flags |= FIO_FILE_##name; \
131} \
132static inline void fio_file_clear_##name(struct fio_file *f) \
133{ \
134 (f)->flags &= ~FIO_FILE_##name; \
135} \
136static inline int fio_file_##name(struct fio_file *f) \
137{ \
138 return ((f)->flags & FIO_FILE_##name) != 0; \
139}
140
141FILE_FLAG_FNS(open);
142FILE_FLAG_FNS(closing);
143FILE_FLAG_FNS(extend);
144FILE_FLAG_FNS(done);
145FILE_FLAG_FNS(size_known);
146FILE_FLAG_FNS(hashed);
Jens Axboeed47cbf2009-07-03 22:52:38 +0200147FILE_FLAG_FNS(partial_mmap);
Jens Axboed6aed792009-06-03 08:41:15 +0200148#undef FILE_FLAG_FNS
149
Jens Axboe4cd02b32009-06-03 08:46:38 +0200150/*
151 * File setup/shutdown
152 */
153struct thread_data;
154extern void close_files(struct thread_data *);
155extern void close_and_free_files(struct thread_data *);
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200156extern unsigned long long get_start_offset(struct thread_data *);
Jens Axboe4cd02b32009-06-03 08:46:38 +0200157extern int __must_check setup_files(struct thread_data *);
158extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
159extern int __must_check generic_open_file(struct thread_data *, struct fio_file *);
160extern int __must_check generic_close_file(struct thread_data *, struct fio_file *);
161extern int __must_check generic_get_file_size(struct thread_data *, struct fio_file *);
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400162extern int __must_check file_lookup_open(struct fio_file *f, int flags);
Jens Axboe4cd02b32009-06-03 08:46:38 +0200163extern int __must_check pre_read_files(struct thread_data *);
164extern int add_file(struct thread_data *, const char *);
Jens Axboe49ffb4a2010-08-24 15:01:37 +0200165extern int add_file_exclusive(struct thread_data *, const char *);
Jens Axboe4cd02b32009-06-03 08:46:38 +0200166extern void get_file(struct fio_file *);
167extern int __must_check put_file(struct thread_data *, struct fio_file *);
Jens Axboee8462bd2009-07-06 12:59:04 +0200168extern void put_file_log(struct thread_data *, struct fio_file *);
Jens Axboe4cd02b32009-06-03 08:46:38 +0200169extern void lock_file(struct thread_data *, struct fio_file *, enum fio_ddir);
170extern void unlock_file(struct thread_data *, struct fio_file *);
171extern void unlock_file_all(struct thread_data *, struct fio_file *);
172extern int add_dir_files(struct thread_data *, const char *);
173extern int init_random_map(struct thread_data *);
174extern void dup_files(struct thread_data *, struct thread_data *);
175extern int get_fileno(struct thread_data *, const char *);
176extern void free_release_files(struct thread_data *);
177
Jens Axboec592b9f2009-06-03 09:29:28 +0200178static inline void fio_file_reset(struct fio_file *f)
179{
180 f->last_free_lookup = 0;
Jens Axboe0ce8b112011-01-27 22:25:29 +0100181 f->failed_rands = 0;
Jens Axboec592b9f2009-06-03 09:29:28 +0200182 f->last_pos = f->file_offset;
Jens Axboe38dad622010-07-20 14:46:00 -0600183 f->last_start = -1ULL;
Jens Axboee943b872010-02-02 09:48:13 +0100184 f->file_pos = -1ULL;
Jens Axboec592b9f2009-06-03 09:29:28 +0200185 if (f->file_map)
Jens Axboe0ce8b112011-01-27 22:25:29 +0100186 memset(f->file_map, 0, f->num_maps * sizeof(unsigned long));
Jens Axboec592b9f2009-06-03 09:29:28 +0200187}
188
Jens Axboed6aed792009-06-03 08:41:15 +0200189#endif