Logo AND Algorithmique Numérique Distribuée

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