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

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

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 21.3 KB
RevLine 
[ecf6b46]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
[3e2b9c9]16#define __cforall_thread__
[43784ac]17#define _GNU_SOURCE
[3e2b9c9]18
[20ab637]19#if defined(__CFA_DEBUG__)
[d60d30e]20        // #define __CFA_DEBUG_PRINT_IO__
21        // #define __CFA_DEBUG_PRINT_IO_CORE__
[20ab637]22#endif
[4069faad]23
[f6660520]24
[3e2b9c9]25#if defined(CFA_HAVE_LINUX_IO_URING_H)
[31bb2e1]26        #include <errno.h>
[3e2b9c9]27        #include <signal.h>
[31bb2e1]28        #include <stdint.h>
29        #include <string.h>
30        #include <unistd.h>
31
[92976d9]32        extern "C" {
33                #include <sys/syscall.h>
[dddb3dd0]34                #include <sys/eventfd.h>
[d3605f8]35                #include <sys/uio.h>
[92976d9]36
37                #include <linux/io_uring.h>
38        }
39
[3e2b9c9]40        #include "stats.hfa"
41        #include "kernel.hfa"
42        #include "kernel/fwd.hfa"
[708ae38]43        #include "kernel/private.hfa"
[78a580d]44        #include "kernel/cluster.hfa"
[3e2b9c9]45        #include "io/types.hfa"
[185efe6]46
[2fab24e3]47        __attribute__((unused)) static const char * opcodes[] = {
[426f60c]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
[11054eb]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 & );
[dddb3dd0]88        static inline void __ioarbiter_notify( $io_context & ctx );
[92976d9]89//=============================================================================================
90// I/O Polling
91//=============================================================================================
[78da4ab]92        static inline unsigned __flush( struct $io_context & );
93        static inline __u32 __release_sqes( struct $io_context & );
[24e321c]94        extern void __kernel_unpark( thread$ * thrd, unpark_hint );
[1d5e4711]95
[18f7858]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
[dddb3dd0]124                /* paranoid */ verify( ! __preemption_enabled() );
[6f121b8]125
[18f7858]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() );
[92976d9]132
[d60d30e]133
[3caf5e3]134                {
135                        const __u32 head = *ctx->cq.head;
136                        const __u32 tail = *ctx->cq.tail;
137
138                        if(head == tail) return false;
139                }
[c1c95b1]140
[3caf5e3]141                // Drain the queue
[4ecc35a]142                if(!__atomic_try_acquire(&ctx->cq.lock)) {
[54c1196]143                        __STATS__( false, io.calls.locked++; )
[4ecc35a]144                        return false;
145                }
146
[18f7858]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;
[78a580d]156                unsigned long long ts_prev = ctx->cq.ts;
157
[3caf5e3]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
[d384787]164                for(i; count) {
[6f121b8]165                        unsigned idx = (head + i) & mask;
[dddb3dd0]166                        volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx];
[92976d9]167
[d384787]168                        /* paranoid */ verify(&cqe);
[92976d9]169
[78da4ab]170                        struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
[1e6ffb44]171                        // __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
[78da4ab]172
[24e321c]173                        __kernel_unpark( fulfil( *future, cqe.res, false ), UNPARK_LOCAL );
[78da4ab]174                }
175
[78a580d]176                unsigned long long ts_next = ctx->cq.ts = rdtscl();
[2d8f7b0]177
[92976d9]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.
[dddb3dd0]180                __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST );
[d28b70a]181                ctx->proc->idle_wctx.drain_time = ts_next;
[92976d9]182
[1e6ffb44]183                __cfadbg_print_safe(io, "Kernel I/O : %u completed age %llu\n", count, ts_next);
[e9c0b4c]184                /* paranoid */ verify( ready_schedule_islocked() );
[dddb3dd0]185                /* paranoid */ verify( ! __preemption_enabled() );
186
[4ecc35a]187                __atomic_unlock(&ctx->cq.lock);
188
[78a580d]189                touch_tsc( cltr->sched.io.tscs, ctx->cq.id, ts_prev, ts_next );
190
[c1c95b1]191                return true;
[92976d9]192        }
193
[4479890]194        bool __cfa_io_drain( processor * proc ) {
195                bool local = false;
196                bool remote = false;
197
[18f7858]198                ready_schedule_lock();
199
[4479890]200                cluster * const cltr = proc->cltr;
201                $io_context * const ctx = proc->io.ctx;
202                /* paranoid */ verify( cltr );
203                /* paranoid */ verify( ctx );
204
205                with(cltr->sched) {
206                        const size_t ctxs_count = io.count;
207
208                        /* paranoid */ verify( ready_schedule_islocked() );
209                        /* paranoid */ verify( ! __preemption_enabled() );
210                        /* paranoid */ verify( active_processor() == proc );
211                        /* paranoid */ verify( __shard_factor.io > 0 );
212                        /* paranoid */ verify( ctxs_count > 0 );
213                        /* paranoid */ verify( ctx->cq.id < ctxs_count );
214
215                        const unsigned this_cache = cache_id(cltr, ctx->cq.id / __shard_factor.io);
216                        const unsigned long long ctsc = rdtscl();
217
218                        if(proc->io.target == MAX) {
219                                uint64_t chaos = __tls_rand();
220                                unsigned ext = chaos & 0xff;
221                                unsigned other  = (chaos >> 8) % (ctxs_count);
222
223                                if(ext < 3 || __atomic_load_n(&caches[other / __shard_factor.io].id, __ATOMIC_RELAXED) == this_cache) {
224                                        proc->io.target = other;
225                                }
226                        }
227                        else {
228                                const unsigned target = proc->io.target;
229                                /* paranoid */ verify( io.tscs[target].tv != MAX );
[18f7858]230                                HELP: if(target < ctxs_count) {
[4479890]231                                        const unsigned long long cutoff = calc_cutoff(ctsc, ctx->cq.id, ctxs_count, io.data, io.tscs, __shard_factor.io);
232                                        const unsigned long long age = moving_average(ctsc, io.tscs[target].tv, io.tscs[target].ma);
[edf247b]233                                        __cfadbg_print_safe(io, "Kernel I/O: Help attempt on %u from %u, age %'llu vs cutoff %'llu, %s\n", target, ctx->cq.id, age, cutoff, age > cutoff ? "yes" : "no");
[18f7858]234                                        if(age <= cutoff) break HELP;
235
236                                        if(!try_acquire(io.data[target])) break HELP;
237
238                                        if(!__cfa_do_drain( io.data[target], cltr )) break HELP;
239
240                                        remote = true;
241                                        __STATS__( false, io.calls.helped++; )
[4479890]242                                }
243                                proc->io.target = MAX;
244                        }
245                }
246
247
248                // Drain the local queue
[18f7858]249                if(try_acquire( proc->io.ctx )) {
250                        local = __cfa_do_drain( proc->io.ctx, cltr );
251                }
[4479890]252
253                /* paranoid */ verify( ready_schedule_islocked() );
254                /* paranoid */ verify( ! __preemption_enabled() );
255                /* paranoid */ verify( active_processor() == proc );
[18f7858]256
257                ready_schedule_unlock();
[4479890]258                return local || remote;
259        }
260
[18f7858]261        bool __cfa_io_flush( processor * proc ) {
[dddb3dd0]262                /* paranoid */ verify( ! __preemption_enabled() );
263                /* paranoid */ verify( proc );
264                /* paranoid */ verify( proc->io.ctx );
[1539bbd]265
[dddb3dd0]266                $io_context & ctx = *proc->io.ctx;
[78da4ab]267
[11054eb]268                __ioarbiter_flush( ctx );
[3c039b0]269
[18f7858]270                if(ctx.sq.to_submit != 0) {
271                        ioring_syscsll(ctx, 0, 0);
[21a5bfb7]272
273                }
[61dd73d]274
[18f7858]275                return __cfa_io_drain( proc );
[61dd73d]276        }
[f6660520]277
[92976d9]278//=============================================================================================
279// I/O Submissions
280//=============================================================================================
281
[2d8f7b0]282// Submition steps :
[e46c753]283// 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
[2d8f7b0]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//
[e46c753]288// 2 - Actually fill the submit entry, this is the only simple and straightforward step.
[2d8f7b0]289//
[e46c753]290// 3 - Append the entry index to the array and adjust the tail accordingly. This operation
[2d8f7b0]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//
[78da4ab]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) {
[1e6ffb44]303                        // __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
[78da4ab]304                        out_sqes[i] = &sqes[idxs[i]];
305                }
306        }
[2489d31]307
[78da4ab]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
[2489d31]315
[78da4ab]316                // If we don't have enough sqes, fail
317                if((ftail - fhead) < want) { return false; }
[426f60c]318
[78da4ab]319                // copy all the indexes we want from the available list
320                for(i; want) {
[1e6ffb44]321                        // __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
[78da4ab]322                        idxs[i] = sq.free_ring.array[(fhead + i) & mask];
[6f121b8]323                }
[2489d31]324
[78da4ab]325                // Advance the head to mark the indexes as consumed
326                __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
[df40a56]327
[78da4ab]328                // return success
329                return true;
330        }
[df40a56]331
[78da4ab]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) {
[1e6ffb44]338                // __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
[df40a56]339
[78da4ab]340                disable_interrupts();
341                processor * proc = __cfaabi_tls.this_processor;
[dddb3dd0]342                $io_context * ctx = proc->io.ctx;
[78da4ab]343                /* paranoid */ verify( __cfaabi_tls.this_processor );
[dddb3dd0]344                /* paranoid */ verify( ctx );
[78da4ab]345
[1e6ffb44]346                // __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
[78da4ab]347
[dddb3dd0]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; )
[a3821fa]352                        enable_interrupts();
[df40a56]353
[1e6ffb44]354                        // __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
[2fafe7e]355
[dddb3dd0]356                        __fill( sqes, want, idxs, ctx );
357                        return ctx;
[df40a56]358                }
[dddb3dd0]359                // The fast path failed, fallback
360                __STATS__( true, io.alloc.fail += 1; )
[df40a56]361
[78da4ab]362                // Fast path failed, fallback on arbitration
[d60d30e]363                __STATS__( true, io.alloc.slow += 1; )
[a3821fa]364                enable_interrupts();
[78da4ab]365
[dddb3dd0]366                $io_arbiter * ioarb = proc->cltr->io.arbiter;
367                /* paranoid */ verify( ioarb );
368
[1e6ffb44]369                // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
[78da4ab]370
[11054eb]371                struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want);
[78da4ab]372
[1e6ffb44]373                // __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
[df40a56]374
[78da4ab]375                __fill( sqes, want, idxs,ret );
376                return ret;
[df40a56]377        }
378
[78da4ab]379        //=============================================================================================
380        // submission
[2432e8e]381        static inline void __submit_only( struct $io_context * ctx, __u32 idxs[], __u32 have) {
[78da4ab]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;
[dddb3dd0]386                __u32 tail = *sq.kring.tail;
[78da4ab]387
388                // Add the sqes to the array
389                for( i; have ) {
[1e6ffb44]390                        // __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
[78da4ab]391                        sq.kring.array[ (tail + i) & mask ] = idxs[i];
[426f60c]392                }
393
[78da4ab]394                // Make the sqes visible to the submitter
[dddb3dd0]395                __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
[e8ac228]396                sq.to_submit += have;
[426f60c]397
[d529ad0]398                __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_RELAXED);
399                __atomic_store_n(&ctx->proc->io.dirty  , true, __ATOMIC_RELAXED);
[2432e8e]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
[70b4aeb9]406                if(sq.to_submit > 30) {
407                        __tls_stats()->io.flush.full++;
[18f7858]408                        __cfa_io_flush( ctx->proc );
[70b4aeb9]409                }
410                if(!lazy) {
411                        __tls_stats()->io.flush.eager++;
[18f7858]412                        __cfa_io_flush( ctx->proc );
[dddb3dd0]413                }
[78da4ab]414        }
[2489d31]415
[dddb3dd0]416        void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
[1e6ffb44]417                // __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
[5dadc9b]418
[78da4ab]419                disable_interrupts();
420                processor * proc = __cfaabi_tls.this_processor;
421                $io_context * ctx = proc->io.ctx;
[dddb3dd0]422                /* paranoid */ verify( __cfaabi_tls.this_processor );
423                /* paranoid */ verify( ctx );
[e46c753]424
[78da4ab]425                // Can we proceed to the fast path
[dddb3dd0]426                if( ctx == inctx )              // We have the right instance?
[78da4ab]427                {
[dddb3dd0]428                        __submit(ctx, idxs, have, lazy);
[e46c753]429
[78da4ab]430                        // Mark the instance as no longer in-use, re-enable interrupts and return
[d60d30e]431                        __STATS__( true, io.submit.fast += 1; )
[a3821fa]432                        enable_interrupts();
[ece0e80]433
[1e6ffb44]434                        // __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
[78da4ab]435                        return;
[e46c753]436                }
[d384787]437
[78da4ab]438                // Fast path failed, fallback on arbitration
[d60d30e]439                __STATS__( true, io.submit.slow += 1; )
[a3821fa]440                enable_interrupts();
[5dadc9b]441
[1e6ffb44]442                // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
[426f60c]443
[11054eb]444                __ioarbiter_submit(inctx, idxs, have, lazy);
[78da4ab]445        }
[2fab24e3]446
[78da4ab]447        //=============================================================================================
448        // Flushing
[426f60c]449        // Go through the ring's submit queue and release everything that has already been consumed
450        // by io_uring
[78da4ab]451        // This cannot be done by multiple threads
452        static __u32 __release_sqes( struct $io_context & ctx ) {
453                const __u32 mask = *ctx.sq.mask;
[732b406]454
[426f60c]455                __attribute__((unused))
[78da4ab]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
[732b406]461
[426f60c]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
[4998155]477                __u32 count = chead - phead;
[426f60c]478
[78da4ab]479                if(count == 0) {
480                        return 0;
481                }
482
[426f60c]483                // We acquired an previous-head/current-head range
484                // go through the range and release the sqes
[34b61882]485                for( i; count ) {
[1e6ffb44]486                        // __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
[78da4ab]487                        __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
488                        ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
[34b61882]489                }
[78da4ab]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
[34b61882]496                return count;
497        }
[35285fd]498
[78da4ab]499//=============================================================================================
500// I/O Arbiter
501//=============================================================================================
[9f5a71eb]502        static inline bool enqueue(__outstanding_io_queue & queue, __outstanding_io & item) {
503                bool was_empty;
504
[11054eb]505                // Lock the list, it's not thread safe
506                lock( queue.lock __cfaabi_dbg_ctx2 );
507                {
[9f5a71eb]508                        was_empty = empty(queue.queue);
509
[11054eb]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
[9f5a71eb]518                return was_empty;
[11054eb]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 ) {
[1e6ffb44]526                // __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
[78da4ab]527
[d60d30e]528                __STATS__( false, io.alloc.block += 1; )
529
[78da4ab]530                // No one has any resources left, wait for something to finish
[11054eb]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;
[78da4ab]535
[9f5a71eb]536                enqueue(this.pending, (__outstanding_io&)pa);
537
538                wait( pa.sem );
[78da4ab]539
[11054eb]540                return pa.ctx;
[dddb3dd0]541
[78da4ab]542        }
543
[11054eb]544        static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) {
545                /* paranoid */ verify( !empty(this.pending.queue) );
[78da4ab]546
[11054eb]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 );
[78da4ab]553
[11054eb]554                                if( have > pa.want ) goto DONE;
555                                drop( this.pending.queue );
[78da4ab]556
[11054eb]557                                /* paranoid */__attribute__((unused)) bool ret =
[78da4ab]558
[11054eb]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 );
[78da4ab]572        }
573
574        static void __ioarbiter_notify( $io_context & ctx ) {
[11054eb]575                if(!empty( ctx.arbiter->pending )) {
[78da4ab]576                        __ioarbiter_notify( *ctx.arbiter, &ctx );
577                }
578        }
579
580        // Simply append to the pending
[11054eb]581        static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
[78da4ab]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
[11054eb]586                __external_io ei;
587                ei.idxs = idxs;
588                ei.have = have;
589                ei.lazy = lazy;
[78da4ab]590
[9f5a71eb]591                bool we = enqueue(ctx->ext_sq, (__outstanding_io&)ei);
592
[d529ad0]593                __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_SEQ_CST);
[9f5a71eb]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 );
[78da4ab]601
602                __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
603        }
604
[11054eb]605        static void __ioarbiter_flush( $io_context & ctx ) {
606                if(!empty( ctx.ext_sq )) {
607                        __STATS__( false, io.flush.external += 1; )
[78da4ab]608
[11054eb]609                        __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
[d60d30e]610
[11054eb]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 );
[78da4ab]615
[2432e8e]616                                        __submit_only(&ctx, ei.idxs, ei.have);
[78da4ab]617
[11054eb]618                                        post( ei.sem );
619                                }
620
621                                ctx.ext_sq.empty = true;
622                        }
623                        unlock(ctx.ext_sq.lock );
624                }
[78da4ab]625        }
[7ef162b2]626
[d3605f8]627        #if defined(CFA_WITH_IO_URING_IDLE)
628                bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) {
[6ddef36]629                        $io_context * ctx = proc->io.ctx;
630                        /* paranoid */ verify( ! __preemption_enabled() );
631                        /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
632                        /* paranoid */ verify( ctx );
[7ef162b2]633
[6ddef36]634                        __u32 idx;
635                        struct io_uring_sqe * sqe;
[7ef162b2]636
[6ddef36]637                        // We can proceed to the fast path
[010636f]638                        if( !__alloc(ctx, &idx, 1) ) {
639                                /* paranoid */ verify( false ); // for now check if this happens, next time just abort the sleep.
640                                return false;
641                        }
[6ddef36]642
643                        // Allocation was successful
644                        __fill( &sqe, 1, &idx, ctx );
645
646                        sqe->user_data = (uintptr_t)&future;
647                        sqe->flags = 0;
[a1f3d93]648                        sqe->fd = fd;
[6ddef36]649                        sqe->off = 0;
[d3605f8]650                        sqe->ioprio = 0;
[6ddef36]651                        sqe->fsync_flags = 0;
652                        sqe->__pad2[0] = 0;
653                        sqe->__pad2[1] = 0;
654                        sqe->__pad2[2] = 0;
[d3605f8]655
656                        #if defined(CFA_HAVE_IORING_OP_READ)
657                                sqe->opcode = IORING_OP_READ;
658                                sqe->addr = (uint64_t)iov.iov_base;
659                                sqe->len = iov.iov_len;
660                        #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV)
661                                sqe->opcode = IORING_OP_READV;
662                                sqe->addr = (uintptr_t)&iov;
663                                sqe->len = 1;
664                        #else
665                                #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined
666                        #endif
[6ddef36]667
668                        asm volatile("": : :"memory");
669
670                        /* paranoid */ verify( sqe->user_data == (uintptr_t)&future );
[010636f]671                        __submit_only( ctx, &idx, 1 );
[6ddef36]672
673                        /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
674                        /* paranoid */ verify( ! __preemption_enabled() );
[078fb05]675
676                        return true;
[6ddef36]677                }
[18f7858]678
679                void __cfa_io_idle( processor * proc ) {
680                        iovec iov;
681                        __atomic_acquire( &proc->io.ctx->cq.lock );
682
[262fafd9]683                        __attribute__((used)) volatile bool was_reset = false;
684
[d5cdbed]685                        with( proc->idle_wctx) {
[18f7858]686
[37a3aa23]687                                // Do we already have a pending read
688                                if(available(*ftr)) {
689                                        // There is no pending read, we need to add one
690                                        reset(*ftr);
691
692                                        iov.iov_base = rdbuf;
693                                        iov.iov_len  = sizeof(eventfd_t);
694                                        __kernel_read(proc, *ftr, iov, evfd );
[262fafd9]695                                        ftr->result = 0xDEADDEAD;
696                                        *((eventfd_t *)rdbuf) = 0xDEADDEADDEADDEAD;
697                                        was_reset = true;
[37a3aa23]698                                }
[18f7858]699                        }
700
[010636f]701                        if( !__atomic_load_n( &proc->do_terminate, __ATOMIC_SEQ_CST ) ) {
702                                __ioarbiter_flush( *proc->io.ctx );
[262fafd9]703                                proc->idle_wctx.sleep_time = rdtscl();
[010636f]704                                ioring_syscsll( *proc->io.ctx, 1, IORING_ENTER_GETEVENTS);
705                        }
[18f7858]706
[d5cdbed]707                        ready_schedule_lock();
[18f7858]708                        __cfa_do_drain( proc->io.ctx, proc->cltr );
[d5cdbed]709                        ready_schedule_unlock();
[262fafd9]710
711                        asm volatile ("" :: "m" (was_reset));
[18f7858]712                }
[6ddef36]713        #endif
[47746a2]714#endif
Note: See TracBrowser for help on using the repository browser.