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

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since a114743 was ff55092, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

removed warning

  • Property mode set to 100644
File size: 20.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 __cforall_thread__
17#define _GNU_SOURCE
18
19#if defined(__CFA_DEBUG__)
20        // #define __CFA_DEBUG_PRINT_IO__
21        // #define __CFA_DEBUG_PRINT_IO_CORE__
22#endif
23
24
25#if defined(CFA_HAVE_LINUX_IO_URING_H)
26        #include <errno.h>
27        #include <signal.h>
28        #include <stdint.h>
29        #include <string.h>
30        #include <unistd.h>
31
32        extern "C" {
33                #include <sys/syscall.h>
34                #include <sys/eventfd.h>
35                #include <sys/uio.h>
36
37                #include <linux/io_uring.h>
38        }
39
40        #include "stats.hfa"
41        #include "kernel.hfa"
42        #include "kernel/fwd.hfa"
43        #include "kernel/private.hfa"
44        #include "kernel/cluster.hfa"
45        #include "io/types.hfa"
46
47        __attribute__((unused)) static const char * opcodes[] = {
48                "OP_NOP",
49                "OP_READV",
50                "OP_WRITEV",
51                "OP_FSYNC",
52                "OP_READ_FIXED",
53                "OP_WRITE_FIXED",
54                "OP_POLL_ADD",
55                "OP_POLL_REMOVE",
56                "OP_SYNC_FILE_RANGE",
57                "OP_SENDMSG",
58                "OP_RECVMSG",
59                "OP_TIMEOUT",
60                "OP_TIMEOUT_REMOVE",
61                "OP_ACCEPT",
62                "OP_ASYNC_CANCEL",
63                "OP_LINK_TIMEOUT",
64                "OP_CONNECT",
65                "OP_FALLOCATE",
66                "OP_OPENAT",
67                "OP_CLOSE",
68                "OP_FILES_UPDATE",
69                "OP_STATX",
70                "OP_READ",
71                "OP_WRITE",
72                "OP_FADVISE",
73                "OP_MADVISE",
74                "OP_SEND",
75                "OP_RECV",
76                "OP_OPENAT2",
77                "OP_EPOLL_CTL",
78                "OP_SPLICE",
79                "OP_PROVIDE_BUFFERS",
80                "OP_REMOVE_BUFFERS",
81                "OP_TEE",
82                "INVALID_OP"
83        };
84
85        static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want );
86        static void __ioarbiter_submit( $io_context * , __u32 idxs[], __u32 have, bool lazy );
87        static void __ioarbiter_flush ( $io_context & );
88        static inline void __ioarbiter_notify( $io_context & ctx );
89//=============================================================================================
90// I/O Polling
91//=============================================================================================
92        static inline unsigned __flush( struct $io_context & );
93        static inline __u32 __release_sqes( struct $io_context & );
94        extern void __kernel_unpark( thread$ * thrd, unpark_hint );
95
96        static void ioring_syscsll( struct $io_context & ctx, unsigned int min_comp, unsigned int flags ) {
97                __STATS__( true, io.calls.flush++; )
98                int ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, min_comp, flags, (sigset_t *)0p, _NSIG / 8);
99                if( ret < 0 ) {
100                        switch((int)errno) {
101                        case EAGAIN:
102                        case EINTR:
103                        case EBUSY:
104                                // Update statistics
105                                __STATS__( false, io.calls.errors.busy ++; )
106                                return false;
107                        default:
108                                abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
109                        }
110                }
111
112                __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd);
113                __STATS__( true, io.calls.submitted += ret; )
114                /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
115                /* paranoid */ verify( ctx.sq.to_submit >= ret );
116
117                ctx.sq.to_submit -= ret;
118
119                /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
120
121                // Release the consumed SQEs
122                __release_sqes( ctx );
123
124                /* paranoid */ verify( ! __preemption_enabled() );
125
126                __atomic_store_n(&ctx.proc->io.pending, false, __ATOMIC_RELAXED);
127        }
128
129        static bool try_acquire( $io_context * ctx ) __attribute__((nonnull(1))) {
130                /* paranoid */ verify( ! __preemption_enabled() );
131                /* paranoid */ verify( ready_schedule_islocked() );
132
133
134                {
135                        const __u32 head = *ctx->cq.head;
136                        const __u32 tail = *ctx->cq.tail;
137
138                        if(head == tail) return false;
139                }
140
141                // Drain the queue
142                if(!__atomic_try_acquire(&ctx->cq.lock)) {
143                        __STATS__( false, io.calls.locked++; )
144                        return false;
145                }
146
147                return true;
148        }
149
150        static bool __cfa_do_drain( $io_context * ctx, cluster * cltr ) __attribute__((nonnull(1, 2))) {
151                /* paranoid */ verify( ! __preemption_enabled() );
152                /* paranoid */ verify( ready_schedule_islocked() );
153                /* paranoid */ verify( ctx->cq.lock == true );
154
155                const __u32 mask = *ctx->cq.mask;
156                unsigned long long ts_prev = ctx->cq.ts;
157
158                // re-read the head and tail in case it already changed.
159                const __u32 head = *ctx->cq.head;
160                const __u32 tail = *ctx->cq.tail;
161                const __u32 count = tail - head;
162                __STATS__( false, io.calls.drain++; io.calls.completed += count; )
163
164                for(i; count) {
165                        unsigned idx = (head + i) & mask;
166                        volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx];
167
168                        /* paranoid */ verify(&cqe);
169
170                        struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
171                        // __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
172
173                        __kernel_unpark( fulfil( *future, cqe.res, false ), UNPARK_LOCAL );
174                }
175
176                unsigned long long ts_next = ctx->cq.ts = rdtscl();
177
178                // Mark to the kernel that the cqe has been seen
179                // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
180                __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST );
181
182                __cfadbg_print_safe(io, "Kernel I/O : %u completed age %llu\n", count, ts_next);
183                /* paranoid */ verify( ready_schedule_islocked() );
184                /* paranoid */ verify( ! __preemption_enabled() );
185
186                __atomic_unlock(&ctx->cq.lock);
187
188                touch_tsc( cltr->sched.io.tscs, ctx->cq.id, ts_prev, ts_next );
189
190                return true;
191        }
192
193        bool __cfa_io_drain( processor * proc ) {
194                bool local = false;
195                bool remote = false;
196
197                ready_schedule_lock();
198
199                cluster * const cltr = proc->cltr;
200                $io_context * const ctx = proc->io.ctx;
201                /* paranoid */ verify( cltr );
202                /* paranoid */ verify( ctx );
203
204                with(cltr->sched) {
205                        const size_t ctxs_count = io.count;
206
207                        /* paranoid */ verify( ready_schedule_islocked() );
208                        /* paranoid */ verify( ! __preemption_enabled() );
209                        /* paranoid */ verify( active_processor() == proc );
210                        /* paranoid */ verify( __shard_factor.io > 0 );
211                        /* paranoid */ verify( ctxs_count > 0 );
212                        /* paranoid */ verify( ctx->cq.id < ctxs_count );
213
214                        const unsigned this_cache = cache_id(cltr, ctx->cq.id / __shard_factor.io);
215                        const unsigned long long ctsc = rdtscl();
216
217                        if(proc->io.target == MAX) {
218                                uint64_t chaos = __tls_rand();
219                                unsigned ext = chaos & 0xff;
220                                unsigned other  = (chaos >> 8) % (ctxs_count);
221
222                                if(ext < 3 || __atomic_load_n(&caches[other / __shard_factor.io].id, __ATOMIC_RELAXED) == this_cache) {
223                                        proc->io.target = other;
224                                }
225                        }
226                        else {
227                                const unsigned target = proc->io.target;
228                                /* paranoid */ verify( io.tscs[target].tv != MAX );
229                                HELP: if(target < ctxs_count) {
230                                        const unsigned long long cutoff = calc_cutoff(ctsc, ctx->cq.id, ctxs_count, io.data, io.tscs, __shard_factor.io);
231                                        const unsigned long long age = moving_average(ctsc, io.tscs[target].tv, io.tscs[target].ma);
232                                        __cfadbg_print_safe(io, "Kernel I/O: Help attempt on %u from %u, age %'llu vs cutoff %'llu, %s\n", target, this, age, cutoff, age > cutoff ? "yes" : "no");
233                                        if(age <= cutoff) break HELP;
234
235                                        if(!try_acquire(io.data[target])) break HELP;
236
237                                        if(!__cfa_do_drain( io.data[target], cltr )) break HELP;
238
239                                        remote = true;
240                                        __STATS__( false, io.calls.helped++; )
241                                }
242                                proc->io.target = MAX;
243                        }
244                }
245
246
247                // Drain the local queue
248                if(try_acquire( proc->io.ctx )) {
249                        local = __cfa_do_drain( proc->io.ctx, cltr );
250                }
251
252                /* paranoid */ verify( ready_schedule_islocked() );
253                /* paranoid */ verify( ! __preemption_enabled() );
254                /* paranoid */ verify( active_processor() == proc );
255
256                ready_schedule_unlock();
257                return local || remote;
258        }
259
260        bool __cfa_io_flush( processor * proc ) {
261                /* paranoid */ verify( ! __preemption_enabled() );
262                /* paranoid */ verify( proc );
263                /* paranoid */ verify( proc->io.ctx );
264
265                $io_context & ctx = *proc->io.ctx;
266
267                __ioarbiter_flush( ctx );
268
269                if(ctx.sq.to_submit != 0) {
270                        ioring_syscsll(ctx, 0, 0);
271
272                }
273
274                return __cfa_io_drain( proc );
275        }
276
277//=============================================================================================
278// I/O Submissions
279//=============================================================================================
280
281// Submition steps :
282// 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
283//     listed in sq.array are visible by the kernel. For those not listed, the kernel does not
284//     offer any assurance that an entry is not being filled by multiple flags. Therefore, we
285//     need to write an allocator that allows allocating concurrently.
286//
287// 2 - Actually fill the submit entry, this is the only simple and straightforward step.
288//
289// 3 - Append the entry index to the array and adjust the tail accordingly. This operation
290//     needs to arrive to two concensus at the same time:
291//     A - The order in which entries are listed in the array: no two threads must pick the
292//         same index for their entries
293//     B - When can the tail be update for the kernel. EVERY entries in the array between
294//         head and tail must be fully filled and shouldn't ever be touched again.
295//
296        //=============================================================================================
297        // Allocation
298        // for user's convenience fill the sqes from the indexes
299        static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx)  {
300                struct io_uring_sqe * sqes = ctx->sq.sqes;
301                for(i; want) {
302                        // __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
303                        out_sqes[i] = &sqes[idxs[i]];
304                }
305        }
306
307        // Try to directly allocate from the a given context
308        // Not thread-safe
309        static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) {
310                __sub_ring_t & sq = ctx->sq;
311                const __u32 mask  = *sq.mask;
312                __u32 fhead = sq.free_ring.head;    // get the current head of the queue
313                __u32 ftail = sq.free_ring.tail;    // get the current tail of the queue
314
315                // If we don't have enough sqes, fail
316                if((ftail - fhead) < want) { return false; }
317
318                // copy all the indexes we want from the available list
319                for(i; want) {
320                        // __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
321                        idxs[i] = sq.free_ring.array[(fhead + i) & mask];
322                }
323
324                // Advance the head to mark the indexes as consumed
325                __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
326
327                // return success
328                return true;
329        }
330
331        // Allocate an submit queue entry.
332        // The kernel cannot see these entries until they are submitted, but other threads must be
333        // able to see which entries can be used and which are already un used by an other thread
334        // for convenience, return both the index and the pointer to the sqe
335        // sqe == &sqes[idx]
336        struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) {
337                // __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
338
339                disable_interrupts();
340                processor * proc = __cfaabi_tls.this_processor;
341                $io_context * ctx = proc->io.ctx;
342                /* paranoid */ verify( __cfaabi_tls.this_processor );
343                /* paranoid */ verify( ctx );
344
345                // __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
346
347                // We can proceed to the fast path
348                if( __alloc(ctx, idxs, want) ) {
349                        // Allocation was successful
350                        __STATS__( true, io.alloc.fast += 1; )
351                        enable_interrupts();
352
353                        // __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
354
355                        __fill( sqes, want, idxs, ctx );
356                        return ctx;
357                }
358                // The fast path failed, fallback
359                __STATS__( true, io.alloc.fail += 1; )
360
361                // Fast path failed, fallback on arbitration
362                __STATS__( true, io.alloc.slow += 1; )
363                enable_interrupts();
364
365                $io_arbiter * ioarb = proc->cltr->io.arbiter;
366                /* paranoid */ verify( ioarb );
367
368                // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
369
370                struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want);
371
372                // __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
373
374                __fill( sqes, want, idxs,ret );
375                return ret;
376        }
377
378        //=============================================================================================
379        // submission
380        static inline void __submit_only( struct $io_context * ctx, __u32 idxs[], __u32 have) {
381                // We can proceed to the fast path
382                // Get the right objects
383                __sub_ring_t & sq = ctx->sq;
384                const __u32 mask  = *sq.mask;
385                __u32 tail = *sq.kring.tail;
386
387                // Add the sqes to the array
388                for( i; have ) {
389                        // __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
390                        sq.kring.array[ (tail + i) & mask ] = idxs[i];
391                }
392
393                // Make the sqes visible to the submitter
394                __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
395                sq.to_submit += have;
396
397                __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_RELAXED);
398                __atomic_store_n(&ctx->proc->io.dirty  , true, __ATOMIC_RELAXED);
399        }
400
401        static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) {
402                __sub_ring_t & sq = ctx->sq;
403                __submit_only(ctx, idxs, have);
404
405                if(sq.to_submit > 30) {
406                        __tls_stats()->io.flush.full++;
407                        __cfa_io_flush( ctx->proc );
408                }
409                if(!lazy) {
410                        __tls_stats()->io.flush.eager++;
411                        __cfa_io_flush( ctx->proc );
412                }
413        }
414
415        void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
416                // __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
417
418                disable_interrupts();
419                processor * proc = __cfaabi_tls.this_processor;
420                $io_context * ctx = proc->io.ctx;
421                /* paranoid */ verify( __cfaabi_tls.this_processor );
422                /* paranoid */ verify( ctx );
423
424                // Can we proceed to the fast path
425                if( ctx == inctx )              // We have the right instance?
426                {
427                        __submit(ctx, idxs, have, lazy);
428
429                        // Mark the instance as no longer in-use, re-enable interrupts and return
430                        __STATS__( true, io.submit.fast += 1; )
431                        enable_interrupts();
432
433                        // __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
434                        return;
435                }
436
437                // Fast path failed, fallback on arbitration
438                __STATS__( true, io.submit.slow += 1; )
439                enable_interrupts();
440
441                // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
442
443                __ioarbiter_submit(inctx, idxs, have, lazy);
444        }
445
446        //=============================================================================================
447        // Flushing
448        // Go through the ring's submit queue and release everything that has already been consumed
449        // by io_uring
450        // This cannot be done by multiple threads
451        static __u32 __release_sqes( struct $io_context & ctx ) {
452                const __u32 mask = *ctx.sq.mask;
453
454                __attribute__((unused))
455                __u32 ctail = *ctx.sq.kring.tail;    // get the current tail of the queue
456                __u32 chead = *ctx.sq.kring.head;        // get the current head of the queue
457                __u32 phead = ctx.sq.kring.released; // get the head the last time we were here
458
459                __u32 ftail = ctx.sq.free_ring.tail;  // get the current tail of the queue
460
461                // the 3 fields are organized like this diagram
462                // except it's are ring
463                // ---+--------+--------+----
464                // ---+--------+--------+----
465                //    ^        ^        ^
466                // phead    chead    ctail
467
468                // make sure ctail doesn't wrap around and reach phead
469                /* paranoid */ verify(
470                           (ctail >= chead && chead >= phead)
471                        || (chead >= phead && phead >= ctail)
472                        || (phead >= ctail && ctail >= chead)
473                );
474
475                // find the range we need to clear
476                __u32 count = chead - phead;
477
478                if(count == 0) {
479                        return 0;
480                }
481
482                // We acquired an previous-head/current-head range
483                // go through the range and release the sqes
484                for( i; count ) {
485                        // __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
486                        __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
487                        ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
488                }
489
490                ctx.sq.kring.released = chead;          // note up to were we processed
491                __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST);
492
493                __ioarbiter_notify(ctx);
494
495                return count;
496        }
497
498//=============================================================================================
499// I/O Arbiter
500//=============================================================================================
501        static inline bool enqueue(__outstanding_io_queue & queue, __outstanding_io & item) {
502                bool was_empty;
503
504                // Lock the list, it's not thread safe
505                lock( queue.lock __cfaabi_dbg_ctx2 );
506                {
507                        was_empty = empty(queue.queue);
508
509                        // Add our request to the list
510                        add( queue.queue, item );
511
512                        // Mark as pending
513                        __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST );
514                }
515                unlock( queue.lock );
516
517                return was_empty;
518        }
519
520        static inline bool empty(__outstanding_io_queue & queue ) {
521                return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST);
522        }
523
524        static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) {
525                // __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
526
527                __STATS__( false, io.alloc.block += 1; )
528
529                // No one has any resources left, wait for something to finish
530                // We need to add ourself to a list of pending allocs and wait for an answer
531                __pending_alloc pa;
532                pa.idxs = idxs;
533                pa.want = want;
534
535                enqueue(this.pending, (__outstanding_io&)pa);
536
537                wait( pa.sem );
538
539                return pa.ctx;
540
541        }
542
543        static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) {
544                /* paranoid */ verify( !empty(this.pending.queue) );
545
546                lock( this.pending.lock __cfaabi_dbg_ctx2 );
547                {
548                        while( !empty(this.pending.queue) ) {
549                                __cfadbg_print_safe(io, "Kernel I/O : notifying\n");
550                                __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head;
551                                __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue );
552
553                                if( have > pa.want ) goto DONE;
554                                drop( this.pending.queue );
555
556                                /* paranoid */__attribute__((unused)) bool ret =
557
558                                __alloc(ctx, pa.idxs, pa.want);
559
560                                /* paranoid */ verify( ret );
561
562                                pa.ctx = ctx;
563
564                                post( pa.sem );
565                        }
566
567                        this.pending.empty = true;
568                        DONE:;
569                }
570                unlock( this.pending.lock );
571        }
572
573        static void __ioarbiter_notify( $io_context & ctx ) {
574                if(!empty( ctx.arbiter->pending )) {
575                        __ioarbiter_notify( *ctx.arbiter, &ctx );
576                }
577        }
578
579        // Simply append to the pending
580        static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
581                __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd);
582
583                __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have);
584
585                __external_io ei;
586                ei.idxs = idxs;
587                ei.have = have;
588                ei.lazy = lazy;
589
590                bool we = enqueue(ctx->ext_sq, (__outstanding_io&)ei);
591
592                __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_SEQ_CST);
593
594                if( we ) {
595                        sigval_t value = { PREEMPT_IO };
596                        pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value);
597                }
598
599                wait( ei.sem );
600
601                __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
602        }
603
604        static void __ioarbiter_flush( $io_context & ctx ) {
605                if(!empty( ctx.ext_sq )) {
606                        __STATS__( false, io.flush.external += 1; )
607
608                        __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
609
610                        lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 );
611                        {
612                                while( !empty(ctx.ext_sq.queue) ) {
613                                        __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue );
614
615                                        __submit_only(&ctx, ei.idxs, ei.have);
616
617                                        post( ei.sem );
618                                }
619
620                                ctx.ext_sq.empty = true;
621                        }
622                        unlock(ctx.ext_sq.lock );
623                }
624        }
625
626        #if defined(CFA_WITH_IO_URING_IDLE)
627                bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) {
628                        $io_context * ctx = proc->io.ctx;
629                        /* paranoid */ verify( ! __preemption_enabled() );
630                        /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
631                        /* paranoid */ verify( ctx );
632
633                        __u32 idx;
634                        struct io_uring_sqe * sqe;
635
636                        // We can proceed to the fast path
637                        if( !__alloc(ctx, &idx, 1) ) return false;
638
639                        // Allocation was successful
640                        __fill( &sqe, 1, &idx, ctx );
641
642                        sqe->user_data = (uintptr_t)&future;
643                        sqe->flags = 0;
644                        sqe->fd = fd;
645                        sqe->off = 0;
646                        sqe->ioprio = 0;
647                        sqe->fsync_flags = 0;
648                        sqe->__pad2[0] = 0;
649                        sqe->__pad2[1] = 0;
650                        sqe->__pad2[2] = 0;
651
652                        #if defined(CFA_HAVE_IORING_OP_READ)
653                                sqe->opcode = IORING_OP_READ;
654                                sqe->addr = (uint64_t)iov.iov_base;
655                                sqe->len = iov.iov_len;
656                        #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV)
657                                sqe->opcode = IORING_OP_READV;
658                                sqe->addr = (uintptr_t)&iov;
659                                sqe->len = 1;
660                        #else
661                                #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined
662                        #endif
663
664                        asm volatile("": : :"memory");
665
666                        /* paranoid */ verify( sqe->user_data == (uintptr_t)&future );
667                        __submit( ctx, &idx, 1, true );
668
669                        /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
670                        /* paranoid */ verify( ! __preemption_enabled() );
671
672                        return true;
673                }
674
675                void __cfa_io_idle( processor * proc ) {
676                        iovec iov;
677                        __atomic_acquire( &proc->io.ctx->cq.lock );
678
679                        with( proc->idle_wctx) {
680
681                                // Do we already have a pending read
682                                if(available(*ftr)) {
683                                        // There is no pending read, we need to add one
684                                        reset(*ftr);
685
686                                        iov.iov_base = rdbuf;
687                                        iov.iov_len  = sizeof(eventfd_t);
688                                        __kernel_read(proc, *ftr, iov, evfd );
689                                }
690                        }
691
692                        __ioarbiter_flush( *proc->io.ctx );
693                        ioring_syscsll( *proc->io.ctx, 1, IORING_ENTER_GETEVENTS);
694
695                        ready_schedule_lock();
696                        __cfa_do_drain( proc->io.ctx, proc->cltr );
697                        ready_schedule_unlock();
698                }
699        #endif
700#endif
Note: See TracBrowser for help on using the repository browser.