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