Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change TCP_gamma to not be a compilation-time #define, but a command line option
[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,"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("Description of the configuration accepted by this simulator:\n");
37                         xbt_cfg_help(_surf_cfg_set);
38                         remove_it=1;
39                         exit(0);
40                 }
41                 if (remove_it) { /*remove this from argv */
42                         for (j = i + 1; j < *argc; j++) {
43                                 argv[j - 1] = argv[j];
44                         }
45
46                         argv[j - 1] = NULL;
47                         (*argc)--;
48                         i--;                      /* compensate effect of next loop incrementation */
49                 }
50         }
51 }
52
53
54 int _surf_init_status = 0;     /* 0: beginning of time;
55                                   1: pre-inited (cfg_set created);
56                                   2: inited (running) */
57
58 /* callback of the workstation_model variable */
59 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
60 {
61         char *val;
62
63         xbt_assert0(_surf_init_status < 2,
64                         "Cannot change the model after the initialization");
65
66         val = xbt_cfg_get_string(_surf_cfg_set, name);
67         /* New Module missing */
68
69         find_model_description(surf_workstation_model_description, val);
70 }
71
72 /* callback of the cpu_model variable */
73 static void _surf_cfg_cb__cpu_model(const char *name, int pos)
74 {
75         char *val;
76
77         xbt_assert0(_surf_init_status < 2,
78                         "Cannot change the model after the initialization");
79
80         val = xbt_cfg_get_string(_surf_cfg_set, name);
81         /* New Module missing */
82         find_model_description(surf_cpu_model_description, val);
83 }
84
85 /* callback of the workstation_model variable */
86 static void _surf_cfg_cb__network_model(const char *name, int pos)
87 {
88         char *val;
89
90         xbt_assert0(_surf_init_status < 2,
91                         "Cannot change the model after the initialization");
92
93         val = xbt_cfg_get_string(_surf_cfg_set, name);
94         /* New Module missing */
95         find_model_description(surf_network_model_description, val);
96 }
97
98 /* callback of the cpu_model variable */
99 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos)
100 {
101         sg_tcp_gamma =  xbt_cfg_get_double(_surf_cfg_set, name);
102 }
103
104
105
106 /* create the config set, register what should be and parse the command line*/
107 void surf_config_init(int *argc, char **argv) {
108
109         /* Create the configuration support */
110         if (!_surf_cfg_set) { /* Only create stuff if not already inited */
111                 _surf_cfg_set = xbt_cfg_new();
112                 _surf_init_status = 1;
113
114                 char *description = xbt_malloc(1024), *p = description;
115                 int i;
116                 sprintf(description,"The model to use for the workstation. Possible values: ");
117                 while (*(++p) != '\0');
118                 for (i=0;surf_workstation_model_description[i].name;i++)
119                         p+=sprintf(p,"%s%s",(i==0?"":", "),surf_workstation_model_description[i].name);
120
121                 xbt_cfg_register(_surf_cfg_set,
122                                 "workstation_model", xbt_strdup(description),  xbt_cfgelm_string, 1, 1,
123                                 &_surf_cfg_cb__workstation_model, NULL);
124
125                 sprintf(description,"The model to use for the CPU. Possible values: ");
126                 p = description;
127                 while (*(++p) != '\0');
128                 for (i=0;surf_cpu_model_description[i].name;i++)
129                         p+=sprintf(p,"%s%s",(i==0?"":", "),surf_cpu_model_description[i].name);
130                 xbt_cfg_register(_surf_cfg_set,
131                                 "cpu_model", xbt_strdup(description), xbt_cfgelm_string, 1, 1,
132                                 &_surf_cfg_cb__cpu_model, NULL);
133
134                 sprintf(description,"The model to use for the network. Possible values: ");
135                 p = description;
136                 while (*(++p) != '\0');
137                 for (i=0;surf_network_model_description[i].name;i++)
138                         p+=sprintf(p,"%s%s",(i==0?"":", "),surf_network_model_description[i].name);
139                 xbt_cfg_register(_surf_cfg_set,
140                                 "network_model", description, xbt_cfgelm_string, 1, 1,
141                                 &_surf_cfg_cb__network_model, NULL);
142
143                 xbt_cfg_set_string(_surf_cfg_set, "workstation_model", "CLM03");
144                 xbt_cfg_set_string(_surf_cfg_set, "cpu_model", "Cas01");
145                 xbt_cfg_set_string(_surf_cfg_set, "network_model", "CM02");
146
147                 xbt_cfg_register(_surf_cfg_set,"TCP_gamma","Size of the biggest TCP window",1,1,
148                                         xbt_cfgelm_double,_surf_cfg_cb__tcp_gamma,NULL);
149                 xbt_cfg_set_double(_surf_cfg_set, "TCP_gamma", 20000.0);
150
151                 surf_config_cmd_line(argc,argv);
152         }
153 }
154 void surf_config_finalize(void)
155 {
156         if (!_surf_init_status)
157                 return;                     /* Not initialized yet. Nothing to do */
158
159         xbt_cfg_free(&_surf_cfg_set);
160         _surf_init_status = 0;
161 }
162
163 void surf_config_models_setup(const char *platform_file){
164         char *workstation_model_name;
165         int workstation_id = -1;
166
167         workstation_model_name =
168                 xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
169
170         DEBUG1("Model : %s", workstation_model_name);
171         workstation_id =
172                 find_model_description(surf_workstation_model_description,
173                                 workstation_model_name);
174         if (!strcmp(workstation_model_name, "compound")) {
175                 xbt_ex_t e;
176                 char *network_model_name = NULL;
177                 char *cpu_model_name = NULL;
178                 int network_id = -1;
179                 int cpu_id = -1;
180
181                 TRY {
182                         cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu_model");
183                 } CATCH(e) {
184                         if (e.category == bound_error) {
185                                 xbt_assert0(0,
186                                                 "Set a cpu model to use with the 'compound' workstation model");
187                                 xbt_ex_free(e);
188                         } else {
189                                 RETHROW;
190                         }
191                 }
192
193                 TRY {
194                         network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network_model");
195                 }
196                 CATCH(e) {
197                         if (e.category == bound_error) {
198                                 xbt_assert0(0,
199                                                 "Set a network model to use with the 'compound' workstation model");
200                                 xbt_ex_free(e);
201                         } else {
202                                 RETHROW;
203                         }
204                 }
205
206                 network_id =
207                         find_model_description(surf_network_model_description,
208                                         network_model_name);
209                 cpu_id =
210                         find_model_description(surf_cpu_model_description, cpu_model_name);
211
212                 surf_cpu_model_description[cpu_id].model_init(platform_file);
213                 surf_network_model_description[network_id].model_init(platform_file);
214         }
215
216         DEBUG0("Call workstation_model_init");
217         surf_workstation_model_description[workstation_id].model_init(platform_file);
218 }
219
220 void surf_config_models_create_elms(void) {
221         char *workstation_model_name = xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
222         int workstation_id =
223                 find_model_description(surf_workstation_model_description,
224                                 workstation_model_name);
225         if (surf_workstation_model_description[workstation_id].create_ws != NULL)
226                 surf_workstation_model_description[workstation_id].create_ws();
227 }