1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
#include "atomic.h"
#include "bfs.h"
#include "bfstd.h"
#include "diag.h"
#include "ioq.h"
#include "sighook.h"
#include "xtime.h"
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
/** Which clock to use for benchmarking. */
static clockid_t clockid = CLOCK_REALTIME;
/** Get a current time measurement. */
static void gettime(struct timespec *tp) {
int ret = clock_gettime(clockid, tp);
bfs_everify(ret == 0, "clock_gettime(%d)", (int)clockid);
}
/**
* Time measurements.
*/
struct times {
/** The start time. */
struct timespec start;
/** Total requests started. */
size_t pushed;
/** Total requests finished. */
size_t popped;
/** Number of timed requests (latency). */
size_t timed_reqs;
/** The start time for the currently tracked request. */
struct timespec req_start;
/** Whether a timed request is currently in flight. */
bool timing;
/** Latency measurements. */
struct {
struct timespec min;
struct timespec max;
struct timespec sum;
} latency;
};
/** Initialize a timer. */
static void times_init(struct times *times) {
*times = (struct times) {
.latency = {
.min = { .tv_sec = 1000 },
},
};
gettime(×->start);
}
/** Start timing a single request. */
static void start_request(struct times *times) {
gettime(×->req_start);
times->timing = true;
}
/** Finish timing a request. */
static void finish_request(struct times *times) {
struct timespec elapsed;
gettime(&elapsed);
timespec_sub(&elapsed, ×->req_start);
timespec_min(×->latency.min, &elapsed);
timespec_max(×->latency.max, &elapsed);
timespec_add(×->latency.sum, &elapsed);
bfs_assert(times->timing);
times->timing = false;
++times->timed_reqs;
}
/** Add times to the totals, and reset the lap times. */
static void times_lap(struct times *total, struct times *lap) {
total->pushed += lap->pushed;
total->popped += lap->popped;
total->timed_reqs += lap->timed_reqs;
timespec_min(&total->latency.min, &lap->latency.min);
timespec_max(&total->latency.max, &lap->latency.max);
timespec_add(&total->latency.sum, &lap->latency.sum);
times_init(lap);
}
/** Print some times. */
static void times_print(const struct times *times, long seconds) {
struct timespec elapsed;
gettime(&elapsed);
timespec_sub(&elapsed, ×->start);
double fsec = timespec_ns(&elapsed) / 1.0e9;
double iops = times->popped / fsec;
double mean = timespec_ns(×->latency.sum) / times->timed_reqs;
double min = timespec_ns(×->latency.min);
double max = timespec_ns(×->latency.max);
if (seconds > 0) {
printf("%9ld", seconds);
} else if (elapsed.tv_nsec >= 10 * 1000 * 1000) {
printf("%9.2f", fsec);
} else {
printf("%9.0f", fsec);
}
printf(" │ %'17.0f │ %'15.0f ∈ [%'6.0f .. %'7.0f]\n", iops, mean, min, max);
fflush(stdout);
}
/** Push an ioq request. */
static bool push(struct ioq *ioq, enum ioq_nop_type type, struct times *lap) {
void *ptr = NULL;
// Track latency for a small fraction of requests
if (!lap->timing && (lap->pushed + 1) % 128 == 0) {
start_request(lap);
ptr = lap;
}
int ret = ioq_nop(ioq, type, ptr);
if (ret != 0) {
bfs_everify(errno == EAGAIN, "ioq_nop(%d)", (int)type);
return false;
}
++lap->pushed;
return true;
}
/** Pop an ioq request. */
static bool pop(struct ioq *ioq, struct times *lap, bool block) {
struct ioq_ent *ent = ioq_pop(ioq, block);
if (!ent) {
return false;
}
if (ent->ptr) {
finish_request(lap);
}
ioq_free(ioq, ent);
++lap->popped;
return true;
}
/** ^C flag. */
static atomic bool quit = false;
/** ^C hook. */
static void ctrlc(int sig, siginfo_t *info, void *arg) {
store(&quit, true, relaxed);
}
int main(int argc, char *argv[]) {
// Use CLOCK_MONOTONIC if available
#if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
if (sysoption(MONOTONIC_CLOCK) > 0) {
clockid = CLOCK_MONOTONIC;
}
#endif
// Enable thousands separators
setlocale(LC_ALL, "");
// -d: queue depth
long depth = 4096;
// -j: threads
long threads = 0;
// -t: timeout
double timeout = 5.0;
// -L|-H: ioq_nop() type
enum ioq_nop_type type = IOQ_NOP_LIGHT;
const char *cmd = argc > 0 ? argv[0] : "ioq";
int c;
while (c = getopt(argc, argv, ":d:j:t:LH"), c != -1) {
switch (c) {
case 'd':
if (xstrtol(optarg, NULL, 10, &depth) != 0) {
fprintf(stderr, "%s: Bad depth '%s': %s\n", cmd, optarg, errstr());
return EXIT_FAILURE;
}
break;
case 'j':
if (xstrtol(optarg, NULL, 10, &threads) != 0) {
fprintf(stderr, "%s: Bad thread count '%s': %s\n", cmd, optarg, errstr());
return EXIT_FAILURE;
}
break;
case 't':
if (xstrtod(optarg, NULL, &timeout) != 0) {
fprintf(stderr, "%s: Bad timeout '%s': %s\n", cmd, optarg, errstr());
return EXIT_FAILURE;
}
break;
case 'L':
type = IOQ_NOP_LIGHT;
break;
case 'H':
type = IOQ_NOP_HEAVY;
break;
case ':':
fprintf(stderr, "%s: Missing argument to -%c\n", cmd, optopt);
return EXIT_FAILURE;
case '?':
fprintf(stderr, "%s: Unrecognized option -%c\n", cmd, optopt);
return EXIT_FAILURE;
}
}
if (threads <= 0) {
threads = xsysconf(_SC_NPROCESSORS_ONLN);
if (threads > 8) {
threads = 8;
}
}
if (threads < 2) {
threads = 2;
}
--threads;
// Listen for ^C to print the summary
struct sighook *hook = sighook(SIGINT, ctrlc, NULL, SH_CONTINUE | SH_ONESHOT);
printf("I/O queue benchmark (%s)\n\n", bfs_version);
printf("[-d] depth: %ld\n", depth);
printf("[-j] threads: %ld (including main)\n", threads + 1);
if (type == IOQ_NOP_HEAVY) {
printf("[-H] type: heavy (with syscalls)\n");
} else {
printf("[-L] type: light (no syscalls)\n");
}
printf("\n");
printf(" Time (s) │ Throughput (IO/s) │ Latency (ns/IO)\n");
printf("══════════╪═══════════════════╪═════════════════\n");
fflush(stdout);
struct ioq *ioq = ioq_create(depth, threads);
bfs_everify(ioq, "ioq_create(%ld, %ld)", depth, threads);
// Pre-allocate all the requests
while (ioq_capacity(ioq) > 0) {
int ret = ioq_nop(ioq, type, NULL);
bfs_everify(ret == 0, "ioq_nop(%d)", (int)type);
}
while (true) {
struct ioq_ent *ent = ioq_pop(ioq, true);
if (!ent) {
break;
}
ioq_free(ioq, ent);
}
struct times total, lap;
times_init(&total);
lap = total;
long seconds = 0;
while (!load(&quit, relaxed)) {
bool was_timing = lap.timing;
for (int i = 0; i < 16; ++i) {
bool block = ioq_capacity(ioq) == 0;
if (!pop(ioq, &lap, block)) {
break;
}
}
if (was_timing && !lap.timing) {
struct timespec elapsed;
gettime(&elapsed);
timespec_sub(&elapsed, &total.start);
if (elapsed.tv_sec > seconds) {
seconds = elapsed.tv_sec;
times_print(&lap, seconds);
times_lap(&total, &lap);
}
double ns = timespec_ns(&elapsed);
if (timeout > 0 && ns >= timeout * 1.0e9) {
break;
}
}
for (int i = 0; i < 8; ++i) {
if (!push(ioq, type, &lap)) {
break;
}
}
ioq_submit(ioq);
}
while (pop(ioq, &lap, true));
times_lap(&total, &lap);
if (load(&quit, relaxed)) {
printf("\r────^C────┼───────────────────┼─────────────────\n");
} else {
printf("──────────┼───────────────────┼─────────────────\n");
}
times_print(&total, 0);
ioq_destroy(ioq);
sigunhook(hook);
return 0;
}
|