Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9281df081ec1111a7ea0c70fec48ad674d999a99
[simgrid.git] / src / simix / smx_global.cpp
1 /* Copyright (c) 2007-2021. 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 "mc/mc.h"
7 #include "simgrid/kernel/Timer.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/smpi/include/smpi_actor.hpp"
11
12 #include "simgrid/sg_config.hpp"
13 #include "src/kernel/EngineImpl.hpp"
14 #include "src/kernel/activity/ExecImpl.hpp"
15 #include "src/kernel/activity/IoImpl.hpp"
16 #include "src/kernel/activity/MailboxImpl.hpp"
17 #include "src/kernel/activity/SleepImpl.hpp"
18 #include "src/kernel/activity/SynchroRaw.hpp"
19 #include "src/mc/mc_record.hpp"
20 #include "src/mc/mc_replay.hpp"
21 #include "src/simix/smx_private.hpp"
22 #include "src/surf/xml/platf.hpp"
23
24 #include "simgrid/kernel/resource/Model.hpp"
25
26 #if SIMGRID_HAVE_MC
27 #include "src/mc/remote/AppSide.hpp"
28 #endif
29
30 #include <memory>
31
32 XBT_LOG_NEW_CATEGORY(simix, "All SIMIX categories");
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix, "Logging specific to SIMIX (kernel)");
34
35 std::unique_ptr<simgrid::simix::Global> simix_global;
36
37 void (*SMPI_switch_data_segment)(simgrid::s4u::ActorPtr) = nullptr;
38
39 namespace simgrid {
40 namespace simix {
41 config::Flag<bool> cfg_verbose_exit{"debug/verbose-exit", "Display the actor status at exit", true};
42
43 xbt_dynar_t simix_global_get_actors_addr()
44 {
45 #if SIMGRID_HAVE_MC
46   return simix_global->actors_vector;
47 #else
48   xbt_die("This function is intended to be used when compiling with MC");
49 #endif
50 }
51 xbt_dynar_t simix_global_get_dead_actors_addr()
52 {
53 #if SIMGRID_HAVE_MC
54   return simix_global->dead_actors_vector;
55 #else
56   xbt_die("This function is intended to be used when compiling with MC");
57 #endif
58 }
59
60 } // namespace simix
61 } // namespace simgrid
62
63 XBT_ATTRIB_NORETURN static void inthandler(int)
64 {
65   if (simgrid::simix::cfg_verbose_exit) {
66     XBT_INFO("CTRL-C pressed. The current status will be displayed before exit (disable that behavior with option "
67              "'debug/verbose-exit').");
68     simix_global->display_all_actor_status();
69   } else {
70     XBT_INFO("CTRL-C pressed, exiting. Hiding the current process status since 'debug/verbose-exit' is set to false.");
71   }
72   exit(1);
73 }
74
75 #ifndef _WIN32
76 static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
77 {
78   if ((siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_ACCERR) || siginfo->si_signo == SIGBUS) {
79     fprintf(stderr,
80             "Access violation or Bus error detected.\n"
81             "This probably comes from a programming error in your code, or from a stack\n"
82             "overflow. If you are certain of your code, try increasing the stack size\n"
83             "   --cfg=contexts/stack-size=XXX (current size is %u KiB).\n"
84             "\n"
85             "If it does not help, this may have one of the following causes:\n"
86             "a bug in SimGrid, a bug in the OS or a bug in a third-party libraries.\n"
87             "Failing hardware can sometimes generate such errors too.\n"
88             "\n"
89             "If you think you've found a bug in SimGrid, please report it along with a\n"
90             "Minimal Working Example (MWE) reproducing your problem and a full backtrace\n"
91             "of the fault captured with gdb or valgrind.\n",
92             smx_context_stack_size / 1024);
93   } else if (siginfo->si_signo == SIGSEGV) {
94     fprintf(stderr, "Segmentation fault.\n");
95 #if HAVE_SMPI
96     if (smpi_enabled() && smpi_cfg_privatization() == SmpiPrivStrategies::NONE) {
97 #if HAVE_PRIVATIZATION
98       fprintf(stderr, "Try to enable SMPI variable privatization with --cfg=smpi/privatization:yes.\n");
99 #else
100       fprintf(stderr, "Sadly, your system does not support --cfg=smpi/privatization:yes (yet).\n");
101 #endif /* HAVE_PRIVATIZATION */
102     }
103 #endif /* HAVE_SMPI */
104   }
105   std::raise(signum);
106 }
107
108 /**
109  * Install signal handler for SIGSEGV.  Check that nobody has already installed
110  * its own handler.  For example, the Java VM does this.
111  */
112 static void install_segvhandler()
113 {
114   stack_t old_stack;
115
116   if (simgrid::kernel::context::Context::install_sigsegv_stack(&old_stack, true) == -1) {
117     XBT_WARN("Failed to register alternate signal stack: %s", strerror(errno));
118     return;
119   }
120   if (not(old_stack.ss_flags & SS_DISABLE)) {
121     XBT_DEBUG("An alternate stack was already installed (sp=%p, size=%zu, flags=%x). Restore it.", old_stack.ss_sp,
122               old_stack.ss_size, (unsigned)old_stack.ss_flags);
123     sigaltstack(&old_stack, nullptr);
124   }
125
126   struct sigaction action;
127   struct sigaction old_action;
128   action.sa_sigaction = &segvhandler;
129   action.sa_flags     = SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
130   sigemptyset(&action.sa_mask);
131
132   /* Linux tend to raise only SIGSEGV where other systems also raise SIGBUS on severe error */
133   for (int sig : {SIGSEGV, SIGBUS}) {
134     if (sigaction(sig, &action, &old_action) == -1) {
135       XBT_WARN("Failed to register signal handler for signal %d: %s", sig, strerror(errno));
136       continue;
137     }
138     if ((old_action.sa_flags & SA_SIGINFO) || old_action.sa_handler != SIG_DFL) {
139       XBT_DEBUG("A signal handler was already installed for signal %d (%p). Restore it.", sig,
140                 (old_action.sa_flags & SA_SIGINFO) ? (void*)old_action.sa_sigaction : (void*)old_action.sa_handler);
141       sigaction(sig, &old_action, nullptr);
142     }
143   }
144 }
145
146 #endif /* _WIN32 */
147
148 /********************************* SIMIX **************************************/
149 namespace simgrid {
150 namespace simix {
151
152 void Global::empty_trash()
153 {
154   while (not actors_to_destroy.empty()) {
155     kernel::actor::ActorImpl* actor = &actors_to_destroy.front();
156     actors_to_destroy.pop_front();
157     XBT_DEBUG("Getting rid of %s (refcount: %d)", actor->get_cname(), actor->get_refcount());
158     intrusive_ptr_release(actor);
159   }
160 #if SIMGRID_HAVE_MC
161   xbt_dynar_reset(dead_actors_vector);
162 #endif
163 }
164
165 void Global::display_all_actor_status() const
166 {
167   XBT_INFO("%zu actors are still running, waiting for something.", process_list.size());
168   /*  List the actors and their state */
169   XBT_INFO("Legend of the following listing: \"Actor <pid> (<name>@<host>): <status>\"");
170   for (auto const& kv : process_list) {
171     kernel::actor::ActorImpl* actor = kv.second;
172
173     if (actor->waiting_synchro_) {
174       const char* synchro_description = "unknown";
175
176       if (boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(actor->waiting_synchro_) != nullptr)
177         synchro_description = "execution";
178
179       if (boost::dynamic_pointer_cast<kernel::activity::CommImpl>(actor->waiting_synchro_) != nullptr)
180         synchro_description = "communication";
181
182       if (boost::dynamic_pointer_cast<kernel::activity::SleepImpl>(actor->waiting_synchro_) != nullptr)
183         synchro_description = "sleeping";
184
185       if (boost::dynamic_pointer_cast<kernel::activity::RawImpl>(actor->waiting_synchro_) != nullptr)
186         synchro_description = "synchronization";
187
188       if (boost::dynamic_pointer_cast<kernel::activity::IoImpl>(actor->waiting_synchro_) != nullptr)
189         synchro_description = "I/O";
190
191       XBT_INFO("Actor %ld (%s@%s): waiting for %s activity %#zx (%s) in state %d to finish", actor->get_pid(),
192                actor->get_cname(), actor->get_host()->get_cname(), synchro_description,
193                (xbt_log_no_loc ? (size_t)0xDEADBEEF : (size_t)actor->waiting_synchro_.get()),
194                actor->waiting_synchro_->get_cname(), (int)actor->waiting_synchro_->state_);
195     } else {
196       XBT_INFO("Actor %ld (%s@%s) simcall %s", actor->get_pid(), actor->get_cname(), actor->get_host()->get_cname(),
197                SIMIX_simcall_name(actor->simcall_));
198     }
199   }
200 }
201
202 } // namespace simix
203 } // namespace simgrid
204
205 static simgrid::kernel::actor::ActorCode maestro_code;
206 void SIMIX_set_maestro(void (*code)(void*), void* data)
207 {
208 #ifdef _WIN32
209   XBT_INFO("WARNING, SIMIX_set_maestro is believed to not work on windows. Please help us investigating this issue if "
210            "you need that feature");
211 #endif
212   maestro_code = std::bind(code, data);
213 }
214
215 void SIMIX_global_init(int* argc, char** argv)
216 {
217   if (simix_global != nullptr)
218     return;
219
220   simix_global = std::make_unique<simgrid::simix::Global>();
221
222 #if SIMGRID_HAVE_MC
223   // The communication initialization is done ASAP, as we need to get some init parameters from the MC for different
224   // layers. But simix_global needs to be created, as we send the address of some of its fields to the MC that wants to
225   // read them directly.
226   simgrid::mc::AppSide::initialize();
227 #endif
228
229   surf_init(argc, argv); /* Initialize SURF structures */
230
231   simix_global->maestro_ = nullptr;
232   SIMIX_context_mod_init();
233
234   // Either create a new context with maestro or create
235   // a context object with the current context maestro):
236   simgrid::kernel::actor::create_maestro(maestro_code);
237
238   /* Prepare to display some more info when dying on Ctrl-C pressing */
239   std::signal(SIGINT, inthandler);
240
241 #ifndef _WIN32
242   install_segvhandler();
243 #endif
244   /* register a function to be called by SURF after the environment creation */
245   sg_platf_init();
246   simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
247
248   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
249     atexit(SIMIX_clean);
250 }
251
252 /**
253  * @ingroup SIMIX_API
254  * @brief Clean the SIMIX simulation
255  *
256  * This functions remove the memory used by SIMIX
257  */
258 void SIMIX_clean()
259 {
260   static bool smx_cleaned = false;
261   if (smx_cleaned)
262     return; // to avoid double cleaning by java and C
263
264   smx_cleaned = true;
265   XBT_DEBUG("SIMIX_clean called. Simulation's over.");
266   auto* engine = simgrid::kernel::EngineImpl::get_instance();
267   if (engine->has_actors_to_run() && SIMIX_get_clock() <= 0.0) {
268     XBT_CRITICAL("   ");
269     XBT_CRITICAL("The time is still 0, and you still have processes ready to run.");
270     XBT_CRITICAL("It seems that you forgot to run the simulation that you setup.");
271     xbt_die("Bailing out to avoid that stop-before-start madness. Please fix your code.");
272   }
273
274 #if HAVE_SMPI
275   if (not simix_global->process_list.empty()) {
276     if (smpi_process()->initialized()) {
277       xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
278     } else {
279       XBT_WARN("Process called exit when leaving - Skipping cleanups");
280       return;
281     }
282   }
283 #endif
284
285   /* Kill all processes (but maestro) */
286   simix_global->maestro_->kill_all();
287   engine->run_all_actors();
288   simix_global->empty_trash();
289
290   /* Exit the SIMIX network module */
291   SIMIX_mailbox_exit();
292
293   while (not simgrid::kernel::timer::kernel_timers().empty()) {
294     delete simgrid::kernel::timer::kernel_timers().top().second;
295     simgrid::kernel::timer::kernel_timers().pop();
296   }
297   /* Free the remaining data structures */
298   simix_global->actors_to_destroy.clear();
299   simix_global->process_list.clear();
300
301 #if SIMGRID_HAVE_MC
302   xbt_dynar_free(&simix_global->actors_vector);
303   xbt_dynar_free(&simix_global->dead_actors_vector);
304 #endif
305
306   /* Let's free maestro now */
307   delete simix_global->maestro_;
308   simix_global->maestro_ = nullptr;
309
310   /* Finish context module and SURF */
311   SIMIX_context_mod_exit();
312
313   surf_exit();
314
315   simix_global = nullptr;
316 }
317
318 /**
319  * @ingroup SIMIX_API
320  * @brief A clock (in second).
321  *
322  * @return Return the clock.
323  */
324 double SIMIX_get_clock()
325 {
326   if (MC_is_active() || MC_record_replay_is_active()) {
327     return MC_process_clock_get(SIMIX_process_self());
328   } else {
329     return surf_get_clock();
330   }
331 }
332
333 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
334 {
335   simgrid::kernel::EngineImpl::get_instance()->run();
336 }
337
338 double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
339 {
340   return simgrid::kernel::timer::Timer::next();
341 }
342
343 smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
344 {
345   return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
346 }
347
348 /** @brief cancels a timer that was added earlier */
349 void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
350 {
351   timer->remove();
352 }
353
354 /** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
355 double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
356 {
357   return timer ? timer->get_date() : 0.0;
358 }
359
360 void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
361 {
362   simix_global->display_all_actor_status();
363 }
364
365 int SIMIX_is_maestro()
366 {
367   if (simix_global == nullptr) // SimDag
368     return true;
369   const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
370   return self == nullptr || self == simix_global->maestro_;
371 }