Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Objectifies the CPU model
[simgrid.git] / src / simgrid / sg_config.cpp
1 /* Copyright (c) 2009-2023. 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 #include <xbt/file.hpp>
12
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/simgrid/module.hpp"
23 #include "src/smpi/include/smpi_config.hpp"
24 #include "src/surf/surf_interface.hpp"
25
26 #include <string_view>
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config, kernel, "About the configuration of SimGrid");
29
30 static simgrid::config::Flag<bool> cfg_continue_after_help
31   {"help-nostop", "Do not stop the execution when --help is found", false};
32
33 /** @brief Allow other libraries to react to the --help flag, too
34  *
35  * When finding --help on the command line, simgrid usually stops right after displaying its help message.
36  * If you are writing a library using simgrid, you may want to display your own help message before everything stops.
37  * If so, just call this function before having simgrid parsing the command line, and you will be given the control
38  * even if the user is asking for help.
39  */
40 void sg_config_continue_after_help()
41 {
42   cfg_continue_after_help = true;
43 }
44
45 /* 0: beginning of time (config cannot be changed yet)
46  * 1: initialized: cfg_set created (config can now be changed)
47  * 2: configured: command line parsed and config part of platform file was
48  *    integrated also, platform construction ongoing or done.
49  *    (Config cannot be changed anymore!)
50  */
51 int _sg_cfg_init_status = 0;
52
53 /* Parse the command line, looking for options */
54 static void sg_config_cmd_line(int *argc, char **argv)
55 {
56   bool shall_exit = false;
57   bool parse_args = true; // Stop parsing the parameters once we found '--'
58
59   int j = 1;
60   for (int i = j; i < *argc; i++) {
61     if (not strcmp("--", argv[i])) {
62       parse_args = false;
63       // Remove that '--' from the arguments
64     } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
65       char *opt = strchr(argv[i], '=');
66       opt++;
67
68       simgrid::config::set_parse(opt);
69       XBT_DEBUG("Did apply '%s' as config setting", opt);
70     } else if (parse_args && not strcmp(argv[i], "--version")) {
71       sg_version();
72       shall_exit = true;
73     } else if (parse_args && (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help"))) {
74       XBT_HELP("Description of the configuration accepted by this simulator:");
75       simgrid::config::help();
76       XBT_HELP("\n"
77                "Each of these configurations can be used by adding\n"
78                "    --cfg=<option name>:<option value>\n"
79                "to the command line. Try passing \"help\" as a value\n"
80                "to get the list of values accepted by a given option.\n"
81                "For example, \"--cfg=plugin:help\" gives you the list of\n"
82                "plugins available in your installation of SimGrid.\n"
83                "\n"
84                "For more information, please refer to:\n"
85                "   --help-aliases for the list of all option aliases.\n"
86                "   --help-logs and --help-log-categories for the details of logging output.\n"
87                "   --help-models for a list of all models known by this simulator.\n"
88                "   --help-tracing for the details of all tracing options known by this simulator.\n"
89                "   --version to get SimGrid version information.\n");
90       shall_exit = not cfg_continue_after_help;
91       argv[j++]  = argv[i]; // Preserve the --help in argv just in case someone else wants to see it
92     } else if (parse_args && not strcmp(argv[i], "--help-aliases")) {
93       XBT_HELP("Here is a list of all deprecated option names, with their replacement.");
94       simgrid::config::show_aliases();
95       XBT_HELP("Please consider using the recent names");
96       shall_exit = true;
97     } else if (parse_args && not strcmp(argv[i], "--help-models")) {
98       surf_host_model_description.help();
99       XBT_HELP("%s", "");
100       simgrid_cpu_models().help();
101       XBT_HELP("%s", "");
102       simgrid_network_models().help();
103       XBT_HELP("\nLong description of all optimization levels accepted by the models of this simulator:");
104       surf_optimization_mode_description.help();
105       XBT_HELP("Both network and CPU models have 'Lazy' as default optimization level\n");
106       shall_exit = true;
107     } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
108       TRACE_help();
109       shall_exit = true;
110     } else {
111       argv[j++] = argv[i];
112     }
113   }
114   if (j < *argc) {
115     argv[j] = nullptr;
116     *argc = j;
117   }
118   if (shall_exit)
119     exit(0);
120 }
121
122 /* callback of the plugin variable */
123 static void _sg_cfg_cb__plugin(const std::string& value)
124 {
125   xbt_assert(_sg_cfg_init_status < 2, "Cannot load a plugin after the initialization");
126
127   if (value.empty())
128     return;
129
130   if (value == "help") {
131     simgrid_plugins().help();
132     exit(0);
133   }
134
135   simgrid_plugins().by_name(value).init();
136 }
137
138 /* callback of the host/model variable */
139 static void _sg_cfg_cb__host_model(const std::string& value)
140 {
141   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
142
143   if (value == "help") {
144     surf_host_model_description.help();
145     exit(0);
146   }
147
148   /* Make sure that the model exists */
149   surf_host_model_description.by_name(value);
150 }
151
152 /* callback of the cpu/model variable */
153 static void _sg_cfg_cb__cpu_model(const std::string& value)
154 {
155   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
156
157   if (value == "help") {
158     simgrid_cpu_models().help();
159     exit(0);
160   }
161
162   /* Make sure that the model exists */
163   simgrid_cpu_models().by_name(value);
164 }
165
166 /* callback of the cpu/model variable */
167 static void _sg_cfg_cb__optimization_mode(const std::string& value)
168 {
169   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
170
171   if (value == "help") {
172     surf_optimization_mode_description.help();
173     exit(0);
174   }
175
176   /* Make sure that the model exists */
177   surf_optimization_mode_description.by_name(value);
178 }
179
180 static void _sg_cfg_cb__disk_model(const std::string& value)
181 {
182   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
183
184   if (value == "help") {
185     surf_disk_model_description.help();
186     exit(0);
187   }
188
189   surf_disk_model_description.by_name(value);
190 }
191
192 /* callback of the network_model variable */
193 static void _sg_cfg_cb__network_model(const std::string& value)
194 {
195   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
196
197   if (value == "help") {
198     simgrid_network_models().help();
199     exit(0);
200   }
201
202   simgrid_network_models().by_name(value); // Simply ensure that it exists
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::Context::parallel_mode = XBT_PARMAP_POSIX;
209   } else if (mode_name == "futex") {
210     simgrid::kernel::context::Context::parallel_mode = XBT_PARMAP_FUTEX;
211   } else if (mode_name == "busy_wait") {
212     simgrid::kernel::context::Context::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 simgrid::ModuleGroup& model_description, const std::string& type,
223                                const std::string& descr)
224 {
225   std::string description = descr + ". Possible values (other compilation flags may activate more " +
226                             model_description.get_kind() + "): " + model_description.existing_values();
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   simgrid_create_models();
240   /* Plugins configuration */
241   declare_model_flag("plugin", "", &_sg_cfg_cb__plugin, simgrid_plugins(), "plugin", "The plugins");
242
243   declare_model_flag("cpu/model", "Cas01", &_sg_cfg_cb__cpu_model, simgrid_cpu_models(), "model",
244                      "The model to use for the CPU");
245
246   declare_model_flag("disk/model", "S19", &_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, simgrid_network_models(), "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   static simgrid::config::Flag<double> _sg_network_loopback_latency{
270       "network/loopback-lat",
271       "For network models with an implicit loopback link (L07, CM02, LV08), "
272       "latency of the loopback link. 0 by default",
273       0.0};
274
275   static simgrid::config::Flag<double> _sg_network_loopback_bandwidth{
276       "network/loopback-bw",
277       "For network models with an implicit loopback link (L07, CM02, LV08), "
278       "bandwidth of the loopback link. 10GBps by default",
279       10e9};
280
281   /* Inclusion path */
282   static simgrid::config::Flag<std::string> cfg_path{
283       "path", "Lookup path for inclusions in platform and deployment XML files", "./", [](std::string const& path) {
284         if (not path.empty())
285           simgrid::xbt::path_push(path);
286       }};
287
288   static simgrid::config::Flag<bool> cfg_cpu_maxmin_selective_update{
289       "cpu/maxmin-selective-update",
290       "Update the constraint set propagating recursively to others constraints "
291       "(off by default unless optim is set to lazy)",
292       false};
293   static simgrid::config::Flag<bool> cfg_network_maxmin_selective_update{"network/maxmin-selective-update",
294                                                                          "Update the constraint set propagating "
295                                                                          "recursively to others constraints (off by "
296                                                                          "default unless optim is set to lazy)",
297                                                                          false};
298
299   static simgrid::config::Flag<int> cfg_context_stack_size{
300       "contexts/stack-size", "Stack size of contexts in KiB (not with threads)", 8 * 1024,
301       [](int value) { simgrid::kernel::context::Context::stack_size = value * 1024; }};
302
303   /* guard size for contexts stacks in memory pages */
304 #if (PTH_STACKGROWTH != -1)
305   int default_guard_size = 0;
306 #else
307   int default_guard_size = 1;
308 #endif
309   static simgrid::config::Flag<int> cfg_context_guard_size{
310       "contexts/guard-size", "Guard size for contexts stacks in memory pages", default_guard_size,
311       [](int value) { simgrid::kernel::context::Context::guard_size = value * xbt_pagesize; }};
312
313   static simgrid::config::Flag<int> cfg_context_nthreads{
314       "contexts/nthreads", "Number of parallel threads used to execute user contexts", 1, [](int nthreads) {
315 #if HAVE_MMALLOC
316         xbt_assert(
317             nthreads == 1 || not malloc_use_mmalloc(),
318             "Parallel simulation is forbidden in the verified program, as there is no protection against race "
319             "conditions in mmalloc itself. Please don't be so greedy and show some mercy for our implementation.");
320 #endif
321         simgrid::kernel::context::Context::set_nthreads(nthreads);
322       }};
323
324   /* synchronization mode for parallel user contexts */
325 #if HAVE_FUTEX_H
326   std::string default_synchro_mode = "futex";
327 #else // No futex on mac and posix is unimplemented yet
328   std::string default_synchro_mode = "busy_wait";
329 #endif
330   static simgrid::config::Flag<std::string> cfg_context_synchro{"contexts/synchro",
331                                                                 "Synchronization mode to use when running contexts in "
332                                                                 "parallel (either futex, posix or busy_wait)",
333                                                                 default_synchro_mode,
334                                                                 &_sg_cfg_cb_contexts_parallel_mode};
335
336   // SMPI model can be used without enable_smpi, so keep this out of the ifdef.
337   static simgrid::config::Flag<std::string> cfg_smpi_IB_penalty_factors{
338       "smpi/IB-penalty-factors",
339       "Correction factor to communications using Infiniband model with "
340       "contention (default value based on Stampede cluster profiling)",
341       "0.965;0.925;1.35"};
342   /* Others */
343
344   static simgrid::config::Flag<bool> cfg_execution_cutpath{
345       "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false};
346
347   _sg_cfg_init_status = 1;
348
349   sg_config_cmd_line(argc, argv);
350
351   xbt_mallocator_initialization_is_done(simgrid::kernel::context::Context::is_parallel());
352 }
353
354 void sg_config_finalize()
355 {
356   simgrid::config::finalize();
357   _sg_cfg_init_status = 0;
358 }