Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Alternative implementation or parmap with busy waits instead of futexes
[simgrid.git] / src / surf / surf_config.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* surf_config: configuration infrastructure for the simulation world       */
8
9 #include "xbt/config.h"
10 #include "xbt/str.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"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf,
16                                 "About the configuration of surf (and the rest of the simulation)");
17
18 xbt_cfg_t _surf_cfg_set = NULL;
19
20 /* Parse the command line, looking for options */
21 static void surf_config_cmd_line(int *argc, char **argv)
22 {
23   int i, j;
24   char *opt;
25
26   for (i = 1; i < *argc; i++) {
27     int remove_it = 0;
28     if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
29       opt = strchr(argv[i], '=');
30       opt++;
31
32       xbt_cfg_set_parse(_surf_cfg_set, opt);
33       XBT_DEBUG("Did apply '%s' as config setting", opt);
34       remove_it = 1;
35     } else if (!strncmp(argv[i], "--cfg-help", strlen("--cfg-help") + 1) ||
36                !strncmp(argv[i], "--help", strlen("--help") + 1)) {
37       printf
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");
41 #ifdef HAVE_TRACING
42       printf("\nYou can also use --help-tracing to see the details of all tracing options known by this simulator.\n");
43 #endif
44       exit(0);
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       exit(0);
50 #ifdef HAVE_TRACING
51     } else if (!strncmp(argv[i], "--help-tracing", strlen("--help-tracing") + 1)) {
52       TRACE_help (1);
53       exit(0);
54 #endif
55     }
56     if (remove_it) {            /*remove this from argv */
57       for (j = i + 1; j < *argc; j++) {
58         argv[j - 1] = argv[j];
59       }
60
61       argv[j - 1] = NULL;
62       (*argc)--;
63       i--;                      /* compensate effect of next loop incrementation */
64     }
65   }
66 }
67
68
69 int _surf_init_status = 0;      /* 0: beginning of time;
70                                    1: pre-inited (cfg_set created);
71                                    2: inited (running) */
72
73 /* callback of the workstation/model variable */
74 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
75 {
76   char *val;
77
78   xbt_assert(_surf_init_status < 2,
79               "Cannot change the model after the initialization");
80
81   val = xbt_cfg_get_string(_surf_cfg_set, name);
82
83   if (!strcmp(val, "help")) {
84     model_help("workstation", surf_workstation_model_description);
85     exit(0);
86   }
87
88   /* Make sure that the model exists */
89   find_model_description(surf_workstation_model_description, val);
90 }
91
92 /* callback of the cpu/model variable */
93 static void _surf_cfg_cb__cpu_model(const char *name, int pos)
94 {
95   char *val;
96
97   xbt_assert(_surf_init_status < 2,
98               "Cannot change the model after the initialization");
99
100   val = xbt_cfg_get_string(_surf_cfg_set, name);
101
102   if (!strcmp(val, "help")) {
103     model_help("CPU", surf_cpu_model_description);
104     exit(0);
105   }
106
107   /* New Module missing */
108   find_model_description(surf_cpu_model_description, val);
109 }
110
111 /* callback of the workstation_model variable */
112 static void _surf_cfg_cb__network_model(const char *name, int pos)
113 {
114   char *val;
115
116   xbt_assert(_surf_init_status < 2,
117               "Cannot change the model after the initialization");
118
119   val = xbt_cfg_get_string(_surf_cfg_set, name);
120
121   if (!strcmp(val, "help")) {
122     model_help("network", surf_network_model_description);
123     exit(0);
124   }
125
126   /* New Module missing */
127   find_model_description(surf_network_model_description, val);
128 }
129
130
131 /* callbacks of the network models values */
132 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos)
133 {
134   sg_tcp_gamma = xbt_cfg_get_double(_surf_cfg_set, name);
135 }
136
137 static void _surf_cfg_cb__maxmin_precision(const char* name, int pos)
138 {
139   sg_maxmin_precision = xbt_cfg_get_double(_surf_cfg_set, name);
140 }
141
142 static void _surf_cfg_cb__sender_gap(const char* name, int pos)
143 {
144   sg_sender_gap = xbt_cfg_get_double(_surf_cfg_set, name);
145 }
146
147 static void _surf_cfg_cb__latency_factor(const char *name, int pos)
148 {
149   sg_latency_factor = xbt_cfg_get_double(_surf_cfg_set, name);
150 }
151
152 static void _surf_cfg_cb__bandwidth_factor(const char *name, int pos)
153 {
154   sg_bandwidth_factor = xbt_cfg_get_double(_surf_cfg_set, name);
155 }
156
157 static void _surf_cfg_cb__weight_S(const char *name, int pos)
158 {
159   sg_weight_S_parameter = xbt_cfg_get_double(_surf_cfg_set, name);
160 }
161
162 static void _surf_cfg_cb__surf_maxmin_selective_update(const char *name,
163                                                        int pos)
164 {
165   sg_maxmin_selective_update = xbt_cfg_get_int(_surf_cfg_set, name);
166 }
167
168 /* callback of the inclusion path */
169 static void _surf_cfg_cb__surf_path(const char *name, int pos)
170 {
171   char *path = xbt_cfg_get_string_at(_surf_cfg_set, name, pos);
172   xbt_dynar_push(surf_path, &path);
173 }
174
175 /* callback to decide if we want to use the model-checking */
176 #include "xbt_modinter.h"
177 extern int _surf_do_model_check;   /* this variable lives in xbt_main until I find a right location for it */
178
179 static void _surf_cfg_cb_model_check(const char *name, int pos)
180 {
181   _surf_do_model_check = xbt_cfg_get_int(_surf_cfg_set, name);
182
183   if (_surf_do_model_check) {
184     /* Tell modules using mallocators that they shouldn't. MC don't like them */
185     xbt_fifo_preinit();
186     xbt_dict_preinit();
187   }
188 }
189
190 extern int _surf_do_verbose_exit;
191
192 static void _surf_cfg_cb_verbose_exit(const char *name, int pos)
193 {
194   _surf_do_verbose_exit = xbt_cfg_get_int(_surf_cfg_set, name);
195 }
196
197
198 static void _surf_cfg_cb_context_factory(const char *name, int pos)
199 {
200   smx_context_factory_name = xbt_cfg_get_string(_surf_cfg_set, name);
201 }
202
203 static void _surf_cfg_cb_context_stack_size(const char *name, int pos)
204 {
205   smx_context_stack_size = xbt_cfg_get_int(_surf_cfg_set, name) * 1024;
206 }
207
208 static void _surf_cfg_cb_contexts_nthreads(const char *name, int pos)
209 {
210   SIMIX_context_set_nthreads(xbt_cfg_get_int(_surf_cfg_set, name));
211 }
212
213 static void _surf_cfg_cb_contexts_parallel_threshold(const char *name, int pos)
214 {
215   SIMIX_context_set_parallel_threshold(xbt_cfg_get_int(_surf_cfg_set, name));
216 }
217
218 static void _surf_cfg_cb_contexts_parallel_mode(const char *name, int pos)
219 {
220   const char* mode_name = xbt_cfg_get_string(_surf_cfg_set, name);
221   if (!strcmp(mode_name, "posix")) {
222     SIMIX_context_set_parallel_mode(XBT_PARMAP_POSIX);
223   }
224   else if (!strcmp(mode_name, "futex")) {
225     SIMIX_context_set_parallel_mode(XBT_PARMAP_FUTEX);
226   }
227   else if (!strcmp(mode_name, "busy_wait")) {
228     SIMIX_context_set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
229   }
230   else {
231     XBT_WARN("Command line setting of the parallel synchronization mode should "
232         "be one of \"posix\", \"futex\" or \"busy_wait\"");
233   }
234 }
235
236 static void _surf_cfg_cb__surf_network_coordinates(const char *name,
237                                                    int pos)
238 {
239   char *val = xbt_cfg_get_string(_surf_cfg_set, name);
240   if (!strcmp(val, "yes")) {
241     if (!COORD_HOST_LEVEL) {
242       COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
243       COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
244     }
245   } else if (!strcmp(val, "no")) {
246     if (COORD_HOST_LEVEL)
247       XBT_WARN("Setting of whether to use coordinate cannot be disabled once set.");
248   } else {
249     XBT_WARN("Command line setting of whether to use coordinates must be either \"yes\" or \"no\"");
250   }
251 }
252
253 static void _surf_cfg_cb__surf_network_fullduplex(const char *name,
254                                                   int pos)
255 {
256   sg_network_fullduplex = xbt_cfg_get_int(_surf_cfg_set, name);
257 }
258
259 #ifdef HAVE_GTNETS
260 static void _surf_cfg_cb__gtnets_jitter(const char *name, int pos)
261 {
262   sg_gtnets_jitter = xbt_cfg_get_double(_surf_cfg_set, name);
263 }
264
265 static void _surf_cfg_cb__gtnets_jitter_seed(const char *name, int pos)
266 {
267   sg_gtnets_jitter_seed = xbt_cfg_get_int(_surf_cfg_set, name);
268 }
269 #endif
270
271 /* create the config set, register what should be and parse the command line*/
272 void surf_config_init(int *argc, char **argv)
273 {
274   char *description = xbt_malloc(1024), *p = description;
275   char *default_value;
276   double double_default_value;
277   int default_value_int;
278   int i;
279
280   /* Create the configuration support */
281   if (_surf_init_status == 0) { /* Only create stuff if not already inited */
282     _surf_init_status = 1;
283
284     sprintf(description,
285             "The model to use for the CPU. Possible values: ");
286     p = description;
287     while (*(++p) != '\0');
288     for (i = 0; surf_cpu_model_description[i].name; i++)
289       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
290                    surf_cpu_model_description[i].name);
291     sprintf(p,
292             ".\n       (use 'help' as a value to see the long description of each model)");
293     default_value = xbt_strdup("Cas01");
294     xbt_cfg_register(&_surf_cfg_set,
295                      "cpu/model", description, xbt_cfgelm_string,
296                      &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
297
298     sprintf(description,
299             "The model to use for the network. Possible values: ");
300     p = description;
301     while (*(++p) != '\0');
302     for (i = 0; surf_network_model_description[i].name; i++)
303       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
304                    surf_network_model_description[i].name);
305     sprintf(p,
306             ".\n       (use 'help' as a value to see the long description of each model)");
307     default_value = xbt_strdup("LV08");
308     xbt_cfg_register(&_surf_cfg_set,
309                      "network/model", description, xbt_cfgelm_string,
310                      &default_value, 1, 1, &_surf_cfg_cb__network_model,
311                      NULL);
312
313     sprintf(description,
314             "The model to use for the workstation. Possible values: ");
315     p = description;
316     while (*(++p) != '\0');
317     for (i = 0; surf_workstation_model_description[i].name; i++)
318       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
319                    surf_workstation_model_description[i].name);
320     sprintf(p,
321             ".\n       (use 'help' as a value to see the long description of each model)");
322     default_value = xbt_strdup("CLM03");
323     xbt_cfg_register(&_surf_cfg_set,
324                      "workstation/model", description, xbt_cfgelm_string,
325                      &default_value, 1, 1,
326                      &_surf_cfg_cb__workstation_model, NULL);
327
328     xbt_free(description);
329
330     default_value = xbt_strdup("Full");
331     xbt_cfg_register(&_surf_cfg_set, "routing",
332                      "Model to use to store the routing information",
333                      xbt_cfgelm_string, &default_value, 1, 1, NULL, NULL);
334
335     xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
336                      "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)",
337                      xbt_cfgelm_double, NULL, 1, 1,
338                      _surf_cfg_cb__tcp_gamma, NULL);
339     xbt_cfg_setdefault_double(_surf_cfg_set, "TCP_gamma", 20000.0);
340
341     xbt_cfg_register(&_surf_cfg_set, "maxmin/precision",
342                      "Minimum retained action value when updating simulation",
343                      xbt_cfgelm_double, NULL, 1, 1, _surf_cfg_cb__maxmin_precision, NULL);
344     xbt_cfg_setdefault_double(_surf_cfg_set, "maxmin/precision", 0.00001); // FIXME use setdefault everywhere here!
345
346     /* The parameters of network models */
347
348     double_default_value = 0.0;
349     xbt_cfg_register(&_surf_cfg_set, "network/sender_gap",
350                      "Minimum gap between two overlapping sends",
351                      xbt_cfgelm_double, &double_default_value, 1, 1,
352                      _surf_cfg_cb__sender_gap, NULL);
353
354     double_default_value = 1.0;
355     xbt_cfg_register(&_surf_cfg_set, "network/latency_factor",
356                      "Correction factor to apply to the provided latency (default value set by network model)",
357                      xbt_cfgelm_double, &double_default_value, 1, 1,
358                      _surf_cfg_cb__latency_factor, NULL);
359     double_default_value = 1.0;
360     xbt_cfg_register(&_surf_cfg_set, "network/bandwidth_factor",
361                      "Correction factor to apply to the provided bandwidth (default value set by network model)",
362                      xbt_cfgelm_double, &double_default_value, 1, 1,
363                      _surf_cfg_cb__bandwidth_factor, NULL);
364     double_default_value = 0.0;
365     xbt_cfg_register(&_surf_cfg_set, "network/weight_S",
366                      "Correction factor to apply to the weight of competing streams(default value set by network model)",
367                      xbt_cfgelm_double, &double_default_value, 1, 1,
368                      _surf_cfg_cb__weight_S, NULL);
369
370     /* Inclusion path */
371     xbt_cfg_register(&_surf_cfg_set, "path",
372                      "Lookup path for inclusions in platform and deployment XML files",
373                      xbt_cfgelm_string, NULL, 0, 0,
374                      _surf_cfg_cb__surf_path, NULL);
375
376     default_value_int = 0;
377     xbt_cfg_register(&_surf_cfg_set, "maxmin_selective_update",
378                      "Update the constraint set propagating recursively to others constraints",
379                      xbt_cfgelm_int, &default_value_int, 0, 1,
380                      _surf_cfg_cb__surf_maxmin_selective_update, NULL);
381
382     /* do model-check */
383     default_value_int = 0;
384     xbt_cfg_register(&_surf_cfg_set, "model-check",
385                      "Activate the model-checking of the \"simulated\" system (EXPERIMENTAL -- msg only for now)",
386                      xbt_cfgelm_int, &default_value_int, 0, 1,
387                      _surf_cfg_cb_model_check, NULL);
388     
389     /*
390        FIXME: this function is not setting model-check to it's default value because
391        internally it calls to variable->cb_set that in this case is the function 
392        _surf_cfg_cb_model_check which sets it's value to 1 (instead of the default value 0)
393        xbt_cfg_set_int(_surf_cfg_set, "model-check", default_value_int); */
394
395     /* do verbose-exit */
396     default_value_int = 0;
397     xbt_cfg_register(&_surf_cfg_set, "verbose-exit",
398                      "Activate the \"do nothing\" mode in Ctrl-C",
399                      xbt_cfgelm_int, &default_value_int, 0, 1,
400                      _surf_cfg_cb_verbose_exit, NULL);
401     
402     
403     /* context factory */
404     default_value = xbt_strdup("ucontext");
405     xbt_cfg_register(&_surf_cfg_set, "contexts/factory",
406                      "Context factory to use in SIMIX (ucontext, thread or raw)",
407                      xbt_cfgelm_string, &default_value, 1, 1, _surf_cfg_cb_context_factory, NULL);
408
409     /* stack size of contexts in Ko */
410     default_value_int = 128;
411     xbt_cfg_register(&_surf_cfg_set, "contexts/stack_size",
412                      "Stack size of contexts in Ko (ucontext or raw only)",
413                      xbt_cfgelm_int, &default_value_int, 1, 1,
414                      _surf_cfg_cb_context_stack_size, NULL);
415
416     /* number of parallel threads for user processes */
417     default_value_int = 1;
418     xbt_cfg_register(&_surf_cfg_set, "contexts/nthreads",
419                      "Number of parallel threads for user contexts (EXPERIMENTAL)",
420                      xbt_cfgelm_int, &default_value_int, 1, 1,
421                      _surf_cfg_cb_contexts_nthreads, NULL);
422
423     /* minimal number of user contexts to be run in parallel */
424     default_value_int = 1;
425     xbt_cfg_register(&_surf_cfg_set, "contexts/parallel_threshold",
426         "Minimal number of user contexts to be run in parallel (raw contexts only)",
427         xbt_cfgelm_int, &default_value_int, 1, 1,
428         _surf_cfg_cb_contexts_parallel_threshold, NULL);
429
430     /* minimal number of user contexts to be run in parallel */
431     default_value = xbt_strdup("futex");
432     xbt_cfg_register(&_surf_cfg_set, "contexts/parallel_mode",
433         "Synchronization mode to use when running contexts in parallel",
434         xbt_cfgelm_string, &default_value, 1, 1,
435         _surf_cfg_cb_contexts_parallel_mode, NULL);
436
437     default_value = xbt_strdup("no");
438     xbt_cfg_register(&_surf_cfg_set, "coordinates",
439                      "\"yes\" or \"no\" (FIXME: document)",
440                      xbt_cfgelm_string, &default_value, 1, 1,
441                      _surf_cfg_cb__surf_network_coordinates, NULL);
442     xbt_cfg_setdefault_string(_surf_cfg_set, "coordinates", default_value);
443
444     default_value_int = 0;
445     xbt_cfg_register(&_surf_cfg_set, "fullduplex",
446                      "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)",
447                      xbt_cfgelm_int, &default_value_int, 0, 1,
448                      _surf_cfg_cb__surf_network_fullduplex, NULL);
449     xbt_cfg_setdefault_int(_surf_cfg_set, "fullduplex", default_value_int);
450
451 #ifdef HAVE_GTNETS
452     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter",
453                      "Double value to oscillate the link latency, uniformly in random interval [-latency*gtnets_jitter,latency*gtnets_jitter)",
454                      xbt_cfgelm_double, NULL, 1, 1,
455                      _surf_cfg_cb__gtnets_jitter, NULL);
456     xbt_cfg_setdefault_double(_surf_cfg_set, "gtnets_jitter", 0.0);
457
458     default_value_int = 10;
459     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter_seed",
460                      "Use a positive seed to reproduce jitted results, value must be in [1,1e8], default is 10",
461                      xbt_cfgelm_int, &default_value_int, 0, 1,
462                      _surf_cfg_cb__gtnets_jitter_seed, NULL);
463 #endif
464 #ifdef HAVE_NS3
465     xbt_cfg_register(&_surf_cfg_set, "ns3/TcpModel",
466                      "The ns3 tcp model can be : NewReno or Reno or Tahoe",
467                      xbt_cfgelm_string, NULL, 1, 1,
468                      NULL, NULL);
469     xbt_cfg_setdefault_string(_surf_cfg_set, "ns3/TcpModel", "default");
470 #endif
471     if (!surf_path) {
472       /* retrieves the current directory of the        current process */
473       const char *initial_path = __surf_get_initial_path();
474       xbt_assert((initial_path),
475                   "__surf_get_initial_path() failed! Can't resolves current Windows directory");
476
477       surf_path = xbt_dynar_new(sizeof(char *), NULL);
478       xbt_cfg_setdefault_string(_surf_cfg_set, "path", initial_path);
479     }
480
481
482     surf_config_cmd_line(argc, argv);
483   } else {
484     XBT_WARN("Call to surf_config_init() after initialization ignored");
485   }
486 }
487
488 void surf_config_finalize(void)
489 {
490   if (!_surf_init_status)
491     return;                     /* Not initialized yet. Nothing to do */
492
493   xbt_cfg_free(&_surf_cfg_set);
494   _surf_init_status = 0;
495 }
496
497 /* Pick the right models for CPU, net and workstation, and call their model_init_preparse */
498 void surf_config_models_setup()
499 {
500   char *workstation_model_name;
501   int workstation_id = -1;
502   char *network_model_name = NULL;
503   char *cpu_model_name = NULL;
504
505   workstation_model_name =
506       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
507   network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network/model");
508   cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu/model");
509
510   /* Check whether we use a net/cpu model differing from the default ones, in which case
511    * we should switch to the "compound" workstation model to correctly dispatch stuff to
512    * the right net/cpu models.
513    */
514
515   if((!xbt_cfg_is_default_value(_surf_cfg_set, "network/model") ||
516           !xbt_cfg_is_default_value(_surf_cfg_set, "cpu/model")) &&
517           xbt_cfg_is_default_value(_surf_cfg_set, "workstation/model"))
518   {
519             const char *val = "compound";
520             XBT_INFO
521                 ("Switching workstation model to compound since you changed the network and/or cpu model(s)");
522             xbt_cfg_set_string(_surf_cfg_set, "workstation/model", val);
523             workstation_model_name = (char *) "compound";
524   }
525
526   XBT_DEBUG("Workstation model: %s", workstation_model_name);
527   workstation_id =
528       find_model_description(surf_workstation_model_description,
529                              workstation_model_name);
530   if (!strcmp(workstation_model_name, "compound")) {
531     int network_id = -1;
532     int cpu_id = -1;
533
534     xbt_assert(cpu_model_name,
535                 "Set a cpu model to use with the 'compound' workstation model");
536
537     xbt_assert(network_model_name,
538                 "Set a network model to use with the 'compound' workstation model");
539
540     network_id =
541         find_model_description(surf_network_model_description,
542                                network_model_name);
543     cpu_id =
544         find_model_description(surf_cpu_model_description, cpu_model_name);
545
546     surf_cpu_model_description[cpu_id].model_init_preparse();
547     surf_network_model_description[network_id].model_init_preparse();
548   }
549
550   XBT_DEBUG("Call workstation_model_init");
551   surf_workstation_model_description[workstation_id].model_init_preparse();
552 }