Merge branch 'genfio-bash' of https://github.com/sitsofe/fio
[fio.git] / engines / filestat.c
CommitLineData
73ccd14e
SF
1/*
2 * filestat engine
3 *
4 * IO engine that doesn't do any IO, just stat files and tracks the latency
5 * of the file stat.
6 */
7#include <stdio.h>
8#include <fcntl.h>
9#include <errno.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <unistd.h>
13#include "../fio.h"
c446eff0 14#include "../optgroup.h"
73ccd14e
SF
15
16struct fc_data {
17 enum fio_ddir stat_ddir;
18};
19
c446eff0
TK
20struct filestat_options {
21 void *pad;
93a13ba5
TK
22 unsigned int stat_type;
23};
24
25enum {
26 FIO_FILESTAT_STAT = 1,
27 FIO_FILESTAT_LSTAT = 2,
28 /*FIO_FILESTAT_STATX = 3,*/
c446eff0
TK
29};
30
31static struct fio_option options[] = {
32 {
93a13ba5
TK
33 .name = "stat_type",
34 .lname = "stat_type",
35 .type = FIO_OPT_STR,
36 .off1 = offsetof(struct filestat_options, stat_type),
37 .help = "Specify stat system call type to measure lookup/getattr performance",
38 .def = "stat",
39 .posval = {
40 { .ival = "stat",
41 .oval = FIO_FILESTAT_STAT,
42 .help = "Use stat(2)",
43 },
44 { .ival = "lstat",
45 .oval = FIO_FILESTAT_LSTAT,
46 .help = "Use lstat(2)",
47 },
48 /*
49 { .ival = "statx",
50 .oval = FIO_FILESTAT_STATX,
51 .help = "Use statx(2)",
52 },
53 */
54 },
c446eff0
TK
55 .category = FIO_OPT_C_ENGINE,
56 .group = FIO_OPT_G_FILESTAT,
57 },
58 {
59 .name = NULL,
60 },
61};
62
73ccd14e
SF
63static int stat_file(struct thread_data *td, struct fio_file *f)
64{
c446eff0 65 struct filestat_options *o = td->eo;
73ccd14e
SF
66 struct timespec start;
67 int do_lat = !td->o.disable_lat;
68 struct stat statbuf;
69 int ret;
70
71 dprint(FD_FILE, "fd stat %s\n", f->file_name);
72
73 if (f->filetype != FIO_TYPE_FILE) {
74 log_err("fio: only files are supported\n");
75 return 1;
76 }
77 if (!strcmp(f->file_name, "-")) {
78 log_err("fio: can't read/write to stdin/out\n");
79 return 1;
80 }
81
82 if (do_lat)
83 fio_gettime(&start, NULL);
84
93a13ba5
TK
85 switch (o->stat_type){
86 case FIO_FILESTAT_STAT:
c446eff0 87 ret = stat(f->file_name, &statbuf);
93a13ba5
TK
88 break;
89 case FIO_FILESTAT_LSTAT:
90 ret = lstat(f->file_name, &statbuf);
91 break;
92 default:
93 ret = -1;
94 break;
95 }
73ccd14e
SF
96
97 if (ret == -1) {
98 char buf[FIO_VERROR_SIZE];
99 int e = errno;
100
93a13ba5
TK
101 snprintf(buf, sizeof(buf), "stat(%s) type=%u", f->file_name,
102 o->stat_type);
73ccd14e
SF
103 td_verror(td, e, buf);
104 return 1;
105 }
106
107 if (do_lat) {
108 struct fc_data *data = td->io_ops_data;
109 uint64_t nsec;
110
111 nsec = ntime_since_now(&start);
b2a432bf 112 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0);
73ccd14e
SF
113 }
114
115 return 0;
116}
117
118static enum fio_q_status queue_io(struct thread_data *td, struct io_u fio_unused *io_u)
119{
120 return FIO_Q_COMPLETED;
121}
122
123static int init(struct thread_data *td)
124{
125 struct fc_data *data;
126
127 data = calloc(1, sizeof(*data));
128
129 if (td_read(td))
130 data->stat_ddir = DDIR_READ;
131 else if (td_write(td))
132 data->stat_ddir = DDIR_WRITE;
133
134 td->io_ops_data = data;
135 return 0;
136}
137
138static void cleanup(struct thread_data *td)
139{
140 struct fc_data *data = td->io_ops_data;
141
142 free(data);
143}
144
145static int stat_invalidate(struct thread_data *td, struct fio_file *f)
146{
147 /* do nothing because file not opened */
148 return 0;
149}
150
151static struct ioengine_ops ioengine = {
152 .name = "filestat",
153 .version = FIO_IOOPS_VERSION,
154 .init = init,
155 .cleanup = cleanup,
156 .queue = queue_io,
157 .invalidate = stat_invalidate,
158 .get_file_size = generic_get_file_size,
159 .open_file = stat_file,
160 .flags = FIO_SYNCIO | FIO_FAKEIO |
161 FIO_NOSTATS | FIO_NOFILEHASH,
c446eff0
TK
162 .options = options,
163 .option_struct_size = sizeof(struct filestat_options),
73ccd14e
SF
164};
165
166static void fio_init fio_filestat_register(void)
167{
168 register_ioengine(&ioengine);
169}
170
171static void fio_exit fio_filestat_unregister(void)
172{
173 unregister_ioengine(&ioengine);
174}