X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b7b9fb781045188e65beb5c1dfc391a2d21e5472..7dd253f0ff4dda733dd0f7c924a25df4b777f0d0:/src/kernel/context/ContextSwapped.cpp diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index 2fd0f477a9..e2ca4165ff 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -6,8 +6,8 @@ #include "simgrid/Exception.hpp" #include "simgrid/modelchecker.h" #include "src/internal_config.h" +#include "src/kernel/actor/ActorImpl.hpp" #include "src/kernel/context/context_private.hpp" -#include "src/simix/ActorImpl.hpp" #include "src/simix/smx_private.hpp" #include "xbt/parmap.hpp" @@ -35,29 +35,19 @@ namespace simgrid { namespace kernel { namespace context { -/* rank of the execution thread */ -thread_local uintptr_t SwappedContext::worker_id_; /* thread-specific storage for the thread id */ - -SwappedContextFactory::SwappedContextFactory() : ContextFactory(), parallel_(SIMIX_context_is_parallel()) -{ - parmap_ = nullptr; // will be created lazily with the right parameters if needed (ie, in parallel) - workers_context_.resize(parallel_ ? SIMIX_context_get_nthreads() : 1, nullptr); -} -SwappedContextFactory::~SwappedContextFactory() -{ - delete parmap_; -} +/* thread-specific storage for the worker's context */ +thread_local SwappedContext* SwappedContext::worker_context_ = nullptr; SwappedContext::SwappedContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory) : Context(std::move(code), actor), factory_(factory) { // Save maestro (=context created first) in preparation for run_all - if (not factory->parallel_ && factory_->workers_context_[0] == nullptr) - factory_->workers_context_[0] = this; + if (not SIMIX_context_is_parallel() && factory->maestro_context_ == nullptr) + factory->maestro_context_ = this; if (has_code()) { + xbt_assert((smx_context_stack_size & 0xf) == 0, "smx_context_stack_size should be multiple of 16"); 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 " @@ -89,8 +79,9 @@ SwappedContext::SwappedContext(std::function&& code, smx_actor_t actor, "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.", + "Please see " + "https://simgrid.org/doc/latest/Configuring_SimGrid.html#configuring-the-user-code-virtualization for more " + "information.", strerror(errno)); /* This is fatal. We are going to fail at some point when we try reusing this. */ } @@ -101,13 +92,13 @@ SwappedContext::SwappedContext(std::function&& code, smx_actor_t actor, } #if PTH_STACKGROWTH == -1 - ASAN_ONLY(this->asan_stack_ = this->stack_ + smx_context_usable_stack_size); + ASAN_ONLY(this->asan_stack_ = this->stack_ + smx_context_stack_size); #else ASAN_ONLY(this->asan_stack_ = this->stack_); #endif #if HAVE_VALGRIND_H - unsigned int valgrind_stack_id = VALGRIND_STACK_REGISTER(this->stack_, this->stack_ + smx_context_stack_size); - memcpy(this->stack_ + smx_context_usable_stack_size, &valgrind_stack_id, sizeof valgrind_stack_id); + if (RUNNING_ON_VALGRIND) + this->valgrind_stack_id_ = VALGRIND_STACK_REGISTER(this->stack_, this->stack_ + smx_context_stack_size); #endif } } @@ -118,9 +109,8 @@ SwappedContext::~SwappedContext() return; #if HAVE_VALGRIND_H - unsigned int valgrind_stack_id; - memcpy(&valgrind_stack_id, stack_ + smx_context_usable_stack_size, sizeof valgrind_stack_id); - VALGRIND_STACK_DEREGISTER(valgrind_stack_id); + if (RUNNING_ON_VALGRIND) + VALGRIND_STACK_DEREGISTER(valgrind_stack_id_); #endif #ifndef _WIN32 @@ -159,12 +149,11 @@ void SwappedContextFactory::run_all() * stuff It is much easier to understand what happens if you see the working threads as bodies that swap their soul * for the ones of the simulated processes that must run. */ - if (parallel_) { - threads_working_ = 0; - + if (SIMIX_context_is_parallel()) { // We lazily create the parmap so that all options are actually processed when doing so. if (parmap_ == nullptr) - parmap_ = new simgrid::xbt::Parmap(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode()); + parmap_.reset( + new simgrid::xbt::Parmap(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode())); // Usually, Parmap::apply() executes the provided function on all elements of the array. // Here, the executed function does not return the control to the parmap before all the array is processed: @@ -174,7 +163,7 @@ void SwappedContextFactory::run_all() // - So, resume() is only launched from the parmap for the first job of each minion. parmap_->apply( [](smx_actor_t process) { - SwappedContext* context = static_cast(process->context_); + SwappedContext* context = static_cast(process->context_.get()); context->resume(); }, simix_global->actors_to_run); @@ -183,10 +172,10 @@ void SwappedContextFactory::run_all() return; /* maestro is already saved in the first slot of workers_context_ */ - smx_actor_t first_actor = simix_global->actors_to_run.front(); + const actor::ActorImpl* first_actor = simix_global->actors_to_run.front(); process_index_ = 1; /* execute the first actor; it will chain to the others when using suspend() */ - static_cast(first_actor->context_)->resume(); + static_cast(first_actor->context_.get())->resume(); } } @@ -198,15 +187,12 @@ void SwappedContextFactory::run_all() */ void SwappedContext::resume() { - if (factory_->parallel_) { - // Save the thread number (my body) in an os-thread-specific area - worker_id_ = factory_->threads_working_.fetch_add(1, std::memory_order_relaxed); - // Save my current soul (either maestro, or one of the minions) in a permanent area - SwappedContext* worker_context = static_cast(self()); - factory_->workers_context_[worker_id_] = worker_context; + if (SIMIX_context_is_parallel()) { + // Save my current soul (either maestro, or one of the minions) in a thread-specific area + worker_context_ = static_cast(self()); // Switch my soul and the actor's one Context::set_current(this); - worker_context->swap_into(this); + worker_context_->swap_into(this); // No body runs that soul anymore at this point, but it is stored in a safe place. // When the executed actor will do a blocking action, ActorImpl::yield() will call suspend(), below. } else { // sequential execution @@ -226,19 +212,19 @@ void SwappedContext::resume() */ void SwappedContext::suspend() { - if (factory_->parallel_) { + if (SIMIX_context_is_parallel()) { // Get some more work to directly swap into the next executable actor instead of yielding back to the parmap boost::optional next_work = factory_->parmap_->next(); SwappedContext* next_context; if (next_work) { // There is a next soul to embody (ie, another executable actor) XBT_DEBUG("Run next process"); - next_context = static_cast(next_work.get()->context_); + next_context = static_cast(next_work.get()->context_.get()); } else { // All actors were run, go back to the parmap context XBT_DEBUG("No more actors to run"); - // worker_id_ is the identity of my body, stored in thread_local when starting the scheduling round - next_context = factory_->workers_context_[worker_id_]; + // worker_context_ is my own soul, stored in thread_local when starting the scheduling round + next_context = worker_context_; // When given that soul, the body will wait for the next scheduling round } @@ -255,11 +241,11 @@ void SwappedContext::suspend() if (i < simix_global->actors_to_run.size()) { /* Actually swap into the next actor directly without transiting to maestro */ XBT_DEBUG("Run next actor"); - next_context = static_cast(simix_global->actors_to_run[i]->context_); + next_context = static_cast(simix_global->actors_to_run[i]->context_.get()); } else { /* all processes were run, actually return to maestro */ XBT_DEBUG("No more actors to run"); - next_context = factory_->workers_context_[0]; + next_context = factory_->maestro_context_; } Context::set_current(next_context); this->swap_into(next_context);