1 /* context_raw - fast context switching inspired from System V ucontexts */
3 /* Copyright (c) 2009-2015. 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. */
10 #include "smx_private.h"
11 #include "xbt/parmap.h"
12 #include "xbt/dynar.h"
15 typedef char * raw_stack_t;
16 typedef void (*rawctx_entry_point_t)(void *);
18 typedef struct s_smx_ctx_raw {
19 s_smx_ctx_base_t super; /* Fields of super implementation */
20 char *malloced_stack; /* malloced area containing the stack */
21 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
22 #ifdef TIME_BENCH_PER_SR
23 unsigned int thread; /* Just for measuring purposes */
25 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
27 #ifdef CONTEXT_THREADS
28 static xbt_parmap_t raw_parmap;
29 static smx_ctx_raw_t* raw_workers_context; /* space to save the worker context in each thread */
30 static unsigned long raw_threads_working; /* number of threads that have started their work */
31 static xbt_os_thread_key_t raw_worker_id_key; /* thread-specific storage for the thread id */
33 #ifdef ADAPTIVE_THRESHOLD
34 #define SCHED_ROUND_LIMIT 5
35 xbt_os_timer_t round_time;
36 double par_time,seq_time;
37 double par_ratio,seq_ratio;
38 int reached_seq_limit, reached_par_limit;
39 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 */
40 static unsigned int seq_sched_round=0, par_sched_round=0; /* Amount of SR that ran serial/parallel*/
41 /*Varables used to calculate running variance and mean*/
42 double prev_avg_par_proc=0,prev_avg_seq_proc=0;
44 double s_par_proc=0,s_seq_proc=0; /*Standard deviation of number of processes computed in par/seq during the current simulation*/
45 double avg_par_proc=0,sd_par_proc=0;
46 double avg_seq_proc=0,sd_seq_proc=0;
47 long long par_window=(long long)HUGE_VAL,seq_window=0;
50 static unsigned long raw_process_index = 0; /* index of the next process to run in the
51 * list of runnable processes */
52 static smx_ctx_raw_t raw_maestro_context;
53 extern "C" raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
54 rawctx_entry_point_t entry_point, void* arg);
55 extern "C" void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context);
61 ".globl _raw_makecontext\n"
65 ".globl raw_makecontext\n"
69 ".globl raw_makecontext\n"
70 ".type raw_makecontext,@function\n"
71 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
73 " mov %rdi,%rax\n" /* stack */
74 " add %rsi,%rax\n" /* size */
75 " andq $-16, %rax\n" /* align stack */
76 " movq $0, -8(%rax)\n" /* @return for func */
77 " mov %rdx,-16(%rax)\n" /* func */
78 " mov %rcx,-24(%rax)\n" /* arg/rdi */
79 " movq $0, -32(%rax)\n" /* rsi */
80 " movq $0, -40(%rax)\n" /* rdx */
81 " movq $0, -48(%rax)\n" /* rcx */
82 " movq $0, -56(%rax)\n" /* r8 */
83 " movq $0, -64(%rax)\n" /* r9 */
84 " movq $0, -72(%rax)\n" /* rbp */
85 " movq $0, -80(%rax)\n" /* rbx */
86 " movq $0, -88(%rax)\n" /* r12 */
87 " movq $0, -96(%rax)\n" /* r13 */
88 " movq $0, -104(%rax)\n" /* r14 */
89 " movq $0, -112(%rax)\n" /* r15 */
97 ".globl _raw_swapcontext\n"
101 ".globl raw_swapcontext\n"
105 ".globl raw_swapcontext\n"
106 ".type raw_swapcontext,@function\n"
107 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
121 " mov %rsp,(%rdi)\n" /* old */
122 " mov %rsi,%rsp\n" /* new */
139 #if defined(APPLE) || defined(_WIN32)
141 ".globl _raw_makecontext\n"
142 "_raw_makecontext:\n"
145 ".globl raw_makecontext\n"
146 ".type raw_makecontext,@function\n"
149 " movl 4(%esp),%eax\n" /* stack */
150 " addl 8(%esp),%eax\n" /* size */
151 " andl $-16, %eax\n" /* align stack */
152 " movl 12(%esp),%ecx\n" /* func */
153 " movl 16(%esp),%edx\n" /* arg */
154 " movl %edx, -4(%eax)\n"
155 " movl $0, -8(%eax)\n" /* @return for func */
156 " movl %ecx,-12(%eax)\n"
157 " movl $0, -16(%eax)\n" /* ebp */
158 " movl $0, -20(%eax)\n" /* ebx */
159 " movl $0, -24(%eax)\n" /* esi */
160 " movl $0, -28(%eax)\n" /* edi */
166 #if defined(APPLE) || defined(_WIN32)
168 ".globl _raw_swapcontext\n"
169 "_raw_swapcontext:\n"
172 ".globl raw_swapcontext\n"
173 ".type raw_swapcontext,@function\n"
176 " movl 4(%esp),%eax\n" /* old */
177 " movl 8(%esp),%edx\n" /* new */
182 " movl %esp,(%eax)\n"
193 /* If you implement raw contexts for other processors, don't forget to
194 update the definition of HAVE_RAWCTX in tools/cmake/CompleteInFiles.cmake */
196 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
197 rawctx_entry_point_t entry_point, void* arg) {
201 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context) {
207 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
209 #ifdef TIME_BENCH_PER_SR
210 #include "xbt/xbt_os_time.h"
211 #define NUM_THREADS 4
212 static xbt_os_timer_t timer;
213 static double time_thread_sr[NUM_THREADS];
214 static double time_thread_ssr[NUM_THREADS];
215 static double time_wasted_sr = 0;
216 static double time_wasted_ssr = 0;
217 static unsigned int sr_count = 0;
218 static unsigned int ssr_count = 0;
219 static char new_sr = 0;
222 #ifdef TIME_BENCH_ENTIRE_SRS
223 static unsigned int sr_count = 0;
224 static xbt_os_timer_t timer;
227 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
228 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
229 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
230 char **argv, void_pfn_smxprocess_t cleanup_func, smx_process_t process);
231 static void smx_ctx_raw_free(smx_context_t context);
232 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
233 static void smx_ctx_raw_stop(smx_context_t context);
234 static void smx_ctx_raw_suspend_serial(smx_context_t context);
235 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
236 #ifdef TIME_BENCH_PER_SR
237 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes);
238 void smx_ctx_raw_new_sr(void);
240 static void smx_ctx_raw_runall_serial(void);
242 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
243 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
244 static void smx_ctx_raw_runall_parallel(void);
245 static void smx_ctx_raw_runall(void);
248 * \brief Initializes the raw context factory.
249 * \param factory where to initialize the factory
251 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
254 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
255 smx_ctx_base_factory_init(factory);
257 (*factory)->finalize = smx_ctx_raw_factory_finalize;
258 (*factory)->create_context = smx_ctx_raw_create_context;
259 /* Do not overload that method (*factory)->finalize */
260 (*factory)->free = smx_ctx_raw_free;
261 (*factory)->stop = smx_ctx_raw_stop;
262 (*factory)->name = "smx_raw_context_factory";
264 if (SIMIX_context_is_parallel()) {
265 #ifdef CONTEXT_THREADS
266 int nthreads = SIMIX_context_get_nthreads();
267 xbt_os_thread_key_create(&raw_worker_id_key);
268 raw_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
269 raw_workers_context = xbt_new(smx_ctx_raw_t, nthreads);
270 raw_maestro_context=NULL;
273 if (SIMIX_context_get_parallel_threshold() > 1) {
274 /* choose dynamically */
275 (*factory)->runall = smx_ctx_raw_runall;
276 (*factory)->suspend = NULL;
279 /* always parallel */
280 (*factory)->runall = smx_ctx_raw_runall_parallel;
281 (*factory)->suspend = smx_ctx_raw_suspend_parallel;
286 (*factory)->runall = smx_ctx_raw_runall_serial;
287 (*factory)->suspend = smx_ctx_raw_suspend_serial;
289 #ifdef TIME_BENCH_ENTIRE_SRS
290 (*factory)->runall = smx_ctx_raw_runall;
291 (*factory)->suspend = NULL;
292 timer = xbt_os_timer_new();
295 #ifdef ADAPTIVE_THRESHOLD
296 round_time = xbt_os_timer_new();
297 reached_seq_limit = 0;
298 reached_par_limit = 0;
301 #ifdef TIME_BENCH_PER_SR
302 timer = xbt_os_timer_new();
307 * \brief Finalizes the raw context factory.
308 * \param factory the raw context factory
310 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
312 #ifdef TIME_BENCH_PER_SR
313 XBT_VERB("Total wasted time in %u SR: %f", sr_count, time_wasted_sr);
314 XBT_VERB("Total wasted time in %u SSR: %f", ssr_count, time_wasted_ssr);
317 #ifdef CONTEXT_THREADS
319 xbt_parmap_destroy(raw_parmap);
320 xbt_free(raw_workers_context);
322 return smx_ctx_base_factory_finalize(factory);
326 * \brief Creates a new raw context.
327 * \param code main function of this context or NULL to create the maestro
329 * \param argc argument number
330 * \param argv arguments to pass to the main function
331 * \param cleanup_func a function to call to free the user data when the
333 * \param process SIMIX process
336 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
337 void_pfn_smxprocess_t cleanup_func,
338 smx_process_t process)
341 smx_ctx_raw_t context =
342 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
343 sizeof(s_smx_ctx_raw_t),
350 /* if the user provided a function for the process then use it,
351 otherwise it is the context for maestro */
353 context->malloced_stack = (char*) SIMIX_context_stack_new();
355 raw_makecontext(context->malloced_stack,
356 smx_context_usable_stack_size,
357 (void_f_pvoid_t)smx_ctx_raw_wrapper, context);
360 if(process != NULL && raw_maestro_context==NULL)
361 raw_maestro_context = context;
364 MC_ignore_heap(&(raw_maestro_context->stack_top), sizeof(raw_maestro_context->stack_top));
368 return (smx_context_t) context;
372 * \brief Destroys a raw context.
373 * \param context a raw context
375 static void smx_ctx_raw_free(smx_context_t context)
378 SIMIX_context_stack_delete(((smx_ctx_raw_t) context)->malloced_stack);
380 smx_ctx_base_free(context);
384 * \brief Wrapper for the main function of a context.
385 * \param context a raw context
387 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
389 (context->super.code) (context->super.argc, context->super.argv);
391 smx_ctx_raw_stop((smx_context_t) context);
395 * \brief Stops a raw context.
397 * This function is called when the main function of the context if finished.
399 * \param context the current context
401 static void smx_ctx_raw_stop(smx_context_t context)
403 smx_ctx_base_stop(context);
404 simix_global->context_factory->suspend(context);
408 * \brief Suspends a running context and resumes another one or returns to
410 * \param context the current context
412 static void smx_ctx_raw_suspend_serial(smx_context_t context)
414 /* determine the next context */
415 smx_context_t next_context;
417 #ifdef TIME_BENCH_PER_SR
418 i = ++raw_process_index;
420 i = raw_process_index++;
422 if (i < xbt_dynar_length(simix_global->process_to_run)) {
423 /* execute the next process */
424 XBT_DEBUG("Run next process");
425 next_context = xbt_dynar_get_as(
426 simix_global->process_to_run, i, smx_process_t)->context;
429 /* all processes were run, return to maestro */
430 XBT_DEBUG("No more process to run");
431 next_context = (smx_context_t) raw_maestro_context;
433 SIMIX_context_set_current(next_context);
434 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
435 ((smx_ctx_raw_t) next_context)->stack_top);
439 * \brief Resumes sequentially all processes ready to run.
440 * \param first_process the first process to resume
442 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
444 smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
445 SIMIX_context_set_current((smx_context_t) context);
446 raw_swapcontext(&raw_maestro_context->stack_top,
447 ((smx_ctx_raw_t) context)->stack_top);
450 #ifdef TIME_BENCH_PER_SR
451 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
453 smx_process_t process;
457 unsigned long num_proc = xbt_dynar_length(simix_global->process_to_run);
459 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
462 time_thread_ssr[0] = 0;
463 xbt_dynar_foreach(processes, cursor, process){
464 XBT_VERB("Schedule item %u of %lu",cursor,num_proc);
465 if(cursor >= t * data_size + data_size){
466 if(time_thread_ssr[t] > tmax)
467 tmax = time_thread_ssr[t];
469 time_thread_ssr[t] = 0;
473 ((smx_ctx_raw_t)process->context)->thread = t;
474 time_thread_sr[t] = 0;
477 xbt_os_cputimer_start(timer);
478 smx_ctx_raw_resume_serial(process);
479 xbt_os_cputimer_stop(timer);
480 elapsed = xbt_os_timer_elapsed(timer);
481 time_thread_ssr[t] += elapsed;
482 time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
488 if(time_thread_ssr[t] > tmax)
489 tmax = time_thread_ssr[t];
491 for(cursor=0; cursor <= t; cursor++){
492 XBT_VERB("Time SSR thread %u = %f (max %f)", cursor, time_thread_ssr[cursor], tmax);
493 time_wasted_ssr += tmax - time_thread_ssr[cursor];
497 void smx_ctx_raw_new_sr(void)
503 for(i=0; i < NUM_THREADS; i++){
504 if(time_thread_sr[i] > tmax)
505 tmax = time_thread_sr[i];
508 for(i=0; i < NUM_THREADS; i++){
509 XBT_CRITICAL("Time SR thread %u = %f (max %f)", i, time_thread_sr[i], tmax);
510 time_wasted_sr += tmax - time_thread_sr[i];
513 XBT_CRITICAL("Total time SR %u = %f, %d", sr_count, tmax, xbt_dynar_length(simix_global->process_that_ran));
514 XBT_CRITICAL("New scheduling round");
518 * \brief Resumes sequentially all processes ready to run.
520 static void smx_ctx_raw_runall_serial(void)
522 smx_process_t first_process =
523 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
524 raw_process_index = 1;
526 /* execute the first process */
527 smx_ctx_raw_resume_serial(first_process);
532 * \brief Suspends a running context and resumes another one or returns to
533 * the main function of the current worker thread.
534 * \param context the context of the current worker thread
536 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
538 #ifdef CONTEXT_THREADS
539 /* determine the next context */
540 smx_process_t next_work = (smx_process_t) xbt_parmap_next(raw_parmap);
541 smx_context_t next_context;
542 raw_stack_t next_stack;
544 if (next_work != NULL) {
545 /* there is a next process to resume */
546 XBT_DEBUG("Run next process");
547 next_context = next_work->context;
548 next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
551 /* all processes were run, go to the barrier */
552 XBT_DEBUG("No more processes to run");
554 unsigned long worker_id =
555 (unsigned long)(uintptr_t) xbt_os_thread_get_specific(raw_worker_id_key);
557 next_context = (smx_context_t)raw_workers_context[worker_id];
558 XBT_DEBUG("Restoring worker stack %lu (working threads = %lu)",
559 worker_id, raw_threads_working);
560 next_stack = ((smx_ctx_raw_t)next_context)->stack_top;
563 SIMIX_context_set_current(next_context);
564 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
569 * \brief Resumes sequentially in the current worker thread the processes ready
571 * \param first_process the first process to resume
573 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
575 #ifdef CONTEXT_THREADS
576 unsigned long worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
577 xbt_os_thread_set_specific(raw_worker_id_key, (void*)(uintptr_t) worker_id);
578 smx_ctx_raw_t worker_context = (smx_ctx_raw_t)SIMIX_context_self();
579 raw_workers_context[worker_id] = worker_context;
580 XBT_DEBUG("Saving worker stack %lu", worker_id);
581 raw_stack_t* worker_stack = &(worker_context)->stack_top;
584 smx_context_t context = first_process->context;
585 SIMIX_context_set_current(context);
586 raw_swapcontext(worker_stack, ((smx_ctx_raw_t) context)->stack_top);
591 * \brief Resumes in parallel all processes ready to run.
593 static void smx_ctx_raw_runall_parallel(void)
595 #ifdef CONTEXT_THREADS
596 raw_threads_working = 0;
597 xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
598 simix_global->process_to_run);
600 xbt_die("You asked for a parallel execution, but you don't have any threads.");
605 * \brief Resumes all processes ready to run.
607 #ifdef ADAPTIVE_THRESHOLD
608 static void smx_ctx_raw_runall(void)
610 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
611 unsigned long threshold = SIMIX_context_get_parallel_threshold();
612 reached_seq_limit = (seq_sched_round % SCHED_ROUND_LIMIT == 0);
613 reached_par_limit = (par_sched_round % SCHED_ROUND_LIMIT == 0);
615 if(reached_seq_limit && reached_par_limit){
616 par_ratio = (par_proc_that_ran != 0) ? (par_time / (double)par_proc_that_ran) : 0;
617 seq_ratio = (seq_proc_that_ran != 0) ? (seq_time / (double)seq_proc_that_ran) : 0;
618 if(seq_ratio > par_ratio){
619 if(nb_processes < avg_par_proc) {
620 threshold = (threshold>2) ? threshold - 1 : threshold ;
621 SIMIX_context_set_parallel_threshold(threshold);
624 if(nb_processes > avg_seq_proc){
625 SIMIX_context_set_parallel_threshold(threshold+1);
630 //XBT_CRITICAL("Thresh: %d", SIMIX_context_get_parallel_threshold());
631 if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
632 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
633 if(nb_processes < par_window){
635 xbt_os_walltimer_start(round_time);
636 smx_ctx_raw_runall_parallel();
637 xbt_os_walltimer_stop(round_time);
638 par_time += xbt_os_timer_elapsed(round_time);
640 prev_avg_par_proc = avg_par_proc;
641 delta = nb_processes - avg_par_proc;
642 avg_par_proc = (par_sched_round==1) ? nb_processes : avg_par_proc + delta / (double) par_sched_round;
644 if(par_sched_round>=2){
645 s_par_proc = s_par_proc + (nb_processes - prev_avg_par_proc) * delta;
646 sd_par_proc = sqrt(s_par_proc / (par_sched_round-1));
647 par_window = (int) (avg_par_proc + sd_par_proc);
652 par_proc_that_ran += nb_processes;
654 smx_ctx_raw_runall_parallel();
657 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
658 if(nb_processes > seq_window){
660 xbt_os_walltimer_start(round_time);
661 smx_ctx_raw_runall_serial();
662 xbt_os_walltimer_stop(round_time);
663 seq_time += xbt_os_timer_elapsed(round_time);
665 prev_avg_seq_proc = avg_seq_proc;
666 delta = (nb_processes-avg_seq_proc);
667 avg_seq_proc = (seq_sched_round==1) ? nb_processes : avg_seq_proc + delta / (double) seq_sched_round;
669 if(seq_sched_round>=2){
670 s_seq_proc = s_seq_proc + (nb_processes - prev_avg_seq_proc)*delta;
671 sd_seq_proc = sqrt(s_seq_proc / (seq_sched_round-1));
672 seq_window = (int) (avg_seq_proc - sd_seq_proc);
677 seq_proc_that_ran += nb_processes;
679 smx_ctx_raw_runall_serial();
686 static void smx_ctx_raw_runall(void)
688 #ifdef TIME_BENCH_ENTIRE_SRS
690 timer = xbt_os_timer_new();
693 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
694 if (SIMIX_context_is_parallel()
695 && (unsigned long) SIMIX_context_get_parallel_threshold() < nb_processes) {
696 XBT_DEBUG("Runall // %lu", nb_processes);
697 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
699 #ifdef TIME_BENCH_ENTIRE_SRS
700 xbt_os_walltimer_start(timer);
703 smx_ctx_raw_runall_parallel();
705 #ifdef TIME_BENCH_ENTIRE_SRS
706 xbt_os_walltimer_stop(timer);
707 elapsed = xbt_os_timer_elapsed(timer);
710 XBT_DEBUG("Runall serial %lu", nb_processes);
711 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
713 #ifdef TIME_BENCH_PER_SR
714 smx_ctx_raw_runall_serial(simix_global->process_to_run);
717 #ifdef TIME_BENCH_ENTIRE_SRS
718 xbt_os_walltimer_start(timer);
721 smx_ctx_raw_runall_serial();
723 #ifdef TIME_BENCH_ENTIRE_SRS
724 xbt_os_walltimer_stop(timer);
725 elapsed = xbt_os_timer_elapsed(timer);
730 #ifdef TIME_BENCH_ENTIRE_SRS
731 XBT_CRITICAL("Total time SR %u = %f, %d", sr_count, elapsed, nb_processes);