Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove cruft [ci-skip]
[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_global->maestro_ = nullptr;
170   SIMIX_context_mod_init();
171
172   // Either create a new context with maestro or create
173   // a context object with the current context maestro):
174   simgrid::kernel::actor::create_maestro(maestro_code);
175
176   /* Prepare to display some more info when dying on Ctrl-C pressing */
177   std::signal(SIGINT, inthandler);
178
179 #ifndef _WIN32
180   install_segvhandler();
181 #endif
182   /* register a function to be called by SURF after the environment creation */
183   sg_platf_init();
184   simgrid::s4u::Engine::on_platform_created.connect(surf_presolve);
185
186   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
187     atexit(SIMIX_clean);
188 }
189
190 /**
191  * @ingroup SIMIX_API
192  * @brief Clean the SIMIX simulation
193  *
194  * This functions remove the memory used by SIMIX
195  */
196 void SIMIX_clean()
197 {
198   static bool smx_cleaned = false;
199   if (smx_cleaned)
200     return; // to avoid double cleaning by java and C
201
202   smx_cleaned = true;
203   XBT_DEBUG("SIMIX_clean called. Simulation's over.");
204   auto* engine = simgrid::kernel::EngineImpl::get_instance();
205   if (engine->has_actors_to_run() && SIMIX_get_clock() <= 0.0) {
206     XBT_CRITICAL("   ");
207     XBT_CRITICAL("The time is still 0, and you still have processes ready to run.");
208     XBT_CRITICAL("It seems that you forgot to run the simulation that you setup.");
209     xbt_die("Bailing out to avoid that stop-before-start madness. Please fix your code.");
210   }
211
212 #if HAVE_SMPI
213   if (not engine->get_actor_list().empty()) {
214     if (smpi_process()->initialized()) {
215       xbt_die("Process exited without calling MPI_Finalize - Killing simulation");
216     } else {
217       XBT_WARN("Process called exit when leaving - Skipping cleanups");
218       return;
219     }
220   }
221 #endif
222
223   /* Kill all processes (but maestro) */
224   simix_global->maestro_->kill_all();
225   engine->run_all_actors();
226   engine->empty_trash();
227
228   /* Let's free maestro now */
229   delete simix_global->maestro_;
230   simix_global->maestro_ = nullptr;
231
232   /* Finish context module and SURF */
233   SIMIX_context_mod_exit();
234
235   surf_exit();
236
237   simix_global = nullptr;
238 }
239
240 /**
241  * @ingroup SIMIX_API
242  * @brief A clock (in second).
243  *
244  * @return Return the clock.
245  */
246 double SIMIX_get_clock()
247 {
248   if (MC_is_active() || MC_record_replay_is_active()) {
249     return MC_process_clock_get(SIMIX_process_self());
250   } else {
251     return surf_get_clock();
252   }
253 }
254
255 void SIMIX_run() // XBT_ATTRIB_DEPRECATED_v332
256 {
257   simgrid::kernel::EngineImpl::get_instance()->run();
258 }
259
260 double SIMIX_timer_next() // XBT_ATTRIB_DEPRECATED_v329
261 {
262   return simgrid::kernel::timer::Timer::next();
263 }
264
265 smx_timer_t SIMIX_timer_set(double date, void (*callback)(void*), void* arg) // XBT_ATTRIB_DEPRECATED_v329
266 {
267   return simgrid::kernel::timer::Timer::set(date, std::bind(callback, arg));
268 }
269
270 /** @brief cancels a timer that was added earlier */
271 void SIMIX_timer_remove(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
272 {
273   timer->remove();
274 }
275
276 /** @brief Returns the date at which the timer will trigger (or 0 if nullptr timer) */
277 double SIMIX_timer_get_date(smx_timer_t timer) // XBT_ATTRIB_DEPRECATED_v329
278 {
279   return timer ? timer->get_date() : 0.0;
280 }
281
282 void SIMIX_display_process_status() // XBT_ATTRIB_DEPRECATED_v329
283 {
284   simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
285 }
286
287 int SIMIX_is_maestro()
288 {
289   if (simix_global == nullptr) // SimDag
290     return true;
291   const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
292   return self == nullptr || self == simix_global->maestro_;
293 }