Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move a static field of SwappedContext into SwappedContextFactory
[simgrid.git] / src / kernel / context / ContextUnix.cpp
1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 /* \file UContext.cpp Context switching with ucontexts from System V        */
7
8 #include "context_private.hpp"
9
10 #include "mc/mc.h"
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"
15
16 #include "ContextUnix.hpp"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19
20 /** Many integers are needed to store a pointer
21  *
22  * Support up to two ints. */
23 constexpr int CTX_ADDR_LEN = 2;
24
25 static_assert(sizeof(simgrid::kernel::context::UContext*) <= CTX_ADDR_LEN * sizeof(int),
26               "Ucontexts are not supported on this arch yet");
27
28 namespace simgrid {
29 namespace kernel {
30 namespace context {
31
32 // UContextFactory
33
34 UContextFactory::UContextFactory() : SwappedContextFactory("UContextFactory") {}
35 UContextFactory::~UContextFactory() = default;
36
37 Context* UContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process)
38 {
39   if (parallel_)
40     return new_context<ParallelUContext>(std::move(code), cleanup, process, this);
41   else
42     return new_context<UContext>(std::move(code), cleanup, process, this);
43 }
44
45
46 // UContext
47
48 UContext::UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
49                    SwappedContextFactory* factory)
50     : SwappedContext(std::move(code), cleanup_func, process, factory)
51 {
52   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
53   if (has_code()) {
54     getcontext(&this->uc_);
55     this->uc_.uc_link = nullptr;
56     this->uc_.uc_stack.ss_sp   = sg_makecontext_stack_addr(this->stack_);
57     this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_usable_stack_size);
58 #if PTH_STACKGROWTH == -1
59     ASAN_ONLY(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
60 #else
61     ASAN_ONLY(this->asan_stack_ = this->stack_);
62 #endif
63     UContext::make_ctx(&this->uc_, UContext::smx_ctx_sysv_wrapper, this);
64   } else {
65     if (process != nullptr && get_maestro() == nullptr)
66       set_maestro(this);
67   }
68
69 #if SIMGRID_HAVE_MC
70   if (MC_is_active() && has_code()) {
71     MC_register_stack_area(this->stack_, process, &(this->uc_), smx_context_usable_stack_size);
72   }
73 #endif
74 }
75
76 UContext::~UContext() = default;
77
78 // The name of this function is currently hardcoded in the code (as string).
79 // Do not change it without fixing those references as well.
80 void UContext::smx_ctx_sysv_wrapper(int i1, int i2)
81 {
82   // Rebuild the Context* pointer from the integers:
83   int ctx_addr[CTX_ADDR_LEN] = {i1, i2};
84   simgrid::kernel::context::UContext* context;
85   memcpy(&context, ctx_addr, sizeof context);
86
87   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
88   try {
89     (*context)();
90   } catch (simgrid::kernel::context::Context::StopRequest const&) {
91     XBT_DEBUG("Caught a StopRequest");
92   } catch (simgrid::Exception const& e) {
93     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
94     throw;
95   }
96   context->Context::stop();
97   ASAN_ONLY(context->asan_stop_ = true);
98   context->suspend();
99 }
100
101 /** A better makecontext
102  *
103  * Makecontext expects integer arguments, we the context variable is decomposed into a serie of integers and each
104  * integer is passed as argument to makecontext.
105  */
106 void UContext::make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg)
107 {
108   int ctx_addr[CTX_ADDR_LEN]{};
109   memcpy(ctx_addr, &arg, sizeof arg);
110   makecontext(ucp, (void (*)())func, 2, ctx_addr[0], ctx_addr[1]);
111 }
112
113 void UContext::swap_into(SwappedContext* to_)
114 {
115   UContext* to = static_cast<UContext*>(to_);
116   ASAN_ONLY(void* fake_stack = nullptr);
117   ASAN_ONLY(to->asan_ctx_ = from);
118   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
119   swapcontext(&this->uc_, &to->uc_);
120   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
121 }
122
123 void UContext::stop()
124 {
125   Context::stop();
126   throw StopRequest();
127 }
128
129 // ParallelUContext
130
131 void ParallelUContext::run_all()
132 {
133 }
134
135 /** Run one particular simulated process on the current thread.
136  *
137  * Only applied to the N first elements of the parmap array, where N is the amount of worker threads in the parmap.
138  * See ParallelUContext::run_all for details.
139  */
140 void ParallelUContext::resume()
141 {
142   // Save the thread number (my body) in an os-thread-specific area
143   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
144   // Save my current soul (either maestro, or one of the minions) in a permanent area
145   SwappedContext* worker_context = static_cast<SwappedContext*>(self());
146   workers_context_[worker_id_]   = worker_context;
147   // Switch my soul and the actor's one
148   Context::set_current(this);
149   worker_context->swap_into(this);
150   // No body runs that soul anymore at this point, but it is stored in a safe place.
151   // When the executed actor will do a blocking action, SIMIX_process_yield() will call suspend(), below.
152 }
153
154 /** Yield
155  *
156  * This function is called when a simulated process wants to yield back to the maestro in a blocking simcall,
157  * ie in SIMIX_process_yield().
158  *
159  * Actually, it does not really yield back to maestro, but directly into the next executable actor.
160  *
161  * This makes the parmap::apply awkward (see ParallelUContext::run_all()) because it only apply regularly
162  * on the few first elements of the array, but it saves a lot of context switches back to maestro,
163  * and directly forth to the next executable actor.
164  */
165 void ParallelUContext::suspend()
166 {
167   // Get some more work to directly swap into the next executable actor instead of yielding back to the parmap
168   boost::optional<smx_actor_t> next_work = parmap_->next();
169   SwappedContext* next_context;
170   if (next_work) {
171     // There is a next soul to embody (ie, another executable actor)
172     XBT_DEBUG("Run next process");
173     next_context = static_cast<ParallelUContext*>(next_work.get()->context_);
174   } else {
175     // All actors were run, go back to the parmap context
176     XBT_DEBUG("No more processes to run");
177     // worker_id_ is the identity of my body, stored in thread_local when starting the scheduling round
178     next_context = workers_context_[worker_id_];
179     // When given that soul, the body will wait for the next scheduling round
180   }
181
182   // Get the next soul to run, either from another actor or the initial minion's one
183   Context::set_current(next_context);
184   this->swap_into(next_context);
185 }
186
187 XBT_PRIVATE ContextFactory* sysv_factory()
188 {
189   XBT_VERB("Activating SYSV context factory");
190   return new UContextFactory();
191 }
192 }}} // namespace simgrid::kernel::context