Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Delete the temporary route table for model Full.
[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       exit(0);
42     } else
43         if (!strncmp
44             (argv[i], "--help-models", strlen("--help-models") + 1)) {
45       model_help("workstation", surf_workstation_model_description);
46       model_help("CPU", surf_cpu_model_description);
47       model_help("network", surf_network_model_description);
48       exit(0);
49     }
50     if (remove_it) {            /*remove this from argv */
51       for (j = i + 1; j < *argc; j++) {
52         argv[j - 1] = argv[j];
53       }
54
55       argv[j - 1] = NULL;
56       (*argc)--;
57       i--;                      /* compensate effect of next loop incrementation */
58     }
59   }
60 }
61
62
63 int _surf_init_status = 0;      /* 0: beginning of time;
64                                    1: pre-inited (cfg_set created);
65                                    2: inited (running) */
66
67 /* callback of the workstation/model variable */
68 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
69 {
70   char *val;
71
72   xbt_assert0(_surf_init_status < 2,
73               "Cannot change the model after the initialization");
74
75   val = xbt_cfg_get_string(_surf_cfg_set, name);
76
77   if (!strcmp(val, "help")) {
78     model_help("workstation", surf_workstation_model_description);
79     exit(0);
80   }
81
82   /* Make sure that the model exists */
83   find_model_description(surf_workstation_model_description, val);
84 }
85
86 /* callback of the cpu/model variable */
87 static void _surf_cfg_cb__cpu_model(const char *name, int pos)
88 {
89   char *val;
90
91   xbt_assert0(_surf_init_status < 2,
92               "Cannot change the model after the initialization");
93
94   val = xbt_cfg_get_string(_surf_cfg_set, name);
95
96   if (!strcmp(val, "help")) {
97     model_help("CPU", surf_cpu_model_description);
98     exit(0);
99   }
100
101   /* New Module missing */
102   find_model_description(surf_cpu_model_description, val);
103 }
104
105 /* callback of the workstation_model variable */
106 static void _surf_cfg_cb__network_model(const char *name, int pos)
107 {
108   char *val;
109
110   xbt_assert0(_surf_init_status < 2,
111               "Cannot change the model after the initialization");
112
113   val = xbt_cfg_get_string(_surf_cfg_set, name);
114
115   if (!strcmp(val, "help")) {
116     model_help("network", surf_network_model_description);
117     exit(0);
118   }
119
120   /* New Module missing */
121   find_model_description(surf_network_model_description, val);
122 }
123
124
125 /* callbacks of the network models values */
126 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos)
127 {
128   sg_tcp_gamma = xbt_cfg_get_double(_surf_cfg_set, name);
129 }
130
131 static void _surf_cfg_cb__latency_factor(const char *name, int pos)
132 {
133   sg_latency_factor = xbt_cfg_get_double(_surf_cfg_set, name);
134 }
135
136 static void _surf_cfg_cb__bandwidth_factor(const char *name, int pos)
137 {
138   sg_bandwidth_factor = xbt_cfg_get_double(_surf_cfg_set, name);
139 }
140
141 static void _surf_cfg_cb__weight_S(const char *name, int pos)
142 {
143   sg_weight_S_parameter = xbt_cfg_get_double(_surf_cfg_set, name);
144 }
145
146 static void _surf_cfg_cb__surf_maxmin_selective_update(const char *name,
147                                                        int pos)
148 {
149   sg_maxmin_selective_update = xbt_cfg_get_int(_surf_cfg_set, name);
150 }
151
152 /* callback of the inclusion path */
153 static void _surf_cfg_cb__surf_path(const char *name, int pos)
154 {
155   char *path = xbt_cfg_get_string_at(_surf_cfg_set, name, pos);
156   xbt_dynar_push(surf_path, &path);
157 }
158
159 /* callback to decide if we want to use the model-checking */
160 #include "xbt_modinter.h"
161 int _surf_do_model_check = 0;   /* this variable is used accros the lib */
162
163 static void _surf_cfg_cb_model_check(const char *name, int pos)
164 {
165   _surf_do_model_check = 1;
166   /* Tell modules using mallocators that they shouldn't. MC don't like them */
167   xbt_fifo_preinit();
168   xbt_dict_preinit();
169 }
170
171 static void _surf_cfg_cb__surf_network_fullduplex(const char *name,
172                                                   int pos)
173 {
174   sg_network_fullduplex = xbt_cfg_get_int(_surf_cfg_set, name);
175 }
176
177 #ifdef HAVE_GTNETS
178 static void _surf_cfg_cb__gtnets_jitter(const char *name, int pos)
179 {
180   sg_gtnets_jitter = xbt_cfg_get_double(_surf_cfg_set, name);
181 }
182
183 static void _surf_cfg_cb__gtnets_jitter_seed(const char *name, int pos)
184 {
185   sg_gtnets_jitter_seed = xbt_cfg_get_int(_surf_cfg_set, name);
186 }
187 #endif
188
189 /* create the config set, register what should be and parse the command line*/
190 void surf_config_init(int *argc, char **argv)
191 {
192   char *description = xbt_malloc(1024), *p = description;
193   char *default_value;
194   double double_default_value;
195   int default_value_int;
196   int i;
197
198   /* Create the configuration support */
199   if (_surf_init_status == 0) { /* Only create stuff if not already inited */
200     _surf_init_status = 1;
201
202     sprintf(description,
203             "The model to use for the CPU. Possible values: ");
204     p = description;
205     while (*(++p) != '\0');
206     for (i = 0; surf_cpu_model_description[i].name; i++)
207       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
208                    surf_cpu_model_description[i].name);
209     sprintf(p,
210             ".\n       (use 'help' as a value to see the long description of each model)");
211     default_value = xbt_strdup("Cas01");
212     xbt_cfg_register(&_surf_cfg_set,
213                      "cpu/model", description, xbt_cfgelm_string,
214                      &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
215
216     sprintf(description,
217             "The model to use for the network. Possible values: ");
218     p = description;
219     while (*(++p) != '\0');
220     for (i = 0; surf_network_model_description[i].name; i++)
221       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
222                    surf_network_model_description[i].name);
223     sprintf(p,
224             ".\n       (use 'help' as a value to see the long description of each model)");
225     default_value = xbt_strdup("LV08");
226     xbt_cfg_register(&_surf_cfg_set,
227                      "network/model", description, xbt_cfgelm_string,
228                      &default_value, 1, 1, &_surf_cfg_cb__network_model,
229                      NULL);
230
231     sprintf(description,
232             "The model to use for the workstation. Possible values: ");
233     p = description;
234     while (*(++p) != '\0');
235     for (i = 0; surf_workstation_model_description[i].name; i++)
236       p += sprintf(p, "%s%s", (i == 0 ? "" : ", "),
237                    surf_workstation_model_description[i].name);
238     sprintf(p,
239             ".\n       (use 'help' as a value to see the long description of each model)");
240     default_value = xbt_strdup("CLM03");
241     xbt_cfg_register(&_surf_cfg_set,
242                      "workstation/model", description, xbt_cfgelm_string,
243                      &default_value, 1, 1,
244                      &_surf_cfg_cb__workstation_model, NULL);
245
246     xbt_free(description);
247
248     default_value = xbt_strdup("Full");
249     xbt_cfg_register(&_surf_cfg_set, "routing",
250                      "Model to use to store the routing information",
251                      xbt_cfgelm_string, &default_value, 1, 1, NULL, NULL);
252
253     xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
254                      "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)",
255                      xbt_cfgelm_double, NULL, 1, 1,
256                      _surf_cfg_cb__tcp_gamma, NULL);
257     xbt_cfg_set_double(_surf_cfg_set, "TCP_gamma", 20000.0);
258
259     /* The parameters of network models */
260     double_default_value = 1.0;
261     xbt_cfg_register(&_surf_cfg_set, "network/latency_factor",
262                      "Correction factor to apply to the provided latency (default value set by network model)",
263                      xbt_cfgelm_double, &double_default_value, 1, 1,
264                      _surf_cfg_cb__latency_factor, NULL);
265     double_default_value = 1.0;
266     xbt_cfg_register(&_surf_cfg_set, "network/bandwidth_factor",
267                      "Correction factor to apply to the provided bandwidth (default value set by network model)",
268                      xbt_cfgelm_double, &double_default_value, 1, 1,
269                      _surf_cfg_cb__bandwidth_factor, NULL);
270     double_default_value = 0.0;
271     xbt_cfg_register(&_surf_cfg_set, "network/weight_S",
272                      "Correction factor to apply to the weight of competing streams(default value set by network model)",
273                      xbt_cfgelm_double, &double_default_value, 1, 1,
274                      _surf_cfg_cb__weight_S, NULL);
275
276     /* Inclusion path */
277     xbt_cfg_register(&_surf_cfg_set, "path",
278                      "Lookup path for inclusions in platform and deployment XML files",
279                      xbt_cfgelm_string, NULL, 0, 0,
280                      _surf_cfg_cb__surf_path, NULL);
281
282     default_value_int = 0;
283     xbt_cfg_register(&_surf_cfg_set, "maxmin_selective_update",
284                      "Update the constraint set propagating recursively to others constraints",
285                      xbt_cfgelm_int, &default_value_int, 0, 1,
286                      _surf_cfg_cb__surf_maxmin_selective_update, NULL);
287
288     /* do model-check */
289     default_value_int = 0;
290     xbt_cfg_register(&_surf_cfg_set, "model-check",
291                      "Activate the model-checking of the \"simulated\" system (EXPERIMENTAL -- msg only for now)",
292                      xbt_cfgelm_int, &default_value_int, 0, 1,
293                      _surf_cfg_cb_model_check, NULL);
294     /*
295        FIXME: this function is not setting model-check to it's default value because
296        internally it calls to variable->cb_set that in this case is the function 
297        _surf_cfg_cb_model_check which sets it's value to 1 (instead of the defalut value 0)
298        xbt_cfg_set_int(_surf_cfg_set, "model-check", default_value_int); */
299
300     default_value_int = 0;
301     xbt_cfg_register(&_surf_cfg_set, "fullduplex",
302                      "Update the constraint set propagating recursively to others constraints",
303                      xbt_cfgelm_int, &default_value_int, 0, 1,
304                      _surf_cfg_cb__surf_network_fullduplex, NULL);
305     xbt_cfg_set_int(_surf_cfg_set, "fullduplex", default_value_int);
306
307 #ifdef HAVE_GTNETS
308     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter",
309                      "Double value to oscillate the link latency, uniformly in random interval [-latency*gtnets_jitter,latency*gtnets_jitter)",
310                      xbt_cfgelm_double, NULL, 1, 1,
311                      _surf_cfg_cb__gtnets_jitter, NULL);
312     xbt_cfg_set_double(_surf_cfg_set, "gtnets_jitter", 0.0);
313
314     default_value_int = 10;
315     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter_seed",
316                      "Use a positive seed to reproduce jitted results, value must be in [1,1e8], default is 10",
317                      xbt_cfgelm_int, &default_value_int, 0, 1,
318                      _surf_cfg_cb__gtnets_jitter_seed, NULL);
319 #endif
320
321     if (!surf_path) {
322       /* retrieves the current directory of the        current process */
323       const char *initial_path = __surf_get_initial_path();
324       xbt_assert0((initial_path),
325                   "__surf_get_initial_path() failed! Can't resolves current Windows directory");
326
327       surf_path = xbt_dynar_new(sizeof(char *), NULL);
328       xbt_cfg_set_string(_surf_cfg_set, "path", initial_path);
329     }
330
331
332     surf_config_cmd_line(argc, argv);
333   } else {
334     WARN0("Call to surf_config_init() after initialization ignored");
335   }
336 }
337
338 void surf_config_finalize(void)
339 {
340   if (!_surf_init_status)
341     return;                     /* Not initialized yet. Nothing to do */
342
343   xbt_cfg_free(&_surf_cfg_set);
344   _surf_init_status = 0;
345 }
346
347 void surf_config_models_setup(const char *platform_file)
348 {
349   char *workstation_model_name;
350   int workstation_id = -1;
351   char *network_model_name = NULL;
352   char *cpu_model_name = NULL;
353   surf_timer_model_init(platform_file);
354
355   workstation_model_name =
356       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
357   network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network/model");
358   cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu/model");
359
360   /* Check whether we use a net/cpu model differing from the default ones, in which case
361    * we should switch to the "compound" workstation model to correctly dispatch stuff to
362    * the right net/cpu models.
363    */
364   if ((strcmp(network_model_name, "LV08")
365        || strcmp(cpu_model_name, "Cas01"))
366       && !strcmp(workstation_model_name, "CLM03")) {
367     const char *val = "compound";
368     INFO0
369         ("Switching workstation model to compound since you changed the network and/or cpu model(s)");
370     xbt_cfg_set_string(_surf_cfg_set, "workstation/model", val);
371     workstation_model_name = (char *) "compound";
372   }
373
374   DEBUG1("Workstation model: %s", workstation_model_name);
375   workstation_id =
376       find_model_description(surf_workstation_model_description,
377                              workstation_model_name);
378   if (!strcmp(workstation_model_name, "compound")) {
379     int network_id = -1;
380     int cpu_id = -1;
381
382     xbt_assert0(cpu_model_name,
383                 "Set a cpu model to use with the 'compound' workstation model");
384
385     xbt_assert0(network_model_name,
386                 "Set a network model to use with the 'compound' workstation model");
387
388     network_id =
389         find_model_description(surf_network_model_description,
390                                network_model_name);
391     cpu_id =
392         find_model_description(surf_cpu_model_description, cpu_model_name);
393
394     surf_cpu_model_description[cpu_id].model_init_preparse(platform_file);
395     surf_network_model_description[network_id].model_init_preparse
396         (platform_file);
397   }
398
399   DEBUG0("Call workstation_model_init");
400   surf_workstation_model_description[workstation_id].model_init_preparse
401       (platform_file);
402 }
403
404 void surf_config_models_create_elms(void)
405 {
406   char *workstation_model_name =
407       xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
408   int workstation_id =
409       find_model_description(surf_workstation_model_description,
410                              workstation_model_name);
411   if (surf_workstation_model_description
412       [workstation_id].model_init_postparse != NULL)
413     surf_workstation_model_description[workstation_id].model_init_postparse
414         ();
415 }