Logo AND Algorithmique Numérique Distribuée

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