Logo AND Algorithmique Numérique Distribuée

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