1 /* Copyright (c) 2009-2023. The SimGrid Team. All rights reserved. */
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. */
6 /* sg_config: configuration infrastructure for the simulation world */
8 #include <simgrid/instr.h>
9 #include <simgrid/version.h>
10 #include <xbt/config.hpp>
11 #include <xbt/file.hpp>
13 #include "simgrid/sg_config.hpp"
14 #include "src/include/xbt/mmalloc.h"
15 #include "src/instr/instr_private.hpp"
16 #include "src/internal_config.h"
17 #include "src/kernel/context/Context.hpp"
18 #include "src/kernel/lmm/maxmin.hpp"
19 #include "src/kernel/resource/NetworkModel.hpp"
20 #include "src/mc/mc_config.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/smpi/include/smpi_config.hpp"
23 #include "src/surf/surf_interface.hpp"
25 #include <string_view>
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf, "About the configuration of SimGrid");
29 static simgrid::config::Flag<bool> cfg_continue_after_help
30 {"help-nostop", "Do not stop the execution when --help is found", false};
32 /** @brief Allow other libraries to react to the --help flag, too
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.
39 void sg_config_continue_after_help()
41 cfg_continue_after_help = true;
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!)
50 int _sg_cfg_init_status = 0;
52 /* Parse the command line, looking for options */
53 static void sg_config_cmd_line(int *argc, char **argv)
55 bool shall_exit = false;
56 bool parse_args = true; // Stop parsing the parameters once we found '--'
59 for (int i = j; i < *argc; i++) {
60 if (not strcmp("--", argv[i])) {
62 // Remove that '--' from the arguments
63 } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
64 char *opt = strchr(argv[i], '=');
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")) {
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();
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"
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");
96 } else if (parse_args && not strcmp(argv[i], "--help-models")) {
97 model_help("host", surf_host_model_description);
99 model_help("CPU", surf_cpu_model_description);
101 model_help("network", surf_network_model_description);
102 XBT_HELP("\nLong description of all optimization levels accepted by the models of this simulator:");
103 for (auto const& item : surf_optimization_mode_description)
104 XBT_HELP(" %s: %s", item.name, item.description);
105 XBT_HELP("Both network and CPU models have 'Lazy' as default optimization level\n");
107 } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
122 /* callback of the plugin variable */
123 static void _sg_cfg_cb__plugin(const std::string& value)
125 xbt_assert(_sg_cfg_init_status < 2, "Cannot load a plugin after the initialization");
130 if (value == "help") {
131 model_help("plugin", surf_plugin_description());
135 const auto* plugin = find_model_description(surf_plugin_description(), value);
136 plugin->model_init_preparse();
139 /* callback of the host/model variable */
140 static void _sg_cfg_cb__host_model(const std::string& value)
142 xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
144 if (value == "help") {
145 model_help("host", surf_host_model_description);
149 /* Make sure that the model exists */
150 find_model_description(surf_host_model_description, value);
153 /* callback of the cpu/model variable */
154 static void _sg_cfg_cb__cpu_model(const std::string& value)
156 xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
158 if (value == "help") {
159 model_help("CPU", surf_cpu_model_description);
163 /* New Module missing */
164 find_model_description(surf_cpu_model_description, value);
167 /* callback of the cpu/model variable */
168 static void _sg_cfg_cb__optimization_mode(const std::string& value)
170 xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
172 if (value == "help") {
173 model_help("optimization", surf_optimization_mode_description);
177 /* New Module missing */
178 find_model_description(surf_optimization_mode_description, value);
181 static void _sg_cfg_cb__disk_model(const std::string& value)
183 xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
185 if (value == "help") {
186 model_help("disk", surf_disk_model_description);
190 find_model_description(surf_disk_model_description, value);
193 /* callback of the network_model variable */
194 static void _sg_cfg_cb__network_model(const std::string& value)
196 xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
198 if (value == "help") {
199 model_help("network", surf_network_model_description);
203 /* New Module missing */
204 find_model_description(surf_network_model_description, value);
207 static void _sg_cfg_cb_contexts_parallel_mode(std::string_view mode_name)
209 if (mode_name == "posix") {
210 simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_POSIX;
211 } else if (mode_name == "futex") {
212 simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_FUTEX;
213 } else if (mode_name == "busy_wait") {
214 simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_BUSY_WAIT;
216 xbt_die("Command line setting of the parallel synchronization mode should "
217 "be one of \"posix\", \"futex\" or \"busy_wait\"");
221 /* build description line with possible values */
222 static void declare_model_flag(const std::string& name, const std::string& value,
223 const std::function<void(std::string const&)>& callback,
224 const std::vector<surf_model_description_t>& model_description, const std::string& type,
225 const std::string& descr)
227 std::string description = descr + ". Possible values: ";
228 std::string sep = "";
229 for (auto const& item : model_description) {
230 description += sep + item.name;
233 description += ".\n (use 'help' as a value to see the long description of each " + type + ")";
234 simgrid::config::declare_flag<std::string>(name, description, value, callback);
237 /* create the config set, register what should be and parse the command line*/
238 void sg_config_init(int *argc, char **argv)
240 /* Create the configuration support */
241 if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */
242 XBT_WARN("Call to sg_config_init() after initialization ignored");
246 /* Plugins configuration */
247 declare_model_flag("plugin", "", &_sg_cfg_cb__plugin, surf_plugin_description(), "plugin", "The plugins");
249 declare_model_flag("cpu/model", "Cas01", &_sg_cfg_cb__cpu_model, surf_cpu_model_description, "model",
250 "The model to use for the CPU");
252 declare_model_flag("disk/model", "S19", &_sg_cfg_cb__disk_model, surf_disk_model_description, "model",
253 "The model to use for the disk");
255 declare_model_flag("network/model", "LV08", &_sg_cfg_cb__network_model, surf_network_model_description, "model",
256 "The model to use for the network");
258 declare_model_flag("network/optim", "Lazy", &_sg_cfg_cb__optimization_mode, surf_optimization_mode_description,
259 "optimization mode", "The optimization modes to use for the network");
261 declare_model_flag("host/model", "default", &_sg_cfg_cb__host_model, surf_host_model_description, "model",
262 "The model to use for the host");
264 simgrid::config::bind_flag(sg_surf_precision, "surf/precision",
265 "Numerical precision used when updating simulation times (in seconds)");
267 simgrid::config::bind_flag(sg_maxmin_precision, "maxmin/precision",
268 "Numerical precision used when computing resource sharing (in flops/sec or bytes/sec)");
270 simgrid::config::bind_flag(sg_concurrency_limit, "maxmin/concurrency-limit",
271 "Maximum number of concurrent variables in the maxmim system. Also limits the number of "
272 "processes on each host, at higher level. (default: -1 means no such limitation)");
274 /* The parameters of network models */
275 static simgrid::config::Flag<double> _sg_network_loopback_latency{
276 "network/loopback-lat",
277 "For network models with an implicit loopback link (L07, CM02, LV08), "
278 "latency of the loopback link. 0 by default",
281 static simgrid::config::Flag<double> _sg_network_loopback_bandwidth{
282 "network/loopback-bw",
283 "For network models with an implicit loopback link (L07, CM02, LV08), "
284 "bandwidth of the loopback link. 10GBps by default",
288 static simgrid::config::Flag<std::string> cfg_path{
289 "path", "Lookup path for inclusions in platform and deployment XML files", "./", [](std::string const& path) {
290 if (not path.empty())
291 simgrid::xbt::path_push(path);
294 static simgrid::config::Flag<bool> cfg_cpu_maxmin_selective_update{
295 "cpu/maxmin-selective-update",
296 "Update the constraint set propagating recursively to others constraints "
297 "(off by default unless optim is set to lazy)",
299 static simgrid::config::Flag<bool> cfg_network_maxmin_selective_update{"network/maxmin-selective-update",
300 "Update the constraint set propagating "
301 "recursively to others constraints (off by "
302 "default unless optim is set to lazy)",
305 static simgrid::config::Flag<int> cfg_context_stack_size{
306 "contexts/stack-size", "Stack size of contexts in KiB (not with threads)", 8 * 1024,
307 [](int value) { simgrid::kernel::context::Context::stack_size = value * 1024; }};
309 /* guard size for contexts stacks in memory pages */
310 #if (PTH_STACKGROWTH != -1)
311 int default_guard_size = 0;
313 int default_guard_size = 1;
315 static simgrid::config::Flag<int> cfg_context_guard_size{
316 "contexts/guard-size", "Guard size for contexts stacks in memory pages", default_guard_size,
317 [](int value) { simgrid::kernel::context::Context::guard_size = value * xbt_pagesize; }};
319 static simgrid::config::Flag<int> cfg_context_nthreads{
320 "contexts/nthreads", "Number of parallel threads used to execute user contexts", 1, [](int nthreads) {
323 nthreads == 1 || not malloc_use_mmalloc(),
324 "Parallel simulation is forbidden in the verified program, as there is no protection against race "
325 "conditions in mmalloc itself. Please don't be so greedy and show some mercy for our implementation.");
327 simgrid::kernel::context::Context::set_nthreads(nthreads);
330 /* synchronization mode for parallel user contexts */
332 std::string default_synchro_mode = "futex";
333 #else // No futex on mac and posix is unimplemented yet
334 std::string default_synchro_mode = "busy_wait";
336 static simgrid::config::Flag<std::string> cfg_context_synchro{"contexts/synchro",
337 "Synchronization mode to use when running contexts in "
338 "parallel (either futex, posix or busy_wait)",
339 default_synchro_mode,
340 &_sg_cfg_cb_contexts_parallel_mode};
342 // SMPI model can be used without enable_smpi, so keep this out of the ifdef.
343 static simgrid::config::Flag<std::string> cfg_smpi_IB_penalty_factors{
344 "smpi/IB-penalty-factors",
345 "Correction factor to communications using Infiniband model with "
346 "contention (default value based on Stampede cluster profiling)",
350 static simgrid::config::Flag<bool> cfg_execution_cutpath{
351 "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false};
353 _sg_cfg_init_status = 1;
355 sg_config_cmd_line(argc, argv);
357 xbt_mallocator_initialization_is_done(simgrid::kernel::context::Context::is_parallel());
360 void sg_config_finalize()
362 simgrid::config::finalize();
363 _sg_cfg_init_status = 0;