Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adding documentation
[simgrid.git] / src / xbt / xbt_main.cpp
1 /* module handling                                                          */
2
3 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #define XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL /* MSVC don't want it to be declared extern in headers and local here */
9
10 #include "simgrid/config.h"
11 #include "simgrid/sg_config.hpp"
12 #include "src/internal_config.h"
13 #include "src/sthread/sthread.h" // sthread_inside_simgrid
14 #include "xbt/config.hpp"
15 #include "xbt/coverage.h"
16 #include "xbt/dynar.h"
17 #include "xbt/log.h"
18 #include "xbt/log.hpp"
19 #include "xbt/misc.h"
20 #include "xbt/module.h" /* this module */
21 #include "xbt/sysdep.h"
22 #include "xbt/xbt_modinter.h" /* prototype of other module's init/exit in XBT */
23
24 #include <cmath>
25 #include <cstdio>
26 #if HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string>
30 #include <vector>
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(module, xbt, "module handling");
33
34 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories"); /* lives here even if that's a bit odd to solve linking issues: this is used in xbt_log_file_appender to detect whether SMPI is used (and thus whether we should unbench the writing to disk) */
35
36 namespace simgrid::xbt {
37 std::string binary_name;          /* Name of the system process containing us (mandatory to retrieve neat backtraces) */
38 std::vector<std::string> cmdline; /* all we got in argv */
39 } // namespace simgrid::xbt
40
41
42 int xbt_initialized = 0;
43 simgrid::config::Flag<bool> cfg_dbg_clean_atexit{
44     "debug/clean-atexit",
45     "Whether to cleanup SimGrid at exit. Disable it if your code segfaults after its end.",
46     true};
47
48 const int xbt_pagesize = static_cast<int>(sysconf(_SC_PAGESIZE));
49 const int xbt_pagebits = static_cast<int>(log2(xbt_pagesize));
50
51 /* Declare xbt_preinit and xbt_postexit as constructor/destructor of the library.
52  * This is crude and rather compiler-specific, unfortunately.
53  */
54 static void xbt_preinit() XBT_ATTRIB_CONSTRUCTOR(200);
55 static void xbt_postexit();
56 XBT_ATTRIB_NOINLINE void sthread_enable()
57 { // These symbols are used from ContextSwapped in any case, but they are only useful
58   asm("");
59 }
60 XBT_ATTRIB_NOINLINE void sthread_disable()
61 { //  when libsthread is LD_PRELOADED. In this case, sthread's implem gets used instead.
62   asm("");
63 }
64
65 static void xbt_preinit()
66 {
67   xbt_log_preinit();
68   xbt_dict_preinit();
69   atexit(xbt_postexit);
70 }
71
72 static void xbt_postexit()
73 {
74   if (not cfg_dbg_clean_atexit)
75     return;
76   xbt_initialized--;
77   xbt_dict_postexit();
78   xbt_log_postexit();
79 }
80
81 /** @brief Initialize the xbt mechanisms. */
82 void xbt_init(int *argc, char **argv)
83 {
84   xbt_initialized++;
85   if (xbt_initialized > 1) {
86     XBT_DEBUG("XBT has been initialized %d times.", xbt_initialized);
87     return;
88   }
89
90   simgrid::xbt::install_exception_handler();
91
92   if (*argc > 0)
93     simgrid::xbt::binary_name = argv[0];
94   for (int i = 0; i < *argc; i++)
95     simgrid::xbt::cmdline.emplace_back(argv[i]);
96
97   xbt_log_init(argc, argv);
98 }
99
100 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
101 /** @brief like xbt_free, but you can be sure that it is a function  */
102 void xbt_free_f(void* p) noexcept(noexcept(::free))
103 {
104   xbt_free(p);
105 }
106
107 /** @brief should be given a pointer to pointer, and frees the second one */
108 void xbt_free_ref(void* d) noexcept(noexcept(::free))
109 {
110   xbt_free(*(void**)d);
111 }
112
113 /** @brief Kill the program in silence */
114 void xbt_abort()
115 {
116   /* Call __gcov_flush on abort when compiling with coverage options. */
117   coverage_checkpoint();
118   abort();
119 }
120
121 #ifndef HAVE_SMPI
122 int SMPI_is_inited()
123 {
124   return false;
125 }
126 #endif