Logo AND Algorithmique Numérique Distribuée

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