Logo AND Algorithmique Numérique Distribuée

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