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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "spdk/stdinc.h"
#include "spdk/nvme.h"
#include "spdk/env.h"
#include "spdk/string.h"
struct ctrlr_entry {
struct spdk_nvme_ctrlr *ctrlr;
struct ctrlr_entry *next;
char name[1024];
};
struct ns_entry {
struct spdk_nvme_ns *ns;
struct spdk_nvme_ctrlr *ctrlr;
struct ns_entry *next;
uint32_t io_size_blocks;
uint64_t size_in_ios;
char name[1024];
};
struct ns_worker_ctx {
struct ns_entry *entry;
struct spdk_nvme_qpair *qpair;
uint64_t io_completed;
uint64_t io_completed_error;
uint64_t io_submitted;
uint64_t current_queue_depth;
uint64_t offset_in_ios;
bool is_draining;
struct ns_worker_ctx *next;
};
struct reset_task {
struct ns_worker_ctx *ns_ctx;
void *buf;
};
struct worker_thread {
struct ns_worker_ctx *ns_ctx;
unsigned lcore;
};
static struct spdk_mempool *task_pool;
static struct ctrlr_entry *g_controllers = NULL;
static struct ns_entry *g_namespaces = NULL;
static int g_num_namespaces = 0;
static struct worker_thread *g_workers = NULL;
static uint64_t g_tsc_rate;
static int g_io_size_bytes;
static int g_rw_percentage;
static int g_is_random;
static int g_queue_depth;
static int g_time_in_sec;
#define TASK_POOL_NUM 8192
static void
register_ns(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns)
{
struct ns_entry *entry;
const struct spdk_nvme_ctrlr_data *cdata;
if (!spdk_nvme_ns_is_active(ns)) {
printf("Skipping inactive NS %u\n", spdk_nvme_ns_get_id(ns));
return;
}
entry = malloc(sizeof(struct ns_entry));
if (entry == NULL) {
perror("ns_entry malloc");
exit(1);
}
cdata = spdk_nvme_ctrlr_get_data(ctrlr);
entry->ns = ns;
entry->ctrlr = ctrlr;
entry->size_in_ios = spdk_nvme_ns_get_size(ns) /
g_io_size_bytes;
entry->io_size_blocks = g_io_size_bytes / spdk_nvme_ns_get_sector_size(ns);
snprintf(entry->name, 44, "%-20.20s (%-20.20s)", cdata->mn, cdata->sn);
g_num_namespaces++;
entry->next = g_namespaces;
g_namespaces = entry;
}
static void
register_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
{
int nsid, num_ns;
struct spdk_nvme_ns *ns;
struct ctrlr_entry *entry = malloc(sizeof(struct ctrlr_entry));
if (entry == NULL) {
perror("ctrlr_entry malloc");
exit(1);
}
entry->ctrlr = ctrlr;
entry->next = g_controllers;
g_controllers = entry;
num_ns = spdk_nvme_ctrlr_get_num_ns(ctrlr);
for (nsid = 1; nsid <= num_ns; nsid++) {
ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid);
if (ns == NULL) {
continue;
}
register_ns(ctrlr, ns);
}
}
static void io_complete(void *ctx, const struct spdk_nvme_cpl *completion);
static __thread unsigned int seed = 0;
static void
submit_single_io(struct ns_worker_ctx *ns_ctx)
{
struct reset_task *task = NULL;
uint64_t offset_in_ios;
int rc;
struct ns_entry *entry = ns_ctx->entry;
task = spdk_mempool_get(task_pool);
if (!task) {
fprintf(stderr, "Failed to get task from task_pool\n");
exit(1);
}
task->buf = spdk_dma_zmalloc(g_io_size_bytes, 0x200, NULL);
if (!task->buf) {
spdk_dma_free(task->buf);
fprintf(stderr, "task->buf spdk_dma_zmalloc failed\n");
exit(1);
}
task->ns_ctx = ns_ctx;
task->ns_ctx->io_submitted++;
if (g_is_random) {
offset_in_ios = rand_r(&seed) % entry->size_in_ios;
} else {
offset_in_ios = ns_ctx->offset_in_ios++;
if (ns_ctx->offset_in_ios == entry->size_in_ios) {
ns_ctx->offset_in_ios = 0;
}
}
if ((g_rw_percentage == 100) ||
(g_rw_percentage != 0 && ((rand_r(&seed) % 100) < g_rw_percentage))) {
rc = spdk_nvme_ns_cmd_read(entry->ns, ns_ctx->qpair, task->buf,
offset_in_ios * entry->io_size_blocks,
entry->io_size_blocks, io_complete, task, 0);
} else {
rc = spdk_nvme_ns_cmd_write(entry->ns, ns_ctx->qpair, task->buf,
offset_in_ios * entry->io_size_blocks,
entry->io_size_blocks, io_complete, task, 0);
}
if (rc != 0) {
fprintf(stderr, "starting I/O failed\n");
}
ns_ctx->current_queue_depth++;
}
static void
task_complete(struct reset_task *task, const struct spdk_nvme_cpl *completion)
{
struct ns_worker_ctx *ns_ctx;
ns_ctx = task->ns_ctx;
ns_ctx->current_queue_depth--;
if (spdk_nvme_cpl_is_error(completion)) {
ns_ctx->io_completed_error++;
} else {
ns_ctx->io_completed++;
}
spdk_dma_free(task->buf);
spdk_mempool_put(task_pool, task);
/*
* is_draining indicates when time has expired for the test run
* and we are just waiting for the previously submitted I/O
* to complete. In this case, do not submit a new I/O to replace
* the one just completed.
*/
if (!ns_ctx->is_draining) {
submit_single_io(ns_ctx);
}
}
static void
io_complete(void *ctx, const struct spdk_nvme_cpl *completion)
{
task_complete((struct reset_task *)ctx, completion);
}
static void
check_io(struct ns_worker_ctx *ns_ctx)
{
spdk_nvme_qpair_process_completions(ns_ctx->qpair, 0);
}
static void
submit_io(struct ns_worker_ctx *ns_ctx, int queue_depth)
{
while (queue_depth-- > 0) {
submit_single_io(ns_ctx);
}
}
static void
drain_io(struct ns_worker_ctx *ns_ctx)
{
ns_ctx->is_draining = true;
while (ns_ctx->current_queue_depth > 0) {
check_io(ns_ctx);
}
}
static int
work_fn(void *arg)
{
uint64_t tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
struct worker_thread *worker = (struct worker_thread *)arg;
struct ns_worker_ctx *ns_ctx = NULL;
bool did_reset = false;
printf("Starting thread on core %u\n", worker->lcore);
/* Submit initial I/O for each namespace. */
ns_ctx = worker->ns_ctx;
while (ns_ctx != NULL) {
ns_ctx->qpair = spdk_nvme_ctrlr_alloc_io_qpair(ns_ctx->entry->ctrlr, NULL, 0);
if (ns_ctx->qpair == NULL) {
fprintf(stderr, "spdk_nvme_ctrlr_alloc_io_qpair() failed on core %u\n", worker->lcore);
return -1;
}
submit_io(ns_ctx, g_queue_depth);
ns_ctx = ns_ctx->next;
}
while (1) {
/*
* Check for completed I/O for each controller. A new
* I/O will be submitted in the io_complete callback
* to replace each I/O that is completed.
*/
ns_ctx = worker->ns_ctx;
while (ns_ctx != NULL) {
check_io(ns_ctx);
ns_ctx = ns_ctx->next;
}
if (!did_reset && ((tsc_end - spdk_get_ticks()) / g_tsc_rate) > (uint64_t)g_time_in_sec / 2) {
ns_ctx = worker->ns_ctx;
while (ns_ctx != NULL) {
if (spdk_nvme_ctrlr_reset(ns_ctx->entry->ctrlr) < 0) {
fprintf(stderr, "nvme reset failed.\n");
return -1;
}
ns_ctx = ns_ctx->next;
}
did_reset = true;
}
if (spdk_get_ticks() > tsc_end) {
break;
}
}
ns_ctx = worker->ns_ctx;
while (ns_ctx != NULL) {
drain_io(ns_ctx);
spdk_nvme_ctrlr_free_io_qpair(ns_ctx->qpair);
ns_ctx = ns_ctx->next;
}
return 0;
}
static void usage(char *program_name)
{
printf("%s options", program_name);
printf("\n");
printf("\t[-q io depth]\n");
printf("\t[-s io size in bytes]\n");
printf("\t[-w io pattern type, must be one of\n");
printf("\t\t(read, write, randread, randwrite, rw, randrw)]\n");
printf("\t[-M rwmixread (100 for reads, 0 for writes)]\n");
printf("\t[-t time in seconds(should be larger than 15 seconds)]\n");
printf("\t[-m max completions per poll]\n");
printf("\t\t(default:0 - unlimited)\n");
}
static int
print_stats(void)
{
uint64_t io_completed, io_submitted, io_completed_error;
uint64_t total_completed_io, total_submitted_io, total_completed_err_io;
struct worker_thread *worker;
struct ns_worker_ctx *ns_ctx;
total_completed_io = 0;
total_submitted_io = 0;
total_completed_err_io = 0;
worker = g_workers;
ns_ctx = worker->ns_ctx;
while (ns_ctx) {
io_completed = ns_ctx->io_completed;
io_submitted = ns_ctx->io_submitted;
io_completed_error = ns_ctx->io_completed_error;
total_completed_io += io_completed;
total_submitted_io += io_submitted;
total_completed_err_io += io_completed_error;
ns_ctx = ns_ctx->next;
}
printf("========================================================\n");
printf("%16lu IO completed successfully\n", total_completed_io);
printf("%16lu IO completed with error\n", total_completed_err_io);
printf("--------------------------------------------------------\n");
printf("%16lu IO completed total\n", total_completed_io + total_completed_err_io);
printf("%16lu IO submitted\n", total_submitted_io);
if (total_submitted_io != (total_completed_io + total_completed_err_io)) {
fprintf(stderr, "Some IO are missing......\n");
return -1;
}
return 0;
}
static int
parse_args(int argc, char **argv)
{
const char *workload_type;
int op;
bool mix_specified = false;
/* default value */
g_queue_depth = 0;
g_io_size_bytes = 0;
workload_type = NULL;
g_time_in_sec = 0;
g_rw_percentage = -1;
while ((op = getopt(argc, argv, "m:q:s:t:w:M:")) != -1) {
switch (op) {
case 'q':
g_queue_depth = atoi(optarg);
break;
case 's':
g_io_size_bytes = atoi(optarg);
break;
case 't':
g_time_in_sec = atoi(optarg);
break;
case 'w':
workload_type = optarg;
break;
case 'M':
g_rw_percentage = atoi(optarg);
mix_specified = true;
break;
default:
usage(argv[0]);
return 1;
}
}
if (!g_queue_depth) {
usage(argv[0]);
return 1;
}
if (!g_io_size_bytes) {
usage(argv[0]);
return 1;
}
if (!workload_type) {
usage(argv[0]);
return 1;
}
if (!g_time_in_sec) {
usage(argv[0]);
return 1;
}
if (strcmp(workload_type, "read") &&
strcmp(workload_type, "write") &&
strcmp(workload_type, "randread") &&
strcmp(workload_type, "randwrite") &&
strcmp(workload_type, "rw") &&
strcmp(workload_type, "randrw")) {
fprintf(stderr,
"io pattern type must be one of\n"
"(read, write, randread, randwrite, rw, randrw)\n");
return 1;
}
if (!strcmp(workload_type, "read") ||
!strcmp(workload_type, "randread")) {
g_rw_percentage = 100;
}
if (!strcmp(workload_type, "write") ||
!strcmp(workload_type, "randwrite")) {
g_rw_percentage = 0;
}
if (!strcmp(workload_type, "read") ||
!strcmp(workload_type, "randread") ||
!strcmp(workload_type, "write") ||
!strcmp(workload_type, "randwrite")) {
if (mix_specified) {
fprintf(stderr, "Ignoring -M option... Please use -M option"
" only when using rw or randrw.\n");
}
}
if (!strcmp(workload_type, "rw") ||
!strcmp(workload_type, "randrw")) {
if (g_rw_percentage < 0 || g_rw_percentage > 100) {
fprintf(stderr,
"-M must be specified to value from 0 to 100 "
"for rw or randrw.\n");
return 1;
}
}
if (!strcmp(workload_type, "read") ||
!strcmp(workload_type, "write") ||
!strcmp(workload_type, "rw")) {
g_is_random = 0;
} else {
g_is_random = 1;
}
return 0;
}
static int
register_workers(void)
{
struct worker_thread *worker;
worker = malloc(sizeof(struct worker_thread));
if (worker == NULL) {
perror("worker_thread malloc");
return -1;
}
memset(worker, 0, sizeof(struct worker_thread));
worker->lcore = spdk_env_get_current_core();
g_workers = worker;
return 0;
}
static bool
probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
struct spdk_nvme_ctrlr_opts *opts)
{
return true;
}
static void
attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
{
register_ctrlr(ctrlr);
}
static int
register_controllers(void)
{
printf("Initializing NVMe Controllers\n");
if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) {
fprintf(stderr, "spdk_nvme_probe() failed\n");
return 1;
}
return 0;
}
static void
unregister_controllers(void)
{
struct ctrlr_entry *entry = g_controllers;
while (entry) {
struct ctrlr_entry *next = entry->next;
spdk_nvme_detach(entry->ctrlr);
free(entry);
entry = next;
}
}
static int
associate_workers_with_ns(void)
{
struct ns_entry *entry = g_namespaces;
struct worker_thread *worker = g_workers;
struct ns_worker_ctx *ns_ctx;
int i, count;
count = g_num_namespaces;
for (i = 0; i < count; i++) {
if (entry == NULL) {
break;
}
ns_ctx = malloc(sizeof(struct ns_worker_ctx));
if (!ns_ctx) {
return -1;
}
memset(ns_ctx, 0, sizeof(*ns_ctx));
printf("Associating %s with lcore %d\n", entry->name, worker->lcore);
ns_ctx->entry = entry;
ns_ctx->next = worker->ns_ctx;
worker->ns_ctx = ns_ctx;
worker = g_workers;
entry = entry->next;
if (entry == NULL) {
entry = g_namespaces;
}
}
return 0;
}
static int
run_nvme_reset_cycle(int retry_count)
{
struct worker_thread *worker;
struct ns_worker_ctx *ns_ctx;
spdk_nvme_retry_count = retry_count;
if (work_fn(g_workers) != 0) {
return -1;
}
if (print_stats() != 0) {
return -1;
}
worker = g_workers;
ns_ctx = worker->ns_ctx;
while (ns_ctx != NULL) {
ns_ctx->io_completed = 0;
ns_ctx->io_completed_error = 0;
ns_ctx->io_submitted = 0;
ns_ctx->is_draining = false;
ns_ctx = ns_ctx->next;
}
return 0;
}
static void
spdk_reset_free_tasks(void)
{
if (spdk_mempool_count(task_pool) != TASK_POOL_NUM) {
fprintf(stderr, "task_pool count is %zu but should be %d\n",
spdk_mempool_count(task_pool), TASK_POOL_NUM);
}
spdk_mempool_free(task_pool);
}
int main(int argc, char **argv)
{
int rc;
int i;
struct spdk_env_opts opts;
rc = parse_args(argc, argv);
if (rc != 0) {
return rc;
}
spdk_env_opts_init(&opts);
opts.name = "reset";
opts.core_mask = "0x1";
opts.shm_id = 0;
if (spdk_env_init(&opts) < 0) {
fprintf(stderr, "Unable to initialize SPDK env\n");
return 1;
}
if (register_controllers() != 0) {
return 1;
}
if (!g_controllers) {
printf("No NVMe controller found, %s exiting\n", argv[0]);
return 1;
}
task_pool = spdk_mempool_create("task_pool", TASK_POOL_NUM,
sizeof(struct reset_task),
64, SPDK_ENV_SOCKET_ID_ANY);
if (!task_pool) {
fprintf(stderr, "Cannot create task pool\n");
return 1;
}
g_tsc_rate = spdk_get_ticks_hz();
if (register_workers() != 0) {
return 1;
}
if (associate_workers_with_ns() != 0) {
rc = 1;
goto cleanup;
}
printf("Initialization complete. Launching workers.\n");
for (i = 2; i >= 0; i--) {
rc = run_nvme_reset_cycle(i);
if (rc != 0) {
goto cleanup;
}
}
cleanup:
unregister_controllers();
spdk_reset_free_tasks();
if (rc != 0) {
fprintf(stderr, "%s: errors occured\n", argv[0]);
}
return rc;
}
|