1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* surf_config: configuration infrastructure for the simulation world */
9 #include "xbt/config.h"
11 #include "surf/surf_private.h"
12 #include "surf/surf_routing.h" /* COORD_HOST_LEVEL and COORD_ASR_LEVEL */
13 #include "simix/context.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf,
16 "About the configuration of surf (and the rest of the simulation)");
18 xbt_cfg_t _surf_cfg_set = NULL;
20 /* Parse the command line, looking for options */
21 static void surf_config_cmd_line(int *argc, char **argv)
26 for (i = 1; i < *argc; i++) {
28 if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
29 opt = strchr(argv[i], '=');
32 xbt_cfg_set_parse(_surf_cfg_set, opt);
33 XBT_DEBUG("Did apply '%s' as config setting", opt);
35 } else if (!strncmp(argv[i], "--cfg-help", strlen("--cfg-help") + 1) ||
36 !strncmp(argv[i], "--help", strlen("--help") + 1)) {
38 ("Description of the configuration accepted by this simulator:\n");
39 xbt_cfg_help(_surf_cfg_set);
40 printf("\nYou can also use --help-models to see the details of all models known by this simulator.\n");
42 printf("\nYou can also use --help-tracing to see the details of all tracing options known by this simulator.\n");
45 } else if (!strncmp(argv[i], "--help-models", strlen("--help-models") + 1)) {
46 model_help("workstation", surf_workstation_model_description);
47 model_help("CPU", surf_cpu_model_description);
48 model_help("network", surf_network_model_description);
49 printf("\nLong description of all optimization levels accepted by the models of this simulator:\n");
50 for (i = 0; surf_optimization_mode_description[i].name; i++)
51 printf(" %s: %s\n", surf_optimization_mode_description[i].name, surf_optimization_mode_description[i].description);
52 printf("Both network and CPU models have 'Lazy' as default optimization level\n");
55 } else if (!strncmp(argv[i], "--help-tracing", strlen("--help-tracing") + 1)) {
60 if (remove_it) { /*remove this from argv */
61 for (j = i + 1; j < *argc; j++) {
62 argv[j - 1] = argv[j];
67 i--; /* compensate effect of next loop incrementation */
73 int _surf_init_status = 0; /* 0: beginning of time;
74 1: pre-inited (cfg_set created);
75 2: inited (running) */
77 /* callback of the workstation/model variable */
78 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
82 xbt_assert(_surf_init_status < 2,
83 "Cannot change the model after the initialization");
85 val = xbt_cfg_get_string(_surf_cfg_set, name);
87 if (!strcmp(val, "help")) {
88 model_help("workstation", surf_workstation_model_description);
92 /* Make sure that the model exists */
93 find_model_description(surf_workstation_model_description, val);
96 /* callback of the cpu/model variable */
97 static void _surf_cfg_cb__cpu_model(const char *name, int pos)
101 xbt_assert(_surf_init_status < 2,
102 "Cannot change the model after the initialization");
104 val = xbt_cfg_get_string(_surf_cfg_set, name);
106 if (!strcmp(val, "help")) {
107 model_help("CPU", surf_cpu_model_description);
111 /* New Module missing */
112 find_model_description(surf_cpu_model_description, val);
115 /* callback of the cpu/model variable */
116 static void _surf_cfg_cb__optimization_mode(const char *name, int pos)
120 xbt_assert(_surf_init_status < 2,
121 "Cannot change the model after the initialization");
123 val = xbt_cfg_get_string(_surf_cfg_set, name);
125 if (!strcmp(val, "help")) {
126 model_help("optimization", surf_optimization_mode_description);
130 /* New Module missing */
131 find_model_description(surf_optimization_mode_description, val);
134 /* callback of the workstation_model variable */
135 static void _surf_cfg_cb__network_model(const char *name, int pos)
139 xbt_assert(_surf_init_status < 2,
140 "Cannot change the model after the initialization");
142 val = xbt_cfg_get_string(_surf_cfg_set, name);
144 if (!strcmp(val, "help")) {
145 model_help("network", surf_network_model_description);
149 /* New Module missing */
150 find_model_description(surf_network_model_description, val);
154 /* callbacks of the network models values */
155 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos)
157 sg_tcp_gamma = xbt_cfg_get_double(_surf_cfg_set, name);
160 static void _surf_cfg_cb__maxmin_precision(const char* name, int pos)
162 sg_maxmin_precision = xbt_cfg_get_double(_surf_cfg_set, name);
165 static void _surf_cfg_cb__sender_gap(const char* name, int pos)
167 sg_sender_gap = xbt_cfg_get_double(_surf_cfg_set, name);
170 static void _surf_cfg_cb__latency_factor(const char *name, int pos)
172 sg_latency_factor = xbt_cfg_get_double(_surf_cfg_set, name);
175 static void _surf_cfg_cb__bandwidth_factor(const char *name, int pos)
177 sg_bandwidth_factor = xbt_cfg_get_double(_surf_cfg_set, name);
180 static void _surf_cfg_cb__weight_S(const char *name, int pos)
182 sg_weight_S_parameter = xbt_cfg_get_double(_surf_cfg_set, name);
185 /* callback of the inclusion path */
186 static void _surf_cfg_cb__surf_path(const char *name, int pos)
188 char *path = xbt_cfg_get_string_at(_surf_cfg_set, name, pos);
189 xbt_dynar_push(surf_path, &path);
192 /* callback to decide if we want to use the model-checking */
193 #include "xbt_modinter.h"
194 extern int _surf_do_model_check; /* this variable lives in xbt_main until I find a right location for it */
196 static void _surf_cfg_cb_model_check(const char *name, int pos)
198 _surf_do_model_check = xbt_cfg_get_int(_surf_cfg_set, name);
200 if (_surf_do_model_check) {
201 /* Tell modules using mallocators that they shouldn't. MC don't like them */
207 extern int _surf_do_verbose_exit;
209 static void _surf_cfg_cb_verbose_exit(const char *name, int pos)
211 _surf_do_verbose_exit = xbt_cfg_get_int(_surf_cfg_set, name);
215 static void _surf_cfg_cb_context_factory(const char *name, int pos)
217 smx_context_factory_name = xbt_cfg_get_string(_surf_cfg_set, name);
220 static void _surf_cfg_cb_context_stack_size(const char *name, int pos)
222 smx_context_stack_size = xbt_cfg_get_int(_surf_cfg_set, name) * 1024;
225 static void _surf_cfg_cb_contexts_nthreads(const char *name, int pos)
227 SIMIX_context_set_nthreads(xbt_cfg_get_int(_surf_cfg_set, name));
230 static void _surf_cfg_cb_contexts_parallel_threshold(const char *name, int pos)
232 SIMIX_context_set_parallel_threshold(xbt_cfg_get_int(_surf_cfg_set, name));
235 static void _surf_cfg_cb_contexts_parallel_mode(const char *name, int pos)
237 const char* mode_name = xbt_cfg_get_string(_surf_cfg_set, name);
238 if (!strcmp(mode_name, "posix")) {
239 SIMIX_context_set_parallel_mode(XBT_PARMAP_POSIX);
241 else if (!strcmp(mode_name, "futex")) {
242 SIMIX_context_set_parallel_mode(XBT_PARMAP_FUTEX);
244 else if (!strcmp(mode_name, "busy_wait")) {
245 SIMIX_context_set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
248 XBT_WARN("Command line setting of the parallel synchronization mode should "
249 "be one of \"posix\", \"futex\" or \"busy_wait\"");
253 static void _surf_cfg_cb__surf_network_coordinates(const char *name,
256 char *val = xbt_cfg_get_string(_surf_cfg_set, name);
257 if (!strcmp(val, "yes")) {
258 if (!COORD_HOST_LEVEL) {
259 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
260 COORD_ASR_LEVEL = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
262 } else if (!strcmp(val, "no")) {
263 if (COORD_HOST_LEVEL)
264 XBT_WARN("Setting of whether to use coordinate cannot be disabled once set.");
266 XBT_WARN("Command line setting of whether to use coordinates must be either \"yes\" or \"no\"");
270 static void _surf_cfg_cb__surf_network_crosstraffic(const char *name,
273 sg_network_crosstraffic = xbt_cfg_get_int(_surf_cfg_set, name);
277 static void _surf_cfg_cb__gtnets_jitter(const char *name, int pos)
279 sg_gtnets_jitter = xbt_cfg_get_double(_surf_cfg_set, name);
282 static void _surf_cfg_cb__gtnets_jitter_seed(const char *name, int pos)
284 sg_gtnets_jitter_seed = xbt_cfg_get_int(_surf_cfg_set, name);
288 /* create the config set, register what should be and parse the command line*/
289 void surf_config_init(int *argc, char **argv)
291 char *description = xbt_malloc(1024), *p = description;
293 double double_default_value;
294 int default_value_int;
297 /* Create the configuration support */
298 if (_surf_init_status == 0) { /* Only create stuff if not already inited */
299 _surf_init_status = 1;
302 "The model to use for the CPU. Possible values: ");
304 while (*(++p) != '\0');
305 for (i = 0; surf_cpu_model_description[i].name; i++)
306 p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
307 surf_cpu_model_description[i].name);
309 ".\n (use 'help' as a value to see the long description of each model)");
310 default_value = xbt_strdup("Cas01");
311 xbt_cfg_register(&_surf_cfg_set,
312 "cpu/model", description, xbt_cfgelm_string,
313 &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
316 "The optimization modes to use for the CPU. Possible values: ");
318 while (*(++p) != '\0');
319 for (i = 0; surf_optimization_mode_description[i].name; i++)
320 p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
321 surf_optimization_mode_description[i].name);
323 ".\n (use 'help' as a value to see the long description of each optimization mode)");
324 default_value = xbt_strdup("Lazy");
325 xbt_cfg_register(&_surf_cfg_set,
326 "cpu/optim", description, xbt_cfgelm_string,
327 &default_value, 1, 1, &_surf_cfg_cb__optimization_mode, NULL);
330 "The model to use for the network. Possible values: ");
332 while (*(++p) != '\0');
333 for (i = 0; surf_network_model_description[i].name; i++)
334 p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
335 surf_network_model_description[i].name);
337 ".\n (use 'help' as a value to see the long description of each model)");
338 default_value = xbt_strdup("LV08");
339 xbt_cfg_register(&_surf_cfg_set,
340 "network/model", description, xbt_cfgelm_string,
341 &default_value, 1, 1, &_surf_cfg_cb__network_model,
345 "The optimization modes to use for the network. Possible values: ");
347 while (*(++p) != '\0');
348 for (i = 0; surf_optimization_mode_description[i].name; i++)
349 p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
350 surf_optimization_mode_description[i].name);
352 ".\n (use 'help' as a value to see the long description of each optimization mode)");
353 default_value = xbt_strdup("Lazy");
354 xbt_cfg_register(&_surf_cfg_set,
355 "network/optim", description, xbt_cfgelm_string,
356 &default_value, 1, 1, &_surf_cfg_cb__optimization_mode, NULL);
359 "The model to use for the workstation. Possible values: ");
361 while (*(++p) != '\0');
362 for (i = 0; surf_workstation_model_description[i].name; i++)
363 p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
364 surf_workstation_model_description[i].name);
366 ".\n (use 'help' as a value to see the long description of each model)");
367 default_value = xbt_strdup("current_default");
368 xbt_cfg_register(&_surf_cfg_set,
369 "workstation/model", description, xbt_cfgelm_string,
370 &default_value, 1, 1,
371 &_surf_cfg_cb__workstation_model, NULL);
373 xbt_free(description);
375 default_value = xbt_strdup("Full");
376 xbt_cfg_register(&_surf_cfg_set, "routing",
377 "Model to use to store the routing information",
378 xbt_cfgelm_string, &default_value, 1, 1, NULL, NULL);
380 xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
381 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; Use the last given value, which is the max window size)",
382 xbt_cfgelm_double, NULL, 1, 1,
383 _surf_cfg_cb__tcp_gamma, NULL);
384 xbt_cfg_setdefault_double(_surf_cfg_set, "TCP_gamma", 20000.0);
386 xbt_cfg_register(&_surf_cfg_set, "maxmin/precision",
387 "Minimum retained action value when updating simulation",
388 xbt_cfgelm_double, NULL, 1, 1, _surf_cfg_cb__maxmin_precision, NULL);
389 xbt_cfg_setdefault_double(_surf_cfg_set, "maxmin/precision", 0.00001); // FIXME use setdefault everywhere here!
391 /* The parameters of network models */
393 double_default_value = 0.0;
394 xbt_cfg_register(&_surf_cfg_set, "network/sender_gap",
395 "Minimum gap between two overlapping sends",
396 xbt_cfgelm_double, &double_default_value, 1, 1,
397 _surf_cfg_cb__sender_gap, NULL);
399 double_default_value = 1.0;
400 xbt_cfg_register(&_surf_cfg_set, "network/latency_factor",
401 "Correction factor to apply to the provided latency (default value set by network model)",
402 xbt_cfgelm_double, &double_default_value, 1, 1,
403 _surf_cfg_cb__latency_factor, NULL);
404 double_default_value = 1.0;
405 xbt_cfg_register(&_surf_cfg_set, "network/bandwidth_factor",
406 "Correction factor to apply to the provided bandwidth (default value set by network model)",
407 xbt_cfgelm_double, &double_default_value, 1, 1,
408 _surf_cfg_cb__bandwidth_factor, NULL);
409 double_default_value = 0.0;
410 xbt_cfg_register(&_surf_cfg_set, "network/weight_S",
411 "Correction factor to apply to the weight of competing streams(default value set by network model)",
412 xbt_cfgelm_double, &double_default_value, 1, 1,
413 _surf_cfg_cb__weight_S, NULL);
416 xbt_cfg_register(&_surf_cfg_set, "path",
417 "Lookup path for inclusions in platform and deployment XML files",
418 xbt_cfgelm_string, NULL, 0, 0,
419 _surf_cfg_cb__surf_path, NULL);
421 default_value_int = 0;
422 xbt_cfg_register(&_surf_cfg_set, "cpu/maxmin_selective_update",
423 "Update the constraint set propagating recursively to others constraints (1 by default when optim is set to lazy)",
424 xbt_cfgelm_int, &default_value_int, 0, 1,
426 default_value_int = 0;
427 xbt_cfg_register(&_surf_cfg_set, "network/maxmin_selective_update",
428 "Update the constraint set propagating recursively to others constraints (1 by default when optim is set to lazy)",
429 xbt_cfgelm_int, &default_value_int, 0, 1,
433 default_value_int = 0;
434 xbt_cfg_register(&_surf_cfg_set, "model-check",
435 "Activate the model-checking of the \"simulated\" system (EXPERIMENTAL -- msg only for now)",
436 xbt_cfgelm_int, &default_value_int, 0, 1,
437 _surf_cfg_cb_model_check, NULL);
440 FIXME: this function is not setting model-check to it's default value because
441 internally it calls to variable->cb_set that in this case is the function
442 _surf_cfg_cb_model_check which sets it's value to 1 (instead of the default value 0)
443 xbt_cfg_set_int(_surf_cfg_set, "model-check", default_value_int); */
445 /* do verbose-exit */
446 default_value_int = 0;
447 xbt_cfg_register(&_surf_cfg_set, "verbose-exit",
448 "Activate the \"do nothing\" mode in Ctrl-C",
449 xbt_cfgelm_int, &default_value_int, 0, 1,
450 _surf_cfg_cb_verbose_exit, NULL);
453 /* context factory */
454 default_value = xbt_strdup("ucontext");
455 xbt_cfg_register(&_surf_cfg_set, "contexts/factory",
456 "Context factory to use in SIMIX (ucontext, thread or raw)",
457 xbt_cfgelm_string, &default_value, 1, 1, _surf_cfg_cb_context_factory, NULL);
459 /* stack size of contexts in Ko */
460 default_value_int = 128;
461 xbt_cfg_register(&_surf_cfg_set, "contexts/stack_size",
462 "Stack size of contexts in Ko (ucontext or raw only)",
463 xbt_cfgelm_int, &default_value_int, 1, 1,
464 _surf_cfg_cb_context_stack_size, NULL);
466 /* number of parallel threads for user processes */
467 default_value_int = 1;
468 xbt_cfg_register(&_surf_cfg_set, "contexts/nthreads",
469 "Number of parallel threads for user contexts (EXPERIMENTAL)",
470 xbt_cfgelm_int, &default_value_int, 1, 1,
471 _surf_cfg_cb_contexts_nthreads, NULL);
473 /* minimal number of user contexts to be run in parallel */
474 default_value_int = 1;
475 xbt_cfg_register(&_surf_cfg_set, "contexts/parallel_threshold",
476 "Minimal number of user contexts to be run in parallel (raw contexts only)",
477 xbt_cfgelm_int, &default_value_int, 1, 1,
478 _surf_cfg_cb_contexts_parallel_threshold, NULL);
480 /* minimal number of user contexts to be run in parallel */
481 default_value = xbt_strdup("futex");
482 xbt_cfg_register(&_surf_cfg_set, "contexts/parallel_mode",
483 "Synchronization mode to use when running contexts in parallel",
484 xbt_cfgelm_string, &default_value, 1, 1,
485 _surf_cfg_cb_contexts_parallel_mode, NULL);
487 default_value = xbt_strdup("no");
488 xbt_cfg_register(&_surf_cfg_set, "coordinates",
489 "\"yes\" or \"no\" (FIXME: document)",
490 xbt_cfgelm_string, &default_value, 1, 1,
491 _surf_cfg_cb__surf_network_coordinates, NULL);
492 xbt_cfg_setdefault_string(_surf_cfg_set, "coordinates", default_value);
494 default_value_int = 0;
495 xbt_cfg_register(&_surf_cfg_set, "network/crosstraffic",
496 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)",
497 xbt_cfgelm_int, &default_value_int, 0, 1,
498 _surf_cfg_cb__surf_network_crosstraffic, NULL);
499 xbt_cfg_setdefault_int(_surf_cfg_set, "network/crosstraffic", default_value_int);
502 xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter",
503 "Double value to oscillate the link latency, uniformly in random interval [-latency*gtnets_jitter,latency*gtnets_jitter)",
504 xbt_cfgelm_double, NULL, 1, 1,
505 _surf_cfg_cb__gtnets_jitter, NULL);
506 xbt_cfg_setdefault_double(_surf_cfg_set, "gtnets_jitter", 0.0);
508 default_value_int = 10;
509 xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter_seed",
510 "Use a positive seed to reproduce jitted results, value must be in [1,1e8], default is 10",
511 xbt_cfgelm_int, &default_value_int, 0, 1,
512 _surf_cfg_cb__gtnets_jitter_seed, NULL);
515 xbt_cfg_register(&_surf_cfg_set, "ns3/TcpModel",
516 "The ns3 tcp model can be : NewReno or Reno or Tahoe",
517 xbt_cfgelm_string, NULL, 1, 1,
519 xbt_cfg_setdefault_string(_surf_cfg_set, "ns3/TcpModel", "default");
522 /* retrieves the current directory of the current process */
523 const char *initial_path = __surf_get_initial_path();
524 xbt_assert((initial_path),
525 "__surf_get_initial_path() failed! Can't resolves current Windows directory");
527 surf_path = xbt_dynar_new(sizeof(char *), NULL);
528 xbt_cfg_setdefault_string(_surf_cfg_set, "path", initial_path);
532 surf_config_cmd_line(argc, argv);
534 XBT_WARN("Call to surf_config_init() after initialization ignored");
538 void surf_config_finalize(void)
540 if (!_surf_init_status)
541 return; /* Not initialized yet. Nothing to do */
543 xbt_cfg_free(&_surf_cfg_set);
544 _surf_init_status = 0;
547 /* Pick the right models for CPU, net and workstation, and call their model_init_preparse */
548 void surf_config_models_setup()
550 char *workstation_model_name;
551 int workstation_id = -1;
552 char *network_model_name = NULL;
553 char *cpu_model_name = NULL;
555 workstation_model_name =
556 xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
557 network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network/model");
558 cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu/model");
560 /* Check whether we use a net/cpu model differing from the default ones, in which case
561 * we should switch to the "compound" workstation model to correctly dispatch stuff to
562 * the right net/cpu models.
565 if((!xbt_cfg_is_default_value(_surf_cfg_set, "network/model") ||
566 !xbt_cfg_is_default_value(_surf_cfg_set, "cpu/model")) &&
567 xbt_cfg_is_default_value(_surf_cfg_set, "workstation/model"))
569 const char *val = "compound";
571 ("Switching workstation model to compound since you changed the network and/or cpu model(s)");
572 xbt_cfg_set_string(_surf_cfg_set, "workstation/model", val);
573 workstation_model_name = (char *) "compound";
576 XBT_DEBUG("Workstation model: %s", workstation_model_name);
578 find_model_description(surf_workstation_model_description,
579 workstation_model_name);
580 if (!strcmp(workstation_model_name, "compound")) {
584 xbt_assert(cpu_model_name,
585 "Set a cpu model to use with the 'compound' workstation model");
587 xbt_assert(network_model_name,
588 "Set a network model to use with the 'compound' workstation model");
591 find_model_description(surf_network_model_description,
594 find_model_description(surf_cpu_model_description, cpu_model_name);
596 surf_cpu_model_description[cpu_id].model_init_preparse();
597 surf_network_model_description[network_id].model_init_preparse();
600 XBT_DEBUG("Call workstation_model_init");
601 surf_workstation_model_description[workstation_id].model_init_preparse();