1 /* Copyright (c) 2007-2021. 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. */
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"
12 #include "simgrid/sg_config.hpp"
13 #include "src/kernel/EngineImpl.hpp"
14 #include "src/mc/mc_record.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "src/simix/smx_private.hpp"
17 #include "src/surf/xml/platf.hpp"
19 #include "simgrid/kernel/resource/Model.hpp"
22 #include "src/mc/remote/AppSide.hpp"
27 XBT_LOG_NEW_CATEGORY(simix, "All SIMIX categories");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix, "Logging specific to SIMIX (kernel)");
30 std::unique_ptr<simgrid::simix::Global> simix_global;
32 void (*SMPI_switch_data_segment)(simgrid::s4u::ActorPtr) = nullptr;
36 config::Flag<bool> cfg_verbose_exit{"debug/verbose-exit", "Display the actor status at exit", true};
38 xbt_dynar_t simix_global_get_actors_addr()
41 return kernel::EngineImpl::get_instance()->get_actors_vector();
43 xbt_die("This function is intended to be used when compiling with MC");
46 xbt_dynar_t simix_global_get_dead_actors_addr()
49 return kernel::EngineImpl::get_instance()->get_dead_actors_vector();
51 xbt_die("This function is intended to be used when compiling with MC");
56 } // namespace simgrid
58 XBT_ATTRIB_NORETURN static void inthandler(int)
60 if (simgrid::simix::cfg_verbose_exit) {
61 XBT_INFO("CTRL-C pressed. The current status will be displayed before exit (disable that behavior with option "
62 "'debug/verbose-exit').");
63 simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
65 XBT_INFO("CTRL-C pressed, exiting. Hiding the current process status since 'debug/verbose-exit' is set to false.");
71 static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
73 if ((siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_ACCERR) || siginfo->si_signo == SIGBUS) {
75 "Access violation or Bus error detected.\n"
76 "This probably comes from a programming error in your code, or from a stack\n"
77 "overflow. If you are certain of your code, try increasing the stack size\n"
78 " --cfg=contexts/stack-size=XXX (current size is %u KiB).\n"
80 "If it does not help, this may have one of the following causes:\n"
81 "a bug in SimGrid, a bug in the OS or a bug in a third-party libraries.\n"
82 "Failing hardware can sometimes generate such errors too.\n"
84 "If you think you've found a bug in SimGrid, please report it along with a\n"
85 "Minimal Working Example (MWE) reproducing your problem and a full backtrace\n"
86 "of the fault captured with gdb or valgrind.\n",
87 smx_context_stack_size / 1024);
88 } else if (siginfo->si_signo == SIGSEGV) {
89 fprintf(stderr, "Segmentation fault.\n");
91 if (smpi_enabled() && smpi_cfg_privatization() == SmpiPrivStrategies::NONE) {
92 #if HAVE_PRIVATIZATION
93 fprintf(stderr, "Try to enable SMPI variable privatization with --cfg=smpi/privatization:yes.\n");
95 fprintf(stderr, "Sadly, your system does not support --cfg=smpi/privatization:yes (yet).\n");
96 #endif /* HAVE_PRIVATIZATION */
98 #endif /* HAVE_SMPI */
104 * Install signal handler for SIGSEGV. Check that nobody has already installed
105 * its own handler. For example, the Java VM does this.
107 static void install_segvhandler()
111 if (simgrid::kernel::context::Context::install_sigsegv_stack(&old_stack, true) == -1) {
112 XBT_WARN("Failed to register alternate signal stack: %s", strerror(errno));
115 if (not(old_stack.ss_flags & SS_DISABLE)) {
116 XBT_DEBUG("An alternate stack was already installed (sp=%p, size=%zu, flags=%x). Restore it.", old_stack.ss_sp,
117 old_stack.ss_size, (unsigned)old_stack.ss_flags);
118 sigaltstack(&old_stack, nullptr);
121 struct sigaction action;
122 struct sigaction old_action;
123 action.sa_sigaction = &segvhandler;
124 action.sa_flags = SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
125 sigemptyset(&action.sa_mask);
127 /* Linux tend to raise only SIGSEGV where other systems also raise SIGBUS on severe error */
128 for (int sig : {SIGSEGV, SIGBUS}) {
129 if (sigaction(sig, &action, &old_action) == -1) {
130 XBT_WARN("Failed to register signal handler for signal %d: %s", sig, strerror(errno));
133 if ((old_action.sa_flags & SA_SIGINFO) || old_action.sa_handler != SIG_DFL) {
134 XBT_DEBUG("A signal handler was already installed for signal %d (%p). Restore it.", sig,
135 (old_action.sa_flags & SA_SIGINFO) ? (void*)old_action.sa_sigaction : (void*)old_action.sa_handler);
136 sigaction(sig, &old_action, nullptr);
143 /********************************* SIMIX **************************************/
149 } // namespace simgrid
151 static simgrid::kernel::actor::ActorCode maestro_code;
152 void SIMIX_set_maestro(void (*code)(void*), void* data)
155 XBT_INFO("WARNING, SIMIX_set_maestro is believed to not work on windows. Please help us investigating this issue if "
156 "you need that feature");
158 maestro_code = std::bind(code, data);
161 void SIMIX_global_init(int* argc, char** argv)
163 if (simix_global != nullptr)
166 simix_global = std::make_unique<simgrid::simix::Global>();
169 // The communication initialization is done ASAP, as we need to get some init parameters from the MC for different
170 // layers. But simix_global needs to be created, as we send the address of some of its fields to the MC that wants to
171 // read them directly.
172 simgrid::mc::AppSide::initialize();
175 surf_init(argc, argv); /* Initialize SURF structures */
177 simix_global->maestro_ = nullptr;
178 SIMIX_context_mod_init();
180 // Either create a new context with maestro or create
181 // a context object with the current context maestro):
182 simgrid::kernel::actor::create_maestro(maestro_code);
184 /* Prepare to display some more info when dying on Ctrl-C pressing */
185 std::signal(SIGINT, inthandler);
188 install_segvhandler();
190 /* register a function to be called by SURF after the environment creation */
192 simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
194 if (simgrid::config::get_value<bool>("debug/clean-atexit"))
200 * @brief Clean the SIMIX simulation
202 * This functions remove the memory used by SIMIX
206 static bool smx_cleaned = false;
208 return; // to avoid double cleaning by java and C
211 XBT_DEBUG("SIMIX_clean called. Simulation's over.");
212 auto* engine = simgrid::kernel::EngineImpl::get_instance();
213 if (engine->has_actors_to_run() && SIMIX_get_clock() <= 0.0) {
215 XBT_CRITICAL("The time is still 0, and you still have processes ready to run.");
216 XBT_CRITICAL("It seems that you forgot to run the simulation that you setup.");
217 xbt_die("Bailing out to avoid that stop-before-start madness. Please fix your code.");
221 if (not engine->get_actor_list().empty()) {
222 if (smpi_process()->initialized()) {
223 xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
225 XBT_WARN("Process called exit when leaving - Skipping cleanups");
231 /* Kill all processes (but maestro) */
232 simix_global->maestro_->kill_all();
233 engine->run_all_actors();
234 engine->empty_trash();
236 /* Exit the SIMIX network module */
237 SIMIX_mailbox_exit();
240 /* Let's free maestro now */
241 delete simix_global->maestro_;
242 simix_global->maestro_ = nullptr;
244 /* Finish context module and SURF */
245 SIMIX_context_mod_exit();
249 simix_global = nullptr;
254 * @brief A clock (in second).
256 * @return Return the clock.
258 double SIMIX_get_clock()
260 if (MC_is_active() || MC_record_replay_is_active()) {
261 return MC_process_clock_get(SIMIX_process_self());
263 return surf_get_clock();
267 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
269 simgrid::kernel::EngineImpl::get_instance()->run();
272 double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
274 return simgrid::kernel::timer::Timer::next();
277 smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
279 return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
282 /** @brief cancels a timer that was added earlier */
283 void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
288 /** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
289 double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
291 return timer ? timer->get_date() : 0.0;
294 void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
296 simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
299 int SIMIX_is_maestro()
301 if (simix_global == nullptr) // SimDag
303 const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
304 return self == nullptr || self == simix_global->maestro_;