source: libcfa/src/concurrency/io/setup.cfa@ d5d3a90

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d5d3a90 was 3e2b9c9, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

More restructuring of translation units
Unclear if it improves compilation time.

  • Property mode set to 100644
File size: 15.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/setup.cfa --
8//
9// Author : Thierry Delisle
10// Created On : Fri Jul 31 16:25:51 2020
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
16#define __cforall_thread__
17#define _GNU_SOURCE /* See feature_test_macros(7) */
18
19#include "io/types.hfa"
20
21#if !defined(CFA_HAVE_LINUX_IO_URING_H)
22 void __kernel_io_startup() {
23 // Nothing to do without io_uring
24 }
25
26 void __kernel_io_shutdown() {
27 // Nothing to do without io_uring
28 }
29
30 void ?{}(io_context & this, struct cluster & cl) {}
31 void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {}
32
33 void ^?{}(io_context & this) {}
34 void ^?{}(io_context & this, bool cluster_context) {}
35
36#else
37 #include <errno.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <signal.h>
41 #include <unistd.h>
42
43 extern "C" {
44 #include <pthread.h>
45 #include <sys/epoll.h>
46 #include <sys/mman.h>
47 #include <sys/syscall.h>
48
49 #include <linux/io_uring.h>
50 }
51
52 #include "bitmanip.hfa"
53 #include "kernel_private.hfa"
54 #include "thread.hfa"
55
56 void ?{}(io_context_params & this) {
57 this.num_entries = 256;
58 this.num_ready = 256;
59 this.submit_aff = -1;
60 this.eager_submits = false;
61 this.poller_submits = false;
62 this.poll_submit = false;
63 this.poll_complete = false;
64 }
65
66 static void * __io_poller_slow( void * arg );
67
68 // Weirdly, some systems that do support io_uring don't actually define these
69 #ifdef __alpha__
70 /*
71 * alpha is the only exception, all other architectures
72 * have common numbers for new system calls.
73 */
74 #ifndef __NR_io_uring_setup
75 #define __NR_io_uring_setup 535
76 #endif
77 #ifndef __NR_io_uring_enter
78 #define __NR_io_uring_enter 536
79 #endif
80 #ifndef __NR_io_uring_register
81 #define __NR_io_uring_register 537
82 #endif
83 #else /* !__alpha__ */
84 #ifndef __NR_io_uring_setup
85 #define __NR_io_uring_setup 425
86 #endif
87 #ifndef __NR_io_uring_enter
88 #define __NR_io_uring_enter 426
89 #endif
90 #ifndef __NR_io_uring_register
91 #define __NR_io_uring_register 427
92 #endif
93 #endif
94
95//=============================================================================================
96// I/O Startup / Shutdown logic + Master Poller
97//=============================================================================================
98
99 // IO Master poller loop forward
100 static void * iopoll_loop( __attribute__((unused)) void * args );
101
102 static struct {
103 pthread_t thrd; // pthread handle to io poller thread
104 void * stack; // pthread stack for io poller thread
105 int epollfd; // file descriptor to the epoll instance
106 volatile bool run; // Whether or not to continue
107 } iopoll;
108
109 void __kernel_io_startup(void) {
110 __cfaabi_dbg_print_safe( "Kernel : Creating EPOLL instance\n" );
111
112 iopoll.epollfd = epoll_create1(0);
113 if (iopoll.epollfd == -1) {
114 abort( "internal error, epoll_create1\n");
115 }
116
117 __cfaabi_dbg_print_safe( "Kernel : Starting io poller thread\n" );
118
119 iopoll.run = true;
120 iopoll.stack = __create_pthread( &iopoll.thrd, iopoll_loop, 0p );
121 }
122
123 void __kernel_io_shutdown(void) {
124 // Notify the io poller thread of the shutdown
125 iopoll.run = false;
126 sigval val = { 1 };
127 pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
128
129 // Wait for the io poller thread to finish
130
131 pthread_join( iopoll.thrd, 0p );
132 free( iopoll.stack );
133
134 int ret = close(iopoll.epollfd);
135 if (ret == -1) {
136 abort( "internal error, close epoll\n");
137 }
138
139 // Io polling is now fully stopped
140
141 __cfaabi_dbg_print_safe( "Kernel : IO poller stopped\n" );
142 }
143
144 static void * iopoll_loop( __attribute__((unused)) void * args ) {
145 __processor_id_t id;
146 id.id = doregister(&id);
147 __cfaabi_dbg_print_safe( "Kernel : IO poller thread starting\n" );
148
149 // Block signals to control when they arrive
150 sigset_t mask;
151 sigfillset(&mask);
152 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
153 abort( "internal error, pthread_sigmask" );
154 }
155
156 sigdelset( &mask, SIGUSR1 );
157
158 // Create sufficient events
159 struct epoll_event events[10];
160 // Main loop
161 while( iopoll.run ) {
162 // Wait for events
163 int nfds = epoll_pwait( iopoll.epollfd, events, 10, -1, &mask );
164
165 // Check if an error occured
166 if (nfds == -1) {
167 if( errno == EINTR ) continue;
168 abort( "internal error, pthread_sigmask" );
169 }
170
171 for(i; nfds) {
172 $io_ctx_thread * io_ctx = ($io_ctx_thread *)(uintptr_t)events[i].data.u64;
173 /* paranoid */ verify( io_ctx );
174 __cfadbg_print_safe(io_core, "Kernel I/O : Unparking io poller %p\n", io_ctx);
175 #if !defined( __CFA_NO_STATISTICS__ )
176 kernelTLS.this_stats = io_ctx->self.curr_cluster->stats;
177 #endif
178 __post( io_ctx->sem, &id );
179 }
180 }
181
182 __cfaabi_dbg_print_safe( "Kernel : IO poller thread stopping\n" );
183 unregister(&id);
184 return 0p;
185 }
186
187//=============================================================================================
188// I/O Context Constrution/Destruction
189//=============================================================================================
190
191 void ?{}($io_ctx_thread & this, struct cluster & cl) { (this.self){ "IO Poller", cl }; }
192 void main( $io_ctx_thread & this );
193 static inline $thread * get_thread( $io_ctx_thread & this ) { return &this.self; }
194 void ^?{}( $io_ctx_thread & mutex this ) {}
195
196 static void __io_create ( __io_data & this, const io_context_params & params_in );
197 static void __io_destroy( __io_data & this );
198
199 void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {
200 (this.thrd){ cl };
201 this.thrd.ring = malloc();
202 __cfadbg_print_safe(io_core, "Kernel I/O : Creating ring for io_context %p\n", &this);
203 __io_create( *this.thrd.ring, params );
204
205 __cfadbg_print_safe(io_core, "Kernel I/O : Starting poller thread for io_context %p\n", &this);
206 this.thrd.done = false;
207 __thrd_start( this.thrd, main );
208
209 __cfadbg_print_safe(io_core, "Kernel I/O : io_context %p ready\n", &this);
210 }
211
212 void ?{}(io_context & this, struct cluster & cl) {
213 io_context_params params;
214 (this){ cl, params };
215 }
216
217 void ^?{}(io_context & this, bool cluster_context) {
218 __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %p\n", &this);
219
220 // Notify the thread of the shutdown
221 __atomic_store_n(&this.thrd.done, true, __ATOMIC_SEQ_CST);
222
223 // If this is an io_context within a cluster, things get trickier
224 $thread & thrd = this.thrd.self;
225 if( cluster_context ) {
226 cluster & cltr = *thrd.curr_cluster;
227 /* paranoid */ verify( cltr.nprocessors == 0 || &cltr == mainCluster );
228 /* paranoid */ verify( !ready_mutate_islocked() );
229
230 // We need to adjust the clean-up based on where the thread is
231 if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) {
232
233 ready_schedule_lock( (struct __processor_id_t *)active_processor() );
234
235 // This is the tricky case
236 // The thread was preempted and now it is on the ready queue
237 // The thread should be the last on the list
238 /* paranoid */ verify( thrd.link.next != 0p );
239
240 // Remove the thread from the ready queue of this cluster
241 __attribute__((unused)) bool removed = remove_head( &cltr, &thrd );
242 /* paranoid */ verify( removed );
243 thrd.link.next = 0p;
244 thrd.link.prev = 0p;
245 __cfaabi_dbg_debug_do( thrd.unpark_stale = true );
246
247 // Fixup the thread state
248 thrd.state = Blocked;
249 thrd.ticket = 0;
250 thrd.preempted = __NO_PREEMPTION;
251
252 ready_schedule_unlock( (struct __processor_id_t *)active_processor() );
253
254 // Pretend like the thread was blocked all along
255 }
256 // !!! This is not an else if !!!
257 if( thrd.state == Blocked ) {
258
259 // This is the "easy case"
260 // The thread is parked and can easily be moved to active cluster
261 verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster );
262 thrd.curr_cluster = active_cluster();
263
264 // unpark the fast io_poller
265 unpark( &thrd __cfaabi_dbg_ctx2 );
266 }
267 else {
268
269 // The thread is in a weird state
270 // I don't know what to do here
271 abort("io_context poller thread is in unexpected state, cannot clean-up correctly\n");
272 }
273 } else {
274 unpark( &thrd __cfaabi_dbg_ctx2 );
275 }
276
277 ^(this.thrd){};
278 __cfadbg_print_safe(io_core, "Kernel I/O : Stopped poller thread for io_context %p\n", &this);
279
280 __io_destroy( *this.thrd.ring );
281 __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %p\n", &this);
282
283 free(this.thrd.ring);
284 }
285
286 void ^?{}(io_context & this) {
287 ^(this){ false };
288 }
289
290 static void __io_create( __io_data & this, const io_context_params & params_in ) {
291 // Step 1 : call to setup
292 struct io_uring_params params;
293 memset(&params, 0, sizeof(params));
294 if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL;
295 if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL;
296
297 uint32_t nentries = params_in.num_entries;
298
299 int fd = syscall(__NR_io_uring_setup, nentries, &params );
300 if(fd < 0) {
301 abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
302 }
303
304 // Step 2 : mmap result
305 memset( &this, 0, sizeof(struct __io_data) );
306 struct __submition_data & sq = this.submit_q;
307 struct __completion_data & cq = this.completion_q;
308
309 // calculate the right ring size
310 sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) );
311 cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe));
312
313 // Requires features
314 #if defined(IORING_FEAT_SINGLE_MMAP)
315 // adjust the size according to the parameters
316 if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
317 cq.ring_sz = sq.ring_sz = max(cq.ring_sz, sq.ring_sz);
318 }
319 #endif
320
321 // mmap the Submit Queue into existence
322 sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
323 if (sq.ring_ptr == (void*)MAP_FAILED) {
324 abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
325 }
326
327 // Requires features
328 #if defined(IORING_FEAT_SINGLE_MMAP)
329 // mmap the Completion Queue into existence (may or may not be needed)
330 if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
331 cq.ring_ptr = sq.ring_ptr;
332 }
333 else
334 #endif
335 {
336 // We need multiple call to MMAP
337 cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
338 if (cq.ring_ptr == (void*)MAP_FAILED) {
339 munmap(sq.ring_ptr, sq.ring_sz);
340 abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
341 }
342 }
343
344 // mmap the submit queue entries
345 size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
346 sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
347 if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) {
348 munmap(sq.ring_ptr, sq.ring_sz);
349 if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz);
350 abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno));
351 }
352
353 // Get the pointers from the kernel to fill the structure
354 // submit queue
355 sq.head = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
356 sq.tail = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
357 sq.mask = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
358 sq.num = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
359 sq.flags = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
360 sq.dropped = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
361 sq.array = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
362 sq.prev_head = *sq.head;
363
364 {
365 const uint32_t num = *sq.num;
366 for( i; num ) {
367 sq.sqes[i].user_data = 0ul64;
368 }
369 }
370
371 (sq.lock){};
372 (sq.release_lock){};
373
374 if( params_in.poller_submits || params_in.eager_submits ) {
375 /* paranoid */ verify( is_pow2( params_in.num_ready ) || (params_in.num_ready < 8) );
376 sq.ready_cnt = max( params_in.num_ready, 8 );
377 sq.ready = alloc_align( 64, sq.ready_cnt );
378 for(i; sq.ready_cnt) {
379 sq.ready[i] = -1ul32;
380 }
381 }
382 else {
383 sq.ready_cnt = 0;
384 sq.ready = 0p;
385 }
386
387 // completion queue
388 cq.head = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.head);
389 cq.tail = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail);
390 cq.mask = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask);
391 cq.num = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries);
392 cq.overflow = ( uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow);
393 cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes);
394
395 // some paranoid checks
396 /* paranoid */ verifyf( (*cq.mask) == ((*cq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*cq.num) - 1ul32, *cq.num, *cq.mask );
397 /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num );
398 /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head );
399 /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail );
400
401 /* paranoid */ verifyf( (*sq.mask) == ((*sq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*sq.num) - 1ul32, *sq.num, *sq.mask );
402 /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
403 /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head );
404 /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail );
405
406 // Update the global ring info
407 this.ring_flags = params.flags;
408 this.fd = fd;
409 this.eager_submits = params_in.eager_submits;
410 this.poller_submits = params_in.poller_submits;
411 }
412
413 static void __io_destroy( __io_data & this ) {
414 // Shutdown the io rings
415 struct __submition_data & sq = this.submit_q;
416 struct __completion_data & cq = this.completion_q;
417
418 // unmap the submit queue entries
419 munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
420
421 // unmap the Submit Queue ring
422 munmap(sq.ring_ptr, sq.ring_sz);
423
424 // unmap the Completion Queue ring, if it is different
425 if (cq.ring_ptr != sq.ring_ptr) {
426 munmap(cq.ring_ptr, cq.ring_sz);
427 }
428
429 // close the file descriptor
430 close(this.fd);
431
432 free( this.submit_q.ready ); // Maybe null, doesn't matter
433 }
434
435//=============================================================================================
436// I/O Context Sleep
437//=============================================================================================
438
439 void __ioctx_register($io_ctx_thread & ctx, struct epoll_event & ev) {
440 ev.events = EPOLLIN | EPOLLONESHOT;
441 ev.data.u64 = (uint64_t)&ctx;
442 int ret = epoll_ctl(iopoll.epollfd, EPOLL_CTL_ADD, ctx.ring->fd, &ev);
443 if (ret < 0) {
444 abort( "KERNEL ERROR: EPOLL ADD - (%d) %s\n", (int)errno, strerror(errno) );
445 }
446 }
447
448 void __ioctx_prepare_block($io_ctx_thread & ctx, struct epoll_event & ev) {
449 int ret = epoll_ctl(iopoll.epollfd, EPOLL_CTL_MOD, ctx.ring->fd, &ev);
450 if (ret < 0) {
451 abort( "KERNEL ERROR: EPOLL REARM - (%d) %s\n", (int)errno, strerror(errno) );
452 }
453 }
454
455//=============================================================================================
456// I/O Context Misc Setup
457//=============================================================================================
458 void register_fixed_files( io_context & ctx, int * files, unsigned count ) {
459 int ret = syscall( __NR_io_uring_register, ctx.thrd.ring->fd, IORING_REGISTER_FILES, files, count );
460 if( ret < 0 ) {
461 abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
462 }
463
464 __cfadbg_print_safe( io_core, "Kernel I/O : Performed io_register for %p, returned %d\n", active_thread(), ret );
465 }
466
467 void register_fixed_files( cluster & cltr, int * files, unsigned count ) {
468 for(i; cltr.io.cnt) {
469 register_fixed_files( cltr.io.ctxs[i], files, count );
470 }
471 }
472#endif
Note: See TracBrowser for help on using the repository browser.