source: libcfa/src/concurrency/io.cfa@ 8291293

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 8291293 was 7812a7b5, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed errors and warning with x86 build

  • Property mode set to 100644
File size: 37.2 KB
RevLine 
[ecf6b46]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
[4069faad]16// #define __CFA_DEBUG_PRINT_IO__
[0a805f2]17// #define __CFA_DEBUG_PRINT_IO_CORE__
[4069faad]18
[92976d9]19#include "kernel.hfa"
[5c581cc]20#include "bitmanip.hfa"
[92976d9]21
22#if !defined(HAVE_LINUX_IO_URING_H)
[dd4e2d7]23 void __kernel_io_startup( cluster &, unsigned, bool ) {
[92976d9]24 // Nothing to do without io_uring
25 }
26
[3f7d0b4]27 void __kernel_io_finish_start( cluster & ) {
[f6660520]28 // Nothing to do without io_uring
29 }
30
[3f7d0b4]31 void __kernel_io_prepare_stop( cluster & ) {
[f6660520]32 // Nothing to do without io_uring
33 }
34
[3f7d0b4]35 void __kernel_io_shutdown( cluster &, bool ) {
[92976d9]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
[f6660520]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
[61dd73d]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
[5dadc9b7]126 volatile uint32_t * ready;
127 uint32_t ready_cnt;
[61dd73d]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;
[b6f2b213]162 uint32_t ring_flags;
163 int cltr_flags;
[61dd73d]164 int fd;
165 semaphore submit;
166 volatile bool done;
167 struct {
168 struct {
[13c5e19]169 __processor_id_t id;
[61dd73d]170 void * stack;
171 pthread_t kthrd;
[5c581cc]172 volatile bool blocked;
[61dd73d]173 } slow;
174 __io_poller_fast fast;
175 __bin_sem_t sem;
176 } poller;
177 };
[185efe6]178
[92976d9]179//=============================================================================================
180// I/O Startup / Shutdown logic
181//=============================================================================================
[dd4e2d7]182 void __kernel_io_startup( cluster & this, unsigned io_flags, bool main_cluster ) {
[61dd73d]183 this.io = malloc();
184
[92976d9]185 // Step 1 : call to setup
186 struct io_uring_params params;
187 memset(&params, 0, sizeof(params));
188
[2d8f7b0]189 uint32_t nentries = entries_per_cluster();
190
191 int fd = syscall(__NR_io_uring_setup, nentries, &params );
[92976d9]192 if(fd < 0) {
193 abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
194 }
195
196 // Step 2 : mmap result
[61dd73d]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;
[92976d9]200
201 // calculate the right ring size
[2d8f7b0]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));
[92976d9]204
205 // Requires features
[d384787]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
[92976d9]212
213 // mmap the Submit Queue into existence
[2d8f7b0]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) {
[92976d9]216 abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
217 }
218
219 // Requires features
[d384787]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 {
[92976d9]228 // We need multiple call to MMAP
[2d8f7b0]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);
[92976d9]232 abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
233 }
[d384787]234 }
[92976d9]235
236 // mmap the submit queue entries
237 size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
[2d8f7b0]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);
[92976d9]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
[2d8f7b0]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);
[6f121b8]254
255 {
256 const uint32_t num = *sq.num;
257 for( i; num ) {
258 sq.sqes[i].user_data = 0ul64;
259 }
260 }
[5dadc9b7]261
262 if( io_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) {
[5c581cc]263 /* paranoid */ verify( is_pow2( io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET ) || ((io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET) < 8) );
[dd4e2d7]264 sq.ready_cnt = max(io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET, 8);
[0335620]265 sq.ready = alloc_align( 64, sq.ready_cnt );
[5dadc9b7]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 }
[92976d9]274
275 // completion queue
[2d8f7b0]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 );
[92976d9]293
294 // Update the global ring info
[b6f2b213]295 this.io->ring_flags = params.flags;
296 this.io->cltr_flags = io_flags;
297 this.io->fd = fd;
298 this.io->done = false;
[61dd73d]299 (this.io->submit){ min(*sq.num, *cq.num) };
[92976d9]300
[f6660520]301 if(!main_cluster) {
302 __kernel_io_finish_start( this );
303 }
304 }
305
306 void __kernel_io_finish_start( cluster & this ) {
[b6f2b213]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 }
[f6660520]312
[92976d9]313 // Create the poller thread
[0a805f2]314 __cfadbg_print_safe(io_core, "Kernel I/O : Creating slow poller for cluter %p\n", &this);
[5c581cc]315 this.io->poller.slow.blocked = false;
[61dd73d]316 this.io->poller.slow.stack = __create_pthread( &this.io->poller.slow.kthrd, __io_poller_slow, &this );
[92976d9]317 }
318
[f6660520]319 void __kernel_io_prepare_stop( cluster & this ) {
[0a805f2]320 __cfadbg_print_safe(io_core, "Kernel I/O : Stopping pollers for cluster\n", &this);
[92976d9]321 // Notify the poller thread of the shutdown
[61dd73d]322 __atomic_store_n(&this.io->done, true, __ATOMIC_SEQ_CST);
[f6660520]323
324 // Stop the IO Poller
[92976d9]325 sigval val = { 1 };
[61dd73d]326 pthread_sigqueue( this.io->poller.slow.kthrd, SIGUSR1, val );
327 post( this.io->poller.sem );
[92976d9]328
329 // Wait for the poller thread to finish
[61dd73d]330 pthread_join( this.io->poller.slow.kthrd, 0p );
331 free( this.io->poller.slow.stack );
[f6660520]332
[0a805f2]333 __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller stopped for cluster\n", &this);
[4069faad]334
[b6f2b213]335 if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) {
[05cfa4d]336 with( this.io->poller.fast ) {
[13c5e19]337 /* paranoid */ verify( this.nprocessors == 0 || &this == mainCluster );
338 /* paranoid */ verify( !ready_mutate_islocked() );
[05cfa4d]339
340 // We need to adjust the clean-up based on where the thread is
[5dadc9b7]341 if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) {
[05cfa4d]342
[13c5e19]343 ready_schedule_lock( (struct __processor_id_t *)active_processor() );
[2f1cb37]344
[13c5e19]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 );
[05cfa4d]349
[13c5e19]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 );
[05cfa4d]356
[13c5e19]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() );
[05cfa4d]363
364 // Pretend like the thread was blocked all along
365 }
366 // !!! This is not an else if !!!
367 if( thrd.state == Blocked ) {
[6502a2b]368
[05cfa4d]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();
[6502a2b]373
[13c5e19]374 // unpark the fast io_poller
[05cfa4d]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 }
[f6660520]385
[61dd73d]386 ^(this.io->poller.fast){};
[4069faad]387
[0a805f2]388 __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller stopped for cluster\n", &this);
[b6f2b213]389 }
[f6660520]390 }
391
392 void __kernel_io_shutdown( cluster & this, bool main_cluster ) {
393 if(!main_cluster) {
394 __kernel_io_prepare_stop( this );
395 }
[92976d9]396
397 // Shutdown the io rings
[61dd73d]398 struct __submition_data & sq = this.io->submit_q;
399 struct __completion_data & cq = this.io->completion_q;
[92976d9]400
401 // unmap the submit queue entries
[2d8f7b0]402 munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
[92976d9]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
[61dd73d]413 close(this.io->fd);
414
[5dadc9b7]415 free( this.io->submit_q.ready ); // Maybe null, doesn't matter
[61dd73d]416 free( this.io );
[92976d9]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
[5dadc9b7]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
[6f121b8]434 uint32_t tail = *ring.submit_q.tail;
[5dadc9b7]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
[6f121b8]446 ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask;
[5dadc9b7]447 to_submit++;
448 }
449
450 // Increment the tail based on how many we are ready to submit
[6f121b8]451 __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST);
[5dadc9b7]452 }
453
[6f121b8]454 const uint32_t smask = *ring.submit_q.mask;
455 uint32_t shead = *ring.submit_q.head;
[5dadc9b7]456 int ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, waitcnt, IORING_ENTER_GETEVENTS, mask, _NSIG / 8);
[d384787]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
[6f121b8]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__)
[13c5e19]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;
[6f121b8]485 #endif
486
[d384787]487 // Drain the queue
[92976d9]488 unsigned head = *ring.completion_q.head;
[6f121b8]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 );
[92976d9]494
[d384787]495 // Nothing was new return 0
496 if (head == tail) {
497 return 0;
498 }
[92976d9]499
[d384787]500 uint32_t count = tail - head;
501 for(i; count) {
[6f121b8]502 unsigned idx = (head + i) & mask;
[d384787]503 struct io_uring_cqe & cqe = ring.completion_q.cqes[idx];
[92976d9]504
[d384787]505 /* paranoid */ verify(&cqe);
[92976d9]506
[7812a7b5]507 struct io_user_data * data = (struct io_user_data *)(uintptr_t)cqe.user_data;
[4069faad]508 __cfadbg_print_safe( io, "Kernel I/O : Performed reading io cqe %p, result %d for %p\n", data, cqe.res, data->thrd );
[2d8f7b0]509
[d384787]510 data->result = cqe.res;
[f6660520]511 if(!in_kernel) { unpark( data->thrd __cfaabi_dbg_ctx2 ); }
[13c5e19]512 else { __unpark( &ring.poller.slow.id, data->thrd __cfaabi_dbg_ctx2 ); }
[d384787]513 }
[2d8f7b0]514
515 // Allow new submissions to happen
[6f121b8]516 // V(ring.submit, count);
[92976d9]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.
[6f121b8]520 __atomic_thread_fence( __ATOMIC_SEQ_CST );
[d384787]521 __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_RELAXED );
[92976d9]522
[5dadc9b7]523 return [count, count > 0 || to_submit > 0];
[92976d9]524 }
525
[f6660520]526 static void * __io_poller_slow( void * arg ) {
[13c5e19]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
[92976d9]533 cluster * cltr = (cluster *)arg;
[61dd73d]534 struct __io_data & ring = *cltr->io;
[92976d9]535
[13c5e19]536 ring.poller.slow.id.id = doregister( &ring.poller.slow.id );
537
[92976d9]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
[1539bbd]549 __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p ready\n", &ring);
550
[b6f2b213]551 if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) {
552 while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) {
[5dadc9b7]553
[5c581cc]554 __atomic_store_n( &ring.poller.slow.blocked, true, __ATOMIC_SEQ_CST );
555
[f6660520]556 // In the user-thread approach drain and if anything was drained,
557 // batton pass to the user-thread
[5dadc9b7]558 int count;
559 bool again;
[5c581cc]560 [count, again] = __drain_io( ring, &mask, 1, true );
561
562 __atomic_store_n( &ring.poller.slow.blocked, false, __ATOMIC_SEQ_CST );
[3c039b0]563
564 // Update statistics
565 #if !defined(__CFA_NO_STATISTICS__)
[13c5e19]566 __tls_stats()->io.complete_q.completed_avg.val += count;
567 __tls_stats()->io.complete_q.completed_avg.slow_cnt += 1;
[3c039b0]568 #endif
569
[5dadc9b7]570 if(again) {
[0a805f2]571 __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to fast poller\n", &ring);
[13c5e19]572 __unpark( &ring.poller.slow.id, &ring.poller.fast.thrd __cfaabi_dbg_ctx2 );
[f6660520]573 wait( ring.poller.sem );
574 }
[b6f2b213]575 }
576 }
577 else {
578 while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) {
[f6660520]579 //In the naive approach, just poll the io completion queue directly
[5dadc9b7]580 int count;
581 bool again;
582 [count, again] = __drain_io( ring, &mask, 1, true );
[3c039b0]583
584 // Update statistics
585 #if !defined(__CFA_NO_STATISTICS__)
[13c5e19]586 __tls_stats()->io.complete_q.completed_avg.val += count;
587 __tls_stats()->io.complete_q.completed_avg.slow_cnt += 1;
[3c039b0]588 #endif
[b6f2b213]589 }
[92976d9]590 }
591
[1539bbd]592 __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p stopping\n", &ring);
593
[13c5e19]594 unregister( &ring.poller.slow.id );
595
[92976d9]596 return 0p;
597 }
598
[61dd73d]599 void main( __io_poller_fast & this ) {
[b6f2b213]600 verify( this.ring->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD );
601
[61dd73d]602 // Start parked
603 park( __cfaabi_dbg_ctx );
[f6660520]604
[61dd73d]605 __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p ready\n", &this.ring);
[1539bbd]606
[4e74466]607 int reset = 0;
608
[61dd73d]609 // Then loop until we need to start
610 while(!__atomic_load_n(&this.ring->done, __ATOMIC_SEQ_CST)) {
[5dadc9b7]611
[61dd73d]612 // Drain the io
[5dadc9b7]613 int count;
614 bool again;
[13c5e19]615 disable_interrupts();
616 [count, again] = __drain_io( *this.ring, 0p, 0, false );
[5dadc9b7]617
[13c5e19]618 if(!again) reset++;
[3c039b0]619
[13c5e19]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 );
[3c039b0]626
[5dadc9b7]627 // If we got something, just yield and check again
[4e74466]628 if(reset < 5) {
[61dd73d]629 yield();
630 }
[5dadc9b7]631 // We didn't get anything baton pass to the slow poller
[61dd73d]632 else {
633 __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to slow poller\n", &this.ring);
[5dadc9b7]634 reset = 0;
635
636 // wake up the slow poller
[61dd73d]637 post( this.ring->poller.sem );
[5dadc9b7]638
639 // park this thread
[61dd73d]640 park( __cfaabi_dbg_ctx );
[f6660520]641 }
642 }
[61dd73d]643
644 __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p stopping\n", &this.ring);
645 }
[f6660520]646
[0335620]647 static inline void __wake_poller( struct __io_data & ring ) __attribute__((artificial));
[5dadc9b7]648 static inline void __wake_poller( struct __io_data & ring ) {
[5c581cc]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 );
[5dadc9b7]653 }
654
[92976d9]655//=============================================================================================
656// I/O Submissions
657//=============================================================================================
658
[2d8f7b0]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
[6f121b8]683 static inline [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ) {
684 verify( data != 0 );
685
[13c5e19]686
[6f121b8]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;
[8ae4165]692
693 disable_interrupts();
694 uint32_t off = __tls_rand();
695 enable_interrupts( __cfaabi_dbg_ctx );
[6f121b8]696
697 // Loop around looking for an available spot
[13c5e19]698 for() {
[6f121b8]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__)
[8ae4165]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 );
[6f121b8]716 #endif
717
[13c5e19]718
[6f121b8]719 // Success return the data
720 return [sqe, idx];
721 }
722 verify(expected != data);
[2489d31]723
[6f121b8]724 len ++;
725 }
[2489d31]726
[6f121b8]727 block++;
728 yield();
729 }
[2489d31]730 }
731
[61dd73d]732 static inline void __submit( struct __io_data & ring, uint32_t idx ) {
[5dadc9b7]733 // Get now the data we definetely need
734 uint32_t * const tail = ring.submit_q.tail;
[2489d31]735 const uint32_t mask = *ring.submit_q.mask;
736
[13c5e19]737 disable_interrupts();
738
[5dadc9b7]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
[2489d31]742
[5dadc9b7]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;
[5c581cc]749 uint32_t ready_mask = ring.submit_q.ready_cnt - 1;
750 uint32_t off = __tls_rand();
751 LOOKING: for() {
[5dadc9b7]752 for(i; ring.submit_q.ready_cnt) {
[5c581cc]753 uint32_t ii = (i + off) & ready_mask;
[6f121b8]754 uint32_t expected = -1ul32;
[5c581cc]755 if( __atomic_compare_exchange_n( &ring.submit_q.ready[ii], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) {
[5dadc9b7]756 break LOOKING;
757 }
[6f121b8]758 verify(expected != idx);
[5dadc9b7]759
760 len ++;
761 }
[2489d31]762
[5dadc9b7]763 block++;
764 yield();
[2489d31]765 }
[5dadc9b7]766
767 __wake_poller( ring );
768
769 // update statistics
770 #if !defined(__CFA_NO_STATISTICS__)
[13c5e19]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;
[5dadc9b7]774 #endif
[dd4e2d7]775
776 __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() );
[2d8f7b0]777 }
[5dadc9b7]778 else {
779 // get mutual exclusion
780 lock(ring.submit_q.lock __cfaabi_dbg_ctx2);
[2489d31]781
[5dadc9b7]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);
[d384787]788
[5dadc9b7]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 }
[d384787]797
[5dadc9b7]798 // update statistics
799 #if !defined(__CFA_NO_STATISTICS__)
[13c5e19]800 __tls_stats()->io.submit_q.submit_avg.csm += 1;
801 __tls_stats()->io.submit_q.submit_avg.cnt += 1;
[5dadc9b7]802 #endif
803
[7bfc849]804 ring.submit_q.sqes[ idx & mask ].user_data = 0;
805
[5dadc9b7]806 unlock(ring.submit_q.lock);
[dd4e2d7]807
808 __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret );
[5dadc9b7]809 }
[13c5e19]810
811 enable_interrupts( __cfaabi_dbg_ctx );
[2489d31]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;
[2d8f7b0]828 }
829
[2489d31]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;
[7812a7b5]833 this.addr = (uint64_t)(uintptr_t)addr;
[2489d31]834 this.len = len;
835 }
[f6660520]836
[92976d9]837
838//=============================================================================================
839// I/O Interface
840//=============================================================================================
[f6660520]841
[2d8f7b0]842 #define __submit_prelude \
[6f121b8]843 io_user_data data = { 0, active_thread() }; \
844 struct __io_data & ring = *data.thrd->curr_cluster->io; \
[2d8f7b0]845 struct io_uring_sqe * sqe; \
846 uint32_t idx; \
[7812a7b5]847 [sqe, idx] = __submit_alloc( ring, (uint64_t)(uintptr_t)&data );
[2d8f7b0]848
849 #define __submit_wait \
[185efe6]850 /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \
[7812a7b5]851 verify( sqe->user_data == (uint64_t)(uintptr_t)&data ); \
[2d8f7b0]852 __submit( ring, idx ); \
853 park( __cfaabi_dbg_ctx ); \
854 return data.result;
[ecf6b46]855#endif
[2d8f7b0]856
[0ea6c5a]857// Some forward declarations
858extern "C" {
[1268ad8]859 #include <unistd.h>
[0ea6c5a]860 #include <sys/types.h>
[93f7c001]861 #include <sys/socket.h>
[6136ecc]862 #include <sys/syscall.h>
[08a994e]863
864#if defined(HAVE_PREADV2)
[0ea6c5a]865 struct iovec;
866 extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
[08a994e]867#endif
868#if defined(HAVE_PWRITEV2)
869 struct iovec;
[0ea6c5a]870 extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
[08a994e]871#endif
[0ea6c5a]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
[2d8f7b0]895//-----------------------------------------------------------------------------
896// Asynchronous operations
[08a994e]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
[ecf6b46]903
[08a994e]904 (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset };
[ecf6b46]905
[08a994e]906 __submit_wait
907 #endif
908 }
909#endif
[ecf6b46]910
[08a994e]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
[ecf6b46]917
[08a994e]918 (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
[ecf6b46]919
[08a994e]920 __submit_wait
921 #endif
922 }
923#endif
[ecf6b46]924
925int 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
937int 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
953ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
954 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG)
[2292067]955 return sendmsg(sockfd, msg, flags);
[ecf6b46]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
966ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) {
967 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG)
[2292067]968 return recvmsg(sockfd, msg, flags);
[ecf6b46]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
979ssize_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
994ssize_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
1009int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
1010 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT)
[0ea6c5a]1011 return accept4( sockfd, addr, addrlen, flags );
[ecf6b46]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
1024int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
1025 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT)
[0ea6c5a]1026 return connect( sockfd, addr, addrlen );
[ecf6b46]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
1038int 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
1053int 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
1068int 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
1083int 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
1098int 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
1111ssize_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
1123ssize_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}
[2d8f7b0]1134
1135//-----------------------------------------------------------------------------
1136// Check if a function is asynchronous
1137
1138// Macro magic to reduce the size of the following switch case
[ecf6b46]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)
[2d8f7b0]1143
[ecf6b46]1144bool has_user_level_blocking( fptr_t func ) {
1145 #if defined(HAVE_LINUX_IO_URING_H)
[08a994e]1146 #if defined(HAVE_PREADV2)
[171ca0d]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)
[08a994e]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
[2d8f7b0]1159
[171ca0d]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);
[2d8f7b0]1164
1165 if( /*func == (fptr_t)ync_file_range || */
[ecf6b46]1166 func == (fptr_t)cfa_sync_file_range )
[2d8f7b0]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 || */
[ecf6b46]1171 func == (fptr_t)cfa_sendmsg )
[2d8f7b0]1172 #define _CFA_IO_FEATURE_IORING_OP_SENDMSG ,
1173 return IS_DEFINED(IORING_OP_SENDMSG);
1174
1175 if( /*func == (fptr_t)recvmsg || */
[ecf6b46]1176 func == (fptr_t)cfa_recvmsg )
[2d8f7b0]1177 #define _CFA_IO_FEATURE_IORING_OP_RECVMSG ,
1178 return IS_DEFINED(IORING_OP_RECVMSG);
1179
1180 if( /*func == (fptr_t)send || */
[2489d31]1181 func == (fptr_t)cfa_send )
[2d8f7b0]1182 #define _CFA_IO_FEATURE_IORING_OP_SEND ,
1183 return IS_DEFINED(IORING_OP_SEND);
1184
1185 if( /*func == (fptr_t)recv || */
[2489d31]1186 func == (fptr_t)cfa_recv )
[2d8f7b0]1187 #define _CFA_IO_FEATURE_IORING_OP_RECV ,
1188 return IS_DEFINED(IORING_OP_RECV);
1189
1190 if( /*func == (fptr_t)accept4 || */
[2489d31]1191 func == (fptr_t)cfa_accept4 )
[2d8f7b0]1192 #define _CFA_IO_FEATURE_IORING_OP_ACCEPT ,
1193 return IS_DEFINED(IORING_OP_ACCEPT);
1194
1195 if( /*func == (fptr_t)connect || */
[2489d31]1196 func == (fptr_t)cfa_connect )
[2d8f7b0]1197 #define _CFA_IO_FEATURE_IORING_OP_CONNECT ,
1198 return IS_DEFINED(IORING_OP_CONNECT);
1199
1200 if( /*func == (fptr_t)fallocate || */
[2489d31]1201 func == (fptr_t)cfa_fallocate )
[2d8f7b0]1202 #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE ,
1203 return IS_DEFINED(IORING_OP_FALLOCATE);
1204
[0ea6c5a]1205 if( /*func == (fptr_t)posix_fadvise || */
[2489d31]1206 func == (fptr_t)cfa_fadvise )
[2d8f7b0]1207 #define _CFA_IO_FEATURE_IORING_OP_FADVISE ,
1208 return IS_DEFINED(IORING_OP_FADVISE);
1209
1210 if( /*func == (fptr_t)madvise || */
[2489d31]1211 func == (fptr_t)cfa_madvise )
[2d8f7b0]1212 #define _CFA_IO_FEATURE_IORING_OP_MADVISE ,
1213 return IS_DEFINED(IORING_OP_MADVISE);
1214
1215 if( /*func == (fptr_t)openat || */
[2489d31]1216 func == (fptr_t)cfa_openat )
[2d8f7b0]1217 #define _CFA_IO_FEATURE_IORING_OP_OPENAT ,
1218 return IS_DEFINED(IORING_OP_OPENAT);
1219
1220 if( /*func == (fptr_t)close || */
[2489d31]1221 func == (fptr_t)cfa_close )
[2d8f7b0]1222 #define _CFA_IO_FEATURE_IORING_OP_CLOSE ,
1223 return IS_DEFINED(IORING_OP_CLOSE);
1224
1225 if( /*func == (fptr_t)read || */
[ecf6b46]1226 func == (fptr_t)cfa_read )
[2d8f7b0]1227 #define _CFA_IO_FEATURE_IORING_OP_READ ,
1228 return IS_DEFINED(IORING_OP_READ);
1229
1230 if( /*func == (fptr_t)write || */
[ecf6b46]1231 func == (fptr_t)cfa_write )
[2d8f7b0]1232 #define _CFA_IO_FEATURE_IORING_OP_WRITE ,
1233 return IS_DEFINED(IORING_OP_WRITE);
[ecf6b46]1234 #endif
[2d8f7b0]1235
[ecf6b46]1236 return false;
1237}
Note: See TracBrowser for help on using the repository browser.