source: libcfa/src/concurrency/io.cfa@ 08a994e

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

Added automatic detection of support for preadv2 and pwritev2

  • Property mode set to 100644
File size: 28.4 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 &, bool ) {
23 // Nothing to do without io_uring
24 }
25
26 void __kernel_io_finish_start( cluster & ) {
27 // Nothing to do without io_uring
28 }
29
30 void __kernel_io_prepare_stop( cluster & ) {
31 // Nothing to do without io_uring
32 }
33
34 void __kernel_io_shutdown( cluster &, bool ) {
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
547#if defined(HAVE_PREADV2)
548 struct iovec;
549 extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
550#endif
551#if defined(HAVE_PWRITEV2)
552 struct iovec;
553 extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
554#endif
555
556 extern int fsync(int fd);
557 extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags);
558
559 struct msghdr;
560 struct sockaddr;
561 extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
562 extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
563 extern ssize_t send(int sockfd, const void *buf, size_t len, int flags);
564 extern ssize_t recv(int sockfd, void *buf, size_t len, int flags);
565 extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
566 extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
567
568 extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len);
569 extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice);
570 extern int madvise(void *addr, size_t length, int advice);
571
572 extern int openat(int dirfd, const char *pathname, int flags, mode_t mode);
573 extern int close(int fd);
574
575 extern ssize_t read (int fd, void *buf, size_t count);
576}
577
578//-----------------------------------------------------------------------------
579// Asynchronous operations
580#if defined(HAVE_PREADV2)
581 ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
582 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READV)
583 return preadv2(fd, iov, iovcnt, offset, flags);
584 #else
585 __submit_prelude
586
587 (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset };
588
589 __submit_wait
590 #endif
591 }
592#endif
593
594#if defined(HAVE_PWRITEV2)
595 ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
596 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITEV)
597 return pwritev2(fd, iov, iovcnt, offset, flags);
598 #else
599 __submit_prelude
600
601 (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
602
603 __submit_wait
604 #endif
605 }
606#endif
607
608int cfa_fsync(int fd) {
609 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FSYNC)
610 return fsync(fd);
611 #else
612 __submit_prelude
613
614 (*sqe){ IORING_OP_FSYNC, fd };
615
616 __submit_wait
617 #endif
618}
619
620int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags) {
621 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SYNC_FILE_RANGE)
622 return sync_file_range(fd, offset, nbytes, flags);
623 #else
624 __submit_prelude
625
626 (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
627 sqe->off = offset;
628 sqe->len = nbytes;
629 sqe->sync_range_flags = flags;
630
631 __submit_wait
632 #endif
633}
634
635
636ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
637 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG)
638 return sendmsg(sockfd, msg, flags);
639 #else
640 __submit_prelude
641
642 (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
643 sqe->msg_flags = flags;
644
645 __submit_wait
646 #endif
647}
648
649ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) {
650 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG)
651 return recvmsg(sockfd, msg, flags);
652 #else
653 __submit_prelude
654
655 (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
656 sqe->msg_flags = flags;
657
658 __submit_wait
659 #endif
660}
661
662ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags) {
663 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SEND)
664 return send( sockfd, buf, len, flags );
665 #else
666 __submit_prelude
667
668 (*sqe){ IORING_OP_SEND, sockfd };
669 sqe->addr = (uint64_t)buf;
670 sqe->len = len;
671 sqe->msg_flags = flags;
672
673 __submit_wait
674 #endif
675}
676
677ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags) {
678 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECV)
679 return recv( sockfd, buf, len, flags );
680 #else
681 __submit_prelude
682
683 (*sqe){ IORING_OP_RECV, sockfd };
684 sqe->addr = (uint64_t)buf;
685 sqe->len = len;
686 sqe->msg_flags = flags;
687
688 __submit_wait
689 #endif
690}
691
692int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
693 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT)
694 return accept4( sockfd, addr, addrlen, flags );
695 #else
696 __submit_prelude
697
698 (*sqe){ IORING_OP_ACCEPT, sockfd };
699 sqe->addr = addr;
700 sqe->addr2 = addrlen;
701 sqe->accept_flags = flags;
702
703 __submit_wait
704 #endif
705}
706
707int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
708 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT)
709 return connect( sockfd, addr, addrlen );
710 #else
711 __submit_prelude
712
713 (*sqe){ IORING_OP_CONNECT, sockfd };
714 sqe->addr = (uint64_t)addr;
715 sqe->off = addrlen;
716
717 __submit_wait
718 #endif
719}
720
721int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len) {
722 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FALLOCATE)
723 return fallocate( fd, mode, offset, len );
724 #else
725 __submit_prelude
726
727 (*sqe){ IORING_OP_FALLOCATE, fd };
728 sqe->off = offset;
729 sqe->len = length;
730 sqe->mode = mode;
731
732 __submit_wait
733 #endif
734}
735
736int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice) {
737 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FADVISE)
738 return posix_fadvise( fd, offset, len, advice );
739 #else
740 __submit_prelude
741
742 (*sqe){ IORING_OP_FADVISE, fd };
743 sqe->off = (uint64_t)offset;
744 sqe->len = length;
745 sqe->fadvise_advice = advice;
746
747 __submit_wait
748 #endif
749}
750
751int cfa_madvise(void *addr, size_t length, int advice) {
752 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_MADVISE)
753 return madvise( addr, length, advice );
754 #else
755 __submit_prelude
756
757 (*sqe){ IORING_OP_MADVISE, 0 };
758 sqe->addr = (uint64_t)addr;
759 sqe->len = length;
760 sqe->fadvise_advice = advice;
761
762 __submit_wait
763 #endif
764}
765
766int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode) {
767 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_OPENAT)
768 return openat( dirfd, pathname, flags, mode );
769 #else
770 __submit_prelude
771
772 (*sqe){ IORING_OP_OPENAT, dirfd };
773 sqe->addr = (uint64_t)pathname;
774 sqe->open_flags = flags;
775 sqe->mode = mode;
776
777 __submit_wait
778 #endif
779}
780
781int cfa_close(int fd) {
782 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CLOSE)
783 return close( fd );
784 #else
785 __submit_prelude
786
787 (*sqe){ IORING_OP_CLOSE, fd };
788
789 __submit_wait
790 #endif
791}
792
793
794ssize_t cfa_read(int fd, void *buf, size_t count) {
795 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READ)
796 return read( fd, buf, count );
797 #else
798 __submit_prelude
799
800 (*sqe){ IORING_OP_READ, fd, buf, count, 0 };
801
802 __submit_wait
803 #endif
804}
805
806ssize_t cfa_write(int fd, void *buf, size_t count) {
807 #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITE)
808 return read( fd, buf, count );
809 #else
810 __submit_prelude
811
812 (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 };
813
814 __submit_wait
815 #endif
816}
817
818//-----------------------------------------------------------------------------
819// Check if a function is asynchronous
820
821// Macro magic to reduce the size of the following switch case
822#define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__)
823#define IS_DEFINED_SECOND(first, second, ...) second
824#define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion
825#define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true)
826
827bool has_user_level_blocking( fptr_t func ) {
828 #if defined(HAVE_LINUX_IO_URING_H)
829 if( /*func == (fptr_t)preadv2 || */
830 func == (fptr_t)cfa_preadv2 )
831 #define _CFA_IO_FEATURE_IORING_OP_READV ,
832 return IS_DEFINED(IORING_OP_READV);
833
834 #if defined(HAVE_PREADV2)
835 if( /*func == (fptr_t)pwritev2 || */
836 func == (fptr_t)cfa_pwritev2 )
837 #define _CFA_IO_FEATURE_IORING_OP_WRITEV ,
838 return IS_DEFINED(IORING_OP_WRITEV);
839 #endif
840
841 #if defined(HAVE_PWRITEV2)
842 if( /*func == (fptr_t)fsync || */
843 func == (fptr_t)cfa_fsync )
844 #define _CFA_IO_FEATURE_IORING_OP_FSYNC ,
845 return IS_DEFINED(IORING_OP_FSYNC);
846 #endif
847
848 if( /*func == (fptr_t)ync_file_range || */
849 func == (fptr_t)cfa_sync_file_range )
850 #define _CFA_IO_FEATURE_IORING_OP_SYNC_FILE_RANGE ,
851 return IS_DEFINED(IORING_OP_SYNC_FILE_RANGE);
852
853 if( /*func == (fptr_t)sendmsg || */
854 func == (fptr_t)cfa_sendmsg )
855 #define _CFA_IO_FEATURE_IORING_OP_SENDMSG ,
856 return IS_DEFINED(IORING_OP_SENDMSG);
857
858 if( /*func == (fptr_t)recvmsg || */
859 func == (fptr_t)cfa_recvmsg )
860 #define _CFA_IO_FEATURE_IORING_OP_RECVMSG ,
861 return IS_DEFINED(IORING_OP_RECVMSG);
862
863 if( /*func == (fptr_t)send || */
864 func == (fptr_t)cfa_send )
865 #define _CFA_IO_FEATURE_IORING_OP_SEND ,
866 return IS_DEFINED(IORING_OP_SEND);
867
868 if( /*func == (fptr_t)recv || */
869 func == (fptr_t)cfa_recv )
870 #define _CFA_IO_FEATURE_IORING_OP_RECV ,
871 return IS_DEFINED(IORING_OP_RECV);
872
873 if( /*func == (fptr_t)accept4 || */
874 func == (fptr_t)cfa_accept4 )
875 #define _CFA_IO_FEATURE_IORING_OP_ACCEPT ,
876 return IS_DEFINED(IORING_OP_ACCEPT);
877
878 if( /*func == (fptr_t)connect || */
879 func == (fptr_t)cfa_connect )
880 #define _CFA_IO_FEATURE_IORING_OP_CONNECT ,
881 return IS_DEFINED(IORING_OP_CONNECT);
882
883 if( /*func == (fptr_t)fallocate || */
884 func == (fptr_t)cfa_fallocate )
885 #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE ,
886 return IS_DEFINED(IORING_OP_FALLOCATE);
887
888 if( /*func == (fptr_t)posix_fadvise || */
889 func == (fptr_t)cfa_fadvise )
890 #define _CFA_IO_FEATURE_IORING_OP_FADVISE ,
891 return IS_DEFINED(IORING_OP_FADVISE);
892
893 if( /*func == (fptr_t)madvise || */
894 func == (fptr_t)cfa_madvise )
895 #define _CFA_IO_FEATURE_IORING_OP_MADVISE ,
896 return IS_DEFINED(IORING_OP_MADVISE);
897
898 if( /*func == (fptr_t)openat || */
899 func == (fptr_t)cfa_openat )
900 #define _CFA_IO_FEATURE_IORING_OP_OPENAT ,
901 return IS_DEFINED(IORING_OP_OPENAT);
902
903 if( /*func == (fptr_t)close || */
904 func == (fptr_t)cfa_close )
905 #define _CFA_IO_FEATURE_IORING_OP_CLOSE ,
906 return IS_DEFINED(IORING_OP_CLOSE);
907
908 if( /*func == (fptr_t)read || */
909 func == (fptr_t)cfa_read )
910 #define _CFA_IO_FEATURE_IORING_OP_READ ,
911 return IS_DEFINED(IORING_OP_READ);
912
913 if( /*func == (fptr_t)write || */
914 func == (fptr_t)cfa_write )
915 #define _CFA_IO_FEATURE_IORING_OP_WRITE ,
916 return IS_DEFINED(IORING_OP_WRITE);
917 #endif
918
919 return false;
920}
Note: See TracBrowser for help on using the repository browser.