source: src/libcfa/concurrency/preemption.c@ 2ac095d

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

Added macros for parameters only present in debug

  • Property mode set to 100644
File size: 11.5 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// signal.c --
9//
10// Author : Thierry Delisle
11// Created On : Mon Jun 5 14:20:42 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count : 0
15//
16
17#include "libhdr.h"
18#include "preemption.h"
19
20extern "C" {
21#include <errno.h>
22#include <execinfo.h>
23#define __USE_GNU
24#include <signal.h>
25#undef __USE_GNU
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29}
30
31
32#ifdef __USE_STREAM__
33#include "fstream"
34#endif
35
36#define __CFA_DEFAULT_PREEMPTION__ 10000
37
38__attribute__((weak)) unsigned int default_preemption() {
39 return __CFA_DEFAULT_PREEMPTION__;
40}
41
42#define __CFA_SIGCXT__ ucontext_t *
43#define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
44
45static void preempt( processor * this );
46static void timeout( thread_desc * this );
47
48void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
49void sigHandler_alarm ( __CFA_SIGPARMS__ );
50void sigHandler_segv ( __CFA_SIGPARMS__ );
51void sigHandler_abort ( __CFA_SIGPARMS__ );
52
53static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
54
55//=============================================================================================
56// Kernel Preemption logic
57//=============================================================================================
58
59void kernel_start_preemption() {
60 LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n");
61 __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO );
62 __kernel_sigaction( SIGALRM, sigHandler_alarm , SA_SIGINFO );
63 __kernel_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO );
64 __kernel_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO );
65 // __kernel_sigaction( SIGABRT, sigHandler_abort , SA_SIGINFO );
66}
67
68void kernel_stop_preemption() {
69 //Block all signals, we are no longer in a position to handle them
70 sigset_t mask;
71 sigfillset( &mask );
72 sigprocmask( SIG_BLOCK, &mask, NULL );
73 LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");
74
75 // assert( !systemProcessor->alarms.head );
76 // assert( systemProcessor->alarms.tail == &systemProcessor->alarms.head );
77}
78
79LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
80
81void tick_preemption() {
82 // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ticking preemption\n" );
83
84 alarm_list_t * alarms = &systemProcessor->alarms;
85 __cfa_time_t currtime = __kernel_get_time();
86 while( alarms->head && alarms->head->alarm < currtime ) {
87 alarm_node_t * node = pop(alarms);
88 // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking %p\n", node );
89
90 if( node->kernel_alarm ) {
91 preempt( node->proc );
92 }
93 else {
94 timeout( node->thrd );
95 }
96
97 verify( validate( alarms ) );
98
99 if( node->period > 0 ) {
100 node->alarm = currtime + node->period;
101 insert( alarms, node );
102 }
103 else {
104 node->set = false;
105 }
106 }
107
108 if( alarms->head ) {
109 __kernel_set_timer( alarms->head->alarm - currtime );
110 }
111
112 verify( validate( alarms ) );
113 // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking preemption done\n" );
114}
115
116void update_preemption( processor * this, __cfa_time_t duration ) {
117 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p updating preemption to %lu\n", this, duration );
118
119 alarm_node_t * alarm = this->preemption_alarm;
120 duration *= 1000;
121
122 // Alarms need to be enabled
123 if ( duration > 0 && !alarm->set ) {
124 alarm->alarm = __kernel_get_time() + duration;
125 alarm->period = duration;
126 register_self( alarm );
127 }
128 // Zero duraction but alarm is set
129 else if ( duration == 0 && alarm->set ) {
130 unregister_self( alarm );
131 alarm->alarm = 0;
132 alarm->period = 0;
133 }
134 // If alarm is different from previous, change it
135 else if ( duration > 0 && alarm->period != duration ) {
136 unregister_self( alarm );
137 alarm->alarm = __kernel_get_time() + duration;
138 alarm->period = duration;
139 register_self( alarm );
140 }
141}
142
143void ?{}( preemption_scope * this, processor * proc ) {
144 (&this->alarm){ proc };
145 this->proc = proc;
146 this->proc->preemption_alarm = &this->alarm;
147 update_preemption( this->proc, this->proc->preemption );
148}
149
150void ^?{}( preemption_scope * this ) {
151 disable_interrupts();
152
153 update_preemption( this->proc, 0 );
154}
155
156//=============================================================================================
157// Kernel Signal logic
158//=============================================================================================
159
160extern "C" {
161 void disable_interrupts() {
162 __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );
163 verify( new_val < (unsigned short)65_000 );
164 verify( new_val != (unsigned short) 0 );
165 }
166
167 void enable_interrupts_noRF() {
168 __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
169 verify( prev != (unsigned short) 0 );
170 }
171
172 void enable_interrupts( DEBUG_CTX_PARAM ) {
173 processor * proc = this_processor;
174 thread_desc * thrd = this_thread;
175 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
176 verify( prev != (unsigned short) 0 );
177 if( prev == 1 && proc->pending_preemption ) {
178 proc->pending_preemption = false;
179 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Executing deferred CtxSwitch on %p\n", this_processor );
180 BlockInternal( thrd );
181 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Executing deferred back\n" );
182 }
183
184 LIB_DEBUG_DO( proc->last_enable = caller; )
185 }
186}
187
188static inline void signal_unblock( int sig ) {
189 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p unblocking sig %i\n", this_processor, sig );
190
191 sigset_t mask;
192 sigemptyset( &mask );
193 sigaddset( &mask, sig );
194
195 if ( sigprocmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
196 abortf( "internal error, sigprocmask" );
197 } // if
198}
199
200static inline bool preemption_ready() {
201 return disable_preempt_count == 0;
202}
203
204static inline void defer_ctxSwitch() {
205 this_processor->pending_preemption = true;
206}
207
208static inline void defer_alarm() {
209 systemProcessor->pending_alarm = true;
210}
211
212extern "C" {
213 __attribute__((noinline)) void __debug_break() {
214 pthread_kill( pthread_self(), SIGTRAP );
215 }
216}
217
218void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
219 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ctx Switch IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
220
221 if( preemption_ready() ) {
222 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Blocking thread %p on %p\n", this_thread, this_processor );
223 signal_unblock( SIGUSR1 );
224 BlockInternal( (thread_desc*)this_thread );
225 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Back\n\n");
226 }
227 else {
228 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Defering\n" );
229 defer_ctxSwitch();
230 signal_unblock( SIGUSR1 );
231 }
232}
233
234void sigHandler_alarm( __CFA_SIGPARMS__ ) {
235 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "\nAlarm IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
236
237 // if( ((intptr_t)cxt->uc_mcontext.gregs[REG_RIP]) > 0xFFFFFF ) __debug_break();
238
239 if( try_lock( &systemProcessor->alarm_lock DEBUG_CTX2 ) ) {
240 tick_preemption();
241 unlock( &systemProcessor->alarm_lock );
242 }
243 else {
244 defer_alarm();
245 }
246
247 signal_unblock( SIGALRM );
248
249 if( preemption_ready() && this_processor->pending_preemption ) {
250 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm IRH : Blocking thread %p on %p\n", this_thread, this_processor );
251 this_processor->pending_preemption = false;
252 BlockInternal( (thread_desc*)this_thread );
253 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm Switch IRH : Back\n\n");
254 }
255}
256
257static void preempt( processor * this ) {
258 // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : signalling %p\n", this );
259
260 if( this != systemProcessor ) {
261 pthread_kill( this->kernel_thread, SIGUSR1 );
262 }
263 else {
264 defer_ctxSwitch();
265 }
266}
267
268static void timeout( thread_desc * this ) {
269 //TODO : implement waking threads
270}
271
272static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
273 struct sigaction act;
274
275 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
276 act.sa_flags = flags;
277
278 // disabled during signal handler
279 sigemptyset( &act.sa_mask );
280 sigaddset( &act.sa_mask, sig );
281
282 if ( sigaction( sig, &act, NULL ) == -1 ) {
283 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,
284 " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
285 sig, handler, flags, errno, strerror( errno )
286 );
287 _exit( EXIT_FAILURE );
288 }
289}
290
291typedef void (*sa_handler_t)(int);
292
293static void __kernel_sigdefault( int sig ) {
294 struct sigaction act;
295
296 // act.sa_handler = SIG_DFL;
297 act.sa_flags = 0;
298 sigemptyset( &act.sa_mask );
299
300 if ( sigaction( sig, &act, NULL ) == -1 ) {
301 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,
302 " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
303 sig, errno, strerror( errno )
304 );
305 _exit( EXIT_FAILURE );
306 }
307}
308
309//=============================================================================================
310// Terminating Signals logic
311//=============================================================================================
312
313LIB_DEBUG_DO(
314 static void __kernel_backtrace( int start ) {
315 // skip first N stack frames
316
317 enum { Frames = 50 };
318 void * array[Frames];
319 int size = backtrace( array, Frames );
320 char ** messages = backtrace_symbols( array, size );
321
322 // find executable name
323 *index( messages[0], '(' ) = '\0';
324 #ifdef __USE_STREAM__
325 serr | "Stack back trace for:" | messages[0] | endl;
326 #else
327 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
328 #endif
329
330 // skip last 2 stack frames after main
331 for ( int i = start; i < size && messages != NULL; i += 1 ) {
332 char * name = NULL;
333 char * offset_begin = NULL;
334 char * offset_end = NULL;
335
336 for ( char *p = messages[i]; *p; ++p ) {
337 // find parantheses and +offset
338 if ( *p == '(' ) {
339 name = p;
340 }
341 else if ( *p == '+' ) {
342 offset_begin = p;
343 }
344 else if ( *p == ')' ) {
345 offset_end = p;
346 break;
347 }
348 }
349
350 // if line contains symbol print it
351 int frameNo = i - start;
352 if ( name && offset_begin && offset_end && name < offset_begin ) {
353 // delimit strings
354 *name++ = '\0';
355 *offset_begin++ = '\0';
356 *offset_end++ = '\0';
357
358 #ifdef __USE_STREAM__
359 serr | "(" | frameNo | ")" | messages[i] | ":"
360 | name | "+" | offset_begin | offset_end | endl;
361 #else
362 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
363 #endif
364 }
365 // otherwise, print the whole line
366 else {
367 #ifdef __USE_STREAM__
368 serr | "(" | frameNo | ")" | messages[i] | endl;
369 #else
370 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
371 #endif
372 }
373 }
374
375 free( messages );
376 }
377)
378
379void sigHandler_segv( __CFA_SIGPARMS__ ) {
380 LIB_DEBUG_DO(
381 #ifdef __USE_STREAM__
382 serr | "*CFA runtime error* program cfa-cpp terminated with"
383 | (sig == SIGSEGV ? "segment fault." : "bus error.")
384 | endl;
385 #else
386 fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
387 #endif
388
389 // skip first 2 stack frames
390 __kernel_backtrace( 1 );
391 )
392 exit( EXIT_FAILURE );
393}
394
395// void sigHandler_abort( __CFA_SIGPARMS__ ) {
396// // skip first 6 stack frames
397// LIB_DEBUG_DO( __kernel_backtrace( 6 ); )
398
399// // reset default signal handler
400// __kernel_sigdefault( SIGABRT );
401
402// raise( SIGABRT );
403// }
Note: See TracBrowser for help on using the repository browser.