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 *)(uintptr_t)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, 0, 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 | #if !defined(__CFA_NO_STATISTICS__) |
---|
597 | __tally_stats(cltr->stats, &local_stats); |
---|
598 | #endif |
---|
599 | |
---|
600 | return 0p; |
---|
601 | } |
---|
602 | |
---|
603 | void main( __io_poller_fast & this ) { |
---|
604 | verify( this.ring->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ); |
---|
605 | |
---|
606 | // Start parked |
---|
607 | park( __cfaabi_dbg_ctx ); |
---|
608 | |
---|
609 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p ready\n", &this.ring); |
---|
610 | |
---|
611 | int reset = 0; |
---|
612 | |
---|
613 | // Then loop until we need to start |
---|
614 | while(!__atomic_load_n(&this.ring->done, __ATOMIC_SEQ_CST)) { |
---|
615 | |
---|
616 | // Drain the io |
---|
617 | int count; |
---|
618 | bool again; |
---|
619 | disable_interrupts(); |
---|
620 | [count, again] = __drain_io( *this.ring, 0p, 0, false ); |
---|
621 | |
---|
622 | if(!again) reset++; |
---|
623 | |
---|
624 | // Update statistics |
---|
625 | #if !defined(__CFA_NO_STATISTICS__) |
---|
626 | __tls_stats()->io.complete_q.completed_avg.val += count; |
---|
627 | __tls_stats()->io.complete_q.completed_avg.fast_cnt += 1; |
---|
628 | #endif |
---|
629 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
630 | |
---|
631 | // If we got something, just yield and check again |
---|
632 | if(reset < 5) { |
---|
633 | yield(); |
---|
634 | } |
---|
635 | // We didn't get anything baton pass to the slow poller |
---|
636 | else { |
---|
637 | __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to slow poller\n", &this.ring); |
---|
638 | reset = 0; |
---|
639 | |
---|
640 | // wake up the slow poller |
---|
641 | post( this.ring->poller.sem ); |
---|
642 | |
---|
643 | // park this thread |
---|
644 | park( __cfaabi_dbg_ctx ); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p stopping\n", &this.ring); |
---|
649 | } |
---|
650 | |
---|
651 | static inline void __wake_poller( struct __io_data & ring ) __attribute__((artificial)); |
---|
652 | static inline void __wake_poller( struct __io_data & ring ) { |
---|
653 | if(!__atomic_load_n( &ring.poller.slow.blocked, __ATOMIC_SEQ_CST)) return; |
---|
654 | |
---|
655 | sigval val = { 1 }; |
---|
656 | pthread_sigqueue( ring.poller.slow.kthrd, SIGUSR1, val ); |
---|
657 | } |
---|
658 | |
---|
659 | //============================================================================================= |
---|
660 | // I/O Submissions |
---|
661 | //============================================================================================= |
---|
662 | |
---|
663 | // Submition steps : |
---|
664 | // 1 - We need to make sure we don't overflow any of the buffer, P(ring.submit) to make sure |
---|
665 | // entries are available. The semaphore make sure that there is no more operations in |
---|
666 | // progress then the number of entries in the buffer. This probably limits concurrency |
---|
667 | // more than necessary since submitted but not completed operations don't need any |
---|
668 | // entries in user space. However, I don't know what happens if we overflow the buffers |
---|
669 | // because too many requests completed at once. This is a safe approach in all cases. |
---|
670 | // Furthermore, with hundreds of entries, this may be okay. |
---|
671 | // |
---|
672 | // 2 - Allocate a queue entry. The ring already has memory for all entries but only the ones |
---|
673 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not |
---|
674 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we |
---|
675 | // need to write an allocator that allows allocating concurrently. |
---|
676 | // |
---|
677 | // 3 - Actually fill the submit entry, this is the only simple and straightforward step. |
---|
678 | // |
---|
679 | // 4 - Append the entry index to the array and adjust the tail accordingly. This operation |
---|
680 | // needs to arrive to two concensus at the same time: |
---|
681 | // A - The order in which entries are listed in the array: no two threads must pick the |
---|
682 | // same index for their entries |
---|
683 | // B - When can the tail be update for the kernel. EVERY entries in the array between |
---|
684 | // head and tail must be fully filled and shouldn't ever be touched again. |
---|
685 | // |
---|
686 | |
---|
687 | // [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ) __attribute__((noinline)); |
---|
688 | static inline [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ) { |
---|
689 | verify( data != 0 ); |
---|
690 | |
---|
691 | |
---|
692 | // Prepare the data we need |
---|
693 | __attribute((unused)) int len = 0; |
---|
694 | __attribute((unused)) int block = 0; |
---|
695 | uint32_t cnt = *ring.submit_q.num; |
---|
696 | uint32_t mask = *ring.submit_q.mask; |
---|
697 | |
---|
698 | disable_interrupts(); |
---|
699 | uint32_t off = __tls_rand(); |
---|
700 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
701 | |
---|
702 | // Loop around looking for an available spot |
---|
703 | for() { |
---|
704 | // Look through the list starting at some offset |
---|
705 | for(i; cnt) { |
---|
706 | uint64_t expected = 0; |
---|
707 | uint32_t idx = (i + off) & mask; |
---|
708 | struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx]; |
---|
709 | volatile uint64_t * udata = &sqe->user_data; |
---|
710 | |
---|
711 | if( *udata == expected && |
---|
712 | __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) |
---|
713 | { |
---|
714 | // update statistics |
---|
715 | #if !defined(__CFA_NO_STATISTICS__) |
---|
716 | disable_interrupts(); |
---|
717 | __tls_stats()->io.submit_q.alloc_avg.val += len; |
---|
718 | __tls_stats()->io.submit_q.alloc_avg.block += block; |
---|
719 | __tls_stats()->io.submit_q.alloc_avg.cnt += 1; |
---|
720 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
721 | #endif |
---|
722 | |
---|
723 | |
---|
724 | // Success return the data |
---|
725 | return [sqe, idx]; |
---|
726 | } |
---|
727 | verify(expected != data); |
---|
728 | |
---|
729 | len ++; |
---|
730 | } |
---|
731 | |
---|
732 | block++; |
---|
733 | yield(); |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | static inline uint32_t __submit_to_ready_array( struct __io_data & ring, uint32_t idx, const uint32_t mask ) { |
---|
738 | /* paranoid */ verify( idx <= mask ); |
---|
739 | /* paranoid */ verify( idx != -1ul32 ); |
---|
740 | |
---|
741 | // We need to find a spot in the ready array |
---|
742 | __attribute((unused)) int len = 0; |
---|
743 | __attribute((unused)) int block = 0; |
---|
744 | uint32_t ready_mask = ring.submit_q.ready_cnt - 1; |
---|
745 | |
---|
746 | disable_interrupts(); |
---|
747 | uint32_t off = __tls_rand(); |
---|
748 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
749 | |
---|
750 | uint32_t picked; |
---|
751 | LOOKING: for() { |
---|
752 | for(i; ring.submit_q.ready_cnt) { |
---|
753 | picked = (i + off) & ready_mask; |
---|
754 | uint32_t expected = -1ul32; |
---|
755 | if( __atomic_compare_exchange_n( &ring.submit_q.ready[picked], &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 | // update statistics |
---|
768 | #if !defined(__CFA_NO_STATISTICS__) |
---|
769 | disable_interrupts(); |
---|
770 | __tls_stats()->io.submit_q.look_avg.val += len; |
---|
771 | __tls_stats()->io.submit_q.look_avg.block += block; |
---|
772 | __tls_stats()->io.submit_q.look_avg.cnt += 1; |
---|
773 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
774 | #endif |
---|
775 | |
---|
776 | return picked; |
---|
777 | } |
---|
778 | |
---|
779 | // void __submit( struct __io_data & ring, uint32_t idx ) __attribute__((noinline)); |
---|
780 | static inline void __submit( struct __io_data & ring, uint32_t idx ) { |
---|
781 | // Get now the data we definetely need |
---|
782 | uint32_t * const tail = ring.submit_q.tail; |
---|
783 | const uint32_t mask = *ring.submit_q.mask; |
---|
784 | |
---|
785 | // There are 2 submission schemes, check which one we are using |
---|
786 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) { |
---|
787 | // If the poller thread submits, then we just need to add this to the ready array |
---|
788 | |
---|
789 | __submit_to_ready_array( ring, idx, mask ); |
---|
790 | |
---|
791 | __wake_poller( ring ); |
---|
792 | |
---|
793 | __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() ); |
---|
794 | } |
---|
795 | else { |
---|
796 | // get mutual exclusion |
---|
797 | lock(ring.submit_q.lock __cfaabi_dbg_ctx2); |
---|
798 | |
---|
799 | // Append to the list of ready entries |
---|
800 | |
---|
801 | /* paranoid */ verify( idx <= mask ); |
---|
802 | |
---|
803 | ring.submit_q.array[ (*tail) & mask ] = idx & mask; |
---|
804 | __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST); |
---|
805 | |
---|
806 | // Submit however, many entries need to be submitted |
---|
807 | int ret = syscall( __NR_io_uring_enter, ring.fd, 1, 0, 0, 0p, 0); |
---|
808 | if( ret < 0 ) { |
---|
809 | switch((int)errno) { |
---|
810 | default: |
---|
811 | abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) ); |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | // update statistics |
---|
816 | #if !defined(__CFA_NO_STATISTICS__) |
---|
817 | __tls_stats()->io.submit_q.submit_avg.csm += 1; |
---|
818 | __tls_stats()->io.submit_q.submit_avg.cnt += 1; |
---|
819 | #endif |
---|
820 | |
---|
821 | ring.submit_q.sqes[ idx & mask ].user_data = 0; |
---|
822 | |
---|
823 | unlock(ring.submit_q.lock); |
---|
824 | |
---|
825 | __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret ); |
---|
826 | } |
---|
827 | } |
---|
828 | |
---|
829 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd) { |
---|
830 | this.opcode = opcode; |
---|
831 | #if !defined(IOSQE_ASYNC) |
---|
832 | this.flags = 0; |
---|
833 | #else |
---|
834 | this.flags = IOSQE_ASYNC; |
---|
835 | #endif |
---|
836 | this.ioprio = 0; |
---|
837 | this.fd = fd; |
---|
838 | this.off = 0; |
---|
839 | this.addr = 0; |
---|
840 | this.len = 0; |
---|
841 | this.rw_flags = 0; |
---|
842 | this.__pad2[0] = this.__pad2[1] = this.__pad2[2] = 0; |
---|
843 | } |
---|
844 | |
---|
845 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd, void * addr, uint32_t len, uint64_t off ) { |
---|
846 | (this){ opcode, fd }; |
---|
847 | this.off = off; |
---|
848 | this.addr = (uint64_t)(uintptr_t)addr; |
---|
849 | this.len = len; |
---|
850 | } |
---|
851 | |
---|
852 | |
---|
853 | //============================================================================================= |
---|
854 | // I/O Interface |
---|
855 | //============================================================================================= |
---|
856 | |
---|
857 | #define __submit_prelude \ |
---|
858 | io_user_data data = { 0, active_thread() }; \ |
---|
859 | struct __io_data & ring = *data.thrd->curr_cluster->io; \ |
---|
860 | struct io_uring_sqe * sqe; \ |
---|
861 | uint32_t idx; \ |
---|
862 | [sqe, idx] = __submit_alloc( ring, (uint64_t)(uintptr_t)&data ); |
---|
863 | |
---|
864 | #define __submit_wait \ |
---|
865 | /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \ |
---|
866 | verify( sqe->user_data == (uint64_t)(uintptr_t)&data ); \ |
---|
867 | __submit( ring, idx ); \ |
---|
868 | park( __cfaabi_dbg_ctx ); \ |
---|
869 | return data.result; |
---|
870 | #endif |
---|
871 | |
---|
872 | // Some forward declarations |
---|
873 | extern "C" { |
---|
874 | #include <unistd.h> |
---|
875 | #include <sys/types.h> |
---|
876 | #include <sys/socket.h> |
---|
877 | #include <sys/syscall.h> |
---|
878 | |
---|
879 | #if defined(HAVE_PREADV2) |
---|
880 | struct iovec; |
---|
881 | extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
882 | #endif |
---|
883 | #if defined(HAVE_PWRITEV2) |
---|
884 | struct iovec; |
---|
885 | extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
886 | #endif |
---|
887 | |
---|
888 | extern int fsync(int fd); |
---|
889 | extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags); |
---|
890 | |
---|
891 | struct msghdr; |
---|
892 | struct sockaddr; |
---|
893 | extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); |
---|
894 | extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); |
---|
895 | extern ssize_t send(int sockfd, const void *buf, size_t len, int flags); |
---|
896 | extern ssize_t recv(int sockfd, void *buf, size_t len, int flags); |
---|
897 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
898 | extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
---|
899 | |
---|
900 | extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len); |
---|
901 | extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice); |
---|
902 | extern int madvise(void *addr, size_t length, int advice); |
---|
903 | |
---|
904 | extern int openat(int dirfd, const char *pathname, int flags, mode_t mode); |
---|
905 | extern int close(int fd); |
---|
906 | |
---|
907 | extern ssize_t read (int fd, void *buf, size_t count); |
---|
908 | } |
---|
909 | |
---|
910 | //----------------------------------------------------------------------------- |
---|
911 | // Asynchronous operations |
---|
912 | #if defined(HAVE_PREADV2) |
---|
913 | ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) { |
---|
914 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READV) |
---|
915 | return preadv2(fd, iov, iovcnt, offset, flags); |
---|
916 | #else |
---|
917 | __submit_prelude |
---|
918 | |
---|
919 | (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset }; |
---|
920 | |
---|
921 | __submit_wait |
---|
922 | #endif |
---|
923 | } |
---|
924 | #endif |
---|
925 | |
---|
926 | #if defined(HAVE_PWRITEV2) |
---|
927 | ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) { |
---|
928 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITEV) |
---|
929 | return pwritev2(fd, iov, iovcnt, offset, flags); |
---|
930 | #else |
---|
931 | __submit_prelude |
---|
932 | |
---|
933 | (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset }; |
---|
934 | |
---|
935 | __submit_wait |
---|
936 | #endif |
---|
937 | } |
---|
938 | #endif |
---|
939 | |
---|
940 | int cfa_fsync(int fd) { |
---|
941 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FSYNC) |
---|
942 | return fsync(fd); |
---|
943 | #else |
---|
944 | __submit_prelude |
---|
945 | |
---|
946 | (*sqe){ IORING_OP_FSYNC, fd }; |
---|
947 | |
---|
948 | __submit_wait |
---|
949 | #endif |
---|
950 | } |
---|
951 | |
---|
952 | int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags) { |
---|
953 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SYNC_FILE_RANGE) |
---|
954 | return sync_file_range(fd, offset, nbytes, flags); |
---|
955 | #else |
---|
956 | __submit_prelude |
---|
957 | |
---|
958 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd }; |
---|
959 | sqe->off = offset; |
---|
960 | sqe->len = nbytes; |
---|
961 | sqe->sync_range_flags = flags; |
---|
962 | |
---|
963 | __submit_wait |
---|
964 | #endif |
---|
965 | } |
---|
966 | |
---|
967 | |
---|
968 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) { |
---|
969 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG) |
---|
970 | return sendmsg(sockfd, msg, flags); |
---|
971 | #else |
---|
972 | __submit_prelude |
---|
973 | |
---|
974 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 }; |
---|
975 | sqe->msg_flags = flags; |
---|
976 | |
---|
977 | __submit_wait |
---|
978 | #endif |
---|
979 | } |
---|
980 | |
---|
981 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) { |
---|
982 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG) |
---|
983 | return recvmsg(sockfd, msg, flags); |
---|
984 | #else |
---|
985 | __submit_prelude |
---|
986 | |
---|
987 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 }; |
---|
988 | sqe->msg_flags = flags; |
---|
989 | |
---|
990 | __submit_wait |
---|
991 | #endif |
---|
992 | } |
---|
993 | |
---|
994 | ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags) { |
---|
995 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SEND) |
---|
996 | return send( sockfd, buf, len, flags ); |
---|
997 | #else |
---|
998 | __submit_prelude |
---|
999 | |
---|
1000 | (*sqe){ IORING_OP_SEND, 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 | ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags) { |
---|
1010 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECV) |
---|
1011 | return recv( sockfd, buf, len, flags ); |
---|
1012 | #else |
---|
1013 | __submit_prelude |
---|
1014 | |
---|
1015 | (*sqe){ IORING_OP_RECV, sockfd }; |
---|
1016 | sqe->addr = (uint64_t)buf; |
---|
1017 | sqe->len = len; |
---|
1018 | sqe->msg_flags = flags; |
---|
1019 | |
---|
1020 | __submit_wait |
---|
1021 | #endif |
---|
1022 | } |
---|
1023 | |
---|
1024 | int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { |
---|
1025 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT) |
---|
1026 | return accept4( sockfd, addr, addrlen, flags ); |
---|
1027 | #else |
---|
1028 | __submit_prelude |
---|
1029 | |
---|
1030 | (*sqe){ IORING_OP_ACCEPT, sockfd }; |
---|
1031 | sqe->addr = addr; |
---|
1032 | sqe->addr2 = addrlen; |
---|
1033 | sqe->accept_flags = flags; |
---|
1034 | |
---|
1035 | __submit_wait |
---|
1036 | #endif |
---|
1037 | } |
---|
1038 | |
---|
1039 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { |
---|
1040 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT) |
---|
1041 | return connect( sockfd, addr, addrlen ); |
---|
1042 | #else |
---|
1043 | __submit_prelude |
---|
1044 | |
---|
1045 | (*sqe){ IORING_OP_CONNECT, sockfd }; |
---|
1046 | sqe->addr = (uint64_t)addr; |
---|
1047 | sqe->off = addrlen; |
---|
1048 | |
---|
1049 | __submit_wait |
---|
1050 | #endif |
---|
1051 | } |
---|
1052 | |
---|
1053 | int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len) { |
---|
1054 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FALLOCATE) |
---|
1055 | return fallocate( fd, mode, offset, len ); |
---|
1056 | #else |
---|
1057 | __submit_prelude |
---|
1058 | |
---|
1059 | (*sqe){ IORING_OP_FALLOCATE, fd }; |
---|
1060 | sqe->off = offset; |
---|
1061 | sqe->len = length; |
---|
1062 | sqe->mode = mode; |
---|
1063 | |
---|
1064 | __submit_wait |
---|
1065 | #endif |
---|
1066 | } |
---|
1067 | |
---|
1068 | int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice) { |
---|
1069 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FADVISE) |
---|
1070 | return posix_fadvise( fd, offset, len, advice ); |
---|
1071 | #else |
---|
1072 | __submit_prelude |
---|
1073 | |
---|
1074 | (*sqe){ IORING_OP_FADVISE, fd }; |
---|
1075 | sqe->off = (uint64_t)offset; |
---|
1076 | sqe->len = length; |
---|
1077 | sqe->fadvise_advice = advice; |
---|
1078 | |
---|
1079 | __submit_wait |
---|
1080 | #endif |
---|
1081 | } |
---|
1082 | |
---|
1083 | int cfa_madvise(void *addr, size_t length, int advice) { |
---|
1084 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_MADVISE) |
---|
1085 | return madvise( addr, length, advice ); |
---|
1086 | #else |
---|
1087 | __submit_prelude |
---|
1088 | |
---|
1089 | (*sqe){ IORING_OP_MADVISE, 0 }; |
---|
1090 | sqe->addr = (uint64_t)addr; |
---|
1091 | sqe->len = length; |
---|
1092 | sqe->fadvise_advice = advice; |
---|
1093 | |
---|
1094 | __submit_wait |
---|
1095 | #endif |
---|
1096 | } |
---|
1097 | |
---|
1098 | int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode) { |
---|
1099 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_OPENAT) |
---|
1100 | return openat( dirfd, pathname, flags, mode ); |
---|
1101 | #else |
---|
1102 | __submit_prelude |
---|
1103 | |
---|
1104 | (*sqe){ IORING_OP_OPENAT, dirfd }; |
---|
1105 | sqe->addr = (uint64_t)pathname; |
---|
1106 | sqe->open_flags = flags; |
---|
1107 | sqe->mode = mode; |
---|
1108 | |
---|
1109 | __submit_wait |
---|
1110 | #endif |
---|
1111 | } |
---|
1112 | |
---|
1113 | int cfa_close(int fd) { |
---|
1114 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CLOSE) |
---|
1115 | return close( fd ); |
---|
1116 | #else |
---|
1117 | __submit_prelude |
---|
1118 | |
---|
1119 | (*sqe){ IORING_OP_CLOSE, fd }; |
---|
1120 | |
---|
1121 | __submit_wait |
---|
1122 | #endif |
---|
1123 | } |
---|
1124 | |
---|
1125 | |
---|
1126 | ssize_t cfa_read(int fd, void *buf, size_t count) { |
---|
1127 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READ) |
---|
1128 | return read( fd, buf, count ); |
---|
1129 | #else |
---|
1130 | __submit_prelude |
---|
1131 | |
---|
1132 | (*sqe){ IORING_OP_READ, fd, buf, count, 0 }; |
---|
1133 | |
---|
1134 | __submit_wait |
---|
1135 | #endif |
---|
1136 | } |
---|
1137 | |
---|
1138 | ssize_t cfa_write(int fd, void *buf, size_t count) { |
---|
1139 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITE) |
---|
1140 | return read( fd, buf, count ); |
---|
1141 | #else |
---|
1142 | __submit_prelude |
---|
1143 | |
---|
1144 | (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 }; |
---|
1145 | |
---|
1146 | __submit_wait |
---|
1147 | #endif |
---|
1148 | } |
---|
1149 | |
---|
1150 | //----------------------------------------------------------------------------- |
---|
1151 | // Check if a function is asynchronous |
---|
1152 | |
---|
1153 | // Macro magic to reduce the size of the following switch case |
---|
1154 | #define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__) |
---|
1155 | #define IS_DEFINED_SECOND(first, second, ...) second |
---|
1156 | #define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion |
---|
1157 | #define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true) |
---|
1158 | |
---|
1159 | bool has_user_level_blocking( fptr_t func ) { |
---|
1160 | #if defined(HAVE_LINUX_IO_URING_H) |
---|
1161 | #if defined(HAVE_PREADV2) |
---|
1162 | if( /*func == (fptr_t)preadv2 || */ |
---|
1163 | func == (fptr_t)cfa_preadv2 ) |
---|
1164 | #define _CFA_IO_FEATURE_IORING_OP_READV , |
---|
1165 | return IS_DEFINED(IORING_OP_READV); |
---|
1166 | #endif |
---|
1167 | |
---|
1168 | #if defined(HAVE_PWRITEV2) |
---|
1169 | if( /*func == (fptr_t)pwritev2 || */ |
---|
1170 | func == (fptr_t)cfa_pwritev2 ) |
---|
1171 | #define _CFA_IO_FEATURE_IORING_OP_WRITEV , |
---|
1172 | return IS_DEFINED(IORING_OP_WRITEV); |
---|
1173 | #endif |
---|
1174 | |
---|
1175 | if( /*func == (fptr_t)fsync || */ |
---|
1176 | func == (fptr_t)cfa_fsync ) |
---|
1177 | #define _CFA_IO_FEATURE_IORING_OP_FSYNC , |
---|
1178 | return IS_DEFINED(IORING_OP_FSYNC); |
---|
1179 | |
---|
1180 | if( /*func == (fptr_t)ync_file_range || */ |
---|
1181 | func == (fptr_t)cfa_sync_file_range ) |
---|
1182 | #define _CFA_IO_FEATURE_IORING_OP_SYNC_FILE_RANGE , |
---|
1183 | return IS_DEFINED(IORING_OP_SYNC_FILE_RANGE); |
---|
1184 | |
---|
1185 | if( /*func == (fptr_t)sendmsg || */ |
---|
1186 | func == (fptr_t)cfa_sendmsg ) |
---|
1187 | #define _CFA_IO_FEATURE_IORING_OP_SENDMSG , |
---|
1188 | return IS_DEFINED(IORING_OP_SENDMSG); |
---|
1189 | |
---|
1190 | if( /*func == (fptr_t)recvmsg || */ |
---|
1191 | func == (fptr_t)cfa_recvmsg ) |
---|
1192 | #define _CFA_IO_FEATURE_IORING_OP_RECVMSG , |
---|
1193 | return IS_DEFINED(IORING_OP_RECVMSG); |
---|
1194 | |
---|
1195 | if( /*func == (fptr_t)send || */ |
---|
1196 | func == (fptr_t)cfa_send ) |
---|
1197 | #define _CFA_IO_FEATURE_IORING_OP_SEND , |
---|
1198 | return IS_DEFINED(IORING_OP_SEND); |
---|
1199 | |
---|
1200 | if( /*func == (fptr_t)recv || */ |
---|
1201 | func == (fptr_t)cfa_recv ) |
---|
1202 | #define _CFA_IO_FEATURE_IORING_OP_RECV , |
---|
1203 | return IS_DEFINED(IORING_OP_RECV); |
---|
1204 | |
---|
1205 | if( /*func == (fptr_t)accept4 || */ |
---|
1206 | func == (fptr_t)cfa_accept4 ) |
---|
1207 | #define _CFA_IO_FEATURE_IORING_OP_ACCEPT , |
---|
1208 | return IS_DEFINED(IORING_OP_ACCEPT); |
---|
1209 | |
---|
1210 | if( /*func == (fptr_t)connect || */ |
---|
1211 | func == (fptr_t)cfa_connect ) |
---|
1212 | #define _CFA_IO_FEATURE_IORING_OP_CONNECT , |
---|
1213 | return IS_DEFINED(IORING_OP_CONNECT); |
---|
1214 | |
---|
1215 | if( /*func == (fptr_t)fallocate || */ |
---|
1216 | func == (fptr_t)cfa_fallocate ) |
---|
1217 | #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE , |
---|
1218 | return IS_DEFINED(IORING_OP_FALLOCATE); |
---|
1219 | |
---|
1220 | if( /*func == (fptr_t)posix_fadvise || */ |
---|
1221 | func == (fptr_t)cfa_fadvise ) |
---|
1222 | #define _CFA_IO_FEATURE_IORING_OP_FADVISE , |
---|
1223 | return IS_DEFINED(IORING_OP_FADVISE); |
---|
1224 | |
---|
1225 | if( /*func == (fptr_t)madvise || */ |
---|
1226 | func == (fptr_t)cfa_madvise ) |
---|
1227 | #define _CFA_IO_FEATURE_IORING_OP_MADVISE , |
---|
1228 | return IS_DEFINED(IORING_OP_MADVISE); |
---|
1229 | |
---|
1230 | if( /*func == (fptr_t)openat || */ |
---|
1231 | func == (fptr_t)cfa_openat ) |
---|
1232 | #define _CFA_IO_FEATURE_IORING_OP_OPENAT , |
---|
1233 | return IS_DEFINED(IORING_OP_OPENAT); |
---|
1234 | |
---|
1235 | if( /*func == (fptr_t)close || */ |
---|
1236 | func == (fptr_t)cfa_close ) |
---|
1237 | #define _CFA_IO_FEATURE_IORING_OP_CLOSE , |
---|
1238 | return IS_DEFINED(IORING_OP_CLOSE); |
---|
1239 | |
---|
1240 | if( /*func == (fptr_t)read || */ |
---|
1241 | func == (fptr_t)cfa_read ) |
---|
1242 | #define _CFA_IO_FEATURE_IORING_OP_READ , |
---|
1243 | return IS_DEFINED(IORING_OP_READ); |
---|
1244 | |
---|
1245 | if( /*func == (fptr_t)write || */ |
---|
1246 | func == (fptr_t)cfa_write ) |
---|
1247 | #define _CFA_IO_FEATURE_IORING_OP_WRITE , |
---|
1248 | return IS_DEFINED(IORING_OP_WRITE); |
---|
1249 | #endif |
---|
1250 | |
---|
1251 | return false; |
---|
1252 | } |
---|