Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f7d686ec9aa06be365025e9307744353a404e9ac
[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 extern int _surf_do_verbose_exit;
196
197 static void _surf_cfg_cb_verbose_exit(const char *name, int pos)
198 {
199   _surf_do_verbose_exit = xbt_cfg_get_int(_surf_cfg_set, name);
200 }
201
202
203 static void _surf_cfg_cb_context_factory(const char *name, int pos)
204 {
205   smx_context_factory_name = xbt_cfg_get_string(_surf_cfg_set, name);
206 }
207
208 static void _surf_cfg_cb_context_stack_size(const char *name, int pos)
209 {
210   smx_context_stack_size = xbt_cfg_get_int(_surf_cfg_set, name) * 1024;
211 }
212
213 static void _surf_cfg_cb_contexts_nthreads(const char *name, int pos)
214 {
215   SIMIX_context_set_nthreads(xbt_cfg_get_int(_surf_cfg_set, name));
216 }
217
218 static void _surf_cfg_cb_contexts_parallel_threshold(const char *name, int pos)
219 {
220   SIMIX_context_set_parallel_threshold(xbt_cfg_get_int(_surf_cfg_set, name));
221 }
222
223 static void _surf_cfg_cb__surf_network_fullduplex(const char *name,
224                                                   int pos)
225 {
226   sg_network_fullduplex = xbt_cfg_get_int(_surf_cfg_set, name);
227 }
228
229 #ifdef HAVE_GTNETS
230 static void _surf_cfg_cb__gtnets_jitter(const char *name, int pos)
231 {
232   sg_gtnets_jitter = xbt_cfg_get_double(_surf_cfg_set, name);
233 }
234
235 static void _surf_cfg_cb__gtnets_jitter_seed(const char *name, int pos)
236 {
237   sg_gtnets_jitter_seed = xbt_cfg_get_int(_surf_cfg_set, name);
238 }
239 #endif
240
241 /* create the config set, register what should be and parse the command line*/
242 void surf_config_init(int *argc, char **argv)
243 {
244   char *description = xbt_malloc(1024), *p = description;
245   char *default_value;
246   double double_default_value;
247   int default_value_int;
248   int i;
249
250   /* Create the configuration support */
251   if (_surf_init_status == 0) { /* Only create stuff if not already inited */
252     _surf_init_status = 1;
253
254     sprintf(description,
255             "The model to use for the CPU. Possible values: ");
256     p = description;
257     while (*(++p) != '\0');
258     for (i = 0; surf_cpu_model_description[i].name; i++)
259       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
260                    surf_cpu_model_description[i].name);
261     sprintf(p,
262             ".\n       (use 'help' as a value to see the long description of each model)");
263     default_value = xbt_strdup("Cas01");
264     xbt_cfg_register(&_surf_cfg_set,
265                      "cpu/model", description, xbt_cfgelm_string,
266                      &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
267
268     sprintf(description,
269             "The model to use for the network. Possible values: ");
270     p = description;
271     while (*(++p) != '\0');
272     for (i = 0; surf_network_model_description[i].name; i++)
273       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
274                    surf_network_model_description[i].name);
275     sprintf(p,
276             ".\n       (use 'help' as a value to see the long description of each model)");
277     default_value = xbt_strdup("LV08");
278     xbt_cfg_register(&_surf_cfg_set,
279                      "network/model", description, xbt_cfgelm_string,
280                      &default_value, 1, 1, &_surf_cfg_cb__network_model,
281                      NULL);
282
283     sprintf(description,
284             "The model to use for the workstation. Possible values: ");
285     p = description;
286     while (*(++p) != '\0');
287     for (i = 0; surf_workstation_model_description[i].name; i++)
288       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
289                    surf_workstation_model_description[i].name);
290     sprintf(p,
291             ".\n       (use 'help' as a value to see the long description of each model)");
292     default_value = xbt_strdup("CLM03");
293     xbt_cfg_register(&_surf_cfg_set,
294                      "workstation/model", description, xbt_cfgelm_string,
295                      &default_value, 1, 1,
296                      &_surf_cfg_cb__workstation_model, NULL);
297
298     xbt_free(description);
299
300     default_value = xbt_strdup("Full");
301     xbt_cfg_register(&_surf_cfg_set, "routing",
302                      "Model to use to store the routing information",
303                      xbt_cfgelm_string, &default_value, 1, 1, NULL, NULL);
304
305     xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
306                      "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)",
307                      xbt_cfgelm_double, NULL, 1, 1,
308                      _surf_cfg_cb__tcp_gamma, NULL);
309     xbt_cfg_setdefault_double(_surf_cfg_set, "TCP_gamma", 20000.0);
310
311     xbt_cfg_register(&_surf_cfg_set, "maxmin/precision",
312                      "Minimum retained action value when updating simulation",
313                      xbt_cfgelm_double, NULL, 1, 1, _surf_cfg_cb__maxmin_precision, NULL);
314     xbt_cfg_setdefault_double(_surf_cfg_set, "maxmin/precision", 0.00001); // FIXME use setdefault everywhere here!
315
316     /* The parameters of network models */
317
318     double_default_value = 0.0;
319     xbt_cfg_register(&_surf_cfg_set, "network/sender_gap",
320                      "Minimum gap between two overlapping sends",
321                      xbt_cfgelm_double, &double_default_value, 1, 1,
322                      _surf_cfg_cb__sender_gap, NULL);
323
324     double_default_value = 1.0;
325     xbt_cfg_register(&_surf_cfg_set, "network/latency_factor",
326                      "Correction factor to apply to the provided latency (default value set by network model)",
327                      xbt_cfgelm_double, &double_default_value, 1, 1,
328                      _surf_cfg_cb__latency_factor, NULL);
329     double_default_value = 1.0;
330     xbt_cfg_register(&_surf_cfg_set, "network/bandwidth_factor",
331                      "Correction factor to apply to the provided bandwidth (default value set by network model)",
332                      xbt_cfgelm_double, &double_default_value, 1, 1,
333                      _surf_cfg_cb__bandwidth_factor, NULL);
334     double_default_value = 0.0;
335     xbt_cfg_register(&_surf_cfg_set, "network/weight_S",
336                      "Correction factor to apply to the weight of competing streams(default value set by network model)",
337                      xbt_cfgelm_double, &double_default_value, 1, 1,
338                      _surf_cfg_cb__weight_S, NULL);
339
340     /* Inclusion path */
341     xbt_cfg_register(&_surf_cfg_set, "path",
342                      "Lookup path for inclusions in platform and deployment XML files",
343                      xbt_cfgelm_string, NULL, 0, 0,
344                      _surf_cfg_cb__surf_path, NULL);
345
346     default_value_int = 0;
347     xbt_cfg_register(&_surf_cfg_set, "maxmin_selective_update",
348                      "Update the constraint set propagating recursively to others constraints",
349                      xbt_cfgelm_int, &default_value_int, 0, 1,
350                      _surf_cfg_cb__surf_maxmin_selective_update, NULL);
351
352     /* do model-check */
353     default_value_int = 0;
354     xbt_cfg_register(&_surf_cfg_set, "model-check",
355                      "Activate the model-checking of the \"simulated\" system (EXPERIMENTAL -- msg only for now)",
356                      xbt_cfgelm_int, &default_value_int, 0, 1,
357                      _surf_cfg_cb_model_check, NULL);
358     
359     /*
360        FIXME: this function is not setting model-check to it's default value because
361        internally it calls to variable->cb_set that in this case is the function 
362        _surf_cfg_cb_model_check which sets it's value to 1 (instead of the default value 0)
363        xbt_cfg_set_int(_surf_cfg_set, "model-check", default_value_int); */
364
365     /* do verbose-exit */
366     default_value_int = 0;
367     xbt_cfg_register(&_surf_cfg_set, "verbose-exit",
368                      "Activate the \"do nothing\" mode in Ctrl-C",
369                      xbt_cfgelm_int, &default_value_int, 0, 1,
370                      _surf_cfg_cb_verbose_exit, NULL);
371     
372     
373     /* context factory */
374     default_value = xbt_strdup("ucontext");
375     xbt_cfg_register(&_surf_cfg_set, "contexts/factory",
376                      "Context factory to use in SIMIX (ucontext, thread or raw)",
377                      xbt_cfgelm_string, &default_value, 1, 1, _surf_cfg_cb_context_factory, NULL);
378
379     /* stack size of contexts in Ko */
380     default_value_int = 128;
381     xbt_cfg_register(&_surf_cfg_set, "contexts/stack_size",
382                      "Stack size of contexts in Ko (ucontext or raw only)",
383                      xbt_cfgelm_int, &default_value_int, 1, 1,
384                      _surf_cfg_cb_context_stack_size, NULL);
385
386     /* number of parallel threads for user processes */
387     default_value_int = 1;
388     xbt_cfg_register(&_surf_cfg_set, "contexts/nthreads",
389                      "Number of parallel threads for user contexts (EXPERIMENTAL)",
390                      xbt_cfgelm_int, &default_value_int, 1, 1,
391                      _surf_cfg_cb_contexts_nthreads, NULL);
392
393     /* minimal number of user contexts to be run in parallel */
394     default_value_int = 1;
395     xbt_cfg_register(&_surf_cfg_set, "contexts/parallel_threshold",
396         "Minimal number of user contexts to be run in parallel",
397         xbt_cfgelm_int, &default_value_int, 1, 1,
398         _surf_cfg_cb_contexts_parallel_threshold, NULL);
399
400     default_value_int = 0;
401     xbt_cfg_register(&_surf_cfg_set, "fullduplex",
402                      "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM03)",
403                      xbt_cfgelm_int, &default_value_int, 0, 1,
404                      _surf_cfg_cb__surf_network_fullduplex, NULL);
405     xbt_cfg_setdefault_int(_surf_cfg_set, "fullduplex", default_value_int);
406
407 #ifdef HAVE_GTNETS
408     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter",
409                      "Double value to oscillate the link latency, uniformly in random interval [-latency*gtnets_jitter,latency*gtnets_jitter)",
410                      xbt_cfgelm_double, NULL, 1, 1,
411                      _surf_cfg_cb__gtnets_jitter, NULL);
412     xbt_cfg_setdefault_double(_surf_cfg_set, "gtnets_jitter", 0.0);
413
414     default_value_int = 10;
415     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter_seed",
416                      "Use a positive seed to reproduce jitted results, value must be in [1,1e8], default is 10",
417                      xbt_cfgelm_int, &default_value_int, 0, 1,
418                      _surf_cfg_cb__gtnets_jitter_seed, NULL);
419 #endif
420
421     if (!surf_path) {
422       /* retrieves the current directory of the        current process */
423       const char *initial_path = __surf_get_initial_path();
424       xbt_assert((initial_path),
425                   "__surf_get_initial_path() failed! Can't resolves current Windows directory");
426
427       surf_path = xbt_dynar_new(sizeof(char *), NULL);
428       xbt_cfg_setdefault_string(_surf_cfg_set, "path", initial_path);
429     }
430
431
432     surf_config_cmd_line(argc, argv);
433   } else {
434     XBT_WARN("Call to surf_config_init() after initialization ignored");
435   }
436 }
437
438 void surf_config_finalize(void)
439 {
440   if (!_surf_init_status)
441     return;                     /* Not initialized yet. Nothing to do */
442
443   xbt_cfg_free(&_surf_cfg_set);
444   _surf_init_status = 0;
445 }
446
447 void surf_config_models_setup(const char *platform_file)
448 {
449   char *workstation_model_name;
450   int workstation_id = -1;
451   char *network_model_name = NULL;
452   char *cpu_model_name = NULL;
453
454   workstation_model_name =
455       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
456   network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network/model");
457   cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu/model");
458
459   /* Check whether we use a net/cpu model differing from the default ones, in which case
460    * we should switch to the "compound" workstation model to correctly dispatch stuff to
461    * the right net/cpu models.
462    */
463
464   if((!xbt_cfg_is_default_value(_surf_cfg_set, "network/model") ||
465           !xbt_cfg_is_default_value(_surf_cfg_set, "cpu/model")) &&
466           xbt_cfg_is_default_value(_surf_cfg_set, "workstation/model"))
467   {
468             const char *val = "compound";
469             XBT_INFO
470                 ("Switching workstation model to compound since you changed the network and/or cpu model(s)");
471             xbt_cfg_set_string(_surf_cfg_set, "workstation/model", val);
472             workstation_model_name = (char *) "compound";
473   }
474
475   XBT_DEBUG("Workstation model: %s", workstation_model_name);
476   workstation_id =
477       find_model_description(surf_workstation_model_description,
478                              workstation_model_name);
479   if (!strcmp(workstation_model_name, "compound")) {
480     int network_id = -1;
481     int cpu_id = -1;
482
483     xbt_assert(cpu_model_name,
484                 "Set a cpu model to use with the 'compound' workstation model");
485
486     xbt_assert(network_model_name,
487                 "Set a network model to use with the 'compound' workstation model");
488
489     network_id =
490         find_model_description(surf_network_model_description,
491                                network_model_name);
492     cpu_id =
493         find_model_description(surf_cpu_model_description, cpu_model_name);
494
495     surf_cpu_model_description[cpu_id].model_init_preparse(platform_file);
496     surf_network_model_description[network_id].model_init_preparse
497         (platform_file);
498   }
499
500   XBT_DEBUG("Call workstation_model_init");
501   surf_workstation_model_description[workstation_id].model_init_preparse
502       (platform_file);
503 }
504
505 void surf_config_models_create_elms(void)
506 {
507   char *workstation_model_name =
508       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
509   int workstation_id =
510       find_model_description(surf_workstation_model_description,
511                              workstation_model_name);
512   if (surf_workstation_model_description
513       [workstation_id].model_init_postparse != NULL)
514     surf_workstation_model_description[workstation_id].model_init_postparse
515         ();
516 }