xref: /freebsd/sbin/nvmecontrol/perftest.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/ioccom.h>
34 
35 #include <ctype.h>
36 #include <err.h>
37 #include <fcntl.h>
38 #include <inttypes.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "nvmecontrol.h"
47 
48 /* Tables for command line parsing */
49 
50 static cmd_fn_t perftest;
51 
52 #define NONE 0xffffffffu
53 static struct options {
54 	bool		perthread;
55 	uint32_t	threads;
56 	uint32_t	size;
57 	uint32_t	time;
58 	const char	*op;
59 	const char	*intr;
60 	const char	*flags;
61 	const char	*dev;
62 } opt = {
63 	.perthread = false,
64 	.threads = 0,
65 	.size = 0,
66 	.time = 0,
67 	.op = NULL,
68 	.intr = NULL,
69 	.flags = NULL,
70 	.dev = NULL,
71 };
72 
73 
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,
81 	    "Size of the test"),
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 }
91 };
92 #undef OPT
93 
94 static const struct args perftest_args[] = {
95 	{ arg_string, &opt.dev, "namespace-id" },
96 	{ arg_none, NULL, NULL },
97 };
98 
99 static struct cmd perftest_cmd = {
100 	.name = "perftest",
101 	.fn = perftest,
102 	.descr = "Perform low-level performance testing",
103 	.ctx_size = sizeof(opt),
104 	.opts = perftest_opts,
105 	.args = perftest_args,
106 };
107 
108 CMD_COMMAND(perftest_cmd);
109 
110 /* End of tables for command line parsing */
111 
112 static void
113 print_perftest(struct nvme_io_test *io_test, bool perthread)
114 {
115 	uint64_t	io_completed = 0, iops, mbps;
116 	uint32_t	i;
117 
118 	for (i = 0; i < io_test->num_threads; i++)
119 		io_completed += io_test->io_completed[i];
120 
121 	iops = io_completed/io_test->time;
122 	mbps = iops * io_test->size / (1024*1024);
123 
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);
128 
129 	if (perthread)
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);
133 }
134 
135 static void
136 perftest(const struct cmd *f, int argc, char *argv[])
137 {
138 	struct nvme_io_test		io_test;
139 	int				fd;
140 	u_long				ioctl_cmd = NVME_IO_TEST;
141 
142 	memset(&io_test, 0, sizeof(io_test));
143 	if (arg_parse(argc, argv, f))
144 		return;
145 
146 	if (opt.op == NULL)
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;
157 		else {
158 			fprintf(stderr, "Unknown interrupt test type %s\n", opt.intr);
159 			arg_help(argc, argv, f);
160 		}
161 	}
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);
165 	}
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;
171 	else {
172 		fprintf(stderr, "\"%s\" not valid opcode.\n", opt.op);
173 		arg_help(argc, argv, f);
174 	}
175 	if (opt.time == 0) {
176 		fprintf(stderr, "No time speciifed\n");
177 		arg_help(argc, argv, f);
178 	}
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");
184 
185 	close(fd);
186 	print_perftest(&io_test, opt.perthread);
187 	exit(0);
188 }
189