xref: /freebsd/sbin/nvmecontrol/perftest.c (revision 8df8b2d3e51d1b816201d8a1fe8bc29fe192e562)
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 #define PERFTEST_USAGE							       \
49 	"perftest <-n num_threads> <-o read|write>\n"	       \
50 	"         <-s size_in_bytes> <-t time_in_seconds>\n"	       \
51 	"         <-i intr|wait> [-f refthread] [-p]\n"	       \
52 	"         <namespace id>\n"
53 
54 static void
55 print_perftest(struct nvme_io_test *io_test, bool perthread)
56 {
57 	uint64_t	io_completed = 0, iops, mbps;
58 	uint32_t	i;
59 
60 	for (i = 0; i < io_test->num_threads; i++)
61 		io_completed += io_test->io_completed[i];
62 
63 	iops = io_completed/io_test->time;
64 	mbps = iops * io_test->size / (1024*1024);
65 
66 	printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7ju MB/s: %4ju\n",
67 	    io_test->num_threads, io_test->size,
68 	    io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
69 	    io_test->time, (uintmax_t)iops, (uintmax_t)mbps);
70 
71 	if (perthread)
72 		for (i = 0; i < io_test->num_threads; i++)
73 			printf("\t%3d: %8ju IO/s\n", i,
74 			    (uintmax_t)io_test->io_completed[i]/io_test->time);
75 }
76 
77 static void
78 perftest(const struct nvme_function *nf, int argc, char *argv[])
79 {
80 	struct nvme_io_test		io_test;
81 	int				fd;
82 	int				opt;
83 	char				*p;
84 	u_long				ioctl_cmd = NVME_IO_TEST;
85 	bool				nflag, oflag, sflag, tflag;
86 	int				perthread = 0;
87 
88 	nflag = oflag = sflag = tflag = false;
89 
90 	memset(&io_test, 0, sizeof(io_test));
91 
92 	while ((opt = getopt(argc, argv, "f:i:n:o:ps:t:")) != -1) {
93 		switch (opt) {
94 		case 'f':
95 			if (!strcmp(optarg, "refthread"))
96 				io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
97 			break;
98 		case 'i':
99 			if (!strcmp(optarg, "bio") ||
100 			    !strcmp(optarg, "wait"))
101 				ioctl_cmd = NVME_BIO_TEST;
102 			else if (!strcmp(optarg, "io") ||
103 				 !strcmp(optarg, "intr"))
104 				ioctl_cmd = NVME_IO_TEST;
105 			break;
106 		case 'n':
107 			nflag = true;
108 			io_test.num_threads = strtoul(optarg, &p, 0);
109 			if (p != NULL && *p != '\0') {
110 				fprintf(stderr,
111 				    "\"%s\" not valid number of threads.\n",
112 				    optarg);
113 				usage(nf);
114 			} else if (io_test.num_threads == 0 ||
115 				   io_test.num_threads > 128) {
116 				fprintf(stderr,
117 				    "\"%s\" not valid number of threads.\n",
118 				    optarg);
119 				usage(nf);
120 			}
121 			break;
122 		case 'o':
123 			oflag = true;
124 			if (!strcmp(optarg, "read") || !strcmp(optarg, "READ"))
125 				io_test.opc = NVME_OPC_READ;
126 			else if (!strcmp(optarg, "write") ||
127 				 !strcmp(optarg, "WRITE"))
128 				io_test.opc = NVME_OPC_WRITE;
129 			else {
130 				fprintf(stderr, "\"%s\" not valid opcode.\n",
131 				    optarg);
132 				usage(nf);
133 			}
134 			break;
135 		case 'p':
136 			perthread = 1;
137 			break;
138 		case 's':
139 			sflag = true;
140 			io_test.size = strtoul(optarg, &p, 0);
141 			if (p == NULL || *p == '\0' || toupper(*p) == 'B') {
142 				// do nothing
143 			} else if (toupper(*p) == 'K') {
144 				io_test.size *= 1024;
145 			} else if (toupper(*p) == 'M') {
146 				io_test.size *= 1024 * 1024;
147 			} else {
148 				fprintf(stderr, "\"%s\" not valid size.\n",
149 				    optarg);
150 				usage(nf);
151 			}
152 			break;
153 		case 't':
154 			tflag = true;
155 			io_test.time = strtoul(optarg, &p, 0);
156 			if (p != NULL && *p != '\0') {
157 				fprintf(stderr,
158 				    "\"%s\" not valid time duration.\n",
159 				    optarg);
160 				usage(nf);
161 			}
162 			break;
163 		}
164 	}
165 
166 	if (!nflag || !oflag || !sflag || !tflag || optind >= argc)
167 		usage(nf);
168 
169 
170 	open_dev(argv[optind], &fd, 1, 1);
171 	if (ioctl(fd, ioctl_cmd, &io_test) < 0)
172 		err(1, "ioctl NVME_IO_TEST failed");
173 
174 	close(fd);
175 	print_perftest(&io_test, perthread);
176 	exit(0);
177 }
178 
179 NVME_COMMAND(top, perftest, perftest, PERFTEST_USAGE);
180