/** @brief Executes all the processes to run (in parallel if possible). */
XBT_PRIVATE void SIMIX_context_runall();
-XBT_PRIVATE void *SIMIX_context_stack_new();
-XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
-
XBT_PUBLIC int SIMIX_process_get_maxpid();
XBT_PRIVATE void SIMIX_post_create_environment();
{
if (parallel_)
return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
- return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
+ return this->new_context<BoostContext>(std::move(code), cleanup_func, process);
}
void BoostContextFactory::run_all()
if (parallel_)
ParallelBoostContext::run_all();
else
- SerialBoostContext::run_all();
+ SwappedContext::run_all();
}
// BoostContext
-BoostContext* BoostContext::maestro_context_ = nullptr;
-
BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : Context(std::move(code), cleanup_func, process)
+ : SwappedContext(std::move(code), cleanup_func, process)
{
/* if the user provided a function for the process then use it, otherwise it is the context for maestro */
if (has_code()) {
- this->stack_ = SIMIX_context_stack_new();
/* We need to pass the bottom of the stack to make_fcontext,
depending on the stack direction it may be the lower or higher address: */
#if PTH_STACKGROWTH == -1
#if BOOST_VERSION < 105600
this->fc_ = new boost::context::fcontext_t();
#endif
- if (BoostContext::maestro_context_ == nullptr)
- BoostContext::maestro_context_ = this;
+ if (get_maestro() == nullptr)
+ set_maestro(this);
}
}
if (not this->stack_)
delete this->fc_;
#endif
- if (this == maestro_context_)
- maestro_context_ = nullptr;
- SIMIX_context_stack_delete(this->stack_);
+ if (this == get_maestro())
+ set_maestro(nullptr);
}
void BoostContext::wrapper(BoostContext::arg_type arg)
context->suspend();
}
-inline void BoostContext::swap(BoostContext* from, BoostContext* to)
+void BoostContext::swap_into(SwappedContext* to_)
{
+ BoostContext* to = static_cast<BoostContext*>(to_);
#if BOOST_VERSION < 105600
- boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
+ boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
#elif BOOST_VERSION < 106100
- boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
+ boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
#else
- BoostContext* ctx[2] = {from, to};
+ BoostContext* ctx[2] = {this, to};
ASAN_ONLY(void* fake_stack = nullptr);
ASAN_ONLY(to->asan_ctx_ = from);
ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
throw StopRequest();
}
-// SerialBoostContext
-
-unsigned long SerialBoostContext::process_index_;
-
-void SerialBoostContext::suspend()
-{
- /* determine the next context */
- SerialBoostContext* next_context;
- unsigned long int i = process_index_;
- process_index_++;
-
- if (i < simix_global->process_to_run.size()) {
- /* execute the next process */
- XBT_DEBUG("Run next process");
- next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context_);
- } else {
- /* all processes were run, return to maestro */
- XBT_DEBUG("No more process to run");
- next_context = static_cast<SerialBoostContext*>(BoostContext::get_maestro());
- }
- Context::set_current(next_context);
- BoostContext::swap(this, next_context);
-}
-
-void SerialBoostContext::resume()
-{
- BoostContext* old = static_cast<BoostContext*>(self());
- Context::set_current(this);
- BoostContext::swap(old, this);
-}
-
-void SerialBoostContext::run_all()
-{
- if (simix_global->process_to_run.empty())
- return;
- smx_actor_t first_process = simix_global->process_to_run.front();
- process_index_ = 1;
- /* execute the first process */
- static_cast<SerialBoostContext*>(first_process->context_)->resume();
-}
-
// ParallelBoostContext
simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
}
Context::set_current(next_context);
- BoostContext::swap(this, next_context);
+ this->swap_into(next_context);
}
void ParallelBoostContext::resume()
workers_context_[worker_id_] = worker_context;
Context::set_current(this);
- BoostContext::swap(worker_context, this);
+ worker_context->swap_into(this);
}
#include <xbt/parmap.hpp>
#include <xbt/xbt_os_thread.h>
-#include "Context.hpp"
#include "src/internal_config.h"
+#include "src/kernel/context/Context.hpp"
+#include "src/kernel/context/ContextSwapped.hpp"
namespace simgrid {
namespace kernel {
namespace context {
/** @brief Userspace context switching implementation based on Boost.Context */
-class BoostContext : public Context {
+class BoostContext : public SwappedContext {
public:
BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
~BoostContext() override;
void stop() override;
- virtual void resume() = 0;
- static void swap(BoostContext* from, BoostContext* to);
- static BoostContext* get_maestro() { return maestro_context_; }
- static void set_maestro(BoostContext* maestro) { maestro_context_ = maestro; }
+ void swap_into(SwappedContext* to) override;
private:
- static BoostContext* maestro_context_;
- void* stack_ = nullptr;
-
#if BOOST_VERSION < 105600
boost::context::fcontext_t* fc_ = nullptr;
typedef intptr_t arg_type;
static void wrapper(arg_type arg);
};
-class SerialBoostContext : public BoostContext {
-public:
- SerialBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : BoostContext(std::move(code), cleanup_func, process)
- {
- }
- void suspend() override;
- void resume() override;
-
- static void run_all();
-
-private:
- static unsigned long process_index_;
-};
-
class ParallelBoostContext : public BoostContext {
public:
ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
{
if (parallel_)
return this->new_context<ParallelRawContext>(std::move(code), cleanup_func, process);
- return this->new_context<SerialRawContext>(std::move(code), cleanup_func, process);
+ return this->new_context<RawContext>(std::move(code), cleanup_func, process);
}
void RawContextFactory::run_all()
if (parallel_)
ParallelRawContext::run_all();
else
- SerialRawContext::run_all();
+ SwappedContext::run_all();
}
// RawContext
-RawContext* RawContext::maestro_context_ = nullptr;
-
RawContext::RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process)
- : Context(std::move(code), cleanup, process)
+ : SwappedContext(std::move(code), cleanup, process)
{
if (has_code()) {
- this->stack_ = SIMIX_context_stack_new();
#if PTH_STACKGROWTH == -1
ASAN_ONLY(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
#else
#endif
this->stack_top_ = raw_makecontext(this->stack_, smx_context_usable_stack_size, RawContext::wrapper, this);
} else {
- if (process != nullptr && maestro_context_ == nullptr)
- maestro_context_ = this;
- if (MC_is_active())
- MC_ignore_heap(&maestro_context_->stack_top_, sizeof(maestro_context_->stack_top_));
+ if (process != nullptr && get_maestro() == nullptr)
+ set_maestro(this);
+ if (MC_is_active()) {
+ XBT_ATTRIB_UNUSED RawContext* maestro = static_cast<RawContext*>(get_maestro());
+ MC_ignore_heap(&maestro->stack_top_, sizeof(maestro->stack_top_));
+ }
}
}
-RawContext::~RawContext()
-{
- SIMIX_context_stack_delete(this->stack_);
-}
+RawContext::~RawContext() = default;
void RawContext::wrapper(void* arg)
{
context->suspend();
}
-inline void RawContext::swap(RawContext* from, RawContext* to)
+void RawContext::swap_into(SwappedContext* to_)
{
+ RawContext* to = static_cast<RawContext*>(to_);
ASAN_ONLY(void* fake_stack = nullptr);
ASAN_ONLY(to->asan_ctx_ = from);
ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
- raw_swapcontext(&from->stack_top_, to->stack_top_);
+ raw_swapcontext(&this->stack_top_, to->stack_top_);
ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
}
throw StopRequest();
}
-// SerialRawContext
-
-unsigned long SerialRawContext::process_index_; /* index of the next process to run in the list of runnable processes */
-
-void SerialRawContext::suspend()
-{
- /* determine the next context */
- SerialRawContext* next_context;
- unsigned long int i = process_index_;
- process_index_++;
- if (i < simix_global->process_to_run.size()) {
- /* execute the next process */
- XBT_DEBUG("Run next process");
- next_context = static_cast<SerialRawContext*>(simix_global->process_to_run[i]->context_);
- } else {
- /* all processes were run, return to maestro */
- XBT_DEBUG("No more process to run");
- next_context = static_cast<SerialRawContext*>(RawContext::get_maestro());
- }
- Context::set_current(next_context);
- RawContext::swap(this, next_context);
-}
-
-void SerialRawContext::resume()
-{
- RawContext* old = static_cast<RawContext*>(self());
- Context::set_current(this);
- RawContext::swap(old, this);
-}
-
-void SerialRawContext::run_all()
-{
- if (simix_global->process_to_run.empty())
- return;
- smx_actor_t first_process = simix_global->process_to_run.front();
- process_index_ = 1;
- static_cast<SerialRawContext*>(first_process->context_)->resume();
-}
-
// ParallelRawContext
simgrid::xbt::Parmap<smx_actor_t>* ParallelRawContext::parmap_;
}
Context::set_current(next_context);
- RawContext::swap(this, next_context);
+ this->swap_into(next_context);
}
void ParallelRawContext::resume()
workers_context_[worker_id_] = worker_context;
XBT_DEBUG("Saving worker stack %zu", worker_id_);
Context::set_current(this);
- RawContext::swap(worker_context, this);
+ worker_context->swap_into(this);
}
ContextFactory* raw_factory()
#include <xbt/parmap.hpp>
#include <xbt/xbt_os_thread.h>
+#include "src/kernel/context/ContextSwapped.hpp"
+
namespace simgrid {
namespace kernel {
namespace context {
* The main difference to the System V context is that Raw Contexts are much faster because they don't
* preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
*/
-class RawContext : public Context {
+class RawContext : public SwappedContext {
public:
RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
~RawContext() override;
void stop() override;
- virtual void resume() = 0;
- static void swap(RawContext* from, RawContext* to);
- static RawContext* get_maestro() { return maestro_context_; }
- static void set_maestro(RawContext* maestro) { maestro_context_ = maestro; }
+ void swap_into(SwappedContext* to) override;
private:
- static RawContext* maestro_context_;
- void* stack_ = nullptr;
/** pointer to top the stack stack */
void* stack_top_ = nullptr;
static void wrapper(void* arg);
};
-class SerialRawContext : public RawContext {
-public:
- SerialRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : RawContext(std::move(code), cleanup_func, process)
- {
- }
- void suspend() override;
- void resume() override;
-
- static void run_all();
-
-private:
- static unsigned long process_index_;
-};
-
class ParallelRawContext : public RawContext {
public:
ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include "simgrid/modelchecker.h"
+#include "src/internal_config.h"
#include "src/kernel/context/context_private.hpp"
#include "src/simix/ActorImpl.hpp"
#include "src/simix/smx_private.hpp"
-#include "ContextSwapped.hpp"
+#include "src/kernel/context/ContextSwapped.hpp"
+
+#ifdef _WIN32
+#include <malloc.h>
+#include <windows.h>
+#else
+#include <sys/mman.h>
+#endif
+
+#ifdef __MINGW32__
+#define _aligned_malloc __mingw_aligned_malloc
+#define _aligned_free __mingw_aligned_free
+#endif /*MINGW*/
+
+#if HAVE_VALGRIND_H
+#include <valgrind/valgrind.h>
+#endif
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
SwappedContext* SwappedContext::maestro_context_ = nullptr;
+SwappedContext::SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
+ : Context(std::move(code), cleanup_func, process)
+{
+ if (has_code()) {
+ if (smx_context_guard_size > 0 && not MC_is_active()) {
+
+#if !defined(PTH_STACKGROWTH) || (PTH_STACKGROWTH != -1)
+ xbt_die(
+ "Stack overflow protection is known to be broken on your system: you stacks grow upwards (or detection is "
+ "broken). "
+ "Please disable stack guards with --cfg=contexts:guard-size:0");
+ /* Current code for stack overflow protection assumes that stacks are growing downward (PTH_STACKGROWTH == -1).
+ * Protected pages need to be put after the stack when PTH_STACKGROWTH == 1. */
+#endif
+
+ size_t size = smx_context_stack_size + smx_context_guard_size;
+#if SIMGRID_HAVE_MC
+ /* Cannot use posix_memalign when SIMGRID_HAVE_MC. Align stack by hand, and save the
+ * pointer returned by xbt_malloc0. */
+ char* alloc = (char*)xbt_malloc0(size + xbt_pagesize);
+ stack_ = alloc - ((uintptr_t)alloc & (xbt_pagesize - 1)) + xbt_pagesize;
+ *((void**)stack_ - 1) = alloc;
+#elif !defined(_WIN32)
+ if (posix_memalign(&this->stack_, xbt_pagesize, size) != 0)
+ xbt_die("Failed to allocate stack.");
+#else
+ this->stack_ = _aligned_malloc(size, xbt_pagesize);
+#endif
+
+#ifndef _WIN32
+ if (mprotect(this->stack_, smx_context_guard_size, PROT_NONE) == -1) {
+ xbt_die(
+ "Failed to protect stack: %s.\n"
+ "If you are running a lot of actors, you may be exceeding the amount of mappings allowed per process.\n"
+ "On Linux systems, change this value with sudo sysctl -w vm.max_map_count=newvalue (default value: 65536)\n"
+ "Please see http://simgrid.gforge.inria.fr/simgrid/latest/doc/html/options.html#options_virt for more "
+ "info.",
+ strerror(errno));
+ /* This is fatal. We are going to fail at some point when we try reusing this. */
+ }
+#endif
+ this->stack_ = (char*)this->stack_ + smx_context_guard_size;
+ } else {
+ this->stack_ = xbt_malloc0(smx_context_stack_size);
+ }
+
+#if HAVE_VALGRIND_H
+ unsigned int valgrind_stack_id =
+ VALGRIND_STACK_REGISTER(this->stack_, (char*)this->stack_ + smx_context_stack_size);
+ memcpy((char*)this->stack_ + smx_context_usable_stack_size, &valgrind_stack_id, sizeof valgrind_stack_id);
+#endif
+ }
+}
+
+SwappedContext::~SwappedContext()
+{
+ if (stack_ == nullptr)
+ return;
+
+#if HAVE_VALGRIND_H
+ unsigned int valgrind_stack_id;
+ memcpy(&valgrind_stack_id, (char*)stack_ + smx_context_usable_stack_size, sizeof valgrind_stack_id);
+ VALGRIND_STACK_DEREGISTER(valgrind_stack_id);
+#endif
+
+#ifndef _WIN32
+ if (smx_context_guard_size > 0 && not MC_is_active()) {
+ stack_ = (char*)stack_ - smx_context_guard_size;
+ if (mprotect(stack_, smx_context_guard_size, PROT_READ | PROT_WRITE) == -1) {
+ XBT_WARN("Failed to remove page protection: %s", strerror(errno));
+ /* try to pursue anyway */
+ }
+#if SIMGRID_HAVE_MC
+ /* Retrieve the saved pointer. See SIMIX_context_stack_new above. */
+ stack_ = *((void**)stack_ - 1);
+#endif
+ }
+#endif /* not windows */
+
+ xbt_free(stack_);
+}
+
void SwappedContext::suspend()
{
/* determine the next context */
#ifndef SIMGRID_SIMIX_SWAPPED_CONTEXT_HPP
#define SIMGRID_SIMIX_SWAPPED_CONTEXT_HPP
-#include "Context.hpp"
+#include "src/kernel/context/Context.hpp"
namespace simgrid {
namespace kernel {
class SwappedContext : public Context {
public:
- SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : Context(std::move(code), cleanup_func, process)
- {
- }
+ SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
+ virtual ~SwappedContext();
+
virtual void suspend();
virtual void resume();
static SwappedContext* get_maestro() { return maestro_context_; }
static void set_maestro(SwappedContext* maestro) { maestro_context_ = maestro; }
+protected:
+ void* stack_ = nullptr; /* the thread stack */
+
private:
static unsigned long process_index_;
static SwappedContext* maestro_context_;
{
/* if the user provided a function for the process then use it, otherwise it is the context for maestro */
if (has_code()) {
- this->stack_ = SIMIX_context_stack_new();
getcontext(&this->uc_);
this->uc_.uc_link = nullptr;
this->uc_.uc_stack.ss_sp = sg_makecontext_stack_addr(this->stack_);
#endif
}
-UContext::~UContext()
-{
- SIMIX_context_stack_delete(this->stack_);
-}
+UContext::~UContext() = default;
// The name of this function is currently hardcoded in the code (as string).
// Do not change it without fixing those references as well.
void swap_into(SwappedContext* to) override;
private:
- void* stack_ = nullptr; /* the thread stack */
ucontext_t uc_; /* the ucontext that executes the code */
#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
-#include "simgrid/modelchecker.h"
-#include "src/internal_config.h"
#include "src/simix/smx_private.hpp"
#include "xbt/config.hpp"
#include <thread>
-#ifdef _WIN32
-#include <windows.h>
-#include <malloc.h>
-#else
-#include <sys/mman.h>
-#endif
-
-#ifdef __MINGW32__
-#define _aligned_malloc __mingw_aligned_malloc
-#define _aligned_free __mingw_aligned_free
-#endif /*MINGW*/
-
-#if HAVE_VALGRIND_H
-# include <valgrind/valgrind.h>
-#endif
-
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mechanism");
static std::pair<const char*, simgrid::kernel::context::ContextFactoryInitializer> context_factories[] = {
simix_global->context_factory = nullptr;
}
-void *SIMIX_context_stack_new()
-{
- void *stack;
-
- if (smx_context_guard_size > 0 && not MC_is_active()) {
-
-#if !defined(PTH_STACKGROWTH) || (PTH_STACKGROWTH != -1)
- xbt_die("Stack overflow protection is known to be broken on your system: you stacks grow upwards (or detection is "
- "broken). "
- "Please disable stack guards with --cfg=contexts:guard-size:0");
- /* Current code for stack overflow protection assumes that stacks are growing downward (PTH_STACKGROWTH == -1).
- * Protected pages need to be put after the stack when PTH_STACKGROWTH == 1. */
-#endif
-
- size_t size = smx_context_stack_size + smx_context_guard_size;
-#if SIMGRID_HAVE_MC
- /* Cannot use posix_memalign when SIMGRID_HAVE_MC. Align stack by hand, and save the
- * pointer returned by xbt_malloc0. */
- char *alloc = (char*)xbt_malloc0(size + xbt_pagesize);
- stack = alloc - ((uintptr_t)alloc & (xbt_pagesize - 1)) + xbt_pagesize;
- *((void **)stack - 1) = alloc;
-#elif !defined(_WIN32)
- if (posix_memalign(&stack, xbt_pagesize, size) != 0)
- xbt_die("Failed to allocate stack.");
-#else
- stack = _aligned_malloc(size, xbt_pagesize);
-#endif
-
-#ifndef _WIN32
- if (mprotect(stack, smx_context_guard_size, PROT_NONE) == -1) {
- xbt_die(
- "Failed to protect stack: %s.\n"
- "If you are running a lot of actors, you may be exceeding the amount of mappings allowed per process.\n"
- "On Linux systems, change this value with sudo sysctl -w vm.max_map_count=newvalue (default value: 65536)\n"
- "Please see http://simgrid.gforge.inria.fr/simgrid/latest/doc/html/options.html#options_virt for more info.",
- strerror(errno));
- /* This is fatal. We are going to fail at some point when we try reusing this. */
- }
-#endif
- stack = (char *)stack + smx_context_guard_size;
- } else {
- stack = xbt_malloc0(smx_context_stack_size);
- }
-
-#if HAVE_VALGRIND_H
- unsigned int valgrind_stack_id = VALGRIND_STACK_REGISTER(stack, (char *)stack + smx_context_stack_size);
- memcpy((char *)stack + smx_context_usable_stack_size, &valgrind_stack_id, sizeof valgrind_stack_id);
-#endif
-
- return stack;
-}
-
-void SIMIX_context_stack_delete(void *stack)
-{
- if (not stack)
- return;
-
-#if HAVE_VALGRIND_H
- unsigned int valgrind_stack_id;
- memcpy(&valgrind_stack_id, (char *)stack + smx_context_usable_stack_size, sizeof valgrind_stack_id);
- VALGRIND_STACK_DEREGISTER(valgrind_stack_id);
-#endif
-
-#ifndef _WIN32
- if (smx_context_guard_size > 0 && not MC_is_active()) {
- stack = (char *)stack - smx_context_guard_size;
- if (mprotect(stack, smx_context_guard_size, PROT_READ | PROT_WRITE) == -1) {
- XBT_WARN("Failed to remove page protection: %s", strerror(errno));
- /* try to pursue anyway */
- }
-#if SIMGRID_HAVE_MC
- /* Retrieve the saved pointer. See SIMIX_context_stack_new above. */
- stack = *((void **)stack - 1);
-#endif
- }
-#endif /* not windows */
-
- xbt_free(stack);
-}
-
/** @brief Returns whether some parallel threads are used for the user contexts. */
int SIMIX_context_is_parallel() {
return smx_parallel_contexts > 1;