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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since f7d6bb0 was 05615ba, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Added commented debug line for preemption, uncomment to print log on preemption

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