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

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

Fix build from machine where I can actually test

  • 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                __cfadbg_print_safe(io, "Kernel I/O : %u completed\n", count);
177                unsigned long long ts_next = ctx->cq.ts = rdtscl();
178
179                // Mark to the kernel that the cqe has been seen
180                // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
181                __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST );
182
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(ready_queue, "Kernel : 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                cluster * cltr = proc->cltr;
266                $io_context & ctx = *proc->io.ctx;
267
268                __ioarbiter_flush( ctx );
269
270                if(ctx.sq.to_submit != 0) {
271                        ioring_syscsll(ctx, 0, 0);
272
273                }
274
275                return __cfa_io_drain( proc );
276        }
277
278//=============================================================================================
279// I/O Submissions
280//=============================================================================================
281
282// Submition steps :
283// 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
284//     listed in sq.array are visible by the kernel. For those not listed, the kernel does not
285//     offer any assurance that an entry is not being filled by multiple flags. Therefore, we
286//     need to write an allocator that allows allocating concurrently.
287//
288// 2 - Actually fill the submit entry, this is the only simple and straightforward step.
289//
290// 3 - Append the entry index to the array and adjust the tail accordingly. This operation
291//     needs to arrive to two concensus at the same time:
292//     A - The order in which entries are listed in the array: no two threads must pick the
293//         same index for their entries
294//     B - When can the tail be update for the kernel. EVERY entries in the array between
295//         head and tail must be fully filled and shouldn't ever be touched again.
296//
297        //=============================================================================================
298        // Allocation
299        // for user's convenience fill the sqes from the indexes
300        static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx)  {
301                struct io_uring_sqe * sqes = ctx->sq.sqes;
302                for(i; want) {
303                        __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
304                        out_sqes[i] = &sqes[idxs[i]];
305                }
306        }
307
308        // Try to directly allocate from the a given context
309        // Not thread-safe
310        static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) {
311                __sub_ring_t & sq = ctx->sq;
312                const __u32 mask  = *sq.mask;
313                __u32 fhead = sq.free_ring.head;    // get the current head of the queue
314                __u32 ftail = sq.free_ring.tail;    // get the current tail of the queue
315
316                // If we don't have enough sqes, fail
317                if((ftail - fhead) < want) { return false; }
318
319                // copy all the indexes we want from the available list
320                for(i; want) {
321                        __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
322                        idxs[i] = sq.free_ring.array[(fhead + i) & mask];
323                }
324
325                // Advance the head to mark the indexes as consumed
326                __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
327
328                // return success
329                return true;
330        }
331
332        // Allocate an submit queue entry.
333        // The kernel cannot see these entries until they are submitted, but other threads must be
334        // able to see which entries can be used and which are already un used by an other thread
335        // for convenience, return both the index and the pointer to the sqe
336        // sqe == &sqes[idx]
337        struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) {
338                __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
339
340                disable_interrupts();
341                processor * proc = __cfaabi_tls.this_processor;
342                $io_context * ctx = proc->io.ctx;
343                /* paranoid */ verify( __cfaabi_tls.this_processor );
344                /* paranoid */ verify( ctx );
345
346                __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
347
348                // We can proceed to the fast path
349                if( __alloc(ctx, idxs, want) ) {
350                        // Allocation was successful
351                        __STATS__( true, io.alloc.fast += 1; )
352                        enable_interrupts();
353
354                        __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
355
356                        __fill( sqes, want, idxs, ctx );
357                        return ctx;
358                }
359                // The fast path failed, fallback
360                __STATS__( true, io.alloc.fail += 1; )
361
362                // Fast path failed, fallback on arbitration
363                __STATS__( true, io.alloc.slow += 1; )
364                enable_interrupts();
365
366                $io_arbiter * ioarb = proc->cltr->io.arbiter;
367                /* paranoid */ verify( ioarb );
368
369                __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
370
371                struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want);
372
373                __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
374
375                __fill( sqes, want, idxs,ret );
376                return ret;
377        }
378
379        //=============================================================================================
380        // submission
381        static inline void __submit_only( struct $io_context * ctx, __u32 idxs[], __u32 have) {
382                // We can proceed to the fast path
383                // Get the right objects
384                __sub_ring_t & sq = ctx->sq;
385                const __u32 mask  = *sq.mask;
386                __u32 tail = *sq.kring.tail;
387
388                // Add the sqes to the array
389                for( i; have ) {
390                        __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
391                        sq.kring.array[ (tail + i) & mask ] = idxs[i];
392                }
393
394                // Make the sqes visible to the submitter
395                __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
396                sq.to_submit += have;
397
398                __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_RELAXED);
399                __atomic_store_n(&ctx->proc->io.dirty  , true, __ATOMIC_RELAXED);
400        }
401
402        static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) {
403                __sub_ring_t & sq = ctx->sq;
404                __submit_only(ctx, idxs, have);
405
406                if(sq.to_submit > 30) {
407                        __tls_stats()->io.flush.full++;
408                        __cfa_io_flush( ctx->proc );
409                }
410                if(!lazy) {
411                        __tls_stats()->io.flush.eager++;
412                        __cfa_io_flush( ctx->proc );
413                }
414        }
415
416        void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
417                __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
418
419                disable_interrupts();
420                processor * proc = __cfaabi_tls.this_processor;
421                $io_context * ctx = proc->io.ctx;
422                /* paranoid */ verify( __cfaabi_tls.this_processor );
423                /* paranoid */ verify( ctx );
424
425                // Can we proceed to the fast path
426                if( ctx == inctx )              // We have the right instance?
427                {
428                        __submit(ctx, idxs, have, lazy);
429
430                        // Mark the instance as no longer in-use, re-enable interrupts and return
431                        __STATS__( true, io.submit.fast += 1; )
432                        enable_interrupts();
433
434                        __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
435                        return;
436                }
437
438                // Fast path failed, fallback on arbitration
439                __STATS__( true, io.submit.slow += 1; )
440                enable_interrupts();
441
442                __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
443
444                __ioarbiter_submit(inctx, idxs, have, lazy);
445        }
446
447        //=============================================================================================
448        // Flushing
449        // Go through the ring's submit queue and release everything that has already been consumed
450        // by io_uring
451        // This cannot be done by multiple threads
452        static __u32 __release_sqes( struct $io_context & ctx ) {
453                const __u32 mask = *ctx.sq.mask;
454
455                __attribute__((unused))
456                __u32 ctail = *ctx.sq.kring.tail;    // get the current tail of the queue
457                __u32 chead = *ctx.sq.kring.head;        // get the current head of the queue
458                __u32 phead = ctx.sq.kring.released; // get the head the last time we were here
459
460                __u32 ftail = ctx.sq.free_ring.tail;  // get the current tail of the queue
461
462                // the 3 fields are organized like this diagram
463                // except it's are ring
464                // ---+--------+--------+----
465                // ---+--------+--------+----
466                //    ^        ^        ^
467                // phead    chead    ctail
468
469                // make sure ctail doesn't wrap around and reach phead
470                /* paranoid */ verify(
471                           (ctail >= chead && chead >= phead)
472                        || (chead >= phead && phead >= ctail)
473                        || (phead >= ctail && ctail >= chead)
474                );
475
476                // find the range we need to clear
477                __u32 count = chead - phead;
478
479                if(count == 0) {
480                        return 0;
481                }
482
483                // We acquired an previous-head/current-head range
484                // go through the range and release the sqes
485                for( i; count ) {
486                        __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
487                        __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
488                        ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
489                }
490
491                ctx.sq.kring.released = chead;          // note up to were we processed
492                __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST);
493
494                __ioarbiter_notify(ctx);
495
496                return count;
497        }
498
499//=============================================================================================
500// I/O Arbiter
501//=============================================================================================
502        static inline bool enqueue(__outstanding_io_queue & queue, __outstanding_io & item) {
503                bool was_empty;
504
505                // Lock the list, it's not thread safe
506                lock( queue.lock __cfaabi_dbg_ctx2 );
507                {
508                        was_empty = empty(queue.queue);
509
510                        // Add our request to the list
511                        add( queue.queue, item );
512
513                        // Mark as pending
514                        __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST );
515                }
516                unlock( queue.lock );
517
518                return was_empty;
519        }
520
521        static inline bool empty(__outstanding_io_queue & queue ) {
522                return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST);
523        }
524
525        static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) {
526                __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
527
528                __STATS__( false, io.alloc.block += 1; )
529
530                // No one has any resources left, wait for something to finish
531                // We need to add ourself to a list of pending allocs and wait for an answer
532                __pending_alloc pa;
533                pa.idxs = idxs;
534                pa.want = want;
535
536                enqueue(this.pending, (__outstanding_io&)pa);
537
538                wait( pa.sem );
539
540                return pa.ctx;
541
542        }
543
544        static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) {
545                /* paranoid */ verify( !empty(this.pending.queue) );
546
547                lock( this.pending.lock __cfaabi_dbg_ctx2 );
548                {
549                        while( !empty(this.pending.queue) ) {
550                                __cfadbg_print_safe(io, "Kernel I/O : notifying\n");
551                                __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head;
552                                __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue );
553
554                                if( have > pa.want ) goto DONE;
555                                drop( this.pending.queue );
556
557                                /* paranoid */__attribute__((unused)) bool ret =
558
559                                __alloc(ctx, pa.idxs, pa.want);
560
561                                /* paranoid */ verify( ret );
562
563                                pa.ctx = ctx;
564
565                                post( pa.sem );
566                        }
567
568                        this.pending.empty = true;
569                        DONE:;
570                }
571                unlock( this.pending.lock );
572        }
573
574        static void __ioarbiter_notify( $io_context & ctx ) {
575                if(!empty( ctx.arbiter->pending )) {
576                        __ioarbiter_notify( *ctx.arbiter, &ctx );
577                }
578        }
579
580        // Simply append to the pending
581        static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
582                __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd);
583
584                __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have);
585
586                __external_io ei;
587                ei.idxs = idxs;
588                ei.have = have;
589                ei.lazy = lazy;
590
591                bool we = enqueue(ctx->ext_sq, (__outstanding_io&)ei);
592
593                __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_SEQ_CST);
594
595                if( we ) {
596                        sigval_t value = { PREEMPT_IO };
597                        pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value);
598                }
599
600                wait( ei.sem );
601
602                __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
603        }
604
605        static void __ioarbiter_flush( $io_context & ctx ) {
606                if(!empty( ctx.ext_sq )) {
607                        __STATS__( false, io.flush.external += 1; )
608
609                        __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
610
611                        lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 );
612                        {
613                                while( !empty(ctx.ext_sq.queue) ) {
614                                        __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue );
615
616                                        __submit_only(&ctx, ei.idxs, ei.have);
617
618                                        post( ei.sem );
619                                }
620
621                                ctx.ext_sq.empty = true;
622                        }
623                        unlock(ctx.ext_sq.lock );
624                }
625        }
626
627        #if defined(CFA_WITH_IO_URING_IDLE)
628                bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) {
629                        $io_context * ctx = proc->io.ctx;
630                        /* paranoid */ verify( ! __preemption_enabled() );
631                        /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
632                        /* paranoid */ verify( ctx );
633
634                        __u32 idx;
635                        struct io_uring_sqe * sqe;
636
637                        // We can proceed to the fast path
638                        if( !__alloc(ctx, &idx, 1) ) return false;
639
640                        // Allocation was successful
641                        __fill( &sqe, 1, &idx, ctx );
642
643                        sqe->user_data = (uintptr_t)&future;
644                        sqe->flags = 0;
645                        sqe->fd = fd;
646                        sqe->off = 0;
647                        sqe->ioprio = 0;
648                        sqe->fsync_flags = 0;
649                        sqe->__pad2[0] = 0;
650                        sqe->__pad2[1] = 0;
651                        sqe->__pad2[2] = 0;
652
653                        #if defined(CFA_HAVE_IORING_OP_READ)
654                                sqe->opcode = IORING_OP_READ;
655                                sqe->addr = (uint64_t)iov.iov_base;
656                                sqe->len = iov.iov_len;
657                        #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV)
658                                sqe->opcode = IORING_OP_READV;
659                                sqe->addr = (uintptr_t)&iov;
660                                sqe->len = 1;
661                        #else
662                                #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined
663                        #endif
664
665                        asm volatile("": : :"memory");
666
667                        /* paranoid */ verify( sqe->user_data == (uintptr_t)&future );
668                        __submit( ctx, &idx, 1, true );
669
670                        /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
671                        /* paranoid */ verify( ! __preemption_enabled() );
672
673                        return true;
674                }
675
676                void __cfa_io_idle( processor * proc ) {
677                        iovec iov;
678                        __atomic_acquire( &proc->io.ctx->cq.lock );
679
680                        with( proc->idle_wctx) {
681
682                                // Do we already have a pending read
683                                if(available(*ftr)) {
684                                        // There is no pending read, we need to add one
685                                        reset(*ftr);
686
687                                        iov.iov_base = rdbuf;
688                                        iov.iov_len  = sizeof(eventfd_t);
689                                        __kernel_read(proc, *ftr, iov, evfd );
690                                }
691                        }
692
693                        __ioarbiter_flush( *proc->io.ctx );
694                        ioring_syscsll( *proc->io.ctx, 1, IORING_ENTER_GETEVENTS);
695
696                        ready_schedule_lock();
697                        __cfa_do_drain( proc->io.ctx, proc->cltr );
698                        ready_schedule_unlock();
699                }
700        #endif
701#endif
Note: See TracBrowser for help on using the repository browser.