Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f43274849da39659cd29718beed374b8ad8eadf6
[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
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf,
14                                 "About the configuration of surf (and the rest of the simulation)");
15
16 xbt_cfg_t _surf_cfg_set = NULL;
17
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       DEBUG1("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_assert0(_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_assert0(_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_assert0(_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__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 int _surf_do_model_check = 0;   /* this variable is used accros the lib */
178
179 static void _surf_cfg_cb_model_check(const char *name, int pos)
180 {
181   _surf_do_model_check = 1;
182   /* Tell modules using mallocators that they shouldn't. MC don't like them */
183   xbt_fifo_preinit();
184   xbt_dict_preinit();
185 }
186
187 int _surf_parallel_contexts = 0;
188
189 static void _surf_cfg_cb_parallel_contexts(const char *name, int pos)
190 {
191   _surf_parallel_contexts = 1;
192 }
193
194 static void _surf_cfg_cb__surf_network_fullduplex(const char *name,
195                                                   int pos)
196 {
197   sg_network_fullduplex = xbt_cfg_get_int(_surf_cfg_set, name);
198 }
199
200 #ifdef HAVE_GTNETS
201 static void _surf_cfg_cb__gtnets_jitter(const char *name, int pos)
202 {
203   sg_gtnets_jitter = xbt_cfg_get_double(_surf_cfg_set, name);
204 }
205
206 static void _surf_cfg_cb__gtnets_jitter_seed(const char *name, int pos)
207 {
208   sg_gtnets_jitter_seed = xbt_cfg_get_int(_surf_cfg_set, name);
209 }
210 #endif
211
212 /* create the config set, register what should be and parse the command line*/
213 void surf_config_init(int *argc, char **argv)
214 {
215   char *description = xbt_malloc(1024), *p = description;
216   char *default_value;
217   double double_default_value;
218   int default_value_int;
219   int i;
220
221   /* Create the configuration support */
222   if (_surf_init_status == 0) { /* Only create stuff if not already inited */
223     _surf_init_status = 1;
224
225     sprintf(description,
226             "The model to use for the CPU. Possible values: ");
227     p = description;
228     while (*(++p) != '\0');
229     for (i = 0; surf_cpu_model_description[i].name; i++)
230       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
231                    surf_cpu_model_description[i].name);
232     sprintf(p,
233             ".\n       (use 'help' as a value to see the long description of each model)");
234     default_value = xbt_strdup("Cas01");
235     xbt_cfg_register(&_surf_cfg_set,
236                      "cpu/model", description, xbt_cfgelm_string,
237                      &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
238
239     sprintf(description,
240             "The model to use for the network. Possible values: ");
241     p = description;
242     while (*(++p) != '\0');
243     for (i = 0; surf_network_model_description[i].name; i++)
244       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
245                    surf_network_model_description[i].name);
246     sprintf(p,
247             ".\n       (use 'help' as a value to see the long description of each model)");
248     default_value = xbt_strdup("LV08");
249     xbt_cfg_register(&_surf_cfg_set,
250                      "network/model", description, xbt_cfgelm_string,
251                      &default_value, 1, 1, &_surf_cfg_cb__network_model,
252                      NULL);
253
254     sprintf(description,
255             "The model to use for the workstation. Possible values: ");
256     p = description;
257     while (*(++p) != '\0');
258     for (i = 0; surf_workstation_model_description[i].name; i++)
259       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
260                    surf_workstation_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("CLM03");
264     xbt_cfg_register(&_surf_cfg_set,
265                      "workstation/model", description, xbt_cfgelm_string,
266                      &default_value, 1, 1,
267                      &_surf_cfg_cb__workstation_model, NULL);
268
269     xbt_free(description);
270
271     default_value = xbt_strdup("Full");
272     xbt_cfg_register(&_surf_cfg_set, "routing",
273                      "Model to use to store the routing information",
274                      xbt_cfgelm_string, &default_value, 1, 1, NULL, NULL);
275
276     xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
277                      "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)",
278                      xbt_cfgelm_double, NULL, 1, 1,
279                      _surf_cfg_cb__tcp_gamma, NULL);
280     xbt_cfg_set_double(_surf_cfg_set, "TCP_gamma", 20000.0);
281
282     xbt_cfg_register(&_surf_cfg_set, "maxmin/precision",
283                      "Minimum retained action value when updating simulation",
284                      xbt_cfgelm_double, NULL, 1, 1, _surf_cfg_cb__maxmin_precision, NULL);
285     xbt_cfg_set_double(_surf_cfg_set, "maxmin/precision", 0.00001);
286
287     /* The parameters of network models */
288     double_default_value = 1.0;
289     xbt_cfg_register(&_surf_cfg_set, "network/latency_factor",
290                      "Correction factor to apply to the provided latency (default value set by network model)",
291                      xbt_cfgelm_double, &double_default_value, 1, 1,
292                      _surf_cfg_cb__latency_factor, NULL);
293     double_default_value = 1.0;
294     xbt_cfg_register(&_surf_cfg_set, "network/bandwidth_factor",
295                      "Correction factor to apply to the provided bandwidth (default value set by network model)",
296                      xbt_cfgelm_double, &double_default_value, 1, 1,
297                      _surf_cfg_cb__bandwidth_factor, NULL);
298     double_default_value = 0.0;
299     xbt_cfg_register(&_surf_cfg_set, "network/weight_S",
300                      "Correction factor to apply to the weight of competing streams(default value set by network model)",
301                      xbt_cfgelm_double, &double_default_value, 1, 1,
302                      _surf_cfg_cb__weight_S, NULL);
303
304     /* Inclusion path */
305     xbt_cfg_register(&_surf_cfg_set, "path",
306                      "Lookup path for inclusions in platform and deployment XML files",
307                      xbt_cfgelm_string, NULL, 0, 0,
308                      _surf_cfg_cb__surf_path, NULL);
309
310     default_value_int = 0;
311     xbt_cfg_register(&_surf_cfg_set, "maxmin_selective_update",
312                      "Update the constraint set propagating recursively to others constraints",
313                      xbt_cfgelm_int, &default_value_int, 0, 1,
314                      _surf_cfg_cb__surf_maxmin_selective_update, NULL);
315
316     /* do model-check */
317     default_value_int = 0;
318     xbt_cfg_register(&_surf_cfg_set, "model-check",
319                      "Activate the model-checking of the \"simulated\" system (EXPERIMENTAL -- msg only for now)",
320                      xbt_cfgelm_int, &default_value_int, 0, 1,
321                      _surf_cfg_cb_model_check, NULL);
322     /*
323        FIXME: this function is not setting model-check to it's default value because
324        internally it calls to variable->cb_set that in this case is the function 
325        _surf_cfg_cb_model_check which sets it's value to 1 (instead of the defalut value 0)
326        xbt_cfg_set_int(_surf_cfg_set, "model-check", default_value_int); */
327
328     /* parallel contexts */
329     default_value_int = 0;
330     xbt_cfg_register(&_surf_cfg_set, "parallel-contexts",
331                      "Activate the parallel execution of user contexts (EXPERIMENTAL -- pthreads only)",
332                      xbt_cfgelm_int, &default_value_int, 0, 1,
333                      _surf_cfg_cb_parallel_contexts, NULL);
334
335     default_value_int = 0;
336     xbt_cfg_register(&_surf_cfg_set, "fullduplex",
337                      "Update the constraint set propagating recursively to others constraints",
338                      xbt_cfgelm_int, &default_value_int, 0, 1,
339                      _surf_cfg_cb__surf_network_fullduplex, NULL);
340     xbt_cfg_set_int(_surf_cfg_set, "fullduplex", default_value_int);
341
342 #ifdef HAVE_GTNETS
343     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter",
344                      "Double value to oscillate the link latency, uniformly in random interval [-latency*gtnets_jitter,latency*gtnets_jitter)",
345                      xbt_cfgelm_double, NULL, 1, 1,
346                      _surf_cfg_cb__gtnets_jitter, NULL);
347     xbt_cfg_set_double(_surf_cfg_set, "gtnets_jitter", 0.0);
348
349     default_value_int = 10;
350     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter_seed",
351                      "Use a positive seed to reproduce jitted results, value must be in [1,1e8], default is 10",
352                      xbt_cfgelm_int, &default_value_int, 0, 1,
353                      _surf_cfg_cb__gtnets_jitter_seed, NULL);
354 #endif
355
356     if (!surf_path) {
357       /* retrieves the current directory of the        current process */
358       const char *initial_path = __surf_get_initial_path();
359       xbt_assert0((initial_path),
360                   "__surf_get_initial_path() failed! Can't resolves current Windows directory");
361
362       surf_path = xbt_dynar_new(sizeof(char *), NULL);
363       xbt_cfg_set_string(_surf_cfg_set, "path", initial_path);
364     }
365
366
367     surf_config_cmd_line(argc, argv);
368   } else {
369     WARN0("Call to surf_config_init() after initialization ignored");
370   }
371 }
372
373 void surf_config_finalize(void)
374 {
375   if (!_surf_init_status)
376     return;                     /* Not initialized yet. Nothing to do */
377
378   xbt_cfg_free(&_surf_cfg_set);
379   _surf_init_status = 0;
380 }
381
382 void surf_config_models_setup(const char *platform_file)
383 {
384   char *workstation_model_name;
385   int workstation_id = -1;
386   char *network_model_name = NULL;
387   char *cpu_model_name = NULL;
388
389   workstation_model_name =
390       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
391   network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network/model");
392   cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu/model");
393
394   /* Check whether we use a net/cpu model differing from the default ones, in which case
395    * we should switch to the "compound" workstation model to correctly dispatch stuff to
396    * the right net/cpu models.
397    */
398   if ((strcmp(network_model_name, "LV08")
399        || strcmp(cpu_model_name, "Cas01"))
400       && !strcmp(workstation_model_name, "CLM03")) {
401     const char *val = "compound";
402     INFO0
403         ("Switching workstation model to compound since you changed the network and/or cpu model(s)");
404     xbt_cfg_set_string(_surf_cfg_set, "workstation/model", val);
405     workstation_model_name = (char *) "compound";
406   }
407
408   DEBUG1("Workstation model: %s", workstation_model_name);
409   workstation_id =
410       find_model_description(surf_workstation_model_description,
411                              workstation_model_name);
412   if (!strcmp(workstation_model_name, "compound")) {
413     int network_id = -1;
414     int cpu_id = -1;
415
416     xbt_assert0(cpu_model_name,
417                 "Set a cpu model to use with the 'compound' workstation model");
418
419     xbt_assert0(network_model_name,
420                 "Set a network model to use with the 'compound' workstation model");
421
422     network_id =
423         find_model_description(surf_network_model_description,
424                                network_model_name);
425     cpu_id =
426         find_model_description(surf_cpu_model_description, cpu_model_name);
427
428     surf_cpu_model_description[cpu_id].model_init_preparse(platform_file);
429     surf_network_model_description[network_id].model_init_preparse
430         (platform_file);
431   }
432
433   DEBUG0("Call workstation_model_init");
434   surf_workstation_model_description[workstation_id].model_init_preparse
435       (platform_file);
436 }
437
438 void surf_config_models_create_elms(void)
439 {
440   char *workstation_model_name =
441       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
442   int workstation_id =
443       find_model_description(surf_workstation_model_description,
444                              workstation_model_name);
445   if (surf_workstation_model_description
446       [workstation_id].model_init_postparse != NULL)
447     surf_workstation_model_description[workstation_id].model_init_postparse
448         ();
449 }