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 "simix/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_local_maestro_stacks; /* space to save maestro's stack in each thread */
36 static unsigned long raw_process_index = 0; /* index of the next process to run in the
37 * list of runnable processes */
38 smx_ctx_raw_t raw_maestro_context;
40 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
41 rawctx_entry_point_t entry_point, void* arg);
42 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
46 #if defined(APPLE) || defined(_WIN32)
48 ".globl _raw_makecontext\n"
52 ".globl raw_makecontext\n"
53 ".type raw_makecontext,@function\n"
56 " movl 4(%esp),%eax\n" /* stack */
57 " addl 8(%esp),%eax\n" /* size */
58 " movl 12(%esp),%ecx\n" /* func */
59 " movl 16(%esp),%edx\n" /* arg */
60 " movl %edx, -4(%eax)\n"
61 " movl $0, -8(%eax)\n" /* @return for func */
62 " movl %ecx,-12(%eax)\n"
63 " movl $0, -16(%eax)\n" /* ebp */
64 " movl $0, -20(%eax)\n" /* ebx */
65 " movl $0, -24(%eax)\n" /* esi */
66 " movl $0, -28(%eax)\n" /* edi */
72 #if defined(APPLE) || defined(_WIN32)
74 ".globl _raw_swapcontext\n"
78 ".globl raw_swapcontext\n"
79 ".type raw_swapcontext,@function\n"
82 " movl 4(%esp),%eax\n" /* old */
83 " movl 8(%esp),%edx\n" /* new */
96 #elif PROCESSOR_x86_64
98 #if defined(APPLE) || defined(_WIN32)
100 ".globl _raw_makecontext\n"
101 "_raw_makecontext:\n"
104 ".globl raw_makecontext\n"
105 ".type raw_makecontext,@function\n"
106 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
108 " movq %rdi,%rax\n" /* stack */
109 " addq %rsi,%rax\n" /* size */
110 " movq $0, -8(%rax)\n" /* @return for func */
111 " movq %rdx,-16(%rax)\n" /* func */
112 " movq %rcx,-24(%rax)\n" /* arg/rdi */
113 " movq $0, -32(%rax)\n" /* rsi */
114 " movq $0, -40(%rax)\n" /* rdx */
115 " movq $0, -48(%rax)\n" /* rcx */
116 " movq $0, -56(%rax)\n" /* r8 */
117 " movq $0, -64(%rax)\n" /* r9 */
118 " movq $0, -72(%rax)\n" /* rbp */
119 " movq $0, -80(%rax)\n" /* rbx */
120 " movq $0, -88(%rax)\n" /* r12 */
121 " movq $0, -96(%rax)\n" /* r13 */
122 " movq $0, -104(%rax)\n" /* r14 */
123 " movq $0, -112(%rax)\n" /* r15 */
129 #if defined(APPLE) || defined(_WIN32)
131 ".globl _raw_swapcontext\n"
132 "_raw_swapcontext:\n"
135 ".globl raw_swapcontext\n"
136 ".type raw_swapcontext,@function\n"
137 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
151 " movq %rsp,(%rdi)\n" /* old */
152 " movq %rsi,%rsp\n" /* new */
169 /* If you implement raw contextes for other processors, don't forget to
170 update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */
172 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
173 rawctx_entry_point_t entry_point, void* arg) {
177 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
183 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
186 #include "xbt/xbt_os_time.h"
187 #define NUM_THREADS 4
188 static xbt_os_timer_t timer;
189 static double time_thread_sr[NUM_THREADS];
190 static double time_thread_ssr[NUM_THREADS];
191 static double time_wasted_sr = 0;
192 static double time_wasted_ssr = 0;
193 static unsigned int sr_count = 0;
194 static unsigned int ssr_count = 0;
195 static char new_sr = 0;
198 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
199 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
200 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
201 char **argv, void_pfn_smxprocess_t cleanup_func, void *data);
202 static void smx_ctx_raw_free(smx_context_t context);
203 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
204 static void smx_ctx_raw_stop(smx_context_t context);
205 static void smx_ctx_raw_suspend_serial(smx_context_t context);
206 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
207 static void smx_ctx_raw_runall_serial(void);
208 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
209 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
210 static void smx_ctx_raw_runall_parallel(void);
211 static void smx_ctx_raw_runall(void);
213 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
215 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
216 smx_ctx_base_factory_init(factory);
218 (*factory)->finalize = smx_ctx_raw_factory_finalize;
219 (*factory)->create_context = smx_ctx_raw_create_context;
220 /* Do not overload that method (*factory)->finalize */
221 (*factory)->free = smx_ctx_raw_free;
222 (*factory)->stop = smx_ctx_raw_stop;
223 (*factory)->name = "smx_raw_context_factory";
225 if (SIMIX_context_is_parallel()) {
226 #ifdef CONTEXT_THREADS
227 int nthreads = SIMIX_context_get_nthreads();
228 raw_parmap = xbt_parmap_new(nthreads);
229 raw_local_maestro_stacks = xbt_new(raw_stack_t, nthreads);
231 if (SIMIX_context_get_parallel_threshold() > 1) {
232 /* choose dynamically */
233 (*factory)->runall = smx_ctx_raw_runall;
234 (*factory)->suspend = NULL;
237 /* always parallel */
238 (*factory)->runall = smx_ctx_raw_runall_parallel;
239 (*factory)->suspend = smx_ctx_raw_suspend_parallel;
244 (*factory)->runall = smx_ctx_raw_runall_serial;
245 (*factory)->suspend = smx_ctx_raw_suspend_serial;
248 timer = xbt_os_timer_new();
252 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
255 XBT_CRITICAL("Total wasted time in %u SR: %lf", sr_count, time_wasted_sr);
256 XBT_CRITICAL("Total wasted time in %u SSR: %lf", ssr_count, time_wasted_ssr);
259 #ifdef CONTEXT_THREADS
261 xbt_parmap_destroy(raw_parmap);
262 xbt_free(raw_local_maestro_stacks);
264 return smx_ctx_base_factory_finalize(factory);
268 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
269 void_pfn_smxprocess_t cleanup_func,
273 smx_ctx_raw_t context =
274 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
275 sizeof(s_smx_ctx_raw_t),
282 /* if the user provided a function for the process then use it,
283 otherwise it is the context for maestro */
285 context->malloced_stack = xbt_malloc0(smx_context_stack_size);
287 raw_makecontext(context->malloced_stack, smx_context_stack_size,
288 (void(*)(void*))smx_ctx_raw_wrapper,context);
290 #ifdef HAVE_VALGRIND_VALGRIND_H
291 context->valgrind_stack_id =
292 VALGRIND_STACK_REGISTER(context->malloced_stack,
293 context->malloced_stack + smx_context_stack_size);
294 #endif /* HAVE_VALGRIND_VALGRIND_H */
297 raw_maestro_context = context;
300 return (smx_context_t) context;
303 static void smx_ctx_raw_free(smx_context_t context)
308 #ifdef HAVE_VALGRIND_VALGRIND_H
309 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
310 context)->valgrind_stack_id);
311 #endif /* HAVE_VALGRIND_VALGRIND_H */
313 free(((smx_ctx_raw_t)context)->malloced_stack);
315 smx_ctx_base_free(context);
318 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
320 (context->super.code) (context->super.argc, context->super.argv);
322 smx_ctx_raw_stop((smx_context_t) context);
325 static void smx_ctx_raw_stop(smx_context_t context)
327 smx_ctx_base_stop(context);
328 simix_global->context_factory->suspend(context);
331 static void smx_ctx_raw_suspend_serial(smx_context_t context)
333 /* determine the next context */
334 smx_context_t next_context;
335 unsigned long int i = raw_process_index++;
337 if (i < xbt_dynar_length(simix_global->process_to_run)) {
338 /* execute the next process */
339 XBT_DEBUG("Run next process");
340 next_context = xbt_dynar_get_as(
341 simix_global->process_to_run,i, smx_process_t)->context;
344 /* all processes were run, return to maestro */
345 XBT_DEBUG("No more process to run");
346 next_context = (smx_context_t) raw_maestro_context;
348 SIMIX_context_set_current(next_context);
349 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
350 ((smx_ctx_raw_t) next_context)->stack_top);
353 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
355 smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
356 SIMIX_context_set_current((smx_context_t) context);
357 raw_swapcontext(&raw_maestro_context->stack_top,
358 ((smx_ctx_raw_t) context)->stack_top);
362 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
364 smx_process_t process;
369 unsigned long num_proc = xbt_dynar_length(processes);
371 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
374 time_thread_ssr[0] = 0;
375 xbt_dynar_foreach(processes, cursor, process) {
376 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
377 if(cursor >= t * data_size + data_size){
378 if(time_thread_ssr[t] > tmax)
379 tmax = time_thread_ssr[t];
381 time_thread_ssr[t] = 0;
385 ((smx_ctx_raw_t)process->context)->thread = t;
386 time_thread_sr[t] = 0;
389 xbt_os_timer_start(timer);
390 smx_ctx_raw_resume(process);
391 xbt_os_timer_stop(timer);
392 elapsed = xbt_os_timer_elapsed(timer);
393 time_thread_ssr[t] += elapsed;
394 time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
400 if(time_thread_ssr[t] > tmax)
401 tmax = time_thread_ssr[t];
403 for(cursor=0; cursor <= t; cursor++){
404 XBT_VERB("Time SSR thread %u = %lf (max %lf)", cursor, time_thread_ssr[cursor], tmax);
405 time_wasted_ssr += tmax - time_thread_ssr[cursor];
409 void smx_ctx_raw_new_sr(void);
410 void smx_ctx_raw_new_sr(void)
416 for(i=0; i < NUM_THREADS; i++){
417 if(time_thread_sr[i] > tmax)
418 tmax = time_thread_sr[i];
421 for(i=0; i < NUM_THREADS; i++){
422 XBT_VERB("Time SR thread %u = %lf (max %lf)", i, time_thread_sr[i], tmax);
423 time_wasted_sr += tmax - time_thread_sr[i];
426 XBT_VERB("New scheduling round");
429 static void smx_ctx_raw_runall_serial(void)
431 if (xbt_dynar_length(simix_global->process_to_run) > 0) {
432 smx_process_t first_process =
433 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
434 raw_process_index = 1;
436 /* execute the first process */
437 smx_ctx_raw_resume_serial(first_process);
442 static void smx_ctx_raw_stop_parallel(smx_context_t context)
444 smx_ctx_base_stop(context);
445 smx_ctx_raw_suspend_parallel(context);
448 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
450 #ifdef CONTEXT_THREADS
451 /* determine the next context */
452 smx_process_t next_work = xbt_parmap_next(raw_parmap);
453 smx_context_t next_context;
454 raw_stack_t next_stack;
456 if (next_work != NULL) {
457 /* there is a next process to resume */
458 XBT_DEBUG("Run next process");
459 next_context = next_work->context;
460 next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
463 /* all processes were run, go to the barrier */
464 XBT_DEBUG("No more processes to run");
465 next_context = (smx_context_t) raw_maestro_context;
466 unsigned long worker_id = xbt_parmap_get_worker_id(raw_parmap);
467 next_stack = raw_local_maestro_stacks[worker_id];
470 SIMIX_context_set_current(next_context);
471 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
475 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
477 #ifdef CONTEXT_THREADS
478 unsigned long worker_id = xbt_parmap_get_worker_id(raw_parmap);
479 raw_stack_t* local_maestro_stack = &raw_local_maestro_stacks[worker_id];
481 smx_context_t context = first_process->context;
482 SIMIX_context_set_current(context);
483 raw_swapcontext(local_maestro_stack, ((smx_ctx_raw_t) context)->stack_top);
487 static void smx_ctx_raw_runall_parallel(void)
489 #ifdef CONTEXT_THREADS
490 xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
491 simix_global->process_to_run);
495 static void smx_ctx_raw_runall(void)
497 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
498 if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
499 XBT_DEBUG("Runall // %lu", nb_processes);
500 simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
501 smx_ctx_raw_runall_parallel();
503 XBT_DEBUG("Runall serial %lu", nb_processes);
504 simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
505 smx_ctx_raw_runall_serial();