xref: /freebsd/sbin/nvmecontrol/perftest.c (revision 51e235148a4becba94e824a44bd69687644a7f56)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2012-2013 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  * 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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <fcntl.h>
36 #include <inttypes.h>
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sysexits.h>
43 #include <unistd.h>
44 
45 #include "nvmecontrol.h"
46 
47 /* Tables for command line parsing */
48 
49 static cmd_fn_t perftest;
50 
51 #define NONE 0xffffffffu
52 static struct options {
53 	bool		perthread;
54 	uint32_t	threads;
55 	uint32_t	size;
56 	uint32_t	time;
57 	const char	*op;
58 	const char	*intr;
59 	const char	*flags;
60 	const char	*dev;
61 } opt = {
62 	.perthread = false,
63 	.threads = 0,
64 	.size = 0,
65 	.time = 0,
66 	.op = NULL,
67 	.intr = NULL,
68 	.flags = NULL,
69 	.dev = NULL,
70 };
71 
72 
73 static const struct opts perftest_opts[] = {
74 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
75 	OPT("perthread", 'p', arg_none, opt, perthread,
76 	    "Report per-thread results"),
77 	OPT("threads", 'n', arg_uint32, opt, threads,
78 	    "Number of threads to run"),
79 	OPT("size", 's', arg_uint32, opt, size,
80 	    "Size of the test"),
81 	OPT("time", 't', arg_uint32, opt, time,
82 	    "How long to run the test in seconds"),
83 	OPT("operation", 'o', arg_string, opt, op,
84 	    "Operation type: 'read' or 'write'"),
85 	OPT("interrupt", 'i', arg_string, opt, intr,
86 	    "Interrupt mode: 'intr' or 'wait'"),
87 	OPT("flags", 'f', arg_string, opt, flags,
88 	    "Turn on testing flags: refthread"),
89 	{ NULL, 0, arg_none, NULL, NULL }
90 };
91 #undef OPT
92 
93 static const struct args perftest_args[] = {
94 	{ arg_string, &opt.dev, "namespace-id" },
95 	{ arg_none, NULL, NULL },
96 };
97 
98 static struct cmd perftest_cmd = {
99 	.name = "perftest",
100 	.fn = perftest,
101 	.descr = "Perform low-level performance testing",
102 	.ctx_size = sizeof(opt),
103 	.opts = perftest_opts,
104 	.args = perftest_args,
105 };
106 
107 CMD_COMMAND(perftest_cmd);
108 
109 /* End of tables for command line parsing */
110 
111 static void
112 print_perftest(struct nvme_io_test *io_test, bool perthread)
113 {
114 	uint64_t	io_completed = 0, iops, mbps;
115 	uint32_t	i;
116 
117 	for (i = 0; i < io_test->num_threads; i++)
118 		io_completed += io_test->io_completed[i];
119 
120 	iops = io_completed/io_test->time;
121 	mbps = iops * io_test->size / (1024*1024);
122 
123 	printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7ju MB/s: %4ju\n",
124 	    io_test->num_threads, io_test->size,
125 	    io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
126 	    io_test->time, (uintmax_t)iops, (uintmax_t)mbps);
127 
128 	if (perthread)
129 		for (i = 0; i < io_test->num_threads; i++)
130 			printf("\t%3d: %8ju IO/s\n", i,
131 			    (uintmax_t)io_test->io_completed[i]/io_test->time);
132 }
133 
134 static void
135 perftest(const struct cmd *f, int argc, char *argv[])
136 {
137 	struct nvme_io_test		io_test;
138 	int				fd;
139 	u_long				ioctl_cmd = NVME_IO_TEST;
140 
141 	memset(&io_test, 0, sizeof(io_test));
142 	if (arg_parse(argc, argv, f))
143 		return;
144 
145 	if (opt.op == NULL)
146 		arg_help(argc, argv, f);
147 	if (opt.flags != NULL && strcmp(opt.flags, "refthread") == 0)
148 		io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
149 	if (opt.intr != NULL) {
150 		if (strcmp(opt.intr, "bio") == 0 ||
151 		    strcmp(opt.intr, "wait") == 0)
152 			ioctl_cmd = NVME_BIO_TEST;
153 		else if (strcmp(opt.intr, "io") == 0 ||
154 		    strcmp(opt.intr, "intr") == 0)
155 			ioctl_cmd = NVME_IO_TEST;
156 		else {
157 			fprintf(stderr, "Unknown interrupt test type %s\n", opt.intr);
158 			arg_help(argc, argv, f);
159 		}
160 	}
161 	if (opt.threads <= 0 || opt.threads > 128) {
162 		fprintf(stderr, "Bad number of threads %d\n", opt.threads);
163 		arg_help(argc, argv, f);
164 	}
165 	io_test.num_threads = opt.threads;
166 	if (strcasecmp(opt.op, "read") == 0)
167 		io_test.opc = NVME_OPC_READ;
168 	else if (strcasecmp(opt.op, "write") == 0)
169 		io_test.opc = NVME_OPC_WRITE;
170 	else {
171 		fprintf(stderr, "\"%s\" not valid opcode.\n", opt.op);
172 		arg_help(argc, argv, f);
173 	}
174 	if (opt.time == 0) {
175 		fprintf(stderr, "No time speciifed\n");
176 		arg_help(argc, argv, f);
177 	}
178 	io_test.time = opt.time;
179 	io_test.size = opt.size;
180 	open_dev(opt.dev, &fd, 1, 1);
181 	if (ioctl(fd, ioctl_cmd, &io_test) < 0)
182 		err(EX_IOERR, "ioctl NVME_IO_TEST failed");
183 
184 	close(fd);
185 	print_perftest(&io_test, opt.perthread);
186 	exit(0);
187 }
188