2 * fileoperations engine
4 * IO engine that doesn't do any IO, just operates files and tracks the latency
5 * of the file operation.
11 #include <sys/types.h>
15 #include "../optgroup.h"
16 #include "../oslib/statx.h"
20 enum fio_ddir stat_ddir;
23 struct filestat_options {
25 unsigned int stat_type;
29 FIO_FILESTAT_STAT = 1,
30 FIO_FILESTAT_LSTAT = 2,
31 FIO_FILESTAT_STATX = 3,
34 static struct fio_option options[] = {
39 .off1 = offsetof(struct filestat_options, stat_type),
40 .help = "Specify stat system call type to measure lookup/getattr performance",
44 .oval = FIO_FILESTAT_STAT,
45 .help = "Use stat(2)",
48 .oval = FIO_FILESTAT_LSTAT,
49 .help = "Use lstat(2)",
52 .oval = FIO_FILESTAT_STATX,
53 .help = "Use statx(2) if exists",
56 .category = FIO_OPT_C_ENGINE,
57 .group = FIO_OPT_G_FILESTAT,
65 static int open_file(struct thread_data *td, struct fio_file *f)
67 struct timespec start;
68 int do_lat = !td->o.disable_lat;
70 dprint(FD_FILE, "fd open %s\n", f->file_name);
72 if (f->filetype != FIO_TYPE_FILE) {
73 log_err("fio: only files are supported\n");
76 if (!strcmp(f->file_name, "-")) {
77 log_err("fio: can't read/write to stdin/out\n");
82 fio_gettime(&start, NULL);
84 f->fd = open(f->file_name, O_CREAT|O_RDWR, 0600);
87 char buf[FIO_VERROR_SIZE];
90 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
91 td_verror(td, e, buf);
96 struct fc_data *data = td->io_ops_data;
99 nsec = ntime_since_now(&start);
100 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0, 0);
106 static int stat_file(struct thread_data *td, struct fio_file *f)
108 struct filestat_options *o = td->eo;
109 struct timespec start;
110 int do_lat = !td->o.disable_lat;
113 struct statx statxbuf;
118 dprint(FD_FILE, "fd stat %s\n", f->file_name);
120 if (f->filetype != FIO_TYPE_FILE) {
121 log_err("fio: only files are supported\n");
124 if (!strcmp(f->file_name, "-")) {
125 log_err("fio: can't read/write to stdin/out\n");
130 fio_gettime(&start, NULL);
132 switch (o->stat_type) {
133 case FIO_FILESTAT_STAT:
134 ret = stat(f->file_name, &statbuf);
136 case FIO_FILESTAT_LSTAT:
137 ret = lstat(f->file_name, &statbuf);
139 case FIO_FILESTAT_STATX:
141 abspath = realpath(f->file_name, NULL);
143 ret = statx(-1, abspath, 0, STATX_ALL, &statxbuf);
157 char buf[FIO_VERROR_SIZE];
160 snprintf(buf, sizeof(buf), "stat(%s) type=%u", f->file_name,
162 td_verror(td, e, buf);
167 struct fc_data *data = td->io_ops_data;
170 nsec = ntime_since_now(&start);
171 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0, 0);
178 static int delete_file(struct thread_data *td, struct fio_file *f)
180 struct timespec start;
181 int do_lat = !td->o.disable_lat;
184 dprint(FD_FILE, "fd delete %s\n", f->file_name);
186 if (f->filetype != FIO_TYPE_FILE) {
187 log_err("fio: only files are supported\n");
190 if (!strcmp(f->file_name, "-")) {
191 log_err("fio: can't read/write to stdin/out\n");
196 fio_gettime(&start, NULL);
198 ret = unlink(f->file_name);
201 char buf[FIO_VERROR_SIZE];
204 snprintf(buf, sizeof(buf), "delete(%s)", f->file_name);
205 td_verror(td, e, buf);
210 struct fc_data *data = td->io_ops_data;
213 nsec = ntime_since_now(&start);
214 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0, 0);
220 static int invalidate_do_nothing(struct thread_data *td, struct fio_file *f)
222 /* do nothing because file not opened */
226 static enum fio_q_status queue_io(struct thread_data *td, struct io_u *io_u)
228 return FIO_Q_COMPLETED;
232 * Ensure that we at least have a block size worth of IO to do for each
233 * file. If the job file has td->o.size < nr_files * block_size, then
234 * fio won't do anything.
236 static int get_file_size(struct thread_data *td, struct fio_file *f)
238 f->real_file_size = td_min_bs(td);
242 static int init(struct thread_data *td)
244 struct fc_data *data;
246 data = calloc(1, sizeof(*data));
249 data->stat_ddir = DDIR_READ;
250 else if (td_write(td))
251 data->stat_ddir = DDIR_WRITE;
253 td->io_ops_data = data;
257 static void cleanup(struct thread_data *td)
259 struct fc_data *data = td->io_ops_data;
264 static struct ioengine_ops ioengine_filecreate = {
265 .name = "filecreate",
266 .version = FIO_IOOPS_VERSION,
270 .get_file_size = get_file_size,
271 .open_file = open_file,
272 .close_file = generic_close_file,
273 .flags = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO |
274 FIO_NOSTATS | FIO_NOFILEHASH,
277 static struct ioengine_ops ioengine_filestat = {
279 .version = FIO_IOOPS_VERSION,
283 .invalidate = invalidate_do_nothing,
284 .get_file_size = generic_get_file_size,
285 .open_file = stat_file,
286 .flags = FIO_SYNCIO | FIO_FAKEIO |
287 FIO_NOSTATS | FIO_NOFILEHASH,
289 .option_struct_size = sizeof(struct filestat_options),
292 static struct ioengine_ops ioengine_filedelete = {
293 .name = "filedelete",
294 .version = FIO_IOOPS_VERSION,
296 .invalidate = invalidate_do_nothing,
299 .get_file_size = generic_get_file_size,
300 .open_file = delete_file,
301 .flags = FIO_SYNCIO | FIO_FAKEIO |
302 FIO_NOSTATS | FIO_NOFILEHASH,
306 static void fio_init fio_fileoperations_register(void)
308 register_ioengine(&ioengine_filecreate);
309 register_ioengine(&ioengine_filestat);
310 register_ioengine(&ioengine_filedelete);
313 static void fio_exit fio_fileoperations_unregister(void)
315 unregister_ioengine(&ioengine_filecreate);
316 unregister_ioengine(&ioengine_filestat);
317 unregister_ioengine(&ioengine_filedelete);