Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stop trying to build on native WIN32, it's broken anyway
[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
12 #include "simgrid/sg_config.hpp"
13 #include "src/include/xbt/mmalloc.h"
14 #include "src/instr/instr_private.hpp"
15 #include "src/internal_config.h"
16 #include "src/kernel/context/Context.hpp"
17 #include "src/kernel/lmm/maxmin.hpp"
18 #include "src/kernel/resource/NetworkModel.hpp"
19 #include "src/mc/mc_config.hpp"
20 #include "src/mc/mc_replay.hpp"
21 #include "src/smpi/include/smpi_config.hpp"
22 #include "src/surf/surf_interface.hpp"
23
24 #include <string_view>
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf, "About the configuration of SimGrid");
27
28 static simgrid::config::Flag<bool> cfg_continue_after_help
29   {"help-nostop", "Do not stop the execution when --help is found", false};
30
31 /** @brief Allow other libraries to react to the --help flag, too
32  *
33  * When finding --help on the command line, simgrid usually stops right after displaying its help message.
34  * If you are writing a library using simgrid, you may want to display your own help message before everything stops.
35  * If so, just call this function before having simgrid parsing the command line, and you will be given the control
36  * even if the user is asking for help.
37  */
38 void sg_config_continue_after_help()
39 {
40   cfg_continue_after_help = true;
41 }
42
43 /* 0: beginning of time (config cannot be changed yet)
44  * 1: initialized: cfg_set created (config can now be changed)
45  * 2: configured: command line parsed and config part of platform file was
46  *    integrated also, platform construction ongoing or done.
47  *    (Config cannot be changed anymore!)
48  */
49 int _sg_cfg_init_status = 0;
50
51 /* Parse the command line, looking for options */
52 static void sg_config_cmd_line(int *argc, char **argv)
53 {
54   bool shall_exit = false;
55   bool parse_args = true; // Stop parsing the parameters once we found '--'
56
57   int j = 1;
58   for (int i = j; i < *argc; i++) {
59     if (not strcmp("--", argv[i])) {
60       parse_args = false;
61       // Remove that '--' from the arguments
62     } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
63       char *opt = strchr(argv[i], '=');
64       opt++;
65
66       simgrid::config::set_parse(opt);
67       XBT_DEBUG("Did apply '%s' as config setting", opt);
68     } else if (parse_args && not strcmp(argv[i], "--version")) {
69       sg_version();
70       shall_exit = true;
71     } else if (parse_args && (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help"))) {
72       XBT_HELP("Description of the configuration accepted by this simulator:");
73       simgrid::config::help();
74       XBT_HELP("\n"
75                "Each of these configurations can be used by adding\n"
76                "    --cfg=<option name>:<option value>\n"
77                "to the command line. Try passing \"help\" as a value\n"
78                "to get the list of values accepted by a given option.\n"
79                "For example, \"--cfg=plugin:help\" gives you the list of\n"
80                "plugins available in your installation of SimGrid.\n"
81                "\n"
82                "For more information, please refer to:\n"
83                "   --help-aliases for the list of all option aliases.\n"
84                "   --help-logs and --help-log-categories for the details of logging output.\n"
85                "   --help-models for a list of all models known by this simulator.\n"
86                "   --help-tracing for the details of all tracing options known by this simulator.\n"
87                "   --version to get SimGrid version information.\n");
88       shall_exit = not cfg_continue_after_help;
89       argv[j++]  = argv[i]; // Preserve the --help in argv just in case someone else wants to see it
90     } else if (parse_args && not strcmp(argv[i], "--help-aliases")) {
91       XBT_HELP("Here is a list of all deprecated option names, with their replacement.");
92       simgrid::config::show_aliases();
93       XBT_HELP("Please consider using the recent names");
94       shall_exit = true;
95     } else if (parse_args && not strcmp(argv[i], "--help-models")) {
96       model_help("host", surf_host_model_description);
97       XBT_HELP("%s", "");
98       model_help("CPU", surf_cpu_model_description);
99       XBT_HELP("%s", "");
100       model_help("network", surf_network_model_description);
101       XBT_HELP("\nLong description of all optimization levels accepted by the models of this simulator:");
102       for (auto const& item : surf_optimization_mode_description)
103         XBT_HELP("  %s: %s", item.name, item.description);
104       XBT_HELP("Both network and CPU models have 'Lazy' as default optimization level\n");
105       shall_exit = true;
106     } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
107       TRACE_help();
108       shall_exit = true;
109     } else {
110       argv[j++] = argv[i];
111     }
112   }
113   if (j < *argc) {
114     argv[j] = nullptr;
115     *argc = j;
116   }
117   if (shall_exit)
118     exit(0);
119 }
120
121 /* callback of the plugin variable */
122 static void _sg_cfg_cb__plugin(const std::string& value)
123 {
124   xbt_assert(_sg_cfg_init_status < 2, "Cannot load a plugin after the initialization");
125
126   if (value.empty())
127     return;
128
129   if (value == "help") {
130     model_help("plugin", surf_plugin_description());
131     exit(0);
132   }
133
134   const auto* plugin = find_model_description(surf_plugin_description(), value);
135   plugin->model_init_preparse();
136 }
137
138 /* callback of the host/model variable */
139 static void _sg_cfg_cb__host_model(const std::string& value)
140 {
141   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
142
143   if (value == "help") {
144     model_help("host", surf_host_model_description);
145     exit(0);
146   }
147
148   /* Make sure that the model exists */
149   find_model_description(surf_host_model_description, value);
150 }
151
152 /* callback of the cpu/model variable */
153 static void _sg_cfg_cb__cpu_model(const std::string& value)
154 {
155   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
156
157   if (value == "help") {
158     model_help("CPU", surf_cpu_model_description);
159     exit(0);
160   }
161
162   /* New Module missing */
163   find_model_description(surf_cpu_model_description, value);
164 }
165
166 /* callback of the cpu/model variable */
167 static void _sg_cfg_cb__optimization_mode(const std::string& value)
168 {
169   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
170
171   if (value == "help") {
172     model_help("optimization", surf_optimization_mode_description);
173     exit(0);
174   }
175
176   /* New Module missing */
177   find_model_description(surf_optimization_mode_description, value);
178 }
179
180 static void _sg_cfg_cb__disk_model(const std::string& value)
181 {
182   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
183
184   if (value == "help") {
185     model_help("disk", surf_disk_model_description);
186     exit(0);
187   }
188
189   find_model_description(surf_disk_model_description, value);
190 }
191
192 /* callback of the network_model variable */
193 static void _sg_cfg_cb__network_model(const std::string& value)
194 {
195   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
196
197   if (value == "help") {
198     model_help("network", surf_network_model_description);
199     exit(0);
200   }
201
202   /* New Module missing */
203   find_model_description(surf_network_model_description, value);
204 }
205
206 static void _sg_cfg_cb_contexts_parallel_mode(std::string_view mode_name)
207 {
208   if (mode_name == "posix") {
209     simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_POSIX);
210   } else if (mode_name == "futex") {
211     simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_FUTEX);
212   } else if (mode_name == "busy_wait") {
213     simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
214   } else {
215     xbt_die("Command line setting of the parallel synchronization mode should "
216             "be one of \"posix\", \"futex\" or \"busy_wait\"");
217   }
218 }
219
220 /* build description line with possible values */
221 static void declare_model_flag(const std::string& name, const std::string& value,
222                                const std::function<void(std::string const&)>& callback,
223                                const std::vector<surf_model_description_t>& model_description, const std::string& type,
224                                const std::string& descr)
225 {
226   std::string description = descr + ". Possible values: ";
227   std::string sep         = "";
228   for (auto const& item : model_description) {
229     description += sep + item.name;
230     sep = ", ";
231   }
232   description += ".\n       (use 'help' as a value to see the long description of each " + type + ")";
233   simgrid::config::declare_flag<std::string>(name, description, value, callback);
234 }
235
236 /* create the config set, register what should be and parse the command line*/
237 void sg_config_init(int *argc, char **argv)
238 {
239   /* Create the configuration support */
240   if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */
241     XBT_WARN("Call to sg_config_init() after initialization ignored");
242     return;
243   }
244
245   /* Plugins configuration */
246   declare_model_flag("plugin", "", &_sg_cfg_cb__plugin, surf_plugin_description(), "plugin", "The plugins");
247
248   declare_model_flag("cpu/model", "Cas01", &_sg_cfg_cb__cpu_model, surf_cpu_model_description, "model",
249                      "The model to use for the CPU");
250
251   declare_model_flag("disk/model", "S19", &_sg_cfg_cb__disk_model, surf_disk_model_description, "model",
252                      "The model to use for the disk");
253
254   declare_model_flag("network/model", "LV08", &_sg_cfg_cb__network_model, surf_network_model_description, "model",
255                      "The model to use for the network");
256
257   declare_model_flag("network/optim", "Lazy", &_sg_cfg_cb__optimization_mode, surf_optimization_mode_description,
258                      "optimization mode", "The optimization modes to use for the network");
259
260   declare_model_flag("host/model", "default", &_sg_cfg_cb__host_model, surf_host_model_description, "model",
261                      "The model to use for the host");
262
263   simgrid::config::bind_flag(sg_surf_precision, "surf/precision",
264                              "Numerical precision used when updating simulation times (in seconds)");
265
266   simgrid::config::bind_flag(sg_maxmin_precision, "maxmin/precision",
267                              "Numerical precision used when computing resource sharing (in flops/sec or bytes/sec)");
268
269   simgrid::config::bind_flag(sg_concurrency_limit, "maxmin/concurrency-limit",
270                              "Maximum number of concurrent variables in the maxmim system. Also limits the number of "
271                              "processes on each host, at higher level. (default: -1 means no such limitation)");
272
273   /* The parameters of network models */
274   static simgrid::config::Flag<double> _sg_network_loopback_latency{
275       "network/loopback-lat",
276       "For network models with an implicit loopback link (L07, CM02, LV08), "
277       "latency of the loopback link. 0 by default",
278       0.0};
279
280   static simgrid::config::Flag<double> _sg_network_loopback_bandwidth{
281       "network/loopback-bw",
282       "For network models with an implicit loopback link (L07, CM02, LV08), "
283       "bandwidth of the loopback link. 10GBps by default",
284       10e9};
285
286   /* Inclusion path */
287   static simgrid::config::Flag<std::string> cfg_path{
288       "path", "Lookup path for inclusions in platform and deployment XML files", "", [](std::string const& path) {
289         if (not path.empty())
290           surf_path.push_back(path);
291       }};
292
293   static simgrid::config::Flag<bool> cfg_cpu_maxmin_selective_update{
294       "cpu/maxmin-selective-update",
295       "Update the constraint set propagating recursively to others constraints "
296       "(off by default unless optim is set to lazy)",
297       false};
298   static simgrid::config::Flag<bool> cfg_network_maxmin_selective_update{"network/maxmin-selective-update",
299                                                                          "Update the constraint set propagating "
300                                                                          "recursively to others constraints (off by "
301                                                                          "default unless optim is set to lazy)",
302                                                                          false};
303
304   static simgrid::config::Flag<int> cfg_context_stack_size{
305       "contexts/stack-size", "Stack size of contexts in KiB (not with threads)", 8 * 1024,
306       [](int value) { simgrid::kernel::context::stack_size = value * 1024; }};
307
308   /* guard size for contexts stacks in memory pages */
309 #if (PTH_STACKGROWTH != -1)
310   int default_guard_size = 0;
311 #else
312   int default_guard_size = 1;
313 #endif
314   static simgrid::config::Flag<int> cfg_context_guard_size{
315       "contexts/guard-size", "Guard size for contexts stacks in memory pages", default_guard_size,
316       [](int value) { simgrid::kernel::context::guard_size = value * xbt_pagesize; }};
317
318   static simgrid::config::Flag<int> cfg_context_nthreads{
319       "contexts/nthreads", "Number of parallel threads used to execute user contexts", 1, [](int nthreads) {
320 #if HAVE_MMALLOC
321         xbt_assert(
322             nthreads == 1 || not malloc_use_mmalloc(),
323             "Parallel simulation is forbidden in the verified program, as there is no protection against race "
324             "conditions in mmalloc itself. Please don't be so greedy and show some mercy for our implementation.");
325 #endif
326         simgrid::kernel::context::set_nthreads(nthreads);
327       }};
328
329   /* synchronization mode for parallel user contexts */
330 #if HAVE_FUTEX_H
331   std::string default_synchro_mode = "futex";
332 #else // No futex on mac and posix is unimplemented yet
333   std::string default_synchro_mode = "busy_wait";
334 #endif
335   static simgrid::config::Flag<std::string> cfg_context_synchro{"contexts/synchro",
336                                                                 "Synchronization mode to use when running contexts in "
337                                                                 "parallel (either futex, posix or busy_wait)",
338                                                                 default_synchro_mode,
339                                                                 &_sg_cfg_cb_contexts_parallel_mode};
340
341   // SMPI model can be used without enable_smpi, so keep this out of the ifdef.
342   static simgrid::config::Flag<std::string> cfg_smpi_IB_penalty_factors{
343       "smpi/IB-penalty-factors",
344       "Correction factor to communications using Infiniband model with "
345       "contention (default value based on Stampede cluster profiling)",
346       "0.965;0.925;1.35"};
347   /* Others */
348
349   static simgrid::config::Flag<bool> cfg_execution_cutpath{
350       "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false};
351
352   if (surf_path.empty())
353     simgrid::config::set_default<std::string>("path", "./");
354
355   _sg_cfg_init_status = 1;
356
357   sg_config_cmd_line(argc, argv);
358
359   xbt_mallocator_initialization_is_done(simgrid::kernel::context::is_parallel());
360 }
361
362 void sg_config_finalize()
363 {
364   simgrid::config::finalize();
365   _sg_cfg_init_status = 0;
366 }