Logo AND Algorithmique Numérique Distribuée

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