]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/nvme/reset/reset.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / test / nvme / reset / reset.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "spdk/stdinc.h"
35
36 #include "spdk/nvme.h"
37 #include "spdk/env.h"
38 #include "spdk/string.h"
39 #include "spdk/pci_ids.h"
40
41 struct ctrlr_entry {
42 struct spdk_nvme_ctrlr *ctrlr;
43 struct ctrlr_entry *next;
44 char name[1024];
45 };
46
47 struct ns_entry {
48 struct spdk_nvme_ns *ns;
49 struct spdk_nvme_ctrlr *ctrlr;
50 struct ns_entry *next;
51 uint32_t io_size_blocks;
52 uint64_t size_in_ios;
53 char name[1024];
54 };
55
56 struct ns_worker_ctx {
57 struct ns_entry *entry;
58 struct spdk_nvme_qpair *qpair;
59 uint64_t io_completed;
60 uint64_t io_completed_error;
61 uint64_t io_submitted;
62 uint64_t current_queue_depth;
63 uint64_t offset_in_ios;
64 bool is_draining;
65
66 struct ns_worker_ctx *next;
67 };
68
69 struct reset_task {
70 struct ns_worker_ctx *ns_ctx;
71 void *buf;
72 };
73
74 struct worker_thread {
75 struct ns_worker_ctx *ns_ctx;
76 unsigned lcore;
77 };
78
79 static struct spdk_mempool *task_pool;
80
81 static struct ctrlr_entry *g_controllers = NULL;
82 static struct ns_entry *g_namespaces = NULL;
83 static int g_num_namespaces = 0;
84 static struct worker_thread *g_workers = NULL;
85 static bool g_qemu_ssd_found = false;
86
87 static uint64_t g_tsc_rate;
88
89 static int g_io_size_bytes;
90 static int g_rw_percentage;
91 static int g_is_random;
92 static int g_queue_depth;
93 static int g_time_in_sec;
94
95 #define TASK_POOL_NUM 8192
96
97 static void
98 register_ns(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns)
99 {
100 struct ns_entry *entry;
101 const struct spdk_nvme_ctrlr_data *cdata;
102
103 if (!spdk_nvme_ns_is_active(ns)) {
104 printf("Skipping inactive NS %u\n", spdk_nvme_ns_get_id(ns));
105 return;
106 }
107
108 entry = malloc(sizeof(struct ns_entry));
109 if (entry == NULL) {
110 perror("ns_entry malloc");
111 exit(1);
112 }
113
114 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
115
116 entry->ns = ns;
117 entry->ctrlr = ctrlr;
118 entry->size_in_ios = spdk_nvme_ns_get_size(ns) /
119 g_io_size_bytes;
120 entry->io_size_blocks = g_io_size_bytes / spdk_nvme_ns_get_sector_size(ns);
121
122 snprintf(entry->name, 44, "%-20.20s (%-20.20s)", cdata->mn, cdata->sn);
123
124 g_num_namespaces++;
125 entry->next = g_namespaces;
126 g_namespaces = entry;
127 }
128
129 static void
130 register_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
131 {
132 int nsid, num_ns;
133 struct spdk_nvme_ns *ns;
134 struct ctrlr_entry *entry = malloc(sizeof(struct ctrlr_entry));
135
136 if (entry == NULL) {
137 perror("ctrlr_entry malloc");
138 exit(1);
139 }
140
141 entry->ctrlr = ctrlr;
142 entry->next = g_controllers;
143 g_controllers = entry;
144
145 num_ns = spdk_nvme_ctrlr_get_num_ns(ctrlr);
146 for (nsid = 1; nsid <= num_ns; nsid++) {
147 ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid);
148 if (ns == NULL) {
149 continue;
150 }
151 register_ns(ctrlr, ns);
152 }
153 }
154
155 static void io_complete(void *ctx, const struct spdk_nvme_cpl *completion);
156
157 static __thread unsigned int seed = 0;
158
159 static void
160 submit_single_io(struct ns_worker_ctx *ns_ctx)
161 {
162 struct reset_task *task = NULL;
163 uint64_t offset_in_ios;
164 int rc;
165 struct ns_entry *entry = ns_ctx->entry;
166
167 task = spdk_mempool_get(task_pool);
168 if (!task) {
169 fprintf(stderr, "Failed to get task from task_pool\n");
170 exit(1);
171 }
172
173 task->buf = spdk_zmalloc(g_io_size_bytes, 0x200, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
174 if (!task->buf) {
175 spdk_free(task->buf);
176 fprintf(stderr, "task->buf spdk_zmalloc failed\n");
177 exit(1);
178 }
179
180 task->ns_ctx = ns_ctx;
181 task->ns_ctx->io_submitted++;
182
183 if (g_is_random) {
184 offset_in_ios = rand_r(&seed) % entry->size_in_ios;
185 } else {
186 offset_in_ios = ns_ctx->offset_in_ios++;
187 if (ns_ctx->offset_in_ios == entry->size_in_ios) {
188 ns_ctx->offset_in_ios = 0;
189 }
190 }
191
192 if ((g_rw_percentage == 100) ||
193 (g_rw_percentage != 0 && ((rand_r(&seed) % 100) < g_rw_percentage))) {
194 rc = spdk_nvme_ns_cmd_read(entry->ns, ns_ctx->qpair, task->buf,
195 offset_in_ios * entry->io_size_blocks,
196 entry->io_size_blocks, io_complete, task, 0);
197 } else {
198 rc = spdk_nvme_ns_cmd_write(entry->ns, ns_ctx->qpair, task->buf,
199 offset_in_ios * entry->io_size_blocks,
200 entry->io_size_blocks, io_complete, task, 0);
201 }
202
203 if (rc != 0) {
204 fprintf(stderr, "starting I/O failed\n");
205 } else {
206 ns_ctx->current_queue_depth++;
207 }
208 }
209
210 static void
211 task_complete(struct reset_task *task, const struct spdk_nvme_cpl *completion)
212 {
213 struct ns_worker_ctx *ns_ctx;
214
215 ns_ctx = task->ns_ctx;
216 ns_ctx->current_queue_depth--;
217
218 if (spdk_nvme_cpl_is_error(completion)) {
219 ns_ctx->io_completed_error++;
220 } else {
221 ns_ctx->io_completed++;
222 }
223
224 spdk_free(task->buf);
225 spdk_mempool_put(task_pool, task);
226
227 /*
228 * is_draining indicates when time has expired for the test run
229 * and we are just waiting for the previously submitted I/O
230 * to complete. In this case, do not submit a new I/O to replace
231 * the one just completed.
232 */
233 if (!ns_ctx->is_draining) {
234 submit_single_io(ns_ctx);
235 }
236 }
237
238 static void
239 io_complete(void *ctx, const struct spdk_nvme_cpl *completion)
240 {
241 task_complete((struct reset_task *)ctx, completion);
242 }
243
244 static void
245 check_io(struct ns_worker_ctx *ns_ctx)
246 {
247 spdk_nvme_qpair_process_completions(ns_ctx->qpair, 0);
248 }
249
250 static void
251 submit_io(struct ns_worker_ctx *ns_ctx, int queue_depth)
252 {
253 while (queue_depth-- > 0) {
254 submit_single_io(ns_ctx);
255 }
256 }
257
258 static void
259 drain_io(struct ns_worker_ctx *ns_ctx)
260 {
261 ns_ctx->is_draining = true;
262 while (ns_ctx->current_queue_depth > 0) {
263 check_io(ns_ctx);
264 }
265 }
266
267 static int
268 work_fn(void *arg)
269 {
270 uint64_t tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
271 struct worker_thread *worker = (struct worker_thread *)arg;
272 struct ns_worker_ctx *ns_ctx = NULL;
273 bool did_reset = false;
274
275 printf("Starting thread on core %u\n", worker->lcore);
276
277 /* Submit initial I/O for each namespace. */
278 ns_ctx = worker->ns_ctx;
279 while (ns_ctx != NULL) {
280 ns_ctx->qpair = spdk_nvme_ctrlr_alloc_io_qpair(ns_ctx->entry->ctrlr, NULL, 0);
281 if (ns_ctx->qpair == NULL) {
282 fprintf(stderr, "spdk_nvme_ctrlr_alloc_io_qpair() failed on core %u\n", worker->lcore);
283 return -1;
284 }
285 submit_io(ns_ctx, g_queue_depth);
286 ns_ctx = ns_ctx->next;
287 }
288
289 while (1) {
290 if (!did_reset && ((tsc_end - spdk_get_ticks()) / g_tsc_rate) > (uint64_t)g_time_in_sec / 2) {
291 ns_ctx = worker->ns_ctx;
292 while (ns_ctx != NULL) {
293 if (spdk_nvme_ctrlr_reset(ns_ctx->entry->ctrlr) < 0) {
294 fprintf(stderr, "nvme reset failed.\n");
295 return -1;
296 }
297 ns_ctx = ns_ctx->next;
298 }
299 did_reset = true;
300 }
301
302 /*
303 * Check for completed I/O for each controller. A new
304 * I/O will be submitted in the io_complete callback
305 * to replace each I/O that is completed.
306 */
307 ns_ctx = worker->ns_ctx;
308 while (ns_ctx != NULL) {
309 check_io(ns_ctx);
310 ns_ctx = ns_ctx->next;
311 }
312
313 if (spdk_get_ticks() > tsc_end) {
314 break;
315 }
316 }
317
318 ns_ctx = worker->ns_ctx;
319 while (ns_ctx != NULL) {
320 drain_io(ns_ctx);
321 spdk_nvme_ctrlr_free_io_qpair(ns_ctx->qpair);
322 ns_ctx = ns_ctx->next;
323 }
324
325 return 0;
326 }
327
328 static void usage(char *program_name)
329 {
330 printf("%s options", program_name);
331 printf("\n");
332 printf("\t[-q io depth]\n");
333 printf("\t[-s io size in bytes]\n");
334 printf("\t[-w io pattern type, must be one of\n");
335 printf("\t\t(read, write, randread, randwrite, rw, randrw)]\n");
336 printf("\t[-M rwmixread (100 for reads, 0 for writes)]\n");
337 printf("\t[-t time in seconds(should be larger than 15 seconds)]\n");
338 printf("\t[-m max completions per poll]\n");
339 printf("\t\t(default:0 - unlimited)\n");
340 }
341
342 static int
343 print_stats(void)
344 {
345 uint64_t io_completed, io_submitted, io_completed_error;
346 uint64_t total_completed_io, total_submitted_io, total_completed_err_io;
347 struct worker_thread *worker;
348 struct ns_worker_ctx *ns_ctx;
349
350 total_completed_io = 0;
351 total_submitted_io = 0;
352 total_completed_err_io = 0;
353
354 worker = g_workers;
355 ns_ctx = worker->ns_ctx;
356 while (ns_ctx) {
357 io_completed = ns_ctx->io_completed;
358 io_submitted = ns_ctx->io_submitted;
359 io_completed_error = ns_ctx->io_completed_error;
360 total_completed_io += io_completed;
361 total_submitted_io += io_submitted;
362 total_completed_err_io += io_completed_error;
363 ns_ctx = ns_ctx->next;
364 }
365
366 printf("========================================================\n");
367 printf("%16lu IO completed successfully\n", total_completed_io);
368 printf("%16lu IO completed with error\n", total_completed_err_io);
369 printf("--------------------------------------------------------\n");
370 printf("%16lu IO completed total\n", total_completed_io + total_completed_err_io);
371 printf("%16lu IO submitted\n", total_submitted_io);
372
373 if (total_submitted_io != (total_completed_io + total_completed_err_io)) {
374 fprintf(stderr, "Some IO are missing......\n");
375 return -1;
376 }
377
378 return 0;
379 }
380
381 static int
382 parse_args(int argc, char **argv)
383 {
384 const char *workload_type;
385 int op;
386 bool mix_specified = false;
387 long int val;
388
389 /* default value */
390 g_queue_depth = 0;
391 g_io_size_bytes = 0;
392 workload_type = NULL;
393 g_time_in_sec = 0;
394 g_rw_percentage = -1;
395
396 while ((op = getopt(argc, argv, "m:q:s:t:w:M:")) != -1) {
397 if (op == 'w') {
398 workload_type = optarg;
399 } else if (op == '?') {
400 usage(argv[0]);
401 return -EINVAL;
402 } else {
403 val = spdk_strtol(optarg, 10);
404 if (val < 0) {
405 fprintf(stderr, "Converting a string to integer failed\n");
406 return val;
407 }
408 switch (op) {
409 case 'q':
410 g_queue_depth = val;
411 break;
412 case 's':
413 g_io_size_bytes = val;
414 break;
415 case 't':
416 g_time_in_sec = val;
417 break;
418 case 'M':
419 g_rw_percentage = val;
420 mix_specified = true;
421 break;
422 default:
423 usage(argv[0]);
424 return -EINVAL;
425 }
426 }
427 }
428
429 if (!g_queue_depth) {
430 usage(argv[0]);
431 return 1;
432 }
433 if (!g_io_size_bytes) {
434 usage(argv[0]);
435 return 1;
436 }
437 if (!workload_type) {
438 usage(argv[0]);
439 return 1;
440 }
441 if (!g_time_in_sec) {
442 usage(argv[0]);
443 return 1;
444 }
445
446 if (strcmp(workload_type, "read") &&
447 strcmp(workload_type, "write") &&
448 strcmp(workload_type, "randread") &&
449 strcmp(workload_type, "randwrite") &&
450 strcmp(workload_type, "rw") &&
451 strcmp(workload_type, "randrw")) {
452 fprintf(stderr,
453 "io pattern type must be one of\n"
454 "(read, write, randread, randwrite, rw, randrw)\n");
455 return 1;
456 }
457
458 if (!strcmp(workload_type, "read") ||
459 !strcmp(workload_type, "randread")) {
460 g_rw_percentage = 100;
461 }
462
463 if (!strcmp(workload_type, "write") ||
464 !strcmp(workload_type, "randwrite")) {
465 g_rw_percentage = 0;
466 }
467
468 if (!strcmp(workload_type, "read") ||
469 !strcmp(workload_type, "randread") ||
470 !strcmp(workload_type, "write") ||
471 !strcmp(workload_type, "randwrite")) {
472 if (mix_specified) {
473 fprintf(stderr, "Ignoring -M option... Please use -M option"
474 " only when using rw or randrw.\n");
475 }
476 }
477
478 if (!strcmp(workload_type, "rw") ||
479 !strcmp(workload_type, "randrw")) {
480 if (g_rw_percentage < 0 || g_rw_percentage > 100) {
481 fprintf(stderr,
482 "-M must be specified to value from 0 to 100 "
483 "for rw or randrw.\n");
484 return 1;
485 }
486 }
487
488 if (!strcmp(workload_type, "read") ||
489 !strcmp(workload_type, "write") ||
490 !strcmp(workload_type, "rw")) {
491 g_is_random = 0;
492 } else {
493 g_is_random = 1;
494 }
495
496 return 0;
497 }
498
499 static int
500 register_workers(void)
501 {
502 struct worker_thread *worker;
503
504 worker = malloc(sizeof(struct worker_thread));
505 if (worker == NULL) {
506 perror("worker_thread malloc");
507 return -1;
508 }
509
510 memset(worker, 0, sizeof(struct worker_thread));
511 worker->lcore = spdk_env_get_current_core();
512
513 g_workers = worker;
514
515 return 0;
516 }
517
518
519 static bool
520 probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
521 struct spdk_nvme_ctrlr_opts *opts)
522 {
523 opts->disable_error_logging = true;
524 return true;
525 }
526
527 static void
528 attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
529 struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
530 {
531 if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
532 struct spdk_pci_device *dev = spdk_nvme_ctrlr_get_pci_device(ctrlr);
533
534 /* QEMU emulated SSDs can't handle this test, so we will skip
535 * them. QEMU NVMe SSDs report themselves as VID == Intel. So we need
536 * to check this specific 0x5845 device ID to know whether it's QEMU
537 * or not.
538 */
539 if (spdk_pci_device_get_vendor_id(dev) == SPDK_PCI_VID_INTEL &&
540 spdk_pci_device_get_device_id(dev) == 0x5845) {
541 g_qemu_ssd_found = true;
542 printf("Skipping QEMU NVMe SSD at %s\n", trid->traddr);
543 return;
544 }
545 }
546
547 register_ctrlr(ctrlr);
548 }
549
550 static int
551 register_controllers(void)
552 {
553 printf("Initializing NVMe Controllers\n");
554
555 if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) {
556 fprintf(stderr, "spdk_nvme_probe() failed\n");
557 return 1;
558 }
559
560 return 0;
561 }
562
563 static void
564 unregister_controllers(void)
565 {
566 struct ctrlr_entry *entry = g_controllers;
567
568 while (entry) {
569 struct ctrlr_entry *next = entry->next;
570 spdk_nvme_detach(entry->ctrlr);
571 free(entry);
572 entry = next;
573 }
574 }
575
576 static int
577 associate_workers_with_ns(void)
578 {
579 struct ns_entry *entry = g_namespaces;
580 struct worker_thread *worker = g_workers;
581 struct ns_worker_ctx *ns_ctx;
582 int i, count;
583
584 count = g_num_namespaces;
585
586 for (i = 0; i < count; i++) {
587 if (entry == NULL) {
588 break;
589 }
590 ns_ctx = malloc(sizeof(struct ns_worker_ctx));
591 if (!ns_ctx) {
592 return -1;
593 }
594 memset(ns_ctx, 0, sizeof(*ns_ctx));
595
596 printf("Associating %s with lcore %d\n", entry->name, worker->lcore);
597 ns_ctx->entry = entry;
598 ns_ctx->next = worker->ns_ctx;
599 worker->ns_ctx = ns_ctx;
600
601 worker = g_workers;
602
603 entry = entry->next;
604 if (entry == NULL) {
605 entry = g_namespaces;
606 }
607 }
608
609 return 0;
610 }
611
612 static int
613 run_nvme_reset_cycle(void)
614 {
615 struct worker_thread *worker;
616 struct ns_worker_ctx *ns_ctx;
617
618 if (work_fn(g_workers) != 0) {
619 return -1;
620 }
621
622 if (print_stats() != 0) {
623 return -1;
624 }
625
626 worker = g_workers;
627 ns_ctx = worker->ns_ctx;
628 while (ns_ctx != NULL) {
629 ns_ctx->io_completed = 0;
630 ns_ctx->io_completed_error = 0;
631 ns_ctx->io_submitted = 0;
632 ns_ctx->is_draining = false;
633 ns_ctx = ns_ctx->next;
634 }
635
636 return 0;
637 }
638
639 static void
640 spdk_reset_free_tasks(void)
641 {
642 if (spdk_mempool_count(task_pool) != TASK_POOL_NUM) {
643 fprintf(stderr, "task_pool count is %zu but should be %d\n",
644 spdk_mempool_count(task_pool), TASK_POOL_NUM);
645 }
646 spdk_mempool_free(task_pool);
647 }
648
649 int main(int argc, char **argv)
650 {
651 int rc;
652 int i;
653 struct spdk_env_opts opts;
654
655
656 rc = parse_args(argc, argv);
657 if (rc != 0) {
658 return rc;
659 }
660
661 spdk_env_opts_init(&opts);
662 opts.name = "reset";
663 opts.core_mask = "0x1";
664 opts.shm_id = 0;
665 if (spdk_env_init(&opts) < 0) {
666 fprintf(stderr, "Unable to initialize SPDK env\n");
667 return 1;
668 }
669
670 if (register_controllers() != 0) {
671 return 1;
672 }
673
674 if (!g_controllers) {
675 printf("No NVMe controller found, %s exiting\n", argv[0]);
676 return g_qemu_ssd_found ? 0 : 1;
677 }
678
679 task_pool = spdk_mempool_create("task_pool", TASK_POOL_NUM,
680 sizeof(struct reset_task),
681 64, SPDK_ENV_SOCKET_ID_ANY);
682 if (!task_pool) {
683 fprintf(stderr, "Cannot create task pool\n");
684 return 1;
685 }
686
687 g_tsc_rate = spdk_get_ticks_hz();
688
689 if (register_workers() != 0) {
690 return 1;
691 }
692
693 if (associate_workers_with_ns() != 0) {
694 rc = 1;
695 goto cleanup;
696 }
697
698 printf("Initialization complete. Launching workers.\n");
699
700 for (i = 2; i >= 0; i--) {
701 rc = run_nvme_reset_cycle();
702 if (rc != 0) {
703 goto cleanup;
704 }
705 }
706
707 cleanup:
708 unregister_controllers();
709 spdk_reset_free_tasks();
710
711 if (rc != 0) {
712 fprintf(stderr, "%s: errors occured\n", argv[0]);
713 }
714
715 return rc;
716 }