Logo AND Algorithmique Numérique Distribuée

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