xref: /freebsd/sbin/nvmecontrol/perftest.c (revision 32e86a82f54826f14ea381affa6674db3aa3b5ae)
1bbc1ac63SJim Harris /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4bbc1ac63SJim Harris  * Copyright (C) 2012-2013 Intel Corporation
5bbc1ac63SJim Harris  * All rights reserved.
6bbc1ac63SJim Harris  *
7bbc1ac63SJim Harris  * Redistribution and use in source and binary forms, with or without
8bbc1ac63SJim Harris  * modification, are permitted provided that the following conditions
9bbc1ac63SJim Harris  * are met:
10bbc1ac63SJim Harris  * 1. Redistributions of source code must retain the above copyright
11bbc1ac63SJim Harris  *    notice, this list of conditions and the following disclaimer.
12bbc1ac63SJim Harris  * 2. Redistributions in binary form must reproduce the above copyright
13bbc1ac63SJim Harris  *    notice, this list of conditions and the following disclaimer in the
14bbc1ac63SJim Harris  *    documentation and/or other materials provided with the distribution.
15bbc1ac63SJim Harris  *
16bbc1ac63SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bbc1ac63SJim Harris  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bbc1ac63SJim Harris  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bbc1ac63SJim Harris  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bbc1ac63SJim Harris  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bbc1ac63SJim Harris  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bbc1ac63SJim Harris  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bbc1ac63SJim Harris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bbc1ac63SJim Harris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bbc1ac63SJim Harris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bbc1ac63SJim Harris  * SUCH DAMAGE.
27bbc1ac63SJim Harris  */
28bbc1ac63SJim Harris 
29bbc1ac63SJim Harris #include <sys/param.h>
30bbc1ac63SJim Harris #include <sys/ioccom.h>
31bbc1ac63SJim Harris 
32bbc1ac63SJim Harris #include <ctype.h>
33821ef73cSJim Harris #include <err.h>
34bbc1ac63SJim Harris #include <fcntl.h>
35992db80fSJim Harris #include <inttypes.h>
36bbc1ac63SJim Harris #include <stdbool.h>
37bbc1ac63SJim Harris #include <stddef.h>
38bbc1ac63SJim Harris #include <stdio.h>
39bbc1ac63SJim Harris #include <stdlib.h>
40bbc1ac63SJim Harris #include <string.h>
415dc463f9SAlexander Motin #include <sysexits.h>
42bbc1ac63SJim Harris #include <unistd.h>
43bbc1ac63SJim Harris 
44bbc1ac63SJim Harris #include "nvmecontrol.h"
45bbc1ac63SJim Harris 
46f634b4c1SWarner Losh /* Tables for command line parsing */
47f634b4c1SWarner Losh 
48f634b4c1SWarner Losh static cmd_fn_t perftest;
49f634b4c1SWarner Losh 
50f634b4c1SWarner Losh #define NONE 0xffffffffu
51f634b4c1SWarner Losh static struct options {
52f634b4c1SWarner Losh 	bool		perthread;
53f634b4c1SWarner Losh 	uint32_t	threads;
54f634b4c1SWarner Losh 	uint32_t	size;
55f634b4c1SWarner Losh 	uint32_t	time;
56f634b4c1SWarner Losh 	const char	*op;
57f634b4c1SWarner Losh 	const char	*intr;
58f634b4c1SWarner Losh 	const char	*flags;
59f634b4c1SWarner Losh 	const char	*dev;
60f634b4c1SWarner Losh } opt = {
61f634b4c1SWarner Losh 	.perthread = false,
62f634b4c1SWarner Losh 	.threads = 0,
63f634b4c1SWarner Losh 	.size = 0,
64f634b4c1SWarner Losh 	.time = 0,
65f634b4c1SWarner Losh 	.op = NULL,
66f634b4c1SWarner Losh 	.intr = NULL,
67f634b4c1SWarner Losh 	.flags = NULL,
68f634b4c1SWarner Losh 	.dev = NULL,
69f634b4c1SWarner Losh };
70f634b4c1SWarner Losh 
71f634b4c1SWarner Losh 
72f634b4c1SWarner Losh static const struct opts perftest_opts[] = {
73f634b4c1SWarner Losh #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
74f634b4c1SWarner Losh 	OPT("perthread", 'p', arg_none, opt, perthread,
75f634b4c1SWarner Losh 	    "Report per-thread results"),
76f634b4c1SWarner Losh 	OPT("threads", 'n', arg_uint32, opt, threads,
77f634b4c1SWarner Losh 	    "Number of threads to run"),
78f634b4c1SWarner Losh 	OPT("size", 's', arg_uint32, opt, size,
79f634b4c1SWarner Losh 	    "Size of the test"),
80f634b4c1SWarner Losh 	OPT("time", 't', arg_uint32, opt, time,
81f634b4c1SWarner Losh 	    "How long to run the test in seconds"),
82f634b4c1SWarner Losh 	OPT("operation", 'o', arg_string, opt, op,
83f634b4c1SWarner Losh 	    "Operation type: 'read' or 'write'"),
84f634b4c1SWarner Losh 	OPT("interrupt", 'i', arg_string, opt, intr,
85f634b4c1SWarner Losh 	    "Interrupt mode: 'intr' or 'wait'"),
86f634b4c1SWarner Losh 	OPT("flags", 'f', arg_string, opt, flags,
87f634b4c1SWarner Losh 	    "Turn on testing flags: refthread"),
88f634b4c1SWarner Losh 	{ NULL, 0, arg_none, NULL, NULL }
89f634b4c1SWarner Losh };
90f634b4c1SWarner Losh #undef OPT
91f634b4c1SWarner Losh 
92f634b4c1SWarner Losh static const struct args perftest_args[] = {
93f634b4c1SWarner Losh 	{ arg_string, &opt.dev, "namespace-id" },
94f634b4c1SWarner Losh 	{ arg_none, NULL, NULL },
95f634b4c1SWarner Losh };
96f634b4c1SWarner Losh 
97f634b4c1SWarner Losh static struct cmd perftest_cmd = {
98f634b4c1SWarner Losh 	.name = "perftest",
99f634b4c1SWarner Losh 	.fn = perftest,
100e843651bSAlexander Motin 	.descr = "Perform low-level performance testing",
101f634b4c1SWarner Losh 	.ctx_size = sizeof(opt),
102f634b4c1SWarner Losh 	.opts = perftest_opts,
103f634b4c1SWarner Losh 	.args = perftest_args,
104f634b4c1SWarner Losh };
105f634b4c1SWarner Losh 
106f634b4c1SWarner Losh CMD_COMMAND(perftest_cmd);
107f634b4c1SWarner Losh 
108f634b4c1SWarner Losh /* End of tables for command line parsing */
109a13a291aSWarner Losh 
110bbc1ac63SJim Harris static void
print_perftest(struct nvme_io_test * io_test,bool perthread)111bbc1ac63SJim Harris print_perftest(struct nvme_io_test *io_test, bool perthread)
112bbc1ac63SJim Harris {
113992db80fSJim Harris 	uint64_t	io_completed = 0, iops, mbps;
114992db80fSJim Harris 	uint32_t	i;
115bbc1ac63SJim Harris 
116bbc1ac63SJim Harris 	for (i = 0; i < io_test->num_threads; i++)
117bbc1ac63SJim Harris 		io_completed += io_test->io_completed[i];
118bbc1ac63SJim Harris 
119bbc1ac63SJim Harris 	iops = io_completed/io_test->time;
120bbc1ac63SJim Harris 	mbps = iops * io_test->size / (1024*1024);
121bbc1ac63SJim Harris 
122992db80fSJim Harris 	printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7ju MB/s: %4ju\n",
123bbc1ac63SJim Harris 	    io_test->num_threads, io_test->size,
124bbc1ac63SJim Harris 	    io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
125992db80fSJim Harris 	    io_test->time, (uintmax_t)iops, (uintmax_t)mbps);
126bbc1ac63SJim Harris 
127bbc1ac63SJim Harris 	if (perthread)
128bbc1ac63SJim Harris 		for (i = 0; i < io_test->num_threads; i++)
129992db80fSJim Harris 			printf("\t%3d: %8ju IO/s\n", i,
130992db80fSJim Harris 			    (uintmax_t)io_test->io_completed[i]/io_test->time);
131bbc1ac63SJim Harris }
132bbc1ac63SJim Harris 
133bbc1ac63SJim Harris static void
perftest(const struct cmd * f,int argc,char * argv[])134f634b4c1SWarner Losh perftest(const struct cmd *f, int argc, char *argv[])
135bbc1ac63SJim Harris {
136bbc1ac63SJim Harris 	struct nvme_io_test		io_test;
137bbc1ac63SJim Harris 	int				fd;
138bbc1ac63SJim Harris 	u_long				ioctl_cmd = NVME_IO_TEST;
139bbc1ac63SJim Harris 
140bbc1ac63SJim Harris 	memset(&io_test, 0, sizeof(io_test));
141f634b4c1SWarner Losh 	if (arg_parse(argc, argv, f))
142f634b4c1SWarner Losh 		return;
143bbc1ac63SJim Harris 
144087d0e0bSWarner Losh 	if (opt.op == NULL)
145f634b4c1SWarner Losh 		arg_help(argc, argv, f);
146087d0e0bSWarner Losh 	if (opt.flags != NULL && strcmp(opt.flags, "refthread") == 0)
147bbc1ac63SJim Harris 		io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
148f634b4c1SWarner Losh 	if (opt.intr != NULL) {
149f634b4c1SWarner Losh 		if (strcmp(opt.intr, "bio") == 0 ||
150f634b4c1SWarner Losh 		    strcmp(opt.intr, "wait") == 0)
151bbc1ac63SJim Harris 			ioctl_cmd = NVME_BIO_TEST;
152f634b4c1SWarner Losh 		else if (strcmp(opt.intr, "io") == 0 ||
153f634b4c1SWarner Losh 		    strcmp(opt.intr, "intr") == 0)
154bbc1ac63SJim Harris 			ioctl_cmd = NVME_IO_TEST;
155f634b4c1SWarner Losh 		else {
156f634b4c1SWarner Losh 			fprintf(stderr, "Unknown interrupt test type %s\n", opt.intr);
157f634b4c1SWarner Losh 			arg_help(argc, argv, f);
158bbc1ac63SJim Harris 		}
159f634b4c1SWarner Losh 	}
160f634b4c1SWarner Losh 	if (opt.threads <= 0 || opt.threads > 128) {
161f634b4c1SWarner Losh 		fprintf(stderr, "Bad number of threads %d\n", opt.threads);
162f634b4c1SWarner Losh 		arg_help(argc, argv, f);
163f634b4c1SWarner Losh 	}
164087d0e0bSWarner Losh 	io_test.num_threads = opt.threads;
165f634b4c1SWarner Losh 	if (strcasecmp(opt.op, "read") == 0)
166bbc1ac63SJim Harris 		io_test.opc = NVME_OPC_READ;
167f634b4c1SWarner Losh 	else if (strcasecmp(opt.op, "write") == 0)
168bbc1ac63SJim Harris 		io_test.opc = NVME_OPC_WRITE;
169bbc1ac63SJim Harris 	else {
170f634b4c1SWarner Losh 		fprintf(stderr, "\"%s\" not valid opcode.\n", opt.op);
171f634b4c1SWarner Losh 		arg_help(argc, argv, f);
172bbc1ac63SJim Harris 	}
173f634b4c1SWarner Losh 	if (opt.time == 0) {
174f634b4c1SWarner Losh 		fprintf(stderr, "No time speciifed\n");
175f634b4c1SWarner Losh 		arg_help(argc, argv, f);
176bbc1ac63SJim Harris 	}
177f634b4c1SWarner Losh 	io_test.time = opt.time;
178087d0e0bSWarner Losh 	io_test.size = opt.size;
179f634b4c1SWarner Losh 	open_dev(opt.dev, &fd, 1, 1);
180821ef73cSJim Harris 	if (ioctl(fd, ioctl_cmd, &io_test) < 0)
1815dc463f9SAlexander Motin 		err(EX_IOERR, "ioctl NVME_IO_TEST failed");
182bbc1ac63SJim Harris 
183bbc1ac63SJim Harris 	close(fd);
184f634b4c1SWarner Losh 	print_perftest(&io_test, opt.perthread);
185821ef73cSJim Harris 	exit(0);
186bbc1ac63SJim Harris }
187