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"
13 typedef char * raw_stack_t;
14 typedef void (*rawctx_entry_point_t)(void *);
16 typedef struct s_smx_ctx_raw {
17 s_smx_ctx_base_t super; /* Fields of super implementation */
18 char *malloced_stack; /* malloced area containing the stack */
19 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
20 #ifdef TIME_BENCH_PER_SR
21 unsigned int thread; /* Just for measuring purposes */
23 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
25 #ifdef CONTEXT_THREADS
26 static xbt_parmap_t raw_parmap;
27 static smx_ctx_raw_t* raw_workers_context; /* space to save the worker context in each thread */
28 static unsigned long raw_threads_working; /* number of threads that have started their work */
29 static xbt_os_thread_key_t raw_worker_id_key; /* thread-specific storage for the thread id */
32 static unsigned long raw_process_index = 0; /* index of the next process to run in the
33 * list of runnable processes */
34 static smx_ctx_raw_t raw_maestro_context;
36 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
37 rawctx_entry_point_t entry_point, void* arg);
38 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
44 ".globl _raw_makecontext\n"
48 ".globl raw_makecontext\n"
52 ".globl raw_makecontext\n"
53 ".type raw_makecontext,@function\n"
54 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
56 " mov %rdi,%rax\n" /* stack */
57 " add %rsi,%rax\n" /* size */
58 " andq $-16, %rax\n" /* align stack */
59 " movq $0, -8(%rax)\n" /* @return for func */
60 " mov %rdx,-16(%rax)\n" /* func */
61 " mov %rcx,-24(%rax)\n" /* arg/rdi */
62 " movq $0, -32(%rax)\n" /* rsi */
63 " movq $0, -40(%rax)\n" /* rdx */
64 " movq $0, -48(%rax)\n" /* rcx */
65 " movq $0, -56(%rax)\n" /* r8 */
66 " movq $0, -64(%rax)\n" /* r9 */
67 " movq $0, -72(%rax)\n" /* rbp */
68 " movq $0, -80(%rax)\n" /* rbx */
69 " movq $0, -88(%rax)\n" /* r12 */
70 " movq $0, -96(%rax)\n" /* r13 */
71 " movq $0, -104(%rax)\n" /* r14 */
72 " movq $0, -112(%rax)\n" /* r15 */
80 ".globl _raw_swapcontext\n"
84 ".globl raw_swapcontext\n"
88 ".globl raw_swapcontext\n"
89 ".type raw_swapcontext,@function\n"
90 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
104 " mov %rsp,(%rdi)\n" /* old */
105 " mov %rsi,%rsp\n" /* new */
122 #if defined(APPLE) || defined(_WIN32)
124 ".globl _raw_makecontext\n"
125 "_raw_makecontext:\n"
128 ".globl raw_makecontext\n"
129 ".type raw_makecontext,@function\n"
132 " movl 4(%esp),%eax\n" /* stack */
133 " addl 8(%esp),%eax\n" /* size */
134 " andl $-16, %eax\n" /* align stack */
135 " movl 12(%esp),%ecx\n" /* func */
136 " movl 16(%esp),%edx\n" /* arg */
137 " movl %edx, -4(%eax)\n"
138 " movl $0, -8(%eax)\n" /* @return for func */
139 " movl %ecx,-12(%eax)\n"
140 " movl $0, -16(%eax)\n" /* ebp */
141 " movl $0, -20(%eax)\n" /* ebx */
142 " movl $0, -24(%eax)\n" /* esi */
143 " movl $0, -28(%eax)\n" /* edi */
149 #if defined(APPLE) || defined(_WIN32)
151 ".globl _raw_swapcontext\n"
152 "_raw_swapcontext:\n"
155 ".globl raw_swapcontext\n"
156 ".type raw_swapcontext,@function\n"
159 " movl 4(%esp),%eax\n" /* old */
160 " movl 8(%esp),%edx\n" /* new */
165 " movl %esp,(%eax)\n"
176 /* If you implement raw contexts for other processors, don't forget to
177 update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */
179 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
180 rawctx_entry_point_t entry_point, void* arg) {
184 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
190 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
192 #ifdef TIME_BENCH_PER_SR
193 #include "xbt/xbt_os_time.h"
194 #define NUM_THREADS 4
195 static xbt_os_timer_t timer;
196 static double time_thread_sr[NUM_THREADS];
197 static double time_thread_ssr[NUM_THREADS];
198 static double time_wasted_sr = 0;
199 static double time_wasted_ssr = 0;
200 static unsigned int sr_count = 0;
201 static unsigned int ssr_count = 0;
202 static char new_sr = 0;
205 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
206 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
207 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
208 char **argv, void_pfn_smxprocess_t cleanup_func, smx_process_t process);
209 static void smx_ctx_raw_free(smx_context_t context);
210 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
211 static void smx_ctx_raw_stop(smx_context_t context);
212 static void smx_ctx_raw_suspend_serial(smx_context_t context);
213 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
214 static void smx_ctx_raw_runall_serial(void);
215 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
216 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
217 static void smx_ctx_raw_runall_parallel(void);
218 static void smx_ctx_raw_runall(void);
221 * \brief Initializes the raw context factory.
222 * \param factory where to initialize the factory
224 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
227 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
228 smx_ctx_base_factory_init(factory);
230 (*factory)->finalize = smx_ctx_raw_factory_finalize;
231 (*factory)->create_context = smx_ctx_raw_create_context;
232 /* Do not overload that method (*factory)->finalize */
233 (*factory)->free = smx_ctx_raw_free;
234 (*factory)->stop = smx_ctx_raw_stop;
235 (*factory)->name = "smx_raw_context_factory";
237 if (SIMIX_context_is_parallel()) {
238 #ifdef CONTEXT_THREADS
239 int nthreads = SIMIX_context_get_nthreads();
240 xbt_os_thread_key_create(&raw_worker_id_key);
241 raw_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
242 raw_workers_context = xbt_new(smx_ctx_raw_t, nthreads);
243 raw_maestro_context=NULL;
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;
262 #ifdef TIME_BENCH_PER_SR
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)
273 #ifdef TIME_BENCH_PER_SR
274 XBT_CRITICAL("Total wasted time in %u SR: %f", sr_count, time_wasted_sr);
275 XBT_CRITICAL("Total wasted time in %u SSR: %f", ssr_count, time_wasted_ssr);
278 #ifdef CONTEXT_THREADS
280 xbt_parmap_destroy(raw_parmap);
281 xbt_free(raw_workers_context);
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 process SIMIX process
297 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
298 void_pfn_smxprocess_t cleanup_func,
299 smx_process_t process)
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 = SIMIX_context_stack_new();
316 raw_makecontext(context->malloced_stack,
317 smx_context_usable_stack_size,
318 (void_f_pvoid_t)smx_ctx_raw_wrapper, context);
321 if(process != NULL && raw_maestro_context==NULL)
322 raw_maestro_context = context;
325 MC_ignore_heap(&(raw_maestro_context->stack_top), sizeof(raw_maestro_context->stack_top));
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)
339 SIMIX_context_stack_delete(((smx_ctx_raw_t) context)->malloced_stack);
341 smx_ctx_base_free(context);
345 * \brief Wrapper for the main function of a context.
346 * \param context a raw context
348 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
350 (context->super.code) (context->super.argc, context->super.argv);
352 smx_ctx_raw_stop((smx_context_t) context);
356 * \brief Stops a raw context.
358 * This function is called when the main function of the context if finished.
360 * \param context the current context
362 static void smx_ctx_raw_stop(smx_context_t context)
364 smx_ctx_base_stop(context);
365 simix_global->context_factory->suspend(context);
369 * \brief Suspends a running context and resumes another one or returns to
371 * \param context the current context
373 static void smx_ctx_raw_suspend_serial(smx_context_t context)
375 /* determine the next context */
376 smx_context_t next_context;
377 unsigned long int i = raw_process_index++;
379 if (i < xbt_dynar_length(simix_global->process_to_run)) {
380 /* execute the next process */
381 XBT_DEBUG("Run next process");
382 next_context = xbt_dynar_get_as(
383 simix_global->process_to_run, i, smx_process_t)->context;
386 /* all processes were run, return to maestro */
387 XBT_DEBUG("No more process to run");
388 next_context = (smx_context_t) raw_maestro_context;
390 SIMIX_context_set_current(next_context);
391 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
392 ((smx_ctx_raw_t) next_context)->stack_top);
396 * \brief Resumes sequentially all processes ready to run.
397 * \param first_process the first process to resume
399 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
401 smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
402 SIMIX_context_set_current((smx_context_t) context);
403 raw_swapcontext(&raw_maestro_context->stack_top,
404 ((smx_ctx_raw_t) context)->stack_top);
407 #ifdef TIME_BENCH_PER_SR
408 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
410 smx_process_t process;
415 unsigned long num_proc = xbt_dynar_length(processes);
417 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
420 time_thread_ssr[0] = 0;
421 xbt_dynar_foreach(processes, cursor, process) {
422 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
423 if(cursor >= t * data_size + data_size){
424 if(time_thread_ssr[t] > tmax)
425 tmax = time_thread_ssr[t];
427 time_thread_ssr[t] = 0;
431 ((smx_ctx_raw_t)process->context)->thread = t;
432 time_thread_sr[t] = 0;
435 xbt_os_cputimer_start(timer);
436 smx_ctx_raw_resume(process);
437 xbt_os_cputimer_stop(timer);
438 elapsed = xbt_os_timer_elapsed(timer);
439 time_thread_ssr[t] += elapsed;
440 time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
446 if(time_thread_ssr[t] > tmax)
447 tmax = time_thread_ssr[t];
449 for(cursor=0; cursor <= t; cursor++){
450 XBT_VERB("Time SSR thread %u = %f (max %f)", cursor, time_thread_ssr[cursor], tmax);
451 time_wasted_ssr += tmax - time_thread_ssr[cursor];
455 void smx_ctx_raw_new_sr(void);
456 void smx_ctx_raw_new_sr(void)
462 for(i=0; i < NUM_THREADS; i++){
463 if(time_thread_sr[i] > tmax)
464 tmax = time_thread_sr[i];
467 for(i=0; i < NUM_THREADS; i++){
468 XBT_VERB("Time SR thread %u = %f (max %f)", i, time_thread_sr[i], tmax);
469 time_wasted_sr += tmax - time_thread_sr[i];
472 XBT_VERB("New scheduling round");
477 * \brief Resumes sequentially all processes ready to run.
479 static void smx_ctx_raw_runall_serial(void)
481 smx_process_t first_process =
482 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
483 raw_process_index = 1;
485 /* execute the first process */
486 smx_ctx_raw_resume_serial(first_process);
491 * \brief Suspends a running context and resumes another one or returns to
492 * the main function of the current worker thread.
493 * \param context the context of the current worker thread
495 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
497 #ifdef CONTEXT_THREADS
498 /* determine the next context */
499 smx_process_t next_work = xbt_parmap_next(raw_parmap);
500 smx_context_t next_context;
501 raw_stack_t next_stack;
503 if (next_work != NULL) {
504 /* there is a next process to resume */
505 XBT_DEBUG("Run next process");
506 next_context = next_work->context;
507 next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
510 /* all processes were run, go to the barrier */
511 XBT_DEBUG("No more processes to run");
513 unsigned long worker_id =
514 (unsigned long) xbt_os_thread_get_specific(raw_worker_id_key);
516 next_context = (smx_context_t)raw_workers_context[worker_id];
517 XBT_DEBUG("Restoring worker stack %lu (working threads = %lu)",
518 worker_id, raw_threads_working);
519 next_stack = ((smx_ctx_raw_t)next_context)->stack_top;
522 SIMIX_context_set_current(next_context);
523 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
528 * \brief Resumes sequentially in the current worker thread the processes ready
530 * \param first_process the first process to resume
532 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
534 #ifdef CONTEXT_THREADS
535 unsigned long worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
536 xbt_os_thread_set_specific(raw_worker_id_key, (void*) worker_id);
537 smx_ctx_raw_t worker_context = (smx_ctx_raw_t)SIMIX_context_self();
538 raw_workers_context[worker_id] = worker_context;
539 XBT_DEBUG("Saving worker stack %lu", worker_id);
540 raw_stack_t* worker_stack = &(worker_context)->stack_top;
543 smx_context_t context = first_process->context;
544 SIMIX_context_set_current(context);
545 raw_swapcontext(worker_stack, ((smx_ctx_raw_t) context)->stack_top);
550 * \brief Resumes in parallel all processes ready to run.
552 static void smx_ctx_raw_runall_parallel(void)
554 #ifdef CONTEXT_THREADS
555 raw_threads_working = 0;
556 xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
557 simix_global->process_to_run);
559 xbt_die("You asked for a parallel execution, but you don't have any threads.");
564 * \brief Resumes all processes ready to run.
566 static void smx_ctx_raw_runall(void)
568 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
569 if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
570 XBT_DEBUG("Runall // %lu", nb_processes);
571 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
572 smx_ctx_raw_runall_parallel();
574 XBT_DEBUG("Runall serial %lu", nb_processes);
575 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
576 smx_ctx_raw_runall_serial();