Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ASM on mac is not the same as linux.
[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 typedef char * raw_stack_t;
18 typedef void (*rawctx_entry_point_t)(void *);
19
20 typedef struct s_smx_ctx_raw {
21   s_smx_ctx_base_t super;       /* Fields of super implementation */
22   char *malloced_stack; /* malloced area containing the stack */
23   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
24   raw_stack_t old_stack_top; /* to whom I should return the control */
25 #ifdef HAVE_VALGRIND_VALGRIND_H
26   unsigned int valgrind_stack_id;       /* the valgrind stack id */
27 #endif
28 #ifdef TIME_BENCH
29   unsigned int thread;  /* Just for measuring purposes */
30 #endif
31 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
32
33 smx_ctx_raw_t maestro_raw_context;
34
35 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
36                                    rawctx_entry_point_t entry_point, void* arg);
37 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
38
39 #ifdef PROCESSOR_i686
40 __asm__ (
41 #ifdef APPLE
42    ".text\n"
43    ".globl _raw_makecontext\n"
44    "_raw_makecontext:\n"
45 #else
46    ".text\n"
47    ".globl raw_makecontext\n"
48    ".type raw_makecontext,@function\n"
49    "raw_makecontext:\n"
50 #endif
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 #ifdef APPLE
68    ".text\n"
69    ".globl _raw_swapcontext\n"
70    "_raw_swapcontext:\n"
71 #else
72    ".text\n"
73    ".globl raw_swapcontext\n"
74    ".type raw_swapcontext,@function\n"
75    "raw_swapcontext:\n"
76 #endif
77    "   movl 4(%esp),%eax\n" /* old */
78    "   movl 8(%esp),%edx\n" /* new */
79    "   pushl %ebp\n"
80    "   pushl %ebx\n"
81    "   pushl %esi\n"
82    "   pushl %edi\n"
83    "   movl %esp,(%eax)\n"
84    "   movl %edx,%esp\n"
85    "   popl %edi\n"
86    "   popl %esi\n"
87    "   popl %ebx\n"
88    "   popl %ebp\n"
89    "   retl\n"
90 );
91 #elif PROCESSOR_x86_64
92 __asm__ (
93 #ifdef APPLE
94    ".text\n"
95    ".globl _raw_makecontext\n"
96    "_raw_makecontext:\n"
97 #else
98    ".text\n"
99    ".globl raw_makecontext\n"
100    ".type raw_makecontext,@function\n"
101    "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
102 #endif
103    "   movq %rdi,%rax\n"      /* stack */
104    "   addq %rsi,%rax\n"      /* size  */
105    "   movq $0,   -8(%rax)\n" /* @return for func */
106    "   movq %rdx,-16(%rax)\n" /* func */
107    "   movq %rcx,-24(%rax)\n" /* arg/rdi */
108    "   movq $0,  -32(%rax)\n" /* rsi */
109    "   movq $0,  -40(%rax)\n" /* rdx */
110    "   movq $0,  -48(%rax)\n" /* rcx */
111    "   movq $0,  -56(%rax)\n" /* r8  */
112    "   movq $0,  -64(%rax)\n" /* r9  */
113    "   movq $0,  -72(%rax)\n" /* rbp */
114    "   movq $0,  -80(%rax)\n" /* rbx */
115    "   movq $0,  -88(%rax)\n" /* r12 */
116    "   movq $0,  -96(%rax)\n" /* r13 */
117    "   movq $0, -104(%rax)\n" /* r14 */
118    "   movq $0, -112(%rax)\n" /* r15 */
119    "   subq $112,%rax\n"
120    "   retq\n"
121 );
122
123 __asm__ (
124 #ifdef APPLE
125    ".text\n"
126    ".globl _raw_swapcontext\n"
127    "_raw_swapcontext:\n"
128 #else
129    ".text\n"
130    ".globl raw_swapcontext\n"
131    ".type raw_swapcontext,@function\n"
132    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
133 #endif
134    "   pushq %rdi\n"
135    "   pushq %rsi\n"
136    "   pushq %rdx\n"
137    "   pushq %rcx\n"
138    "   pushq %r8\n"
139    "   pushq %r9\n"
140    "   pushq %rbp\n"
141    "   pushq %rbx\n"
142    "   pushq %r12\n"
143    "   pushq %r13\n"
144    "   pushq %r14\n"
145    "   pushq %r15\n"
146    "   movq %rsp,(%rdi)\n" /* old */
147    "   movq %rsi,%rsp\n" /* new */
148    "   popq %r15\n"
149    "   popq %r14\n"
150    "   popq %r13\n"
151    "   popq %r12\n"
152    "   popq %rbx\n"
153    "   popq %rbp\n"
154    "   popq %r9\n"
155    "   popq %r8\n"
156    "   popq %rcx\n"
157    "   popq %rdx\n"
158    "   popq %rsi\n"
159    "   popq %rdi\n"
160    "   retq\n"
161 );
162 #else
163
164 /* If you implement raw contextes for other processors, don't forget to 
165    update the definition of HAVE_RAWCTX in buildtools/Cmake/AddTests.cmake */
166
167 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
168                             rawctx_entry_point_t entry_point, void* arg) {
169    THROW_UNIMPLEMENTED;
170 }
171
172 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
173    THROW_UNIMPLEMENTED;
174 }
175
176 #endif
177
178 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
179
180 #ifdef CONTEXT_THREADS
181 static xbt_parmap_t parmap;
182 #endif
183
184 #ifdef TIME_BENCH
185 #include "xbt/xbt_os_time.h"
186 #define NUM_THREADS 4
187 static xbt_os_timer_t timer;
188 static double time_thread_sr[NUM_THREADS];
189 static double time_thread_ssr[NUM_THREADS];
190 static double time_wasted_sr = 0;
191 static double time_wasted_ssr = 0;
192 static unsigned int sr_count = 0;
193 static unsigned int ssr_count = 0;
194 static char new_sr = 0;
195 #endif
196
197 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
198
199 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
200 {
201 #ifdef TIME_BENCH
202   XBT_CRITICAL("Total wasted time in %u SR: %lf", sr_count, time_wasted_sr);
203   XBT_CRITICAL("Total wasted time in %u SSR: %lf", ssr_count, time_wasted_ssr);
204 #endif
205
206 #ifdef CONTEXT_THREADS
207   if(parmap)
208       xbt_parmap_destroy(parmap);
209 #endif
210   return smx_ctx_base_factory_finalize(factory);
211 }
212
213
214 static smx_context_t
215 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
216     void_pfn_smxprocess_t cleanup_func,
217     void *data)
218 {
219
220   smx_ctx_raw_t context =
221       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
222           sizeof(s_smx_ctx_raw_t),
223           code,
224           argc,
225           argv,
226           cleanup_func,
227           data);
228
229   /* If the user provided a function for the process then use it
230      otherwise is the context for maestro */
231      if (code) {
232        context->malloced_stack = xbt_malloc0(smx_context_stack_size);
233        context->stack_top =
234            raw_makecontext(context->malloced_stack, smx_context_stack_size,
235                (void(*)(void*))smx_ctx_raw_wrapper,context);
236
237 #ifdef HAVE_VALGRIND_VALGRIND_H
238        context->valgrind_stack_id =
239            VALGRIND_STACK_REGISTER(context->malloced_stack,
240                context->malloced_stack + smx_context_stack_size);
241 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
242
243      }else{
244        maestro_raw_context = context;
245      }
246
247      return (smx_context_t) context;
248 }
249
250 static void smx_ctx_raw_free(smx_context_t context)
251 {
252
253   if (context) {
254
255 #ifdef HAVE_VALGRIND_VALGRIND_H
256     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
257         context)->valgrind_stack_id);
258 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
259
260     free(((smx_ctx_raw_t)context)->malloced_stack);
261   }
262   smx_ctx_base_free(context);
263 }
264
265 static void smx_ctx_raw_suspend(smx_context_t context)
266 {
267   SIMIX_context_set_current((smx_context_t) maestro_raw_context);
268   raw_swapcontext(
269       &((smx_ctx_raw_t) context)->stack_top,
270       ((smx_ctx_raw_t) context)->old_stack_top);
271 }
272
273 static void smx_ctx_raw_stop(smx_context_t context)
274 {
275   smx_ctx_base_stop(context);
276   smx_ctx_raw_suspend(context);
277 }
278
279 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
280
281   (context->super.code) (context->super.argc, context->super.argv);
282
283   smx_ctx_raw_stop((smx_context_t) context);
284 }
285
286 static void smx_ctx_raw_resume(smx_process_t process)
287 {
288   smx_ctx_raw_t context = (smx_ctx_raw_t)process->context;
289   SIMIX_context_set_current((smx_context_t) context);
290   raw_swapcontext(
291       &((smx_ctx_raw_t) context)->old_stack_top,
292       ((smx_ctx_raw_t) context)->stack_top);
293 }
294
295 #ifdef TIME_BENCH
296 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
297 {
298   smx_process_t process;
299   unsigned int cursor;
300
301   double elapsed = 0;
302   double tmax = 0;
303   unsigned long num_proc = xbt_dynar_length(processes);
304   unsigned int t=0;
305   unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
306
307   ssr_count++;
308   time_thread_ssr[0] = 0;
309   xbt_dynar_foreach(processes, cursor, process) {
310     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
311     if(cursor >= t * data_size + data_size){
312       if(time_thread_ssr[t] > tmax)
313         tmax = time_thread_ssr[t];
314       t++;
315       time_thread_ssr[t] = 0;
316     }
317
318     if(new_sr){
319       ((smx_ctx_raw_t)process->context)->thread = t;
320       time_thread_sr[t] = 0;
321     }
322
323     xbt_os_timer_start(timer);
324     smx_ctx_raw_resume(process);
325     xbt_os_timer_stop(timer);
326     elapsed = xbt_os_timer_elapsed(timer);
327     time_thread_ssr[t] += elapsed;
328     time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
329   }
330
331   if(new_sr)
332     new_sr = FALSE;
333
334   if(time_thread_ssr[t] > tmax)
335     tmax = time_thread_ssr[t];
336
337   for(cursor=0; cursor <= t; cursor++){
338     XBT_VERB("Time SSR thread %u = %lf (max %lf)", cursor, time_thread_ssr[cursor], tmax);
339     time_wasted_ssr += tmax - time_thread_ssr[cursor];
340   }
341 }
342
343 void smx_ctx_raw_new_sr(void);
344 void smx_ctx_raw_new_sr(void)
345 {
346   int i;
347   double tmax = 0;
348   new_sr = TRUE;
349   sr_count++;
350   for(i=0; i < NUM_THREADS; i++){
351     if(time_thread_sr[i] > tmax)
352       tmax = time_thread_sr[i];
353   }
354
355   for(i=0; i < NUM_THREADS; i++){
356     XBT_VERB("Time SR thread %u = %lf (max %lf)", i, time_thread_sr[i], tmax);
357     time_wasted_sr += tmax - time_thread_sr[i];
358   }
359
360   XBT_VERB("New scheduling round");
361 }
362 #else
363 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
364 {
365   smx_process_t process;
366   unsigned int cursor;
367
368   xbt_dynar_foreach(processes, cursor, process) {
369     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
370     smx_ctx_raw_resume(process);
371   }
372 }
373 #endif
374
375 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
376 {
377 #ifdef CONTEXT_THREADS
378   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_raw_resume, processes);
379 #endif
380 }
381
382 static void smx_ctx_raw_runall(xbt_dynar_t processes)
383 {
384   if (xbt_dynar_length(processes) >= SIMIX_context_get_parallel_threshold()) {
385     XBT_DEBUG("Runall // %lu", xbt_dynar_length(processes));
386     smx_ctx_raw_runall_parallel(processes);
387   } else {
388     XBT_DEBUG("Runall serial %lu", xbt_dynar_length(processes));
389     smx_ctx_raw_runall_serial(processes);
390   }
391 }
392
393 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
394 {
395   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
396   smx_ctx_base_factory_init(factory);
397
398   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
399   (*factory)->create_context = smx_ctx_raw_create_context;
400   /* Do not overload that method (*factory)->finalize */
401   (*factory)->free = smx_ctx_raw_free;
402   (*factory)->stop = smx_ctx_raw_stop;
403   (*factory)->suspend = smx_ctx_raw_suspend;
404   (*factory)->name = "smx_raw_context_factory";
405
406   if (SIMIX_context_is_parallel()) {
407 #ifdef CONTEXT_THREADS
408     parmap = xbt_parmap_new(SIMIX_context_get_nthreads());
409 #endif
410     if (SIMIX_context_get_parallel_threshold() > 1) {
411       /* choose dynamically */
412       (*factory)->runall = smx_ctx_raw_runall;
413     }
414     else {
415       /* always parallel */
416       (*factory)->runall = smx_ctx_raw_runall_parallel;
417     }
418   }
419   else {
420     /* always serial */
421     (*factory)->runall = smx_ctx_raw_runall_serial;
422   }
423 #ifdef TIME_BENCH
424   timer = xbt_os_timer_new();
425 #endif
426 }