Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
18822c5545b25c7d60bf67ad1e10fc379787c895
[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 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                                   smx_process_t process);
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, smx_process_t process);
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_context = xbt_new(smx_ctx_sysv_t, nthreads);
94     sysv_maestro_context = NULL;
95     xbt_os_thread_key_create(&sysv_worker_id_key);
96     (*factory)->stop = smx_ctx_sysv_stop_parallel;
97     (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
98     (*factory)->runall = smx_ctx_sysv_runall_parallel;
99 #else
100     THROWF(arg_error, 0, "No thread support for parallel context execution");
101 #endif
102   } else {
103     (*factory)->stop = smx_ctx_sysv_stop_serial;
104     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
105     (*factory)->runall = smx_ctx_sysv_runall_serial;
106   }    
107 }
108
109 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
110
111 #ifdef CONTEXT_THREADS
112   if (sysv_parmap)
113     xbt_parmap_destroy(sysv_parmap);
114   xbt_free(sysv_workers_context);
115 #endif
116   return smx_ctx_base_factory_finalize(factory);
117 }
118
119 static smx_context_t
120 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
121                             void_pfn_smxprocess_t cleanup_func,
122                             smx_process_t process)
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(
127       sizeof(s_smx_ctx_sysv_t),
128       code,
129       argc,
130       argv,
131       cleanup_func,
132       process);
133
134   /* if the user provided a function for the process then use it,
135      otherwise it is the context for maestro */
136   if (code) {
137
138     context->stack = SIMIX_context_stack_new();
139     getcontext(&(context->uc));
140
141     context->uc.uc_link = NULL;
142
143     context->uc.uc_stack.ss_sp =
144         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
145
146     context->uc.uc_stack.ss_size =
147         pth_sksize_makecontext(context->stack, smx_context_stack_size);
148
149 #ifdef HAVE_VALGRIND_VALGRIND_H
150     context->valgrind_stack_id =
151         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
152                                 ((char *) context->uc.uc_stack.ss_sp) +
153                                 context->uc.uc_stack.ss_size);
154 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
155     memcpy(ctx_addr, &context, sizeof(smx_ctx_sysv_t));
156     switch (CTX_ADDR_LEN) {
157     case 1:
158       makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
159                   1, ctx_addr[0]);
160       break;
161     case 2:
162       makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
163                   2, ctx_addr[0], ctx_addr[1]);
164       break;
165     default:
166       xbt_die("Ucontexts are not supported on this arch yet (addr len = %zu/%zu = %zu)",
167               sizeof(smx_ctx_sysv_t), sizeof(int), CTX_ADDR_LEN);
168     }
169   } else {
170     if (process != NULL && sysv_maestro_context == NULL)
171       sysv_maestro_context = context;
172   }
173
174   if(MC_is_active() && code)
175     MC_new_stack_area(context, ((smx_context_t)context)->process->name,
176                       &(context->uc), sizeof(s_smx_ctx_sysv_t));
177
178   return (smx_context_t) context;
179 }
180
181 static void smx_ctx_sysv_free(smx_context_t context)
182 {
183
184   if (context) {
185
186 #ifdef HAVE_VALGRIND_VALGRIND_H
187     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
188                                context)->valgrind_stack_id);
189 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
190     SIMIX_context_stack_delete(((smx_ctx_sysv_t)context)->stack);
191   }
192   smx_ctx_base_free(context);
193 }
194
195 static void smx_ctx_sysv_wrapper(int first, ...)
196
197   int ctx_addr[CTX_ADDR_LEN];
198   smx_ctx_sysv_t context;
199
200   ctx_addr[0] = first;
201   if (CTX_ADDR_LEN > 1) {
202     va_list ap;
203     int i;
204     va_start(ap, first);
205     for (i = 1; i < CTX_ADDR_LEN; i++)
206       ctx_addr[i] = va_arg(ap, int);
207     va_end(ap);
208   }
209   memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
210   (context->super.code) (context->super.argc, context->super.argv);
211
212   simix_global->context_factory->stop((smx_context_t) context);
213 }
214
215 static void smx_ctx_sysv_stop_serial(smx_context_t context)
216 {
217   smx_ctx_base_stop(context);
218   smx_ctx_sysv_suspend_serial(context);
219 }
220
221 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
222 {
223   /* determine the next context */
224   smx_context_t next_context;
225   unsigned long int i = sysv_process_index++;
226
227   if (i < xbt_dynar_length(simix_global->process_to_run)) {
228     /* execute the next process */
229     XBT_DEBUG("Run next process");
230     next_context = xbt_dynar_get_as(
231         simix_global->process_to_run,i, smx_process_t)->context;
232   }
233   else {
234     /* all processes were run, return to maestro */
235     XBT_DEBUG("No more process to run");
236     next_context = (smx_context_t) sysv_maestro_context;
237   }
238   SIMIX_context_set_current(next_context);
239   swapcontext(&((smx_ctx_sysv_t) context)->uc,
240       &((smx_ctx_sysv_t) next_context)->uc);
241 }
242
243 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
244 {
245   smx_context_t context = first_process->context;
246   SIMIX_context_set_current(context);
247   swapcontext(&sysv_maestro_context->uc,
248       &((smx_ctx_sysv_t) context)->uc);
249 }
250
251 static void smx_ctx_sysv_runall_serial(void)
252 {
253   smx_process_t first_process =
254       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
255   sysv_process_index = 1;
256
257   /* execute the first process */
258   smx_ctx_sysv_resume_serial(first_process);
259 }
260
261 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
262 {
263   smx_ctx_base_stop(context);
264   smx_ctx_sysv_suspend_parallel(context);
265 }
266
267 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
268 {
269 #ifdef CONTEXT_THREADS
270   /* determine the next context */
271   smx_process_t next_work = xbt_parmap_next(sysv_parmap);
272   smx_context_t next_context;
273   ucontext_t* next_stack;
274
275   if (next_work != NULL) {
276     /* there is a next process to resume */
277     XBT_DEBUG("Run next process");
278     next_context = next_work->context;
279     next_stack = &((smx_ctx_sysv_t) next_context)->uc;
280   }
281   else {
282     /* all processes were run, go to the barrier */
283     XBT_DEBUG("No more processes to run");
284     unsigned long worker_id =
285         (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
286     next_context = (smx_context_t)sysv_workers_context[worker_id];
287     next_stack = &((smx_ctx_sysv_t)next_context)->uc;
288   }
289
290   SIMIX_context_set_current(next_context);
291   swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);
292 #endif
293 }
294
295 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
296 {
297 #ifdef CONTEXT_THREADS
298   unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1);
299   xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);
300   smx_ctx_sysv_t worker_context = (smx_ctx_sysv_t)SIMIX_context_self();
301   sysv_workers_context[worker_id] = worker_context;
302   ucontext_t* worker_stack = &worker_context->uc;
303
304   smx_context_t context = first_process->context;
305   SIMIX_context_set_current(context);
306   swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
307 #endif
308 }
309
310 static void smx_ctx_sysv_runall_parallel(void)
311 {
312 #ifdef CONTEXT_THREADS
313   sysv_threads_working = 0;
314   xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
315       simix_global->process_to_run);
316 #endif
317 }