1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // io.cfa --
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Thu Apr 23 17:31:00 2020
|
---|
11 | // Last Modified By :
|
---|
12 | // Last Modified On :
|
---|
13 | // Update Count :
|
---|
14 | //
|
---|
15 |
|
---|
16 | // #define __CFA_DEBUG_PRINT_IO__
|
---|
17 | // #define __CFA_DEBUG_PRINT_IO_CORE__
|
---|
18 |
|
---|
19 | #include "kernel.hfa"
|
---|
20 | #include "bitmanip.hfa"
|
---|
21 |
|
---|
22 | #if !defined(HAVE_LINUX_IO_URING_H)
|
---|
23 | void __kernel_io_startup( cluster &, unsigned, bool ) {
|
---|
24 | // Nothing to do without io_uring
|
---|
25 | }
|
---|
26 |
|
---|
27 | void __kernel_io_finish_start( cluster & ) {
|
---|
28 | // Nothing to do without io_uring
|
---|
29 | }
|
---|
30 |
|
---|
31 | void __kernel_io_prepare_stop( cluster & ) {
|
---|
32 | // Nothing to do without io_uring
|
---|
33 | }
|
---|
34 |
|
---|
35 | void __kernel_io_shutdown( cluster &, bool ) {
|
---|
36 | // Nothing to do without io_uring
|
---|
37 | }
|
---|
38 |
|
---|
39 | #else
|
---|
40 | extern "C" {
|
---|
41 | #define _GNU_SOURCE /* See feature_test_macros(7) */
|
---|
42 | #include <errno.h>
|
---|
43 | #include <stdint.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <unistd.h>
|
---|
46 | #include <sys/mman.h>
|
---|
47 | #include <sys/syscall.h>
|
---|
48 |
|
---|
49 | #include <linux/io_uring.h>
|
---|
50 | }
|
---|
51 |
|
---|
52 | #include "bits/signal.hfa"
|
---|
53 | #include "kernel_private.hfa"
|
---|
54 | #include "thread.hfa"
|
---|
55 |
|
---|
56 | uint32_t entries_per_cluster() {
|
---|
57 | return 256;
|
---|
58 | }
|
---|
59 |
|
---|
60 | static void * __io_poller_slow( void * arg );
|
---|
61 |
|
---|
62 | // Weirdly, some systems that do support io_uring don't actually define these
|
---|
63 | #ifdef __alpha__
|
---|
64 | /*
|
---|
65 | * alpha is the only exception, all other architectures
|
---|
66 | * have common numbers for new system calls.
|
---|
67 | */
|
---|
68 | #ifndef __NR_io_uring_setup
|
---|
69 | #define __NR_io_uring_setup 535
|
---|
70 | #endif
|
---|
71 | #ifndef __NR_io_uring_enter
|
---|
72 | #define __NR_io_uring_enter 536
|
---|
73 | #endif
|
---|
74 | #ifndef __NR_io_uring_register
|
---|
75 | #define __NR_io_uring_register 537
|
---|
76 | #endif
|
---|
77 | #else /* !__alpha__ */
|
---|
78 | #ifndef __NR_io_uring_setup
|
---|
79 | #define __NR_io_uring_setup 425
|
---|
80 | #endif
|
---|
81 | #ifndef __NR_io_uring_enter
|
---|
82 | #define __NR_io_uring_enter 426
|
---|
83 | #endif
|
---|
84 | #ifndef __NR_io_uring_register
|
---|
85 | #define __NR_io_uring_register 427
|
---|
86 | #endif
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | // Fast poller user-thread
|
---|
90 | // Not using the "thread" keyword because we want to control
|
---|
91 | // more carefully when to start/stop it
|
---|
92 | struct __io_poller_fast {
|
---|
93 | struct __io_data * ring;
|
---|
94 | $thread thrd;
|
---|
95 | };
|
---|
96 |
|
---|
97 | void ?{}( __io_poller_fast & this, struct cluster & cltr ) {
|
---|
98 | this.ring = cltr.io;
|
---|
99 | (this.thrd){ "Fast I/O Poller", cltr };
|
---|
100 | }
|
---|
101 | void ^?{}( __io_poller_fast & mutex this );
|
---|
102 | void main( __io_poller_fast & this );
|
---|
103 | static inline $thread * get_thread( __io_poller_fast & this ) { return &this.thrd; }
|
---|
104 | void ^?{}( __io_poller_fast & mutex this ) {}
|
---|
105 |
|
---|
106 | struct __submition_data {
|
---|
107 | // Head and tail of the ring (associated with array)
|
---|
108 | volatile uint32_t * head;
|
---|
109 | volatile uint32_t * tail;
|
---|
110 |
|
---|
111 | // The actual kernel ring which uses head/tail
|
---|
112 | // indexes into the sqes arrays
|
---|
113 | uint32_t * array;
|
---|
114 |
|
---|
115 | // number of entries and mask to go with it
|
---|
116 | const uint32_t * num;
|
---|
117 | const uint32_t * mask;
|
---|
118 |
|
---|
119 | // Submission flags (Not sure what for)
|
---|
120 | uint32_t * flags;
|
---|
121 |
|
---|
122 | // number of sqes not submitted (whatever that means)
|
---|
123 | uint32_t * dropped;
|
---|
124 |
|
---|
125 | // Like head/tail but not seen by the kernel
|
---|
126 | volatile uint32_t * ready;
|
---|
127 | uint32_t ready_cnt;
|
---|
128 |
|
---|
129 | __spinlock_t lock;
|
---|
130 |
|
---|
131 | // A buffer of sqes (not the actual ring)
|
---|
132 | struct io_uring_sqe * sqes;
|
---|
133 |
|
---|
134 | // The location and size of the mmaped area
|
---|
135 | void * ring_ptr;
|
---|
136 | size_t ring_sz;
|
---|
137 | };
|
---|
138 |
|
---|
139 | struct __completion_data {
|
---|
140 | // Head and tail of the ring
|
---|
141 | volatile uint32_t * head;
|
---|
142 | volatile uint32_t * tail;
|
---|
143 |
|
---|
144 | // number of entries and mask to go with it
|
---|
145 | const uint32_t * mask;
|
---|
146 | const uint32_t * num;
|
---|
147 |
|
---|
148 | // number of cqes not submitted (whatever that means)
|
---|
149 | uint32_t * overflow;
|
---|
150 |
|
---|
151 | // the kernel ring
|
---|
152 | struct io_uring_cqe * cqes;
|
---|
153 |
|
---|
154 | // The location and size of the mmaped area
|
---|
155 | void * ring_ptr;
|
---|
156 | size_t ring_sz;
|
---|
157 | };
|
---|
158 |
|
---|
159 | struct __io_data {
|
---|
160 | struct __submition_data submit_q;
|
---|
161 | struct __completion_data completion_q;
|
---|
162 | uint32_t ring_flags;
|
---|
163 | int cltr_flags;
|
---|
164 | int fd;
|
---|
165 | semaphore submit;
|
---|
166 | volatile bool done;
|
---|
167 | struct {
|
---|
168 | struct {
|
---|
169 | __processor_id_t id;
|
---|
170 | void * stack;
|
---|
171 | pthread_t kthrd;
|
---|
172 | volatile bool blocked;
|
---|
173 | } slow;
|
---|
174 | __io_poller_fast fast;
|
---|
175 | __bin_sem_t sem;
|
---|
176 | } poller;
|
---|
177 | };
|
---|
178 |
|
---|
179 | //=============================================================================================
|
---|
180 | // I/O Startup / Shutdown logic
|
---|
181 | //=============================================================================================
|
---|
182 | void __kernel_io_startup( cluster & this, unsigned io_flags, bool main_cluster ) {
|
---|
183 | this.io = malloc();
|
---|
184 |
|
---|
185 | // Step 1 : call to setup
|
---|
186 | struct io_uring_params params;
|
---|
187 | memset(¶ms, 0, sizeof(params));
|
---|
188 |
|
---|
189 | uint32_t nentries = entries_per_cluster();
|
---|
190 |
|
---|
191 | int fd = syscall(__NR_io_uring_setup, nentries, ¶ms );
|
---|
192 | if(fd < 0) {
|
---|
193 | abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
|
---|
194 | }
|
---|
195 |
|
---|
196 | // Step 2 : mmap result
|
---|
197 | memset( this.io, 0, sizeof(struct __io_data) );
|
---|
198 | struct __submition_data & sq = this.io->submit_q;
|
---|
199 | struct __completion_data & cq = this.io->completion_q;
|
---|
200 |
|
---|
201 | // calculate the right ring size
|
---|
202 | sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) );
|
---|
203 | cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe));
|
---|
204 |
|
---|
205 | // Requires features
|
---|
206 | #if defined(IORING_FEAT_SINGLE_MMAP)
|
---|
207 | // adjust the size according to the parameters
|
---|
208 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
|
---|
209 | cq->ring_sz = sq->ring_sz = max(cq->ring_sz, sq->ring_sz);
|
---|
210 | }
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | // mmap the Submit Queue into existence
|
---|
214 | sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
|
---|
215 | if (sq.ring_ptr == (void*)MAP_FAILED) {
|
---|
216 | abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
|
---|
217 | }
|
---|
218 |
|
---|
219 | // Requires features
|
---|
220 | #if defined(IORING_FEAT_SINGLE_MMAP)
|
---|
221 | // mmap the Completion Queue into existence (may or may not be needed)
|
---|
222 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
|
---|
223 | cq->ring_ptr = sq->ring_ptr;
|
---|
224 | }
|
---|
225 | else
|
---|
226 | #endif
|
---|
227 | {
|
---|
228 | // We need multiple call to MMAP
|
---|
229 | cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
|
---|
230 | if (cq.ring_ptr == (void*)MAP_FAILED) {
|
---|
231 | munmap(sq.ring_ptr, sq.ring_sz);
|
---|
232 | abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | // mmap the submit queue entries
|
---|
237 | size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
|
---|
238 | sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
|
---|
239 | if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) {
|
---|
240 | munmap(sq.ring_ptr, sq.ring_sz);
|
---|
241 | if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz);
|
---|
242 | abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno));
|
---|
243 | }
|
---|
244 |
|
---|
245 | // Get the pointers from the kernel to fill the structure
|
---|
246 | // submit queue
|
---|
247 | sq.head = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
|
---|
248 | sq.tail = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
|
---|
249 | sq.mask = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
|
---|
250 | sq.num = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
|
---|
251 | sq.flags = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
|
---|
252 | sq.dropped = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
|
---|
253 | sq.array = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
|
---|
254 |
|
---|
255 | {
|
---|
256 | const uint32_t num = *sq.num;
|
---|
257 | for( i; num ) {
|
---|
258 | sq.sqes[i].user_data = 0ul64;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | if( io_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) {
|
---|
263 | /* paranoid */ verify( is_pow2( io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET ) || ((io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET) < 8) );
|
---|
264 | sq.ready_cnt = max(io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET, 8);
|
---|
265 | sq.ready = alloc_align( 64, sq.ready_cnt );
|
---|
266 | for(i; sq.ready_cnt) {
|
---|
267 | sq.ready[i] = -1ul32;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | else {
|
---|
271 | sq.ready_cnt = 0;
|
---|
272 | sq.ready = 0p;
|
---|
273 | }
|
---|
274 |
|
---|
275 | // completion queue
|
---|
276 | cq.head = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.head);
|
---|
277 | cq.tail = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail);
|
---|
278 | cq.mask = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask);
|
---|
279 | cq.num = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries);
|
---|
280 | cq.overflow = ( uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow);
|
---|
281 | cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes);
|
---|
282 |
|
---|
283 | // some paranoid checks
|
---|
284 | /* paranoid */ verifyf( (*cq.mask) == ((*cq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*cq.num) - 1ul32, *cq.num, *cq.mask );
|
---|
285 | /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num );
|
---|
286 | /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head );
|
---|
287 | /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail );
|
---|
288 |
|
---|
289 | /* paranoid */ verifyf( (*sq.mask) == ((*sq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*sq.num) - 1ul32, *sq.num, *sq.mask );
|
---|
290 | /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
|
---|
291 | /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head );
|
---|
292 | /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail );
|
---|
293 |
|
---|
294 | // Update the global ring info
|
---|
295 | this.io->ring_flags = params.flags;
|
---|
296 | this.io->cltr_flags = io_flags;
|
---|
297 | this.io->fd = fd;
|
---|
298 | this.io->done = false;
|
---|
299 | (this.io->submit){ min(*sq.num, *cq.num) };
|
---|
300 |
|
---|
301 | if(!main_cluster) {
|
---|
302 | __kernel_io_finish_start( this );
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | void __kernel_io_finish_start( cluster & this ) {
|
---|
307 | if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) {
|
---|
308 | __cfadbg_print_safe(io_core, "Kernel I/O : Creating fast poller for cluter %p\n", &this);
|
---|
309 | (this.io->poller.fast){ this };
|
---|
310 | __thrd_start( this.io->poller.fast, main );
|
---|
311 | }
|
---|
312 |
|
---|
313 | // Create the poller thread
|
---|
314 | __cfadbg_print_safe(io_core, "Kernel I/O : Creating slow poller for cluter %p\n", &this);
|
---|
315 | this.io->poller.slow.blocked = false;
|
---|
316 | this.io->poller.slow.stack = __create_pthread( &this.io->poller.slow.kthrd, __io_poller_slow, &this );
|
---|
317 | }
|
---|
318 |
|
---|
319 | void __kernel_io_prepare_stop( cluster & this ) {
|
---|
320 | __cfadbg_print_safe(io_core, "Kernel I/O : Stopping pollers for cluster\n", &this);
|
---|
321 | // Notify the poller thread of the shutdown
|
---|
322 | __atomic_store_n(&this.io->done, true, __ATOMIC_SEQ_CST);
|
---|
323 |
|
---|
324 | // Stop the IO Poller
|
---|
325 | sigval val = { 1 };
|
---|
326 | pthread_sigqueue( this.io->poller.slow.kthrd, SIGUSR1, val );
|
---|
327 | post( this.io->poller.sem );
|
---|
328 |
|
---|
329 | // Wait for the poller thread to finish
|
---|
330 | pthread_join( this.io->poller.slow.kthrd, 0p );
|
---|
331 | free( this.io->poller.slow.stack );
|
---|
332 |
|
---|
333 | __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller stopped for cluster\n", &this);
|
---|
334 |
|
---|
335 | if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) {
|
---|
336 | with( this.io->poller.fast ) {
|
---|
337 | /* paranoid */ verify( this.nprocessors == 0 || &this == mainCluster );
|
---|
338 | /* paranoid */ verify( !ready_mutate_islocked() );
|
---|
339 |
|
---|
340 | // We need to adjust the clean-up based on where the thread is
|
---|
341 | if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) {
|
---|
342 |
|
---|
343 | ready_schedule_lock( (struct __processor_id_t *)active_processor() );
|
---|
344 |
|
---|
345 | // This is the tricky case
|
---|
346 | // The thread was preempted and now it is on the ready queue
|
---|
347 | // The thread should be the last on the list
|
---|
348 | /* paranoid */ verify( thrd.link.next != 0p );
|
---|
349 |
|
---|
350 | // Remove the thread from the ready queue of this cluster
|
---|
351 | __attribute__((unused)) bool removed = remove_head( &this, &thrd );
|
---|
352 | /* paranoid */ verify( removed );
|
---|
353 | thrd.link.next = 0p;
|
---|
354 | thrd.link.prev = 0p;
|
---|
355 | __cfaabi_dbg_debug_do( thrd.unpark_stale = true );
|
---|
356 |
|
---|
357 | // Fixup the thread state
|
---|
358 | thrd.state = Blocked;
|
---|
359 | thrd.ticket = 0;
|
---|
360 | thrd.preempted = __NO_PREEMPTION;
|
---|
361 |
|
---|
362 | ready_schedule_unlock( (struct __processor_id_t *)active_processor() );
|
---|
363 |
|
---|
364 | // Pretend like the thread was blocked all along
|
---|
365 | }
|
---|
366 | // !!! This is not an else if !!!
|
---|
367 | if( thrd.state == Blocked ) {
|
---|
368 |
|
---|
369 | // This is the "easy case"
|
---|
370 | // The thread is parked and can easily be moved to active cluster
|
---|
371 | verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster );
|
---|
372 | thrd.curr_cluster = active_cluster();
|
---|
373 |
|
---|
374 | // unpark the fast io_poller
|
---|
375 | unpark( &thrd __cfaabi_dbg_ctx2 );
|
---|
376 | }
|
---|
377 | else {
|
---|
378 |
|
---|
379 | // The thread is in a weird state
|
---|
380 | // I don't know what to do here
|
---|
381 | abort("Fast poller thread is in unexpected state, cannot clean-up correctly\n");
|
---|
382 | }
|
---|
383 |
|
---|
384 | }
|
---|
385 |
|
---|
386 | ^(this.io->poller.fast){};
|
---|
387 |
|
---|
388 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller stopped for cluster\n", &this);
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | void __kernel_io_shutdown( cluster & this, bool main_cluster ) {
|
---|
393 | if(!main_cluster) {
|
---|
394 | __kernel_io_prepare_stop( this );
|
---|
395 | }
|
---|
396 |
|
---|
397 | // Shutdown the io rings
|
---|
398 | struct __submition_data & sq = this.io->submit_q;
|
---|
399 | struct __completion_data & cq = this.io->completion_q;
|
---|
400 |
|
---|
401 | // unmap the submit queue entries
|
---|
402 | munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
|
---|
403 |
|
---|
404 | // unmap the Submit Queue ring
|
---|
405 | munmap(sq.ring_ptr, sq.ring_sz);
|
---|
406 |
|
---|
407 | // unmap the Completion Queue ring, if it is different
|
---|
408 | if (cq.ring_ptr != sq.ring_ptr) {
|
---|
409 | munmap(cq.ring_ptr, cq.ring_sz);
|
---|
410 | }
|
---|
411 |
|
---|
412 | // close the file descriptor
|
---|
413 | close(this.io->fd);
|
---|
414 |
|
---|
415 | free( this.io->submit_q.ready ); // Maybe null, doesn't matter
|
---|
416 | free( this.io );
|
---|
417 | }
|
---|
418 |
|
---|
419 | //=============================================================================================
|
---|
420 | // I/O Polling
|
---|
421 | //=============================================================================================
|
---|
422 | struct io_user_data {
|
---|
423 | int32_t result;
|
---|
424 | $thread * thrd;
|
---|
425 | };
|
---|
426 |
|
---|
427 | // Process a single completion message from the io_uring
|
---|
428 | // This is NOT thread-safe
|
---|
429 | static [int, bool] __drain_io( & struct __io_data ring, * sigset_t mask, int waitcnt, bool in_kernel ) {
|
---|
430 | unsigned to_submit = 0;
|
---|
431 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) {
|
---|
432 |
|
---|
433 | // If the poller thread also submits, then we need to aggregate the submissions which are ready
|
---|
434 | uint32_t tail = *ring.submit_q.tail;
|
---|
435 | const uint32_t mask = *ring.submit_q.mask;
|
---|
436 |
|
---|
437 | // Go through the list of ready submissions
|
---|
438 | for( i; ring.submit_q.ready_cnt ) {
|
---|
439 | // replace any submission with the sentinel, to consume it.
|
---|
440 | uint32_t idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED);
|
---|
441 |
|
---|
442 | // If it was already the sentinel, then we are done
|
---|
443 | if( idx == -1ul32 ) continue;
|
---|
444 |
|
---|
445 | // If we got a real submission, append it to the list
|
---|
446 | ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask;
|
---|
447 | to_submit++;
|
---|
448 | }
|
---|
449 |
|
---|
450 | // Increment the tail based on how many we are ready to submit
|
---|
451 | __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST);
|
---|
452 | }
|
---|
453 |
|
---|
454 | const uint32_t smask = *ring.submit_q.mask;
|
---|
455 | uint32_t shead = *ring.submit_q.head;
|
---|
456 | int ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, waitcnt, IORING_ENTER_GETEVENTS, mask, _NSIG / 8);
|
---|
457 | if( ret < 0 ) {
|
---|
458 | switch((int)errno) {
|
---|
459 | case EAGAIN:
|
---|
460 | case EINTR:
|
---|
461 | return -EAGAIN;
|
---|
462 | default:
|
---|
463 | abort( "KERNEL ERROR: IO_URING WAIT - %s\n", strerror(errno) );
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | // Release the consumed SQEs
|
---|
468 | for( i; ret ) {
|
---|
469 | uint32_t idx = ring.submit_q.array[ (i + shead) & smask ];
|
---|
470 | ring.submit_q.sqes[ idx ].user_data = 0;
|
---|
471 | }
|
---|
472 |
|
---|
473 | uint32_t avail = 0;
|
---|
474 | uint32_t sqe_num = *ring.submit_q.num;
|
---|
475 | for(i; sqe_num) {
|
---|
476 | if( ring.submit_q.sqes[ i ].user_data == 0 ) avail++;
|
---|
477 | }
|
---|
478 |
|
---|
479 | // update statistics
|
---|
480 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
481 | __tls_stats()->io.submit_q.submit_avg.rdy += to_submit;
|
---|
482 | __tls_stats()->io.submit_q.submit_avg.csm += ret;
|
---|
483 | __tls_stats()->io.submit_q.submit_avg.avl += avail;
|
---|
484 | __tls_stats()->io.submit_q.submit_avg.cnt += 1;
|
---|
485 | #endif
|
---|
486 |
|
---|
487 | // Drain the queue
|
---|
488 | unsigned head = *ring.completion_q.head;
|
---|
489 | unsigned tail = *ring.completion_q.tail;
|
---|
490 | const uint32_t mask = *ring.completion_q.mask;
|
---|
491 |
|
---|
492 | // Memory barrier
|
---|
493 | __atomic_thread_fence( __ATOMIC_SEQ_CST );
|
---|
494 |
|
---|
495 | // Nothing was new return 0
|
---|
496 | if (head == tail) {
|
---|
497 | return 0;
|
---|
498 | }
|
---|
499 |
|
---|
500 | uint32_t count = tail - head;
|
---|
501 | for(i; count) {
|
---|
502 | unsigned idx = (head + i) & mask;
|
---|
503 | struct io_uring_cqe & cqe = ring.completion_q.cqes[idx];
|
---|
504 |
|
---|
505 | /* paranoid */ verify(&cqe);
|
---|
506 |
|
---|
507 | struct io_user_data * data = (struct io_user_data *)cqe.user_data;
|
---|
508 | __cfadbg_print_safe( io, "Kernel I/O : Performed reading io cqe %p, result %d for %p\n", data, cqe.res, data->thrd );
|
---|
509 |
|
---|
510 | data->result = cqe.res;
|
---|
511 | if(!in_kernel) { unpark( data->thrd __cfaabi_dbg_ctx2 ); }
|
---|
512 | else { __unpark( &ring.poller.slow.id, data->thrd __cfaabi_dbg_ctx2 ); }
|
---|
513 | }
|
---|
514 |
|
---|
515 | // Allow new submissions to happen
|
---|
516 | // V(ring.submit, count);
|
---|
517 |
|
---|
518 | // Mark to the kernel that the cqe has been seen
|
---|
519 | // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
|
---|
520 | __atomic_thread_fence( __ATOMIC_SEQ_CST );
|
---|
521 | __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_RELAXED );
|
---|
522 |
|
---|
523 | return [count, count > 0 || to_submit > 0];
|
---|
524 | }
|
---|
525 |
|
---|
526 | static void * __io_poller_slow( void * arg ) {
|
---|
527 | #if !defined( __CFA_NO_STATISTICS__ )
|
---|
528 | __stats_t local_stats;
|
---|
529 | __init_stats( &local_stats );
|
---|
530 | kernelTLS.this_stats = &local_stats;
|
---|
531 | #endif
|
---|
532 |
|
---|
533 | cluster * cltr = (cluster *)arg;
|
---|
534 | struct __io_data & ring = *cltr->io;
|
---|
535 |
|
---|
536 | ring.poller.slow.id.id = doregister( &ring.poller.slow.id );
|
---|
537 |
|
---|
538 | sigset_t mask;
|
---|
539 | sigfillset(&mask);
|
---|
540 | if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
|
---|
541 | abort( "KERNEL ERROR: IO_URING - pthread_sigmask" );
|
---|
542 | }
|
---|
543 |
|
---|
544 | sigdelset( &mask, SIGUSR1 );
|
---|
545 |
|
---|
546 | verify( (*ring.submit_q.head) == (*ring.submit_q.tail) );
|
---|
547 | verify( (*ring.completion_q.head) == (*ring.completion_q.tail) );
|
---|
548 |
|
---|
549 | __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p ready\n", &ring);
|
---|
550 |
|
---|
551 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) {
|
---|
552 | while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) {
|
---|
553 |
|
---|
554 | __atomic_store_n( &ring.poller.slow.blocked, true, __ATOMIC_SEQ_CST );
|
---|
555 |
|
---|
556 | // In the user-thread approach drain and if anything was drained,
|
---|
557 | // batton pass to the user-thread
|
---|
558 | int count;
|
---|
559 | bool again;
|
---|
560 | [count, again] = __drain_io( ring, &mask, 1, true );
|
---|
561 |
|
---|
562 | __atomic_store_n( &ring.poller.slow.blocked, false, __ATOMIC_SEQ_CST );
|
---|
563 |
|
---|
564 | // Update statistics
|
---|
565 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
566 | __tls_stats()->io.complete_q.completed_avg.val += count;
|
---|
567 | __tls_stats()->io.complete_q.completed_avg.slow_cnt += 1;
|
---|
568 | #endif
|
---|
569 |
|
---|
570 | if(again) {
|
---|
571 | __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to fast poller\n", &ring);
|
---|
572 | __unpark( &ring.poller.slow.id, &ring.poller.fast.thrd __cfaabi_dbg_ctx2 );
|
---|
573 | wait( ring.poller.sem );
|
---|
574 | }
|
---|
575 | }
|
---|
576 | }
|
---|
577 | else {
|
---|
578 | while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) {
|
---|
579 | //In the naive approach, just poll the io completion queue directly
|
---|
580 | int count;
|
---|
581 | bool again;
|
---|
582 | [count, again] = __drain_io( ring, &mask, 1, true );
|
---|
583 |
|
---|
584 | // Update statistics
|
---|
585 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
586 | __tls_stats()->io.complete_q.completed_avg.val += count;
|
---|
587 | __tls_stats()->io.complete_q.completed_avg.slow_cnt += 1;
|
---|
588 | #endif
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p stopping\n", &ring);
|
---|
593 |
|
---|
594 | unregister( &ring.poller.slow.id );
|
---|
595 |
|
---|
596 | return 0p;
|
---|
597 | }
|
---|
598 |
|
---|
599 | void main( __io_poller_fast & this ) {
|
---|
600 | verify( this.ring->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD );
|
---|
601 |
|
---|
602 | // Start parked
|
---|
603 | park( __cfaabi_dbg_ctx );
|
---|
604 |
|
---|
605 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p ready\n", &this.ring);
|
---|
606 |
|
---|
607 | int reset = 0;
|
---|
608 |
|
---|
609 | // Then loop until we need to start
|
---|
610 | while(!__atomic_load_n(&this.ring->done, __ATOMIC_SEQ_CST)) {
|
---|
611 |
|
---|
612 | // Drain the io
|
---|
613 | int count;
|
---|
614 | bool again;
|
---|
615 | disable_interrupts();
|
---|
616 | [count, again] = __drain_io( *this.ring, 0p, 0, false );
|
---|
617 |
|
---|
618 | if(!again) reset++;
|
---|
619 |
|
---|
620 | // Update statistics
|
---|
621 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
622 | __tls_stats()->io.complete_q.completed_avg.val += count;
|
---|
623 | __tls_stats()->io.complete_q.completed_avg.fast_cnt += 1;
|
---|
624 | #endif
|
---|
625 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
626 |
|
---|
627 | // If we got something, just yield and check again
|
---|
628 | if(reset < 5) {
|
---|
629 | yield();
|
---|
630 | }
|
---|
631 | // We didn't get anything baton pass to the slow poller
|
---|
632 | else {
|
---|
633 | __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to slow poller\n", &this.ring);
|
---|
634 | reset = 0;
|
---|
635 |
|
---|
636 | // wake up the slow poller
|
---|
637 | post( this.ring->poller.sem );
|
---|
638 |
|
---|
639 | // park this thread
|
---|
640 | park( __cfaabi_dbg_ctx );
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p stopping\n", &this.ring);
|
---|
645 | }
|
---|
646 |
|
---|
647 | static inline void __wake_poller( struct __io_data & ring ) __attribute__((artificial));
|
---|
648 | static inline void __wake_poller( struct __io_data & ring ) {
|
---|
649 | if(!__atomic_load_n( &ring.poller.slow.blocked, __ATOMIC_SEQ_CST)) return;
|
---|
650 |
|
---|
651 | sigval val = { 1 };
|
---|
652 | pthread_sigqueue( ring.poller.slow.kthrd, SIGUSR1, val );
|
---|
653 | }
|
---|
654 |
|
---|
655 | //=============================================================================================
|
---|
656 | // I/O Submissions
|
---|
657 | //=============================================================================================
|
---|
658 |
|
---|
659 | // Submition steps :
|
---|
660 | // 1 - We need to make sure we don't overflow any of the buffer, P(ring.submit) to make sure
|
---|
661 | // entries are available. The semaphore make sure that there is no more operations in
|
---|
662 | // progress then the number of entries in the buffer. This probably limits concurrency
|
---|
663 | // more than necessary since submitted but not completed operations don't need any
|
---|
664 | // entries in user space. However, I don't know what happens if we overflow the buffers
|
---|
665 | // because too many requests completed at once. This is a safe approach in all cases.
|
---|
666 | // Furthermore, with hundreds of entries, this may be okay.
|
---|
667 | //
|
---|
668 | // 2 - Allocate a queue entry. The ring already has memory for all entries but only the ones
|
---|
669 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not
|
---|
670 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we
|
---|
671 | // need to write an allocator that allows allocating concurrently.
|
---|
672 | //
|
---|
673 | // 3 - Actually fill the submit entry, this is the only simple and straightforward step.
|
---|
674 | //
|
---|
675 | // 4 - Append the entry index to the array and adjust the tail accordingly. This operation
|
---|
676 | // needs to arrive to two concensus at the same time:
|
---|
677 | // A - The order in which entries are listed in the array: no two threads must pick the
|
---|
678 | // same index for their entries
|
---|
679 | // B - When can the tail be update for the kernel. EVERY entries in the array between
|
---|
680 | // head and tail must be fully filled and shouldn't ever be touched again.
|
---|
681 | //
|
---|
682 |
|
---|
683 | static inline [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ) {
|
---|
684 | verify( data != 0 );
|
---|
685 |
|
---|
686 |
|
---|
687 | // Prepare the data we need
|
---|
688 | __attribute((unused)) int len = 0;
|
---|
689 | __attribute((unused)) int block = 0;
|
---|
690 | uint32_t cnt = *ring.submit_q.num;
|
---|
691 | uint32_t mask = *ring.submit_q.mask;
|
---|
692 |
|
---|
693 | disable_interrupts();
|
---|
694 | uint32_t off = __tls_rand();
|
---|
695 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
696 |
|
---|
697 | // Loop around looking for an available spot
|
---|
698 | for() {
|
---|
699 | // Look through the list starting at some offset
|
---|
700 | for(i; cnt) {
|
---|
701 | uint64_t expected = 0;
|
---|
702 | uint32_t idx = (i + off) & mask;
|
---|
703 | struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx];
|
---|
704 | volatile uint64_t * udata = &sqe->user_data;
|
---|
705 |
|
---|
706 | if( *udata == expected &&
|
---|
707 | __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) )
|
---|
708 | {
|
---|
709 | // update statistics
|
---|
710 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
711 | disable_interrupts();
|
---|
712 | __tls_stats()->io.submit_q.alloc_avg.val += len;
|
---|
713 | __tls_stats()->io.submit_q.alloc_avg.block += block;
|
---|
714 | __tls_stats()->io.submit_q.alloc_avg.cnt += 1;
|
---|
715 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
716 | #endif
|
---|
717 |
|
---|
718 |
|
---|
719 | // Success return the data
|
---|
720 | return [sqe, idx];
|
---|
721 | }
|
---|
722 | verify(expected != data);
|
---|
723 |
|
---|
724 | len ++;
|
---|
725 | }
|
---|
726 |
|
---|
727 | block++;
|
---|
728 | yield();
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | static inline void __submit( struct __io_data & ring, uint32_t idx ) {
|
---|
733 | // Get now the data we definetely need
|
---|
734 | uint32_t * const tail = ring.submit_q.tail;
|
---|
735 | const uint32_t mask = *ring.submit_q.mask;
|
---|
736 |
|
---|
737 | disable_interrupts();
|
---|
738 |
|
---|
739 | // There are 2 submission schemes, check which one we are using
|
---|
740 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) {
|
---|
741 | // If the poller thread submits, then we just need to add this to the ready array
|
---|
742 |
|
---|
743 | /* paranoid */ verify( idx <= mask );
|
---|
744 | /* paranoid */ verify( idx != -1ul32 );
|
---|
745 |
|
---|
746 | // We need to find a spot in the ready array
|
---|
747 | __attribute((unused)) int len = 0;
|
---|
748 | __attribute((unused)) int block = 0;
|
---|
749 | uint32_t ready_mask = ring.submit_q.ready_cnt - 1;
|
---|
750 | uint32_t off = __tls_rand();
|
---|
751 | LOOKING: for() {
|
---|
752 | for(i; ring.submit_q.ready_cnt) {
|
---|
753 | uint32_t ii = (i + off) & ready_mask;
|
---|
754 | uint32_t expected = -1ul32;
|
---|
755 | if( __atomic_compare_exchange_n( &ring.submit_q.ready[ii], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) {
|
---|
756 | break LOOKING;
|
---|
757 | }
|
---|
758 | verify(expected != idx);
|
---|
759 |
|
---|
760 | len ++;
|
---|
761 | }
|
---|
762 |
|
---|
763 | block++;
|
---|
764 | yield();
|
---|
765 | }
|
---|
766 |
|
---|
767 | __wake_poller( ring );
|
---|
768 |
|
---|
769 | // update statistics
|
---|
770 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
771 | __tls_stats()->io.submit_q.look_avg.val += len;
|
---|
772 | __tls_stats()->io.submit_q.look_avg.block += block;
|
---|
773 | __tls_stats()->io.submit_q.look_avg.cnt += 1;
|
---|
774 | #endif
|
---|
775 |
|
---|
776 | __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() );
|
---|
777 | }
|
---|
778 | else {
|
---|
779 | // get mutual exclusion
|
---|
780 | lock(ring.submit_q.lock __cfaabi_dbg_ctx2);
|
---|
781 |
|
---|
782 | // Append to the list of ready entries
|
---|
783 |
|
---|
784 | /* paranoid */ verify( idx <= mask );
|
---|
785 |
|
---|
786 | ring.submit_q.array[ (*tail) & mask ] = idx & mask;
|
---|
787 | __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST);
|
---|
788 |
|
---|
789 | // Submit however, many entries need to be submitted
|
---|
790 | int ret = syscall( __NR_io_uring_enter, ring.fd, 1, 0, 0, 0p, 0);
|
---|
791 | if( ret < 0 ) {
|
---|
792 | switch((int)errno) {
|
---|
793 | default:
|
---|
794 | abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) );
|
---|
795 | }
|
---|
796 | }
|
---|
797 |
|
---|
798 | // update statistics
|
---|
799 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
800 | __tls_stats()->io.submit_q.submit_avg.csm += 1;
|
---|
801 | __tls_stats()->io.submit_q.submit_avg.cnt += 1;
|
---|
802 | #endif
|
---|
803 |
|
---|
804 | ring.submit_q.sqes[ idx & mask ].user_data = 0;
|
---|
805 |
|
---|
806 | unlock(ring.submit_q.lock);
|
---|
807 |
|
---|
808 | __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret );
|
---|
809 | }
|
---|
810 |
|
---|
811 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
812 | }
|
---|
813 |
|
---|
814 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd) {
|
---|
815 | this.opcode = opcode;
|
---|
816 | #if !defined(IOSQE_ASYNC)
|
---|
817 | this.flags = 0;
|
---|
818 | #else
|
---|
819 | this.flags = IOSQE_ASYNC;
|
---|
820 | #endif
|
---|
821 | this.ioprio = 0;
|
---|
822 | this.fd = fd;
|
---|
823 | this.off = 0;
|
---|
824 | this.addr = 0;
|
---|
825 | this.len = 0;
|
---|
826 | this.rw_flags = 0;
|
---|
827 | this.__pad2[0] = this.__pad2[1] = this.__pad2[2] = 0;
|
---|
828 | }
|
---|
829 |
|
---|
830 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd, void * addr, uint32_t len, uint64_t off ) {
|
---|
831 | (this){ opcode, fd };
|
---|
832 | this.off = off;
|
---|
833 | this.addr = (uint64_t)addr;
|
---|
834 | this.len = len;
|
---|
835 | }
|
---|
836 |
|
---|
837 |
|
---|
838 | //=============================================================================================
|
---|
839 | // I/O Interface
|
---|
840 | //=============================================================================================
|
---|
841 |
|
---|
842 | #define __submit_prelude \
|
---|
843 | io_user_data data = { 0, active_thread() }; \
|
---|
844 | struct __io_data & ring = *data.thrd->curr_cluster->io; \
|
---|
845 | struct io_uring_sqe * sqe; \
|
---|
846 | uint32_t idx; \
|
---|
847 | [sqe, idx] = __submit_alloc( ring, (uint64_t)&data );
|
---|
848 |
|
---|
849 | #define __submit_wait \
|
---|
850 | /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \
|
---|
851 | verify( sqe->user_data == (uint64_t)&data ); \
|
---|
852 | __submit( ring, idx ); \
|
---|
853 | park( __cfaabi_dbg_ctx ); \
|
---|
854 | return data.result;
|
---|
855 | #endif
|
---|
856 |
|
---|
857 | // Some forward declarations
|
---|
858 | extern "C" {
|
---|
859 | #include <unistd.h>
|
---|
860 | #include <sys/types.h>
|
---|
861 | #include <sys/socket.h>
|
---|
862 | #include <sys/syscall.h>
|
---|
863 |
|
---|
864 | #if defined(HAVE_PREADV2)
|
---|
865 | struct iovec;
|
---|
866 | extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
|
---|
867 | #endif
|
---|
868 | #if defined(HAVE_PWRITEV2)
|
---|
869 | struct iovec;
|
---|
870 | extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
|
---|
871 | #endif
|
---|
872 |
|
---|
873 | extern int fsync(int fd);
|
---|
874 | extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags);
|
---|
875 |
|
---|
876 | struct msghdr;
|
---|
877 | struct sockaddr;
|
---|
878 | extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
|
---|
879 | extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
|
---|
880 | extern ssize_t send(int sockfd, const void *buf, size_t len, int flags);
|
---|
881 | extern ssize_t recv(int sockfd, void *buf, size_t len, int flags);
|
---|
882 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
|
---|
883 | extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
---|
884 |
|
---|
885 | extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len);
|
---|
886 | extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice);
|
---|
887 | extern int madvise(void *addr, size_t length, int advice);
|
---|
888 |
|
---|
889 | extern int openat(int dirfd, const char *pathname, int flags, mode_t mode);
|
---|
890 | extern int close(int fd);
|
---|
891 |
|
---|
892 | extern ssize_t read (int fd, void *buf, size_t count);
|
---|
893 | }
|
---|
894 |
|
---|
895 | //-----------------------------------------------------------------------------
|
---|
896 | // Asynchronous operations
|
---|
897 | #if defined(HAVE_PREADV2)
|
---|
898 | ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
|
---|
899 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READV)
|
---|
900 | return preadv2(fd, iov, iovcnt, offset, flags);
|
---|
901 | #else
|
---|
902 | __submit_prelude
|
---|
903 |
|
---|
904 | (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset };
|
---|
905 |
|
---|
906 | __submit_wait
|
---|
907 | #endif
|
---|
908 | }
|
---|
909 | #endif
|
---|
910 |
|
---|
911 | #if defined(HAVE_PWRITEV2)
|
---|
912 | ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
|
---|
913 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITEV)
|
---|
914 | return pwritev2(fd, iov, iovcnt, offset, flags);
|
---|
915 | #else
|
---|
916 | __submit_prelude
|
---|
917 |
|
---|
918 | (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
|
---|
919 |
|
---|
920 | __submit_wait
|
---|
921 | #endif
|
---|
922 | }
|
---|
923 | #endif
|
---|
924 |
|
---|
925 | int cfa_fsync(int fd) {
|
---|
926 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FSYNC)
|
---|
927 | return fsync(fd);
|
---|
928 | #else
|
---|
929 | __submit_prelude
|
---|
930 |
|
---|
931 | (*sqe){ IORING_OP_FSYNC, fd };
|
---|
932 |
|
---|
933 | __submit_wait
|
---|
934 | #endif
|
---|
935 | }
|
---|
936 |
|
---|
937 | int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags) {
|
---|
938 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SYNC_FILE_RANGE)
|
---|
939 | return sync_file_range(fd, offset, nbytes, flags);
|
---|
940 | #else
|
---|
941 | __submit_prelude
|
---|
942 |
|
---|
943 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
|
---|
944 | sqe->off = offset;
|
---|
945 | sqe->len = nbytes;
|
---|
946 | sqe->sync_range_flags = flags;
|
---|
947 |
|
---|
948 | __submit_wait
|
---|
949 | #endif
|
---|
950 | }
|
---|
951 |
|
---|
952 |
|
---|
953 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
|
---|
954 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG)
|
---|
955 | return sendmsg(sockfd, msg, flags);
|
---|
956 | #else
|
---|
957 | __submit_prelude
|
---|
958 |
|
---|
959 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
|
---|
960 | sqe->msg_flags = flags;
|
---|
961 |
|
---|
962 | __submit_wait
|
---|
963 | #endif
|
---|
964 | }
|
---|
965 |
|
---|
966 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) {
|
---|
967 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG)
|
---|
968 | return recvmsg(sockfd, msg, flags);
|
---|
969 | #else
|
---|
970 | __submit_prelude
|
---|
971 |
|
---|
972 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
|
---|
973 | sqe->msg_flags = flags;
|
---|
974 |
|
---|
975 | __submit_wait
|
---|
976 | #endif
|
---|
977 | }
|
---|
978 |
|
---|
979 | ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags) {
|
---|
980 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SEND)
|
---|
981 | return send( sockfd, buf, len, flags );
|
---|
982 | #else
|
---|
983 | __submit_prelude
|
---|
984 |
|
---|
985 | (*sqe){ IORING_OP_SEND, sockfd };
|
---|
986 | sqe->addr = (uint64_t)buf;
|
---|
987 | sqe->len = len;
|
---|
988 | sqe->msg_flags = flags;
|
---|
989 |
|
---|
990 | __submit_wait
|
---|
991 | #endif
|
---|
992 | }
|
---|
993 |
|
---|
994 | ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags) {
|
---|
995 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECV)
|
---|
996 | return recv( sockfd, buf, len, flags );
|
---|
997 | #else
|
---|
998 | __submit_prelude
|
---|
999 |
|
---|
1000 | (*sqe){ IORING_OP_RECV, sockfd };
|
---|
1001 | sqe->addr = (uint64_t)buf;
|
---|
1002 | sqe->len = len;
|
---|
1003 | sqe->msg_flags = flags;
|
---|
1004 |
|
---|
1005 | __submit_wait
|
---|
1006 | #endif
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
|
---|
1010 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT)
|
---|
1011 | return accept4( sockfd, addr, addrlen, flags );
|
---|
1012 | #else
|
---|
1013 | __submit_prelude
|
---|
1014 |
|
---|
1015 | (*sqe){ IORING_OP_ACCEPT, sockfd };
|
---|
1016 | sqe->addr = addr;
|
---|
1017 | sqe->addr2 = addrlen;
|
---|
1018 | sqe->accept_flags = flags;
|
---|
1019 |
|
---|
1020 | __submit_wait
|
---|
1021 | #endif
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
---|
1025 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT)
|
---|
1026 | return connect( sockfd, addr, addrlen );
|
---|
1027 | #else
|
---|
1028 | __submit_prelude
|
---|
1029 |
|
---|
1030 | (*sqe){ IORING_OP_CONNECT, sockfd };
|
---|
1031 | sqe->addr = (uint64_t)addr;
|
---|
1032 | sqe->off = addrlen;
|
---|
1033 |
|
---|
1034 | __submit_wait
|
---|
1035 | #endif
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len) {
|
---|
1039 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FALLOCATE)
|
---|
1040 | return fallocate( fd, mode, offset, len );
|
---|
1041 | #else
|
---|
1042 | __submit_prelude
|
---|
1043 |
|
---|
1044 | (*sqe){ IORING_OP_FALLOCATE, fd };
|
---|
1045 | sqe->off = offset;
|
---|
1046 | sqe->len = length;
|
---|
1047 | sqe->mode = mode;
|
---|
1048 |
|
---|
1049 | __submit_wait
|
---|
1050 | #endif
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice) {
|
---|
1054 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FADVISE)
|
---|
1055 | return posix_fadvise( fd, offset, len, advice );
|
---|
1056 | #else
|
---|
1057 | __submit_prelude
|
---|
1058 |
|
---|
1059 | (*sqe){ IORING_OP_FADVISE, fd };
|
---|
1060 | sqe->off = (uint64_t)offset;
|
---|
1061 | sqe->len = length;
|
---|
1062 | sqe->fadvise_advice = advice;
|
---|
1063 |
|
---|
1064 | __submit_wait
|
---|
1065 | #endif
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | int cfa_madvise(void *addr, size_t length, int advice) {
|
---|
1069 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_MADVISE)
|
---|
1070 | return madvise( addr, length, advice );
|
---|
1071 | #else
|
---|
1072 | __submit_prelude
|
---|
1073 |
|
---|
1074 | (*sqe){ IORING_OP_MADVISE, 0 };
|
---|
1075 | sqe->addr = (uint64_t)addr;
|
---|
1076 | sqe->len = length;
|
---|
1077 | sqe->fadvise_advice = advice;
|
---|
1078 |
|
---|
1079 | __submit_wait
|
---|
1080 | #endif
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode) {
|
---|
1084 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_OPENAT)
|
---|
1085 | return openat( dirfd, pathname, flags, mode );
|
---|
1086 | #else
|
---|
1087 | __submit_prelude
|
---|
1088 |
|
---|
1089 | (*sqe){ IORING_OP_OPENAT, dirfd };
|
---|
1090 | sqe->addr = (uint64_t)pathname;
|
---|
1091 | sqe->open_flags = flags;
|
---|
1092 | sqe->mode = mode;
|
---|
1093 |
|
---|
1094 | __submit_wait
|
---|
1095 | #endif
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | int cfa_close(int fd) {
|
---|
1099 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CLOSE)
|
---|
1100 | return close( fd );
|
---|
1101 | #else
|
---|
1102 | __submit_prelude
|
---|
1103 |
|
---|
1104 | (*sqe){ IORING_OP_CLOSE, fd };
|
---|
1105 |
|
---|
1106 | __submit_wait
|
---|
1107 | #endif
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | ssize_t cfa_read(int fd, void *buf, size_t count) {
|
---|
1112 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READ)
|
---|
1113 | return read( fd, buf, count );
|
---|
1114 | #else
|
---|
1115 | __submit_prelude
|
---|
1116 |
|
---|
1117 | (*sqe){ IORING_OP_READ, fd, buf, count, 0 };
|
---|
1118 |
|
---|
1119 | __submit_wait
|
---|
1120 | #endif
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | ssize_t cfa_write(int fd, void *buf, size_t count) {
|
---|
1124 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITE)
|
---|
1125 | return read( fd, buf, count );
|
---|
1126 | #else
|
---|
1127 | __submit_prelude
|
---|
1128 |
|
---|
1129 | (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 };
|
---|
1130 |
|
---|
1131 | __submit_wait
|
---|
1132 | #endif
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | //-----------------------------------------------------------------------------
|
---|
1136 | // Check if a function is asynchronous
|
---|
1137 |
|
---|
1138 | // Macro magic to reduce the size of the following switch case
|
---|
1139 | #define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__)
|
---|
1140 | #define IS_DEFINED_SECOND(first, second, ...) second
|
---|
1141 | #define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion
|
---|
1142 | #define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true)
|
---|
1143 |
|
---|
1144 | bool has_user_level_blocking( fptr_t func ) {
|
---|
1145 | #if defined(HAVE_LINUX_IO_URING_H)
|
---|
1146 | #if defined(HAVE_PREADV2)
|
---|
1147 | if( /*func == (fptr_t)preadv2 || */
|
---|
1148 | func == (fptr_t)cfa_preadv2 )
|
---|
1149 | #define _CFA_IO_FEATURE_IORING_OP_READV ,
|
---|
1150 | return IS_DEFINED(IORING_OP_READV);
|
---|
1151 | #endif
|
---|
1152 |
|
---|
1153 | #if defined(HAVE_PWRITEV2)
|
---|
1154 | if( /*func == (fptr_t)pwritev2 || */
|
---|
1155 | func == (fptr_t)cfa_pwritev2 )
|
---|
1156 | #define _CFA_IO_FEATURE_IORING_OP_WRITEV ,
|
---|
1157 | return IS_DEFINED(IORING_OP_WRITEV);
|
---|
1158 | #endif
|
---|
1159 |
|
---|
1160 | if( /*func == (fptr_t)fsync || */
|
---|
1161 | func == (fptr_t)cfa_fsync )
|
---|
1162 | #define _CFA_IO_FEATURE_IORING_OP_FSYNC ,
|
---|
1163 | return IS_DEFINED(IORING_OP_FSYNC);
|
---|
1164 |
|
---|
1165 | if( /*func == (fptr_t)ync_file_range || */
|
---|
1166 | func == (fptr_t)cfa_sync_file_range )
|
---|
1167 | #define _CFA_IO_FEATURE_IORING_OP_SYNC_FILE_RANGE ,
|
---|
1168 | return IS_DEFINED(IORING_OP_SYNC_FILE_RANGE);
|
---|
1169 |
|
---|
1170 | if( /*func == (fptr_t)sendmsg || */
|
---|
1171 | func == (fptr_t)cfa_sendmsg )
|
---|
1172 | #define _CFA_IO_FEATURE_IORING_OP_SENDMSG ,
|
---|
1173 | return IS_DEFINED(IORING_OP_SENDMSG);
|
---|
1174 |
|
---|
1175 | if( /*func == (fptr_t)recvmsg || */
|
---|
1176 | func == (fptr_t)cfa_recvmsg )
|
---|
1177 | #define _CFA_IO_FEATURE_IORING_OP_RECVMSG ,
|
---|
1178 | return IS_DEFINED(IORING_OP_RECVMSG);
|
---|
1179 |
|
---|
1180 | if( /*func == (fptr_t)send || */
|
---|
1181 | func == (fptr_t)cfa_send )
|
---|
1182 | #define _CFA_IO_FEATURE_IORING_OP_SEND ,
|
---|
1183 | return IS_DEFINED(IORING_OP_SEND);
|
---|
1184 |
|
---|
1185 | if( /*func == (fptr_t)recv || */
|
---|
1186 | func == (fptr_t)cfa_recv )
|
---|
1187 | #define _CFA_IO_FEATURE_IORING_OP_RECV ,
|
---|
1188 | return IS_DEFINED(IORING_OP_RECV);
|
---|
1189 |
|
---|
1190 | if( /*func == (fptr_t)accept4 || */
|
---|
1191 | func == (fptr_t)cfa_accept4 )
|
---|
1192 | #define _CFA_IO_FEATURE_IORING_OP_ACCEPT ,
|
---|
1193 | return IS_DEFINED(IORING_OP_ACCEPT);
|
---|
1194 |
|
---|
1195 | if( /*func == (fptr_t)connect || */
|
---|
1196 | func == (fptr_t)cfa_connect )
|
---|
1197 | #define _CFA_IO_FEATURE_IORING_OP_CONNECT ,
|
---|
1198 | return IS_DEFINED(IORING_OP_CONNECT);
|
---|
1199 |
|
---|
1200 | if( /*func == (fptr_t)fallocate || */
|
---|
1201 | func == (fptr_t)cfa_fallocate )
|
---|
1202 | #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE ,
|
---|
1203 | return IS_DEFINED(IORING_OP_FALLOCATE);
|
---|
1204 |
|
---|
1205 | if( /*func == (fptr_t)posix_fadvise || */
|
---|
1206 | func == (fptr_t)cfa_fadvise )
|
---|
1207 | #define _CFA_IO_FEATURE_IORING_OP_FADVISE ,
|
---|
1208 | return IS_DEFINED(IORING_OP_FADVISE);
|
---|
1209 |
|
---|
1210 | if( /*func == (fptr_t)madvise || */
|
---|
1211 | func == (fptr_t)cfa_madvise )
|
---|
1212 | #define _CFA_IO_FEATURE_IORING_OP_MADVISE ,
|
---|
1213 | return IS_DEFINED(IORING_OP_MADVISE);
|
---|
1214 |
|
---|
1215 | if( /*func == (fptr_t)openat || */
|
---|
1216 | func == (fptr_t)cfa_openat )
|
---|
1217 | #define _CFA_IO_FEATURE_IORING_OP_OPENAT ,
|
---|
1218 | return IS_DEFINED(IORING_OP_OPENAT);
|
---|
1219 |
|
---|
1220 | if( /*func == (fptr_t)close || */
|
---|
1221 | func == (fptr_t)cfa_close )
|
---|
1222 | #define _CFA_IO_FEATURE_IORING_OP_CLOSE ,
|
---|
1223 | return IS_DEFINED(IORING_OP_CLOSE);
|
---|
1224 |
|
---|
1225 | if( /*func == (fptr_t)read || */
|
---|
1226 | func == (fptr_t)cfa_read )
|
---|
1227 | #define _CFA_IO_FEATURE_IORING_OP_READ ,
|
---|
1228 | return IS_DEFINED(IORING_OP_READ);
|
---|
1229 |
|
---|
1230 | if( /*func == (fptr_t)write || */
|
---|
1231 | func == (fptr_t)cfa_write )
|
---|
1232 | #define _CFA_IO_FEATURE_IORING_OP_WRITE ,
|
---|
1233 | return IS_DEFINED(IORING_OP_WRITE);
|
---|
1234 | #endif
|
---|
1235 |
|
---|
1236 | return false;
|
---|
1237 | }
|
---|