Logo AND Algorithmique Numérique Distribuée

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