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"
12 #ifdef HAVE_VALGRIND_VALGRIND_H
13 # include <valgrind/valgrind.h>
14 #endif /* HAVE_VALGRIND_VALGRIND_H */
16 typedef char * raw_stack_t;
17 typedef void (*rawctx_entry_point_t)(void *);
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 */
27 unsigned int thread; /* Just for measuring purposes */
29 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
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 */
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;
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);
48 #if defined(APPLE) || defined(_WIN32)
50 ".globl _raw_makecontext\n"
54 ".globl raw_makecontext\n"
55 ".type raw_makecontext,@function\n"
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 */
74 #if defined(APPLE) || defined(_WIN32)
76 ".globl _raw_swapcontext\n"
80 ".globl raw_swapcontext\n"
81 ".type raw_swapcontext,@function\n"
84 " movl 4(%esp),%eax\n" /* old */
85 " movl 8(%esp),%edx\n" /* new */
98 #elif PROCESSOR_x86_64
102 ".globl _raw_makecontext\n"
103 "_raw_makecontext:\n"
104 #elif defined(_WIN32)
106 ".globl raw_makecontext\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 */
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 */
137 ".globl _raw_swapcontext\n"
138 "_raw_swapcontext:\n"
139 #elif defined(_WIN32)
141 ".globl raw_swapcontext\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 */
161 " movq %rsp,(%rdi)\n" /* old */
162 " movq %rsi,%rsp\n" /* new */
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 */
182 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
183 rawctx_entry_point_t entry_point, void* arg) {
187 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
193 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
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;
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);
224 * \brief Initializes the raw context factory.
225 * \param factory where to initialize the factory
227 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
229 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
230 smx_ctx_base_factory_init(factory);
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";
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);
246 if (SIMIX_context_get_parallel_threshold() > 1) {
247 /* choose dynamically */
248 (*factory)->runall = smx_ctx_raw_runall;
249 (*factory)->suspend = NULL;
252 /* always parallel */
253 (*factory)->runall = smx_ctx_raw_runall_parallel;
254 (*factory)->suspend = smx_ctx_raw_suspend_parallel;
259 (*factory)->runall = smx_ctx_raw_runall_serial;
260 (*factory)->suspend = smx_ctx_raw_suspend_serial;
263 timer = xbt_os_timer_new();
268 * \brief Finalizes the raw context factory.
269 * \param factory the raw context factory
271 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
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);
278 #ifdef CONTEXT_THREADS
280 xbt_parmap_destroy(raw_parmap);
281 xbt_free(raw_workers_stacks);
283 return smx_ctx_base_factory_finalize(factory);
287 * \brief Creates a new raw context.
288 * \param code main function of this context or NULL to create the maestro
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
294 * \param data user data
297 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
298 void_pfn_smxprocess_t cleanup_func,
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),
311 /* if the user provided a function for the process then use it,
312 otherwise it is the context for maestro */
314 context->malloced_stack = xbt_malloc0(smx_context_stack_size);
316 raw_makecontext(context->malloced_stack, smx_context_stack_size,
317 (void_f_pvoid_t) smx_ctx_raw_wrapper, context);
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 */
326 raw_maestro_context = context;
329 return (smx_context_t) context;
333 * \brief Destroys a raw context.
334 * \param context a raw context
336 static void smx_ctx_raw_free(smx_context_t context)
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 */
345 free(((smx_ctx_raw_t) context)->malloced_stack);
347 smx_ctx_base_free(context);
351 * \brief Wrapper for the main function of a context.
352 * \param context a raw context
354 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
356 (context->super.code) (context->super.argc, context->super.argv);
358 smx_ctx_raw_stop((smx_context_t) context);
362 * \brief Stops a raw context.
364 * This function is called when the main function of the context if finished.
366 * \param context the current context
368 static void smx_ctx_raw_stop(smx_context_t context)
370 smx_ctx_base_stop(context);
371 simix_global->context_factory->suspend(context);
375 * \brief Suspends a running context and resumes another one or returns to
377 * \param context the current context
379 static void smx_ctx_raw_suspend_serial(smx_context_t context)
381 /* determine the next context */
382 smx_context_t next_context;
383 unsigned long int i = raw_process_index++;
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;
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;
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);
402 * \brief Resumes sequentially all processes ready to run.
403 * \param first_process the first process to resume
405 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
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);
414 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
416 smx_process_t process;
421 unsigned long num_proc = xbt_dynar_length(processes);
423 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
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];
433 time_thread_ssr[t] = 0;
437 ((smx_ctx_raw_t)process->context)->thread = t;
438 time_thread_sr[t] = 0;
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;
452 if(time_thread_ssr[t] > tmax)
453 tmax = time_thread_ssr[t];
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];
461 void smx_ctx_raw_new_sr(void);
462 void smx_ctx_raw_new_sr(void)
468 for(i=0; i < NUM_THREADS; i++){
469 if(time_thread_sr[i] > tmax)
470 tmax = time_thread_sr[i];
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];
478 XBT_VERB("New scheduling round");
483 * \brief Resumes sequentially all processes ready to run.
485 static void smx_ctx_raw_runall_serial(void)
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;
492 /* execute the first process */
493 smx_ctx_raw_resume_serial(first_process);
499 * \brief Stops a raw context.
501 * This function is called when the main function of the context if finished.
503 * \param context the context of the current worker thread
505 static void smx_ctx_raw_stop_parallel(smx_context_t context)
507 smx_ctx_base_stop(context);
508 smx_ctx_raw_suspend_parallel(context);
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
516 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
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;
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;
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];
541 SIMIX_context_set_current(next_context);
542 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
547 * \brief Resumes sequentially in the current worker thread the processes ready
549 * \param first_process the first process to resume
551 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
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];
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);
566 * \brief Resumes in parallel all processes ready to run.
568 static void smx_ctx_raw_runall_parallel(void)
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);
578 * \brief Resumes all processes ready to run.
580 static void smx_ctx_raw_runall(void)
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();
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();