3 /* Copyright (c) 2009 The SimGrid team. All rights reserved. */
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. */
8 /* surf_config: configuration infrastructure for the simulation world */
10 #include "xbt/config.h"
12 #include "surf/surf_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config,surf,"About the configuration of surf (and the rest of the simulation)");
16 xbt_cfg_t _surf_cfg_set = NULL;
19 /* Parse the command line, looking for options */
20 static void surf_config_cmd_line(int *argc,char **argv)
25 for (i = 1; i < *argc; i++) {
27 if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
28 opt = strchr(argv[i], '=');
31 xbt_cfg_set_parse(_surf_cfg_set,opt);
32 DEBUG1("Did apply '%s' as config setting", opt);
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);
41 if (remove_it) { /*remove this from argv */
42 for (j = i + 1; j < *argc; j++) {
43 argv[j - 1] = argv[j];
48 i--; /* compensate effect of next loop incrementation */
54 int _surf_init_status = 0; /* 0: beginning of time;
55 1: pre-inited (cfg_set created);
56 2: inited (running) */
58 /* callback of the workstation_model variable */
59 static void _surf_cfg_cb__workstation_model(const char *name, int pos)
63 xbt_assert0(_surf_init_status < 2,
64 "Cannot change the model after the initialization");
66 val = xbt_cfg_get_string(_surf_cfg_set, name);
67 /* New Module missing */
69 find_model_description(surf_workstation_model_description, val);
72 /* callback of the cpu_model variable */
73 static void _surf_cfg_cb__cpu_model(const char *name, int pos)
77 xbt_assert0(_surf_init_status < 2,
78 "Cannot change the model after the initialization");
80 val = xbt_cfg_get_string(_surf_cfg_set, name);
81 /* New Module missing */
82 find_model_description(surf_cpu_model_description, val);
85 /* callback of the workstation_model variable */
86 static void _surf_cfg_cb__network_model(const char *name, int pos)
90 xbt_assert0(_surf_init_status < 2,
91 "Cannot change the model after the initialization");
93 val = xbt_cfg_get_string(_surf_cfg_set, name);
94 /* New Module missing */
95 find_model_description(surf_network_model_description, val);
98 /* callback of the tcp gamma variable */
99 static void _surf_cfg_cb__tcp_gamma(const char *name, int pos) {
100 sg_tcp_gamma = xbt_cfg_get_double(_surf_cfg_set, name);
103 static void _surf_cfg_cb__surf_path(const char *name, int pos) {
104 char *path = xbt_cfg_get_string_at(_surf_cfg_set, name,pos);
105 xbt_dynar_push(surf_path, &path);
110 /* create the config set, register what should be and parse the command line*/
111 void surf_config_init(int *argc, char **argv) {
113 /* Create the configuration support */
114 if (!_surf_cfg_set) { /* Only create stuff if not already inited */
115 _surf_cfg_set = xbt_cfg_new();
116 _surf_init_status = 1;
118 char *description = xbt_malloc(1024), *p = description;
120 sprintf(description,"The model to use for the workstation. Possible values: ");
121 while (*(++p) != '\0');
122 for (i=0;surf_workstation_model_description[i].name;i++)
123 p+=sprintf(p,"%s%s",(i==0?"":", "),surf_workstation_model_description[i].name);
125 xbt_cfg_register(_surf_cfg_set,
126 "workstation_model", xbt_strdup(description), xbt_cfgelm_string, 1, 1,
127 &_surf_cfg_cb__workstation_model, NULL);
129 sprintf(description,"The model to use for the CPU. Possible values: ");
131 while (*(++p) != '\0');
132 for (i=0;surf_cpu_model_description[i].name;i++)
133 p+=sprintf(p,"%s%s",(i==0?"":", "),surf_cpu_model_description[i].name);
134 xbt_cfg_register(_surf_cfg_set,
135 "cpu_model", xbt_strdup(description), xbt_cfgelm_string, 1, 1,
136 &_surf_cfg_cb__cpu_model, NULL);
138 sprintf(description,"The model to use for the network. Possible values: ");
140 while (*(++p) != '\0');
141 for (i=0;surf_network_model_description[i].name;i++)
142 p+=sprintf(p,"%s%s",(i==0?"":", "),surf_network_model_description[i].name);
143 xbt_cfg_register(_surf_cfg_set,
144 "network_model", description, xbt_cfgelm_string, 1, 1,
145 &_surf_cfg_cb__network_model, NULL);
147 xbt_cfg_set_string(_surf_cfg_set, "workstation_model", "CLM03");
148 xbt_cfg_set_string(_surf_cfg_set, "cpu_model", "Cas01");
149 xbt_cfg_set_string(_surf_cfg_set, "network_model", "CM02");
151 xbt_cfg_register(_surf_cfg_set,"TCP_gamma","Size of the biggest TCP window",1,1,
152 xbt_cfgelm_double,_surf_cfg_cb__tcp_gamma,NULL);
153 xbt_cfg_set_double(_surf_cfg_set, "TCP_gamma", 20000.0);
155 xbt_cfg_register(_surf_cfg_set,"path","Lookup path for inclusions in platform and deployment XML files",
156 xbt_cfgelm_string, 0,0,_surf_cfg_cb__surf_path,NULL);
158 /* retrieves the current directory of the current process */
159 const char *initial_path = __surf_get_initial_path();
160 xbt_assert0((initial_path),
161 "__surf_get_initial_path() failed! Can't resolves current Windows directory");
163 surf_path = xbt_dynar_new(sizeof(char *), NULL);
164 xbt_cfg_set_string(_surf_cfg_set,"path", initial_path);
167 surf_config_cmd_line(argc,argv);
170 void surf_config_finalize(void)
172 if (!_surf_init_status)
173 return; /* Not initialized yet. Nothing to do */
175 xbt_cfg_free(&_surf_cfg_set);
176 _surf_init_status = 0;
179 void surf_config_models_setup(const char *platform_file){
180 char *workstation_model_name;
181 int workstation_id = -1;
183 workstation_model_name =
184 xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
186 DEBUG1("Model : %s", workstation_model_name);
188 find_model_description(surf_workstation_model_description,
189 workstation_model_name);
190 if (!strcmp(workstation_model_name, "compound")) {
192 char *network_model_name = NULL;
193 char *cpu_model_name = NULL;
198 cpu_model_name = xbt_cfg_get_string(_surf_cfg_set, "cpu_model");
200 if (e.category == bound_error) {
202 "Set a cpu model to use with the 'compound' workstation model");
210 network_model_name = xbt_cfg_get_string(_surf_cfg_set, "network_model");
213 if (e.category == bound_error) {
215 "Set a network model to use with the 'compound' workstation model");
223 find_model_description(surf_network_model_description,
226 find_model_description(surf_cpu_model_description, cpu_model_name);
228 surf_cpu_model_description[cpu_id].model_init(platform_file);
229 surf_network_model_description[network_id].model_init(platform_file);
232 DEBUG0("Call workstation_model_init");
233 surf_workstation_model_description[workstation_id].model_init(platform_file);
236 void surf_config_models_create_elms(void) {
237 char *workstation_model_name = xbt_cfg_get_string(_surf_cfg_set, "workstation_model");
239 find_model_description(surf_workstation_model_description,
240 workstation_model_name);
241 if (surf_workstation_model_description[workstation_id].create_ws != NULL)
242 surf_workstation_model_description[workstation_id].create_ws();