Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
60da0de93759cda2b92f8a30cc6a382e0c334d54
[simgrid.git] / src / surf / surf_config.c
1 /* Copyright (c) 2009-2010 The SimGrid team. All rights reserved.           */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* surf_config: configuration infrastructure for the simulation world       */
7
8 #include "xbt/config.h"
9 #include "xbt/str.h"
10 #include "surf/surf_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf,
13                                 "About the configuration of surf (and the rest of the simulation)");
14
15 xbt_cfg_t _surf_cfg_set = NULL;
16
17
18 /* Parse the command line, looking for options */
19 static void surf_config_cmd_line(int *argc, char **argv)
20 {
21   int i, j;
22   char *opt;
23
24   for (i = 1; i < *argc; i++) {
25     int remove_it = 0;
26     if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
27       opt = strchr(argv[i], '=');
28       opt++;
29
30       xbt_cfg_set_parse(_surf_cfg_set, opt);
31       DEBUG1("Did apply '%s' as config setting", opt);
32       remove_it = 1;
33     } else if (!strncmp(argv[i], "--cfg-help", strlen("--cfg-help") + 1) ||
34                !strncmp(argv[i], "--help", strlen("--help") + 1)) {
35       printf("Description of the configuration accepted by this simulator:\n");
36       xbt_cfg_help(_surf_cfg_set);
37       remove_it = 1;
38       exit(0);
39     }
40     if (remove_it) {            /*remove this from argv */
41       for (j = i + 1; j < *argc; j++) {
42         argv[j - 1] = argv[j];
43       }
44
45       argv[j - 1] = NULL;
46       (*argc)--;
47       i--;                      /* compensate effect of next loop incrementation */
48     }
49   }
50 }
51
52
53 int _surf_init_status = 0;      /* 0: beginning of time;
54                                    1: pre-inited (cfg_set created);
55                                    2: inited (running) */
56
57 /* callback of the workstation/model variable */
58 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
59 {
60   char *val;
61
62   xbt_assert0(_surf_init_status < 2,
63               "Cannot change the model after the initialization");
64
65   val = xbt_cfg_get_string(_surf_cfg_set, name);
66
67   if (!strcmp(val,"help"))
68     model_help("workstation",surf_workstation_model_description);
69
70   /* Make sure that the model exists */
71   find_model_description(surf_workstation_model_description, val);
72 }
73
74 /* callback of the cpu/model variable */
75 static void _surf_cfg_cb__cpu_model(const char *name, int pos)
76 {
77   char *val;
78
79   xbt_assert0(_surf_init_status < 2,
80               "Cannot change the model after the initialization");
81
82   val = xbt_cfg_get_string(_surf_cfg_set, name);
83
84   if (!strcmp(val,"help"))
85     model_help("CPU",surf_cpu_model_description);
86
87   /* New Module missing */
88   find_model_description(surf_cpu_model_description, val);
89 }
90
91 /* callback of the workstation_model variable */
92 static void _surf_cfg_cb__network_model(const char *name, int pos)
93 {
94   char *val;
95
96   xbt_assert0(_surf_init_status < 2,
97               "Cannot change the model after the initialization");
98
99   val = xbt_cfg_get_string(_surf_cfg_set, name);
100
101   if (!strcmp(val,"help"))
102     model_help("network",surf_network_model_description);
103
104   /* New Module missing */
105   find_model_description(surf_network_model_description, val);
106 }
107
108
109 /* callbacks of the network models values */
110 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos) {
111   sg_tcp_gamma = xbt_cfg_get_double(_surf_cfg_set, name);
112 }
113 static void _surf_cfg_cb__latency_factor(const char *name, int pos) {
114   sg_latency_factor = xbt_cfg_get_double(_surf_cfg_set, name);
115 }
116 static void _surf_cfg_cb__bandwidth_factor(const char *name, int pos) {
117   sg_bandwidth_factor = xbt_cfg_get_double(_surf_cfg_set, name);
118 }
119 static void _surf_cfg_cb__weight_S(const char *name, int pos) {
120   sg_weight_S_parameter = xbt_cfg_get_double(_surf_cfg_set, name);
121 }
122 static void _surf_cfg_cb__surf_maxmin_selective_update(const char *name, int pos) {
123         sg_maxmin_selective_update = xbt_cfg_get_int(_surf_cfg_set, name);
124 }
125
126 /* callback of the inclusion path */
127 static void _surf_cfg_cb__surf_path(const char *name, int pos) {
128   char *path = xbt_cfg_get_string_at(_surf_cfg_set, name, pos);
129   xbt_dynar_push(surf_path, &path);
130 }
131
132
133 #ifdef HAVE_GTNETS
134 static void _surf_cfg_cb__gtnets_jitter(const char *name, int pos){
135         sg_gtnets_jitter = xbt_cfg_get_double(_surf_cfg_set, name);
136 }
137 static void _surf_cfg_cb__gtnets_jitter_seed(const char *name, int pos){
138         sg_gtnets_jitter_seed = xbt_cfg_get_int(_surf_cfg_set, name);
139 }
140 #endif
141
142 /* create the config set, register what should be and parse the command line*/
143 void surf_config_init(int *argc, char **argv)
144 {
145
146   /* Create the configuration support */
147   if (_surf_init_status == 0) { /* Only create stuff if not already inited */
148     _surf_init_status = 1;
149
150     char *description = xbt_malloc(1024), *p = description;
151     char *default_value;
152     double double_default_value;
153         int default_value_int;
154     int i;
155
156     sprintf(description, "The model to use for the CPU. Possible values: ");
157     p = description;
158     while (*(++p) != '\0');
159     for (i = 0; surf_cpu_model_description[i].name; i++)
160       p +=
161         sprintf(p, "%s%s", (i == 0 ? "" : ", "),
162                 surf_cpu_model_description[i].name);
163     sprintf(p,".\n       (use 'help' as a value to see the long description of each model)");
164     default_value = xbt_strdup("Cas01");
165     xbt_cfg_register(&_surf_cfg_set,
166                      "cpu/model", description, xbt_cfgelm_string,
167                      &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
168
169     sprintf(description,
170             "The model to use for the network. Possible values: ");
171     p = description;
172     while (*(++p) != '\0');
173     for (i = 0; surf_network_model_description[i].name; i++)
174       p +=
175         sprintf(p, "%s%s", (i == 0 ? "" : ", "),
176                 surf_network_model_description[i].name);
177     sprintf(p,".\n       (use 'help' as a value to see the long description of each model)");
178     default_value = xbt_strdup("LV08");
179     xbt_cfg_register(&_surf_cfg_set,
180                      "network/model", description, xbt_cfgelm_string,
181                      &default_value, 1, 1, &_surf_cfg_cb__network_model,
182                      NULL);
183
184     sprintf(description,
185             "The model to use for the workstation. Possible values: ");
186     p = description;
187     while (*(++p) != '\0');
188     for (i = 0; surf_workstation_model_description[i].name; i++)
189       p +=
190         sprintf(p, "%s%s", (i == 0 ? "" : ", "),
191                 surf_workstation_model_description[i].name);
192     sprintf(p,".\n       (use 'help' as a value to see the long description of each model)");
193     default_value = xbt_strdup("CLM03");
194     xbt_cfg_register(&_surf_cfg_set,
195                      "workstation/model", description, xbt_cfgelm_string,
196                      &default_value, 1, 1, &_surf_cfg_cb__workstation_model,
197                      NULL);
198
199     xbt_free(description);
200
201     default_value = xbt_strdup("Full");
202     xbt_cfg_register(&_surf_cfg_set, "routing",
203                      "Model to use to store the routing information",
204                      xbt_cfgelm_string, &default_value, 1, 1, NULL, NULL);
205
206     xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
207                      "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; middle value=default value => you probably want default send value)",
208                      xbt_cfgelm_double, NULL, 1, 1, _surf_cfg_cb__tcp_gamma, NULL);
209     xbt_cfg_set_double(_surf_cfg_set, "TCP_gamma", 20000.0);
210
211     /* The parameters of network models */
212     double_default_value = 1.0;
213     xbt_cfg_register(&_surf_cfg_set, "network/latency_factor",
214                      "Correction factor to apply to the provided latency (default value set by network model)",
215                      xbt_cfgelm_double, &double_default_value, 1, 1, _surf_cfg_cb__latency_factor, NULL);
216     double_default_value = 1.0;
217     xbt_cfg_register(&_surf_cfg_set, "network/bandwidth_factor",
218                      "Correction factor to apply to the provided bandwidth (default value set by network model)",
219                      xbt_cfgelm_double, &double_default_value, 1, 1, _surf_cfg_cb__bandwidth_factor, NULL);
220     double_default_value = 0.0;
221     xbt_cfg_register(&_surf_cfg_set, "network/weight_S",
222                      "Correction factor to apply to the weight of competing streams(default value set by network model)",
223                      xbt_cfgelm_double, &double_default_value, 1, 1, _surf_cfg_cb__weight_S, NULL);
224
225     /* Inclusion path */
226     xbt_cfg_register(&_surf_cfg_set, "path",
227                      "Lookup path for inclusions in platform and deployment XML files",
228                      xbt_cfgelm_string, NULL, 0, 0, _surf_cfg_cb__surf_path,
229                      NULL);
230
231                 default_value_int = 0;
232     xbt_cfg_register(&_surf_cfg_set, "maxmin_selective_update",
233                      "Update the constraint set propagating recursively to others constraints",
234                      xbt_cfgelm_int, &default_value_int, 0, 1, _surf_cfg_cb__surf_maxmin_selective_update, NULL);
235
236 #ifdef HAVE_GTNETS
237     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter",
238                      "Double value to oscillate the link latency, uniformly in random interval [-latency*gtnets_jitter,latency*gtnets_jitter)", xbt_cfgelm_double,
239                      NULL, 1, 1, _surf_cfg_cb__gtnets_jitter, NULL);
240     xbt_cfg_set_double(_surf_cfg_set, "gtnets_jitter", 0.0);
241
242     default_value_int = 10;
243     xbt_cfg_register(&_surf_cfg_set, "gtnets_jitter_seed",
244                      "Use a positive seed to reproduce jitted results, value must be in [1,1e8], default is 10",
245                      xbt_cfgelm_int, &default_value_int, 0, 1, _surf_cfg_cb__gtnets_jitter_seed, NULL);
246 #endif
247
248     if (!surf_path) {
249       /* retrieves the current directory of the        current process */
250       const char *initial_path = __surf_get_initial_path();
251       xbt_assert0((initial_path),
252                   "__surf_get_initial_path() failed! Can't resolves current Windows directory");
253
254       surf_path = xbt_dynar_new(sizeof(char *), NULL);
255       xbt_cfg_set_string(_surf_cfg_set, "path", initial_path);
256     }
257
258
259     surf_config_cmd_line(argc, argv);
260   } else {
261     WARN0("Call to surf_config_init() after initialization ignored");
262   }
263 }
264
265 void surf_config_finalize(void)
266 {
267   if (!_surf_init_status)
268     return;                     /* Not initialized yet. Nothing to do */
269
270   xbt_cfg_free(&_surf_cfg_set);
271   _surf_init_status = 0;
272 }
273
274 void surf_config_models_setup(const char *platform_file)
275 {
276   char *workstation_model_name;
277   int workstation_id = -1;
278
279   surf_timer_model_init(platform_file);
280
281   workstation_model_name =
282     xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
283   char *network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network/model");
284   char *cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu/model");
285
286   if ((strcmp(network_model_name,"LV08") || strcmp(cpu_model_name,"Cas01"))
287       && !strcmp(workstation_model_name, "CLM03")){
288     const char *val = "compound";
289     INFO0("Switching workstation model to compound since you changed the network and/or cpu model(s)");
290     xbt_cfg_set_string(_surf_cfg_set,"workstation/model",val);
291     workstation_model_name = (char*)"compound";
292   }
293
294   DEBUG1("Workstation model: %s", workstation_model_name);
295   workstation_id =
296     find_model_description(surf_workstation_model_description,
297                            workstation_model_name);
298   if (!strcmp(workstation_model_name, "compound")) {
299     int network_id = -1;
300     int cpu_id = -1;
301
302     xbt_assert0(cpu_model_name,
303         "Set a cpu model to use with the 'compound' workstation model");
304
305     xbt_assert0(network_model_name,
306         "Set a network model to use with the 'compound' workstation model");
307
308     network_id =
309       find_model_description(surf_network_model_description,
310                              network_model_name);
311     cpu_id =
312       find_model_description(surf_cpu_model_description, cpu_model_name);
313
314     surf_cpu_model_description[cpu_id].model_init_preparse(platform_file);
315     surf_network_model_description[network_id].
316       model_init_preparse(platform_file);
317   }
318
319   DEBUG0("Call workstation_model_init");
320   surf_workstation_model_description[workstation_id].model_init_preparse
321     (platform_file);
322 }
323
324 void surf_config_models_create_elms(void)
325 {
326   char *workstation_model_name =
327     xbt_cfg_get_string(_surf_cfg_set, "workstation/model");
328   int workstation_id =
329     find_model_description(surf_workstation_model_description,
330                            workstation_model_name);
331   if (surf_workstation_model_description[workstation_id].
332       model_init_postparse != NULL)
333     surf_workstation_model_description[workstation_id].model_init_postparse();
334 }