source: libcfa/src/concurrency/io.cfa@ 06bdba4

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 06bdba4 was 7affcda, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fixed a bug where io wouldn't drain everything if the completion queue is filled

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