Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make simix_global->maestro_ private
[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/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"
18
19 #include "simgrid/kernel/resource/Model.hpp"
20
21 #if SIMGRID_HAVE_MC
22 #include "src/mc/remote/AppSide.hpp"
23 #endif
24
25 #include <memory>
26
27 XBT_LOG_NEW_CATEGORY(simix, "All SIMIX categories");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix, "Logging specific to SIMIX (kernel)");
29
30 std::unique_ptr<simgrid::simix::Global> simix_global;
31
32 void (*SMPI_switch_data_segment)(simgrid::s4u::ActorPtr) = nullptr;
33
34 namespace simgrid {
35 namespace simix {
36 config::Flag<bool> cfg_verbose_exit{"debug/verbose-exit", "Display the actor status at exit", true};
37
38 xbt_dynar_t simix_global_get_actors_addr()
39 {
40 #if SIMGRID_HAVE_MC
41   return kernel::EngineImpl::get_instance()->get_actors_vector();
42 #else
43   xbt_die("This function is intended to be used when compiling with MC");
44 #endif
45 }
46 xbt_dynar_t simix_global_get_dead_actors_addr()
47 {
48 #if SIMGRID_HAVE_MC
49   return kernel::EngineImpl::get_instance()->get_dead_actors_vector();
50 #else
51   xbt_die("This function is intended to be used when compiling with MC");
52 #endif
53 }
54
55 } // namespace simix
56 } // namespace simgrid
57
58 XBT_ATTRIB_NORETURN static void inthandler(int)
59 {
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();
64   } else {
65     XBT_INFO("CTRL-C pressed, exiting. Hiding the current process status since 'debug/verbose-exit' is set to false.");
66   }
67   exit(1);
68 }
69
70 #ifndef _WIN32
71 static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
72 {
73   if ((siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_ACCERR) || siginfo->si_signo == SIGBUS) {
74     fprintf(stderr,
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"
79             "\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"
83             "\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");
90 #if HAVE_SMPI
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");
94 #else
95       fprintf(stderr, "Sadly, your system does not support --cfg=smpi/privatization:yes (yet).\n");
96 #endif /* HAVE_PRIVATIZATION */
97     }
98 #endif /* HAVE_SMPI */
99   }
100   std::raise(signum);
101 }
102
103 /**
104  * Install signal handler for SIGSEGV.  Check that nobody has already installed
105  * its own handler.  For example, the Java VM does this.
106  */
107 static void install_segvhandler()
108 {
109   stack_t old_stack;
110
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));
113     return;
114   }
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);
119   }
120
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);
126
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));
131       continue;
132     }
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);
137     }
138   }
139 }
140
141 #endif /* _WIN32 */
142
143 static simgrid::kernel::actor::ActorCode maestro_code;
144 void SIMIX_set_maestro(void (*code)(void*), void* data)
145 {
146 #ifdef _WIN32
147   XBT_INFO("WARNING, SIMIX_set_maestro is believed to not work on windows. Please help us investigating this issue if "
148            "you need that feature");
149 #endif
150   maestro_code = std::bind(code, data);
151 }
152
153 void SIMIX_global_init(int* argc, char** argv)
154 {
155   if (simix_global != nullptr)
156     return;
157
158   simix_global = std::make_unique<simgrid::simix::Global>();
159
160 #if SIMGRID_HAVE_MC
161   // The communication initialization is done ASAP, as we need to get some init parameters from the MC for different
162   // layers. But simix_global needs to be created, as we send the address of some of its fields to the MC that wants to
163   // read them directly.
164   simgrid::mc::AppSide::initialize();
165 #endif
166
167   surf_init(argc, argv); /* Initialize SURF structures */
168
169   SIMIX_context_mod_init();
170
171   // Either create a new context with maestro or create
172   // a context object with the current context maestro):
173   simgrid::kernel::actor::create_maestro(maestro_code);
174
175   /* Prepare to display some more info when dying on Ctrl-C pressing */
176   std::signal(SIGINT, inthandler);
177
178 #ifndef _WIN32
179   install_segvhandler();
180 #endif
181   /* register a function to be called by SURF after the environment creation */
182   sg_platf_init();
183   simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
184
185   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
186     atexit(SIMIX_clean);
187 }
188
189 /**
190  * @ingroup SIMIX_API
191  * @brief Clean the SIMIX simulation
192  *
193  * This functions remove the memory used by SIMIX
194  */
195 void SIMIX_clean()
196 {
197   static bool smx_cleaned = false;
198   if (smx_cleaned)
199     return; // to avoid double cleaning by java and C
200
201   smx_cleaned = true;
202   XBT_DEBUG("SIMIX_clean called. Simulation's over.");
203   auto* engine = simgrid::kernel::EngineImpl::get_instance();
204   if (engine->has_actors_to_run() && SIMIX_get_clock() <= 0.0) {
205     XBT_CRITICAL("   ");
206     XBT_CRITICAL("The time is still 0, and you still have processes ready to run.");
207     XBT_CRITICAL("It seems that you forgot to run the simulation that you setup.");
208     xbt_die("Bailing out to avoid that stop-before-start madness. Please fix your code.");
209   }
210
211 #if HAVE_SMPI
212   if (not engine->get_actor_list().empty()) {
213     if (smpi_process()->initialized()) {
214       xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
215     } else {
216       XBT_WARN("Process called exit when leaving - Skipping cleanups");
217       return;
218     }
219   }
220 #endif
221
222   /* Kill all processes (but maestro) */
223   simix_global->get_maestro()->kill_all();
224   engine->run_all_actors();
225   engine->empty_trash();
226
227   /* Let's free maestro now */
228   simix_global->destroy_maestro();
229
230   /* Finish context module and SURF */
231   simix_global->destroy_context_factory();
232
233   surf_exit();
234
235   simix_global = nullptr;
236 }
237
238 /**
239  * @ingroup SIMIX_API
240  * @brief A clock (in second).
241  *
242  * @return Return the clock.
243  */
244 double SIMIX_get_clock()
245 {
246   if (MC_is_active() || MC_record_replay_is_active()) {
247     return MC_process_clock_get(SIMIX_process_self());
248   } else {
249     return surf_get_clock();
250   }
251 }
252
253 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
254 {
255   simgrid::kernel::EngineImpl::get_instance()->run();
256 }
257
258 double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
259 {
260   return simgrid::kernel::timer::Timer::next();
261 }
262
263 smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
264 {
265   return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
266 }
267
268 /** @brief cancels a timer that was added earlier */
269 void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
270 {
271   timer->remove();
272 }
273
274 /** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
275 double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
276 {
277   return timer ? timer->get_date() : 0.0;
278 }
279
280 void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
281 {
282   simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
283 }
284
285 int SIMIX_is_maestro()
286 {
287   if (simix_global == nullptr) // SimDag
288     return true;
289   simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
290   return self == nullptr || simix_global->is_maestro(self);
291 }