Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sanitize the way surf options are declared: in surf_config not dupplicated in simix...
[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                 if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
70                         opt = strchr(argv[i], '=');
71                         opt++;
72
73                         xbt_cfg_set_parse(_surf_cfg_set,opt);
74                         DEBUG1("Did apply '%s' as config setting", opt);
75                         /*remove this from argv */
76
77                         for (j = i + 1; j < *argc; j++) {
78                                 argv[j - 1] = argv[j];
79                         }
80
81                         argv[j - 1] = NULL;
82                         (*argc)--;
83                         i--;                      /* compensate effect of next loop incrementation */
84                 }
85         }
86 }
87
88 /* create the config set, register what should be and parse the command line*/
89 void surf_config_init(int *argc, char **argv) {
90
91         /* Create the configuration support */
92         if (!_surf_cfg_set) { /* Only create stuff if not already inited */
93                 _surf_cfg_set = xbt_cfg_new();
94                 _surf_init_status = 1;
95
96                 char *description = xbt_malloc(1024), *p = description;
97                 int i;
98                 sprintf(description,"The model to use for the workstation. Possible values: ");
99                 while (*(++p) != '\0');
100                 for (i=0;surf_workstation_model_description[i].name;i++)
101                         p+=sprintf(p,"%s%s",(i==0?"":", "),surf_workstation_model_description[i].name);
102
103                 xbt_cfg_register(_surf_cfg_set,
104                                 "workstation_model", description,  xbt_cfgelm_string, 1, 1,
105                                 &_surf_cfg_cb__workstation_model, NULL);
106
107                 sprintf(description,"The model to use for the CPU. Possible values: ");
108                 p = description;
109                 while (*(++p) != '\0');
110                 for (i=0;surf_cpu_model_description[i].name;i++)
111                         p+=sprintf(p,"%s%s",(i==0?"":", "),surf_cpu_model_description[i].name);
112                 xbt_cfg_register(_surf_cfg_set,
113                                 "cpu_model", description, xbt_cfgelm_string, 1, 1,
114                                 &_surf_cfg_cb__cpu_model, NULL);
115
116                 sprintf(description,"The model to use for the network. Possible values: ");
117                 p = description;
118                 while (*(++p) != '\0');
119                 for (i=0;surf_network_model_description[i].name;i++)
120                         p+=sprintf(p,"%s%s",(i==0?"":", "),surf_network_model_description[i].name);
121                 xbt_cfg_register(_surf_cfg_set,
122                                 "network_model", description, xbt_cfgelm_string, 1, 1,
123                                 &_surf_cfg_cb__network_model, NULL);
124
125                 xbt_cfg_set_string(_surf_cfg_set, "workstation_model", "CLM03");
126
127                 surf_config_cmd_line(argc,argv);
128         }
129 }
130 void surf_config_finalize(void)
131 {
132         if (!_surf_init_status)
133                 return;                     /* Not initialized yet. Nothing to do */
134
135         xbt_cfg_free(&_surf_cfg_set);
136         _surf_init_status = 0;
137 }
138
139 void surf_config_models_setup(const char *platform_file){
140         char *workstation_model_name;
141         int workstation_id = -1;
142
143         workstation_model_name =
144                 xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
145
146         DEBUG1("Model : %s", workstation_model_name);
147         workstation_id =
148                 find_model_description(surf_workstation_model_description,
149                                 workstation_model_name);
150         if (!strcmp(workstation_model_name, "compound")) {
151                 xbt_ex_t e;
152                 char *network_model_name = NULL;
153                 char *cpu_model_name = NULL;
154                 int network_id = -1;
155                 int cpu_id = -1;
156
157                 TRY {
158                         cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu_model");
159                 } CATCH(e) {
160                         if (e.category == bound_error) {
161                                 xbt_assert0(0,
162                                                 "Set a cpu model to use with the 'compound' workstation model");
163                                 xbt_ex_free(e);
164                         } else {
165                                 RETHROW;
166                         }
167                 }
168
169                 TRY {
170                         network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network_model");
171                 }
172                 CATCH(e) {
173                         if (e.category == bound_error) {
174                                 xbt_assert0(0,
175                                                 "Set a network model to use with the 'compound' workstation model");
176                                 xbt_ex_free(e);
177                         } else {
178                                 RETHROW;
179                         }
180                 }
181
182                 network_id =
183                         find_model_description(surf_network_model_description,
184                                         network_model_name);
185                 cpu_id =
186                         find_model_description(surf_cpu_model_description, cpu_model_name);
187
188                 surf_cpu_model_description[cpu_id].model_init(platform_file);
189                 surf_network_model_description[network_id].model_init(platform_file);
190         }
191
192         DEBUG0("Call workstation_model_init");
193         surf_workstation_model_description[workstation_id].model_init(platform_file);
194 }
195
196 void surf_config_models_create_elms(void) {
197         char *workstation_model_name = xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
198         int workstation_id =
199                 find_model_description(surf_workstation_model_description,
200                                 workstation_model_name);
201         if (surf_workstation_model_description[workstation_id].create_ws != NULL)
202                 surf_workstation_model_description[workstation_id].create_ws();
203 }