Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check for failed links on back_route when crosstraffic is activated
[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 "simix/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 smx_ctx_sysv_self_parallel(void);
56 static smx_context_t
57 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
58     void_pfn_smxprocess_t cleanup_func, void* data);
59
60 static void smx_ctx_sysv_wrapper(int count, ...);
61
62 static void smx_ctx_sysv_stop_serial(smx_context_t context);
63 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
64 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
65 static void smx_ctx_sysv_runall_serial(void);
66
67 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
68 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
69 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
70 static void smx_ctx_sysv_runall_parallel(void);
71
72 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
73  * but it doesn't harm. */
74 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
75 union u_ctx_addr {
76   void *addr;
77   int intv[CTX_ADDR_LEN];
78 };
79 #if (CTX_ADDR_LEN == 1)
80 #  define CTX_ADDR_SPLIT(u) (u).intv[0]
81 #elif (CTX_ADDR_LEN == 2)
82 #  define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
83 #else
84 #  error Your architecture is not supported yet
85 #endif
86
87 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
88 {
89   smx_ctx_base_factory_init(factory);
90   XBT_VERB("Activating SYSV context factory");
91
92   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
93   (*factory)->create_context = smx_ctx_sysv_create_context;
94   /* Do not overload that method (*factory)->finalize */
95   (*factory)->free = smx_ctx_sysv_free;
96   (*factory)->name = "smx_sysv_context_factory";
97
98   if (SIMIX_context_is_parallel()) {
99 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
100     int nthreads = SIMIX_context_get_nthreads();
101     sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
102     sysv_workers_stacks = xbt_new(ucontext_t, nthreads);
103     xbt_os_thread_key_create(&sysv_worker_id_key);
104     (*factory)->stop = smx_ctx_sysv_stop_parallel;
105     (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
106     (*factory)->runall = smx_ctx_sysv_runall_parallel;
107 #else
108     THROWF(arg_error, 0, "No thread support for parallel context execution");
109 #endif
110   } else {
111     (*factory)->stop = smx_ctx_sysv_stop_serial;
112     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
113     (*factory)->runall = smx_ctx_sysv_runall_serial;
114   }    
115 }
116
117 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
118
119 #ifdef CONTEXT_THREADS
120   if (sysv_parmap)
121     xbt_parmap_destroy(sysv_parmap);
122   xbt_free(sysv_workers_stacks);
123 #endif
124   return smx_ctx_base_factory_finalize(factory);
125 }
126
127 static smx_context_t
128 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
129                                   int argc, char **argv,
130                                   void_pfn_smxprocess_t cleanup_func,
131                                   void *data)
132 {
133   union u_ctx_addr ctx_addr;
134   smx_ctx_sysv_t context =
135       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
136                                                                  code,
137                                                                  argc,
138                                                                  argv,
139                                                                  cleanup_func,
140                                                                  data);
141
142   /* if the user provided a function for the process then use it,
143      otherwise it is the context for maestro */
144   if (code) {
145
146     getcontext(&(context->uc));
147
148     context->uc.uc_link = NULL;
149
150     context->uc.uc_stack.ss_sp =
151         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
152
153     context->uc.uc_stack.ss_size =
154         pth_sksize_makecontext(context->stack, smx_context_stack_size);
155
156 #ifdef HAVE_VALGRIND_VALGRIND_H
157     context->valgrind_stack_id =
158         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
159                                 ((char *) context->uc.uc_stack.ss_sp) +
160                                 context->uc.uc_stack.ss_size);
161 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
162     ctx_addr.addr = context;
163     makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
164                 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
165   } else {
166     sysv_maestro_context = context;
167   }
168
169   return (smx_context_t) context;
170 }
171
172 static smx_context_t
173 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
174     void_pfn_smxprocess_t cleanup_func,
175     void *data)
176 {
177
178   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
179                                            code, argc, argv, cleanup_func,
180                                            data);
181
182 }
183
184 static void smx_ctx_sysv_free(smx_context_t context)
185 {
186
187   if (context) {
188
189 #ifdef HAVE_VALGRIND_VALGRIND_H
190     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
191                                context)->valgrind_stack_id);
192 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
193
194   }
195   smx_ctx_base_free(context);
196 }
197
198 static void smx_ctx_sysv_wrapper(int first, ...)
199
200   union u_ctx_addr ctx_addr;
201   smx_ctx_sysv_t context;
202
203   ctx_addr.intv[0] = first;
204   if (CTX_ADDR_LEN > 1) {
205     va_list ap;
206     int i;
207     va_start(ap, first);
208     for (i = 1; i < CTX_ADDR_LEN; i++)
209       ctx_addr.intv[i] = va_arg(ap, int);
210     va_end(ap);
211   }
212   context = ctx_addr.addr;
213   (context->super.code) (context->super.argc, context->super.argv);
214
215   simix_global->context_factory->stop((smx_context_t) context);
216 }
217
218 static void smx_ctx_sysv_stop_serial(smx_context_t context)
219 {
220   smx_ctx_base_stop(context);
221   smx_ctx_sysv_suspend_serial(context);
222 }
223
224 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
225 {
226   /* determine the next context */
227   smx_context_t next_context;
228   unsigned long int i = sysv_process_index++;
229
230   if (i < xbt_dynar_length(simix_global->process_to_run)) {
231     /* execute the next process */
232     XBT_DEBUG("Run next process");
233     next_context = xbt_dynar_get_as(
234         simix_global->process_to_run,i, smx_process_t)->context;
235   }
236   else {
237     /* all processes were run, return to maestro */
238     XBT_DEBUG("No more process to run");
239     next_context = (smx_context_t) sysv_maestro_context;
240   }
241   SIMIX_context_set_current(next_context);
242   swapcontext(&((smx_ctx_sysv_t) context)->uc,
243       &((smx_ctx_sysv_t) next_context)->uc);
244 }
245
246 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
247 {
248   smx_context_t context = first_process->context;
249   SIMIX_context_set_current(context);
250   swapcontext(&sysv_maestro_context->uc,
251       &((smx_ctx_sysv_t) context)->uc);
252 }
253
254 static void smx_ctx_sysv_runall_serial(void)
255 {
256   if (!xbt_dynar_is_empty(simix_global->process_to_run)) {
257     smx_process_t first_process =
258         xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
259     sysv_process_index = 1;
260
261     /* execute the first process */
262     smx_ctx_sysv_resume_serial(first_process);
263   }
264 }
265
266 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
267 {
268   smx_ctx_base_stop(context);
269   smx_ctx_sysv_suspend_parallel(context);
270 }
271
272 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
273 {
274 #ifdef CONTEXT_THREADS
275   /* determine the next context */
276   smx_process_t next_work = xbt_parmap_next(sysv_parmap);
277   smx_context_t next_context;
278   ucontext_t* next_stack;
279
280   if (next_work != NULL) {
281     /* there is a next process to resume */
282     XBT_DEBUG("Run next process");
283     next_context = next_work->context;
284     next_stack = &((smx_ctx_sysv_t) next_context)->uc;
285   }
286   else {
287     /* all processes were run, go to the barrier */
288     XBT_DEBUG("No more processes to run");
289     next_context = (smx_context_t) sysv_maestro_context;
290     unsigned long worker_id =
291         (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
292     next_stack = &sysv_workers_stacks[worker_id];
293   }
294
295   SIMIX_context_set_current(next_context);
296   swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);
297 #endif
298 }
299
300 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
301 {
302 #ifdef CONTEXT_THREADS
303   unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1);
304   xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);
305   ucontext_t* worker_stack = &sysv_workers_stacks[worker_id];
306
307   smx_context_t context = first_process->context;
308   SIMIX_context_set_current(context);
309   swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
310 #endif
311 }
312
313 static void smx_ctx_sysv_runall_parallel(void)
314 {
315 #ifdef CONTEXT_THREADS
316   sysv_threads_working = 0;
317   xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
318       simix_global->process_to_run);
319 #endif
320 }