source: libcfa/src/concurrency/io.cfa @ 0ea6c5a

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0ea6c5a was 0ea6c5a, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Replaced some headers with forward declarations to speed-up build

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