1 /* Copyright (c) 2009-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "src/internal_config.h"
15 #include "xbt/parmap.h"
16 #include "xbt/dynar.h"
18 #include "src/simix/smx_private.h"
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
23 // ***** Class definitions
29 class RawContextFactory;
31 /** @brief Fast context switching inspired from SystemV ucontexts.
33 * The main difference to the System V context is that Raw Contexts are much faster because they don't
34 * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
36 class RawContext : public Context {
38 void* stack_ = nullptr;
39 /** pointer to top the stack stack */
40 void* stack_top_ = nullptr;
42 friend class RawContextFactory;
43 RawContext(std::function<void()> code,
44 void_pfn_smxprocess_t cleanup_func,
45 smx_process_t process);
46 ~RawContext() override;
48 static void wrapper(void* arg);
50 void suspend() override;
53 void suspend_serial();
54 void suspend_parallel();
56 void resume_parallel();
59 class RawContextFactory : public ContextFactory {
62 ~RawContextFactory() override;
63 RawContext* create_context(std::function<void()> code,
64 void_pfn_smxprocess_t cleanup, smx_process_t process) override;
65 void run_all() override;
67 void run_all_adaptative();
68 void run_all_serial();
69 void run_all_parallel();
72 ContextFactory* raw_factory()
74 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
75 return new RawContextFactory();
81 // ***** Loads of static stuff
83 #if HAVE_THREAD_CONTEXTS
84 static xbt_parmap_t raw_parmap;
85 static simgrid::simix::RawContext** raw_workers_context; /* space to save the worker context in each thread */
86 static uintptr_t raw_threads_working; /* number of threads that have started their work */
87 static xbt_os_thread_key_t raw_worker_id_key; /* thread-specific storage for the thread id */
89 static unsigned long raw_process_index = 0; /* index of the next process to run in the
90 * list of runnable processes */
91 static simgrid::simix::RawContext* raw_maestro_context;
93 static bool raw_context_parallel = false;
95 // ***** Raw context routines
97 typedef void (*rawctx_entry_point_t)(void *);
99 typedef void* raw_stack_t;
100 extern "C" raw_stack_t raw_makecontext(void* malloced_stack, int stack_size,
101 rawctx_entry_point_t entry_point, void* arg);
102 extern "C" void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context);
104 // TODO, we should handle FP, MMX and the x87 control-word (for x86 and x86_64)
106 #if SIMGRID_PROCESSOR_x86_64
108 #if defined(__APPLE__)
110 ".globl _raw_makecontext\n"
111 "_raw_makecontext:\n"
112 #elif defined(_WIN32)
114 ".globl raw_makecontext\n"
118 ".globl raw_makecontext\n"
119 ".type raw_makecontext,@function\n"
120 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
122 " mov %rdi,%rax\n" /* stack */
123 " add %rsi,%rax\n" /* size */
124 " andq $-16, %rax\n" /* align stack */
125 " movq $0, -8(%rax)\n" /* @return for func */
126 " mov %rdx,-16(%rax)\n" /* func */
127 " mov %rcx,-24(%rax)\n" /* arg/rdi */
128 " movq $0, -32(%rax)\n" /* rsi */
129 " movq $0, -40(%rax)\n" /* rdx */
130 " movq $0, -48(%rax)\n" /* rcx */
131 " movq $0, -56(%rax)\n" /* r8 */
132 " movq $0, -64(%rax)\n" /* r9 */
133 " movq $0, -72(%rax)\n" /* rbp */
134 " movq $0, -80(%rax)\n" /* rbx */
135 " movq $0, -88(%rax)\n" /* r12 */
136 " movq $0, -96(%rax)\n" /* r13 */
137 " movq $0, -104(%rax)\n" /* r14 */
138 " movq $0, -112(%rax)\n" /* r15 */
144 #if defined(__APPLE__)
146 ".globl _raw_swapcontext\n"
147 "_raw_swapcontext:\n"
148 #elif defined(_WIN32)
150 ".globl raw_swapcontext\n"
154 ".globl raw_swapcontext\n"
155 ".type raw_swapcontext,@function\n"
156 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
170 " mov %rsp,(%rdi)\n" /* old */
171 " mov %rsi,%rsp\n" /* new */
186 #elif SIMGRID_PROCESSOR_i686
188 #if defined(__APPLE__) || defined(_WIN32)
190 ".globl _raw_makecontext\n"
191 "_raw_makecontext:\n"
194 ".globl raw_makecontext\n"
195 ".type raw_makecontext,@function\n"
198 " movl 4(%esp),%eax\n" /* stack */
199 " addl 8(%esp),%eax\n" /* size */
200 " andl $-16, %eax\n" /* align stack */
201 " movl 12(%esp),%ecx\n" /* func */
202 " movl 16(%esp),%edx\n" /* arg */
203 " movl %edx, -4(%eax)\n"
204 " movl $0, -8(%eax)\n" /* @return for func */
205 " movl %ecx,-12(%eax)\n"
206 " movl $0, -16(%eax)\n" /* ebp */
207 " movl $0, -20(%eax)\n" /* ebx */
208 " movl $0, -24(%eax)\n" /* esi */
209 " movl $0, -28(%eax)\n" /* edi */
215 #if defined(__APPLE__) || defined(_WIN32)
217 ".globl _raw_swapcontext\n"
218 "_raw_swapcontext:\n"
221 ".globl raw_swapcontext\n"
222 ".type raw_swapcontext,@function\n"
225 // Fetch the parameters:
226 " movl 4(%esp),%eax\n" /* old (raw_stack_t*) */
227 " movl 8(%esp),%edx\n" /* new (raw_stack_t) */
228 // Save registers of the current context on the stack:
233 // Save the current context (stack pointer) in *old:
234 " movl %esp,(%eax)\n"
235 // Switch to the stack of the new context:
237 // Pop the values of the new context:
242 // Return using the return address of the new context:
248 /* If you implement raw contexts for other processors, don't forget to
249 update the definition of HAVE_RAW_CONTEXTS in tools/cmake/CompleteInFiles.cmake */
251 raw_stack_t raw_makecontext(void* malloced_stack, int stack_size,
252 rawctx_entry_point_t entry_point, void* arg) {
256 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context) {
262 // ***** Method definitions
267 RawContextFactory::RawContextFactory()
268 : ContextFactory("RawContextFactory")
270 raw_context_parallel = SIMIX_context_is_parallel();
271 if (raw_context_parallel) {
272 #if HAVE_THREAD_CONTEXTS
273 int nthreads = SIMIX_context_get_nthreads();
274 xbt_os_thread_key_create(&raw_worker_id_key);
276 raw_parmap = nullptr;
277 raw_workers_context = xbt_new(RawContext*, nthreads);
278 raw_maestro_context = nullptr;
280 // TODO, if(SIMIX_context_get_parallel_threshold() > 1) => choose dynamically
284 RawContextFactory::~RawContextFactory()
286 #if HAVE_THREAD_CONTEXTS
288 xbt_parmap_destroy(raw_parmap);
289 xbt_free(raw_workers_context);
293 RawContext* RawContextFactory::create_context(std::function<void()> code,
294 void_pfn_smxprocess_t cleanup, smx_process_t process)
296 return this->new_context<RawContext>(std::move(code),
300 void RawContext::wrapper(void* arg)
302 RawContext* context = (RawContext*) arg;
307 RawContext::RawContext(std::function<void()> code,
308 void_pfn_smxprocess_t cleanup, smx_process_t process)
309 : Context(std::move(code), cleanup, process)
312 this->stack_ = SIMIX_context_stack_new();
313 this->stack_top_ = raw_makecontext(this->stack_,
314 smx_context_usable_stack_size,
318 if(process != nullptr && raw_maestro_context == nullptr)
319 raw_maestro_context = this;
322 &raw_maestro_context->stack_top_,
323 sizeof(raw_maestro_context->stack_top_));
327 RawContext::~RawContext()
329 SIMIX_context_stack_delete(this->stack_);
332 void RawContext::stop()
338 void RawContextFactory::run_all()
340 if (raw_context_parallel)
346 void RawContextFactory::run_all_serial()
348 smx_process_t first_process =
349 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
350 raw_process_index = 1;
351 static_cast<RawContext*>(first_process->context)->resume_serial();
354 void RawContextFactory::run_all_parallel()
356 #if HAVE_THREAD_CONTEXTS
357 raw_threads_working = 0;
358 if (raw_parmap == nullptr)
359 raw_parmap = xbt_parmap_new(
360 SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
361 xbt_parmap_apply(raw_parmap,
363 smx_process_t process = static_cast<smx_process_t>(arg);
364 RawContext* context = static_cast<RawContext*>(process->context);
365 context->resume_parallel();
367 simix_global->process_to_run);
369 xbt_die("You asked for a parallel execution, but you don't have any threads.");
373 void RawContext::suspend()
375 if (raw_context_parallel)
376 RawContext::suspend_parallel();
378 RawContext::suspend_serial();
381 void RawContext::suspend_serial()
383 /* determine the next context */
384 RawContext* next_context = nullptr;
386 i = raw_process_index++;
387 if (i < xbt_dynar_length(simix_global->process_to_run)) {
388 /* execute the next process */
389 XBT_DEBUG("Run next process");
390 next_context = (RawContext*) xbt_dynar_get_as(
391 simix_global->process_to_run, i, smx_process_t)->context;
394 /* all processes were run, return to maestro */
395 XBT_DEBUG("No more process to run");
396 next_context = (RawContext*) raw_maestro_context;
398 SIMIX_context_set_current(next_context);
399 raw_swapcontext(&this->stack_top_, next_context->stack_top_);
402 void RawContext::suspend_parallel()
404 #if HAVE_THREAD_CONTEXTS
405 /* determine the next context */
406 smx_process_t next_work = (smx_process_t) xbt_parmap_next(raw_parmap);
407 RawContext* next_context = nullptr;
409 if (next_work != nullptr) {
410 /* there is a next process to resume */
411 XBT_DEBUG("Run next process");
412 next_context = (RawContext*) next_work->context;
415 /* all processes were run, go to the barrier */
416 XBT_DEBUG("No more processes to run");
417 uintptr_t worker_id = (uintptr_t)
418 xbt_os_thread_get_specific(raw_worker_id_key);
419 next_context = (RawContext*) raw_workers_context[worker_id];
420 XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)",
421 worker_id, raw_threads_working);
424 SIMIX_context_set_current(next_context);
425 raw_swapcontext(&this->stack_top_, next_context->stack_top_);
429 void RawContext::resume()
431 if (raw_context_parallel)
437 void RawContext::resume_serial()
439 SIMIX_context_set_current(this);
440 raw_swapcontext(&raw_maestro_context->stack_top_, this->stack_top_);
443 void RawContext::resume_parallel()
445 #if HAVE_THREAD_CONTEXTS
446 uintptr_t worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
447 xbt_os_thread_set_specific(raw_worker_id_key, (void*) worker_id);
448 RawContext* worker_context = (RawContext*) SIMIX_context_self();
449 raw_workers_context[worker_id] = worker_context;
450 XBT_DEBUG("Saving worker stack %zu", worker_id);
451 SIMIX_context_set_current(this);
452 raw_swapcontext(&worker_context->stack_top_, this->stack_top_);
454 xbt_die("Parallel execution disabled");
458 /** @brief Resumes all processes ready to run. */
459 void RawContextFactory::run_all_adaptative()
461 unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
462 if (SIMIX_context_is_parallel()
463 && (unsigned long) SIMIX_context_get_parallel_threshold() < nb_processes) {
464 raw_context_parallel = true;
465 XBT_DEBUG("Runall // %lu", nb_processes);
466 this->run_all_parallel();
468 XBT_DEBUG("Runall serial %lu", nb_processes);
469 raw_context_parallel = false;
470 this->run_all_serial();