Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6a8beb7c37d9276c96d242aa9c6b85c6b02e55c8
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2017. 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 #include "ContextBoost.hpp"
7
8 #include <utility>
9 #include <xbt/log.h>
10
11 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
12 #include <sanitizer/asan_interface.h>
13 #define ASAN_EVAL(expr) (expr)
14 #define ASAN_START_SWITCH(fake_stack_save, bottom, size) __sanitizer_start_switch_fiber(fake_stack_save, bottom, size)
15 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old)                                                      \
16   __sanitizer_finish_switch_fiber(fake_stack_save, bottom_old, size_old)
17 #else
18 #define ASAN_EVAL(expr) (void)0
19 #define ASAN_START_SWITCH(fake_stack_save, bottom, size) (void)0
20 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) (void)(fake_stack_save)
21 #endif
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
24
25 namespace simgrid {
26 namespace kernel {
27 namespace context {
28
29 // BoostContextFactory
30
31 BoostContextFactory::BoostContextFactory()
32   : ContextFactory("BoostContextFactory")
33 {
34   BoostContext::parallel_ = SIMIX_context_is_parallel();
35   if (BoostContext::parallel_) {
36 #if HAVE_THREAD_CONTEXTS
37     BoostContext::parmap_ = nullptr;
38     BoostContext::workers_context_.clear();
39     BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
40     BoostContext::maestro_context_ = nullptr;
41     xbt_os_thread_key_create(&BoostContext::worker_id_key_);
42 #else
43     xbt_die("No thread support for parallel context execution");
44 #endif
45   }
46 }
47
48 BoostContextFactory::~BoostContextFactory()
49 {
50 #if HAVE_THREAD_CONTEXTS
51   delete BoostContext::parmap_;
52   BoostContext::parmap_ = nullptr;
53   BoostContext::workers_context_.clear();
54 #endif
55 }
56
57 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
58                                                   smx_actor_t process)
59 {
60 #if HAVE_THREAD_CONTEXTS
61   if (BoostContext::parallel_)
62     return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
63 #endif
64
65   return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
66 }
67
68 void BoostContextFactory::run_all()
69 {
70 #if HAVE_THREAD_CONTEXTS
71   if (BoostContext::parallel_) {
72     BoostContext::threads_working_ = 0;
73     if (not BoostContext::parmap_)
74       BoostContext::parmap_ =
75           new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
76     BoostContext::parmap_->apply(
77         [](smx_actor_t process) {
78           BoostContext* context = static_cast<BoostContext*>(process->context);
79           return context->resume();
80         },
81         simix_global->process_to_run);
82     return;
83   }
84 #endif
85
86   if (simix_global->process_to_run.empty())
87     return;
88   smx_actor_t first_process    = simix_global->process_to_run.front();
89   BoostContext::process_index_ = 1;
90   /* execute the first process */
91   static_cast<BoostContext*>(first_process->context)->resume();
92 }
93
94
95 // BoostContext
96
97 bool BoostContext::parallel_                             = false;
98 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
99 uintptr_t BoostContext::threads_working_                 = 0;
100 xbt_os_thread_key_t BoostContext::worker_id_key_;
101 unsigned long BoostContext::process_index_   = 0;
102 BoostContext* BoostContext::maestro_context_ = nullptr;
103 std::vector<BoostContext*> BoostContext::workers_context_;
104
105 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
106     : Context(std::move(code), cleanup_func, process)
107 {
108
109   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
110   if (has_code()) {
111     this->stack_ = SIMIX_context_stack_new();
112     /* We need to pass the bottom of the stack to make_fcontext,
113        depending on the stack direction it may be the lower or higher address: */
114 #if PTH_STACKGROWTH == -1
115     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
116 #else
117     void* stack = this->stack_;
118 #endif
119     ASAN_EVAL(this->asan_stack_ = stack);
120 #if BOOST_VERSION < 106100
121     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
122 #else
123     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
124 #endif
125   } else {
126 #if BOOST_VERSION < 105600
127     this->fc_ = new boost::context::fcontext_t();
128 #endif
129     if (BoostContext::maestro_context_ == nullptr)
130       BoostContext::maestro_context_ = this;
131   }
132 }
133
134 BoostContext::~BoostContext()
135 {
136 #if BOOST_VERSION < 105600
137   if (not this->stack_)
138     delete this->fc_;
139 #endif
140   if (this == maestro_context_)
141     maestro_context_ = nullptr;
142   SIMIX_context_stack_delete(this->stack_);
143 }
144
145 void BoostContext::wrapper(BoostContext::arg_type arg)
146 {
147 #if BOOST_VERSION < 106100
148   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
149 #else
150   ASAN_FINISH_SWITCH(nullptr, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
151                      &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
152   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
153   BoostContext* context                         = static_cast<BoostContext**>(arg.data)[1];
154 #endif
155   try {
156     (*context)();
157     context->Context::stop();
158   } catch (StopRequest const&) {
159     XBT_DEBUG("Caught a StopRequest");
160   }
161   ASAN_EVAL(context->asan_stop_ = true);
162   context->suspend();
163 }
164
165 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
166 {
167 #if BOOST_VERSION < 105600
168   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
169 #elif BOOST_VERSION < 106100
170   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
171 #else
172   BoostContext* ctx[2] = {from, to};
173   void* fake_stack;
174   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
175   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
176   ASAN_FINISH_SWITCH(fake_stack, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
177                      &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
178   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
179 #endif
180 }
181
182 void BoostContext::stop()
183 {
184   Context::stop();
185   throw StopRequest();
186 }
187
188 // SerialBoostContext
189
190 void SerialBoostContext::suspend()
191 {
192   /* determine the next context */
193   SerialBoostContext* next_context;
194   unsigned long int i = process_index_;
195   process_index_++;
196
197   if (i < simix_global->process_to_run.size()) {
198     /* execute the next process */
199     XBT_DEBUG("Run next process");
200     next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context);
201   } else {
202     /* all processes were run, return to maestro */
203     XBT_DEBUG("No more process to run");
204     next_context = static_cast<SerialBoostContext*>(maestro_context_);
205   }
206   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
207   BoostContext::swap(this, next_context);
208 }
209
210 void SerialBoostContext::resume()
211 {
212   SIMIX_context_set_current(this);
213   BoostContext::swap(maestro_context_, this);
214 }
215
216 // ParallelBoostContext
217
218 #if HAVE_THREAD_CONTEXTS
219
220 void ParallelBoostContext::suspend()
221 {
222   boost::optional<smx_actor_t> next_work = parmap_->next();
223   ParallelBoostContext* next_context;
224   if (next_work) {
225     XBT_DEBUG("Run next process");
226     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context);
227   } else {
228     XBT_DEBUG("No more processes to run");
229     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
230     next_context        = static_cast<ParallelBoostContext*>(workers_context_[worker_id]);
231   }
232
233   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
234   BoostContext::swap(this, next_context);
235 }
236
237 void ParallelBoostContext::resume()
238 {
239   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
240   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
241
242   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
243   workers_context_[worker_id] = worker_context;
244
245   SIMIX_context_set_current(this);
246   BoostContext::swap(worker_context, this);
247 }
248
249 #endif
250
251 XBT_PRIVATE ContextFactory* boost_factory()
252 {
253   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
254   return new BoostContextFactory();
255 }
256 }}} // namespace