From f45abd9a448a9196144cb2aa845cb966b741d562 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sat, 5 Jan 2019 22:43:11 +0100 Subject: [PATCH] further cleanups in contexts - Kill SerialRawContext and use SwappedContext - Centralize everything related to the stack pointer in SwappedContext Previously, alloc/dealloc was done in functions in Context.cpp while the fields were duplicated in all SwappedContext childs --- src/kernel/context/Context.hpp | 3 - src/kernel/context/ContextBoost.cpp | 72 ++++-------------- src/kernel/context/ContextBoost.hpp | 28 +------ src/kernel/context/ContextRaw.cpp | 72 ++++-------------- src/kernel/context/ContextRaw.hpp | 26 +------ src/kernel/context/ContextSwapped.cpp | 102 +++++++++++++++++++++++++- src/kernel/context/ContextSwapped.hpp | 12 +-- src/kernel/context/ContextUnix.cpp | 6 +- src/kernel/context/ContextUnix.hpp | 1 - src/simix/smx_context.cpp | 98 ------------------------- 10 files changed, 146 insertions(+), 274 deletions(-) diff --git a/src/kernel/context/Context.hpp b/src/kernel/context/Context.hpp index 6271a8b704..b16f819291 100644 --- a/src/kernel/context/Context.hpp +++ b/src/kernel/context/Context.hpp @@ -146,9 +146,6 @@ XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ]; /** @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(); diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index e9ea749708..ac3313465f 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -35,7 +35,7 @@ smx_context_t BoostContextFactory::create_context(std::function code, vo { if (parallel_) return this->new_context(std::move(code), cleanup_func, process); - return this->new_context(std::move(code), cleanup_func, process); + return this->new_context(std::move(code), cleanup_func, process); } void BoostContextFactory::run_all() @@ -43,20 +43,17 @@ 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 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 @@ -74,8 +71,8 @@ BoostContext::BoostContext(std::function code, void_pfn_smxprocess_t cle #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); } } @@ -85,9 +82,8 @@ BoostContext::~BoostContext() 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) @@ -113,14 +109,15 @@ 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(to_); #if BOOST_VERSION < 105600 - boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast(to)); + boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast(to)); #elif BOOST_VERSION < 106100 - boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast(to)); + boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast(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_); @@ -137,47 +134,6 @@ void BoostContext::stop() 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(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(BoostContext::get_maestro()); - } - Context::set_current(next_context); - BoostContext::swap(this, next_context); -} - -void SerialBoostContext::resume() -{ - BoostContext* old = static_cast(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(first_process->context_)->resume(); -} - // ParallelBoostContext simgrid::xbt::Parmap* ParallelBoostContext::parmap_; @@ -225,7 +181,7 @@ void ParallelBoostContext::suspend() } Context::set_current(next_context); - BoostContext::swap(this, next_context); + this->swap_into(next_context); } void ParallelBoostContext::resume() @@ -236,7 +192,7 @@ void ParallelBoostContext::resume() workers_context_[worker_id_] = worker_context; Context::set_current(this); - BoostContext::swap(worker_context, this); + worker_context->swap_into(this); } diff --git a/src/kernel/context/ContextBoost.hpp b/src/kernel/context/ContextBoost.hpp index fb60021fd9..643549cd31 100644 --- a/src/kernel/context/ContextBoost.hpp +++ b/src/kernel/context/ContextBoost.hpp @@ -22,29 +22,24 @@ #include #include -#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 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; @@ -65,21 +60,6 @@ private: static void wrapper(arg_type arg); }; -class SerialBoostContext : public BoostContext { -public: - SerialBoostContext(std::function 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 code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process) diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index 93d7d86824..31b2aafe59 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -206,7 +206,7 @@ Context* RawContextFactory::create_context(std::function code, void_pfn_ { if (parallel_) return this->new_context(std::move(code), cleanup_func, process); - return this->new_context(std::move(code), cleanup_func, process); + return this->new_context(std::move(code), cleanup_func, process); } void RawContextFactory::run_all() @@ -214,18 +214,15 @@ 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 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(this->stack_) + smx_context_usable_stack_size); #else @@ -233,17 +230,16 @@ RawContext::RawContext(std::function code, void_pfn_smxprocess_t cleanup #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(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) { @@ -263,12 +259,13 @@ 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(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_); } @@ -278,45 +275,6 @@ void RawContext::stop() 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(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(RawContext::get_maestro()); - } - Context::set_current(next_context); - RawContext::swap(this, next_context); -} - -void SerialRawContext::resume() -{ - RawContext* old = static_cast(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(first_process->context_)->resume(); -} - // ParallelRawContext simgrid::xbt::Parmap* ParallelRawContext::parmap_; @@ -369,7 +327,7 @@ void ParallelRawContext::suspend() } Context::set_current(next_context); - RawContext::swap(this, next_context); + this->swap_into(next_context); } void ParallelRawContext::resume() @@ -379,7 +337,7 @@ 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() diff --git a/src/kernel/context/ContextRaw.hpp b/src/kernel/context/ContextRaw.hpp index e1ececd741..f1b7914011 100644 --- a/src/kernel/context/ContextRaw.hpp +++ b/src/kernel/context/ContextRaw.hpp @@ -14,6 +14,8 @@ #include #include +#include "src/kernel/context/ContextSwapped.hpp" + namespace simgrid { namespace kernel { namespace context { @@ -23,20 +25,15 @@ 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 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; @@ -50,21 +47,6 @@ private: static void wrapper(void* arg); }; -class SerialRawContext : public RawContext { -public: - SerialRawContext(std::function 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 code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process) diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index 6897767f37..437fce0600 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -3,11 +3,29 @@ /* 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 +#include +#else +#include +#endif + +#ifdef __MINGW32__ +#define _aligned_malloc __mingw_aligned_malloc +#define _aligned_free __mingw_aligned_free +#endif /*MINGW*/ + +#if HAVE_VALGRIND_H +#include +#endif XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context); @@ -19,6 +37,88 @@ unsigned long SwappedContext::process_index_; SwappedContext* SwappedContext::maestro_context_ = nullptr; +SwappedContext::SwappedContext(std::function 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 */ diff --git a/src/kernel/context/ContextSwapped.hpp b/src/kernel/context/ContextSwapped.hpp index 8b5188d941..e132621984 100644 --- a/src/kernel/context/ContextSwapped.hpp +++ b/src/kernel/context/ContextSwapped.hpp @@ -6,7 +6,7 @@ #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 { @@ -14,10 +14,9 @@ namespace context { class SwappedContext : public Context { public: - SwappedContext(std::function code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process) - : Context(std::move(code), cleanup_func, process) - { - } + SwappedContext(std::function code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process); + virtual ~SwappedContext(); + virtual void suspend(); virtual void resume(); @@ -28,6 +27,9 @@ public: 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_; diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index ba2bce922a..2d4bf3b129 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -71,7 +71,6 @@ UContext::UContext(std::function code, void_pfn_smxprocess_t cleanup_fun { /* 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_); @@ -94,10 +93,7 @@ UContext::UContext(std::function code, void_pfn_smxprocess_t cleanup_fun #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. diff --git a/src/kernel/context/ContextUnix.hpp b/src/kernel/context/ContextUnix.hpp index e295305be0..1fcbc338d5 100644 --- a/src/kernel/context/ContextUnix.hpp +++ b/src/kernel/context/ContextUnix.hpp @@ -33,7 +33,6 @@ public: 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 diff --git a/src/simix/smx_context.cpp b/src/simix/smx_context.cpp index 14a36f9a84..3a6a2245df 100644 --- a/src/simix/smx_context.cpp +++ b/src/simix/smx_context.cpp @@ -5,29 +5,11 @@ /* 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 -#ifdef _WIN32 -#include -#include -#else -#include -#endif - -#ifdef __MINGW32__ -#define _aligned_malloc __mingw_aligned_malloc -#define _aligned_free __mingw_aligned_free -#endif /*MINGW*/ - -#if HAVE_VALGRIND_H -# include -#endif - XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mechanism"); static std::pair context_factories[] = { @@ -139,86 +121,6 @@ void SIMIX_context_mod_exit() 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; -- 2.20.1