Logo AND Algorithmique Numérique Distribuée

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