Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Install signal handler for SIGSEGV.
[simgrid.git] / src / simix / smx_context_sysv.c
1 /* context_sysv - context switching with ucontexts from System V           */
2
3 /* Copyright (c) 2009-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdarg.h>
10
11 #include "xbt/parmap.h"
12 #include "smx_private.h"
13 #include "internal_config.h"
14 #include "context_sysv_config.h"        /* loads context system definitions */
15 #include "mc/mc.h"
16
17 #ifdef _XBT_WIN32
18 #  include <win32_ucontext.h>     /* context relative declarations */
19 #else
20 #  include <ucontext.h>           /* context relative declarations */
21 #endif
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
24
25 typedef struct s_smx_ctx_sysv {
26   s_smx_ctx_base_t super;       /* Fields of super implementation */
27   ucontext_t uc;                /* the ucontext that executes the code */
28   char *stack;                  /* the thread stack */
29 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
30
31 #ifdef CONTEXT_THREADS
32 static xbt_parmap_t sysv_parmap;
33 static smx_ctx_sysv_t* sysv_workers_context;   /* space to save the worker's context in each thread */
34 static unsigned long sysv_threads_working;     /* number of threads that have started their work */
35 static xbt_os_thread_key_t sysv_worker_id_key; /* thread-specific storage for the thread id */
36 #endif
37 static unsigned long sysv_process_index = 0;   /* index of the next process to run in the
38                                                 * list of runnable processes */
39 static smx_ctx_sysv_t sysv_maestro_context;
40
41 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
42 static void smx_ctx_sysv_free(smx_context_t context);
43 static smx_context_t
44 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
45     void_pfn_smxprocess_t cleanup_func, smx_process_t process);
46
47 static void smx_ctx_sysv_wrapper(int count, ...);
48
49 static void smx_ctx_sysv_stop_serial(smx_context_t context);
50 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
51 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
52 static void smx_ctx_sysv_runall_serial(void);
53
54 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
55 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
56 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
57 static void smx_ctx_sysv_runall_parallel(void);
58
59 /* This is a bit paranoid about sizeof(smx_ctx_sysv_t) not being a multiple of
60  * sizeof(int), but it doesn't harm. */
61 #define CTX_ADDR_LEN                            \
62   (sizeof(smx_ctx_sysv_t) / sizeof(int) +       \
63    !!(sizeof(smx_ctx_sysv_t) % sizeof(int)))
64
65 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
66 {
67   smx_ctx_base_factory_init(factory);
68   XBT_VERB("Activating SYSV context factory");
69
70   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
71   (*factory)->create_context = smx_ctx_sysv_create_context;
72   /* Do not overload that method (*factory)->finalize */
73   (*factory)->free = smx_ctx_sysv_free;
74   (*factory)->name = "smx_sysv_context_factory";
75
76   if (SIMIX_context_is_parallel()) {
77 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
78     int nthreads = SIMIX_context_get_nthreads();
79     sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
80     sysv_workers_context = xbt_new(smx_ctx_sysv_t, nthreads);
81     sysv_maestro_context = NULL;
82     xbt_os_thread_key_create(&sysv_worker_id_key);
83     (*factory)->stop = smx_ctx_sysv_stop_parallel;
84     (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
85     (*factory)->runall = smx_ctx_sysv_runall_parallel;
86 #else
87     THROWF(arg_error, 0, "No thread support for parallel context execution");
88 #endif
89   } else {
90     (*factory)->stop = smx_ctx_sysv_stop_serial;
91     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
92     (*factory)->runall = smx_ctx_sysv_runall_serial;
93   }    
94 }
95
96 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
97
98 #ifdef CONTEXT_THREADS
99   if (sysv_parmap)
100     xbt_parmap_destroy(sysv_parmap);
101   xbt_free(sysv_workers_context);
102 #endif
103   return smx_ctx_base_factory_finalize(factory);
104 }
105
106 static smx_context_t
107 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
108                             void_pfn_smxprocess_t cleanup_func,
109                             smx_process_t process)
110 {
111   int ctx_addr[CTX_ADDR_LEN];
112   smx_ctx_sysv_t context =
113     (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(
114       sizeof(s_smx_ctx_sysv_t),
115       code,
116       argc,
117       argv,
118       cleanup_func,
119       process);
120
121   /* if the user provided a function for the process then use it,
122      otherwise it is the context for maestro */
123   if (code) {
124
125     context->stack = SIMIX_context_stack_new();
126     getcontext(&(context->uc));
127
128     context->uc.uc_link = NULL;
129
130     context->uc.uc_stack.ss_sp =
131         pth_skaddr_makecontext(context->stack, smx_context_usable_stack_size);
132
133     context->uc.uc_stack.ss_size =
134         pth_sksize_makecontext(context->stack, smx_context_usable_stack_size);
135
136     memcpy(ctx_addr, &context, sizeof(smx_ctx_sysv_t));
137     switch (CTX_ADDR_LEN) {
138     case 1:
139       makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
140                   1, ctx_addr[0]);
141       break;
142     case 2:
143       makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
144                   2, ctx_addr[0], ctx_addr[1]);
145       break;
146     default:
147       xbt_die("Ucontexts are not supported on this arch yet (addr len = %zu/%zu = %zu)",
148               sizeof(smx_ctx_sysv_t), sizeof(int), CTX_ADDR_LEN);
149     }
150   } else {
151     if (process != NULL && sysv_maestro_context == NULL)
152       sysv_maestro_context = context;
153   }
154
155   if(MC_is_active() && code)
156     MC_new_stack_area(context->stack, ((smx_context_t)context)->process->name,
157                       &(context->uc), smx_context_usable_stack_size);
158
159   return (smx_context_t) context;
160 }
161
162 static void smx_ctx_sysv_free(smx_context_t context)
163 {
164
165   if (context) {
166     SIMIX_context_stack_delete(((smx_ctx_sysv_t)context)->stack);
167   }
168   smx_ctx_base_free(context);
169 }
170
171 static void smx_ctx_sysv_wrapper(int first, ...)
172
173   int ctx_addr[CTX_ADDR_LEN];
174   smx_ctx_sysv_t context;
175
176   ctx_addr[0] = first;
177   if (CTX_ADDR_LEN > 1) {
178     va_list ap;
179     int i;
180     va_start(ap, first);
181     for (i = 1; i < CTX_ADDR_LEN; i++)
182       ctx_addr[i] = va_arg(ap, int);
183     va_end(ap);
184   }
185   memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
186   (context->super.code) (context->super.argc, context->super.argv);
187
188   simix_global->context_factory->stop((smx_context_t) context);
189 }
190
191 static void smx_ctx_sysv_stop_serial(smx_context_t context)
192 {
193   smx_ctx_base_stop(context);
194   smx_ctx_sysv_suspend_serial(context);
195 }
196
197 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
198 {
199   /* determine the next context */
200   smx_context_t next_context;
201   unsigned long int i = sysv_process_index++;
202
203   if (i < xbt_dynar_length(simix_global->process_to_run)) {
204     /* execute the next process */
205     XBT_DEBUG("Run next process");
206     next_context = xbt_dynar_get_as(
207         simix_global->process_to_run,i, smx_process_t)->context;
208   }
209   else {
210     /* all processes were run, return to maestro */
211     XBT_DEBUG("No more process to run");
212     next_context = (smx_context_t) sysv_maestro_context;
213   }
214   SIMIX_context_set_current(next_context);
215   swapcontext(&((smx_ctx_sysv_t) context)->uc,
216       &((smx_ctx_sysv_t) next_context)->uc);
217 }
218
219 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
220 {
221   smx_context_t context = first_process->context;
222   SIMIX_context_set_current(context);
223   swapcontext(&sysv_maestro_context->uc,
224       &((smx_ctx_sysv_t) context)->uc);
225 }
226
227 static void smx_ctx_sysv_runall_serial(void)
228 {
229   smx_process_t first_process =
230       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
231   sysv_process_index = 1;
232
233   /* execute the first process */
234   smx_ctx_sysv_resume_serial(first_process);
235 }
236
237 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
238 {
239   smx_ctx_base_stop(context);
240   smx_ctx_sysv_suspend_parallel(context);
241 }
242
243 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
244 {
245 #ifdef CONTEXT_THREADS
246   /* determine the next context */
247   smx_process_t next_work = xbt_parmap_next(sysv_parmap);
248   smx_context_t next_context;
249   ucontext_t* next_stack;
250
251   if (next_work != NULL) {
252     /* there is a next process to resume */
253     XBT_DEBUG("Run next process");
254     next_context = next_work->context;
255     next_stack = &((smx_ctx_sysv_t) next_context)->uc;
256   }
257   else {
258     /* all processes were run, go to the barrier */
259     XBT_DEBUG("No more processes to run");
260     unsigned long worker_id =
261         (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
262     next_context = (smx_context_t)sysv_workers_context[worker_id];
263     next_stack = &((smx_ctx_sysv_t)next_context)->uc;
264   }
265
266   SIMIX_context_set_current(next_context);
267   swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);
268 #endif
269 }
270
271 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
272 {
273 #ifdef CONTEXT_THREADS
274   unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1);
275   xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);
276   smx_ctx_sysv_t worker_context = (smx_ctx_sysv_t)SIMIX_context_self();
277   sysv_workers_context[worker_id] = worker_context;
278   ucontext_t* worker_stack = &worker_context->uc;
279
280   smx_context_t context = first_process->context;
281   SIMIX_context_set_current(context);
282   swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
283 #endif
284 }
285
286 static void smx_ctx_sysv_runall_parallel(void)
287 {
288 #ifdef CONTEXT_THREADS
289   sysv_threads_working = 0;
290   xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
291       simix_global->process_to_run);
292 #endif
293 }