Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af7c4ce0d39b7fe56e9184be3f6d620966f9e884
[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 "simix/private.h"
10 #include "xbt/parmap.h"
11
12
13 #ifdef HAVE_VALGRIND_VALGRIND_H
14 #  include <valgrind/valgrind.h>
15 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
16
17 #ifdef _XBT_WIN32
18 #include "win32_ucontext.h"
19 #include "win32_ucontext.c"
20 #else
21 #include "ucontext.h"
22 #endif
23
24 typedef char * raw_stack_t;
25 typedef void (*rawctx_entry_point_t)(void *);
26
27 typedef struct s_smx_ctx_raw {
28   s_smx_ctx_base_t super;       /* Fields of super implementation */
29   char *malloced_stack; /* malloced area containing the stack */
30   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
31   raw_stack_t old_stack_top; /* to whom I should return the control */
32 #ifdef HAVE_VALGRIND_VALGRIND_H
33   unsigned int valgrind_stack_id;       /* the valgrind stack id */
34 #endif
35 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
36
37 smx_ctx_raw_t maestro_raw_context;
38
39 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
40                                    rawctx_entry_point_t entry_point, void* arg);
41 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
42
43 #ifdef PROCESSOR_i686
44 __asm__ (
45    ".text\n"
46    ".globl raw_makecontext\n"
47    ".type raw_makecontext,@function\n"
48    "raw_makecontext:\n"
49    "   movl 4(%esp),%eax\n"   /* stack */
50    "   addl 8(%esp),%eax\n"   /* size  */
51    "   movl 12(%esp),%ecx\n"  /* func  */
52    "   movl 16(%esp),%edx\n"  /* arg   */
53    "   movl %edx, -4(%eax)\n"
54    "   movl $0,   -8(%eax)\n" /* @return for func */
55    "   movl %ecx,-12(%eax)\n"
56    "   movl $0,  -16(%eax)\n" /* ebp */
57    "   movl $0,  -20(%eax)\n" /* ebx */
58    "   movl $0,  -24(%eax)\n" /* esi */
59    "   movl $0,  -28(%eax)\n" /* edi */
60    "   subl $28,%eax\n"
61    "   retl\n"
62 );
63
64 __asm__ (
65    ".text\n"
66    ".globl raw_swapcontext\n"
67    ".type raw_swapcontext,@function\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    ".text\n"
86    ".globl raw_makecontext\n"
87    ".type raw_makecontext,@function\n"
88    "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
89    "   movq %rdi,%rax\n"      /* stack */
90    "   addq %rsi,%rax\n"      /* size  */
91    "   movq $0,   -8(%rax)\n" /* @return for func */
92    "   movq %rdx,-16(%rax)\n" /* func */
93    "   movq %rcx,-24(%rax)\n" /* arg/rdi */
94    "   movq $0,  -32(%rax)\n" /* rsi */
95    "   movq $0,  -40(%rax)\n" /* rdx */
96    "   movq $0,  -48(%rax)\n" /* rcx */
97    "   movq $0,  -56(%rax)\n" /* r8  */
98    "   movq $0,  -64(%rax)\n" /* r9  */
99    "   movq $0,  -72(%rax)\n" /* rbp */
100    "   movq $0,  -80(%rax)\n" /* rbx */
101    "   movq $0,  -88(%rax)\n" /* r12 */
102    "   movq $0,  -96(%rax)\n" /* r13 */
103    "   movq $0, -104(%rax)\n" /* r14 */
104    "   movq $0, -112(%rax)\n" /* r15 */
105    "   subq $112,%rax\n"
106    "   retq\n"
107 );
108
109 __asm__ (
110    ".text\n"
111    ".globl raw_swapcontext\n"
112    ".type raw_swapcontext,@function\n"
113    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
114    "   pushq %rdi\n"
115    "   pushq %rsi\n"
116    "   pushq %rdx\n"
117    "   pushq %rcx\n"
118    "   pushq %r8\n"
119    "   pushq %r9\n"
120    "   pushq %rbp\n"
121    "   pushq %rbx\n"
122    "   pushq %r12\n"
123    "   pushq %r13\n"
124    "   pushq %r14\n"
125    "   pushq %r15\n"
126    "   movq %rsp,(%rdi)\n" /* old */
127    "   movq %rsi,%rsp\n" /* new */
128    "   popq %r15\n"
129    "   popq %r14\n"
130    "   popq %r13\n"
131    "   popq %r12\n"
132    "   popq %rbx\n"
133    "   popq %rbp\n"
134    "   popq %r9\n"
135    "   popq %r8\n"
136    "   popq %rcx\n"
137    "   popq %rdx\n"
138    "   popq %rsi\n"
139    "   popq %rdi\n"
140    "   retq\n"
141 );
142 #else
143 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
144                             rawctx_entry_point_t entry_point, void* arg) {
145    THROW_UNIMPLEMENTED;
146 }
147
148 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
149    THROW_UNIMPLEMENTED;
150 }
151
152 #endif
153
154 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
155
156 #ifdef CONTEXT_THREADS
157 static xbt_parmap_t parmap;
158 #endif
159
160 static smx_context_factory_t raw_factory;
161
162 #include "xbt/xbt_os_time.h"
163 static xbt_os_timer_t timer;
164 static double totaltime = 0;
165
166
167 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
168
169 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
170
171   XBT_VERB("Total User Time: %lf", totaltime);
172 #ifdef CONTEXT_THREADS
173   if(parmap)
174       xbt_parmap_destroy(parmap);
175 #endif
176   return smx_ctx_base_factory_finalize(factory);
177 }
178
179
180 static smx_context_t
181 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
182     void_pfn_smxprocess_t cleanup_func,
183     void *data)
184 {
185
186   smx_ctx_raw_t context =
187       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
188           sizeof(s_smx_ctx_raw_t),
189           code,
190           argc,
191           argv,
192           cleanup_func,
193           data);
194
195   /* If the user provided a function for the process then use it
196      otherwise is the context for maestro */
197      if (code) {
198        context->malloced_stack = xbt_malloc0(smx_context_stack_size);
199        context->stack_top =
200            raw_makecontext(context->malloced_stack, smx_context_stack_size,
201                (void(*)(void*))smx_ctx_raw_wrapper,context);
202
203 #ifdef HAVE_VALGRIND_VALGRIND_H
204        context->valgrind_stack_id =
205            VALGRIND_STACK_REGISTER(context->malloced_stack,
206                context->malloced_stack + smx_context_stack_size);
207 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
208
209      }else{
210        maestro_raw_context = context;
211      }
212
213      return (smx_context_t) context;
214 }
215
216 static void smx_ctx_raw_free(smx_context_t context)
217 {
218
219   if (context) {
220
221 #ifdef HAVE_VALGRIND_VALGRIND_H
222     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
223         context)->valgrind_stack_id);
224 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
225
226     free(((smx_ctx_raw_t)context)->malloced_stack);
227   }
228   smx_ctx_base_free(context);
229 }
230
231 static void smx_ctx_raw_suspend(smx_context_t context)
232 {
233   smx_current_context = (smx_context_t)maestro_raw_context;
234   raw_swapcontext(
235       &((smx_ctx_raw_t) context)->stack_top,
236       ((smx_ctx_raw_t) context)->old_stack_top);
237 }
238
239 static void smx_ctx_raw_stop(smx_context_t context)
240 {
241   smx_ctx_base_stop(context);
242   smx_ctx_raw_suspend(context);
243 }
244
245 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
246
247   (context->super.code) (context->super.argc, context->super.argv);
248
249   smx_ctx_raw_stop((smx_context_t) context);
250 }
251
252 static void smx_ctx_raw_resume(smx_process_t process)
253 {
254   smx_ctx_raw_t context = (smx_ctx_raw_t)process->context;
255   smx_current_context = (smx_context_t)context;
256   raw_swapcontext(
257       &((smx_ctx_raw_t) context)->old_stack_top,
258       ((smx_ctx_raw_t) context)->stack_top);
259 }
260
261 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
262 {
263   smx_process_t process;
264   unsigned int cursor;
265
266   xbt_dynar_foreach(processes, cursor, process) {
267     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
268     xbt_os_timer_start(timer);
269     smx_ctx_raw_resume(process);
270     xbt_os_timer_stop(timer);
271     totaltime += xbt_os_timer_elapsed(timer);
272   }
273   xbt_dynar_reset(processes);
274 }
275
276 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
277 {
278 #ifdef CONTEXT_THREADS
279   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_raw_resume, processes);
280 #endif
281   xbt_dynar_reset(processes);
282 }
283
284 static smx_context_t smx_ctx_raw_self_parallel(void)
285 {
286   return smx_current_context;
287 }
288
289 static int smx_ctx_raw_get_thread_id(){
290   return (int)(unsigned long)xbt_os_thread_get_extra_data();
291 }
292
293 static void smx_ctx_raw_runall(xbt_dynar_t processes)
294 {
295   if (xbt_dynar_length(processes) >= SIMIX_context_get_parallel_threshold()) {
296     XBT_DEBUG("Runall // %lu", xbt_dynar_length(processes));
297     raw_factory->self = smx_ctx_raw_self_parallel;
298     raw_factory->get_thread_id = smx_ctx_raw_get_thread_id;
299     smx_ctx_raw_runall_parallel(processes);
300   } else {
301     XBT_DEBUG("Runall serial %lu", xbt_dynar_length(processes));
302     raw_factory->self = smx_ctx_base_self;
303     raw_factory->get_thread_id = smx_ctx_base_get_thread_id;
304     smx_ctx_raw_runall_serial(processes);
305   }
306 }
307
308 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
309 {
310   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
311   smx_ctx_base_factory_init(factory);
312
313   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
314   (*factory)->create_context = smx_ctx_raw_create_context;
315   /* Do not overload that method (*factory)->finalize */
316   (*factory)->free = smx_ctx_raw_free;
317   (*factory)->stop = smx_ctx_raw_stop;
318   (*factory)->suspend = smx_ctx_raw_suspend;
319   (*factory)->name = "smx_raw_context_factory";
320 #ifdef CONTEXT_THREADS
321   parmap = xbt_parmap_new(2);
322 #endif
323   if (SIMIX_context_is_parallel()) {
324     if (SIMIX_context_get_parallel_threshold() > 1) {
325       /* choose dynamically */
326       (*factory)->runall = smx_ctx_raw_runall;
327     }
328     else {
329       /* always parallel */
330       (*factory)->self = smx_ctx_raw_self_parallel;
331       (*factory)->get_thread_id = smx_ctx_raw_get_thread_id;
332       (*factory)->runall = smx_ctx_raw_runall_parallel;
333     }
334   }
335   else {
336     /* always serial */
337     (*factory)->self = smx_ctx_base_self;
338     (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
339     (*factory)->runall = smx_ctx_raw_runall_serial;
340   }
341   raw_factory = *factory;
342
343   timer = xbt_os_timer_new();
344 }