Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into 'python_bindings_host_load_plugin'
[simgrid.git] / src / simgrid / sg_config.cpp
1 /* Copyright (c) 2009-2023. 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 /* sg_config: configuration infrastructure for the simulation world         */
7
8 #include <simgrid/instr.h>
9 #include <simgrid/version.h>
10 #include <xbt/config.hpp>
11 #include <xbt/file.hpp>
12
13 #include "src/instr/instr_private.hpp"
14 #include "src/internal_config.h"
15 #include "src/kernel/context/Context.hpp"
16 #include "src/kernel/lmm/maxmin.hpp"
17 #include "src/kernel/resource/NetworkModel.hpp"
18 #include "src/mc/mc_config.hpp"
19 #include "src/mc/mc_replay.hpp"
20 #include "src/simgrid/module.hpp"
21 #include "src/simgrid/sg_config.hpp"
22 #include "src/smpi/include/smpi_config.hpp"
23 #include "src/xbt/mmalloc/mmalloc.h"
24
25 #include <string_view>
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config, kernel, "About the configuration of SimGrid");
28
29 static simgrid::config::Flag<bool> cfg_continue_after_help
30   {"help-nostop", "Do not stop the execution when --help is found", false};
31
32 /** @brief Allow other libraries to react to the --help flag, too
33  *
34  * When finding --help on the command line, SimGrid usually stops right after displaying its help message.
35  * If you are writing a library using SimGrid, you may want to display your own help message before everything stops.
36  * If so, just call this function before having SimGrid parsing the command line, and you will be given the control
37  * even if the user is asking for help.
38  */
39 void sg_config_continue_after_help()
40 {
41   cfg_continue_after_help = true;
42 }
43
44 /* 0: beginning of time (config cannot be changed yet)
45  * 1: initialized: cfg_set created (config can now be changed)
46  * 2: configured: command line parsed and config part of platform file was
47  *    integrated also, platform construction ongoing or done.
48  *    (Config cannot be changed anymore!)
49  */
50 int _sg_cfg_init_status = 0;
51
52 /* Parse the command line, looking for options */
53 static void sg_config_cmd_line(int *argc, char **argv)
54 {
55   bool shall_exit = false;
56   bool parse_args = true; // Stop parsing the parameters once we found '--'
57
58   int j = 1;
59   for (int i = j; i < *argc; i++) {
60     if (not strcmp("--", argv[i])) {
61       parse_args = false;
62       // Remove that '--' from the arguments
63     } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
64       char *opt = strchr(argv[i], '=');
65       opt++;
66
67       simgrid::config::set_parse(opt);
68       XBT_DEBUG("Did apply '%s' as config setting", opt);
69     } else if (parse_args && not strcmp(argv[i], "--version")) {
70       sg_version();
71       shall_exit = true;
72     } else if (parse_args && (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help"))) {
73       XBT_HELP("Description of the configuration accepted by this simulator:");
74       simgrid::config::help();
75       XBT_HELP("\n"
76                "Each of these configurations can be used by adding\n"
77                "    --cfg=<option name>:<option value>\n"
78                "to the command line. Try passing \"help\" as a value\n"
79                "to get the list of values accepted by a given option.\n"
80                "For example, \"--cfg=plugin:help\" gives you the list of\n"
81                "plugins available in your installation of SimGrid.\n"
82                "\n"
83                "For more information, please refer to:\n"
84                "   --help-aliases for the list of all option aliases.\n"
85                "   --help-logs and --help-log-categories for the details of logging output.\n"
86                "   --help-models for a list of all models known by this simulator.\n"
87                "   --help-tracing for the details of all tracing options known by this simulator.\n"
88                "   --version to get SimGrid version information.\n");
89       shall_exit = not cfg_continue_after_help;
90       argv[j++]  = argv[i]; // Preserve the --help in argv just in case someone else wants to see it
91     } else if (parse_args && not strcmp(argv[i], "--help-aliases")) {
92       XBT_HELP("Here is a list of all deprecated option names, with their replacement.");
93       simgrid::config::show_aliases();
94       XBT_HELP("Please consider using the recent names");
95       shall_exit = true;
96     } else if (parse_args && not strcmp(argv[i], "--help-models")) {
97       simgrid_host_models().help();
98       XBT_HELP("%s", "");
99       simgrid_cpu_models().help();
100       XBT_HELP("%s", "");
101       simgrid_network_models().help();
102       shall_exit = true;
103     } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
104       TRACE_help();
105       shall_exit = true;
106     } else {
107       argv[j++] = argv[i];
108     }
109   }
110   if (j < *argc) {
111     argv[j] = nullptr;
112     *argc = j;
113   }
114   if (shall_exit)
115     exit(0);
116 }
117
118 static void _sg_cfg_cb_contexts_parallel_mode(std::string_view mode_name)
119 {
120   if (mode_name == "posix") {
121     simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_POSIX;
122   } else if (mode_name == "futex") {
123     simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_FUTEX;
124   } else if (mode_name == "busy_wait") {
125     simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_BUSY_WAIT;
126   } else {
127     xbt_die("Command line setting of the parallel synchronization mode should "
128             "be one of \"posix\", \"futex\" or \"busy_wait\"");
129   }
130 }
131
132 /* create the config set, register what should be and parse the command line*/
133 void sg_config_init(int *argc, char **argv)
134 {
135   /* Create the configuration support */
136   if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */
137     XBT_WARN("Call to sg_config_init() after initialization ignored");
138     return;
139   }
140   _sg_cfg_init_status = 1;
141
142   /* Plugins and models configuration */
143   simgrid_plugins().create_flag("plugin", "The plugins", "", true);
144   simgrid_cpu_models().create_flag("cpu/model", "The model to use for the CPU", "Cas01", false);
145   simgrid_network_models().create_flag("network/model", "The model to use for the network", "LV08", false);
146   simgrid_host_models().create_flag("host/model", "The model to use for the host", "default", false);
147   simgrid_disk_models().create_flag("disk/model", "The model to use for the disk", "S19", false);
148
149   simgrid::config::bind_flag(sg_precision_timing, "precision/timing", {"surf/precision"},
150                              "Numerical precision used when updating simulation times (in seconds)");
151
152   simgrid::config::bind_flag(sg_precision_workamount, "precision/work-amount", {"maxmin/precision"},
153                              "Numerical precision used when computing resource sharing (in flops/sec or bytes/sec)");
154
155   simgrid::config::bind_flag(sg_concurrency_limit, "maxmin/concurrency-limit",
156                              "Maximum number of concurrent variables in the maxmim system. Also limits the number of "
157                              "processes on each host, at higher level. (default: -1 means no such limitation)");
158
159   /* The parameters of network models */
160   static simgrid::config::Flag<double> _sg_network_loopback_latency{
161       "network/loopback-lat",
162       "For network models with an implicit loopback link (L07, CM02, LV08), "
163       "latency of the loopback link. 0 by default",
164       0.0};
165
166   static simgrid::config::Flag<double> _sg_network_loopback_bandwidth{
167       "network/loopback-bw",
168       "For network models with an implicit loopback link (L07, CM02, LV08), "
169       "bandwidth of the loopback link. 10GBps by default",
170       10e9};
171
172   /* Inclusion path */
173   static simgrid::config::Flag<std::string> cfg_path{
174       "path", "Lookup path for inclusions in platform and deployment XML files", "./", [](std::string const& path) {
175         if (not path.empty())
176           simgrid::xbt::path_push(path);
177       }};
178
179   static simgrid::config::Flag<bool> cfg_cpu_maxmin_selective_update{
180       "cpu/maxmin-selective-update",
181       "Update the constraint set propagating recursively to others constraints "
182       "(off by default unless optim is set to lazy)",
183       false};
184   static simgrid::config::Flag<bool> cfg_network_maxmin_selective_update{"network/maxmin-selective-update",
185                                                                          "Update the constraint set propagating "
186                                                                          "recursively to others constraints (off by "
187                                                                          "default unless optim is set to lazy)",
188                                                                          false};
189
190   static simgrid::config::Flag<int> cfg_context_stack_size{
191       "contexts/stack-size", "Stack size of contexts in KiB (not with threads)", 8 * 1024,
192       [](int value) { simgrid::kernel::context::Context::stack_size = value * 1024; }};
193
194   /* guard size for contexts stacks in memory pages */
195 #if (PTH_STACKGROWTH != -1)
196   int default_guard_size = 0;
197 #else
198   int default_guard_size = 1;
199 #endif
200   static simgrid::config::Flag<int> cfg_context_guard_size{
201       "contexts/guard-size", "Guard size for contexts stacks in memory pages", default_guard_size,
202       [](int value) { simgrid::kernel::context::Context::guard_size = value * xbt_pagesize; }};
203
204   static simgrid::config::Flag<int> cfg_context_nthreads{
205       "contexts/nthreads", "Number of parallel threads used to execute user contexts", 1, [](int nthreads) {
206 #if HAVE_MMALLOC
207         xbt_assert(
208             nthreads == 1 || not malloc_use_mmalloc(),
209             "Parallel simulation is forbidden in the verified program, as there is no protection against race "
210             "conditions in mmalloc itself. Please don't be so greedy and show some mercy for our implementation.");
211 #endif
212         simgrid::kernel::context::Context::set_nthreads(nthreads);
213       }};
214
215   /* synchronization mode for parallel user contexts */
216 #if HAVE_FUTEX_H
217   std::string default_synchro_mode = "futex";
218 #else // No futex on mac and posix is unimplemented yet
219   std::string default_synchro_mode = "busy_wait";
220 #endif
221   static simgrid::config::Flag<std::string> cfg_context_synchro{"contexts/synchro",
222                                                                 "Synchronization mode to use when running contexts in "
223                                                                 "parallel (either futex, posix or busy_wait)",
224                                                                 default_synchro_mode,
225                                                                 &_sg_cfg_cb_contexts_parallel_mode};
226
227   // SMPI model can be used without enable_smpi, so keep this out of the ifdef.
228   static simgrid::config::Flag<std::string> cfg_smpi_IB_penalty_factors{
229       "smpi/IB-penalty-factors",
230       "Correction factor to communications using Infiniband model with "
231       "contention (default value based on Stampede cluster profiling)",
232       "0.965;0.925;1.35"};
233   /* Others */
234
235   static simgrid::config::Flag<bool> cfg_execution_cutpath{
236       "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false};
237
238   sg_config_cmd_line(argc, argv);
239
240   xbt_mallocator_initialization_is_done(simgrid::kernel::context::Context::is_parallel());
241 }
242
243 void sg_config_finalize()
244 {
245   simgrid::config::finalize();
246   _sg_cfg_init_status = 0;
247 }