xref: /freebsd/sbin/nvmecontrol/perftest.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 <sysexits.h>
45 #include <unistd.h>
46 
47 #include "nvmecontrol.h"
48 
49 /* Tables for command line parsing */
50 
51 static cmd_fn_t perftest;
52 
53 #define NONE 0xffffffffu
54 static struct options {
55 	bool		perthread;
56 	uint32_t	threads;
57 	uint32_t	size;
58 	uint32_t	time;
59 	const char	*op;
60 	const char	*intr;
61 	const char	*flags;
62 	const char	*dev;
63 } opt = {
64 	.perthread = false,
65 	.threads = 0,
66 	.size = 0,
67 	.time = 0,
68 	.op = NULL,
69 	.intr = NULL,
70 	.flags = NULL,
71 	.dev = NULL,
72 };
73 
74 
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,
82 	    "Size of the test"),
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 }
92 };
93 #undef OPT
94 
95 static const struct args perftest_args[] = {
96 	{ arg_string, &opt.dev, "namespace-id" },
97 	{ arg_none, NULL, NULL },
98 };
99 
100 static struct cmd perftest_cmd = {
101 	.name = "perftest",
102 	.fn = perftest,
103 	.descr = "Perform low-level performance testing",
104 	.ctx_size = sizeof(opt),
105 	.opts = perftest_opts,
106 	.args = perftest_args,
107 };
108 
109 CMD_COMMAND(perftest_cmd);
110 
111 /* End of tables for command line parsing */
112 
113 static void
114 print_perftest(struct nvme_io_test *io_test, bool perthread)
115 {
116 	uint64_t	io_completed = 0, iops, mbps;
117 	uint32_t	i;
118 
119 	for (i = 0; i < io_test->num_threads; i++)
120 		io_completed += io_test->io_completed[i];
121 
122 	iops = io_completed/io_test->time;
123 	mbps = iops * io_test->size / (1024*1024);
124 
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);
129 
130 	if (perthread)
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);
134 }
135 
136 static void
137 perftest(const struct cmd *f, int argc, char *argv[])
138 {
139 	struct nvme_io_test		io_test;
140 	int				fd;
141 	u_long				ioctl_cmd = NVME_IO_TEST;
142 
143 	memset(&io_test, 0, sizeof(io_test));
144 	if (arg_parse(argc, argv, f))
145 		return;
146 
147 	if (opt.op == NULL)
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;
158 		else {
159 			fprintf(stderr, "Unknown interrupt test type %s\n", opt.intr);
160 			arg_help(argc, argv, f);
161 		}
162 	}
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);
166 	}
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;
172 	else {
173 		fprintf(stderr, "\"%s\" not valid opcode.\n", opt.op);
174 		arg_help(argc, argv, f);
175 	}
176 	if (opt.time == 0) {
177 		fprintf(stderr, "No time speciifed\n");
178 		arg_help(argc, argv, f);
179 	}
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");
185 
186 	close(fd);
187 	print_perftest(&io_test, opt.perthread);
188 	exit(0);
189 }
190