Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add missing copyright notices.
[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 namespace simgrid {
33 namespace simix {
34 config::Flag<bool> cfg_verbose_exit{"debug/verbose-exit", "Display the actor status at exit", true};
35
36 xbt_dynar_t simix_global_get_actors_addr()
37 {
38 #if SIMGRID_HAVE_MC
39   return kernel::EngineImpl::get_instance()->get_actors_vector();
40 #else
41   xbt_die("This function is intended to be used when compiling with MC");
42 #endif
43 }
44 xbt_dynar_t simix_global_get_dead_actors_addr()
45 {
46 #if SIMGRID_HAVE_MC
47   return kernel::EngineImpl::get_instance()->get_dead_actors_vector();
48 #else
49   xbt_die("This function is intended to be used when compiling with MC");
50 #endif
51 }
52
53 } // namespace simix
54 } // namespace simgrid
55
56 XBT_ATTRIB_NORETURN static void inthandler(int)
57 {
58   if (simgrid::simix::cfg_verbose_exit) {
59     XBT_INFO("CTRL-C pressed. The current status will be displayed before exit (disable that behavior with option "
60              "'debug/verbose-exit').");
61     simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
62   } else {
63     XBT_INFO("CTRL-C pressed, exiting. Hiding the current process status since 'debug/verbose-exit' is set to false.");
64   }
65   exit(1);
66 }
67
68 #ifndef _WIN32
69 static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
70 {
71   if ((siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_ACCERR) || siginfo->si_signo == SIGBUS) {
72     fprintf(stderr,
73             "Access violation or Bus error detected.\n"
74             "This probably comes from a programming error in your code, or from a stack\n"
75             "overflow. If you are certain of your code, try increasing the stack size\n"
76             "   --cfg=contexts/stack-size=XXX (current size is %u KiB).\n"
77             "\n"
78             "If it does not help, this may have one of the following causes:\n"
79             "a bug in SimGrid, a bug in the OS or a bug in a third-party libraries.\n"
80             "Failing hardware can sometimes generate such errors too.\n"
81             "\n"
82             "If you think you've found a bug in SimGrid, please report it along with a\n"
83             "Minimal Working Example (MWE) reproducing your problem and a full backtrace\n"
84             "of the fault captured with gdb or valgrind.\n",
85             smx_context_stack_size / 1024);
86   } else if (siginfo->si_signo == SIGSEGV) {
87     fprintf(stderr, "Segmentation fault.\n");
88 #if HAVE_SMPI
89     if (smpi_enabled() && smpi_cfg_privatization() == SmpiPrivStrategies::NONE) {
90 #if HAVE_PRIVATIZATION
91       fprintf(stderr, "Try to enable SMPI variable privatization with --cfg=smpi/privatization:yes.\n");
92 #else
93       fprintf(stderr, "Sadly, your system does not support --cfg=smpi/privatization:yes (yet).\n");
94 #endif /* HAVE_PRIVATIZATION */
95     }
96 #endif /* HAVE_SMPI */
97   }
98   std::raise(signum);
99 }
100
101 /**
102  * Install signal handler for SIGSEGV.  Check that nobody has already installed
103  * its own handler.  For example, the Java VM does this.
104  */
105 static void install_segvhandler()
106 {
107   stack_t old_stack;
108
109   if (simgrid::kernel::context::Context::install_sigsegv_stack(&old_stack, true) == -1) {
110     XBT_WARN("Failed to register alternate signal stack: %s", strerror(errno));
111     return;
112   }
113   if (not(old_stack.ss_flags & SS_DISABLE)) {
114     XBT_DEBUG("An alternate stack was already installed (sp=%p, size=%zu, flags=%x). Restore it.", old_stack.ss_sp,
115               old_stack.ss_size, (unsigned)old_stack.ss_flags);
116     sigaltstack(&old_stack, nullptr);
117   }
118
119   struct sigaction action;
120   struct sigaction old_action;
121   action.sa_sigaction = &segvhandler;
122   action.sa_flags     = SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
123   sigemptyset(&action.sa_mask);
124
125   /* Linux tend to raise only SIGSEGV where other systems also raise SIGBUS on severe error */
126   for (int sig : {SIGSEGV, SIGBUS}) {
127     if (sigaction(sig, &action, &old_action) == -1) {
128       XBT_WARN("Failed to register signal handler for signal %d: %s", sig, strerror(errno));
129       continue;
130     }
131     if ((old_action.sa_flags & SA_SIGINFO) || old_action.sa_handler != SIG_DFL) {
132       XBT_DEBUG("A signal handler was already installed for signal %d (%p). Restore it.", sig,
133                 (old_action.sa_flags & SA_SIGINFO) ? (void*)old_action.sa_sigaction : (void*)old_action.sa_handler);
134       sigaction(sig, &old_action, nullptr);
135     }
136   }
137 }
138
139 #endif /* _WIN32 */
140
141 static simgrid::kernel::actor::ActorCode maestro_code;
142 void SIMIX_set_maestro(void (*code)(void*), void* data)
143 {
144 #ifdef _WIN32
145   XBT_INFO("WARNING, SIMIX_set_maestro is believed to not work on windows. Please help us investigating this issue if "
146            "you need that feature");
147 #endif
148   maestro_code = std::bind(code, data);
149 }
150
151 void SIMIX_global_init(int* argc, char** argv)
152 {
153   if (simix_global != nullptr)
154     return;
155
156   simix_global = std::make_unique<simgrid::simix::Global>();
157
158 #if SIMGRID_HAVE_MC
159   // The communication initialization is done ASAP, as we need to get some init parameters from the MC for different
160   // layers. But simix_global needs to be created, as we send the address of some of its fields to the MC that wants to
161   // read them directly.
162   simgrid::mc::AppSide::initialize();
163 #endif
164
165   surf_init(argc, argv); /* Initialize SURF structures */
166
167   SIMIX_context_mod_init();
168
169   // Either create a new context with maestro or create
170   // a context object with the current context maestro):
171   simgrid::kernel::actor::create_maestro(maestro_code);
172
173   /* Prepare to display some more info when dying on Ctrl-C pressing */
174   std::signal(SIGINT, inthandler);
175
176 #ifndef _WIN32
177   install_segvhandler();
178 #endif
179   /* register a function to be called by SURF after the environment creation */
180   sg_platf_init();
181   simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
182
183   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
184     atexit(SIMIX_clean);
185 }
186
187 /**
188  * @ingroup SIMIX_API
189  * @brief Clean the SIMIX simulation
190  *
191  * This functions remove the memory used by SIMIX
192  */
193 void SIMIX_clean()
194 {
195   static bool smx_cleaned = false;
196   if (smx_cleaned)
197     return; // to avoid double cleaning by java and C
198
199   smx_cleaned = true;
200   XBT_DEBUG("SIMIX_clean called. Simulation's over.");
201   auto* engine = simgrid::kernel::EngineImpl::get_instance();
202   if (engine->has_actors_to_run() && simgrid::s4u::Engine::get_clock() <= 0.0) {
203     XBT_CRITICAL("   ");
204     XBT_CRITICAL("The time is still 0, and you still have processes ready to run.");
205     XBT_CRITICAL("It seems that you forgot to run the simulation that you setup.");
206     xbt_die("Bailing out to avoid that stop-before-start madness. Please fix your code.");
207   }
208
209 #if HAVE_SMPI
210   if (not engine->get_actor_list().empty()) {
211     if (smpi_process()->initialized()) {
212       xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
213     } else {
214       XBT_WARN("Process called exit when leaving - Skipping cleanups");
215       return;
216     }
217   }
218 #endif
219
220   /* Kill all processes (but maestro) */
221   simix_global->get_maestro()->kill_all();
222   engine->run_all_actors();
223   engine->empty_trash();
224
225   /* Let's free maestro now */
226   simix_global->destroy_maestro();
227
228   /* Finish context module and SURF */
229   simix_global->destroy_context_factory();
230
231   surf_exit();
232
233   simix_global = nullptr;
234 }
235
236 /**
237  * @ingroup SIMIX_API
238  * @brief A clock (in second).
239  *
240  * @return Return the clock.
241  */
242 double SIMIX_get_clock() // XBT_ATTRIB_DEPRECATED_v332
243 {
244   return simgrid::s4u::Engine::get_clock();
245 }
246
247 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
248 {
249   simgrid::kernel::EngineImpl::get_instance()->run();
250 }
251
252 double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
253 {
254   return simgrid::kernel::timer::Timer::next();
255 }
256
257 smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
258 {
259   return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
260 }
261
262 /** @brief cancels a timer that was added earlier */
263 void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
264 {
265   timer->remove();
266 }
267
268 /** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
269 double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
270 {
271   return timer ? timer->get_date() : 0.0;
272 }
273
274 void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
275 {
276   simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
277 }
278
279 int SIMIX_is_maestro()
280 {
281   if (simix_global == nullptr) // SimDag
282     return true;
283   const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
284   return self == nullptr || simix_global->is_maestro(self);
285 }