1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* \file UContext.cpp Context switching with ucontexts from System V */
8 #include "context_private.hpp"
11 #include "simgrid/Exception.hpp"
12 #include "src/mc/mc_ignore.hpp"
13 #include "src/simix/ActorImpl.hpp"
14 #include "src/simix/smx_private.hpp"
16 #include "ContextUnix.hpp"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
20 /** Many integers are needed to store a pointer
22 * Support up to two ints. */
23 constexpr int CTX_ADDR_LEN = 2;
25 static_assert(sizeof(simgrid::kernel::context::UContext*) <= CTX_ADDR_LEN * sizeof(int),
26 "Ucontexts are not supported on this arch yet");
34 UContextFactory::UContextFactory() : ContextFactory("UContextFactory"), parallel_(SIMIX_context_is_parallel())
36 UContext::set_maestro(nullptr);
38 ParallelUContext::initialize();
41 UContextFactory::~UContextFactory()
44 ParallelUContext::finalize();
47 Context* UContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process)
50 return new_context<ParallelUContext>(std::move(code), cleanup, process);
52 return new_context<UContext>(std::move(code), cleanup, process);
55 /* This function is called by maestro at the beginning of a scheduling round to get all working threads executing some
56 * stuff It is much easier to understand what happens if you see the working threads as bodies that swap their soul for
57 * the ones of the simulated processes that must run.
59 void UContextFactory::run_all()
62 ParallelUContext::run_all();
64 SwappedContext::run_all();
69 UContext::UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
70 : SwappedContext(std::move(code), cleanup_func, process)
72 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
74 getcontext(&this->uc_);
75 this->uc_.uc_link = nullptr;
76 this->uc_.uc_stack.ss_sp = sg_makecontext_stack_addr(this->stack_);
77 this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_usable_stack_size);
78 #if PTH_STACKGROWTH == -1
79 ASAN_ONLY(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
81 ASAN_ONLY(this->asan_stack_ = this->stack_);
83 UContext::make_ctx(&this->uc_, UContext::smx_ctx_sysv_wrapper, this);
85 if (process != nullptr && get_maestro() == nullptr)
90 if (MC_is_active() && has_code()) {
91 MC_register_stack_area(this->stack_, process, &(this->uc_), smx_context_usable_stack_size);
96 UContext::~UContext() = default;
98 // The name of this function is currently hardcoded in the code (as string).
99 // Do not change it without fixing those references as well.
100 void UContext::smx_ctx_sysv_wrapper(int i1, int i2)
102 // Rebuild the Context* pointer from the integers:
103 int ctx_addr[CTX_ADDR_LEN] = {i1, i2};
104 simgrid::kernel::context::UContext* context;
105 memcpy(&context, ctx_addr, sizeof context);
107 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
110 } catch (simgrid::kernel::context::Context::StopRequest const&) {
111 XBT_DEBUG("Caught a StopRequest");
112 } catch (simgrid::Exception const& e) {
113 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
116 context->Context::stop();
117 ASAN_ONLY(context->asan_stop_ = true);
121 /** A better makecontext
123 * Makecontext expects integer arguments, we the context variable is decomposed into a serie of integers and each
124 * integer is passed as argument to makecontext.
126 void UContext::make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg)
128 int ctx_addr[CTX_ADDR_LEN]{};
129 memcpy(ctx_addr, &arg, sizeof arg);
130 makecontext(ucp, (void (*)())func, 2, ctx_addr[0], ctx_addr[1]);
133 void UContext::swap_into(SwappedContext* to_)
135 UContext* to = static_cast<UContext*>(to_);
136 ASAN_ONLY(void* fake_stack = nullptr);
137 ASAN_ONLY(to->asan_ctx_ = from);
138 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
139 swapcontext(&this->uc_, &to->uc_);
140 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
143 void UContext::stop()
151 void ParallelUContext::run_all()
153 threads_working_ = 0;
154 // Parmap_apply ensures that every working thread get an index in the process_to_run array (through an atomic
155 // fetch_and_add), and runs the ParallelUContext::resume function on that index
157 // We lazily create the parmap because the parmap creates context with simix_global->context_factory (which might not
158 // be initialized when bootstrapping):
159 if (parmap_ == nullptr)
160 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
162 [](smx_actor_t process) {
163 ParallelUContext* context = static_cast<ParallelUContext*>(process->context_);
166 simix_global->process_to_run);
171 * This function is called when a simulated process wants to yield back to the maestro in a blocking simcall. This
172 * naturally occurs within SIMIX_context_suspend(self->context), called from SIMIX_process_yield() Actually, it does not
173 * really yield back to maestro, but into the next process that must be executed. If no one is to be executed, then it
174 * yields to the initial soul that was in this working thread (that was saved in resume_parallel).
176 void ParallelUContext::suspend()
178 /* determine the next context */
179 // Get the next soul to embody now:
180 boost::optional<smx_actor_t> next_work = parmap_->next();
181 SwappedContext* next_context;
183 // There is a next soul to embody (ie, a next process to resume)
184 XBT_DEBUG("Run next process");
185 next_context = static_cast<ParallelUContext*>(next_work.get()->context_);
187 // All processes were run, go to the barrier
188 XBT_DEBUG("No more processes to run");
189 // worker_id_ is the identity of my body, stored in thread_local when starting the scheduling round
190 // Deduce the initial soul of that body
191 next_context = workers_context_[worker_id_];
192 // When given that soul, the body will wait for the next scheduling round
195 Context::set_current(next_context);
196 // Get the next soul to run, either simulated or initial minion's one:
197 this->swap_into(next_context);
200 /** Run one particular simulated process on the current thread. */
201 void ParallelUContext::resume()
203 // What is my containing body? Store its number in os-thread-specific area :
204 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
205 // Get my current soul:
206 ParallelUContext* worker_context = static_cast<ParallelUContext*>(self());
207 // Write down that this soul is hosted in that body (for now)
208 workers_context_[worker_id_] = worker_context;
209 // Write in simix that I switched my soul
210 Context::set_current(this);
211 // Actually do that using the relevant library call:
212 worker_context->swap_into(this);
213 // No body runs that soul anymore at this point. Instead the current body took the soul of simulated process The
214 // simulated process wakes back after the call to "SIMIX_context_suspend(self->context);" within
215 // smx_process.c::SIMIX_process_yield()
217 // From now on, the simulated processes will change their soul with the next soul to execute (in suspend_parallel,
218 // below). When nobody is to be executed in this scheduling round, the last simulated process will take back the
219 // initial soul of the current working thread
222 XBT_PRIVATE ContextFactory* sysv_factory()
224 XBT_VERB("Activating SYSV context factory");
225 return new UContextFactory();
227 }}} // namespace simgrid::kernel::context