Logo AND Algorithmique Numérique Distribuée

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