Logo AND Algorithmique Numérique Distribuée

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