source: libcfa/src/concurrency/kernel.cfa @ 4479890

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

Implemented helping for io drain based on timestamps.

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