source: libcfa/src/concurrency/io.cfa@ 927a918

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 927a918 was 927a918, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed support for statx which caused only problems and is barely supported in libc

  • Property mode set to 100644
File size: 28.1 KB
Line 
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
21#if !defined(HAVE_LINUX_IO_URING_H)
22 void __kernel_io_startup( cluster & ) {
23 // Nothing to do without io_uring
24 }
25
26 void __kernel_io_start_thrd( cluster & ) {
27 // Nothing to do without io_uring
28 }
29
30 void __kernel_io_stop_thrd ( cluster & ) {
31 // Nothing to do without io_uring
32 }
33
34 void __kernel_io_shutdown( cluster & ) {
35 // Nothing to do without io_uring
36 }
37
38#else
39 extern "C" {
40 #define _GNU_SOURCE /* See feature_test_macros(7) */
41 #include <errno.h>
42 #include <stdint.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/mman.h>
46 #include <sys/syscall.h>
47
48 #include <linux/io_uring.h>
49 }
50
51 #include "bits/signal.hfa"
52 #include "kernel_private.hfa"
53 #include "thread.hfa"
54
55 uint32_t entries_per_cluster() {
56 return 256;
57 }
58
59 static void * __io_poller_slow( void * arg );
60
61 // Weirdly, some systems that do support io_uring don't actually define these
62 #ifdef __alpha__
63 /*
64 * alpha is the only exception, all other architectures
65 * have common numbers for new system calls.
66 */
67 #ifndef __NR_io_uring_setup
68 #define __NR_io_uring_setup 535
69 #endif
70 #ifndef __NR_io_uring_enter
71 #define __NR_io_uring_enter 536
72 #endif
73 #ifndef __NR_io_uring_register
74 #define __NR_io_uring_register 537
75 #endif
76 #else /* !__alpha__ */
77 #ifndef __NR_io_uring_setup
78 #define __NR_io_uring_setup 425
79 #endif
80 #ifndef __NR_io_uring_enter
81 #define __NR_io_uring_enter 426
82 #endif
83 #ifndef __NR_io_uring_register
84 #define __NR_io_uring_register 427
85 #endif
86 #endif
87
88 #if defined(__CFA_IO_POLLING_USER__)
89 void ?{}( __io_poller_fast & this, struct cluster & cltr ) {
90 this.ring = &cltr.io;
91 (this.thrd){ "Fast I/O Poller", cltr };
92 }
93 void ^?{}( __io_poller_fast & mutex this );
94 void main( __io_poller_fast & this );
95 static inline $thread * get_thread( __io_poller_fast & this ) { return &this.thrd; }
96 void ^?{}( __io_poller_fast & mutex this ) {}
97 #endif
98
99//=============================================================================================
100// I/O Startup / Shutdown logic
101//=============================================================================================
102 void __kernel_io_startup( cluster & this, bool main_cluster ) {
103 // Step 1 : call to setup
104 struct io_uring_params params;
105 memset(&params, 0, sizeof(params));
106
107 uint32_t nentries = entries_per_cluster();
108
109 int fd = syscall(__NR_io_uring_setup, nentries, &params );
110 if(fd < 0) {
111 abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
112 }
113
114 // Step 2 : mmap result
115 memset(&this.io, 0, sizeof(struct io_ring));
116 struct io_uring_sq & sq = this.io.submit_q;
117 struct io_uring_cq & cq = this.io.completion_q;
118
119 // calculate the right ring size
120 sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) );
121 cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe));
122
123 // Requires features
124 #if defined(IORING_FEAT_SINGLE_MMAP)
125 // adjust the size according to the parameters
126 if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
127 cq->ring_sz = sq->ring_sz = max(cq->ring_sz, sq->ring_sz);
128 }
129 #endif
130
131 // mmap the Submit Queue into existence
132 sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
133 if (sq.ring_ptr == (void*)MAP_FAILED) {
134 abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
135 }
136
137 // Requires features
138 #if defined(IORING_FEAT_SINGLE_MMAP)
139 // mmap the Completion Queue into existence (may or may not be needed)
140 if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
141 cq->ring_ptr = sq->ring_ptr;
142 }
143 else
144 #endif
145 {
146 // We need multiple call to MMAP
147 cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
148 if (cq.ring_ptr == (void*)MAP_FAILED) {
149 munmap(sq.ring_ptr, sq.ring_sz);
150 abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
151 }
152 }
153
154 // mmap the submit queue entries
155 size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
156 sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
157 if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) {
158 munmap(sq.ring_ptr, sq.ring_sz);
159 if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz);
160 abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno));
161 }
162
163 // Get the pointers from the kernel to fill the structure
164 // submit queue
165 sq.head = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
166 sq.tail = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
167 sq.mask = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
168 sq.num = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
169 sq.flags = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
170 sq.dropped = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
171 sq.array = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
172 sq.alloc = *sq.tail;
173 sq.ready = *sq.tail;
174
175 // completion queue
176 cq.head = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.head);
177 cq.tail = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail);
178 cq.mask = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask);
179 cq.num = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries);
180 cq.overflow = ( uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow);
181 cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes);
182
183 // some paranoid checks
184 /* 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 );
185 /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num );
186 /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head );
187 /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail );
188
189 /* 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 );
190 /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
191 /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head );
192 /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail );
193
194 // Update the global ring info
195 this.io.flags = params.flags;
196 this.io.fd = fd;
197 this.io.done = false;
198 (this.io.submit){ min(*sq.num, *cq.num) };
199
200 // Initialize statistics
201 #if !defined(__CFA_NO_STATISTICS__)
202 this.io.submit_q.stats.submit_avg.val = 0;
203 this.io.submit_q.stats.submit_avg.cnt = 0;
204 this.io.completion_q.stats.completed_avg.val = 0;
205 this.io.completion_q.stats.completed_avg.cnt = 0;
206 #endif
207
208 if(!main_cluster) {
209 __kernel_io_finish_start( this );
210 }
211 }
212
213 void __kernel_io_finish_start( cluster & this ) {
214 #if defined(__CFA_IO_POLLING_USER__)
215 __cfadbg_print_safe(io_core, "Kernel I/O : Creating fast poller for cluter %p\n", &this);
216 (this.io.poller.fast){ this };
217 __thrd_start( this.io.poller.fast, main );
218 #endif
219
220 // Create the poller thread
221 __cfadbg_print_safe(io_core, "Kernel I/O : Creating slow poller for cluter %p\n", &this);
222 this.io.poller.slow.stack = __create_pthread( &this.io.poller.slow.kthrd, __io_poller_slow, &this );
223 }
224
225 void __kernel_io_prepare_stop( cluster & this ) {
226 __cfadbg_print_safe(io_core, "Kernel I/O : Stopping pollers for cluster\n", &this);
227 // Notify the poller thread of the shutdown
228 __atomic_store_n(&this.io.done, true, __ATOMIC_SEQ_CST);
229
230 // Stop the IO Poller
231 sigval val = { 1 };
232 pthread_sigqueue( this.io.poller.slow.kthrd, SIGUSR1, val );
233 #if defined(__CFA_IO_POLLING_USER__)
234 post( this.io.poller.sem );
235 #endif
236
237 // Wait for the poller thread to finish
238 pthread_join( this.io.poller.slow.kthrd, 0p );
239 free( this.io.poller.slow.stack );
240
241 __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller stopped for cluster\n", &this);
242
243 #if defined(__CFA_IO_POLLING_USER__)
244 // unpark the fast io_poller
245 unpark( &this.io.poller.fast.thrd __cfaabi_dbg_ctx2 );
246
247 ^(this.io.poller.fast){};
248
249 __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller stopped for cluster\n", &this);
250 #endif
251 }
252
253 void __kernel_io_shutdown( cluster & this, bool main_cluster ) {
254 if(!main_cluster) {
255 __kernel_io_prepare_stop( this );
256 }
257
258 // print statistics
259 #if !defined(__CFA_NO_STATISTICS__)
260 if(this.print_stats) {
261 __cfaabi_bits_print_safe( STDERR_FILENO,
262 "----- I/O uRing Stats -----\n"
263 "- total submit calls : %llu\n"
264 "- avg submit : %lf\n"
265 "- total wait calls : %llu\n"
266 "- avg completion/wait : %lf\n",
267 this.io.submit_q.stats.submit_avg.cnt,
268 ((double)this.io.submit_q.stats.submit_avg.val) / this.io.submit_q.stats.submit_avg.cnt,
269 this.io.completion_q.stats.completed_avg.cnt,
270 ((double)this.io.completion_q.stats.completed_avg.val) / this.io.completion_q.stats.completed_avg.cnt
271 );
272 }
273 #endif
274
275 // Shutdown the io rings
276 struct io_uring_sq & sq = this.io.submit_q;
277 struct io_uring_cq & cq = this.io.completion_q;
278
279 // unmap the submit queue entries
280 munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
281
282 // unmap the Submit Queue ring
283 munmap(sq.ring_ptr, sq.ring_sz);
284
285 // unmap the Completion Queue ring, if it is different
286 if (cq.ring_ptr != sq.ring_ptr) {
287 munmap(cq.ring_ptr, cq.ring_sz);
288 }
289
290 // close the file descriptor
291 close(this.io.fd);
292 }
293
294//=============================================================================================
295// I/O Polling
296//=============================================================================================
297 struct io_user_data {
298 int32_t result;
299 $thread * thrd;
300 };
301
302 // Process a single completion message from the io_uring
303 // This is NOT thread-safe
304 static int __drain_io( struct io_ring & ring, sigset_t * mask, int waitcnt, bool in_kernel ) {
305 int ret = syscall( __NR_io_uring_enter, ring.fd, 0, waitcnt, IORING_ENTER_GETEVENTS, mask, _NSIG / 8);
306 if( ret < 0 ) {
307 switch((int)errno) {
308 case EAGAIN:
309 case EINTR:
310 return -EAGAIN;
311 default:
312 abort( "KERNEL ERROR: IO_URING WAIT - %s\n", strerror(errno) );
313 }
314 }
315
316 // Drain the queue
317 unsigned head = *ring.completion_q.head;
318 unsigned tail = __atomic_load_n(ring.completion_q.tail, __ATOMIC_ACQUIRE);
319
320 // Nothing was new return 0
321 if (head == tail) {
322 #if !defined(__CFA_NO_STATISTICS__)
323 ring.completion_q.stats.completed_avg.cnt += 1;
324 #endif
325 return 0;
326 }
327
328 uint32_t count = tail - head;
329 for(i; count) {
330 unsigned idx = (head + i) & (*ring.completion_q.mask);
331 struct io_uring_cqe & cqe = ring.completion_q.cqes[idx];
332
333 /* paranoid */ verify(&cqe);
334
335 struct io_user_data * data = (struct io_user_data *)cqe.user_data;
336 __cfadbg_print_safe( io, "Kernel I/O : Performed reading io cqe %p, result %d for %p\n", data, cqe.res, data->thrd );
337
338 data->result = cqe.res;
339 if(!in_kernel) { unpark( data->thrd __cfaabi_dbg_ctx2 ); }
340 else { __unpark( data->thrd __cfaabi_dbg_ctx2 ); }
341 }
342
343 // Allow new submissions to happen
344 V(ring.submit, count);
345
346 // Mark to the kernel that the cqe has been seen
347 // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
348 __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_RELAXED );
349
350 // Update statistics
351 #if !defined(__CFA_NO_STATISTICS__)
352 ring.completion_q.stats.completed_avg.val += count;
353 ring.completion_q.stats.completed_avg.cnt += 1;
354 #endif
355
356 return count;
357 }
358
359 static void * __io_poller_slow( void * arg ) {
360 cluster * cltr = (cluster *)arg;
361 struct io_ring & ring = cltr->io;
362
363 sigset_t mask;
364 sigfillset(&mask);
365 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
366 abort( "KERNEL ERROR: IO_URING - pthread_sigmask" );
367 }
368
369 sigdelset( &mask, SIGUSR1 );
370
371 verify( (*ring.submit_q.head) == (*ring.submit_q.tail) );
372 verify( (*ring.completion_q.head) == (*ring.completion_q.tail) );
373
374 while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) {
375 #if defined(__CFA_IO_POLLING_USER__)
376
377 // In the user-thread approach drain and if anything was drained,
378 // batton pass to the user-thread
379 int count = __drain_io( ring, &mask, 1, true );
380 if(count > 0) {
381 __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to fast poller\n", &ring);
382 __unpark( &ring.poller.fast.thrd __cfaabi_dbg_ctx2 );
383 wait( ring.poller.sem );
384 }
385
386 #else
387
388 //In the naive approach, just poll the io completion queue directly
389 __drain_io( ring, &mask, 1, true );
390
391 #endif
392 }
393
394 return 0p;
395 }
396
397 #if defined(__CFA_IO_POLLING_USER__)
398 void main( __io_poller_fast & this ) {
399 // Start parked
400 park( __cfaabi_dbg_ctx );
401
402 // Then loop until we need to start
403 while(!__atomic_load_n(&this.ring->done, __ATOMIC_SEQ_CST)) {
404 // Drain the io
405 if(0 > __drain_io( *this.ring, 0p, 0, false )) {
406 // If we got something, just yield and check again
407 yield();
408 }
409 else {
410 // We didn't get anything baton pass to the slow poller
411 __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to slow poller\n", &this.ring);
412 post( this.ring->poller.sem );
413 park( __cfaabi_dbg_ctx );
414 }
415 }
416 }
417 #endif
418
419//=============================================================================================
420// I/O Submissions
421//=============================================================================================
422
423// Submition steps :
424// 1 - We need to make sure we don't overflow any of the buffer, P(ring.submit) to make sure
425// entries are available. The semaphore make sure that there is no more operations in
426// progress then the number of entries in the buffer. This probably limits concurrency
427// more than necessary since submitted but not completed operations don't need any
428// entries in user space. However, I don't know what happens if we overflow the buffers
429// because too many requests completed at once. This is a safe approach in all cases.
430// Furthermore, with hundreds of entries, this may be okay.
431//
432// 2 - Allocate a queue entry. The ring already has memory for all entries but only the ones
433// listed in sq.array are visible by the kernel. For those not listed, the kernel does not
434// offer any assurance that an entry is not being filled by multiple flags. Therefore, we
435// need to write an allocator that allows allocating concurrently.
436//
437// 3 - Actually fill the submit entry, this is the only simple and straightforward step.
438//
439// 4 - Append the entry index to the array and adjust the tail accordingly. This operation
440// needs to arrive to two concensus at the same time:
441// A - The order in which entries are listed in the array: no two threads must pick the
442// same index for their entries
443// B - When can the tail be update for the kernel. EVERY entries in the array between
444// head and tail must be fully filled and shouldn't ever be touched again.
445//
446
447 static inline [* struct io_uring_sqe, uint32_t] __submit_alloc( struct io_ring & ring ) {
448 // Wait for a spot to be available
449 P(ring.submit);
450
451 // Allocate the sqe
452 uint32_t idx = __atomic_fetch_add(&ring.submit_q.alloc, 1ul32, __ATOMIC_SEQ_CST);
453
454 // Validate that we didn't overflow anything
455 // Check that nothing overflowed
456 /* paranoid */ verify( true );
457
458 // Check that it goes head -> tail -> alloc and never head -> alloc -> tail
459 /* paranoid */ verify( true );
460
461 // Return the sqe
462 return [&ring.submit_q.sqes[ idx & (*ring.submit_q.mask)], idx];
463 }
464
465 static inline void __submit( struct io_ring & ring, uint32_t idx ) {
466 // get mutual exclusion
467 lock(ring.submit_q.lock __cfaabi_dbg_ctx2);
468
469 // Append to the list of ready entries
470 uint32_t * tail = ring.submit_q.tail;
471 const uint32_t mask = *ring.submit_q.mask;
472
473 ring.submit_q.array[ (*tail) & mask ] = idx & mask;
474 __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST);
475
476 // Submit however, many entries need to be submitted
477 int ret = syscall( __NR_io_uring_enter, ring.fd, 1, 0, 0, 0p, 0);
478 if( ret < 0 ) {
479 switch((int)errno) {
480 default:
481 abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) );
482 }
483 }
484
485 // update statistics
486 #if !defined(__CFA_NO_STATISTICS__)
487 ring.submit_q.stats.submit_avg.val += 1;
488 ring.submit_q.stats.submit_avg.cnt += 1;
489 #endif
490
491 unlock(ring.submit_q.lock);
492 // Make sure that idx was submitted
493 // Be careful to not get false positive if we cycled the entire list or that someone else submitted for us
494 __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret );
495 }
496
497 static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd) {
498 this.opcode = opcode;
499 #if !defined(IOSQE_ASYNC)
500 this.flags = 0;
501 #else
502 this.flags = IOSQE_ASYNC;
503 #endif
504 this.ioprio = 0;
505 this.fd = fd;
506 this.off = 0;
507 this.addr = 0;
508 this.len = 0;
509 this.rw_flags = 0;
510 this.__pad2[0] = this.__pad2[1] = this.__pad2[2] = 0;
511 }
512
513 static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd, void * addr, uint32_t len, uint64_t off ) {
514 (this){ opcode, fd };
515 this.off = off;
516 this.addr = (uint64_t)addr;
517 this.len = len;
518 }
519
520
521//=============================================================================================
522// I/O Interface
523//=============================================================================================
524
525 #define __submit_prelude \
526 struct io_ring & ring = active_cluster()->io; \
527 struct io_uring_sqe * sqe; \
528 uint32_t idx; \
529 [sqe, idx] = __submit_alloc( ring );
530
531 #define __submit_wait \
532 io_user_data data = { 0, active_thread() }; \
533 /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \
534 sqe->user_data = (uint64_t)&data; \
535 __submit( ring, idx ); \
536 park( __cfaabi_dbg_ctx ); \
537 return data.result;
538#endif
539
540// Some forward declarations
541extern "C" {
542 #include <unistd.h>
543 #include <sys/types.h>
544 #include <sys/socket.h>
545 #include <sys/syscall.h>
546 struct iovec;
547 extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
548 extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
549
550 extern int fsync(int fd);
551 extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags);
552
553 struct msghdr;
554 struct sockaddr;
555 extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
556 extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
557 extern ssize_t send(int sockfd, const void *buf, size_t len, int flags);
558 extern ssize_t recv(int sockfd, void *buf, size_t len, int flags);
559 extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
560 extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
561
562 extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len);
563 extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice);
564 extern int madvise(void *addr, size_t length, int advice);
565
566 extern int openat(int dirfd, const char *pathname, int flags, mode_t mode);
567 extern int close(int fd);
568
569 extern ssize_t read (int fd, void *buf, size_t count);
570}
571
572//-----------------------------------------------------------------------------
573// Asynchronous operations
574ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
575 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READV)
576 return preadv2(fd, iov, iovcnt, offset, flags);
577 #else
578 __submit_prelude
579
580 (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset };
581
582 __submit_wait
583 #endif
584}
585
586ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
587 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITEV)
588 return pwritev2(fd, iov, iovcnt, offset, flags);
589 #else
590 __submit_prelude
591
592 (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
593
594 __submit_wait
595 #endif
596}
597
598int cfa_fsync(int fd) {
599 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FSYNC)
600 return fsync(fd);
601 #else
602 __submit_prelude
603
604 (*sqe){ IORING_OP_FSYNC, fd };
605
606 __submit_wait
607 #endif
608}
609
610int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags) {
611 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SYNC_FILE_RANGE)
612 return sync_file_range(fd, offset, nbytes, flags);
613 #else
614 __submit_prelude
615
616 (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
617 sqe->off = offset;
618 sqe->len = nbytes;
619 sqe->sync_range_flags = flags;
620
621 __submit_wait
622 #endif
623}
624
625
626ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
627 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG)
628 return sendmsg(sockfd, msg, flags);
629 #else
630 __submit_prelude
631
632 (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
633 sqe->msg_flags = flags;
634
635 __submit_wait
636 #endif
637}
638
639ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) {
640 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG)
641 return recvmsg(sockfd, msg, flags);
642 #else
643 __submit_prelude
644
645 (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
646 sqe->msg_flags = flags;
647
648 __submit_wait
649 #endif
650}
651
652ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags) {
653 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SEND)
654 return send( sockfd, buf, len, flags );
655 #else
656 __submit_prelude
657
658 (*sqe){ IORING_OP_SEND, sockfd };
659 sqe->addr = (uint64_t)buf;
660 sqe->len = len;
661 sqe->msg_flags = flags;
662
663 __submit_wait
664 #endif
665}
666
667ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags) {
668 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECV)
669 return recv( sockfd, buf, len, flags );
670 #else
671 __submit_prelude
672
673 (*sqe){ IORING_OP_RECV, sockfd };
674 sqe->addr = (uint64_t)buf;
675 sqe->len = len;
676 sqe->msg_flags = flags;
677
678 __submit_wait
679 #endif
680}
681
682int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
683 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT)
684 return accept4( sockfd, addr, addrlen, flags );
685 #else
686 __submit_prelude
687
688 (*sqe){ IORING_OP_ACCEPT, sockfd };
689 sqe->addr = addr;
690 sqe->addr2 = addrlen;
691 sqe->accept_flags = flags;
692
693 __submit_wait
694 #endif
695}
696
697int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
698 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT)
699 return connect( sockfd, addr, addrlen );
700 #else
701 __submit_prelude
702
703 (*sqe){ IORING_OP_CONNECT, sockfd };
704 sqe->addr = (uint64_t)addr;
705 sqe->off = addrlen;
706
707 __submit_wait
708 #endif
709}
710
711int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len) {
712 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FALLOCATE)
713 return fallocate( fd, mode, offset, len );
714 #else
715 __submit_prelude
716
717 (*sqe){ IORING_OP_FALLOCATE, fd };
718 sqe->off = offset;
719 sqe->len = length;
720 sqe->mode = mode;
721
722 __submit_wait
723 #endif
724}
725
726int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice) {
727 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FADVISE)
728 return posix_fadvise( fd, offset, len, advice );
729 #else
730 __submit_prelude
731
732 (*sqe){ IORING_OP_FADVISE, fd };
733 sqe->off = (uint64_t)offset;
734 sqe->len = length;
735 sqe->fadvise_advice = advice;
736
737 __submit_wait
738 #endif
739}
740
741int cfa_madvise(void *addr, size_t length, int advice) {
742 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_MADVISE)
743 return madvise( addr, length, advice );
744 #else
745 __submit_prelude
746
747 (*sqe){ IORING_OP_MADVISE, 0 };
748 sqe->addr = (uint64_t)addr;
749 sqe->len = length;
750 sqe->fadvise_advice = advice;
751
752 __submit_wait
753 #endif
754}
755
756int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode) {
757 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_OPENAT)
758 return openat( dirfd, pathname, flags, mode );
759 #else
760 __submit_prelude
761
762 (*sqe){ IORING_OP_OPENAT, dirfd };
763 sqe->addr = (uint64_t)pathname;
764 sqe->open_flags = flags;
765 sqe->mode = mode;
766
767 __submit_wait
768 #endif
769}
770
771int cfa_close(int fd) {
772 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CLOSE)
773 return close( fd );
774 #else
775 __submit_prelude
776
777 (*sqe){ IORING_OP_CLOSE, fd };
778
779 __submit_wait
780 #endif
781}
782
783
784ssize_t cfa_read(int fd, void *buf, size_t count) {
785 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READ)
786 return read( fd, buf, count );
787 #else
788 __submit_prelude
789
790 (*sqe){ IORING_OP_READ, fd, buf, count, 0 };
791
792 __submit_wait
793 #endif
794}
795
796ssize_t cfa_write(int fd, void *buf, size_t count) {
797 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITE)
798 return read( fd, buf, count );
799 #else
800 __submit_prelude
801
802 (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 };
803
804 __submit_wait
805 #endif
806}
807
808//-----------------------------------------------------------------------------
809// Check if a function is asynchronous
810
811// Macro magic to reduce the size of the following switch case
812#define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__)
813#define IS_DEFINED_SECOND(first, second, ...) second
814#define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion
815#define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true)
816
817bool has_user_level_blocking( fptr_t func ) {
818 #if defined(HAVE_LINUX_IO_URING_H)
819 if( /*func == (fptr_t)preadv2 || */
820 func == (fptr_t)cfa_preadv2 )
821 #define _CFA_IO_FEATURE_IORING_OP_READV ,
822 return IS_DEFINED(IORING_OP_READV);
823
824 if( /*func == (fptr_t)pwritev2 || */
825 func == (fptr_t)cfa_pwritev2 )
826 #define _CFA_IO_FEATURE_IORING_OP_WRITEV ,
827 return IS_DEFINED(IORING_OP_WRITEV);
828
829 if( /*func == (fptr_t)fsync || */
830 func == (fptr_t)cfa_fsync )
831 #define _CFA_IO_FEATURE_IORING_OP_FSYNC ,
832 return IS_DEFINED(IORING_OP_FSYNC);
833
834 if( /*func == (fptr_t)ync_file_range || */
835 func == (fptr_t)cfa_sync_file_range )
836 #define _CFA_IO_FEATURE_IORING_OP_SYNC_FILE_RANGE ,
837 return IS_DEFINED(IORING_OP_SYNC_FILE_RANGE);
838
839 if( /*func == (fptr_t)sendmsg || */
840 func == (fptr_t)cfa_sendmsg )
841 #define _CFA_IO_FEATURE_IORING_OP_SENDMSG ,
842 return IS_DEFINED(IORING_OP_SENDMSG);
843
844 if( /*func == (fptr_t)recvmsg || */
845 func == (fptr_t)cfa_recvmsg )
846 #define _CFA_IO_FEATURE_IORING_OP_RECVMSG ,
847 return IS_DEFINED(IORING_OP_RECVMSG);
848
849 if( /*func == (fptr_t)send || */
850 func == (fptr_t)cfa_send )
851 #define _CFA_IO_FEATURE_IORING_OP_SEND ,
852 return IS_DEFINED(IORING_OP_SEND);
853
854 if( /*func == (fptr_t)recv || */
855 func == (fptr_t)cfa_recv )
856 #define _CFA_IO_FEATURE_IORING_OP_RECV ,
857 return IS_DEFINED(IORING_OP_RECV);
858
859 if( /*func == (fptr_t)accept4 || */
860 func == (fptr_t)cfa_accept4 )
861 #define _CFA_IO_FEATURE_IORING_OP_ACCEPT ,
862 return IS_DEFINED(IORING_OP_ACCEPT);
863
864 if( /*func == (fptr_t)connect || */
865 func == (fptr_t)cfa_connect )
866 #define _CFA_IO_FEATURE_IORING_OP_CONNECT ,
867 return IS_DEFINED(IORING_OP_CONNECT);
868
869 if( /*func == (fptr_t)fallocate || */
870 func == (fptr_t)cfa_fallocate )
871 #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE ,
872 return IS_DEFINED(IORING_OP_FALLOCATE);
873
874 if( /*func == (fptr_t)posix_fadvise || */
875 func == (fptr_t)cfa_fadvise )
876 #define _CFA_IO_FEATURE_IORING_OP_FADVISE ,
877 return IS_DEFINED(IORING_OP_FADVISE);
878
879 if( /*func == (fptr_t)madvise || */
880 func == (fptr_t)cfa_madvise )
881 #define _CFA_IO_FEATURE_IORING_OP_MADVISE ,
882 return IS_DEFINED(IORING_OP_MADVISE);
883
884 if( /*func == (fptr_t)openat || */
885 func == (fptr_t)cfa_openat )
886 #define _CFA_IO_FEATURE_IORING_OP_OPENAT ,
887 return IS_DEFINED(IORING_OP_OPENAT);
888
889 if( /*func == (fptr_t)close || */
890 func == (fptr_t)cfa_close )
891 #define _CFA_IO_FEATURE_IORING_OP_CLOSE ,
892 return IS_DEFINED(IORING_OP_CLOSE);
893
894 if( /*func == (fptr_t)read || */
895 func == (fptr_t)cfa_read )
896 #define _CFA_IO_FEATURE_IORING_OP_READ ,
897 return IS_DEFINED(IORING_OP_READ);
898
899 if( /*func == (fptr_t)write || */
900 func == (fptr_t)cfa_write )
901 #define _CFA_IO_FEATURE_IORING_OP_WRITE ,
902 return IS_DEFINED(IORING_OP_WRITE);
903 #endif
904
905 return false;
906}
Note: See TracBrowser for help on using the repository browser.