1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // kernel.c --
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Tue Jan 17 12:27:26 2017
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Mon Aug 31 07:08:20 2020
|
---|
13 | // Update Count : 71
|
---|
14 | //
|
---|
15 |
|
---|
16 | #define __cforall_thread__
|
---|
17 | #define _GNU_SOURCE
|
---|
18 |
|
---|
19 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
|
---|
20 |
|
---|
21 | //C Includes
|
---|
22 | #include <errno.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <string.h>
|
---|
25 | #include <signal.h>
|
---|
26 | #include <unistd.h>
|
---|
27 | extern "C" {
|
---|
28 | #include <sys/eventfd.h>
|
---|
29 | #include <sys/uio.h>
|
---|
30 | }
|
---|
31 |
|
---|
32 | //CFA Includes
|
---|
33 | #include "kernel_private.hfa"
|
---|
34 | #include "preemption.hfa"
|
---|
35 | #include "strstream.hfa"
|
---|
36 | #include "device/cpu.hfa"
|
---|
37 | #include "io/types.hfa"
|
---|
38 |
|
---|
39 | //Private includes
|
---|
40 | #define __CFA_INVOKE_PRIVATE__
|
---|
41 | #include "invoke.h"
|
---|
42 |
|
---|
43 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
44 | #define __STATS( ...) __VA_ARGS__
|
---|
45 | #else
|
---|
46 | #define __STATS( ...)
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | //-----------------------------------------------------------------------------
|
---|
50 | // Some assembly required
|
---|
51 | #if defined( __i386 )
|
---|
52 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
|
---|
53 | // fcw : X87 FPU control word (preserved across function calls)
|
---|
54 | #define __x87_store \
|
---|
55 | uint32_t __mxcr; \
|
---|
56 | uint16_t __fcw; \
|
---|
57 | __asm__ volatile ( \
|
---|
58 | "stmxcsr %0\n" \
|
---|
59 | "fnstcw %1\n" \
|
---|
60 | : "=m" (__mxcr),\
|
---|
61 | "=m" (__fcw) \
|
---|
62 | )
|
---|
63 |
|
---|
64 | #define __x87_load \
|
---|
65 | __asm__ volatile ( \
|
---|
66 | "fldcw %1\n" \
|
---|
67 | "ldmxcsr %0\n" \
|
---|
68 | ::"m" (__mxcr),\
|
---|
69 | "m" (__fcw) \
|
---|
70 | )
|
---|
71 |
|
---|
72 | #elif defined( __x86_64 )
|
---|
73 | #define __x87_store \
|
---|
74 | uint32_t __mxcr; \
|
---|
75 | uint16_t __fcw; \
|
---|
76 | __asm__ volatile ( \
|
---|
77 | "stmxcsr %0\n" \
|
---|
78 | "fnstcw %1\n" \
|
---|
79 | : "=m" (__mxcr),\
|
---|
80 | "=m" (__fcw) \
|
---|
81 | )
|
---|
82 |
|
---|
83 | #define __x87_load \
|
---|
84 | __asm__ volatile ( \
|
---|
85 | "fldcw %1\n" \
|
---|
86 | "ldmxcsr %0\n" \
|
---|
87 | :: "m" (__mxcr),\
|
---|
88 | "m" (__fcw) \
|
---|
89 | )
|
---|
90 |
|
---|
91 | #elif defined( __arm__ )
|
---|
92 | #define __x87_store
|
---|
93 | #define __x87_load
|
---|
94 |
|
---|
95 | #elif defined( __aarch64__ )
|
---|
96 | #define __x87_store \
|
---|
97 | uint32_t __fpcntl[2]; \
|
---|
98 | __asm__ volatile ( \
|
---|
99 | "mrs x9, FPCR\n" \
|
---|
100 | "mrs x10, FPSR\n" \
|
---|
101 | "stp x9, x10, %0\n" \
|
---|
102 | : "=m" (__fpcntl) : : "x9", "x10" \
|
---|
103 | )
|
---|
104 |
|
---|
105 | #define __x87_load \
|
---|
106 | __asm__ volatile ( \
|
---|
107 | "ldp x9, x10, %0\n" \
|
---|
108 | "msr FPSR, x10\n" \
|
---|
109 | "msr FPCR, x9\n" \
|
---|
110 | : "=m" (__fpcntl) : : "x9", "x10" \
|
---|
111 | )
|
---|
112 |
|
---|
113 | #else
|
---|
114 | #error unsupported hardware architecture
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | extern thread$ * mainThread;
|
---|
118 | extern processor * mainProcessor;
|
---|
119 |
|
---|
120 | //-----------------------------------------------------------------------------
|
---|
121 | // Kernel Scheduling logic
|
---|
122 | static thread$ * __next_thread(cluster * this);
|
---|
123 | static thread$ * __next_thread_slow(cluster * this);
|
---|
124 | static inline bool __must_unpark( thread$ * thrd ) __attribute((nonnull(1)));
|
---|
125 | static void __run_thread(processor * this, thread$ * dst);
|
---|
126 | static void __wake_one(cluster * cltr);
|
---|
127 |
|
---|
128 | static void idle_sleep(processor * proc, io_future_t & future, iovec & iov);
|
---|
129 | static bool mark_idle (__cluster_proc_list & idles, processor & proc);
|
---|
130 | static void mark_awake(__cluster_proc_list & idles, processor & proc);
|
---|
131 |
|
---|
132 | extern void __cfa_io_start( processor * );
|
---|
133 | extern bool __cfa_io_drain( processor * );
|
---|
134 | extern bool __cfa_io_flush( processor *, int min_comp );
|
---|
135 | extern void __cfa_io_stop ( processor * );
|
---|
136 | static inline bool __maybe_io_drain( processor * );
|
---|
137 |
|
---|
138 | #if defined(CFA_WITH_IO_URING_IDLE)
|
---|
139 | extern bool __kernel_read(processor * proc, io_future_t & future, iovec &, int fd);
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | extern void __disable_interrupts_hard();
|
---|
143 | extern void __enable_interrupts_hard();
|
---|
144 |
|
---|
145 | static inline void __disable_interrupts_checked() {
|
---|
146 | /* paranoid */ verify( __preemption_enabled() );
|
---|
147 | disable_interrupts();
|
---|
148 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
149 | }
|
---|
150 |
|
---|
151 | static inline void __enable_interrupts_checked( bool poll = true ) {
|
---|
152 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
153 | enable_interrupts( poll );
|
---|
154 | /* paranoid */ verify( __preemption_enabled() );
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | //=============================================================================================
|
---|
159 | // Kernel Scheduling logic
|
---|
160 | //=============================================================================================
|
---|
161 | //Main of the processor contexts
|
---|
162 | void main(processorCtx_t & runner) {
|
---|
163 | // Because of a bug, we couldn't initialized the seed on construction
|
---|
164 | // Do it here
|
---|
165 | __cfaabi_tls.rand_seed ^= rdtscl();
|
---|
166 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&runner);
|
---|
167 | __tls_rand_advance_bck();
|
---|
168 |
|
---|
169 | processor * this = runner.proc;
|
---|
170 | verify(this);
|
---|
171 |
|
---|
172 | io_future_t future; // used for idle sleep when io_uring is present
|
---|
173 | future.self.ptr = 1p; // mark it as already fulfilled so we know if there is a pending request or not
|
---|
174 | eventfd_t idle_val;
|
---|
175 | iovec idle_iovec = { &idle_val, sizeof(idle_val) };
|
---|
176 |
|
---|
177 | __cfa_io_start( this );
|
---|
178 |
|
---|
179 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
|
---|
180 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
181 | if( this->print_halts ) {
|
---|
182 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->unique_id, this->name, (void*)this);
|
---|
183 | }
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | {
|
---|
187 | // Setup preemption data
|
---|
188 | preemption_scope scope = { this };
|
---|
189 |
|
---|
190 | // if we need to run some special setup, now is the time to do it.
|
---|
191 | if(this->init.thrd) {
|
---|
192 | this->init.thrd->curr_cluster = this->cltr;
|
---|
193 | __run_thread(this, this->init.thrd);
|
---|
194 | }
|
---|
195 |
|
---|
196 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
|
---|
197 |
|
---|
198 | thread$ * readyThread = 0p;
|
---|
199 | MAIN_LOOP:
|
---|
200 | for() {
|
---|
201 | #define OLD_MAIN 1
|
---|
202 | #if OLD_MAIN
|
---|
203 | // Check if there is pending io
|
---|
204 | __maybe_io_drain( this );
|
---|
205 |
|
---|
206 | // Try to get the next thread
|
---|
207 | readyThread = __next_thread( this->cltr );
|
---|
208 |
|
---|
209 | if( !readyThread ) {
|
---|
210 | __cfa_io_flush( this, 0 );
|
---|
211 |
|
---|
212 | readyThread = __next_thread_slow( this->cltr );
|
---|
213 | }
|
---|
214 |
|
---|
215 | HALT:
|
---|
216 | if( !readyThread ) {
|
---|
217 | // Don't block if we are done
|
---|
218 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
219 |
|
---|
220 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
221 | __tls_stats()->ready.sleep.halts++;
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | // Push self to idle stack
|
---|
225 | if(!mark_idle(this->cltr->procs, * this)) continue MAIN_LOOP;
|
---|
226 |
|
---|
227 | // Confirm the ready-queue is empty
|
---|
228 | readyThread = __next_thread_slow( this->cltr );
|
---|
229 | if( readyThread ) {
|
---|
230 | // A thread was found, cancel the halt
|
---|
231 | mark_awake(this->cltr->procs, * this);
|
---|
232 |
|
---|
233 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
234 | __tls_stats()->ready.sleep.cancels++;
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | // continue the mai loop
|
---|
238 | break HALT;
|
---|
239 | }
|
---|
240 |
|
---|
241 | idle_sleep( this, future, idle_iovec );
|
---|
242 |
|
---|
243 | // We were woken up, remove self from idle
|
---|
244 | mark_awake(this->cltr->procs, * this);
|
---|
245 |
|
---|
246 | // DON'T just proceed, start looking again
|
---|
247 | continue MAIN_LOOP;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* paranoid */ verify( readyThread );
|
---|
251 |
|
---|
252 | // Reset io dirty bit
|
---|
253 | this->io.dirty = false;
|
---|
254 |
|
---|
255 | // We found a thread run it
|
---|
256 | __run_thread(this, readyThread);
|
---|
257 |
|
---|
258 | // Are we done?
|
---|
259 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
260 |
|
---|
261 | if(this->io.pending && !this->io.dirty) {
|
---|
262 | __cfa_io_flush( this, 0 );
|
---|
263 | }
|
---|
264 |
|
---|
265 | #else
|
---|
266 | #warning new kernel loop
|
---|
267 | SEARCH: {
|
---|
268 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
269 |
|
---|
270 | // First, lock the scheduler since we are searching for a thread
|
---|
271 | ready_schedule_lock();
|
---|
272 |
|
---|
273 | // Try to get the next thread
|
---|
274 | readyThread = pop_fast( this->cltr );
|
---|
275 | if(readyThread) { ready_schedule_unlock(); break SEARCH; }
|
---|
276 |
|
---|
277 | // If we can't find a thread, might as well flush any outstanding I/O
|
---|
278 | if(this->io.pending) { __cfa_io_flush( this, 0 ); }
|
---|
279 |
|
---|
280 | // Spin a little on I/O, just in case
|
---|
281 | for(5) {
|
---|
282 | __maybe_io_drain( this );
|
---|
283 | readyThread = pop_fast( this->cltr );
|
---|
284 | if(readyThread) { ready_schedule_unlock(); break SEARCH; }
|
---|
285 | }
|
---|
286 |
|
---|
287 | // no luck, try stealing a few times
|
---|
288 | for(5) {
|
---|
289 | if( __maybe_io_drain( this ) ) {
|
---|
290 | readyThread = pop_fast( this->cltr );
|
---|
291 | } else {
|
---|
292 | readyThread = pop_slow( this->cltr );
|
---|
293 | }
|
---|
294 | if(readyThread) { ready_schedule_unlock(); break SEARCH; }
|
---|
295 | }
|
---|
296 |
|
---|
297 | // still no luck, search for a thread
|
---|
298 | readyThread = pop_search( this->cltr );
|
---|
299 | if(readyThread) { ready_schedule_unlock(); break SEARCH; }
|
---|
300 |
|
---|
301 | // Don't block if we are done
|
---|
302 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) {
|
---|
303 | ready_schedule_unlock();
|
---|
304 | break MAIN_LOOP;
|
---|
305 | }
|
---|
306 |
|
---|
307 | __STATS( __tls_stats()->ready.sleep.halts++; )
|
---|
308 |
|
---|
309 | // Push self to idle stack
|
---|
310 | ready_schedule_unlock();
|
---|
311 | if(!mark_idle(this->cltr->procs, * this)) goto SEARCH;
|
---|
312 | ready_schedule_lock();
|
---|
313 |
|
---|
314 | // Confirm the ready-queue is empty
|
---|
315 | __maybe_io_drain( this );
|
---|
316 | readyThread = pop_search( this->cltr );
|
---|
317 | ready_schedule_unlock();
|
---|
318 |
|
---|
319 | if( readyThread ) {
|
---|
320 | // A thread was found, cancel the halt
|
---|
321 | mark_awake(this->cltr->procs, * this);
|
---|
322 |
|
---|
323 | __STATS( __tls_stats()->ready.sleep.cancels++; )
|
---|
324 |
|
---|
325 | // continue the main loop
|
---|
326 | break SEARCH;
|
---|
327 | }
|
---|
328 |
|
---|
329 | __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl()); )
|
---|
330 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle_fd);
|
---|
331 |
|
---|
332 | {
|
---|
333 | eventfd_t val;
|
---|
334 | ssize_t ret = read( this->idle_fd, &val, sizeof(val) );
|
---|
335 | if(ret < 0) {
|
---|
336 | switch((int)errno) {
|
---|
337 | case EAGAIN:
|
---|
338 | #if EAGAIN != EWOULDBLOCK
|
---|
339 | case EWOULDBLOCK:
|
---|
340 | #endif
|
---|
341 | case EINTR:
|
---|
342 | // No need to do anything special here, just assume it's a legitimate wake-up
|
---|
343 | break;
|
---|
344 | default:
|
---|
345 | abort( "KERNEL : internal error, read failure on idle eventfd, error(%d) %s.", (int)errno, strerror( (int)errno ) );
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl()); )
|
---|
351 |
|
---|
352 | // We were woken up, remove self from idle
|
---|
353 | mark_awake(this->cltr->procs, * this);
|
---|
354 |
|
---|
355 | // DON'T just proceed, start looking again
|
---|
356 | continue MAIN_LOOP;
|
---|
357 | }
|
---|
358 |
|
---|
359 | RUN_THREAD:
|
---|
360 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
361 | /* paranoid */ verify( readyThread );
|
---|
362 |
|
---|
363 | // Reset io dirty bit
|
---|
364 | this->io.dirty = false;
|
---|
365 |
|
---|
366 | // We found a thread run it
|
---|
367 | __run_thread(this, readyThread);
|
---|
368 |
|
---|
369 | // Are we done?
|
---|
370 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
371 |
|
---|
372 | if(this->io.pending && !this->io.dirty) {
|
---|
373 | __cfa_io_flush( this, 0 );
|
---|
374 | }
|
---|
375 |
|
---|
376 | ready_schedule_lock();
|
---|
377 | __maybe_io_drain( this );
|
---|
378 | ready_schedule_unlock();
|
---|
379 | #endif
|
---|
380 | }
|
---|
381 |
|
---|
382 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
|
---|
383 | }
|
---|
384 |
|
---|
385 | for(int i = 0; !available(future); i++) {
|
---|
386 | if(i > 1000) __cfaabi_dbg_write( "ERROR: kernel has bin spinning on a flush after exit loop.\n", 60);
|
---|
387 | __cfa_io_flush( this, 1 );
|
---|
388 | }
|
---|
389 |
|
---|
390 | __cfa_io_stop( this );
|
---|
391 |
|
---|
392 | post( this->terminated );
|
---|
393 |
|
---|
394 | if(this == mainProcessor) {
|
---|
395 | // HACK : the coroutine context switch expects this_thread to be set
|
---|
396 | // and it make sense for it to be set in all other cases except here
|
---|
397 | // fake it
|
---|
398 | __cfaabi_tls.this_thread = mainThread;
|
---|
399 | }
|
---|
400 |
|
---|
401 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
|
---|
402 | }
|
---|
403 |
|
---|
404 | static int * __volatile_errno() __attribute__((noinline));
|
---|
405 | static int * __volatile_errno() { asm(""); return &errno; }
|
---|
406 |
|
---|
407 | // KERNEL ONLY
|
---|
408 | // runThread runs a thread by context switching
|
---|
409 | // from the processor coroutine to the target thread
|
---|
410 | static void __run_thread(processor * this, thread$ * thrd_dst) {
|
---|
411 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
412 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted);
|
---|
413 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
|
---|
414 | __builtin_prefetch( thrd_dst->context.SP );
|
---|
415 |
|
---|
416 | __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name);
|
---|
417 |
|
---|
418 | coroutine$ * proc_cor = get_coroutine(this->runner);
|
---|
419 |
|
---|
420 | // set state of processor coroutine to inactive
|
---|
421 | verify(proc_cor->state == Active);
|
---|
422 | proc_cor->state = Blocked;
|
---|
423 |
|
---|
424 | // Actually run the thread
|
---|
425 | RUNNING: while(true) {
|
---|
426 | thrd_dst->preempted = __NO_PREEMPTION;
|
---|
427 | thrd_dst->state = Active;
|
---|
428 |
|
---|
429 | // Update global state
|
---|
430 | kernelTLS().this_thread = thrd_dst;
|
---|
431 |
|
---|
432 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
433 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
|
---|
434 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
|
---|
435 | /* paranoid */ verify( thrd_dst->context.SP );
|
---|
436 | /* paranoid */ verify( thrd_dst->state != Halted );
|
---|
437 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor
|
---|
438 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor
|
---|
439 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
|
---|
440 |
|
---|
441 |
|
---|
442 |
|
---|
443 | // set context switch to the thread that the processor is executing
|
---|
444 | __cfactx_switch( &proc_cor->context, &thrd_dst->context );
|
---|
445 | // when __cfactx_switch returns we are back in the processor coroutine
|
---|
446 |
|
---|
447 |
|
---|
448 |
|
---|
449 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
|
---|
450 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst );
|
---|
451 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst );
|
---|
452 | /* paranoid */ verify( thrd_dst->context.SP );
|
---|
453 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
|
---|
454 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
|
---|
455 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
456 |
|
---|
457 | // Reset global state
|
---|
458 | kernelTLS().this_thread = 0p;
|
---|
459 |
|
---|
460 | // We just finished running a thread, there are a few things that could have happened.
|
---|
461 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
|
---|
462 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it.
|
---|
463 | // 4 - Preempted
|
---|
464 | // In case 1, we may have won a race so we can't write to the state again.
|
---|
465 | // In case 2, we lost the race so we now own the thread.
|
---|
466 |
|
---|
467 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
|
---|
468 | // The thread was preempted, reschedule it and reset the flag
|
---|
469 | schedule_thread$( thrd_dst, UNPARK_LOCAL );
|
---|
470 | break RUNNING;
|
---|
471 | }
|
---|
472 |
|
---|
473 | if(unlikely(thrd_dst->state == Halting)) {
|
---|
474 | // The thread has halted, it should never be scheduled/run again
|
---|
475 | // finish the thread
|
---|
476 | __thread_finish( thrd_dst );
|
---|
477 | break RUNNING;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /* paranoid */ verify( thrd_dst->state == Active );
|
---|
481 | thrd_dst->state = Blocked;
|
---|
482 |
|
---|
483 | // set state of processor coroutine to active and the thread to inactive
|
---|
484 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
|
---|
485 | switch(old_ticket) {
|
---|
486 | case TICKET_RUNNING:
|
---|
487 | // This is case 1, the regular case, nothing more is needed
|
---|
488 | break RUNNING;
|
---|
489 | case TICKET_UNBLOCK:
|
---|
490 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
491 | __tls_stats()->ready.threads.threads++;
|
---|
492 | #endif
|
---|
493 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking
|
---|
494 | // In this case, just run it again.
|
---|
495 | continue RUNNING;
|
---|
496 | default:
|
---|
497 | // This makes no sense, something is wrong abort
|
---|
498 | abort();
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | // Just before returning to the processor, set the processor coroutine to active
|
---|
503 | proc_cor->state = Active;
|
---|
504 |
|
---|
505 | __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst);
|
---|
506 |
|
---|
507 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
508 | __tls_stats()->ready.threads.threads--;
|
---|
509 | #endif
|
---|
510 |
|
---|
511 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
512 | }
|
---|
513 |
|
---|
514 | // KERNEL_ONLY
|
---|
515 | void returnToKernel() {
|
---|
516 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
517 | coroutine$ * proc_cor = get_coroutine(kernelTLS().this_processor->runner);
|
---|
518 | thread$ * thrd_src = kernelTLS().this_thread;
|
---|
519 |
|
---|
520 | __STATS( thrd_src->last_proc = kernelTLS().this_processor; )
|
---|
521 |
|
---|
522 | // Run the thread on this processor
|
---|
523 | {
|
---|
524 | int local_errno = *__volatile_errno();
|
---|
525 | #if defined( __i386 ) || defined( __x86_64 )
|
---|
526 | __x87_store;
|
---|
527 | #endif
|
---|
528 | /* paranoid */ verify( proc_cor->context.SP );
|
---|
529 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary );
|
---|
530 | __cfactx_switch( &thrd_src->context, &proc_cor->context );
|
---|
531 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary );
|
---|
532 | #if defined( __i386 ) || defined( __x86_64 )
|
---|
533 | __x87_load;
|
---|
534 | #endif
|
---|
535 | *__volatile_errno() = local_errno;
|
---|
536 | }
|
---|
537 |
|
---|
538 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
539 | /* paranoid */ verify( thrd_src->last_proc != 0p );
|
---|
540 | if(thrd_src->last_proc != kernelTLS().this_processor) {
|
---|
541 | __tls_stats()->ready.threads.migration++;
|
---|
542 | }
|
---|
543 | #endif
|
---|
544 |
|
---|
545 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
546 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ) || thrd_src->corctx_flag, "ERROR : Returning thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_src );
|
---|
547 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit) || thrd_src->corctx_flag, "ERROR : Returning thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_src );
|
---|
548 | }
|
---|
549 |
|
---|
550 | //-----------------------------------------------------------------------------
|
---|
551 | // Scheduler routines
|
---|
552 | // KERNEL ONLY
|
---|
553 | static void __schedule_thread( thread$ * thrd, unpark_hint hint ) {
|
---|
554 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
555 | /* paranoid */ verify( ready_schedule_islocked());
|
---|
556 | /* paranoid */ verify( thrd );
|
---|
557 | /* paranoid */ verify( thrd->state != Halted );
|
---|
558 | /* paranoid */ verify( thrd->curr_cluster );
|
---|
559 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
|
---|
560 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
|
---|
561 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
---|
562 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
|
---|
563 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
---|
564 | /* paranoid */ #endif
|
---|
565 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
|
---|
566 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary );
|
---|
567 |
|
---|
568 | const bool local = thrd->state != Start;
|
---|
569 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
|
---|
570 |
|
---|
571 | // Dereference the thread now because once we push it, there is not guaranteed it's still valid.
|
---|
572 | struct cluster * cl = thrd->curr_cluster;
|
---|
573 | __STATS(bool outside = hint == UNPARK_LOCAL && thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; )
|
---|
574 |
|
---|
575 | // push the thread to the cluster ready-queue
|
---|
576 | push( cl, thrd, hint );
|
---|
577 |
|
---|
578 | // variable thrd is no longer safe to use
|
---|
579 | thrd = 0xdeaddeaddeaddeadp;
|
---|
580 |
|
---|
581 | // wake the cluster using the save variable.
|
---|
582 | __wake_one( cl );
|
---|
583 |
|
---|
584 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
585 | if( kernelTLS().this_stats ) {
|
---|
586 | __tls_stats()->ready.threads.threads++;
|
---|
587 | if(outside) {
|
---|
588 | __tls_stats()->ready.threads.extunpark++;
|
---|
589 | }
|
---|
590 | }
|
---|
591 | else {
|
---|
592 | __atomic_fetch_add(&cl->stats->ready.threads.threads, 1, __ATOMIC_RELAXED);
|
---|
593 | __atomic_fetch_add(&cl->stats->ready.threads.extunpark, 1, __ATOMIC_RELAXED);
|
---|
594 | }
|
---|
595 | #endif
|
---|
596 |
|
---|
597 | /* paranoid */ verify( ready_schedule_islocked());
|
---|
598 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
599 | }
|
---|
600 |
|
---|
601 | void schedule_thread$( thread$ * thrd, unpark_hint hint ) {
|
---|
602 | ready_schedule_lock();
|
---|
603 | __schedule_thread( thrd, hint );
|
---|
604 | ready_schedule_unlock();
|
---|
605 | }
|
---|
606 |
|
---|
607 | // KERNEL ONLY
|
---|
608 | static inline thread$ * __next_thread(cluster * this) with( *this ) {
|
---|
609 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
610 |
|
---|
611 | ready_schedule_lock();
|
---|
612 | thread$ * thrd = pop_fast( this );
|
---|
613 | ready_schedule_unlock();
|
---|
614 |
|
---|
615 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
616 | return thrd;
|
---|
617 | }
|
---|
618 |
|
---|
619 | // KERNEL ONLY
|
---|
620 | static inline thread$ * __next_thread_slow(cluster * this) with( *this ) {
|
---|
621 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
622 |
|
---|
623 | ready_schedule_lock();
|
---|
624 | thread$ * thrd;
|
---|
625 | for(25) {
|
---|
626 | thrd = pop_slow( this );
|
---|
627 | if(thrd) goto RET;
|
---|
628 | }
|
---|
629 | thrd = pop_search( this );
|
---|
630 |
|
---|
631 | RET:
|
---|
632 | ready_schedule_unlock();
|
---|
633 |
|
---|
634 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
635 | return thrd;
|
---|
636 | }
|
---|
637 |
|
---|
638 | static inline bool __must_unpark( thread$ * thrd ) {
|
---|
639 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
|
---|
640 | switch(old_ticket) {
|
---|
641 | case TICKET_RUNNING:
|
---|
642 | // Wake won the race, the thread will reschedule/rerun itself
|
---|
643 | return false;
|
---|
644 | case TICKET_BLOCKED:
|
---|
645 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
|
---|
646 | /* paranoid */ verify( thrd->state == Blocked );
|
---|
647 | return true;
|
---|
648 | default:
|
---|
649 | // This makes no sense, something is wrong abort
|
---|
650 | abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name);
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | void __kernel_unpark( thread$ * thrd, unpark_hint hint ) {
|
---|
655 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
656 | /* paranoid */ verify( ready_schedule_islocked());
|
---|
657 |
|
---|
658 | if( !thrd ) return;
|
---|
659 |
|
---|
660 | if(__must_unpark(thrd)) {
|
---|
661 | // Wake lost the race,
|
---|
662 | __schedule_thread( thrd, hint );
|
---|
663 | }
|
---|
664 |
|
---|
665 | /* paranoid */ verify( ready_schedule_islocked());
|
---|
666 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
667 | }
|
---|
668 |
|
---|
669 | void unpark( thread$ * thrd, unpark_hint hint ) {
|
---|
670 | if( !thrd ) return;
|
---|
671 |
|
---|
672 | if(__must_unpark(thrd)) {
|
---|
673 | disable_interrupts();
|
---|
674 | // Wake lost the race,
|
---|
675 | schedule_thread$( thrd, hint );
|
---|
676 | enable_interrupts(false);
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | void park( void ) {
|
---|
681 | __disable_interrupts_checked();
|
---|
682 | /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION );
|
---|
683 | returnToKernel();
|
---|
684 | __enable_interrupts_checked();
|
---|
685 |
|
---|
686 | }
|
---|
687 |
|
---|
688 | extern "C" {
|
---|
689 | // Leave the thread monitor
|
---|
690 | // last routine called by a thread.
|
---|
691 | // Should never return
|
---|
692 | void __cfactx_thrd_leave() {
|
---|
693 | thread$ * thrd = active_thread();
|
---|
694 | monitor$ * this = &thrd->self_mon;
|
---|
695 |
|
---|
696 | // Lock the monitor now
|
---|
697 | lock( this->lock __cfaabi_dbg_ctx2 );
|
---|
698 |
|
---|
699 | disable_interrupts();
|
---|
700 |
|
---|
701 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
702 | /* paranoid */ verify( thrd->state == Active );
|
---|
703 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary );
|
---|
704 | /* paranoid */ verify( kernelTLS().this_thread == thrd );
|
---|
705 | /* paranoid */ verify( thrd->context.SP );
|
---|
706 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : thread$ %p has been corrupted.\n StackPointer too large.\n", thrd );
|
---|
707 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : thread$ %p has been corrupted.\n StackPointer too small.\n", thrd );
|
---|
708 |
|
---|
709 | thrd->state = Halting;
|
---|
710 | if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
|
---|
711 | if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); }
|
---|
712 | if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
|
---|
713 |
|
---|
714 | // Leave the thread
|
---|
715 | returnToKernel();
|
---|
716 |
|
---|
717 | // Control flow should never reach here!
|
---|
718 | abort();
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | // KERNEL ONLY
|
---|
723 | bool force_yield( __Preemption_Reason reason ) {
|
---|
724 | __disable_interrupts_checked();
|
---|
725 | thread$ * thrd = kernelTLS().this_thread;
|
---|
726 | /* paranoid */ verify(thrd->state == Active);
|
---|
727 |
|
---|
728 | // SKULLDUGGERY: It is possible that we are preempting this thread just before
|
---|
729 | // it was going to park itself. If that is the case and it is already using the
|
---|
730 | // intrusive fields then we can't use them to preempt the thread
|
---|
731 | // If that is the case, abandon the preemption.
|
---|
732 | bool preempted = false;
|
---|
733 | if(thrd->link.next == 0p) {
|
---|
734 | preempted = true;
|
---|
735 | thrd->preempted = reason;
|
---|
736 | returnToKernel();
|
---|
737 | }
|
---|
738 | __enable_interrupts_checked( false );
|
---|
739 | return preempted;
|
---|
740 | }
|
---|
741 |
|
---|
742 | //=============================================================================================
|
---|
743 | // Kernel Idle Sleep
|
---|
744 | //=============================================================================================
|
---|
745 | // Wake a thread from the front if there are any
|
---|
746 | static void __wake_one(cluster * this) {
|
---|
747 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
748 | /* paranoid */ verify( ready_schedule_islocked() );
|
---|
749 |
|
---|
750 | // Check if there is a sleeping processor
|
---|
751 | int fd = __atomic_load_n(&this->procs.fd, __ATOMIC_SEQ_CST);
|
---|
752 |
|
---|
753 | // If no one is sleeping, we are done
|
---|
754 | if( fd == 0 ) return;
|
---|
755 |
|
---|
756 | // We found a processor, wake it up
|
---|
757 | eventfd_t val;
|
---|
758 | val = 1;
|
---|
759 | eventfd_write( fd, val );
|
---|
760 |
|
---|
761 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
762 | if( kernelTLS().this_stats ) {
|
---|
763 | __tls_stats()->ready.sleep.wakes++;
|
---|
764 | }
|
---|
765 | else {
|
---|
766 | __atomic_fetch_add(&this->stats->ready.sleep.wakes, 1, __ATOMIC_RELAXED);
|
---|
767 | }
|
---|
768 | #endif
|
---|
769 |
|
---|
770 | /* paranoid */ verify( ready_schedule_islocked() );
|
---|
771 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
772 |
|
---|
773 | return;
|
---|
774 | }
|
---|
775 |
|
---|
776 | // Unconditionnaly wake a thread
|
---|
777 | void __wake_proc(processor * this) {
|
---|
778 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
|
---|
779 |
|
---|
780 | __disable_interrupts_checked();
|
---|
781 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
782 | eventfd_t val;
|
---|
783 | val = 1;
|
---|
784 | eventfd_write( this->idle_fd, val );
|
---|
785 | __enable_interrupts_checked();
|
---|
786 | }
|
---|
787 |
|
---|
788 | static void idle_sleep(processor * this, io_future_t & future, iovec & iov) {
|
---|
789 | #if !defined(CFA_WITH_IO_URING_IDLE)
|
---|
790 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
791 | if(this->print_halts) {
|
---|
792 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl());
|
---|
793 | }
|
---|
794 | #endif
|
---|
795 |
|
---|
796 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle_fd);
|
---|
797 |
|
---|
798 | {
|
---|
799 | eventfd_t val;
|
---|
800 | ssize_t ret = read( this->idle_fd, &val, sizeof(val) );
|
---|
801 | if(ret < 0) {
|
---|
802 | switch((int)errno) {
|
---|
803 | case EAGAIN:
|
---|
804 | #if EAGAIN != EWOULDBLOCK
|
---|
805 | case EWOULDBLOCK:
|
---|
806 | #endif
|
---|
807 | case EINTR:
|
---|
808 | // No need to do anything special here, just assume it's a legitimate wake-up
|
---|
809 | break;
|
---|
810 | default:
|
---|
811 | abort( "KERNEL : internal error, read failure on idle eventfd, error(%d) %s.", (int)errno, strerror( (int)errno ) );
|
---|
812 | }
|
---|
813 | }
|
---|
814 | }
|
---|
815 |
|
---|
816 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
817 | if(this->print_halts) {
|
---|
818 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl());
|
---|
819 | }
|
---|
820 | #endif
|
---|
821 | #else
|
---|
822 | // Do we already have a pending read
|
---|
823 | if(available(future)) {
|
---|
824 | // There is no pending read, we need to add one
|
---|
825 | reset(future);
|
---|
826 |
|
---|
827 | __kernel_read(this, future, iov, this->idle_fd );
|
---|
828 | }
|
---|
829 |
|
---|
830 | __cfa_io_flush( this, 1 );
|
---|
831 | #endif
|
---|
832 | }
|
---|
833 |
|
---|
834 | static bool mark_idle(__cluster_proc_list & this, processor & proc) {
|
---|
835 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
836 | if(!try_lock( this )) return false;
|
---|
837 | this.idle++;
|
---|
838 | /* paranoid */ verify( this.idle <= this.total );
|
---|
839 | remove(proc);
|
---|
840 | insert_first(this.idles, proc);
|
---|
841 |
|
---|
842 | __atomic_store_n(&this.fd, proc.idle_fd, __ATOMIC_SEQ_CST);
|
---|
843 | unlock( this );
|
---|
844 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
845 |
|
---|
846 | return true;
|
---|
847 | }
|
---|
848 |
|
---|
849 | static void mark_awake(__cluster_proc_list & this, processor & proc) {
|
---|
850 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
851 | lock( this );
|
---|
852 | this.idle--;
|
---|
853 | /* paranoid */ verify( this.idle >= 0 );
|
---|
854 | remove(proc);
|
---|
855 | insert_last(this.actives, proc);
|
---|
856 |
|
---|
857 | {
|
---|
858 | int fd = 0;
|
---|
859 | if(!this.idles`isEmpty) fd = this.idles`first.idle_fd;
|
---|
860 | __atomic_store_n(&this.fd, fd, __ATOMIC_SEQ_CST);
|
---|
861 | }
|
---|
862 |
|
---|
863 | unlock( this );
|
---|
864 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
865 | }
|
---|
866 |
|
---|
867 | //=============================================================================================
|
---|
868 | // Unexpected Terminating logic
|
---|
869 | //=============================================================================================
|
---|
870 | void __kernel_abort_msg( char * abort_text, int abort_text_size ) {
|
---|
871 | thread$ * thrd = __cfaabi_tls.this_thread;
|
---|
872 |
|
---|
873 | if(thrd) {
|
---|
874 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
|
---|
875 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
876 |
|
---|
877 | if ( &thrd->self_cor != thrd->curr_cor ) {
|
---|
878 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
|
---|
879 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
880 | }
|
---|
881 | else {
|
---|
882 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
|
---|
883 | }
|
---|
884 | }
|
---|
885 | else {
|
---|
886 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
|
---|
887 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
888 | }
|
---|
889 | }
|
---|
890 |
|
---|
891 | int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
---|
892 | return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
---|
893 | }
|
---|
894 |
|
---|
895 | static __spinlock_t kernel_debug_lock;
|
---|
896 |
|
---|
897 | extern "C" {
|
---|
898 | void __cfaabi_bits_acquire() {
|
---|
899 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
---|
900 | }
|
---|
901 |
|
---|
902 | void __cfaabi_bits_release() {
|
---|
903 | unlock( kernel_debug_lock );
|
---|
904 | }
|
---|
905 | }
|
---|
906 |
|
---|
907 | //=============================================================================================
|
---|
908 | // Kernel Utilities
|
---|
909 | //=============================================================================================
|
---|
910 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
911 | #include "io/types.hfa"
|
---|
912 | #endif
|
---|
913 |
|
---|
914 | static inline bool __maybe_io_drain( processor * proc ) {
|
---|
915 | bool ret = false;
|
---|
916 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
917 | __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd);
|
---|
918 |
|
---|
919 | // Check if we should drain the queue
|
---|
920 | $io_context * ctx = proc->io.ctx;
|
---|
921 | unsigned head = *ctx->cq.head;
|
---|
922 | unsigned tail = *ctx->cq.tail;
|
---|
923 | if(head == tail) return false;
|
---|
924 | #if OLD_MAIN
|
---|
925 | ready_schedule_lock();
|
---|
926 | ret = __cfa_io_drain( proc );
|
---|
927 | ready_schedule_unlock();
|
---|
928 | #else
|
---|
929 | ret = __cfa_io_drain( proc );
|
---|
930 | #endif
|
---|
931 | #endif
|
---|
932 | return ret;
|
---|
933 | }
|
---|
934 |
|
---|
935 | //-----------------------------------------------------------------------------
|
---|
936 | // Debug
|
---|
937 | __cfaabi_dbg_debug_do(
|
---|
938 | extern "C" {
|
---|
939 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
|
---|
940 | this.prev_name = prev_name;
|
---|
941 | this.prev_thrd = kernelTLS().this_thread;
|
---|
942 | }
|
---|
943 | }
|
---|
944 | )
|
---|
945 |
|
---|
946 | //-----------------------------------------------------------------------------
|
---|
947 | // Debug
|
---|
948 | bool threading_enabled(void) __attribute__((const)) {
|
---|
949 | return true;
|
---|
950 | }
|
---|
951 |
|
---|
952 | //-----------------------------------------------------------------------------
|
---|
953 | // Statistics
|
---|
954 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
955 | void print_halts( processor & this ) {
|
---|
956 | this.print_halts = true;
|
---|
957 | }
|
---|
958 |
|
---|
959 | static void crawl_list( cluster * cltr, dlist(processor) & list, unsigned count ) {
|
---|
960 | /* paranoid */ verify( cltr->stats );
|
---|
961 |
|
---|
962 | processor * it = &list`first;
|
---|
963 | for(unsigned i = 0; i < count; i++) {
|
---|
964 | /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count);
|
---|
965 | /* paranoid */ verify( it->local_data->this_stats );
|
---|
966 | // __print_stats( it->local_data->this_stats, cltr->print_stats, "Processor", it->name, (void*)it );
|
---|
967 | __tally_stats( cltr->stats, it->local_data->this_stats );
|
---|
968 | it = &(*it)`next;
|
---|
969 | }
|
---|
970 | }
|
---|
971 |
|
---|
972 | void crawl_cluster_stats( cluster & this ) {
|
---|
973 | // Stop the world, otherwise stats could get really messed-up
|
---|
974 | // this doesn't solve all problems but does solve many
|
---|
975 | // so it's probably good enough
|
---|
976 | disable_interrupts();
|
---|
977 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
978 |
|
---|
979 | crawl_list(&this, this.procs.actives, this.procs.total - this.procs.idle);
|
---|
980 | crawl_list(&this, this.procs.idles , this.procs.idle );
|
---|
981 |
|
---|
982 | // Unlock the RWlock
|
---|
983 | ready_mutate_unlock( last_size );
|
---|
984 | enable_interrupts();
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 | void print_stats_now( cluster & this, int flags ) {
|
---|
989 | crawl_cluster_stats( this );
|
---|
990 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this );
|
---|
991 | }
|
---|
992 | #endif
|
---|
993 | // Local Variables: //
|
---|
994 | // mode: c //
|
---|
995 | // tab-width: 4 //
|
---|
996 | // End: //
|
---|