-
Notifications
You must be signed in to change notification settings - Fork 0
/
dio.c
433 lines (390 loc) · 11.5 KB
/
dio.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* Copyright 2021 IBM Corporation
*
* Author: Constantine Gavrilov
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include "logger.h"
DECLARE_BFN
#include "compiler.h"
#include "iobench.h"
#include "core_affinity.h"
#include <pthread.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/nvme_ioctl.h>
#include <limits.h>
static void dio_destroy_thread_ctx(io_bench_thr_ctx_t *ctx);
static int dio_init_thread_ctx(io_bench_thr_ctx_t **pctx, io_bench_params_t *params, void *buf_head, unsigned int dev_idx, unsigned int cpu);
static int dio_poll_completions(io_bench_thr_ctx_t *ctx, int n);
static io_ctx_t *dio_get_io_ctx(io_bench_thr_ctx_t *ctx, uint16_t slot);
static int dio_queue_io(io_bench_thr_ctx_t *ctx, io_ctx_t *io);
io_eng_def_t dio_engine = {
.init_thread_ctx = &dio_init_thread_ctx,
.stop_thread_ctx = &dio_destroy_thread_ctx,
.poll_completions = &dio_poll_completions,
.get_io_ctx = &dio_get_io_ctx,
.queue_io = &dio_queue_io,
.seed_per_io = true,
};
io_eng_def_t nvme_engine = {
.init_thread_ctx = &dio_init_thread_ctx,
.stop_thread_ctx = &dio_destroy_thread_ctx,
.poll_completions = &dio_poll_completions,
.get_io_ctx = &dio_get_io_ctx,
.queue_io = &dio_queue_io,
.seed_per_io = true,
};
typedef struct {
int fd;
io_ctx_t ioctx;
io_bench_thr_ctx_t *parent;
pthread_t tid;
pthread_cond_t run_cond;
pthread_mutex_t run_mutex;
int cpu;
bool may_run;
} dio_ioctx_t;
typedef struct {
dio_ioctx_t *ioctx;
uint64_t target_usec_lat;
pthread_t self;
int *fds;
int lba_size_bits;
uint32_t bs;
uint32_t qs;
uint32_t ndevs;
bool rr;
io_bench_thr_ctx_t iobench_ctx;
} dio_thr_ctx_t;
#define U_2_P(_u_h_) \
({ \
force_type((_u_h_), io_bench_thr_ctx_t *); \
dio_thr_ctx_t *__res__ = list_parent_struct(_u_h_, dio_thr_ctx_t, iobench_ctx); \
__res__; \
})
#define UIO_2_PIO(_u_h_) \
({ \
force_type((_u_h_), io_ctx_t*); \
dio_ioctx_t *__res__ = list_parent_struct(_u_h_, dio_ioctx_t, ioctx); \
__res__; \
})
static void kill_all_dio_threads(io_bench_thr_ctx_t *ctx)
{
dio_thr_ctx_t *pctx = U_2_P(ctx);
unsigned int i;
if (!pctx->ioctx)
return;
for(i = 0; i < pctx->qs; i++) {
if (pctx->ioctx[i].tid) {
pthread_kill(pctx->ioctx[i].tid, SIGUSR1);
}
}
for(i = 0; i < pctx->qs; i++) {
if (pctx->ioctx[i].tid) {
pthread_join(pctx->ioctx[i].tid, NULL);
}
}
for (i = 0; i < pctx->qs; i++) {
if (pctx->ioctx[i].fd >= 0 && !pctx->rr)
close(pctx->ioctx[i].fd);
}
}
static void dio_destroy_thread_ctx(io_bench_thr_ctx_t *ctx)
{
dio_thr_ctx_t *pctx = U_2_P(ctx);
kill_all_dio_threads(ctx);
if (pctx->ioctx)
free(pctx->ioctx);
if (pctx->rr && pctx->fds) {
uint32_t i;
for (i = 0; i < pctx->ndevs; i++) {
if (pctx->fds[i] >= 0)
close(pctx->fds[i]);
}
}
free(pctx->fds);
pctx->fds = NULL;
}
static void term_handler(int signo)
{
pthread_exit(NULL);
}
#define SET_ERR(__rc__) ((__rc__ < 0) ? (errno ? -errno : -1) : -ENODATA)
static void *thread_func_dio(void *arg)
{
dio_ioctx_t *ctx = arg;
io_ctx_t *ioctx = &ctx->ioctx;
io_bench_thr_ctx_t *thr_ctx = ctx->parent;
dio_thr_ctx_t *pctx = U_2_P(thr_ctx);
uint32_t bs = U_2_P(thr_ctx)->bs;
int rc;
if (ctx->cpu != -1)
set_thread_affinity(ctx->cpu);
pthread_mutex_lock(&ctx->run_mutex);
while (!ctx->may_run)
pthread_cond_wait(&ctx->run_cond, &ctx->run_mutex);
pthread_mutex_unlock(&ctx->run_mutex);
ioctx->slack = 0;
ioctx->start_stamp = get_uptime_us();
while (1) {
int fd = (pctx->rr) ? pctx->fds[ioctx->dev_idx] : ctx->fd;
uint64_t stamp = ioctx->start_stamp;
rc = (!ioctx->write) ? pread(fd, ioctx->buf, bs, ioctx->offset) : pwrite(fd, ioctx->buf, bs, ioctx->offset);
ioctx->status = (rc == bs) ? 0 : SET_ERR(rc);
io_bench_complete_and_prep_io(thr_ctx, ioctx);
if (pctx->target_usec_lat) {
uint64_t now = get_uptime_us();
uint64_t delta = now - stamp;
if (delta < pctx->target_usec_lat) {
delta = pctx->target_usec_lat - delta;
if (delta > ioctx->slack) {
delta -= ioctx->slack;
ioctx->slack = 0;
stamp = now + delta;
usleep(delta);
now = get_uptime_us();
ioctx->slack = (now > stamp) ? now - stamp : 0;
ioctx->start_stamp = now;
} else {
ioctx->slack -= delta;
}
} else {
ioctx->slack += (delta - pctx->target_usec_lat);
}
}
}
return NULL;
}
#define NVME_CMD_READ (2)
#define NVME_CMD_WRITE (1)
static void *thread_func_nvme(void *arg)
{
dio_ioctx_t *ctx = arg;
io_ctx_t *ioctx = &ctx->ioctx;
io_bench_thr_ctx_t *thr_ctx = ctx->parent;
dio_thr_ctx_t *pctx = U_2_P(thr_ctx);
uint32_t bs =(U_2_P(thr_ctx)->bs >> U_2_P(thr_ctx)->lba_size_bits) - 1;
int rc;
if (ctx->cpu != -1)
set_thread_affinity(ctx->cpu);
pthread_mutex_lock(&ctx->run_mutex);
while (!ctx->may_run)
pthread_cond_wait(&ctx->run_cond, &ctx->run_mutex);
pthread_mutex_unlock(&ctx->run_mutex);
ioctx->start_stamp = get_uptime_us();
ioctx->slack = 0;
while (1) {
int fd = (pctx->rr) ? pctx->fds[ioctx->dev_idx] : ctx->fd;
uint64_t stamp = ioctx->start_stamp;
struct nvme_user_io nvme_io = {
.opcode = !ioctx->write ? NVME_CMD_READ : NVME_CMD_WRITE,
.nblocks = bs,
.addr = (uint64_t)ioctx->buf,
.slba = ioctx->offset >> U_2_P(thr_ctx)->lba_size_bits,
};
rc = ioctl(fd, NVME_IOCTL_SUBMIT_IO, &nvme_io);
ioctx->status = (rc == 0) ? 0 : SET_ERR(rc);
io_bench_complete_and_prep_io(thr_ctx, ioctx);
if (pctx->target_usec_lat) {
uint64_t now = get_uptime_us();
uint64_t delta = now - stamp;
if (delta < pctx->target_usec_lat) {
delta = pctx->target_usec_lat - delta;
if (delta > ioctx->slack) {
delta -= ioctx->slack;
ioctx->slack = 0;
stamp = now + delta;
usleep(delta);
now = get_uptime_us();
ioctx->slack = (now > stamp) ? now - stamp : 0;
ioctx->start_stamp = now;
} else {
ioctx->slack -= delta;
}
} else {
ioctx->slack += (delta - pctx->target_usec_lat);
}
}
}
return NULL;
}
static int get_lba_size_bits(const char *name)
{
const char *p;
int rc;
int fd;
char path[PATH_MAX];
char buf[16];
int i;
p = strrchr(name, '/');
p = (p) ? p+1 : name;
snprintf(path, sizeof(path), "/sys/block/%s/queue/minimum_io_size", p);
fd = open(path, O_RDONLY);
if (fd < 0) {
rc = errno ? -errno : -1;
ERROR("Failed to open %s", path);
return rc;
}
rc = read(fd, buf, sizeof(buf)-1);
if (rc <= 0) {
rc = rc ? (errno ? -errno : -1) : -EINVAL;
ERROR("Failed to read %s", path);
}
close(fd);
if (rc < 0)
return rc;
buf[rc] = '\0';
if (sscanf(buf, "%d", &rc) != 1) {
ERROR("Failed to parse %s", path);
}
for (i = 1; i < 32; i++) {
if ((1 << i) == rc)
return i;
}
ERROR("Unsupported LBA size of %d", rc);
return -EINVAL;
}
static int dio_init_thread_ctx(io_bench_thr_ctx_t **pctx, io_bench_params_t *params, void *buf_head, unsigned int dev_idx, unsigned int cpu)
{
dio_thr_ctx_t *dio_thr_ctx;
unsigned int i;
unsigned int seed;
struct sigaction act = {{0}};
*pctx = NULL;
dio_thr_ctx = calloc(1, sizeof(*dio_thr_ctx));
if (!dio_thr_ctx) {
ERROR("Failed to alloc");
return -ENOMEM;
}
dio_thr_ctx->target_usec_lat = 1000000.0 / ((params->kiops * 1000)/(params->threads *params->qs));
dio_thr_ctx->self = pthread_self();
dio_thr_ctx->qs = params->qs;
dio_thr_ctx->bs = params->bs;
dio_thr_ctx->rr = params->rr;
dio_thr_ctx->ndevs = params->ndevs;
if (params->rr) {
dio_thr_ctx->fds = calloc(sizeof(int), params->ndevs);
if (dio_thr_ctx->fds) {
memset(dio_thr_ctx->fds, 0xff, sizeof(int) * params->ndevs);
} else {
ERROR("Failed to map device FD array");
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return -ENOMEM;
}
}
dio_thr_ctx->ioctx = calloc(params->qs, sizeof(*dio_thr_ctx->ioctx));
if (!dio_thr_ctx->ioctx) {
ERROR("Failed to map IO buffers");
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return -ENOMEM;
}
seed = get_uptime_us();
for (i = 0; i < dio_thr_ctx->qs; i++) {
dio_thr_ctx->ioctx[i].ioctx.seed = seed++;
dio_thr_ctx->ioctx[i].fd = -1;
dio_thr_ctx->ioctx[i].run_mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
dio_thr_ctx->ioctx[i].run_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
dio_thr_ctx->ioctx[i].parent = &dio_thr_ctx->iobench_ctx;
dio_thr_ctx->ioctx[i].cpu = -1U;
if (params->cpuset) {
dio_thr_ctx->ioctx[i].cpu = get_next_cpu_from_set(params->cpuset);
} else if (params->remap_numa || params->use_numa) {
unsigned int numa = get_numa_id_of_block_device(params->devices[dev_idx]);
if (params->remap_numa && numa != -1U)
dio_thr_ctx->ioctx[i].cpu = get_next_remapped_numa_cpu(params->remap_numa, numa);
else
dio_thr_ctx->ioctx[i].cpu = get_next_numa_rr_cpu(numa);
}
}
if (!params->rr) {
for (i = 0; i < dio_thr_ctx->qs; i++) {
dio_thr_ctx->ioctx[i].fd = open(params->devices[dev_idx], O_RDWR|O_DIRECT);
if (dio_thr_ctx->ioctx[i].fd < 0) {
ERROR("Failed to open %s", params->devices[dev_idx]);
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return -ENOMEM;
}
}
} else {
for (i = 0; i < params->ndevs; i++) {
dio_thr_ctx->fds[i] = open(params->devices[i], O_RDWR|O_DIRECT);
if (dio_thr_ctx->fds[i] < 0) {
ERROR("Failed to open %s", params->devices[i]);
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return -ENOMEM;
}
}
dio_thr_ctx->ioctx[0].fd = dio_thr_ctx->fds[dev_idx];
}
dio_thr_ctx->lba_size_bits = get_lba_size_bits(params->devices[dev_idx]);
if (dio_thr_ctx->lba_size_bits < 0) {
int rc = dio_thr_ctx->lba_size_bits;
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return rc;
}
if ((params->engine != ENGINE_DIO) && (params->bs % (1 << dio_thr_ctx->lba_size_bits))) {
ERROR("Block size must be a multiple of sector size %d", 1 << dio_thr_ctx->lba_size_bits);
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return -EINVAL;
}
act.sa_handler = term_handler;
if (sigaction(SIGUSR1, &act, NULL)) {
ERROR("Failed to setup SIGTERM handler");
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return -EPERM;
}
for (i = 0; i < dio_thr_ctx->qs; i++) {
int rc = pthread_create(&dio_thr_ctx->ioctx[i].tid, NULL, params->engine == ENGINE_DIO ? thread_func_dio : thread_func_nvme, &dio_thr_ctx->ioctx[i]);
if (rc ) {
dio_thr_ctx->ioctx[i].tid = 0;
if (rc > 0)
rc = -rc;
ERROR("Failed to create DIO thread");
dio_destroy_thread_ctx(&dio_thr_ctx->iobench_ctx);
return rc;
}
}
*pctx = &dio_thr_ctx->iobench_ctx;
return 0;
}
static int dio_poll_completions(io_bench_thr_ctx_t *ctx, int n)
{
usleep(100000);
return 0;
}
static io_ctx_t *dio_get_io_ctx(io_bench_thr_ctx_t *ctx, uint16_t slot)
{
dio_thr_ctx_t *pctx = U_2_P(ctx);
if (pctx->qs <= slot)
return NULL;
return &pctx->ioctx[slot].ioctx;
}
static int dio_queue_io(io_bench_thr_ctx_t *ctx, io_ctx_t *io)
{
dio_ioctx_t *pio = UIO_2_PIO(io);
dio_thr_ctx_t *pctx = U_2_P(ctx);
ASSERT(pctx->self == pthread_self());
pthread_mutex_lock(&pio->run_mutex);
pio->may_run = true;
pthread_cond_signal(&pio->run_cond);
pthread_mutex_unlock(&pio->run_mutex);
return 0;
}