source: src/libcfa/concurrency/kernel.c @ e464759

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since e464759 was 5ea06d6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Prototype of multi monitor internal scheduling

  • Property mode set to 100644
File size: 15.0 KB
Line 
1//                              -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// kernel.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17#include "startup.h"
18
19//Start and stop routine for the kernel, declared first to make sure they run first
20void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
21void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
22
23//Header
24#include "kernel_private.h"
25
26//C Includes
27#include <stddef.h>
28extern "C" {
29#include <stdio.h>
30#include <fenv.h>
31#include <sys/resource.h>
32#include <signal.h>
33#include <unistd.h>
34}
35
36//CFA Includes
37#include "libhdr.h"
38
39//Private includes
40#define __CFA_INVOKE_PRIVATE__
41#include "invoke.h"
42
43//-----------------------------------------------------------------------------
44// Kernel storage
45#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
46
47KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
48KERNEL_STORAGE(cluster, systemCluster);
49KERNEL_STORAGE(processor, systemProcessor);
50KERNEL_STORAGE(thread_desc, mainThread);
51KERNEL_STORAGE(machine_context_t, mainThread_context);
52
53cluster * systemCluster;
54processor * systemProcessor;
55thread_desc * mainThread;
56
57//-----------------------------------------------------------------------------
58// Global state
59
60thread_local processor * this_processor;
61
62coroutine_desc * this_coroutine(void) {
63        return this_processor->current_coroutine;
64}
65
66thread_desc * this_thread(void) {
67        return this_processor->current_thread;
68}
69
70//-----------------------------------------------------------------------------
71// Main thread construction
72struct current_stack_info_t {
73        machine_context_t ctx; 
74        unsigned int size;              // size of stack
75        void *base;                             // base of stack
76        void *storage;                  // pointer to stack
77        void *limit;                    // stack grows towards stack limit
78        void *context;                  // address of cfa_context_t
79        void *top;                              // address of top of storage
80};
81
82void ?{}( current_stack_info_t * this ) {
83        CtxGet( &this->ctx );
84        this->base = this->ctx.FP;
85        this->storage = this->ctx.SP;
86
87        rlimit r;
88        getrlimit( RLIMIT_STACK, &r);
89        this->size = r.rlim_cur;
90
91        this->limit = (void *)(((intptr_t)this->base) - this->size);
92        this->context = &mainThread_context_storage;
93        this->top = this->base;
94}
95
96void ?{}( coStack_t * this, current_stack_info_t * info) {
97        this->size = info->size;
98        this->storage = info->storage;
99        this->limit = info->limit;
100        this->base = info->base;
101        this->context = info->context;
102        this->top = info->top;
103        this->userStack = true;
104}
105
106void ?{}( coroutine_desc * this, current_stack_info_t * info) {
107        (&this->stack){ info }; 
108        this->name = "Main Thread";
109        this->errno_ = 0;
110        this->state = Start;
111}
112
113void ?{}( thread_desc * this, current_stack_info_t * info) {
114        (&this->cor){ info };
115}
116
117//-----------------------------------------------------------------------------
118// Processor coroutine
119void ?{}(processorCtx_t * this, processor * proc) {
120        (&this->__cor){};
121        this->proc = proc;
122        proc->runner = this;
123}
124
125void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
126        (&this->__cor){ info };
127        this->proc = proc;
128        proc->runner = this;
129}
130
131void ?{}(processor * this) {
132        this{ systemCluster };
133}
134
135void ?{}(processor * this, cluster * cltr) {
136        this->cltr = cltr;
137        this->current_coroutine = NULL;
138        this->current_thread = NULL;
139        (&this->terminated){};
140        this->is_terminated = false;
141
142        start( this );
143}
144
145void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
146        this->cltr = cltr;
147        this->current_coroutine = NULL;
148        this->current_thread = NULL;
149        (&this->terminated){};
150        this->is_terminated = false;
151
152        this->runner = runner;
153        LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
154        runner{ this };
155}
156
157void ^?{}(processor * this) {
158        if( ! this->is_terminated ) {
159                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
160                this->is_terminated = true;
161                wait( &this->terminated );
162        }
163}
164
165void ?{}(cluster * this) {
166        ( &this->ready_queue ){};
167        ( &this->lock ){};
168}
169
170void ^?{}(cluster * this) {
171       
172}
173
174//=============================================================================================
175// Kernel Scheduling logic
176//=============================================================================================
177//Main of the processor contexts
178void main(processorCtx_t * runner) {
179        processor * this = runner->proc;
180        LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
181
182        thread_desc * readyThread = NULL;
183        for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 
184        {
185                readyThread = nextThread( this->cltr );
186
187                if(readyThread) 
188                {
189                        runThread(this, readyThread);
190
191                        //Some actions need to be taken from the kernel
192                        finishRunning(this);
193
194                        spin_count = 0;
195                } 
196                else 
197                {
198                        spin(this, &spin_count);
199                }               
200        }
201
202        LIB_DEBUG_PRINT_SAFE("Kernel : core %p unlocking thread\n", this);
203        signal( &this->terminated );
204        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
205}
206
207// runThread runs a thread by context switching
208// from the processor coroutine to the target thread
209void runThread(processor * this, thread_desc * dst) {
210        coroutine_desc * proc_cor = get_coroutine(this->runner);
211        coroutine_desc * thrd_cor = get_coroutine(dst);
212       
213        //Reset the terminating actions here
214        this->finish.action_code = No_Action;
215
216        //Update global state
217        this->current_thread = dst;
218
219        // Context Switch to the thread
220        ThreadCtxSwitch(proc_cor, thrd_cor);
221        // when ThreadCtxSwitch returns we are back in the processor coroutine
222}
223
224// Once a thread has finished running, some of
225// its final actions must be executed from the kernel
226void finishRunning(processor * this) {
227        if( this->finish.action_code == Release ) {
228                unlock( this->finish.lock );
229        }
230        else if( this->finish.action_code == Schedule ) {
231                ScheduleThread( this->finish.thrd );
232        }
233        else if( this->finish.action_code == Release_Schedule ) {
234                unlock( this->finish.lock );           
235                ScheduleThread( this->finish.thrd );
236        }
237        else {
238                assert(this->finish.action_code == No_Action);
239        }
240}
241
242// Handles spinning logic
243// TODO : find some strategy to put cores to sleep after some time
244void spin(processor * this, unsigned int * spin_count) {
245        (*spin_count)++;
246}
247
248// Context invoker for processors
249// This is the entry point for processors (kernel threads)
250// It effectively constructs a coroutine by stealing the pthread stack
251void * CtxInvokeProcessor(void * arg) {
252        processor * proc = (processor *) arg;
253        this_processor = proc;
254        // SKULLDUGGERY: We want to create a context for the processor coroutine
255        // which is needed for the 2-step context switch. However, there is no reason
256        // to waste the perfectly valid stack create by pthread.
257        current_stack_info_t info;
258        machine_context_t ctx;
259        info.context = &ctx;
260        processorCtx_t proc_cor_storage = { proc, &info };
261
262        LIB_DEBUG_PRINT_SAFE("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
263
264        //Set global state
265        proc->current_coroutine = &proc->runner->__cor;
266        proc->current_thread = NULL;
267
268        //We now have a proper context from which to schedule threads
269        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
270
271        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
272        // resume it to start it like it normally would, it will just context switch
273        // back to here. Instead directly call the main since we already are on the
274        // appropriate stack.
275        proc_cor_storage.__cor.state = Active;
276      main( &proc_cor_storage );
277      proc_cor_storage.__cor.state = Halted;
278
279        // Main routine of the core returned, the core is now fully terminated
280        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner); 
281
282        return NULL;
283}
284
285void start(processor * this) {
286        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
287       
288        // pthread_attr_t attributes;
289        // pthread_attr_init( &attributes );
290
291        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
292
293        // pthread_attr_destroy( &attributes );
294
295        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
296}
297
298//-----------------------------------------------------------------------------
299// Scheduler routines
300void ScheduleThread( thread_desc * thrd ) {
301        if( !thrd ) return;
302
303        assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
304       
305        lock( &systemProcessor->cltr->lock );
306        append( &systemProcessor->cltr->ready_queue, thrd );
307        unlock( &systemProcessor->cltr->lock );
308}
309
310thread_desc * nextThread(cluster * this) {
311        lock( &this->lock );
312        thread_desc * head = pop_head( &this->ready_queue );
313        unlock( &this->lock );
314        return head;
315}
316
317void ScheduleInternal() {
318        suspend();
319}
320
321void ScheduleInternal( spinlock * lock ) {
322        this_processor->finish.action_code = Release;
323        this_processor->finish.lock = lock;
324        suspend();
325}
326
327void ScheduleInternal( thread_desc * thrd ) {
328        this_processor->finish.action_code = Schedule;
329        this_processor->finish.thrd = thrd;
330        suspend();
331}
332
333void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
334        this_processor->finish.action_code = Release_Schedule;
335        this_processor->finish.lock = lock;
336        this_processor->finish.thrd = thrd;
337        suspend();
338}
339
340//-----------------------------------------------------------------------------
341// Kernel boot procedures
342void kernel_startup(void) {
343        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");   
344
345        // Start by initializing the main thread
346        // SKULLDUGGERY: the mainThread steals the process main thread
347        // which will then be scheduled by the systemProcessor normally
348        mainThread = (thread_desc *)&mainThread_storage;
349        current_stack_info_t info;
350        mainThread{ &info };
351
352        // Initialize the system cluster
353        systemCluster = (cluster *)&systemCluster_storage;
354        systemCluster{};
355
356        // Initialize the system processor and the system processor ctx
357        // (the coroutine that contains the processing control flow)
358        systemProcessor = (processor *)&systemProcessor_storage;
359        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
360
361        // Add the main thread to the ready queue
362        // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
363        ScheduleThread(mainThread);
364
365        //initialize the global state variables
366        this_processor = systemProcessor;
367        this_processor->current_thread = mainThread;
368        this_processor->current_coroutine = &mainThread->cor;
369
370        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
371        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
372        // mainThread is on the ready queue when this call is made.
373        resume(systemProcessor->runner);
374
375
376
377        // THE SYSTEM IS NOW COMPLETELY RUNNING
378        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
379}
380
381void kernel_shutdown(void) {
382        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
383
384        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
385        // When its coroutine terminates, it return control to the mainThread
386        // which is currently here
387        systemProcessor->is_terminated = true;
388        suspend();
389
390        // THE SYSTEM IS NOW COMPLETELY STOPPED
391
392        // Destroy the system processor and its context in reverse order of construction
393        // These were manually constructed so we need manually destroy them
394        ^(systemProcessor->runner){};
395        ^(systemProcessor){};
396
397        // Final step, destroy the main thread since it is no longer needed
398        // Since we provided a stack to this taxk it will not destroy anything
399        ^(mainThread){};
400
401        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");   
402}
403
404static spinlock kernel_abort_lock;
405static spinlock kernel_debug_lock;
406static bool kernel_abort_called = false;
407
408void * kernel_abort    (void) __attribute__ ((__nothrow__)) {
409        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
410        // the globalAbort flag is true.
411        lock( &kernel_abort_lock );
412
413        // first task to abort ?
414        if ( !kernel_abort_called ) {                   // not first task to abort ?
415                kernel_abort_called = true;
416                unlock( &kernel_abort_lock );
417        } 
418        else {
419                unlock( &kernel_abort_lock );
420               
421                sigset_t mask;
422                sigemptyset( &mask );
423                sigaddset( &mask, SIGALRM );                    // block SIGALRM signals
424                sigaddset( &mask, SIGUSR1 );                    // block SIGUSR1 signals
425                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
426                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it             
427        }
428
429        return this_thread();
430}
431
432void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
433        thread_desc * thrd = kernel_data;
434
435        int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->cor.name, thrd );
436        __lib_debug_write( STDERR_FILENO, abort_text, len );
437
438        if ( thrd != this_coroutine() ) {
439                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine()->name, this_coroutine() );
440                __lib_debug_write( STDERR_FILENO, abort_text, len );
441        } 
442        else {
443                __lib_debug_write( STDERR_FILENO, ".\n", 2 );
444        }
445}
446
447extern "C" {
448        void __lib_debug_acquire() {
449                lock(&kernel_debug_lock);
450        }
451
452        void __lib_debug_release() {
453                unlock(&kernel_debug_lock);
454        }
455}
456
457//-----------------------------------------------------------------------------
458// Locks
459void ?{}( spinlock * this ) {
460        this->lock = 0;
461}
462void ^?{}( spinlock * this ) {
463
464}
465
466void lock( spinlock * this ) {
467        for ( unsigned int i = 1;; i += 1 ) {
468                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
469        }
470}
471
472void unlock( spinlock * this ) {
473        __sync_lock_release_4( &this->lock );
474}
475
476void ?{}( signal_once * this ) {
477        this->cond = false;
478}
479void ^?{}( signal_once * this ) {
480
481}
482
483void wait( signal_once * this ) {
484        lock( &this->lock );
485        if( !this->cond ) {
486                append( &this->blocked, this_thread() );
487                ScheduleInternal( &this->lock );
488                lock( &this->lock );
489        }
490        unlock( &this->lock );
491}
492
493void signal( signal_once * this ) {
494        lock( &this->lock );
495        {
496                this->cond = true;
497
498                thread_desc * it;
499                while( it = pop_head( &this->blocked) ) {
500                        ScheduleThread( it );
501                }
502        }
503        unlock( &this->lock );
504}
505
506//-----------------------------------------------------------------------------
507// Queues
508void ?{}( __thread_queue_t * this ) {
509        this->head = NULL;
510        this->tail = &this->head;
511}
512
513void append( __thread_queue_t * this, thread_desc * t ) {
514        assert(this->tail != NULL);
515        *this->tail = t;
516        this->tail = &t->next;
517}
518
519thread_desc * pop_head( __thread_queue_t * this ) {
520        thread_desc * head = this->head;
521        if( head ) {
522                this->head = head->next;
523                if( !head->next ) {
524                        this->tail = &this->head;
525                }
526                head->next = NULL;
527        }       
528        return head;
529}
530
531void ?{}( __thread_stack_t * this ) {
532        this->top = NULL;
533}
534
535void push( __thread_stack_t * this, thread_desc * t ) {
536        assert(t->next != NULL);
537        t->next = this->top;
538        this->top = t;
539}
540
541thread_desc * pop( __thread_stack_t * this ) {
542        thread_desc * top = this->top;
543        if( top ) {
544                this->top = top->next;
545                top->next = NULL;
546        }       
547        return top;
548}
549// Local Variables: //
550// mode: c //
551// tab-width: 4 //
552// End: //
Note: See TracBrowser for help on using the repository browser.