Logo AND Algorithmique Numérique Distribuée

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