1 /* context_raw - context switching with ucontextes from System V */
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"
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 raw_stack_t old_stack_top; /* to whom I should return the control */
25 #ifdef HAVE_VALGRIND_VALGRIND_H
26 unsigned int valgrind_stack_id; /* the valgrind stack id */
29 unsigned int thread; /* Just for measuring purposes */
31 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
33 smx_ctx_raw_t maestro_raw_context;
35 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
36 rawctx_entry_point_t entry_point, void* arg);
37 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
42 ".globl raw_makecontext\n"
43 ".type raw_makecontext,@function\n"
45 " movl 4(%esp),%eax\n" /* stack */
46 " addl 8(%esp),%eax\n" /* size */
47 " movl 12(%esp),%ecx\n" /* func */
48 " movl 16(%esp),%edx\n" /* arg */
49 " movl %edx, -4(%eax)\n"
50 " movl $0, -8(%eax)\n" /* @return for func */
51 " movl %ecx,-12(%eax)\n"
52 " movl $0, -16(%eax)\n" /* ebp */
53 " movl $0, -20(%eax)\n" /* ebx */
54 " movl $0, -24(%eax)\n" /* esi */
55 " movl $0, -28(%eax)\n" /* edi */
62 ".globl raw_swapcontext\n"
63 ".type raw_swapcontext,@function\n"
65 " movl 4(%esp),%eax\n" /* old */
66 " movl 8(%esp),%edx\n" /* new */
79 #elif PROCESSOR_x86_64
82 ".globl raw_makecontext\n"
83 ".type raw_makecontext,@function\n"
84 "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
85 " movq %rdi,%rax\n" /* stack */
86 " addq %rsi,%rax\n" /* size */
87 " movq $0, -8(%rax)\n" /* @return for func */
88 " movq %rdx,-16(%rax)\n" /* func */
89 " movq %rcx,-24(%rax)\n" /* arg/rdi */
90 " movq $0, -32(%rax)\n" /* rsi */
91 " movq $0, -40(%rax)\n" /* rdx */
92 " movq $0, -48(%rax)\n" /* rcx */
93 " movq $0, -56(%rax)\n" /* r8 */
94 " movq $0, -64(%rax)\n" /* r9 */
95 " movq $0, -72(%rax)\n" /* rbp */
96 " movq $0, -80(%rax)\n" /* rbx */
97 " movq $0, -88(%rax)\n" /* r12 */
98 " movq $0, -96(%rax)\n" /* r13 */
99 " movq $0, -104(%rax)\n" /* r14 */
100 " movq $0, -112(%rax)\n" /* r15 */
107 ".globl raw_swapcontext\n"
108 ".type raw_swapcontext,@function\n"
109 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
122 " movq %rsp,(%rdi)\n" /* old */
123 " movq %rsi,%rsp\n" /* new */
140 /* If you implement raw contextes for other processors, don't forget to
141 update the definition of HAVE_RAWCTX in buildtools/Cmake/AddTests.cmake */
143 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
144 rawctx_entry_point_t entry_point, void* arg) {
148 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
154 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
156 #ifdef CONTEXT_THREADS
157 static xbt_parmap_t parmap;
161 #include "xbt/xbt_os_time.h"
162 #define NUM_THREADS 4
163 static xbt_os_timer_t timer;
164 static double time_thread_sr[NUM_THREADS];
165 static double time_thread_ssr[NUM_THREADS];
166 static double time_wasted_sr = 0;
167 static double time_wasted_ssr = 0;
168 static unsigned int sr_count = 0;
169 static unsigned int ssr_count = 0;
170 static char new_sr = 0;
173 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
175 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
178 XBT_CRITICAL("Total wasted time in %u SR: %lf", sr_count, time_wasted_sr);
179 XBT_CRITICAL("Total wasted time in %u SSR: %lf", ssr_count, time_wasted_ssr);
182 #ifdef CONTEXT_THREADS
184 xbt_parmap_destroy(parmap);
186 return smx_ctx_base_factory_finalize(factory);
191 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
192 void_pfn_smxprocess_t cleanup_func,
196 smx_ctx_raw_t context =
197 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
198 sizeof(s_smx_ctx_raw_t),
205 /* If the user provided a function for the process then use it
206 otherwise is the context for maestro */
208 context->malloced_stack = xbt_malloc0(smx_context_stack_size);
210 raw_makecontext(context->malloced_stack, smx_context_stack_size,
211 (void(*)(void*))smx_ctx_raw_wrapper,context);
213 #ifdef HAVE_VALGRIND_VALGRIND_H
214 context->valgrind_stack_id =
215 VALGRIND_STACK_REGISTER(context->malloced_stack,
216 context->malloced_stack + smx_context_stack_size);
217 #endif /* HAVE_VALGRIND_VALGRIND_H */
220 maestro_raw_context = context;
223 return (smx_context_t) context;
226 static void smx_ctx_raw_free(smx_context_t context)
231 #ifdef HAVE_VALGRIND_VALGRIND_H
232 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
233 context)->valgrind_stack_id);
234 #endif /* HAVE_VALGRIND_VALGRIND_H */
236 free(((smx_ctx_raw_t)context)->malloced_stack);
238 smx_ctx_base_free(context);
241 static void smx_ctx_raw_suspend(smx_context_t context)
243 SIMIX_context_set_current((smx_context_t) maestro_raw_context);
245 &((smx_ctx_raw_t) context)->stack_top,
246 ((smx_ctx_raw_t) context)->old_stack_top);
249 static void smx_ctx_raw_stop(smx_context_t context)
251 smx_ctx_base_stop(context);
252 smx_ctx_raw_suspend(context);
255 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
257 (context->super.code) (context->super.argc, context->super.argv);
259 smx_ctx_raw_stop((smx_context_t) context);
262 static void smx_ctx_raw_resume(smx_process_t process)
264 smx_ctx_raw_t context = (smx_ctx_raw_t)process->context;
265 SIMIX_context_set_current((smx_context_t) context);
267 &((smx_ctx_raw_t) context)->old_stack_top,
268 ((smx_ctx_raw_t) context)->stack_top);
272 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
274 smx_process_t process;
279 unsigned long num_proc = xbt_dynar_length(processes);
281 unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
284 time_thread_ssr[0] = 0;
285 xbt_dynar_foreach(processes, cursor, process) {
286 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
287 if(cursor >= t * data_size + data_size){
288 if(time_thread_ssr[t] > tmax)
289 tmax = time_thread_ssr[t];
291 time_thread_ssr[t] = 0;
295 ((smx_ctx_raw_t)process->context)->thread = t;
296 time_thread_sr[t] = 0;
299 xbt_os_timer_start(timer);
300 smx_ctx_raw_resume(process);
301 xbt_os_timer_stop(timer);
302 elapsed = xbt_os_timer_elapsed(timer);
303 time_thread_ssr[t] += elapsed;
304 time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
310 if(time_thread_ssr[t] > tmax)
311 tmax = time_thread_ssr[t];
313 for(cursor=0; cursor <= t; cursor++){
314 XBT_VERB("Time SSR thread %u = %lf (max %lf)", cursor, time_thread_ssr[cursor], tmax);
315 time_wasted_ssr += tmax - time_thread_ssr[cursor];
319 void smx_ctx_raw_new_sr(void);
320 void smx_ctx_raw_new_sr(void)
326 for(i=0; i < NUM_THREADS; i++){
327 if(time_thread_sr[i] > tmax)
328 tmax = time_thread_sr[i];
331 for(i=0; i < NUM_THREADS; i++){
332 XBT_VERB("Time SR thread %u = %lf (max %lf)", i, time_thread_sr[i], tmax);
333 time_wasted_sr += tmax - time_thread_sr[i];
336 XBT_VERB("New scheduling round");
339 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
341 smx_process_t process;
344 xbt_dynar_foreach(processes, cursor, process) {
345 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
346 smx_ctx_raw_resume(process);
351 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
353 #ifdef CONTEXT_THREADS
354 xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_raw_resume, processes);
358 static void smx_ctx_raw_runall(xbt_dynar_t processes)
360 if (xbt_dynar_length(processes) >= SIMIX_context_get_parallel_threshold()) {
361 XBT_DEBUG("Runall // %lu", xbt_dynar_length(processes));
362 smx_ctx_raw_runall_parallel(processes);
364 XBT_DEBUG("Runall serial %lu", xbt_dynar_length(processes));
365 smx_ctx_raw_runall_serial(processes);
369 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
371 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
372 smx_ctx_base_factory_init(factory);
374 (*factory)->finalize = smx_ctx_raw_factory_finalize;
375 (*factory)->create_context = smx_ctx_raw_create_context;
376 /* Do not overload that method (*factory)->finalize */
377 (*factory)->free = smx_ctx_raw_free;
378 (*factory)->stop = smx_ctx_raw_stop;
379 (*factory)->suspend = smx_ctx_raw_suspend;
380 (*factory)->name = "smx_raw_context_factory";
382 if (SIMIX_context_is_parallel()) {
383 #ifdef CONTEXT_THREADS
384 parmap = xbt_parmap_new(SIMIX_context_get_nthreads());
386 if (SIMIX_context_get_parallel_threshold() > 1) {
387 /* choose dynamically */
388 (*factory)->runall = smx_ctx_raw_runall;
391 /* always parallel */
392 (*factory)->runall = smx_ctx_raw_runall_parallel;
397 (*factory)->runall = smx_ctx_raw_runall_serial;
400 timer = xbt_os_timer_new();