Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5cf7c0726ea68916b05c81cd215daebfb9a09d42
[simgrid.git] / src / simix / smx_context_raw.c
1 /* context_raw - fast context switching inspired from System V ucontextes   */
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 #ifdef HAVE_VALGRIND_VALGRIND_H
13 #  include <valgrind/valgrind.h>
14 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
15
16 typedef char * raw_stack_t;
17 typedef void (*rawctx_entry_point_t)(void *);
18
19 typedef struct s_smx_ctx_raw {
20   s_smx_ctx_base_t super;         /* Fields of super implementation */
21   char *malloced_stack;           /* malloced area containing the stack */
22   raw_stack_t stack_top;          /* pointer to stack top (within previous area) */
23 #ifdef HAVE_VALGRIND_VALGRIND_H
24   unsigned int valgrind_stack_id; /* the valgrind stack id */
25 #endif
26 #ifdef TIME_BENCH
27   unsigned int thread;            /* Just for measuring purposes */
28 #endif
29 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
30
31 #ifdef CONTEXT_THREADS
32 static xbt_parmap_t raw_parmap;
33 static raw_stack_t* raw_local_maestro_stacks; /* space to save maestro's stack in each thread */
34 #endif
35
36 static unsigned long raw_process_index = 0;   /* index of the next process to run in the
37                                                * list of runnable processes */
38 smx_ctx_raw_t raw_maestro_context;
39
40 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
41                                    rawctx_entry_point_t entry_point, void* arg);
42 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
43
44 #ifdef PROCESSOR_i686
45 __asm__ (
46 #if defined(APPLE) || defined(_WIN32)
47    ".text\n"
48    ".globl _raw_makecontext\n"
49    "_raw_makecontext:\n"
50 #else
51    ".text\n"
52    ".globl raw_makecontext\n"
53    ".type raw_makecontext,@function\n"
54    "raw_makecontext:\n"
55 #endif
56    "   movl 4(%esp),%eax\n"   /* stack */
57    "   addl 8(%esp),%eax\n"   /* size  */
58    "   movl 12(%esp),%ecx\n"  /* func  */
59    "   movl 16(%esp),%edx\n"  /* arg   */
60    "   movl %edx, -4(%eax)\n"
61    "   movl $0,   -8(%eax)\n" /* @return for func */
62    "   movl %ecx,-12(%eax)\n"
63    "   movl $0,  -16(%eax)\n" /* ebp */
64    "   movl $0,  -20(%eax)\n" /* ebx */
65    "   movl $0,  -24(%eax)\n" /* esi */
66    "   movl $0,  -28(%eax)\n" /* edi */
67    "   subl $28,%eax\n"
68    "   retl\n"
69 );
70
71 __asm__ (
72 #if defined(APPLE) || defined(_WIN32)
73    ".text\n"
74    ".globl _raw_swapcontext\n"
75    "_raw_swapcontext:\n"
76 #else
77    ".text\n"
78    ".globl raw_swapcontext\n"
79    ".type raw_swapcontext,@function\n"
80    "raw_swapcontext:\n"
81 #endif
82    "   movl 4(%esp),%eax\n" /* old */
83    "   movl 8(%esp),%edx\n" /* new */
84    "   pushl %ebp\n"
85    "   pushl %ebx\n"
86    "   pushl %esi\n"
87    "   pushl %edi\n"
88    "   movl %esp,(%eax)\n"
89    "   movl %edx,%esp\n"
90    "   popl %edi\n"
91    "   popl %esi\n"
92    "   popl %ebx\n"
93    "   popl %ebp\n"
94    "   retl\n"
95 );
96 #elif PROCESSOR_x86_64
97 __asm__ (
98 #if defined(APPLE) || defined(_WIN32)
99    ".text\n"
100    ".globl _raw_makecontext\n"
101    "_raw_makecontext:\n"
102 #else
103    ".text\n"
104    ".globl raw_makecontext\n"
105    ".type raw_makecontext,@function\n"
106    "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
107 #endif
108    "   movq %rdi,%rax\n"      /* stack */
109    "   addq %rsi,%rax\n"      /* size  */
110    "   movq $0,   -8(%rax)\n" /* @return for func */
111    "   movq %rdx,-16(%rax)\n" /* func */
112    "   movq %rcx,-24(%rax)\n" /* arg/rdi */
113    "   movq $0,  -32(%rax)\n" /* rsi */
114    "   movq $0,  -40(%rax)\n" /* rdx */
115    "   movq $0,  -48(%rax)\n" /* rcx */
116    "   movq $0,  -56(%rax)\n" /* r8  */
117    "   movq $0,  -64(%rax)\n" /* r9  */
118    "   movq $0,  -72(%rax)\n" /* rbp */
119    "   movq $0,  -80(%rax)\n" /* rbx */
120    "   movq $0,  -88(%rax)\n" /* r12 */
121    "   movq $0,  -96(%rax)\n" /* r13 */
122    "   movq $0, -104(%rax)\n" /* r14 */
123    "   movq $0, -112(%rax)\n" /* r15 */
124    "   subq $112,%rax\n"
125    "   retq\n"
126 );
127
128 __asm__ (
129 #if defined(APPLE) || defined(_WIN32)
130    ".text\n"
131    ".globl _raw_swapcontext\n"
132    "_raw_swapcontext:\n"
133 #else
134    ".text\n"
135    ".globl raw_swapcontext\n"
136    ".type raw_swapcontext,@function\n"
137    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
138 #endif
139    "   pushq %rdi\n"
140    "   pushq %rsi\n"
141    "   pushq %rdx\n"
142    "   pushq %rcx\n"
143    "   pushq %r8\n"
144    "   pushq %r9\n"
145    "   pushq %rbp\n"
146    "   pushq %rbx\n"
147    "   pushq %r12\n"
148    "   pushq %r13\n"
149    "   pushq %r14\n"
150    "   pushq %r15\n"
151    "   movq %rsp,(%rdi)\n" /* old */
152    "   movq %rsi,%rsp\n" /* new */
153    "   popq %r15\n"
154    "   popq %r14\n"
155    "   popq %r13\n"
156    "   popq %r12\n"
157    "   popq %rbx\n"
158    "   popq %rbp\n"
159    "   popq %r9\n"
160    "   popq %r8\n"
161    "   popq %rcx\n"
162    "   popq %rdx\n"
163    "   popq %rsi\n"
164    "   popq %rdi\n"
165    "   retq\n"
166 );
167 #else
168
169 /* If you implement raw contextes for other processors, don't forget to 
170    update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */
171
172 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
173                             rawctx_entry_point_t entry_point, void* arg) {
174    THROW_UNIMPLEMENTED;
175 }
176
177 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
178    THROW_UNIMPLEMENTED;
179 }
180
181 #endif
182
183 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
184
185 #ifdef TIME_BENCH
186 #include "xbt/xbt_os_time.h"
187 #define NUM_THREADS 4
188 static xbt_os_timer_t timer;
189 static double time_thread_sr[NUM_THREADS];
190 static double time_thread_ssr[NUM_THREADS];
191 static double time_wasted_sr = 0;
192 static double time_wasted_ssr = 0;
193 static unsigned int sr_count = 0;
194 static unsigned int ssr_count = 0;
195 static char new_sr = 0;
196 #endif
197
198 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
199 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
200 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
201     char **argv, void_pfn_smxprocess_t cleanup_func, void *data);
202 static void smx_ctx_raw_free(smx_context_t context);
203 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
204 static void smx_ctx_raw_stop(smx_context_t context);
205 static void smx_ctx_raw_suspend_serial(smx_context_t context);
206 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
207 static void smx_ctx_raw_runall_serial(void);
208 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
209 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
210 static void smx_ctx_raw_runall_parallel(void);
211 static void smx_ctx_raw_runall(void);
212
213 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
214 {
215   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
216   smx_ctx_base_factory_init(factory);
217
218   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
219   (*factory)->create_context = smx_ctx_raw_create_context;
220   /* Do not overload that method (*factory)->finalize */
221   (*factory)->free = smx_ctx_raw_free;
222   (*factory)->stop = smx_ctx_raw_stop;
223   (*factory)->name = "smx_raw_context_factory";
224
225   if (SIMIX_context_is_parallel()) {
226 #ifdef CONTEXT_THREADS
227     int nthreads = SIMIX_context_get_nthreads();
228     raw_parmap = xbt_parmap_new(nthreads);
229     raw_local_maestro_stacks = xbt_new(raw_stack_t, nthreads);
230 #endif
231     if (SIMIX_context_get_parallel_threshold() > 1) {
232       /* choose dynamically */
233       (*factory)->runall = smx_ctx_raw_runall;
234       (*factory)->suspend = NULL;
235     }
236     else {
237       /* always parallel */
238       (*factory)->runall = smx_ctx_raw_runall_parallel;
239       (*factory)->suspend = smx_ctx_raw_suspend_parallel;
240     }
241   }
242   else {
243     /* always serial */
244     (*factory)->runall = smx_ctx_raw_runall_serial;
245     (*factory)->suspend = smx_ctx_raw_suspend_serial;
246   }
247 #ifdef TIME_BENCH
248   timer = xbt_os_timer_new();
249 #endif
250 }
251
252 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
253 {
254 #ifdef TIME_BENCH
255   XBT_CRITICAL("Total wasted time in %u SR: %lf", sr_count, time_wasted_sr);
256   XBT_CRITICAL("Total wasted time in %u SSR: %lf", ssr_count, time_wasted_ssr);
257 #endif
258
259 #ifdef CONTEXT_THREADS
260   if (raw_parmap)
261     xbt_parmap_destroy(raw_parmap);
262   xbt_free(raw_local_maestro_stacks);
263 #endif
264   return smx_ctx_base_factory_finalize(factory);
265 }
266
267 static smx_context_t
268 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
269     void_pfn_smxprocess_t cleanup_func,
270     void *data)
271 {
272
273   smx_ctx_raw_t context =
274       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
275           sizeof(s_smx_ctx_raw_t),
276           code,
277           argc,
278           argv,
279           cleanup_func,
280           data);
281
282   /* if the user provided a function for the process then use it,
283      otherwise it is the context for maestro */
284      if (code) {
285        context->malloced_stack = xbt_malloc0(smx_context_stack_size);
286        context->stack_top =
287            raw_makecontext(context->malloced_stack, smx_context_stack_size,
288                (void(*)(void*))smx_ctx_raw_wrapper,context);
289
290 #ifdef HAVE_VALGRIND_VALGRIND_H
291        context->valgrind_stack_id =
292            VALGRIND_STACK_REGISTER(context->malloced_stack,
293                context->malloced_stack + smx_context_stack_size);
294 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
295
296      } else {
297        raw_maestro_context = context;
298      }
299
300      return (smx_context_t) context;
301 }
302
303 static void smx_ctx_raw_free(smx_context_t context)
304 {
305
306   if (context) {
307
308 #ifdef HAVE_VALGRIND_VALGRIND_H
309     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
310         context)->valgrind_stack_id);
311 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
312
313     free(((smx_ctx_raw_t)context)->malloced_stack);
314   }
315   smx_ctx_base_free(context);
316 }
317
318 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
319 {
320   (context->super.code) (context->super.argc, context->super.argv);
321
322   smx_ctx_raw_stop((smx_context_t) context);
323 }
324
325 static void smx_ctx_raw_stop(smx_context_t context)
326 {
327   smx_ctx_base_stop(context);
328   simix_global->context_factory->suspend(context);
329 }
330
331 static void smx_ctx_raw_suspend_serial(smx_context_t context)
332 {
333   /* determine the next context */
334   smx_context_t next_context;
335   unsigned long int i = raw_process_index++;
336
337   if (i < xbt_dynar_length(simix_global->process_to_run)) {
338     /* execute the next process */
339     XBT_DEBUG("Run next process");
340     next_context = xbt_dynar_get_as(
341         simix_global->process_to_run,i, smx_process_t)->context;
342   }
343   else {
344     /* all processes were run, return to maestro */
345     XBT_DEBUG("No more process to run");
346     next_context = (smx_context_t) raw_maestro_context;
347   }
348   SIMIX_context_set_current(next_context);
349   raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
350       ((smx_ctx_raw_t) next_context)->stack_top);
351 }
352
353 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
354 {
355   smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
356   SIMIX_context_set_current((smx_context_t) context);
357   raw_swapcontext(&raw_maestro_context->stack_top,
358       ((smx_ctx_raw_t) context)->stack_top);
359 }
360
361 #ifdef TIME_BENCH
362 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
363 {
364   smx_process_t process;
365   unsigned int cursor;
366
367   double elapsed = 0;
368   double tmax = 0;
369   unsigned long num_proc = xbt_dynar_length(processes);
370   unsigned int t=0;
371   unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
372
373   ssr_count++;
374   time_thread_ssr[0] = 0;
375   xbt_dynar_foreach(processes, cursor, process) {
376     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
377     if(cursor >= t * data_size + data_size){
378       if(time_thread_ssr[t] > tmax)
379         tmax = time_thread_ssr[t];
380       t++;
381       time_thread_ssr[t] = 0;
382     }
383
384     if(new_sr){
385       ((smx_ctx_raw_t)process->context)->thread = t;
386       time_thread_sr[t] = 0;
387     }
388
389     xbt_os_timer_start(timer);
390     smx_ctx_raw_resume(process);
391     xbt_os_timer_stop(timer);
392     elapsed = xbt_os_timer_elapsed(timer);
393     time_thread_ssr[t] += elapsed;
394     time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
395   }
396
397   if(new_sr)
398     new_sr = FALSE;
399
400   if(time_thread_ssr[t] > tmax)
401     tmax = time_thread_ssr[t];
402
403   for(cursor=0; cursor <= t; cursor++){
404     XBT_VERB("Time SSR thread %u = %lf (max %lf)", cursor, time_thread_ssr[cursor], tmax);
405     time_wasted_ssr += tmax - time_thread_ssr[cursor];
406   }
407 }
408
409 void smx_ctx_raw_new_sr(void);
410 void smx_ctx_raw_new_sr(void)
411 {
412   int i;
413   double tmax = 0;
414   new_sr = TRUE;
415   sr_count++;
416   for(i=0; i < NUM_THREADS; i++){
417     if(time_thread_sr[i] > tmax)
418       tmax = time_thread_sr[i];
419   }
420
421   for(i=0; i < NUM_THREADS; i++){
422     XBT_VERB("Time SR thread %u = %lf (max %lf)", i, time_thread_sr[i], tmax);
423     time_wasted_sr += tmax - time_thread_sr[i];
424   }
425
426   XBT_VERB("New scheduling round");
427 }
428 #else
429 static void smx_ctx_raw_runall_serial(void)
430 {
431   if (xbt_dynar_length(simix_global->process_to_run) > 0) {
432     smx_process_t first_process =
433         xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
434     raw_process_index = 1;
435
436     /* execute the first process */
437     smx_ctx_raw_resume_serial(first_process);
438   }
439 }
440 #endif
441
442 static void smx_ctx_raw_stop_parallel(smx_context_t context)
443 {
444   smx_ctx_base_stop(context);
445   smx_ctx_raw_suspend_parallel(context);
446 }
447
448 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
449 {
450 #ifdef CONTEXT_THREADS
451   /* determine the next context */
452   smx_process_t next_work = xbt_parmap_next(raw_parmap);
453   smx_context_t next_context;
454   raw_stack_t next_stack;
455
456   if (next_work != NULL) {
457     /* there is a next process to resume */
458     XBT_DEBUG("Run next process");
459     next_context = next_work->context;
460     next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
461   }
462   else {
463     /* all processes were run, go to the barrier */
464     XBT_DEBUG("No more processes to run");
465     next_context = (smx_context_t) raw_maestro_context;
466     unsigned long worker_id = xbt_parmap_get_worker_id(raw_parmap);
467     next_stack = raw_local_maestro_stacks[worker_id];
468   }
469
470   SIMIX_context_set_current(next_context);
471   raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
472 #endif
473 }
474
475 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
476 {
477 #ifdef CONTEXT_THREADS
478   unsigned long worker_id = xbt_parmap_get_worker_id(raw_parmap);
479   raw_stack_t* local_maestro_stack = &raw_local_maestro_stacks[worker_id];
480
481   smx_context_t context = first_process->context;
482   SIMIX_context_set_current(context);
483   raw_swapcontext(local_maestro_stack, ((smx_ctx_raw_t) context)->stack_top);
484 #endif
485 }
486
487 static void smx_ctx_raw_runall_parallel(void)
488 {
489 #ifdef CONTEXT_THREADS
490   xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
491       simix_global->process_to_run);
492 #endif
493 }
494
495 static void smx_ctx_raw_runall(void)
496 {
497   unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
498   if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
499     XBT_DEBUG("Runall // %lu", nb_processes);
500     simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
501     smx_ctx_raw_runall_parallel();
502   } else {
503     XBT_DEBUG("Runall serial %lu", nb_processes);
504     simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
505     smx_ctx_raw_runall_serial();
506   }
507 }