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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
#include "ioq.h"
#include "alloc.h"
#include "atomic.h"
#include "bfstd.h"
#include "bit.h"
#include "config.h"
#include "diag.h"
#include "dir.h"
#include "lock.h"
#include "sanity.h"
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
/**
* An I/O queue request.
*/
struct ioq_req {
/** Directory allocation. */
struct bfs_dir *dir;
/** Base file descriptor for openat(). */
int dfd;
/** Path to open, relative to dfd. */
const char *path;
/** Arbitrary user data. */
void *ptr;
};
/**
* An I/O queue command.
*/
union ioq_cmd {
struct ioq_req req;
struct ioq_res res;
};
/**
* A monitor for an I/O queue slot.
*/
struct ioq_monitor {
cache_align pthread_mutex_t mutex;
pthread_cond_t cond;
};
/** Initialize an ioq_monitor. */
static int ioq_monitor_init(struct ioq_monitor *monitor) {
if (mutex_init(&monitor->mutex, NULL) != 0) {
return -1;
}
if (cond_init(&monitor->cond, NULL) != 0) {
mutex_destroy(&monitor->mutex);
return -1;
}
return 0;
}
/** Destroy an ioq_monitor. */
static void ioq_monitor_destroy(struct ioq_monitor *monitor) {
cond_destroy(&monitor->cond);
mutex_destroy(&monitor->mutex);
}
/**
* An MPMC queue of I/O commands.
*/
struct ioqq {
/** Circular buffer index mask. */
size_t slot_mask;
/** Monitor index mask. */
size_t monitor_mask;
/** Array of monitors used by the slots. */
struct ioq_monitor *monitors;
/** Index of next writer. */
cache_align atomic size_t head;
/** Index of next reader. */
cache_align atomic size_t tail;
/** The circular buffer itself. */
cache_align atomic uintptr_t slots[];
};
// If we assign slots sequentially, threads will likely be operating on
// consecutive slots. If these slots are in the same cache line, that will
// result in false sharing. We can mitigate this by assigning slots with a
// stride larger than a cache line e.g. 0, 9, 18, ..., 1, 10, 19, ...
// As long as the stride is relatively prime to circular buffer length, we'll
// still use every available slot. Since the length is a power of two, that
// means the stride must be odd.
#define IOQ_STRIDE ((FALSE_SHARING_SIZE / sizeof(atomic uintptr_t)) | 1)
bfs_static_assert(IOQ_STRIDE % 2 == 1);
/** Slot flag bit to indicate waiters. */
#define IOQ_BLOCKED ((uintptr_t)1)
bfs_static_assert(alignof(union ioq_cmd) > 1);
/** Destroy an I/O command queue. */
static void ioqq_destroy(struct ioqq *ioqq) {
for (size_t i = 0; i < ioqq->monitor_mask + 1; ++i) {
ioq_monitor_destroy(&ioqq->monitors[i]);
}
free(ioqq->monitors);
free(ioqq);
}
/** Create an I/O command queue. */
static struct ioqq *ioqq_create(size_t size) {
// Circular buffer size must be a power of two
size = bit_ceil(size);
struct ioqq *ioqq = ALLOC_FLEX(struct ioqq, slots, size);
if (!ioqq) {
return NULL;
}
ioqq->slot_mask = size - 1;
ioqq->monitor_mask = -1;
// Use a pool of monitors
size_t nmonitors = size < 64 ? size : 64;
ioqq->monitors = ALLOC_ARRAY(struct ioq_monitor, nmonitors);
if (!ioqq->monitors) {
ioqq_destroy(ioqq);
return NULL;
}
for (size_t i = 0; i < nmonitors; ++i) {
if (ioq_monitor_init(&ioqq->monitors[i]) != 0) {
ioqq_destroy(ioqq);
return NULL;
}
++ioqq->monitor_mask;
}
atomic_init(&ioqq->head, 0);
atomic_init(&ioqq->tail, 0);
for (size_t i = 0; i < size; ++i) {
atomic_init(&ioqq->slots[i], 0);
}
return ioqq;
}
/** Atomically wait for a slot to change. */
static uintptr_t ioqq_wait(struct ioqq *ioqq, size_t i, uintptr_t value) {
atomic uintptr_t *slot = &ioqq->slots[i & ioqq->slot_mask];
struct ioq_monitor *monitor = &ioqq->monitors[i & ioqq->monitor_mask];
mutex_lock(&monitor->mutex);
uintptr_t ret;
while ((ret = load(slot, relaxed)) == value) {
// To avoid missed wakeups, it is important that
// cond_broadcast() is not called right here
cond_wait(&monitor->cond, &monitor->mutex);
}
mutex_unlock(&monitor->mutex);
return ret;
}
/** Wake up any threads waiting on a slot. */
static void ioqq_wake(struct ioqq *ioqq, size_t i) {
struct ioq_monitor *monitor = &ioqq->monitors[i & ioqq->monitor_mask];
// The following implementation would clearly avoid the missed wakeup
// issue mentioned above in ioqq_wait():
//
// mutex_lock(&monitor->mutex);
// cond_broadcast(&monitor->cond);
// mutex_unlock(&monitor->mutex);
//
// As a minor optimization, we move the broadcast outside of the lock.
// This optimization is correct, even though it leads to a seemingly-
// useless empty critical section.
mutex_lock(&monitor->mutex);
mutex_unlock(&monitor->mutex);
cond_broadcast(&monitor->cond);
}
/** Push a command onto the queue. */
static void ioqq_push(struct ioqq *ioqq, union ioq_cmd *cmd) {
size_t i = fetch_add(&ioqq->head, IOQ_STRIDE, relaxed);
atomic uintptr_t *slot = &ioqq->slots[i & ioqq->slot_mask];
uintptr_t addr = (uintptr_t)cmd;
bfs_assert(!(addr & IOQ_BLOCKED));
uintptr_t prev = load(slot, relaxed);
do {
while (prev & ~IOQ_BLOCKED) {
prev = fetch_or(slot, IOQ_BLOCKED, relaxed);
if (prev & ~IOQ_BLOCKED) {
prev = ioqq_wait(ioqq, i, prev | IOQ_BLOCKED);
}
}
} while (!compare_exchange_weak(slot, &prev, addr, release, relaxed));
if (prev & IOQ_BLOCKED) {
ioqq_wake(ioqq, i);
}
}
/** Pop a command from a queue. */
static union ioq_cmd *ioqq_pop(struct ioqq *ioqq) {
size_t i = fetch_add(&ioqq->tail, IOQ_STRIDE, relaxed);
atomic uintptr_t *slot = &ioqq->slots[i & ioqq->slot_mask];
uintptr_t prev = load(slot, relaxed);
do {
while (!(prev & ~IOQ_BLOCKED)) {
prev = fetch_or(slot, IOQ_BLOCKED, relaxed);
if (!(prev & ~IOQ_BLOCKED)) {
prev = ioqq_wait(ioqq, i, IOQ_BLOCKED);
}
}
} while (!compare_exchange_weak(slot, &prev, 0, acquire, relaxed));
if (prev & IOQ_BLOCKED) {
ioqq_wake(ioqq, i);
}
prev &= ~IOQ_BLOCKED;
return (union ioq_cmd *)prev;
}
/** Pop a command from a queue if one is available. */
static union ioq_cmd *ioqq_trypop(struct ioqq *ioqq) {
size_t i = load(&ioqq->tail, relaxed);
atomic uintptr_t *slot = &ioqq->slots[i & ioqq->slot_mask];
uintptr_t prev = exchange(slot, 0, acquire);
if (prev & IOQ_BLOCKED) {
ioqq_wake(ioqq, i);
}
prev &= ~IOQ_BLOCKED;
if (prev) {
size_t j = exchange(&ioqq->tail, i + IOQ_STRIDE, relaxed);
bfs_assert(j == i, "ioqq_trypop() only supports a single consumer");
(void)j;
}
return (union ioq_cmd *)prev;
}
/** Sentinel stop command. */
static union ioq_cmd IOQ_STOP;
struct ioq {
/** The depth of the queue. */
size_t depth;
/** The current size of the queue. */
size_t size;
/** ioq_cmd command arena. */
struct arena cmds;
/** Pending I/O requests. */
struct ioqq *pending;
/** Ready I/O responses. */
struct ioqq *ready;
/** The number of background threads. */
size_t nthreads;
/** The background threads themselves. */
pthread_t threads[];
};
/** Background thread entry point. */
static void *ioq_work(void *ptr) {
struct ioq *ioq = ptr;
while (true) {
union ioq_cmd *cmd = ioqq_pop(ioq->pending);
if (cmd == &IOQ_STOP) {
break;
}
struct ioq_req req = cmd->req;
sanitize_uninit(cmd);
struct ioq_res *res = &cmd->res;
res->ptr = req.ptr;
res->dir = req.dir;
res->error = 0;
if (bfs_opendir(req.dir, req.dfd, req.path) == 0) {
bfs_polldir(res->dir);
} else {
res->error = errno;
}
ioqq_push(ioq->ready, cmd);
}
return NULL;
}
struct ioq *ioq_create(size_t depth, size_t nthreads) {
struct ioq *ioq = ZALLOC_FLEX(struct ioq, threads, nthreads);
if (!ioq) {
goto fail;
}
ioq->depth = depth;
ARENA_INIT(&ioq->cmds, union ioq_cmd);
ioq->pending = ioqq_create(depth);
if (!ioq->pending) {
goto fail;
}
ioq->ready = ioqq_create(depth);
if (!ioq->ready) {
goto fail;
}
for (size_t i = 0; i < nthreads; ++i) {
errno = pthread_create(&ioq->threads[i], NULL, ioq_work, ioq);
if (errno != 0) {
goto fail;
}
++ioq->nthreads;
}
return ioq;
int err;
fail:
err = errno;
ioq_destroy(ioq);
errno = err;
return NULL;
}
int ioq_opendir(struct ioq *ioq, struct bfs_dir *dir, int dfd, const char *path, void *ptr) {
if (ioq->size >= ioq->depth) {
return -1;
}
union ioq_cmd *cmd = arena_alloc(&ioq->cmds);
if (!cmd) {
return -1;
}
struct ioq_req *req = &cmd->req;
req->dir = dir;
req->dfd = dfd;
req->path = path;
req->ptr = ptr;
ioqq_push(ioq->pending, cmd);
++ioq->size;
return 0;
}
struct ioq_res *ioq_pop(struct ioq *ioq) {
if (ioq->size == 0) {
return NULL;
}
union ioq_cmd *cmd = ioqq_pop(ioq->ready);
--ioq->size;
return &cmd->res;
}
struct ioq_res *ioq_trypop(struct ioq *ioq) {
if (ioq->size == 0) {
return NULL;
}
union ioq_cmd *cmd = ioqq_trypop(ioq->ready);
if (!cmd) {
return NULL;
}
--ioq->size;
return &cmd->res;
}
void ioq_free(struct ioq *ioq, struct ioq_res *res) {
arena_free(&ioq->cmds, (union ioq_cmd *)res);
}
void ioq_destroy(struct ioq *ioq) {
if (!ioq) {
return;
}
for (size_t i = 0; i < ioq->nthreads; ++i) {
ioqq_push(ioq->pending, &IOQ_STOP);
}
for (size_t i = 0; i < ioq->nthreads; ++i) {
if (pthread_join(ioq->threads[i], NULL) != 0) {
abort();
}
}
ioqq_destroy(ioq->ready);
ioqq_destroy(ioq->pending);
arena_destroy(&ioq->cmds);
free(ioq);
}
|