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