1 /* context_raw - fast context switching inspired from System V ucontextes */
3 /* Copyright (c) 2009, 2010. 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"
13 #ifdef HAVE_VALGRIND_VALGRIND_H
14 # include <valgrind/valgrind.h>
15 #endif /* HAVE_VALGRIND_VALGRIND_H */
17 typedef char * raw_stack_t;
18 typedef void (*rawctx_entry_point_t)(void *);
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 #ifdef HAVE_VALGRIND_VALGRIND_H
25 unsigned int valgrind_stack_id; /* the valgrind stack id */
27 #ifdef TIME_BENCH_PER_SR
28 unsigned int thread; /* Just for measuring purposes */
30 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
32 #ifdef CONTEXT_THREADS
33 static xbt_parmap_t raw_parmap;
34 static raw_stack_t* raw_workers_stacks; /* space to save the worker stack in each thread */
35 static unsigned long raw_threads_working; /* number of threads that have started their work */
36 static xbt_os_thread_key_t raw_worker_id_key; /* thread-specific storage for the thread id */
39 static unsigned long raw_process_index = 0; /* index of the next process to run in the
40 * list of runnable processes */
41 static smx_ctx_raw_t raw_maestro_context;
43 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
44 rawctx_entry_point_t entry_point, void* arg);
45 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
49 #if defined(APPLE) || defined(_WIN32)
51 ".globl _raw_makecontext\n"
55 ".globl raw_makecontext\n"
56 ".type raw_makecontext,@function\n"
59 " movl 4(%esp),%eax\n" /* stack */
60 " addl 8(%esp),%eax\n" /* size */
61 " movl 12(%esp),%ecx\n" /* func */
62 " movl 16(%esp),%edx\n" /* arg */
63 " movl %edx, -4(%eax)\n"
64 " movl $0, -8(%eax)\n" /* @return for func */
65 " movl %ecx,-12(%eax)\n"
66 " movl $0, -16(%eax)\n" /* ebp */
67 " movl $0, -20(%eax)\n" /* ebx */
68 " movl $0, -24(%eax)\n" /* esi */
69 " movl $0, -28(%eax)\n" /* edi */
75 #if defined(APPLE) || defined(_WIN32)
77 ".globl _raw_swapcontext\n"
81 ".globl raw_swapcontext\n"
82 ".type raw_swapcontext,@function\n"
85 " movl 4(%esp),%eax\n" /* old */
86 " movl 8(%esp),%edx\n" /* new */
99 #elif PROCESSOR_x86_64
103 ".globl _raw_makecontext\n"
104 "_raw_makecontext:\n"
105 #elif defined(_WIN32)
107 ".globl raw_makecontext\n"
111 ".globl raw_makecontext\n"
112 ".type raw_makecontext,@function\n"
113 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
115 " movq %rdi,%rax\n" /* stack */
116 " addq %rsi,%rax\n" /* size */
117 " movq $0, -8(%rax)\n" /* @return for func */
118 " movq %rdx,-16(%rax)\n" /* func */
119 " movq %rcx,-24(%rax)\n" /* arg/rdi */
120 " movq $0, -32(%rax)\n" /* rsi */
121 " movq $0, -40(%rax)\n" /* rdx */
122 " movq $0, -48(%rax)\n" /* rcx */
123 " movq $0, -56(%rax)\n" /* r8 */
124 " movq $0, -64(%rax)\n" /* r9 */
125 " movq $0, -72(%rax)\n" /* rbp */
126 " movq $0, -80(%rax)\n" /* rbx */
127 " movq $0, -88(%rax)\n" /* r12 */
128 " movq $0, -96(%rax)\n" /* r13 */
129 " movq $0, -104(%rax)\n" /* r14 */
130 " movq $0, -112(%rax)\n" /* r15 */
138 ".globl _raw_swapcontext\n"
139 "_raw_swapcontext:\n"
140 #elif defined(_WIN32)
142 ".globl raw_swapcontext\n"
146 ".globl raw_swapcontext\n"
147 ".type raw_swapcontext,@function\n"
148 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
162 " movq %rsp,(%rdi)\n" /* old */
163 " movq %rsi,%rsp\n" /* new */
180 /* If you implement raw contexts for other processors, don't forget to
181 update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */
183 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
184 rawctx_entry_point_t entry_point, void* arg) {
188 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
194 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
196 #ifdef TIME_BENCH_PER_SR
197 #include "xbt/xbt_os_time.h"
198 #define NUM_THREADS 4
199 static xbt_os_timer_t timer;
200 static double time_thread_sr[NUM_THREADS];
201 static double time_thread_ssr[NUM_THREADS];
202 static double time_wasted_sr = 0;
203 static double time_wasted_ssr = 0;
204 static unsigned int sr_count = 0;
205 static unsigned int ssr_count = 0;
206 static char new_sr = 0;
209 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
210 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
211 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
212 char **argv, void_pfn_smxprocess_t cleanup_func, void *data);
213 static void smx_ctx_raw_free(smx_context_t context);
214 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
215 static void smx_ctx_raw_stop(smx_context_t context);
216 static void smx_ctx_raw_suspend_serial(smx_context_t context);
217 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
218 static void smx_ctx_raw_runall_serial(void);
219 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
220 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
221 static void smx_ctx_raw_runall_parallel(void);
222 static void smx_ctx_raw_runall(void);
225 * \brief Initializes the raw context factory.
226 * \param factory where to initialize the factory
228 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
231 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
232 smx_ctx_base_factory_init(factory);
234 (*factory)->finalize = smx_ctx_raw_factory_finalize;
235 (*factory)->create_context = smx_ctx_raw_create_context;
236 /* Do not overload that method (*factory)->finalize */
237 (*factory)->free = smx_ctx_raw_free;
238 (*factory)->stop = smx_ctx_raw_stop;
239 (*factory)->name = "smx_raw_context_factory";
241 if (SIMIX_context_is_parallel()) {
242 #ifdef CONTEXT_THREADS
243 int nthreads = SIMIX_context_get_nthreads();
244 raw_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
245 raw_workers_stacks = xbt_new(raw_stack_t, nthreads);
246 xbt_os_thread_key_create(&raw_worker_id_key);
248 if (SIMIX_context_get_parallel_threshold() > 1) {
249 /* choose dynamically */
250 (*factory)->runall = smx_ctx_raw_runall;
251 (*factory)->suspend = NULL;
254 /* always parallel */
255 (*factory)->runall = smx_ctx_raw_runall_parallel;
256 (*factory)->suspend = smx_ctx_raw_suspend_parallel;
261 (*factory)->runall = smx_ctx_raw_runall_serial;
262 (*factory)->suspend = smx_ctx_raw_suspend_serial;
264 #ifdef TIME_BENCH_PER_SR
265 timer = xbt_os_timer_new();
270 * \brief Finalizes the raw context factory.
271 * \param factory the raw context factory
273 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
275 #ifdef TIME_BENCH_PER_SR
276 XBT_CRITICAL("Total wasted time in %u SR: %lf", sr_count, time_wasted_sr);
277 XBT_CRITICAL("Total wasted time in %u SSR: %lf", ssr_count, time_wasted_ssr);
280 #ifdef CONTEXT_THREADS
282 xbt_parmap_destroy(raw_parmap);
283 xbt_free(raw_workers_stacks);
285 return smx_ctx_base_factory_finalize(factory);
289 * \brief Creates a new raw context.
290 * \param code main function of this context or NULL to create the maestro
292 * \param argc argument number
293 * \param argv arguments to pass to the main function
294 * \param cleanup_func a function to call to free the user data when the
296 * \param data user data
299 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
300 void_pfn_smxprocess_t cleanup_func,
304 smx_ctx_raw_t context =
305 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
306 sizeof(s_smx_ctx_raw_t),
313 /* if the user provided a function for the process then use it,
314 otherwise it is the context for maestro */
316 context->malloced_stack = xbt_malloc0(smx_context_stack_size);
318 raw_makecontext(context->malloced_stack, smx_context_stack_size,
319 (void_f_pvoid_t) smx_ctx_raw_wrapper, context);
321 #ifdef HAVE_VALGRIND_VALGRIND_H
322 context->valgrind_stack_id =
323 VALGRIND_STACK_REGISTER(context->malloced_stack,
324 context->malloced_stack + smx_context_stack_size);
325 #endif /* HAVE_VALGRIND_VALGRIND_H */
328 raw_maestro_context = context;
331 MC_ignore_heap(&(raw_maestro_context->stack_top), sizeof(raw_maestro_context->stack_top));
335 return (smx_context_t) context;
339 * \brief Destroys a raw context.
340 * \param context a raw context
342 static void smx_ctx_raw_free(smx_context_t context)
346 #ifdef HAVE_VALGRIND_VALGRIND_H
347 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
348 context)->valgrind_stack_id);
349 #endif /* HAVE_VALGRIND_VALGRIND_H */
351 free(((smx_ctx_raw_t) context)->malloced_stack);
353 smx_ctx_base_free(context);
357 * \brief Wrapper for the main function of a context.
358 * \param context a raw context
360 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
362 (context->super.code) (context->super.argc, context->super.argv);
364 smx_ctx_raw_stop((smx_context_t) context);
368 * \brief Stops a raw context.
370 * This function is called when the main function of the context if finished.
372 * \param context the current context
374 static void smx_ctx_raw_stop(smx_context_t context)
376 smx_ctx_base_stop(context);
377 simix_global->context_factory->suspend(context);
381 * \brief Suspends a running context and resumes another one or returns to
383 * \param context the current context
385 static void smx_ctx_raw_suspend_serial(smx_context_t context)
387 /* determine the next context */
388 smx_context_t next_context;
389 unsigned long int i = raw_process_index++;
391 if (i < xbt_dynar_length(simix_global->process_to_run)) {
392 /* execute the next process */
393 XBT_DEBUG("Run next process");
394 next_context = xbt_dynar_get_as(
395 simix_global->process_to_run, i, smx_process_t)->context;
398 /* all processes were run, return to maestro */
399 XBT_DEBUG("No more process to run");
400 next_context = (smx_context_t) raw_maestro_context;
402 SIMIX_context_set_current(next_context);
403 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
404 ((smx_ctx_raw_t) next_context)->stack_top);
408 * \brief Resumes sequentially all processes ready to run.
409 * \param first_process the first process to resume
411 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
413 smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
414 SIMIX_context_set_current((smx_context_t) context);
415 raw_swapcontext(&raw_maestro_context->stack_top,
416 ((smx_ctx_raw_t) context)->stack_top);
419 #ifdef TIME_BENCH_PER_SR
420 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
422 smx_process_t process;
427 unsigned long num_proc = xbt_dynar_length(processes);
429 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
432 time_thread_ssr[0] = 0;
433 xbt_dynar_foreach(processes, cursor, process) {
434 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
435 if(cursor >= t * data_size + data_size){
436 if(time_thread_ssr[t] > tmax)
437 tmax = time_thread_ssr[t];
439 time_thread_ssr[t] = 0;
443 ((smx_ctx_raw_t)process->context)->thread = t;
444 time_thread_sr[t] = 0;
447 xbt_os_timer_start(timer);
448 smx_ctx_raw_resume(process);
449 xbt_os_timer_stop(timer);
450 elapsed = xbt_os_timer_elapsed(timer);
451 time_thread_ssr[t] += elapsed;
452 time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
458 if(time_thread_ssr[t] > tmax)
459 tmax = time_thread_ssr[t];
461 for(cursor=0; cursor <= t; cursor++){
462 XBT_VERB("Time SSR thread %u = %lf (max %lf)", cursor, time_thread_ssr[cursor], tmax);
463 time_wasted_ssr += tmax - time_thread_ssr[cursor];
467 void smx_ctx_raw_new_sr(void);
468 void smx_ctx_raw_new_sr(void)
474 for(i=0; i < NUM_THREADS; i++){
475 if(time_thread_sr[i] > tmax)
476 tmax = time_thread_sr[i];
479 for(i=0; i < NUM_THREADS; i++){
480 XBT_VERB("Time SR thread %u = %lf (max %lf)", i, time_thread_sr[i], tmax);
481 time_wasted_sr += tmax - time_thread_sr[i];
484 XBT_VERB("New scheduling round");
489 * \brief Resumes sequentially all processes ready to run.
491 static void smx_ctx_raw_runall_serial(void)
493 smx_process_t first_process =
494 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
495 raw_process_index = 1;
497 /* execute the first process */
498 smx_ctx_raw_resume_serial(first_process);
503 * \brief Suspends a running context and resumes another one or returns to
504 * the main function of the current worker thread.
505 * \param context the context of the current worker thread
507 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
509 #ifdef CONTEXT_THREADS
510 /* determine the next context */
511 smx_process_t next_work = xbt_parmap_next(raw_parmap);
512 smx_context_t next_context;
513 raw_stack_t next_stack;
515 if (next_work != NULL) {
516 /* there is a next process to resume */
517 XBT_DEBUG("Run next process");
518 next_context = next_work->context;
519 next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
522 /* all processes were run, go to the barrier */
523 XBT_DEBUG("No more processes to run");
524 next_context = (smx_context_t) raw_maestro_context;
525 unsigned long worker_id =
526 (unsigned long) xbt_os_thread_get_specific(raw_worker_id_key);
527 XBT_DEBUG("Restoring worker stack %lu (working threads = %lu)",
528 worker_id, raw_threads_working);
529 next_stack = raw_workers_stacks[worker_id];
532 SIMIX_context_set_current(next_context);
533 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
538 * \brief Resumes sequentially in the current worker thread the processes ready
540 * \param first_process the first process to resume
542 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
544 #ifdef CONTEXT_THREADS
545 unsigned long worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
546 xbt_os_thread_set_specific(raw_worker_id_key, (void*) worker_id);
547 XBT_DEBUG("Saving worker stack %lu", worker_id);
548 raw_stack_t* worker_stack = &raw_workers_stacks[worker_id];
550 smx_context_t context = first_process->context;
551 SIMIX_context_set_current(context);
552 raw_swapcontext(worker_stack, ((smx_ctx_raw_t) context)->stack_top);
557 * \brief Resumes in parallel all processes ready to run.
559 static void smx_ctx_raw_runall_parallel(void)
561 #ifdef CONTEXT_THREADS
562 raw_threads_working = 0;
563 xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
564 simix_global->process_to_run);
566 xbt_die("You asked for a parallel execution, but you don't have any threads.")
571 * \brief Resumes all processes ready to run.
573 static void smx_ctx_raw_runall(void)
575 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
576 if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
577 XBT_DEBUG("Runall // %lu", nb_processes);
578 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
579 smx_ctx_raw_runall_parallel();
581 XBT_DEBUG("Runall serial %lu", nb_processes);
582 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
583 smx_ctx_raw_runall_serial();