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

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

Added second io printing group with less verbose prints.

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