Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Include surf_resource.h, for surf_resource_name().
[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 smx_ctx_sysv_parmap;
30 static ucontext_t* smx_ctx_sysv_local_maestro_uc;    /* space to save maestro's stack in each thread */
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     smx_ctx_sysv_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 #else
89     THROWF(arg_error, 0, "No thread support for parallel context execution");
90 #endif
91   } else {
92     (*factory)->stop = smx_ctx_sysv_stop_serial;
93     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
94     (*factory)->runall = smx_ctx_sysv_runall_serial;
95   }    
96 }
97
98 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
99
100 #ifdef CONTEXT_THREADS
101   if (smx_ctx_sysv_parmap)
102     xbt_parmap_destroy(smx_ctx_sysv_parmap);
103   xbt_free(smx_ctx_sysv_local_maestro_uc);
104 #endif
105   return smx_ctx_base_factory_finalize(factory);
106 }
107
108 smx_context_t
109 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
110                                   int argc, char **argv,
111                                   void_pfn_smxprocess_t cleanup_func,
112                                   void *data)
113 {
114   union u_ctx_addr ctx_addr;
115   smx_ctx_sysv_t context =
116       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
117                                                                  code,
118                                                                  argc,
119                                                                  argv,
120                                                                  cleanup_func,
121                                                                  data);
122
123   /* if the user provided a function for the process then use it,
124      otherwise it is the context for maestro */
125   if (code) {
126
127     getcontext(&(context->uc));
128
129     context->uc.uc_link = NULL;
130
131     context->uc.uc_stack.ss_sp =
132         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
133
134     context->uc.uc_stack.ss_size =
135         pth_sksize_makecontext(context->stack, smx_context_stack_size);
136
137 #ifdef HAVE_VALGRIND_VALGRIND_H
138     context->valgrind_stack_id =
139         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
140                                 ((char *) context->uc.uc_stack.ss_sp) +
141                                 context->uc.uc_stack.ss_size);
142 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
143     ctx_addr.addr = context;
144     makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
145                 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
146   } else {
147     smx_ctx_sysv_maestro_context = context;
148   }
149
150   return (smx_context_t) context;
151 }
152
153 static smx_context_t
154 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
155     void_pfn_smxprocess_t cleanup_func,
156     void *data)
157 {
158
159   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
160                                            code, argc, argv, cleanup_func,
161                                            data);
162
163 }
164
165 void smx_ctx_sysv_free(smx_context_t context)
166 {
167
168   if (context) {
169
170 #ifdef HAVE_VALGRIND_VALGRIND_H
171     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
172                                context)->valgrind_stack_id);
173 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
174
175   }
176   smx_ctx_base_free(context);
177 }
178
179 void smx_ctx_sysv_wrapper(int first, ...)
180
181   union u_ctx_addr ctx_addr;
182   smx_ctx_sysv_t context;
183
184   ctx_addr.intv[0] = first;
185   if (CTX_ADDR_LEN > 1) {
186     va_list ap;
187     int i;
188     va_start(ap, first);
189     for (i = 1; i < CTX_ADDR_LEN; i++)
190       ctx_addr.intv[i] = va_arg(ap, int);
191     va_end(ap);
192   }
193   context = ctx_addr.addr;
194   (context->super.code) (context->super.argc, context->super.argv);
195
196   simix_global->context_factory->stop((smx_context_t) context);
197 }
198
199 static void smx_ctx_sysv_stop_serial(smx_context_t context)
200 {
201   smx_ctx_base_stop(context);
202   smx_ctx_sysv_suspend_serial(context);
203 }
204
205 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
206 {
207   /* determine the next context */
208   smx_context_t next_context;
209   unsigned long int i = smx_ctx_sysv_process_index++;
210
211   if (i < xbt_dynar_length(simix_global->process_to_run)) {
212     /* execute the next process */
213     XBT_DEBUG("Run next process");
214     next_context = xbt_dynar_get_as(
215         simix_global->process_to_run,i, smx_process_t)->context;
216   }
217   else {
218     /* all processes were run, return to maestro */
219     XBT_DEBUG("No more process to run");
220     next_context = (smx_context_t) smx_ctx_sysv_maestro_context;
221   }
222   SIMIX_context_set_current(next_context);
223   swapcontext(&((smx_ctx_sysv_t) context)->uc,
224       &((smx_ctx_sysv_t) next_context)->uc);
225 }
226
227 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
228 {
229   smx_context_t context = first_process->context;
230   SIMIX_context_set_current(context);
231   swapcontext(&smx_ctx_sysv_maestro_context->uc,
232       &((smx_ctx_sysv_t) context)->uc);
233 }
234
235 static void smx_ctx_sysv_runall_serial(void)
236 {
237   if (!xbt_dynar_is_empty(simix_global->process_to_run)) {
238     smx_process_t first_process =
239         xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
240     smx_ctx_sysv_process_index = 1;
241
242     /* execute the first process */
243     smx_ctx_sysv_resume_serial(first_process);
244   }
245 }
246
247 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
248 {
249   smx_ctx_base_stop(context);
250   smx_ctx_sysv_suspend_parallel(context);
251 }
252
253 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
254 {
255 #ifdef CONTEXT_THREADS
256   /* determine the next context */
257   smx_process_t next_work = xbt_parmap_next(smx_ctx_sysv_parmap);
258   smx_context_t next_context;
259   ucontext_t* next_uc;
260
261   if (next_work != NULL) {
262     /* there is a next process to resume */
263     XBT_DEBUG("Run next process");
264     next_context = next_work->context;
265     next_uc = &((smx_ctx_sysv_t) next_context)->uc;
266   }
267   else {
268     /* all processes were run, go to the barrier */
269     XBT_DEBUG("No more processes to run");
270     next_context = (smx_context_t) smx_ctx_sysv_maestro_context;
271     unsigned long worker_id = xbt_parmap_get_worker_id(smx_ctx_sysv_parmap);
272     next_uc = &smx_ctx_sysv_local_maestro_uc[worker_id];
273   }
274
275   SIMIX_context_set_current(next_context);
276   swapcontext(&((smx_ctx_sysv_t) context)->uc, next_uc);
277 #endif
278 }
279
280 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
281 {
282 #ifdef CONTEXT_THREADS
283   unsigned long worker_id = xbt_parmap_get_worker_id(smx_ctx_sysv_parmap);
284   ucontext_t* local_maestro_uc = &smx_ctx_sysv_local_maestro_uc[worker_id];
285
286   smx_context_t context = first_process->context;
287   SIMIX_context_set_current(context);
288   swapcontext(local_maestro_uc, &((smx_ctx_sysv_t) context)->uc);
289 #endif
290 }
291
292 static void smx_ctx_sysv_runall_parallel(void)
293 {
294 #ifdef CONTEXT_THREADS
295   xbt_parmap_apply(smx_ctx_sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
296       simix_global->process_to_run);
297 #endif
298 }