Logo AND Algorithmique Numérique Distribuée

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