-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfio_fileserver.c
411 lines (372 loc) · 11 KB
/
fio_fileserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// FIO engine to test "file server" access pattern
// I.e. read/write over a number of files sharded over several levels of subdirectories
//
// USAGE: fio -name=test -ioengine=./libfio_fileserver.so -chunk_size=256K \
// -directory=/home/bench -size=10G [-direct=1] [-fsync_on_close=1] [-sync=1] [-dir_levels=2] [-subdirs_per_dir=64]
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#define CONFIG_HAVE_GETTID
#define CONFIG_SYNC_FILE_RANGE
#define CONFIG_PWRITEV2
#include "fio/fio.h"
#include "fio/optgroup.h"
struct sec_options;
struct sec_data
{
int __pad[2];
struct thread_data *td;
struct sec_options *opt;
pthread_mutex_t mutex;
pthread_cond_t cond, cond_done;
int finished;
int in_flight;
pthread_t *threads;
int thread_count, thread_alloc;
struct io_u **requests;
int request_count, request_alloc;
struct io_u **done;
int done_count, done_alloc;
};
struct sec_options
{
int __pad;
int __pad2;
int dir_levels;
int subdirs_per_dir;
int chunk_size;
};
static struct fio_option options[] = {
{
.name = "dir_levels",
.lname = "dir levels",
.type = FIO_OPT_INT,
.off1 = offsetof(struct sec_options, dir_levels),
.help = "levels of nested directories (2 by default)",
.def = "2",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_FILENAME,
},
{
.name = "subdirs_per_dir",
.lname = "subdirectories per directory",
.type = FIO_OPT_INT,
.off1 = offsetof(struct sec_options, subdirs_per_dir),
.help = "subdirectories per directory (64 by default)",
.def = "64",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_FILENAME,
},
{
.name = "chunk_size",
.lname = "size of each fileserver chunk (file)",
.type = FIO_OPT_INT,
.off1 = offsetof(struct sec_options, chunk_size),
.help = "all I/O will be divided between files of this size (256K by default)",
.def = "262144",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_FILENAME,
},
{
.name = NULL,
},
};
static int sec_init(struct thread_data *td)
{
struct sec_data *bsd;
struct sec_options *opt = (struct sec_options*)td->eo;
if (!td->o.directory || opt->chunk_size <= 0 || opt->dir_levels > 0 && opt->subdirs_per_dir <= 0)
{
fprintf(stderr, "USAGE: fio -name=test -ioengine=./libfio_fileserver.so"
" -chunk_size=256K -directory=/home/bench -size=10G [-direct=1] [-fsync_on_close=1] [-sync=1] [-dir_levels=2] [-subdirs_per_dir=64]\n");
exit(1);
}
bsd = calloc(1, sizeof(struct sec_data));
if (!bsd)
{
td_verror(td, errno, "calloc");
return 1;
}
td->io_ops_data = bsd;
bsd->td = td;
bsd->opt = opt;
if (!td->files_index)
{
add_file(td, "fileserver", 0, 0);
td->o.nr_files = td->o.nr_files ? : 1;
td->o.open_files++;
}
if (pthread_mutex_init(&bsd->mutex, NULL) != 0)
{
td_verror(td, errno, "pthread_mutex_init");
return 1;
}
if (pthread_cond_init(&bsd->cond, NULL) != 0)
{
td_verror(td, errno, "pthread_cond_init");
return 1;
}
if (pthread_cond_init(&bsd->cond_done, NULL) != 0)
{
td_verror(td, errno, "pthread_cond_init");
return 1;
}
return 0;
}
static void sec_cleanup(struct thread_data *td)
{
struct sec_data *bsd = (struct sec_data*)td->io_ops_data;
if (bsd)
{
int i;
void *retval = NULL;
pthread_mutex_lock(&bsd->mutex);
bsd->finished = 1;
pthread_cond_broadcast(&bsd->cond);
pthread_cond_broadcast(&bsd->cond_done);
pthread_mutex_unlock(&bsd->mutex);
for (i = 0; i < bsd->thread_count; i++)
{
if (pthread_join(bsd->threads[i], &retval) != 0)
{
td_verror(td, errno, "pthread_join");
exit(1);
}
}
pthread_cond_destroy(&bsd->cond);
pthread_cond_destroy(&bsd->cond_done);
pthread_mutex_destroy(&bsd->mutex);
free(bsd);
}
}
/* Begin read or write request. */
static void sec_exec(struct sec_data *bsd, struct io_u *io)
{
struct thread_data *td = bsd->td;
struct sec_options *opt = bsd->opt;
int i, r, pos;
int dirname_len, filename_buf;
char *file_name;
struct stat statbuf;
uint64_t file_idx;
io->engine_data = bsd;
file_idx = io->offset / opt->chunk_size;
dirname_len = strlen(td->o.directory);
filename_buf = dirname_len + opt->subdirs_per_dir * 3 + 256;
file_name = malloc(filename_buf);
if (!file_name)
exit(1);
strncpy(file_name, td->o.directory, filename_buf);
pos = dirname_len;
for (i = 0; i < opt->dir_levels; i++)
{
int subdir = file_idx % opt->subdirs_per_dir;
file_idx /= opt->subdirs_per_dir;
pos += snprintf(file_name + pos, filename_buf - pos - 1, "/%02x", subdir);
r = stat(file_name, &statbuf);
if (r < 0)
{
if (errno == ENOENT)
{
if (io->ddir == DDIR_WRITE)
{
r = mkdir(file_name, 0777);
if (r < 0 && errno != EEXIST)
{
fprintf(stderr, "Error mkdir(%s): %d (%s)\n", file_name, errno, strerror(errno));
exit(1);
}
}
}
else
{
fprintf(stderr, "Error stat(%s): %d (%s)\n", file_name, errno, strerror(errno));
exit(1);
}
}
}
pos += snprintf(file_name + pos, filename_buf - pos - 1, "/%02lx", file_idx);
file_name[pos] = 0;
int fd = open(file_name,
(io->ddir == DDIR_WRITE ? (O_CREAT|O_RDWR) : O_RDONLY)
| (td->o.sync_io ? O_SYNC : 0)
| (td->o.odirect ? O_DIRECT : 0),
0644
);
if (fd < 0 && errno != ENOENT)
{
fprintf(stderr, "Error open(%s): %d (%s)\n", file_name, errno, strerror(errno));
exit(1);
}
if (io->ddir == DDIR_READ)
{
if (fd >= 0)
{
io->error = pread(fd, io->xfer_buf, io->xfer_buflen, io->offset % opt->chunk_size) < 0 ? errno : 0;
}
else
{
// Read from a non-existing file
io->error = ENOENT;
}
}
else if (io->ddir == DDIR_WRITE)
{
io->error = pwrite(fd, io->xfer_buf, io->xfer_buflen, io->offset % opt->chunk_size) < 0 ? errno : 0;
if (io->error >= 0 && td->o.fsync_on_close)
{
io->error = fsync(fd) < 0 ? errno : 0;
}
}
else if (io->ddir == DDIR_SYNC)
{
io->error = fsync(fd) < 0 ? errno : 0;
}
else
{
io->error = EINVAL;
}
close(fd);
free(file_name);
}
static void* sec_thread(void *opaque)
{
struct sec_data *bsd = (struct sec_data*)opaque;
struct io_u* req = NULL;
while (1)
{
// Get request
pthread_mutex_lock(&bsd->mutex);
while (bsd->request_count <= 0 && !bsd->finished)
{
pthread_cond_wait(&bsd->cond, &bsd->mutex);
}
if (bsd->finished)
{
pthread_mutex_unlock(&bsd->mutex);
break;
}
req = bsd->requests[--bsd->request_count];
pthread_mutex_unlock(&bsd->mutex);
// Execute
sec_exec(bsd, req);
// Put response
pthread_mutex_lock(&bsd->mutex);
if (bsd->done_count >= bsd->done_alloc)
{
bsd->done_alloc += 16;
bsd->done = realloc(bsd->done, bsd->done_alloc*sizeof(struct io_u*));
}
bsd->done[bsd->done_count++] = req;
if (bsd->done_count == 1)
{
pthread_cond_signal(&bsd->cond_done);
}
pthread_mutex_unlock(&bsd->mutex);
}
return NULL;
}
/* Begin read or write request. */
static enum fio_q_status sec_queue(struct thread_data *td, struct io_u *io)
{
struct sec_data *bsd = (struct sec_data*)td->io_ops_data;
fio_ro_check(td, io);
pthread_mutex_lock(&bsd->mutex);
if (bsd->request_count >= bsd->request_alloc)
{
bsd->request_alloc += 16;
bsd->requests = realloc(bsd->requests, bsd->request_alloc*sizeof(struct io_u*));
}
bsd->requests[bsd->request_count++] = io;
if (bsd->request_count == 1)
{
pthread_cond_signal(&bsd->cond);
}
pthread_mutex_unlock(&bsd->mutex);
bsd->in_flight++;
while (bsd->in_flight > bsd->thread_count)
{
if (bsd->thread_count >= bsd->thread_alloc)
{
bsd->thread_alloc += 16;
bsd->threads = realloc(bsd->threads, bsd->thread_alloc*sizeof(pthread_t));
}
if (pthread_create(&bsd->threads[bsd->thread_count++], NULL, sec_thread, bsd) != 0)
{
td_verror(td, errno, "pthread_create");
exit(1);
}
}
return FIO_Q_QUEUED;
}
static int sec_getevents(struct thread_data *td, unsigned int min, unsigned int max, const struct timespec *t)
{
struct sec_data *bsd = (struct sec_data*)td->io_ops_data;
int done_count = 0;
pthread_mutex_lock(&bsd->mutex);
while (bsd->done_count <= 0)
{
pthread_cond_wait(&bsd->cond_done, &bsd->mutex);
}
done_count = bsd->done_count;
pthread_mutex_unlock(&bsd->mutex);
return done_count;
}
static struct io_u *sec_event(struct thread_data *td, int event)
{
struct sec_data *bsd = (struct sec_data*)td->io_ops_data;
struct io_u *req = NULL;
pthread_mutex_lock(&bsd->mutex);
if (bsd->done_count > 0)
{
req = bsd->done[--bsd->done_count];
}
bsd->in_flight--;
pthread_mutex_unlock(&bsd->mutex);
return req;
}
static int sec_io_u_init(struct thread_data *td, struct io_u *io)
{
io->engine_data = NULL;
return 0;
}
static void sec_io_u_free(struct thread_data *td, struct io_u *io)
{
}
static int sec_open_file(struct thread_data *td, struct fio_file *f)
{
return 0;
}
static int sec_invalidate(struct thread_data *td, struct fio_file *f)
{
return 0;
}
struct ioengine_ops ioengine = {
.name = "fileserver",
.version = FIO_IOOPS_VERSION,
.flags = FIO_MEMALIGN | FIO_DISKLESSIO | FIO_NOEXTEND,
.init = sec_init,
.queue = sec_queue,
.getevents = sec_getevents,
.event = sec_event,
.cleanup = sec_cleanup,
.open_file = sec_open_file,
.invalidate = sec_invalidate,
.io_u_init = sec_io_u_init,
.io_u_free = sec_io_u_free,
.option_struct_size = sizeof(struct sec_options),
.options = options,
};
static void fio_init fio_sec_register(void)
{
register_ioengine(&ioengine);
}
static void fio_exit fio_sec_unregister(void)
{
unregister_ioengine(&ioengine);
}