2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2013 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/ioccom.h>
47 #include "nvmecontrol.h"
49 /* Tables for command line parsing */
51 static cmd_fn_t perftest;
53 #define NONE 0xffffffffu
54 static struct options {
75 static const struct opts perftest_opts[] = {
76 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
77 OPT("perthread", 'p', arg_none, opt, perthread,
78 "Report per-thread results"),
79 OPT("threads", 'n', arg_uint32, opt, threads,
80 "Number of threads to run"),
81 OPT("size", 's', arg_uint32, opt, size,
83 OPT("time", 't', arg_uint32, opt, time,
84 "How long to run the test in seconds"),
85 OPT("operation", 'o', arg_string, opt, op,
86 "Operation type: 'read' or 'write'"),
87 OPT("interrupt", 'i', arg_string, opt, intr,
88 "Interrupt mode: 'intr' or 'wait'"),
89 OPT("flags", 'f', arg_string, opt, flags,
90 "Turn on testing flags: refthread"),
91 { NULL, 0, arg_none, NULL, NULL }
95 static const struct args perftest_args[] = {
96 { arg_string, &opt.dev, "namespace-id" },
97 { arg_none, NULL, NULL },
100 static struct cmd perftest_cmd = {
103 .descr = "Perform low-level performance testing",
104 .ctx_size = sizeof(opt),
105 .opts = perftest_opts,
106 .args = perftest_args,
109 CMD_COMMAND(perftest_cmd);
111 /* End of tables for command line parsing */
114 print_perftest(struct nvme_io_test *io_test, bool perthread)
116 uint64_t io_completed = 0, iops, mbps;
119 for (i = 0; i < io_test->num_threads; i++)
120 io_completed += io_test->io_completed[i];
122 iops = io_completed/io_test->time;
123 mbps = iops * io_test->size / (1024*1024);
125 printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7ju MB/s: %4ju\n",
126 io_test->num_threads, io_test->size,
127 io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
128 io_test->time, (uintmax_t)iops, (uintmax_t)mbps);
131 for (i = 0; i < io_test->num_threads; i++)
132 printf("\t%3d: %8ju IO/s\n", i,
133 (uintmax_t)io_test->io_completed[i]/io_test->time);
137 perftest(const struct cmd *f, int argc, char *argv[])
139 struct nvme_io_test io_test;
141 u_long ioctl_cmd = NVME_IO_TEST;
143 memset(&io_test, 0, sizeof(io_test));
144 if (arg_parse(argc, argv, f))
148 arg_help(argc, argv, f);
149 if (opt.flags != NULL && strcmp(opt.flags, "refthread") == 0)
150 io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
151 if (opt.intr != NULL) {
152 if (strcmp(opt.intr, "bio") == 0 ||
153 strcmp(opt.intr, "wait") == 0)
154 ioctl_cmd = NVME_BIO_TEST;
155 else if (strcmp(opt.intr, "io") == 0 ||
156 strcmp(opt.intr, "intr") == 0)
157 ioctl_cmd = NVME_IO_TEST;
159 fprintf(stderr, "Unknown interrupt test type %s\n", opt.intr);
160 arg_help(argc, argv, f);
163 if (opt.threads <= 0 || opt.threads > 128) {
164 fprintf(stderr, "Bad number of threads %d\n", opt.threads);
165 arg_help(argc, argv, f);
167 io_test.num_threads = opt.threads;
168 if (strcasecmp(opt.op, "read") == 0)
169 io_test.opc = NVME_OPC_READ;
170 else if (strcasecmp(opt.op, "write") == 0)
171 io_test.opc = NVME_OPC_WRITE;
173 fprintf(stderr, "\"%s\" not valid opcode.\n", opt.op);
174 arg_help(argc, argv, f);
177 fprintf(stderr, "No time speciifed\n");
178 arg_help(argc, argv, f);
180 io_test.time = opt.time;
181 io_test.size = opt.size;
182 open_dev(opt.dev, &fd, 1, 1);
183 if (ioctl(fd, ioctl_cmd, &io_test) < 0)
184 err(EX_IOERR, "ioctl NVME_IO_TEST failed");
187 print_perftest(&io_test, opt.perthread);