source: src/libcfa/concurrency/preemption.c @ c2b9f21

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 c2b9f21 was c2b9f21, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed libhdr, moved its content to bits

  • Property mode set to 100644
File size: 15.8 KB
RevLine 
[c81ebf9]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// signal.c --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon Jun 5 14:20:42 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:36:05 2017
13// Update Count     : 2
[c81ebf9]14//
15
16#include "preemption.h"
17
18extern "C" {
[82ff5845]19#include <errno.h>
[1c273d0]20#include <execinfo.h>
[4e6fb8e]21#define __USE_GNU
[c81ebf9]22#include <signal.h>
[4e6fb8e]23#undef __USE_GNU
[82ff5845]24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
[c81ebf9]27}
28
[1c273d0]29
30#ifdef __USE_STREAM__
31#include "fstream"
32#endif
[82ff5845]33
[969b3fe]34//TODO move to defaults
[82ff5845]35#define __CFA_DEFAULT_PREEMPTION__ 10000
[c81ebf9]36
[969b3fe]37//TODO move to defaults
[c81ebf9]38__attribute__((weak)) unsigned int default_preemption() {
39        return __CFA_DEFAULT_PREEMPTION__;
40}
41
[969b3fe]42// Short hands for signal context information
[82ff5845]43#define __CFA_SIGCXT__ ucontext_t *
44#define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
45
[969b3fe]46// FwdDeclarations : timeout handlers
[c81ebf9]47static void preempt( processor   * this );
48static void timeout( thread_desc * this );
49
[969b3fe]50// FwdDeclarations : Signal handlers
[82ff5845]51void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
[1c273d0]52void sigHandler_segv     ( __CFA_SIGPARMS__ );
53void sigHandler_abort    ( __CFA_SIGPARMS__ );
[82ff5845]54
[969b3fe]55// FwdDeclarations : sigaction wrapper
[82ff5845]56static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
[cd17862]57
[969b3fe]58// FwdDeclarations : alarm thread main
59void * alarm_loop( __attribute__((unused)) void * args );
60
61// Machine specific register name
[cd17862]62#ifdef __x86_64__
63#define CFA_REG_IP REG_RIP
64#else
65#define CFA_REG_IP REG_EIP
66#endif
67
[969b3fe]68KERNEL_STORAGE(event_kernel_t, event_kernel);         // private storage for event kernel
69event_kernel_t * event_kernel;                        // kernel public handle to even kernel
70static pthread_t alarm_thread;                        // pthread handle to alarm thread
71
[9236060]72void ?{}(event_kernel_t & this) {
73        (this.alarms){};
74        (this.lock){};
[969b3fe]75}
[82ff5845]76
[c81ebf9]77//=============================================================================================
78// Kernel Preemption logic
79//=============================================================================================
80
[969b3fe]81// Get next expired node
82static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
83        if( !alarms->head ) return NULL;                          // If no alarms return null
84        if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
85        return pop(alarms);                                       // Otherwise just pop head
86}
87
88// Tick one frame of the Discrete Event Simulation for alarms
[c81ebf9]89void tick_preemption() {
[969b3fe]90        alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
91        alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
92        __cfa_time_t currtime = __kernel_get_time();    // Check current time once so we everything "happens at once"
[8cb529e]93
[969b3fe]94        //Loop throught every thing expired
95        while( node = get_expired( alarms, currtime ) ) {
[1c273d0]96
[969b3fe]97                // Check if this is a kernel
[c81ebf9]98                if( node->kernel_alarm ) {
99                        preempt( node->proc );
100                }
101                else {
102                        timeout( node->thrd );
103                }
104
[969b3fe]105                // Check if this is a periodic alarm
[8cb529e]106                __cfa_time_t period = node->period;
107                if( period > 0 ) {
[969b3fe]108                        node->alarm = currtime + period;    // Alarm is periodic, add currtime to it (used cached current time)
109                        insert( alarms, node );             // Reinsert the node for the next time it triggers
[c81ebf9]110                }
111                else {
[969b3fe]112                        node->set = false;                  // Node is one-shot, just mark it as not pending
[c81ebf9]113                }
114        }
115
[969b3fe]116        // If there are still alarms pending, reset the timer
117        if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); }
[c81ebf9]118}
119
[969b3fe]120// Update the preemption of a processor and notify interested parties
[c81ebf9]121void update_preemption( processor * this, __cfa_time_t duration ) {
122        alarm_node_t * alarm = this->preemption_alarm;
123
124        // Alarms need to be enabled
125        if ( duration > 0 && !alarm->set ) {
126                alarm->alarm = __kernel_get_time() + duration;
127                alarm->period = duration;
128                register_self( alarm );
129        }
130        // Zero duraction but alarm is set
131        else if ( duration == 0 && alarm->set ) {
132                unregister_self( alarm );
133                alarm->alarm = 0;
134                alarm->period = 0;
135        }
136        // If alarm is different from previous, change it
137        else if ( duration > 0 && alarm->period != duration ) {
138                unregister_self( alarm );
139                alarm->alarm = __kernel_get_time() + duration;
140                alarm->period = duration;
141                register_self( alarm );
142        }
143}
144
145//=============================================================================================
[cd17862]146// Kernel Signal Tools
[c81ebf9]147//=============================================================================================
148
[36982fc]149__cfaabi_dbg_debug_do( static thread_local void * last_interrupt = 0; )
[b227f68]150
[82ff5845]151extern "C" {
[969b3fe]152        // Disable interrupts by incrementing the counter
[82ff5845]153        void disable_interrupts() {
[4e6fb8e]154                __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );
[969b3fe]155                verify( new_val < 65_000u );              // If this triggers someone is disabling interrupts without enabling them
[82ff5845]156        }
157
[969b3fe]158        // Enable interrupts by decrementing the counter
159        // If counter reaches 0, execute any pending CtxSwitch
[36982fc]160        void enable_interrupts( __cfaabi_dbg_ctx_param ) {
[969b3fe]161                processor * proc   = this_processor;      // Cache the processor now since interrupts can start happening after the atomic add
162                thread_desc * thrd = this_thread;         // Cache the thread now since interrupts can start happening after the atomic add
163
[4e6fb8e]164                unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
[969b3fe]165                verify( prev != 0u );                     // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
166
167                // Check if we need to prempt the thread because an interrupt was missed
[1c273d0]168                if( prev == 1 && proc->pending_preemption ) {
169                        proc->pending_preemption = false;
170                        BlockInternal( thrd );
[82ff5845]171                }
[4e6fb8e]172
[969b3fe]173                // For debugging purposes : keep track of the last person to enable the interrupts
[36982fc]174                __cfaabi_dbg_debug_do( proc->last_enable = caller; )
[82ff5845]175        }
[969b3fe]176
177        // Disable interrupts by incrementint the counter
178        // Don't execute any pending CtxSwitch even if counter reaches 0
179        void enable_interrupts_noPoll() {
180                __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
181                verify( prev != 0u );                     // If this triggers someone is enabled already enabled interrupts
182        }
[82ff5845]183}
184
[969b3fe]185// sigprocmask wrapper : unblock a single signal
[1c273d0]186static inline void signal_unblock( int sig ) {
[82ff5845]187        sigset_t mask;
188        sigemptyset( &mask );
[1c273d0]189        sigaddset( &mask, sig );
[82ff5845]190
[47ecf2b]191        if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
192            abortf( "internal error, pthread_sigmask" );
[cd17862]193        }
[82ff5845]194}
195
[969b3fe]196// sigprocmask wrapper : block a single signal
[cd17862]197static inline void signal_block( int sig ) {
198        sigset_t mask;
199        sigemptyset( &mask );
200        sigaddset( &mask, sig );
[47ecf2b]201
[cd17862]202        if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
203            abortf( "internal error, pthread_sigmask" );
204        }
205}
[47ecf2b]206
[969b3fe]207// kill wrapper : signal a processor
[cd17862]208static void preempt( processor * this ) {
209        pthread_kill( this->kernel_thread, SIGUSR1 );
[1c273d0]210}
[82ff5845]211
[969b3fe]212// reserved for future use
[cd17862]213static void timeout( thread_desc * this ) {
214        //TODO : implement waking threads
215}
216
[969b3fe]217
218// Check if a CtxSwitch signal handler shoud defer
219// If true  : preemption is safe
220// If false : preemption is unsafe and marked as pending
221static inline bool preemption_ready() {
222        bool ready = disable_preempt_count == 0 && !preemption_in_progress; // Check if preemption is safe
223        this_processor->pending_preemption = !ready;                        // Adjust the pending flag accordingly
224        return ready;
225}
226
[cd17862]227//=============================================================================================
228// Kernel Signal Startup/Shutdown logic
229//=============================================================================================
230
[969b3fe]231// Startup routine to activate preemption
232// Called from kernel_startup
[cd17862]233void kernel_start_preemption() {
[36982fc]234        __cfaabi_dbg_print_safe("Kernel : Starting preemption\n");
[969b3fe]235
236        // Start with preemption disabled until ready
237        disable_preempt_count = 1;
238
239        // Initialize the event kernel
240        event_kernel = (event_kernel_t *)&storage_event_kernel;
[9236060]241        (*event_kernel){};
[969b3fe]242
243        // Setup proper signal handlers
[ed235b6]244        __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART );         // CtxSwitch handler
[969b3fe]245        // __kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );      // Failure handler
246        // __kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );      // Failure handler
[cd17862]247
248        signal_block( SIGALRM );
249
250        pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
251}
252
[969b3fe]253// Shutdown routine to deactivate preemption
254// Called from kernel_shutdown
[cd17862]255void kernel_stop_preemption() {
[36982fc]256        __cfaabi_dbg_print_safe("Kernel : Preemption stopping\n");
[d6ff3ff]257
[969b3fe]258        // Block all signals since we are already shutting down
[cd17862]259        sigset_t mask;
260        sigfillset( &mask );
261        sigprocmask( SIG_BLOCK, &mask, NULL );
262
[969b3fe]263        // Notify the alarm thread of the shutdown
[a0b3e32]264        sigval val = { 1 };
265        pthread_sigqueue( alarm_thread, SIGALRM, val );
[969b3fe]266
267        // Wait for the preemption thread to finish
[cd17862]268        pthread_join( alarm_thread, NULL );
[969b3fe]269
270        // Preemption is now fully stopped
271
[36982fc]272        __cfaabi_dbg_print_safe("Kernel : Preemption stopped\n");
[cd17862]273}
274
[969b3fe]275// Raii ctor/dtor for the preemption_scope
276// Used by thread to control when they want to receive preemption signals
[242a902]277void ?{}( preemption_scope & this, processor * proc ) {
[9236060]278        (this.alarm){ proc, zero_time, zero_time };
[242a902]279        this.proc = proc;
280        this.proc->preemption_alarm = &this.alarm;
[969b3fe]281
[9236060]282        update_preemption( this.proc, from_us(this.proc->cltr->preemption) );
[cd17862]283}
284
[242a902]285void ^?{}( preemption_scope & this ) {
[cd17862]286        disable_interrupts();
287
[9236060]288        update_preemption( this.proc, zero_time );
[cd17862]289}
290
291//=============================================================================================
292// Kernel Signal Handlers
293//=============================================================================================
[47ecf2b]294
[969b3fe]295// Context switch signal handler
296// Receives SIGUSR1 signal and causes the current thread to yield
[1c273d0]297void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
[36982fc]298        __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
[969b3fe]299
300        // Check if it is safe to preempt here
301        if( !preemption_ready() ) { return; }
302
303        preemption_in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
304        signal_unblock( SIGUSR1 );                          // We are about to CtxSwitch out of the signal handler, let other handlers in
305        preemption_in_progress = false;                     // Clear the in progress flag
306
307        // Preemption can occur here
308
309        BlockInternal( (thread_desc*)this_thread );         // Do the actual CtxSwitch
[c81ebf9]310}
311
[969b3fe]312// Main of the alarm thread
313// Waits on SIGALRM and send SIGUSR1 to whom ever needs it
[cd17862]314void * alarm_loop( __attribute__((unused)) void * args ) {
[969b3fe]315        // Block sigalrms to control when they arrive
[cd17862]316        sigset_t mask;
317        sigemptyset( &mask );
318        sigaddset( &mask, SIGALRM );
319
320        if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
321            abortf( "internal error, pthread_sigmask" );
[82ff5845]322        }
[c81ebf9]323
[969b3fe]324        // Main loop
[cd17862]325        while( true ) {
[969b3fe]326                // Wait for a sigalrm
[a0b3e32]327                siginfo_t info;
328                int sig = sigwaitinfo( &mask, &info );
[969b3fe]329
[e2f7bc3]330                if( sig < 0 ) {
331                        //Error!
332                        int err = errno;
333                        switch( err ) {
334                                case EAGAIN :
335                                case EINTR :
336                                        continue;
337                        case EINVAL :
338                                        abortf("Timeout was invalid.");
339                                default:
340                                        abortf("Unhandled error %d", err);
341                        }
342                }
343
[969b3fe]344                // If another signal arrived something went wrong
[8cb529e]345                assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
346
[36982fc]347                // __cfaabi_dbg_print_safe("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
[969b3fe]348                // Switch on the code (a.k.a. the sender) to
[8cb529e]349                switch( info.si_code )
[a0b3e32]350                {
[969b3fe]351                // Timers can apparently be marked as sent for the kernel
352                // In either case, tick preemption
[8cb529e]353                case SI_TIMER:
354                case SI_KERNEL:
[36982fc]355                        // __cfaabi_dbg_print_safe("Kernel : Preemption thread tick\n");
356                        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[8cb529e]357                        tick_preemption();
[ea7d2b0]358                        unlock( event_kernel->lock );
[8cb529e]359                        break;
[969b3fe]360                // Signal was not sent by the kernel but by an other thread
[8cb529e]361                case SI_QUEUE:
[969b3fe]362                        // For now, other thread only signal the alarm thread to shut it down
363                        // If this needs to change use info.si_value and handle the case here
[8cb529e]364                        goto EXIT;
[cd17862]365                }
366        }
[a0b3e32]367
[8cb529e]368EXIT:
[36982fc]369        __cfaabi_dbg_print_safe("Kernel : Preemption thread stopping\n");
[a0b3e32]370        return NULL;
[82ff5845]371}
372
[969b3fe]373// Sigaction wrapper : register an signal handler
[82ff5845]374static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
375        struct sigaction act;
376
377        act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
[1c273d0]378        act.sa_flags = flags;
379
380        if ( sigaction( sig, &act, NULL ) == -1 ) {
[36982fc]381                __cfaabi_dbg_print_buffer_decl(
[1c273d0]382                        " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
383                        sig, handler, flags, errno, strerror( errno )
384                );
385                _exit( EXIT_FAILURE );
386        }
387}
388
[969b3fe]389// Sigaction wrapper : restore default handler
[1c273d0]390static void __kernel_sigdefault( int sig ) {
391        struct sigaction act;
392
[969b3fe]393        act.sa_handler = SIG_DFL;
[1c273d0]394        act.sa_flags = 0;
395        sigemptyset( &act.sa_mask );
[82ff5845]396
397        if ( sigaction( sig, &act, NULL ) == -1 ) {
[36982fc]398                __cfaabi_dbg_print_buffer_decl(
[1c273d0]399                        " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
400                        sig, errno, strerror( errno )
401                );
[82ff5845]402                _exit( EXIT_FAILURE );
[1c273d0]403        }
404}
405
406//=============================================================================================
407// Terminating Signals logic
408//=============================================================================================
409
[36982fc]410__cfaabi_dbg_debug_do(
[1c273d0]411        static void __kernel_backtrace( int start ) {
412                // skip first N stack frames
413
414                enum { Frames = 50 };
415                void * array[Frames];
416                int size = backtrace( array, Frames );
417                char ** messages = backtrace_symbols( array, size );
418
419                // find executable name
420                *index( messages[0], '(' ) = '\0';
421                #ifdef __USE_STREAM__
422                serr | "Stack back trace for:" | messages[0] | endl;
423                #else
424                fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
425                #endif
426
427                // skip last 2 stack frames after main
428                for ( int i = start; i < size && messages != NULL; i += 1 ) {
429                        char * name = NULL;
430                        char * offset_begin = NULL;
431                        char * offset_end = NULL;
432
433                        for ( char *p = messages[i]; *p; ++p ) {
434                                // find parantheses and +offset
435                                if ( *p == '(' ) {
436                                        name = p;
437                                }
438                                else if ( *p == '+' ) {
439                                        offset_begin = p;
440                                }
441                                else if ( *p == ')' ) {
442                                        offset_end = p;
443                                        break;
444                                }
445                        }
446
447                        // if line contains symbol print it
448                        int frameNo = i - start;
449                        if ( name && offset_begin && offset_end && name < offset_begin ) {
450                                // delimit strings
451                                *name++ = '\0';
452                                *offset_begin++ = '\0';
453                                *offset_end++ = '\0';
454
455                                #ifdef __USE_STREAM__
456                                serr    | "("  | frameNo | ")" | messages[i] | ":"
457                                        | name | "+" | offset_begin | offset_end | endl;
458                                #else
459                                fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
460                                #endif
461                        }
462                        // otherwise, print the whole line
463                        else {
464                                #ifdef __USE_STREAM__
465                                serr | "(" | frameNo | ")" | messages[i] | endl;
466                                #else
467                                fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
468                                #endif
469                        }
470                }
471
472                free( messages );
473        }
474)
475
[f2b12406]476// void sigHandler_segv( __CFA_SIGPARMS__ ) {
[36982fc]477//      __cfaabi_dbg_debug_do(
[f2b12406]478//              #ifdef __USE_STREAM__
479//              serr    | "*CFA runtime error* program cfa-cpp terminated with"
480//                      | (sig == SIGSEGV ? "segment fault." : "bus error.")
481//                      | endl;
482//              #else
483//              fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
484//              #endif
485
486//              // skip first 2 stack frames
487//              __kernel_backtrace( 1 );
488//      )
489//      exit( EXIT_FAILURE );
490// }
[1c273d0]491
492// void sigHandler_abort( __CFA_SIGPARMS__ ) {
493//      // skip first 6 stack frames
[36982fc]494//      __cfaabi_dbg_debug_do( __kernel_backtrace( 6 ); )
[1c273d0]495
496//      // reset default signal handler
497//      __kernel_sigdefault( SIGABRT );
498
499//      raise( SIGABRT );
[6b0b624]500// }
501
502// Local Variables: //
503// mode: c //
504// tab-width: 4 //
505// End: //
Note: See TracBrowser for help on using the repository browser.