Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement stride for parmap_apply.
[simgrid.git] / src / simix / smx_context_sysv.c
1 /* context_sysv - context switching with ucontexts from System V           */
2
3 /* Copyright (c) 2009-2013. 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   unsigned int parmap_index;
33 #ifdef HAVE_VALGRIND_VALGRIND_H
34   unsigned int valgrind_stack_id;       /* the valgrind stack id */
35 #endif
36   char stack[0];                /* the thread stack (must remain the last element of the structure) */
37 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
38
39 #ifdef CONTEXT_THREADS
40 static xbt_parmap_t sysv_parmap;
41 static smx_ctx_sysv_t* sysv_workers_context;   /* space to save the worker's context in each thread */
42 static unsigned long sysv_threads_working;     /* number of threads that have started their work */
43 static xbt_os_thread_key_t sysv_worker_id_key; /* thread-specific storage for the thread id */
44 #endif
45 static unsigned long sysv_process_index = 0;   /* index of the next process to run in the
46                                                 * list of runnable processes */
47 static smx_ctx_sysv_t sysv_maestro_context;
48
49 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
50 static smx_context_t
51 smx_ctx_sysv_create_context_sized(size_t structure_size,
52                                   xbt_main_func_t code, int argc,
53                                   char **argv,
54                                   void_pfn_smxprocess_t cleanup_func,
55                                   void *data);
56 static void smx_ctx_sysv_free(smx_context_t context);
57 static smx_context_t
58 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
59     void_pfn_smxprocess_t cleanup_func, void* data);
60
61 static void smx_ctx_sysv_wrapper(int count, ...);
62
63 static void smx_ctx_sysv_stop_serial(smx_context_t context);
64 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
65 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
66 static void smx_ctx_sysv_runall_serial(void);
67
68 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
69 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
70 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
71 static void smx_ctx_sysv_runall_parallel(void);
72
73 /* This is a bit paranoid about sizeof(smx_ctx_sysv_t) not being a multiple of
74  * sizeof(int), but it doesn't harm. */
75 #define CTX_ADDR_LEN                            \
76   (sizeof(smx_ctx_sysv_t) / sizeof(int) +       \
77    !!(sizeof(smx_ctx_sysv_t) % sizeof(int)))
78
79 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
80 {
81   smx_ctx_base_factory_init(factory);
82   XBT_VERB("Activating SYSV context factory");
83
84   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
85   (*factory)->create_context = smx_ctx_sysv_create_context;
86   /* Do not overload that method (*factory)->finalize */
87   (*factory)->free = smx_ctx_sysv_free;
88   (*factory)->name = "smx_sysv_context_factory";
89
90   if (SIMIX_context_is_parallel()) {
91 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
92     int nthreads = SIMIX_context_get_nthreads();
93     sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
94     sysv_workers_context = xbt_new(smx_ctx_sysv_t, nthreads);
95     sysv_maestro_context = NULL;
96     xbt_os_thread_key_create(&sysv_worker_id_key);
97     (*factory)->stop = smx_ctx_sysv_stop_parallel;
98     (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
99     (*factory)->runall = smx_ctx_sysv_runall_parallel;
100 #else
101     THROWF(arg_error, 0, "No thread support for parallel context execution");
102 #endif
103   } else {
104     (*factory)->stop = smx_ctx_sysv_stop_serial;
105     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
106     (*factory)->runall = smx_ctx_sysv_runall_serial;
107   }    
108 }
109
110 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
111
112 #ifdef CONTEXT_THREADS
113   if (sysv_parmap)
114     xbt_parmap_destroy(sysv_parmap);
115   xbt_free(sysv_workers_context);
116 #endif
117   return smx_ctx_base_factory_finalize(factory);
118 }
119
120 static smx_context_t
121 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
122                                   int argc, char **argv,
123                                   void_pfn_smxprocess_t cleanup_func,
124                                   void *data)
125 {
126   int ctx_addr[CTX_ADDR_LEN];
127   smx_ctx_sysv_t context =
128       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
129                                                                  code,
130                                                                  argc,
131                                                                  argv,
132                                                                  cleanup_func,
133                                                                  data);
134
135   /* if the user provided a function for the process then use it,
136      otherwise it is the context for maestro */
137   if (code) {
138
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(data != 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_process_t)((smx_context_t)context)->data)->name, &(context->uc), size);
176
177   return (smx_context_t) context;
178 }
179
180 static smx_context_t
181 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
182     void_pfn_smxprocess_t cleanup_func,
183     void *data)
184 {
185
186   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
187                                            code, argc, argv, cleanup_func,
188                                            data);
189
190 }
191
192 static void smx_ctx_sysv_free(smx_context_t context)
193 {
194
195   if (context) {
196
197 #ifdef HAVE_VALGRIND_VALGRIND_H
198     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
199                                context)->valgrind_stack_id);
200 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
201
202   }
203   smx_ctx_base_free(context);
204 }
205
206 static void smx_ctx_sysv_wrapper(int first, ...)
207
208   int ctx_addr[CTX_ADDR_LEN];
209   smx_ctx_sysv_t context;
210
211   ctx_addr[0] = first;
212   if (CTX_ADDR_LEN > 1) {
213     va_list ap;
214     int i;
215     va_start(ap, first);
216     for (i = 1; i < CTX_ADDR_LEN; i++)
217       ctx_addr[i] = va_arg(ap, int);
218     va_end(ap);
219   }
220   memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
221   (context->super.code) (context->super.argc, context->super.argv);
222
223   simix_global->context_factory->stop((smx_context_t) context);
224 }
225
226 static void smx_ctx_sysv_stop_serial(smx_context_t context)
227 {
228   smx_ctx_base_stop(context);
229   smx_ctx_sysv_suspend_serial(context);
230 }
231
232 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
233 {
234   /* determine the next context */
235   smx_context_t next_context;
236   unsigned long int i = sysv_process_index++;
237
238   if (i < xbt_dynar_length(simix_global->process_to_run)) {
239     /* execute the next process */
240     XBT_DEBUG("Run next process");
241     next_context = xbt_dynar_get_as(
242         simix_global->process_to_run,i, smx_process_t)->context;
243   }
244   else {
245     /* all processes were run, return to maestro */
246     XBT_DEBUG("No more process to run");
247     next_context = (smx_context_t) sysv_maestro_context;
248   }
249   SIMIX_context_set_current(next_context);
250   swapcontext(&((smx_ctx_sysv_t) context)->uc,
251       &((smx_ctx_sysv_t) next_context)->uc);
252 }
253
254 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
255 {
256   smx_context_t context = first_process->context;
257   SIMIX_context_set_current(context);
258   swapcontext(&sysv_maestro_context->uc,
259       &((smx_ctx_sysv_t) context)->uc);
260 }
261
262 static void smx_ctx_sysv_runall_serial(void)
263 {
264   smx_process_t first_process =
265       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
266   sysv_process_index = 1;
267
268   /* execute the first process */
269   smx_ctx_sysv_resume_serial(first_process);
270 }
271
272 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
273 {
274   smx_ctx_base_stop(context);
275   smx_ctx_sysv_suspend_parallel(context);
276 }
277
278 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
279 {
280 #ifdef CONTEXT_THREADS
281   /* determine the next context */
282   smx_process_t next_work;
283   smx_context_t next_context;
284   ucontext_t* next_stack;
285
286   next_work = xbt_parmap_next(sysv_parmap,
287                               &((smx_ctx_sysv_t) context)->parmap_index);
288   if (next_work != NULL) {
289     /* there is a next process to resume */
290     XBT_DEBUG("Run next process");
291     next_context = next_work->context;
292     next_stack = &((smx_ctx_sysv_t) next_context)->uc;
293     ((smx_ctx_sysv_t) next_context)->parmap_index =
294       ((smx_ctx_sysv_t) context)->parmap_index;
295   }
296   else {
297     /* all processes were run, go to the barrier */
298     XBT_DEBUG("No more processes to run");
299     unsigned long worker_id =
300         (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
301     next_context = (smx_context_t)sysv_workers_context[worker_id];
302     next_stack = &((smx_ctx_sysv_t)next_context)->uc;
303   }
304
305   SIMIX_context_set_current(next_context);
306   swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);
307 #endif
308 }
309
310 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
311 {
312 #ifdef CONTEXT_THREADS
313   unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1);
314   xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);
315   smx_ctx_sysv_t worker_context = (smx_ctx_sysv_t)SIMIX_context_self();
316   sysv_workers_context[worker_id] = worker_context;
317   ucontext_t* worker_stack = &worker_context->uc;
318   unsigned int index = 0;
319
320   first_process = xbt_parmap_next(sysv_parmap, &index);
321   if (first_process != NULL) {
322     smx_context_t context = first_process->context;
323     ((smx_ctx_sysv_t) context)->parmap_index = index;
324     SIMIX_context_set_current(context);
325     swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
326   }
327 #endif
328 }
329
330 static void smx_ctx_sysv_runall_parallel(void)
331 {
332 #ifdef CONTEXT_THREADS
333   sysv_threads_working = 0;
334   xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
335                    simix_global->process_to_run,
336                    1 + SIMIX_context_get_parallel_threshold() / SIMIX_context_get_nthreads(), 1);
337 #endif
338 }