Logo AND Algorithmique Numérique Distribuée

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