Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
47e0a64d6ae6ad4fefd556a681a3443706851cfc
[simgrid.git] / src / surf / surf_config.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009 The SimGrid team. All rights reserved.                */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 /* surf_config: configuration infrastructure for the simulation world       */
9
10 #include "xbt/config.h"
11 #include "xbt/str.h"
12 #include "surf/surf_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf,
15                                 "About the configuration of surf (and the rest of the simulation)");
16
17 xbt_cfg_t _surf_cfg_set = NULL;
18
19
20 /* Parse the command line, looking for options */
21 static void surf_config_cmd_line(int *argc, char **argv)
22 {
23   int i, j;
24   char *opt;
25
26   for (i = 1; i < *argc; i++) {
27     int remove_it = 0;
28     if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
29       opt = strchr(argv[i], '=');
30       opt++;
31
32       xbt_cfg_set_parse(_surf_cfg_set, opt);
33       DEBUG1("Did apply '%s' as config setting", opt);
34       remove_it = 1;
35     } else if (!strncmp(argv[i], "--cfg-help", strlen("--cfg-help") + 1) ||
36                !strncmp(argv[i], "--help", strlen("--help") + 1)) {
37       printf
38         ("Description of the configuration accepted by this simulator:\n");
39       xbt_cfg_help(_surf_cfg_set);
40       remove_it = 1;
41       exit(0);
42     }
43     if (remove_it) {            /*remove this from argv */
44       for (j = i + 1; j < *argc; j++) {
45         argv[j - 1] = argv[j];
46       }
47
48       argv[j - 1] = NULL;
49       (*argc)--;
50       i--;                      /* compensate effect of next loop incrementation */
51     }
52   }
53 }
54
55
56 int _surf_init_status = 0;      /* 0: beginning of time;
57                                    1: pre-inited (cfg_set created);
58                                    2: inited (running) */
59
60 /* callback of the workstation_model variable */
61 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
62 {
63   char *val;
64
65   xbt_assert0(_surf_init_status < 2,
66               "Cannot change the model after the initialization");
67
68   val = xbt_cfg_get_string(_surf_cfg_set, name);
69   /* New Module missing */
70
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   /* New Module missing */
84   find_model_description(surf_cpu_model_description, val);
85 }
86
87 /* callback of the workstation_model variable */
88 static void _surf_cfg_cb__network_model(const char *name, int pos)
89 {
90   char *val;
91
92   xbt_assert0(_surf_init_status < 2,
93               "Cannot change the model after the initialization");
94
95   val = xbt_cfg_get_string(_surf_cfg_set, name);
96   /* New Module missing */
97   find_model_description(surf_network_model_description, val);
98 }
99
100 /* callback of the tcp gamma variable */
101 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos)
102 {
103   sg_tcp_gamma = xbt_cfg_get_double(_surf_cfg_set, name);
104 }
105
106 static void _surf_cfg_cb__surf_path(const char *name, int pos)
107 {
108   char *path = xbt_cfg_get_string_at(_surf_cfg_set, name, pos);
109   xbt_dynar_push(surf_path, &path);
110 }
111
112
113
114 /* create the config set, register what should be and parse the command line*/
115 void surf_config_init(int *argc, char **argv)
116 {
117
118   /* Create the configuration support */
119   if (_surf_init_status == 0) { /* Only create stuff if not already inited */
120     _surf_init_status = 1;
121
122     char *description = xbt_malloc(1024), *p = description;
123     char *default_value;
124     int i;
125
126     sprintf(description, "The model to use for the CPU. Possible values: ");
127     p = description;
128     while (*(++p) != '\0');
129     for (i = 0; surf_cpu_model_description[i].name; i++)
130       p +=
131         sprintf(p, "%s%s", (i == 0 ? "" : ", "),
132                 surf_cpu_model_description[i].name);
133     default_value = xbt_strdup("Cas01");
134     xbt_cfg_register(&_surf_cfg_set,
135                      "cpu_model", description, xbt_cfgelm_string,
136                      &default_value, 1, 1, &_surf_cfg_cb__cpu_model, NULL);
137
138     sprintf(description,
139             "The model to use for the network. Possible values: ");
140     p = description;
141     while (*(++p) != '\0');
142     for (i = 0; surf_network_model_description[i].name; i++)
143       p +=
144         sprintf(p, "%s%s", (i == 0 ? "" : ", "),
145                 surf_network_model_description[i].name);
146     default_value = xbt_strdup("CM02");
147     xbt_cfg_register(&_surf_cfg_set,
148                      "network_model", description, xbt_cfgelm_string,
149                      &default_value, 1, 1, &_surf_cfg_cb__network_model,
150                      NULL);
151
152     sprintf(description,
153             "The model to use for the workstation. Possible values: ");
154     p = description;
155     while (*(++p) != '\0');
156     for (i = 0; surf_workstation_model_description[i].name; i++)
157       p +=
158         sprintf(p, "%s%s", (i == 0 ? "" : ", "),
159                 surf_workstation_model_description[i].name);
160     default_value = xbt_strdup("CLM03");
161     xbt_cfg_register(&_surf_cfg_set,
162                      "workstation_model", description, xbt_cfgelm_string,
163                      &default_value, 1, 1, &_surf_cfg_cb__workstation_model,
164                      NULL);
165
166     xbt_free(description);
167
168     default_value = xbt_strdup("Full");
169     xbt_cfg_register(&_surf_cfg_set, "routing",
170                      "Model to use to store the routing information",
171                      xbt_cfgelm_string, &default_value, 0, 0, NULL,
172                      NULL);
173
174     xbt_cfg_register(&_surf_cfg_set, "TCP_gamma",
175                      "Size of the biggest TCP window", xbt_cfgelm_double,
176                      NULL, 1, 1, _surf_cfg_cb__tcp_gamma, NULL);
177     xbt_cfg_set_double(_surf_cfg_set, "TCP_gamma", 20000.0);
178
179     xbt_cfg_register(&_surf_cfg_set, "path",
180                      "Lookup path for inclusions in platform and deployment XML files",
181                      xbt_cfgelm_string, NULL, 0, 0, _surf_cfg_cb__surf_path,
182                      NULL);
183     if (!surf_path) {
184       /* retrieves the current directory of the        current process */
185       const char *initial_path = __surf_get_initial_path();
186       xbt_assert0((initial_path),
187                   "__surf_get_initial_path() failed! Can't resolves current Windows directory");
188
189       surf_path = xbt_dynar_new(sizeof(char *), NULL);
190       xbt_cfg_set_string(_surf_cfg_set, "path", initial_path);
191     }
192
193
194     surf_config_cmd_line(argc, argv);
195   }
196 }
197
198 void surf_config_finalize(void)
199 {
200   if (!_surf_init_status)
201     return;                     /* Not initialized yet. Nothing to do */
202
203   xbt_cfg_free(&_surf_cfg_set);
204   _surf_init_status = 0;
205 }
206
207 void surf_config_models_setup(const char *platform_file)
208 {
209   char *workstation_model_name;
210   int workstation_id = -1;
211
212   surf_timer_model_init(platform_file);
213
214   workstation_model_name =
215     xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
216
217   DEBUG1("Model : %s", workstation_model_name);
218   workstation_id =
219     find_model_description(surf_workstation_model_description,
220                            workstation_model_name);
221   if (!strcmp(workstation_model_name, "compound")) {
222     xbt_ex_t e;
223     char *network_model_name = NULL;
224     char *cpu_model_name = NULL;
225     int network_id = -1;
226     int cpu_id = -1;
227
228     TRY {
229       cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu_model");
230     } CATCH(e) {
231       if (e.category == bound_error) {
232         xbt_assert0(0,
233                     "Set a cpu model to use with the 'compound' workstation model");
234         xbt_ex_free(e);
235       } else {
236         RETHROW;
237       }
238     }
239
240     TRY {
241       network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network_model");
242     }
243     CATCH(e) {
244       if (e.category == bound_error) {
245         xbt_assert0(0,
246                     "Set a network model to use with the 'compound' workstation model");
247         xbt_ex_free(e);
248       } else {
249         RETHROW;
250       }
251     }
252
253     network_id =
254       find_model_description(surf_network_model_description,
255                              network_model_name);
256     cpu_id =
257       find_model_description(surf_cpu_model_description, cpu_model_name);
258
259     surf_cpu_model_description[cpu_id].model_init_preparse(platform_file);
260     surf_network_model_description[network_id].
261       model_init_preparse(platform_file);
262   }
263
264   DEBUG0("Call workstation_model_init");
265   surf_workstation_model_description[workstation_id].model_init_preparse
266     (platform_file);
267 }
268
269 void surf_config_models_create_elms(void)
270 {
271   char *workstation_model_name =
272     xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
273   int workstation_id =
274     find_model_description(surf_workstation_model_description,
275                            workstation_model_name);
276   if (surf_workstation_model_description[workstation_id].
277       model_init_postparse != NULL)
278     surf_workstation_model_description[workstation_id].model_init_postparse();
279 }