Home | History | Annotate | Line # | Download | only in nvmectl
perftest.c revision 1.2
      1 /*	$NetBSD: perftest.c,v 1.2 2016/07/14 10:02:43 nonaka Exp $	*/
      2 
      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 #ifndef lint
     31 __RCSID("$NetBSD: perftest.c,v 1.2 2016/07/14 10:02:43 nonaka Exp $");
     32 #if 0
     33 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/perftest.c 257531 2013-11-01 22:05:29Z jimharris $");
     34 #endif
     35 #endif
     36 
     37 #include <sys/param.h>
     38 #include <sys/ioccom.h>
     39 
     40 #include <ctype.h>
     41 #include <err.h>
     42 #include <fcntl.h>
     43 #include <inttypes.h>
     44 #include <stdbool.h>
     45 #include <stddef.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <unistd.h>
     50 
     51 #include "nvmectl.h"
     52 
     53 #ifdef PERFTEST_USAGE
     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_usage(void)
     79 {
     80 	fprintf(stderr, "usage:\n");
     81 	fprintf(stderr, PERFTEST_USAGE);
     82 	exit(1);
     83 }
     84 
     85 void
     86 perftest(int argc, char *argv[])
     87 {
     88 	struct nvme_io_test		io_test;
     89 	int				fd;
     90 	int				ch;
     91 	char				*p;
     92 	u_long				ioctl_cmd = NVME_IO_TEST;
     93 	bool				nflag, oflag, sflag, tflag;
     94 	int				perthread = 0;
     95 
     96 	nflag = oflag = sflag = tflag = false;
     97 
     98 	memset(&io_test, 0, sizeof(io_test));
     99 
    100 	while ((ch = getopt(argc, argv, "f:i:n:o:ps:t:")) != -1) {
    101 		switch (ch) {
    102 		case 'f':
    103 			if (!strcmp(optarg, "refthread"))
    104 				io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
    105 			break;
    106 		case 'i':
    107 			if (!strcmp(optarg, "bio") ||
    108 			    !strcmp(optarg, "wait"))
    109 				ioctl_cmd = NVME_BIO_TEST;
    110 			else if (!strcmp(optarg, "io") ||
    111 				 !strcmp(optarg, "intr"))
    112 				ioctl_cmd = NVME_IO_TEST;
    113 			break;
    114 		case 'n':
    115 			nflag = true;
    116 			io_test.num_threads = strtoul(optarg, &p, 0);
    117 			if (p != NULL && *p != '\0') {
    118 				fprintf(stderr,
    119 				    "\"%s\" not valid number of threads.\n",
    120 				    optarg);
    121 				perftest_usage();
    122 			} else if (io_test.num_threads == 0 ||
    123 				   io_test.num_threads > 128) {
    124 				fprintf(stderr,
    125 				    "\"%s\" not valid number of threads.\n",
    126 				    optarg);
    127 				perftest_usage();
    128 			}
    129 			break;
    130 		case 'o':
    131 			oflag = true;
    132 			if (!strcmp(optarg, "read") || !strcmp(optarg, "READ"))
    133 				io_test.opc = NVME_OPC_READ;
    134 			else if (!strcmp(optarg, "write") ||
    135 				 !strcmp(optarg, "WRITE"))
    136 				io_test.opc = NVME_OPC_WRITE;
    137 			else {
    138 				fprintf(stderr, "\"%s\" not valid opcode.\n",
    139 				    optarg);
    140 				perftest_usage();
    141 			}
    142 			break;
    143 		case 'p':
    144 			perthread = 1;
    145 			break;
    146 		case 's':
    147 			sflag = true;
    148 			io_test.size = strtoul(optarg, &p, 0);
    149 			if (p == NULL || *p == '\0' || toupper(*p) == 'B') {
    150 				// do nothing
    151 			} else if (toupper(*p) == 'K') {
    152 				io_test.size *= 1024;
    153 			} else if (toupper(*p) == 'M') {
    154 				io_test.size *= 1024 * 1024;
    155 			} else {
    156 				fprintf(stderr, "\"%s\" not valid size.\n",
    157 				    optarg);
    158 				perftest_usage();
    159 			}
    160 			break;
    161 		case 't':
    162 			tflag = true;
    163 			io_test.time = strtoul(optarg, &p, 0);
    164 			if (p != NULL && *p != '\0') {
    165 				fprintf(stderr,
    166 				    "\"%s\" not valid time duration.\n",
    167 				    optarg);
    168 				perftest_usage();
    169 			}
    170 			break;
    171 		}
    172 	}
    173 
    174 	if (!nflag || !oflag || !sflag || !tflag || optind >= argc)
    175 		perftest_usage();
    176 
    177 	open_dev(argv[optind], &fd, 1, 1);
    178 	if (ioctl(fd, ioctl_cmd, &io_test) < 0)
    179 		err(1, "ioctl NVME_IO_TEST failed");
    180 
    181 	close(fd);
    182 	print_perftest(&io_test, perthread);
    183 	exit(0);
    184 }
    185 #endif
    186