Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7adea1a9aa5f464bef68e185a8800f82f4583a10
[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 /********************************* SIMIX **************************************/
144 namespace simgrid {
145 namespace simix {
146
147
148 } // namespace simix
149 } // namespace simgrid
150
151 static simgrid::kernel::actor::ActorCode maestro_code;
152 void SIMIX_set_maestro(void (*code)(void*), void* data)
153 {
154 #ifdef _WIN32
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");
157 #endif
158   maestro_code = std::bind(code, data);
159 }
160
161 void SIMIX_global_init(int* argc, char** argv)
162 {
163   if (simix_global != nullptr)
164     return;
165
166   simix_global = std::make_unique<simgrid::simix::Global>();
167
168 #if SIMGRID_HAVE_MC
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();
173 #endif
174
175   surf_init(argc, argv); /* Initialize SURF structures */
176
177   simix_global->maestro_ = nullptr;
178   SIMIX_context_mod_init();
179
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);
183
184   /* Prepare to display some more info when dying on Ctrl-C pressing */
185   std::signal(SIGINT, inthandler);
186
187 #ifndef _WIN32
188   install_segvhandler();
189 #endif
190   /* register a function to be called by SURF after the environment creation */
191   sg_platf_init();
192   simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
193
194   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
195     atexit(SIMIX_clean);
196 }
197
198 /**
199  * @ingroup SIMIX_API
200  * @brief Clean the SIMIX simulation
201  *
202  * This functions remove the memory used by SIMIX
203  */
204 void SIMIX_clean()
205 {
206   static bool smx_cleaned = false;
207   if (smx_cleaned)
208     return; // to avoid double cleaning by java and C
209
210   smx_cleaned = true;
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) {
214     XBT_CRITICAL("   ");
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.");
218   }
219
220 #if HAVE_SMPI
221   if (not engine->get_actor_list().empty()) {
222     if (smpi_process()->initialized()) {
223       xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
224     } else {
225       XBT_WARN("Process called exit when leaving - Skipping cleanups");
226       return;
227     }
228   }
229 #endif
230
231   /* Kill all processes (but maestro) */
232   simix_global->maestro_->kill_all();
233   engine->run_all_actors();
234   engine->empty_trash();
235
236   /* Let's free maestro now */
237   delete simix_global->maestro_;
238   simix_global->maestro_ = nullptr;
239
240   /* Finish context module and SURF */
241   SIMIX_context_mod_exit();
242
243   surf_exit();
244
245   simix_global = nullptr;
246 }
247
248 /**
249  * @ingroup SIMIX_API
250  * @brief A clock (in second).
251  *
252  * @return Return the clock.
253  */
254 double SIMIX_get_clock()
255 {
256   if (MC_is_active() || MC_record_replay_is_active()) {
257     return MC_process_clock_get(SIMIX_process_self());
258   } else {
259     return surf_get_clock();
260   }
261 }
262
263 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
264 {
265   simgrid::kernel::EngineImpl::get_instance()->run();
266 }
267
268 double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
269 {
270   return simgrid::kernel::timer::Timer::next();
271 }
272
273 smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
274 {
275   return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
276 }
277
278 /** @brief cancels a timer that was added earlier */
279 void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
280 {
281   timer->remove();
282 }
283
284 /** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
285 double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
286 {
287   return timer ? timer->get_date() : 0.0;
288 }
289
290 void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
291 {
292   simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
293 }
294
295 int SIMIX_is_maestro()
296 {
297   if (simix_global == nullptr) // SimDag
298     return true;
299   const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
300   return self == nullptr || self == simix_global->maestro_;
301 }