Logo AND Algorithmique Numérique Distribuée

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