Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b91dfd67ae693edaf636cee2e8ab2164a5b0e294
[simgrid.git] / src / simix / smx_context_raw.c
1 /* context_raw - 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 "xbt/threadpool.h"
10 #include "simix/private.h"
11
12 #ifdef HAVE_VALGRIND_VALGRIND_H
13 #  include <valgrind/valgrind.h>
14 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
15
16 #ifdef _XBT_WIN32
17 #include "win32_ucontext.h"
18 #include "win32_ucontext.c"
19 #else
20 #include "ucontext.h"
21 #endif
22
23 /* lower this if you want to reduce the memory consumption  */
24 #ifndef CONTEXT_STACK_SIZE      /* allow lua to override this */
25 #define CONTEXT_STACK_SIZE 128*1024
26 #endif                          /*CONTEXT_STACK_SIZE */
27
28
29 typedef char * raw_stack_t;
30 typedef void (*rawctx_entry_point_t)(void *);
31
32 typedef struct s_smx_ctx_raw {
33   s_smx_ctx_base_t super;       /* Fields of super implementation */
34   char *malloced_stack; /* malloced area containing the stack */
35   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
36   raw_stack_t old_stack_top; /* to whom I should return the control */
37 #ifdef HAVE_VALGRIND_VALGRIND_H
38   unsigned int valgrind_stack_id;       /* the valgrind stack id */
39 #endif
40 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
41
42 smx_ctx_raw_t maestro_raw_context;
43
44 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
45                                    rawctx_entry_point_t entry_point, void* arg);
46 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
47
48 #ifdef PROCESSOR_i686
49 __asm__ (
50    ".text\n"
51    ".globl raw_makecontext\n"
52    ".type raw_makecontext,@function\n"
53    "raw_makecontext:\n"
54    "   movl 4(%esp),%eax\n"   /* stack */
55    "   addl 8(%esp),%eax\n"   /* size  */
56    "   movl 12(%esp),%ecx\n"  /* func  */
57    "   movl 16(%esp),%edx\n"  /* arg   */
58    "   movl %edx, -4(%eax)\n"
59    "   movl $0,   -8(%eax)\n" /* @return for func */
60    "   movl %ecx,-12(%eax)\n"
61    "   movl $0,  -16(%eax)\n" /* ebp */
62    "   movl $0,  -20(%eax)\n" /* ebx */
63    "   movl $0,  -24(%eax)\n" /* esi */
64    "   movl $0,  -28(%eax)\n" /* edi */
65    "   subl $28,%eax\n"
66    "   retl\n"
67 );
68
69 __asm__ (
70    ".text\n"
71    ".globl raw_swapcontext\n"
72    ".type raw_swapcontext,@function\n"
73    "raw_swapcontext:\n"
74    "   movl 4(%esp),%eax\n" /* old */
75    "   movl 8(%esp),%edx\n" /* new */
76    "   pushl %ebp\n"
77    "   pushl %ebx\n"
78    "   pushl %esi\n"
79    "   pushl %edi\n"
80    "   movl %esp,(%eax)\n"
81    "   movl %edx,%esp\n"
82    "   popl %edi\n"
83    "   popl %esi\n"
84    "   popl %ebx\n"
85    "   popl %ebp\n"
86    "   retl\n"
87 );
88 #elif PROCESSOR_x86_64
89 __asm__ (
90    ".text\n"
91    ".globl raw_makecontext\n"
92    ".type raw_makecontext,@function\n"
93    "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
94    "   movq %rdi,%rax\n"      /* stack */
95    "   addq %rsi,%rax\n"      /* size  */
96    "   movq $0,   -8(%rax)\n" /* @return for func */
97    "   movq %rdx,-16(%rax)\n" /* func */
98    "   movq %rcx,-24(%rax)\n" /* arg/rdi */
99    "   movq $0,  -32(%rax)\n" /* rsi */
100    "   movq $0,  -40(%rax)\n" /* rdx */
101    "   movq $0,  -48(%rax)\n" /* rcx */
102    "   movq $0,  -56(%rax)\n" /* r8  */
103    "   movq $0,  -64(%rax)\n" /* r9  */
104    "   movq $0,  -72(%rax)\n" /* rbp */
105    "   movq $0,  -80(%rax)\n" /* rbx */
106    "   movq $0,  -88(%rax)\n" /* r12 */
107    "   movq $0,  -96(%rax)\n" /* r13 */
108    "   movq $0, -104(%rax)\n" /* r14 */
109    "   movq $0, -112(%rax)\n" /* r15 */
110    "   subq $112,%rax\n"
111    "   retq\n"
112 );
113
114 __asm__ (
115    ".text\n"
116    ".globl raw_swapcontext\n"
117    ".type raw_swapcontext,@function\n"
118    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
119    "   pushq %rdi\n"
120    "   pushq %rsi\n"
121    "   pushq %rdx\n"
122    "   pushq %rcx\n"
123    "   pushq %r8\n"
124    "   pushq %r9\n"
125    "   pushq %rbp\n"
126    "   pushq %rbx\n"
127    "   pushq %r12\n"
128    "   pushq %r13\n"
129    "   pushq %r14\n"
130    "   pushq %r15\n"
131    "   movq %rsp,(%rdi)\n" /* old */
132    "   movq %rsi,%rsp\n" /* new */
133    "   popq %r15\n"
134    "   popq %r14\n"
135    "   popq %r13\n"
136    "   popq %r12\n"
137    "   popq %rbx\n"
138    "   popq %rbp\n"
139    "   popq %r9\n"
140    "   popq %r8\n"
141    "   popq %rcx\n"
142    "   popq %rdx\n"
143    "   popq %rsi\n"
144    "   popq %rdi\n"
145    "   retq\n"
146 );
147 #else
148 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
149                             rawctx_entry_point_t entry_point, void* arg) {
150    THROW_UNIMPLEMENTED;
151 }
152
153 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
154    THROW_UNIMPLEMENTED;
155 }
156
157 #endif
158
159 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
160
161 static xbt_tpool_t tpool;
162
163 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
164
165
166 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
167
168   if(tpool)
169     xbt_tpool_destroy(tpool);
170   return smx_ctx_base_factory_finalize(factory);
171 }
172
173 static smx_context_t
174 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
175     int argc, char **argv,
176     void_pfn_smxprocess_t cleanup_func,
177     void *data)
178 {
179
180   smx_ctx_raw_t context =
181       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
182           code,
183           argc,
184           argv,
185           cleanup_func,
186           data);
187
188   /* If the user provided a function for the process then use it
189      otherwise is the context for maestro */
190      if (code) {
191        context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
192        context->stack_top =
193            raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
194                (void(*)(void*))smx_ctx_raw_wrapper,context);
195
196 #ifdef HAVE_VALGRIND_VALGRIND_H
197        context->valgrind_stack_id =
198            VALGRIND_STACK_REGISTER(context->malloced_stack,
199                context->malloced_stack + CONTEXT_STACK_SIZE);
200 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
201
202      }else{
203        maestro_raw_context = context;
204      }
205
206      return (smx_context_t) context;
207
208 }
209
210 static smx_context_t
211 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
212     void_pfn_smxprocess_t cleanup_func,
213     void *data)
214 {
215
216   return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
217       code, argc, argv, cleanup_func,
218       data);
219
220 }
221
222 static void smx_ctx_raw_free(smx_context_t context)
223 {
224
225   if (context) {
226
227 #ifdef HAVE_VALGRIND_VALGRIND_H
228     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
229         context)->valgrind_stack_id);
230 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
231
232   }
233   smx_ctx_base_free(context);
234 }
235
236 static void smx_ctx_raw_suspend(smx_context_t context)
237 {
238   smx_current_context = (smx_context_t)maestro_raw_context;
239   raw_swapcontext(
240       &((smx_ctx_raw_t) context)->stack_top,
241       ((smx_ctx_raw_t) context)->old_stack_top);
242 }
243
244 static void smx_ctx_raw_stop(smx_context_t context)
245 {
246   smx_ctx_base_stop(context);
247   smx_ctx_raw_suspend(context);
248 }
249
250 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
251
252   (context->super.code) (context->super.argc, context->super.argv);
253
254   smx_ctx_raw_stop((smx_context_t) context);
255 }
256
257 static void smx_ctx_raw_resume(smx_context_t context)
258 {
259   smx_current_context = context; 
260   raw_swapcontext(
261       &((smx_ctx_raw_t) context)->old_stack_top,
262       ((smx_ctx_raw_t) context)->stack_top);
263 }
264
265 static void smx_ctx_raw_runall(xbt_swag_t processes)
266 {
267   smx_process_t process;
268
269   while ((process = xbt_swag_extract(processes)))
270     smx_ctx_raw_resume(process->context);
271 }
272
273 static void smx_ctx_raw_resume_parallel(smx_context_t context)
274 {
275   xbt_os_thread_set_extra_data(context);
276   raw_swapcontext(
277       &((smx_ctx_raw_t) context)->old_stack_top,
278       ((smx_ctx_raw_t) context)->stack_top);
279   xbt_os_thread_set_extra_data(NULL);
280 }
281
282 static void smx_ctx_raw_runall_parallel(xbt_swag_t processes)
283 {
284   smx_process_t process;
285   while((process = xbt_swag_extract(processes))){
286     xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, process->context);
287   }
288   xbt_tpool_wait_all(tpool);
289 }
290
291 static smx_context_t smx_ctx_raw_self_parallel(void)
292 {
293   smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
294   return self_context ? self_context : (smx_context_t) maestro_raw_context;
295 }
296
297 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
298 {
299   INFO0("Using raw contexts. Because the glibc is just not good enough for us.");
300   smx_ctx_base_factory_init(factory);
301
302   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
303   (*factory)->create_context = smx_ctx_raw_create_context;
304   /* Do not overload that method (*factory)->finalize */
305   (*factory)->free = smx_ctx_raw_free;
306   (*factory)->stop = smx_ctx_raw_stop;
307   (*factory)->suspend = smx_ctx_raw_suspend;
308   (*factory)->name = "smx_raw_context_factory";
309
310   if(_surf_parallel_contexts){
311 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
312     tpool = xbt_tpool_new(2, 10);
313     (*factory)->runall = smx_ctx_raw_runall_parallel;
314     (*factory)->self = smx_ctx_raw_self_parallel;
315 #else
316     THROW0(arg_error, 0, "No thread support for parallel context execution");
317 #endif
318   }else{
319     (*factory)->runall = smx_ctx_raw_runall;
320   }
321 }