Logo AND Algorithmique Numérique Distribuée

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