]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/nvme/reset/reset.c
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "spdk/stdinc.h"
36 #include "spdk/nvme.h"
38 #include "spdk/string.h"
39 #include "spdk/pci_ids.h"
42 struct spdk_nvme_ctrlr
*ctrlr
;
43 struct ctrlr_entry
*next
;
48 struct spdk_nvme_ns
*ns
;
49 struct spdk_nvme_ctrlr
*ctrlr
;
50 struct ns_entry
*next
;
51 uint32_t io_size_blocks
;
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
;
66 struct ns_worker_ctx
*next
;
70 struct ns_worker_ctx
*ns_ctx
;
74 struct worker_thread
{
75 struct ns_worker_ctx
*ns_ctx
;
79 static struct spdk_mempool
*task_pool
;
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;
87 static uint64_t g_tsc_rate
;
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
;
95 #define TASK_POOL_NUM 8192
98 register_ns(struct spdk_nvme_ctrlr
*ctrlr
, struct spdk_nvme_ns
*ns
)
100 struct ns_entry
*entry
;
101 const struct spdk_nvme_ctrlr_data
*cdata
;
103 if (!spdk_nvme_ns_is_active(ns
)) {
104 printf("Skipping inactive NS %u\n", spdk_nvme_ns_get_id(ns
));
108 entry
= malloc(sizeof(struct ns_entry
));
110 perror("ns_entry malloc");
114 cdata
= spdk_nvme_ctrlr_get_data(ctrlr
);
117 entry
->ctrlr
= ctrlr
;
118 entry
->size_in_ios
= spdk_nvme_ns_get_size(ns
) /
120 entry
->io_size_blocks
= g_io_size_bytes
/ spdk_nvme_ns_get_sector_size(ns
);
122 snprintf(entry
->name
, 44, "%-20.20s (%-20.20s)", cdata
->mn
, cdata
->sn
);
125 entry
->next
= g_namespaces
;
126 g_namespaces
= entry
;
130 register_ctrlr(struct spdk_nvme_ctrlr
*ctrlr
)
133 struct spdk_nvme_ns
*ns
;
134 struct ctrlr_entry
*entry
= malloc(sizeof(struct ctrlr_entry
));
137 perror("ctrlr_entry malloc");
141 entry
->ctrlr
= ctrlr
;
142 entry
->next
= g_controllers
;
143 g_controllers
= entry
;
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
);
151 register_ns(ctrlr
, ns
);
155 static void io_complete(void *ctx
, const struct spdk_nvme_cpl
*completion
);
157 static __thread
unsigned int seed
= 0;
160 submit_single_io(struct ns_worker_ctx
*ns_ctx
)
162 struct reset_task
*task
= NULL
;
163 uint64_t offset_in_ios
;
165 struct ns_entry
*entry
= ns_ctx
->entry
;
167 task
= spdk_mempool_get(task_pool
);
169 fprintf(stderr
, "Failed to get task from task_pool\n");
173 task
->buf
= spdk_zmalloc(g_io_size_bytes
, 0x200, NULL
, SPDK_ENV_LCORE_ID_ANY
, SPDK_MALLOC_DMA
);
175 spdk_free(task
->buf
);
176 fprintf(stderr
, "task->buf spdk_zmalloc failed\n");
180 task
->ns_ctx
= ns_ctx
;
181 task
->ns_ctx
->io_submitted
++;
184 offset_in_ios
= rand_r(&seed
) % entry
->size_in_ios
;
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;
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);
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);
204 fprintf(stderr
, "starting I/O failed\n");
206 ns_ctx
->current_queue_depth
++;
211 task_complete(struct reset_task
*task
, const struct spdk_nvme_cpl
*completion
)
213 struct ns_worker_ctx
*ns_ctx
;
215 ns_ctx
= task
->ns_ctx
;
216 ns_ctx
->current_queue_depth
--;
218 if (spdk_nvme_cpl_is_error(completion
)) {
219 ns_ctx
->io_completed_error
++;
221 ns_ctx
->io_completed
++;
224 spdk_free(task
->buf
);
225 spdk_mempool_put(task_pool
, task
);
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.
233 if (!ns_ctx
->is_draining
) {
234 submit_single_io(ns_ctx
);
239 io_complete(void *ctx
, const struct spdk_nvme_cpl
*completion
)
241 task_complete((struct reset_task
*)ctx
, completion
);
245 check_io(struct ns_worker_ctx
*ns_ctx
)
247 spdk_nvme_qpair_process_completions(ns_ctx
->qpair
, 0);
251 submit_io(struct ns_worker_ctx
*ns_ctx
, int queue_depth
)
253 while (queue_depth
-- > 0) {
254 submit_single_io(ns_ctx
);
259 drain_io(struct ns_worker_ctx
*ns_ctx
)
261 ns_ctx
->is_draining
= true;
262 while (ns_ctx
->current_queue_depth
> 0) {
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;
275 printf("Starting thread on core %u\n", worker
->lcore
);
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
);
285 submit_io(ns_ctx
, g_queue_depth
);
286 ns_ctx
= ns_ctx
->next
;
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");
297 ns_ctx
= ns_ctx
->next
;
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.
307 ns_ctx
= worker
->ns_ctx
;
308 while (ns_ctx
!= NULL
) {
310 ns_ctx
= ns_ctx
->next
;
313 if (spdk_get_ticks() > tsc_end
) {
318 ns_ctx
= worker
->ns_ctx
;
319 while (ns_ctx
!= NULL
) {
321 spdk_nvme_ctrlr_free_io_qpair(ns_ctx
->qpair
);
322 ns_ctx
= ns_ctx
->next
;
328 static void usage(char *program_name
)
330 printf("%s options", program_name
);
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");
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
;
350 total_completed_io
= 0;
351 total_submitted_io
= 0;
352 total_completed_err_io
= 0;
355 ns_ctx
= worker
->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
;
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
);
373 if (total_submitted_io
!= (total_completed_io
+ total_completed_err_io
)) {
374 fprintf(stderr
, "Some IO are missing......\n");
382 parse_args(int argc
, char **argv
)
384 const char *workload_type
;
386 bool mix_specified
= false;
392 workload_type
= NULL
;
394 g_rw_percentage
= -1;
396 while ((op
= getopt(argc
, argv
, "m:q:s:t:w:M:")) != -1) {
398 workload_type
= optarg
;
399 } else if (op
== '?') {
403 val
= spdk_strtol(optarg
, 10);
405 fprintf(stderr
, "Converting a string to integer failed\n");
413 g_io_size_bytes
= val
;
419 g_rw_percentage
= val
;
420 mix_specified
= true;
429 if (!g_queue_depth
) {
433 if (!g_io_size_bytes
) {
437 if (!workload_type
) {
441 if (!g_time_in_sec
) {
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")) {
453 "io pattern type must be one of\n"
454 "(read, write, randread, randwrite, rw, randrw)\n");
458 if (!strcmp(workload_type
, "read") ||
459 !strcmp(workload_type
, "randread")) {
460 g_rw_percentage
= 100;
463 if (!strcmp(workload_type
, "write") ||
464 !strcmp(workload_type
, "randwrite")) {
468 if (!strcmp(workload_type
, "read") ||
469 !strcmp(workload_type
, "randread") ||
470 !strcmp(workload_type
, "write") ||
471 !strcmp(workload_type
, "randwrite")) {
473 fprintf(stderr
, "Ignoring -M option... Please use -M option"
474 " only when using rw or randrw.\n");
478 if (!strcmp(workload_type
, "rw") ||
479 !strcmp(workload_type
, "randrw")) {
480 if (g_rw_percentage
< 0 || g_rw_percentage
> 100) {
482 "-M must be specified to value from 0 to 100 "
483 "for rw or randrw.\n");
488 if (!strcmp(workload_type
, "read") ||
489 !strcmp(workload_type
, "write") ||
490 !strcmp(workload_type
, "rw")) {
500 register_workers(void)
502 struct worker_thread
*worker
;
504 worker
= malloc(sizeof(struct worker_thread
));
505 if (worker
== NULL
) {
506 perror("worker_thread malloc");
510 memset(worker
, 0, sizeof(struct worker_thread
));
511 worker
->lcore
= spdk_env_get_current_core();
520 probe_cb(void *cb_ctx
, const struct spdk_nvme_transport_id
*trid
,
521 struct spdk_nvme_ctrlr_opts
*opts
)
523 opts
->disable_error_logging
= true;
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
)
531 if (trid
->trtype
== SPDK_NVME_TRANSPORT_PCIE
) {
532 struct spdk_pci_device
*dev
= spdk_nvme_ctrlr_get_pci_device(ctrlr
);
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
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
);
547 register_ctrlr(ctrlr
);
551 register_controllers(void)
553 printf("Initializing NVMe Controllers\n");
555 if (spdk_nvme_probe(NULL
, NULL
, probe_cb
, attach_cb
, NULL
) != 0) {
556 fprintf(stderr
, "spdk_nvme_probe() failed\n");
564 unregister_controllers(void)
566 struct ctrlr_entry
*entry
= g_controllers
;
569 struct ctrlr_entry
*next
= entry
->next
;
570 spdk_nvme_detach(entry
->ctrlr
);
577 associate_workers_with_ns(void)
579 struct ns_entry
*entry
= g_namespaces
;
580 struct worker_thread
*worker
= g_workers
;
581 struct ns_worker_ctx
*ns_ctx
;
584 count
= g_num_namespaces
;
586 for (i
= 0; i
< count
; i
++) {
590 ns_ctx
= malloc(sizeof(struct ns_worker_ctx
));
594 memset(ns_ctx
, 0, sizeof(*ns_ctx
));
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
;
605 entry
= g_namespaces
;
613 run_nvme_reset_cycle(void)
615 struct worker_thread
*worker
;
616 struct ns_worker_ctx
*ns_ctx
;
618 if (work_fn(g_workers
) != 0) {
622 if (print_stats() != 0) {
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
;
640 spdk_reset_free_tasks(void)
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
);
646 spdk_mempool_free(task_pool
);
649 int main(int argc
, char **argv
)
653 struct spdk_env_opts opts
;
656 rc
= parse_args(argc
, argv
);
661 spdk_env_opts_init(&opts
);
663 opts
.core_mask
= "0x1";
665 if (spdk_env_init(&opts
) < 0) {
666 fprintf(stderr
, "Unable to initialize SPDK env\n");
670 if (register_controllers() != 0) {
674 if (!g_controllers
) {
675 printf("No NVMe controller found, %s exiting\n", argv
[0]);
676 return g_qemu_ssd_found
? 0 : 1;
679 task_pool
= spdk_mempool_create("task_pool", TASK_POOL_NUM
,
680 sizeof(struct reset_task
),
681 64, SPDK_ENV_SOCKET_ID_ANY
);
683 fprintf(stderr
, "Cannot create task pool\n");
687 g_tsc_rate
= spdk_get_ticks_hz();
689 if (register_workers() != 0) {
693 if (associate_workers_with_ns() != 0) {
698 printf("Initialization complete. Launching workers.\n");
700 for (i
= 2; i
>= 0; i
--) {
701 rc
= run_nvme_reset_cycle();
708 unregister_controllers();
709 spdk_reset_free_tasks();
712 fprintf(stderr
, "%s: errors occured\n", argv
[0]);