2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
45 #include "nvmecontrol.h"
47 static void perftest_usage(void);
52 fprintf(stderr, "usage:\n");
53 fprintf(stderr, DEVLIST_USAGE);
54 fprintf(stderr, IDENTIFY_USAGE);
55 fprintf(stderr, RESET_USAGE);
56 fprintf(stderr, PERFTEST_USAGE);
61 read_controller_data(int fd, struct nvme_controller_data *cdata)
63 struct nvme_pt_command pt;
65 memset(&pt, 0, sizeof(pt));
66 pt.cmd.opc = NVME_OPC_IDENTIFY;
69 pt.len = sizeof(*cdata);
72 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
73 printf("Identify request failed. errno=%d (%s)\n",
74 errno, strerror(errno));
78 if (nvme_completion_is_error(&pt.cpl)) {
79 printf("Passthrough command returned error.\n");
85 read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata)
87 struct nvme_pt_command pt;
89 memset(&pt, 0, sizeof(pt));
90 pt.cmd.opc = NVME_OPC_IDENTIFY;
93 pt.len = sizeof(*nsdata);
96 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
97 printf("Identify request failed. errno=%d (%s)\n",
98 errno, strerror(errno));
102 if (nvme_completion_is_error(&pt.cpl)) {
103 printf("Passthrough command returned error.\n");
109 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
114 snprintf(full_path, sizeof(full_path), "/dev/%s", str);
115 if (stat(full_path, &devstat) != 0) {
117 fprintf(stderr, "error\n");
124 *fd = open(full_path, O_RDWR);
127 printf("Could not open %s. errno=%d (%s)\n", full_path,
128 errno, strerror(errno));
139 print_perftest(struct nvme_io_test *io_test, bool perthread)
141 uint32_t i, io_completed = 0, iops, mbps;
143 for (i = 0; i < io_test->num_threads; i++)
144 io_completed += io_test->io_completed[i];
146 iops = io_completed/io_test->time;
147 mbps = iops * io_test->size / (1024*1024);
149 printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7d MB/s: %4d\n",
150 io_test->num_threads, io_test->size,
151 io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
152 io_test->time, iops, mbps);
155 for (i = 0; i < io_test->num_threads; i++)
156 printf("\t%3d: %8d IO/s\n", i,
157 io_test->io_completed[i]/io_test->time);
165 fprintf(stderr, "usage:\n");
166 fprintf(stderr, PERFTEST_USAGE);
171 perftest(int argc, char *argv[])
173 struct nvme_io_test io_test;
177 u_long ioctl_cmd = NVME_IO_TEST;
178 bool nflag, oflag, sflag, tflag;
181 nflag = oflag = sflag = tflag = false;
183 memset(&io_test, 0, sizeof(io_test));
185 while ((ch = getopt(argc, argv, "f:i:n:o:ps:t:")) != -1) {
188 if (!strcmp(optarg, "refthread"))
189 io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
192 if (!strcmp(optarg, "bio") ||
193 !strcmp(optarg, "wait"))
194 ioctl_cmd = NVME_BIO_TEST;
195 else if (!strcmp(optarg, "io") ||
196 !strcmp(optarg, "intr"))
197 ioctl_cmd = NVME_IO_TEST;
201 io_test.num_threads = strtoul(optarg, &p, 0);
202 if (p != NULL && *p != '\0') {
204 "\"%s\" not valid number of threads.\n",
207 } else if (io_test.num_threads == 0 ||
208 io_test.num_threads > 128) {
210 "\"%s\" not valid number of threads.\n",
217 if (!strcmp(optarg, "read") || !strcmp(optarg, "READ"))
218 io_test.opc = NVME_OPC_READ;
219 else if (!strcmp(optarg, "write") ||
220 !strcmp(optarg, "WRITE"))
221 io_test.opc = NVME_OPC_WRITE;
223 fprintf(stderr, "\"%s\" not valid opcode.\n",
233 io_test.size = strtoul(optarg, &p, 0);
234 if (p == NULL || *p == '\0' || toupper(*p) == 'B') {
236 } else if (toupper(*p) == 'K') {
237 io_test.size *= 1024;
238 } else if (toupper(*p) == 'M') {
239 io_test.size *= 1024 * 1024;
241 fprintf(stderr, "\"%s\" not valid size.\n",
248 io_test.time = strtoul(optarg, &p, 0);
249 if (p != NULL && *p != '\0') {
251 "\"%s\" not valid time duration.\n",
259 if (!nflag || !oflag || !sflag || !tflag || optind >= argc)
262 open_dev(argv[optind], &fd, 1, 1);
263 if (ioctl(fd, ioctl_cmd, &io_test) < 0) {
264 fprintf(stderr, "NVME_IO_TEST failed. errno=%d (%s)\n", errno,
271 print_perftest(&io_test, perthread);
278 fprintf(stderr, "usage:\n");
279 fprintf(stderr, RESET_USAGE);
284 reset_ctrlr(int argc, char *argv[])
288 while ((ch = getopt(argc, argv, "")) != -1) {
295 open_dev(argv[optind], &fd, 1, 1);
296 if (ioctl(fd, NVME_RESET_CONTROLLER) < 0) {
297 printf("Reset request to %s failed. errno=%d (%s)\n",
298 argv[optind], errno, strerror(errno));
306 main(int argc, char *argv[])
312 if (strcmp(argv[1], "devlist") == 0)
313 devlist(argc-1, &argv[1]);
314 else if (strcmp(argv[1], "identify") == 0)
315 identify(argc-1, &argv[1]);
316 else if (strcmp(argv[1], "perftest") == 0)
317 perftest(argc-1, &argv[1]);
318 else if (strcmp(argv[1], "reset") == 0)
319 reset_ctrlr(argc-1, &argv[1]);