Logo AND Algorithmique Numérique Distribuée

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