Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix: do not hardcode the number of workers in the parmap, use the command line...
[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 #include "win32_ucontext.c"
23 #else
24 #include "ucontext.h"
25 #endif
26
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
28
29 #ifdef CONTEXT_THREADS
30 static xbt_parmap_t parmap;
31 #endif
32
33 static smx_context_t
34 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
35     void_pfn_smxprocess_t cleanup_func, void* data);
36
37 static void smx_ctx_sysv_wrapper(int count, ...);
38
39 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
40  * but it doesn't harm. */
41 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
42 union u_ctx_addr {
43   void *addr;
44   int intv[CTX_ADDR_LEN];
45 };
46 #if (CTX_ADDR_LEN == 1)
47 #  define CTX_ADDR_SPLIT(u) (u).intv[0]
48 #elif (CTX_ADDR_LEN == 2)
49 #  define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
50 #else
51 #  error Your architecture is not supported yet
52 #endif
53
54 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
55 {
56   smx_ctx_base_factory_init(factory);
57   XBT_VERB("Activating SYSV context factory");
58
59   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
60   (*factory)->create_context = smx_ctx_sysv_create_context;
61   /* Do not overload that method (*factory)->finalize */
62   (*factory)->free = smx_ctx_sysv_free;
63   (*factory)->stop = smx_ctx_sysv_stop;
64   (*factory)->suspend = smx_ctx_sysv_suspend;
65   (*factory)->name = "smx_sysv_context_factory";
66
67   if (SIMIX_context_is_parallel()) {
68 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
69     parmap = xbt_parmap_new(2);
70     (*factory)->runall = smx_ctx_sysv_runall_parallel;
71     (*factory)->self = smx_ctx_sysv_self_parallel;
72     (*factory)->get_thread_id = smx_ctx_sysv_get_thread_id;
73 #else
74     THROW0(arg_error, 0, "No thread support for parallel context execution");
75 #endif
76   }else{
77     (*factory)->runall = smx_ctx_sysv_runall;
78   }    
79 }
80
81 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
82
83 #ifdef CONTEXT_THREADS
84   if(parmap)
85     xbt_parmap_destroy(parmap);
86 #endif
87   return smx_ctx_base_factory_finalize(factory);
88 }
89
90 smx_context_t
91 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
92                                   int argc, char **argv,
93                                   void_pfn_smxprocess_t cleanup_func,
94                                   void *data)
95 {
96   union u_ctx_addr ctx_addr;
97   smx_ctx_sysv_t context =
98       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
99                                                                  code,
100                                                                  argc,
101                                                                  argv,
102                                                                  cleanup_func,
103                                                                  data);
104
105   /* If the user provided a function for the process then use it
106      otherwise is the context for maestro */
107   if (code) {
108
109     int res;
110     res = getcontext(&(context->uc));
111     xbt_assert2(res == 0,
112                 "Error in context saving: %d (%s)", errno,
113                 strerror(errno));
114
115     context->uc.uc_link = NULL;
116
117     context->uc.uc_stack.ss_sp =
118         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
119
120     context->uc.uc_stack.ss_size =
121         pth_sksize_makecontext(context->stack, smx_context_stack_size);
122
123 #ifdef HAVE_VALGRIND_VALGRIND_H
124     context->valgrind_stack_id =
125         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
126                                 ((char *) context->uc.uc_stack.ss_sp) +
127                                 context->uc.uc_stack.ss_size);
128 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
129     ctx_addr.addr = context;
130     makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
131                 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
132   }else{
133     maestro_context = context;
134   }
135
136   return (smx_context_t) context;
137
138 }
139
140 static smx_context_t
141 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
142     void_pfn_smxprocess_t cleanup_func,
143     void *data)
144 {
145
146   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
147                                            code, argc, argv, cleanup_func,
148                                            data);
149
150 }
151
152 void smx_ctx_sysv_free(smx_context_t context)
153 {
154
155   if (context) {
156
157 #ifdef HAVE_VALGRIND_VALGRIND_H
158     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
159                                context)->valgrind_stack_id);
160 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
161
162   }
163   smx_ctx_base_free(context);
164 }
165
166 void smx_ctx_sysv_stop(smx_context_t context)
167 {
168   smx_ctx_base_stop(context);
169   smx_ctx_sysv_suspend(context);
170 }
171
172 void smx_ctx_sysv_wrapper(int first, ...)
173
174   union u_ctx_addr ctx_addr;
175   smx_ctx_sysv_t context;
176
177   ctx_addr.intv[0] = first;
178   if (CTX_ADDR_LEN > 1) {
179     va_list ap;
180     int i;
181     va_start(ap, first);
182     for(i = 1; i < CTX_ADDR_LEN; i++)
183       ctx_addr.intv[i] = va_arg(ap, int);
184     va_end(ap);
185   }
186   context = ctx_addr.addr;
187   (context->super.code) (context->super.argc, context->super.argv);
188
189   smx_ctx_sysv_stop((smx_context_t) context);
190 }
191
192 void smx_ctx_sysv_suspend(smx_context_t context)
193 {
194   smx_current_context = (smx_context_t)maestro_context;
195   int rv;
196   rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
197
198   xbt_assert0((rv == 0), "Context swapping failure");
199 }
200
201 void smx_ctx_sysv_resume(smx_context_t context)
202 {
203   smx_current_context = context; 
204   int rv;
205   rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
206
207   xbt_assert0((rv == 0), "Context swapping failure");
208 }
209
210 void smx_ctx_sysv_runall(xbt_dynar_t processes)
211 {
212   smx_process_t process;
213   unsigned int cursor;
214
215   xbt_dynar_foreach(processes, cursor, process) {
216     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
217     smx_ctx_sysv_resume(process->context);
218   }
219   xbt_dynar_reset(processes);
220 }
221
222 void smx_ctx_sysv_resume_parallel(smx_process_t process)
223 {
224   smx_context_t context = process->context;
225   smx_current_context = (smx_context_t)context;
226   int rv;
227   rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
228   smx_current_context = (smx_context_t)maestro_context;
229
230   xbt_assert0((rv == 0), "Context swapping failure");
231 }
232
233 void smx_ctx_sysv_runall_parallel(xbt_dynar_t processes)
234 {
235 #ifdef CONTEXT_THREADS
236   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, processes);
237 #endif
238   xbt_dynar_reset(processes);
239 }
240
241 smx_context_t smx_ctx_sysv_self_parallel(void)
242 {
243   /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
244   return self_context ? self_context : (smx_context_t) maestro_context;*/
245   return smx_current_context;
246 }
247
248 int smx_ctx_sysv_get_thread_id(void)
249 {
250   return (int)(unsigned long)xbt_os_thread_get_extra_data();
251 }