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>
46 #include "nvmecontrol.h"
48 /* Tables for command line parsing */
50 static cmd_fn_t perftest;
52 #define NONE 0xffffffffu
53 static struct options {
74 static const struct opts perftest_opts[] = {
75 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
76 OPT("perthread", 'p', arg_none, opt, perthread,
77 "Report per-thread results"),
78 OPT("threads", 'n', arg_uint32, opt, threads,
79 "Number of threads to run"),
80 OPT("size", 's', arg_uint32, opt, size,
82 OPT("time", 't', arg_uint32, opt, time,
83 "How long to run the test in seconds"),
84 OPT("operation", 'o', arg_string, opt, op,
85 "Operation type: 'read' or 'write'"),
86 OPT("interrupt", 'i', arg_string, opt, intr,
87 "Interrupt mode: 'intr' or 'wait'"),
88 OPT("flags", 'f', arg_string, opt, flags,
89 "Turn on testing flags: refthread"),
90 { NULL, 0, arg_none, NULL, NULL }
94 static const struct args perftest_args[] = {
95 { arg_string, &opt.dev, "namespace-id" },
96 { arg_none, NULL, NULL },
99 static struct cmd perftest_cmd = {
102 .descr = "Perform low-level performance testing",
103 .ctx_size = sizeof(opt),
104 .opts = perftest_opts,
105 .args = perftest_args,
108 CMD_COMMAND(perftest_cmd);
110 /* End of tables for command line parsing */
113 print_perftest(struct nvme_io_test *io_test, bool perthread)
115 uint64_t io_completed = 0, iops, mbps;
118 for (i = 0; i < io_test->num_threads; i++)
119 io_completed += io_test->io_completed[i];
121 iops = io_completed/io_test->time;
122 mbps = iops * io_test->size / (1024*1024);
124 printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7ju MB/s: %4ju\n",
125 io_test->num_threads, io_test->size,
126 io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
127 io_test->time, (uintmax_t)iops, (uintmax_t)mbps);
130 for (i = 0; i < io_test->num_threads; i++)
131 printf("\t%3d: %8ju IO/s\n", i,
132 (uintmax_t)io_test->io_completed[i]/io_test->time);
136 perftest(const struct cmd *f, int argc, char *argv[])
138 struct nvme_io_test io_test;
140 u_long ioctl_cmd = NVME_IO_TEST;
142 memset(&io_test, 0, sizeof(io_test));
143 if (arg_parse(argc, argv, f))
147 arg_help(argc, argv, f);
148 if (opt.flags != NULL && strcmp(opt.flags, "refthread") == 0)
149 io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
150 if (opt.intr != NULL) {
151 if (strcmp(opt.intr, "bio") == 0 ||
152 strcmp(opt.intr, "wait") == 0)
153 ioctl_cmd = NVME_BIO_TEST;
154 else if (strcmp(opt.intr, "io") == 0 ||
155 strcmp(opt.intr, "intr") == 0)
156 ioctl_cmd = NVME_IO_TEST;
158 fprintf(stderr, "Unknown interrupt test type %s\n", opt.intr);
159 arg_help(argc, argv, f);
162 if (opt.threads <= 0 || opt.threads > 128) {
163 fprintf(stderr, "Bad number of threads %d\n", opt.threads);
164 arg_help(argc, argv, f);
166 io_test.num_threads = opt.threads;
167 if (strcasecmp(opt.op, "read") == 0)
168 io_test.opc = NVME_OPC_READ;
169 else if (strcasecmp(opt.op, "write") == 0)
170 io_test.opc = NVME_OPC_WRITE;
172 fprintf(stderr, "\"%s\" not valid opcode.\n", opt.op);
173 arg_help(argc, argv, f);
176 fprintf(stderr, "No time speciifed\n");
177 arg_help(argc, argv, f);
179 io_test.time = opt.time;
180 io_test.size = opt.size;
181 open_dev(opt.dev, &fd, 1, 1);
182 if (ioctl(fd, ioctl_cmd, &io_test) < 0)
183 err(1, "ioctl NVME_IO_TEST failed");
186 print_perftest(&io_test, opt.perthread);