1 /* context_raw - fast context switching inspired from System V ucontexts */
3 /* Copyright (c) 2009-2014. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "smx_private.h"
10 #include "xbt/parmap.h"
11 #include "xbt/dynar.h"
14 typedef char * raw_stack_t;
15 typedef void (*rawctx_entry_point_t)(void *);
17 typedef struct s_smx_ctx_raw {
18 s_smx_ctx_base_t super; /* Fields of super implementation */
19 char *malloced_stack; /* malloced area containing the stack */
20 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
21 #ifdef TIME_BENCH_PER_SR
22 unsigned int thread; /* Just for measuring purposes */
24 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
26 #ifdef CONTEXT_THREADS
27 static xbt_parmap_t raw_parmap;
28 static smx_ctx_raw_t* raw_workers_context; /* space to save the worker context in each thread */
29 static unsigned long raw_threads_working; /* number of threads that have started their work */
30 static xbt_os_thread_key_t raw_worker_id_key; /* thread-specific storage for the thread id */
32 #ifdef ADAPTIVE_THRESHOLD
33 #define SCHED_ROUND_LIMIT 5
34 xbt_os_timer_t round_time;
35 double par_time,seq_time;
36 double par_ratio,seq_ratio;
37 int reached_seq_limit, reached_par_limit;
38 static unsigned int par_proc_that_ran = 0,seq_proc_that_ran = 0; /* Counters of processes that have run in SCHED_ROUND_LIMIT scheduling rounds */
39 static unsigned int seq_sched_round=0, par_sched_round=0; /* Amount of SR that ran serial/parallel*/
40 /*Varables used to calculate running variance and mean*/
41 double prev_avg_par_proc=0,prev_avg_seq_proc=0;
43 double s_par_proc=0,s_seq_proc=0; /*Standard deviation of number of processes computed in par/seq during the current simulation*/
44 double avg_par_proc=0,sd_par_proc=0;
45 double avg_seq_proc=0,sd_seq_proc=0;
46 long long par_window=(long long)HUGE_VAL,seq_window=0;
49 static unsigned long raw_process_index = 0; /* index of the next process to run in the
50 * list of runnable processes */
51 static smx_ctx_raw_t raw_maestro_context;
52 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
53 rawctx_entry_point_t entry_point, void* arg);
54 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
60 ".globl _raw_makecontext\n"
64 ".globl raw_makecontext\n"
68 ".globl raw_makecontext\n"
69 ".type raw_makecontext,@function\n"
70 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
72 " mov %rdi,%rax\n" /* stack */
73 " add %rsi,%rax\n" /* size */
74 " andq $-16, %rax\n" /* align stack */
75 " movq $0, -8(%rax)\n" /* @return for func */
76 " mov %rdx,-16(%rax)\n" /* func */
77 " mov %rcx,-24(%rax)\n" /* arg/rdi */
78 " movq $0, -32(%rax)\n" /* rsi */
79 " movq $0, -40(%rax)\n" /* rdx */
80 " movq $0, -48(%rax)\n" /* rcx */
81 " movq $0, -56(%rax)\n" /* r8 */
82 " movq $0, -64(%rax)\n" /* r9 */
83 " movq $0, -72(%rax)\n" /* rbp */
84 " movq $0, -80(%rax)\n" /* rbx */
85 " movq $0, -88(%rax)\n" /* r12 */
86 " movq $0, -96(%rax)\n" /* r13 */
87 " movq $0, -104(%rax)\n" /* r14 */
88 " movq $0, -112(%rax)\n" /* r15 */
96 ".globl _raw_swapcontext\n"
100 ".globl raw_swapcontext\n"
104 ".globl raw_swapcontext\n"
105 ".type raw_swapcontext,@function\n"
106 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
120 " mov %rsp,(%rdi)\n" /* old */
121 " mov %rsi,%rsp\n" /* new */
138 #if defined(APPLE) || defined(_WIN32)
140 ".globl _raw_makecontext\n"
141 "_raw_makecontext:\n"
144 ".globl raw_makecontext\n"
145 ".type raw_makecontext,@function\n"
148 " movl 4(%esp),%eax\n" /* stack */
149 " addl 8(%esp),%eax\n" /* size */
150 " andl $-16, %eax\n" /* align stack */
151 " movl 12(%esp),%ecx\n" /* func */
152 " movl 16(%esp),%edx\n" /* arg */
153 " movl %edx, -4(%eax)\n"
154 " movl $0, -8(%eax)\n" /* @return for func */
155 " movl %ecx,-12(%eax)\n"
156 " movl $0, -16(%eax)\n" /* ebp */
157 " movl $0, -20(%eax)\n" /* ebx */
158 " movl $0, -24(%eax)\n" /* esi */
159 " movl $0, -28(%eax)\n" /* edi */
165 #if defined(APPLE) || defined(_WIN32)
167 ".globl _raw_swapcontext\n"
168 "_raw_swapcontext:\n"
171 ".globl raw_swapcontext\n"
172 ".type raw_swapcontext,@function\n"
175 " movl 4(%esp),%eax\n" /* old */
176 " movl 8(%esp),%edx\n" /* new */
181 " movl %esp,(%eax)\n"
192 /* If you implement raw contexts for other processors, don't forget to
193 update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */
195 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
196 rawctx_entry_point_t entry_point, void* arg) {
200 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
206 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
208 #ifdef TIME_BENCH_PER_SR
209 #include "xbt/xbt_os_time.h"
210 #define NUM_THREADS 4
211 static xbt_os_timer_t timer;
212 static double time_thread_sr[NUM_THREADS];
213 static double time_thread_ssr[NUM_THREADS];
214 static double time_wasted_sr = 0;
215 static double time_wasted_ssr = 0;
216 static unsigned int sr_count = 0;
217 static unsigned int ssr_count = 0;
218 static char new_sr = 0;
221 #ifdef TIME_BENCH_ENTIRE_SRS
222 static unsigned int sr_count = 0;
223 static xbt_os_timer_t timer;
226 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
227 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
228 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
229 char **argv, void_pfn_smxprocess_t cleanup_func, smx_process_t process);
230 static void smx_ctx_raw_free(smx_context_t context);
231 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
232 static void smx_ctx_raw_stop(smx_context_t context);
233 static void smx_ctx_raw_suspend_serial(smx_context_t context);
234 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
235 #ifdef TIME_BENCH_PER_SR
236 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes);
237 void smx_ctx_raw_new_sr(void);
239 static void smx_ctx_raw_runall_serial(void);
241 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
242 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
243 static void smx_ctx_raw_runall_parallel(void);
244 static void smx_ctx_raw_runall(void);
247 * \brief Initializes the raw context factory.
248 * \param factory where to initialize the factory
250 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
253 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
254 smx_ctx_base_factory_init(factory);
256 (*factory)->finalize = smx_ctx_raw_factory_finalize;
257 (*factory)->create_context = smx_ctx_raw_create_context;
258 /* Do not overload that method (*factory)->finalize */
259 (*factory)->free = smx_ctx_raw_free;
260 (*factory)->stop = smx_ctx_raw_stop;
261 (*factory)->name = "smx_raw_context_factory";
263 if (SIMIX_context_is_parallel()) {
264 #ifdef CONTEXT_THREADS
265 int nthreads = SIMIX_context_get_nthreads();
266 xbt_os_thread_key_create(&raw_worker_id_key);
267 raw_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
268 raw_workers_context = xbt_new(smx_ctx_raw_t, nthreads);
269 raw_maestro_context=NULL;
272 if (SIMIX_context_get_parallel_threshold() > 1) {
273 /* choose dynamically */
274 (*factory)->runall = smx_ctx_raw_runall;
275 (*factory)->suspend = NULL;
278 /* always parallel */
279 (*factory)->runall = smx_ctx_raw_runall_parallel;
280 (*factory)->suspend = smx_ctx_raw_suspend_parallel;
285 (*factory)->runall = smx_ctx_raw_runall_serial;
286 (*factory)->suspend = smx_ctx_raw_suspend_serial;
288 #ifdef TIME_BENCH_ENTIRE_SRS
289 (*factory)->runall = smx_ctx_raw_runall;
290 (*factory)->suspend = NULL;
291 timer = xbt_os_timer_new();
294 #ifdef ADAPTIVE_THRESHOLD
295 round_time = xbt_os_timer_new();
296 reached_seq_limit = 0;
297 reached_par_limit = 0;
300 #ifdef TIME_BENCH_PER_SR
301 timer = xbt_os_timer_new();
306 * \brief Finalizes the raw context factory.
307 * \param factory the raw context factory
309 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
311 #ifdef TIME_BENCH_PER_SR
312 XBT_VERB("Total wasted time in %u SR: %f", sr_count, time_wasted_sr);
313 XBT_VERB("Total wasted time in %u SSR: %f", ssr_count, time_wasted_ssr);
316 #ifdef CONTEXT_THREADS
318 xbt_parmap_destroy(raw_parmap);
319 xbt_free(raw_workers_context);
321 return smx_ctx_base_factory_finalize(factory);
325 * \brief Creates a new raw context.
326 * \param code main function of this context or NULL to create the maestro
328 * \param argc argument number
329 * \param argv arguments to pass to the main function
330 * \param cleanup_func a function to call to free the user data when the
332 * \param process SIMIX process
335 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
336 void_pfn_smxprocess_t cleanup_func,
337 smx_process_t process)
340 smx_ctx_raw_t context =
341 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
342 sizeof(s_smx_ctx_raw_t),
349 /* if the user provided a function for the process then use it,
350 otherwise it is the context for maestro */
352 context->malloced_stack = SIMIX_context_stack_new();
354 raw_makecontext(context->malloced_stack,
355 smx_context_usable_stack_size,
356 (void_f_pvoid_t)smx_ctx_raw_wrapper, context);
359 if(process != NULL && raw_maestro_context==NULL)
360 raw_maestro_context = context;
363 MC_ignore_heap(&(raw_maestro_context->stack_top), sizeof(raw_maestro_context->stack_top));
367 return (smx_context_t) context;
371 * \brief Destroys a raw context.
372 * \param context a raw context
374 static void smx_ctx_raw_free(smx_context_t context)
377 SIMIX_context_stack_delete(((smx_ctx_raw_t) context)->malloced_stack);
379 smx_ctx_base_free(context);
383 * \brief Wrapper for the main function of a context.
384 * \param context a raw context
386 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
388 (context->super.code) (context->super.argc, context->super.argv);
390 smx_ctx_raw_stop((smx_context_t) context);
394 * \brief Stops a raw context.
396 * This function is called when the main function of the context if finished.
398 * \param context the current context
400 static void smx_ctx_raw_stop(smx_context_t context)
402 smx_ctx_base_stop(context);
403 simix_global->context_factory->suspend(context);
407 * \brief Suspends a running context and resumes another one or returns to
409 * \param context the current context
411 static void smx_ctx_raw_suspend_serial(smx_context_t context)
413 /* determine the next context */
414 smx_context_t next_context;
416 #ifdef TIME_BENCH_PER_SR
417 i = ++raw_process_index;
419 i = raw_process_index++;
421 if (i < xbt_dynar_length(simix_global->process_to_run)) {
422 /* execute the next process */
423 XBT_DEBUG("Run next process");
424 next_context = xbt_dynar_get_as(
425 simix_global->process_to_run, i, smx_process_t)->context;
428 /* all processes were run, return to maestro */
429 XBT_DEBUG("No more process to run");
430 next_context = (smx_context_t) raw_maestro_context;
432 SIMIX_context_set_current(next_context);
433 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
434 ((smx_ctx_raw_t) next_context)->stack_top);
438 * \brief Resumes sequentially all processes ready to run.
439 * \param first_process the first process to resume
441 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
443 smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
444 SIMIX_context_set_current((smx_context_t) context);
445 raw_swapcontext(&raw_maestro_context->stack_top,
446 ((smx_ctx_raw_t) context)->stack_top);
449 #ifdef TIME_BENCH_PER_SR
450 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
452 smx_process_t process;
456 unsigned long num_proc = xbt_dynar_length(simix_global->process_to_run);
458 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
461 time_thread_ssr[0] = 0;
462 xbt_dynar_foreach(processes, cursor, process){
463 XBT_VERB("Schedule item %u of %lu",cursor,num_proc);
464 if(cursor >= t * data_size + data_size){
465 if(time_thread_ssr[t] > tmax)
466 tmax = time_thread_ssr[t];
468 time_thread_ssr[t] = 0;
472 ((smx_ctx_raw_t)process->context)->thread = t;
473 time_thread_sr[t] = 0;
476 xbt_os_cputimer_start(timer);
477 smx_ctx_raw_resume_serial(process);
478 xbt_os_cputimer_stop(timer);
479 elapsed = xbt_os_timer_elapsed(timer);
480 time_thread_ssr[t] += elapsed;
481 time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
487 if(time_thread_ssr[t] > tmax)
488 tmax = time_thread_ssr[t];
490 for(cursor=0; cursor <= t; cursor++){
491 XBT_VERB("Time SSR thread %u = %f (max %f)", cursor, time_thread_ssr[cursor], tmax);
492 time_wasted_ssr += tmax - time_thread_ssr[cursor];
496 void smx_ctx_raw_new_sr(void)
502 for(i=0; i < NUM_THREADS; i++){
503 if(time_thread_sr[i] > tmax)
504 tmax = time_thread_sr[i];
507 for(i=0; i < NUM_THREADS; i++){
508 XBT_CRITICAL("Time SR thread %u = %f (max %f)", i, time_thread_sr[i], tmax);
509 time_wasted_sr += tmax - time_thread_sr[i];
512 XBT_CRITICAL("Total time SR %u = %f, %d", sr_count, tmax, xbt_dynar_length(simix_global->process_that_ran));
513 XBT_CRITICAL("New scheduling round");
517 * \brief Resumes sequentially all processes ready to run.
519 static void smx_ctx_raw_runall_serial(void)
521 smx_process_t first_process =
522 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
523 raw_process_index = 1;
525 /* execute the first process */
526 smx_ctx_raw_resume_serial(first_process);
531 * \brief Suspends a running context and resumes another one or returns to
532 * the main function of the current worker thread.
533 * \param context the context of the current worker thread
535 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
537 #ifdef CONTEXT_THREADS
538 /* determine the next context */
539 smx_process_t next_work = xbt_parmap_next(raw_parmap);
540 smx_context_t next_context;
541 raw_stack_t next_stack;
543 if (next_work != NULL) {
544 /* there is a next process to resume */
545 XBT_DEBUG("Run next process");
546 next_context = next_work->context;
547 next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
550 /* all processes were run, go to the barrier */
551 XBT_DEBUG("No more processes to run");
553 unsigned long worker_id =
554 (unsigned long) xbt_os_thread_get_specific(raw_worker_id_key);
556 next_context = (smx_context_t)raw_workers_context[worker_id];
557 XBT_DEBUG("Restoring worker stack %lu (working threads = %lu)",
558 worker_id, raw_threads_working);
559 next_stack = ((smx_ctx_raw_t)next_context)->stack_top;
562 SIMIX_context_set_current(next_context);
563 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
568 * \brief Resumes sequentially in the current worker thread the processes ready
570 * \param first_process the first process to resume
572 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
574 #ifdef CONTEXT_THREADS
575 unsigned long worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
576 xbt_os_thread_set_specific(raw_worker_id_key, (void*) worker_id);
577 smx_ctx_raw_t worker_context = (smx_ctx_raw_t)SIMIX_context_self();
578 raw_workers_context[worker_id] = worker_context;
579 XBT_DEBUG("Saving worker stack %lu", worker_id);
580 raw_stack_t* worker_stack = &(worker_context)->stack_top;
583 smx_context_t context = first_process->context;
584 SIMIX_context_set_current(context);
585 raw_swapcontext(worker_stack, ((smx_ctx_raw_t) context)->stack_top);
590 * \brief Resumes in parallel all processes ready to run.
592 static void smx_ctx_raw_runall_parallel(void)
594 #ifdef CONTEXT_THREADS
595 raw_threads_working = 0;
596 xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
597 simix_global->process_to_run);
599 xbt_die("You asked for a parallel execution, but you don't have any threads.");
604 * \brief Resumes all processes ready to run.
606 #ifdef ADAPTIVE_THRESHOLD
607 static void smx_ctx_raw_runall(void)
609 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
610 unsigned long threshold = SIMIX_context_get_parallel_threshold();
611 reached_seq_limit = (seq_sched_round % SCHED_ROUND_LIMIT == 0);
612 reached_par_limit = (par_sched_round % SCHED_ROUND_LIMIT == 0);
614 if(reached_seq_limit && reached_par_limit){
615 par_ratio = (par_proc_that_ran != 0) ? (par_time / (double)par_proc_that_ran) : 0;
616 seq_ratio = (seq_proc_that_ran != 0) ? (seq_time / (double)seq_proc_that_ran) : 0;
617 if(seq_ratio > par_ratio){
618 if(nb_processes < avg_par_proc) {
619 threshold = (threshold>2) ? threshold - 1 : threshold ;
620 SIMIX_context_set_parallel_threshold(threshold);
623 if(nb_processes > avg_seq_proc){
624 SIMIX_context_set_parallel_threshold(threshold+1);
629 //XBT_CRITICAL("Thresh: %d", SIMIX_context_get_parallel_threshold());
630 if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
631 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
632 if(nb_processes < par_window){
634 xbt_os_walltimer_start(round_time);
635 smx_ctx_raw_runall_parallel();
636 xbt_os_walltimer_stop(round_time);
637 par_time += xbt_os_timer_elapsed(round_time);
639 prev_avg_par_proc = avg_par_proc;
640 delta = nb_processes - avg_par_proc;
641 avg_par_proc = (par_sched_round==1) ? nb_processes : avg_par_proc + delta / (double) par_sched_round;
643 if(par_sched_round>=2){
644 s_par_proc = s_par_proc + (nb_processes - prev_avg_par_proc) * delta;
645 sd_par_proc = sqrt(s_par_proc / (par_sched_round-1));
646 par_window = (int) (avg_par_proc + sd_par_proc);
651 par_proc_that_ran += nb_processes;
653 smx_ctx_raw_runall_parallel();
656 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
657 if(nb_processes > seq_window){
659 xbt_os_walltimer_start(round_time);
660 smx_ctx_raw_runall_serial();
661 xbt_os_walltimer_stop(round_time);
662 seq_time += xbt_os_timer_elapsed(round_time);
664 prev_avg_seq_proc = avg_seq_proc;
665 delta = (nb_processes-avg_seq_proc);
666 avg_seq_proc = (seq_sched_round==1) ? nb_processes : avg_seq_proc + delta / (double) seq_sched_round;
668 if(seq_sched_round>=2){
669 s_seq_proc = s_seq_proc + (nb_processes - prev_avg_seq_proc)*delta;
670 sd_seq_proc = sqrt(s_seq_proc / (seq_sched_round-1));
671 seq_window = (int) (avg_seq_proc - sd_seq_proc);
676 seq_proc_that_ran += nb_processes;
678 smx_ctx_raw_runall_serial();
685 static void smx_ctx_raw_runall(void)
687 #ifdef TIME_BENCH_ENTIRE_SRS
689 timer = xbt_os_timer_new();
692 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
693 if (SIMIX_context_is_parallel() && SIMIX_context_get_parallel_threshold()<nb_processes) {
694 XBT_DEBUG("Runall // %lu", nb_processes);
695 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
697 #ifdef TIME_BENCH_ENTIRE_SRS
698 xbt_os_walltimer_start(timer);
701 smx_ctx_raw_runall_parallel();
703 #ifdef TIME_BENCH_ENTIRE_SRS
704 xbt_os_walltimer_stop(timer);
705 elapsed = xbt_os_timer_elapsed(timer);
708 XBT_DEBUG("Runall serial %lu", nb_processes);
709 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
711 #ifdef TIME_BENCH_PER_SR
712 smx_ctx_raw_runall_serial(simix_global->process_to_run);
715 #ifdef TIME_BENCH_ENTIRE_SRS
716 xbt_os_walltimer_start(timer);
719 smx_ctx_raw_runall_serial();
721 #ifdef TIME_BENCH_ENTIRE_SRS
722 xbt_os_walltimer_stop(timer);
723 elapsed = xbt_os_timer_elapsed(timer);
728 #ifdef TIME_BENCH_ENTIRE_SRS
729 XBT_CRITICAL("Total time SR %u = %f, %d", sr_count, elapsed, nb_processes);