Logo AND Algorithmique Numérique Distribuée

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