Logo AND Algorithmique Numérique Distribuée

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