Logo AND Algorithmique Numérique Distribuée

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