Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak (free is too robust to my stupidity)
[simgrid.git] / src / surf / surfxml_parse.cpp
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <errno.h>
8 #include <math.h>
9 #include <stdarg.h> /* va_arg */
10
11 #include "xbt/misc.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/file.h"
15 #include "xbt/dict.h"
16 #include "src/surf/surf_private.h"
17 #include "simgrid/sg_config.h"
18 #include "surfxml_private.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
21                                 "Logging specific to the SURF parsing module");
22 #undef CLEANUP
23 int ETag_surfxml_include_state(void);
24
25 #include "simgrid_dtd.c"
26
27 char* surf_parsed_filename = NULL; // to locate parse error messages
28
29 xbt_dynar_t parsed_link_list = NULL;   /* temporary store of current list link of a route */
30 /*
31  * Helping functions
32  */
33 void surf_parse_error(const char *fmt, ...) {
34   va_list va;
35   va_start(va,fmt);
36   int lineno = surf_parse_lineno;
37   char *msg = bvprintf(fmt,va);
38   va_end(va);
39   cleanup();
40   XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
41   surf_exit();
42   xbt_die("Exiting now");
43 }
44 void surf_parse_warn(const char *fmt, ...) {
45   va_list va;
46   va_start(va,fmt);
47   char *msg = bvprintf(fmt,va);
48   va_end(va);
49     XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
50     free(msg);
51 }
52
53 double surf_parse_get_double(const char *string) {
54   double res;
55   int ret = sscanf(string, "%lg", &res);
56   if (ret != 1)
57     surf_parse_error("%s is not a double", string);
58   //printf("Parsed double [%g] %s\n", res, string);
59   return res;
60 }
61
62 int surf_parse_get_int(const char *string) {
63   int res;
64   int ret = sscanf(string, "%d", &res);
65   if (ret != 1)
66     surf_parse_error("%s is not an integer", string);
67   return res;
68 }
69
70 struct unit_scale {
71   const char *unit;
72   double scale;
73 };
74
75 /* Note: field `unit' for the last element of parameter `units' should be
76  * NULL. */
77 static double surf_parse_get_value_with_unit(const char *string, const struct unit_scale *units,
78     const char *entity_kind, const char *name,
79     const char *error_msg, const char *default_unit)
80 {
81   char* ptr;
82   double res;
83   int i;
84   errno = 0;
85   res   = strtod(string, &ptr);
86   if (errno == ERANGE)
87     surf_parse_error("value out of range: %s", string);
88   if (ptr == string)
89     surf_parse_error("cannot parse number: %s", string);
90   if (ptr[0] == '\0') {
91     if (res == 0)
92       return res; // Ok, 0 can be unit-less
93
94     XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
95     ptr = (char*)default_unit;
96   }
97   for (i = 0; units[i].unit != NULL && strcmp(ptr, units[i].unit) != 0; i++) {
98   }
99   if (units[i].unit != NULL)
100     res *= units[i].scale;
101   else
102     surf_parse_error("unknown unit: %s", ptr);
103   return res;
104 }
105
106 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
107 {
108   const struct unit_scale units[] = {
109     { "w",  7 * 24 * 60 * 60 },
110     { "d",  24 * 60 * 60 },
111     { "h",  60 * 60 },
112     { "m",  60 },
113     { "s",  1.0 },
114     { "ms", 1e-3 },
115     { "us", 1e-6 },
116     { "ns", 1e-9 },
117     { "ps", 1e-12 },
118     { NULL, 0 }
119   };
120   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
121       "Append 's' to your time to get seconds", "s");
122 }
123
124 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
125 {
126   const struct unit_scale units[] = {
127     { "TiB", pow(1024, 4) },
128     { "GiB", pow(1024, 3) },
129     { "MiB", pow(1024, 2) },
130     { "KiB", 1024 },
131     { "TB",  1e12 },
132     { "GB",  1e9 },
133     { "MB",  1e6 },
134     { "kB",  1e3 },
135     { "B",   1.0 },
136     { "Tib", 0.125 * pow(1024, 4) },
137     { "Gib", 0.125 * pow(1024, 3) },
138     { "Mib", 0.125 * pow(1024, 2) },
139     { "Kib", 0.125 * 1024 },
140     { "Tb",  0.125 * 1e12 },
141     { "Gb",  0.125 * 1e9 },
142     { "Mb",  0.125 * 1e6 },
143     { "kb",  0.125 * 1e3 },
144     { "b",   0.125 },
145     { NULL,    0 }
146   };
147   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
148       "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
149 }
150
151 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
152 {
153   const struct unit_scale units[] = {
154     { "TiBps", pow(1024, 4) },
155     { "GiBps", pow(1024, 3) },
156     { "MiBps", pow(1024, 2) },
157     { "KiBps", 1024 },
158     { "TBps",  1e12 },
159     { "GBps",  1e9 },
160     { "MBps",  1e6 },
161     { "kBps",  1e3 },
162     { "Bps",   1.0 },
163     { "Tibps", 0.125 * pow(1024, 4) },
164     { "Gibps", 0.125 * pow(1024, 3) },
165     { "Mibps", 0.125 * pow(1024, 2) },
166     { "Kibps", 0.125 * 1024 },
167     { "Tbps",  0.125 * 1e12 },
168     { "Gbps",  0.125 * 1e9 },
169     { "Mbps",  0.125 * 1e6 },
170     { "kbps",  0.125 * 1e3 },
171     { "bps",   0.125 },
172     { NULL,    0 }
173   };
174   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
175       "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
176 }
177
178 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
179 {
180   const struct unit_scale units[] = {
181     { "yottaflops", 1e24 },
182     { "Yf",         1e24 },
183     { "zettaflops", 1e21 },
184     { "Zf",         1e21 },
185     { "exaflops",   1e18 },
186     { "Ef",         1e18 },
187     { "petaflops",  1e15 },
188     { "Pf",         1e15 },
189     { "teraflops",  1e12 },
190     { "Tf",         1e12 },
191     { "gigaflops",  1e9 },
192     { "Gf",         1e9 },
193     { "megaflops",  1e6 },
194     { "Mf",         1e6 },
195     { "kiloflops",  1e3 },
196     { "kf",         1e3 },
197     { "flops",      1.0 },
198     { "f",          1.0 },
199     { NULL,         0 }
200   };
201   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
202       "Append 'f' or 'flops' to your speed to get flop per second", "f");
203 }
204
205 /*
206  * All the callback lists that can be overridden anywhere.
207  * (this list should probably be reduced to the bare minimum to allow the models to work)
208  */
209
210 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
211
212 /* The default current property receiver. Setup in the corresponding opening callbacks. */
213 xbt_dict_t current_property_set = NULL;
214 xbt_dict_t current_model_property_set = NULL;
215 xbt_dict_t as_current_property_set = NULL;
216 int AS_TAG = 0;
217 char* as_name_tab[1024];
218 void* as_dict_tab[1024];
219 int as_prop_nb = 0;
220
221
222 /* dictionary of random generator data */
223 xbt_dict_t random_data_list = NULL;
224
225 YY_BUFFER_STATE surf_input_buffer;
226 FILE *surf_file_to_parse = NULL;
227
228 /*
229  * Stuff relative to storage
230  */
231 void STag_surfxml_storage(void)
232 {
233   AS_TAG = 0;
234   XBT_DEBUG("STag_surfxml_storage");
235   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
236 }
237 void ETag_surfxml_storage(void)
238 {
239   s_sg_platf_storage_cbarg_t storage = SG_PLATF_STORAGE_INITIALIZER;
240   memset(&storage,0,sizeof(storage));
241
242   storage.id           = A_surfxml_storage_id;
243   storage.type_id      = A_surfxml_storage_typeId;
244   storage.content      = A_surfxml_storage_content;
245   storage.content_type = A_surfxml_storage_content___type;
246   storage.properties   = current_property_set;
247   storage.attach       = A_surfxml_storage_attach;
248   sg_platf_new_storage(&storage);
249   current_property_set = NULL;
250 }
251 void STag_surfxml_storage___type(void)
252 {
253   AS_TAG = 0;
254   XBT_DEBUG("STag_surfxml_storage___type");
255   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
256   xbt_assert(current_model_property_set == NULL, "Someone forgot to reset the model property set to NULL in its closing tag (or XML malformed)");
257 }
258 void ETag_surfxml_storage___type(void)
259 {
260   s_sg_platf_storage_type_cbarg_t storage_type = SG_PLATF_STORAGE_TYPE_INITIALIZER;
261   memset(&storage_type,0,sizeof(storage_type));
262
263   storage_type.content          = A_surfxml_storage___type_content;
264   storage_type.content_type     = A_surfxml_storage___type_content___type;
265   storage_type.id               = A_surfxml_storage___type_id;
266   storage_type.model            = A_surfxml_storage___type_model;
267   storage_type.properties       = current_property_set;
268   storage_type.model_properties = current_model_property_set;
269   storage_type.size             = surf_parse_get_size(A_surfxml_storage___type_size,
270         "size of storage type", storage_type.id);
271   sg_platf_new_storage_type(&storage_type);
272   current_property_set       = NULL;
273   current_model_property_set = NULL;
274 }
275 void STag_surfxml_mstorage(void)
276 {
277   XBT_DEBUG("STag_surfxml_mstorage");
278 }
279 void ETag_surfxml_mstorage(void)
280 {
281   s_sg_platf_mstorage_cbarg_t mstorage = SG_PLATF_MSTORAGE_INITIALIZER;
282   memset(&mstorage,0,sizeof(mstorage));
283
284   mstorage.name    = A_surfxml_mstorage_name;
285   mstorage.type_id = A_surfxml_mstorage_typeId;
286   sg_platf_new_mstorage(&mstorage);
287 }
288 void STag_surfxml_mount(void)
289 {
290   XBT_DEBUG("STag_surfxml_mount");
291 }
292 void ETag_surfxml_mount(void)
293 {
294   s_sg_platf_mount_cbarg_t mount = SG_PLATF_MOUNT_INITIALIZER;
295   memset(&mount,0,sizeof(mount));
296
297   mount.name      = A_surfxml_mount_name;
298   mount.storageId = A_surfxml_mount_storageId;
299   sg_platf_new_mount(&mount);
300 }
301
302 /*
303  * Stuff relative to the <include> tag
304  */
305 static xbt_dynar_t surf_input_buffer_stack    = NULL;
306 static xbt_dynar_t surf_file_to_parse_stack   = NULL;
307 static xbt_dynar_t surf_parsed_filename_stack = NULL;
308
309 void STag_surfxml_include(void)
310 {
311   parse_after_config();
312   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
313   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
314   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
315
316   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
317
318   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
319   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
320               A_surfxml_include_file);
321   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
322   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
323   surf_parse_push_buffer_state(surf_input_buffer);
324
325   fflush(NULL);
326 }
327
328 void ETag_surfxml_include(void) {
329 /* Nothing to do when done with reading the include tag.
330  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
331 }
332
333 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
334  *
335  * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
336  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
337  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
338  * error message in that case.
339  *
340  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
341  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
342  * on a line. One day maybe, if the maya allow it.
343  */
344 int ETag_surfxml_include_state(void)
345 {
346   fflush(NULL);
347   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
348
349   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
350     return 0;
351
352   // Yeah, we were in an <include> Restore state and proceed.
353   fclose(surf_file_to_parse);
354   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
355   surf_parse_pop_buffer_state();
356   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
357
358   // Restore the filename for error messages
359   free(surf_parsed_filename);
360   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
361
362   return 1;
363 }
364
365
366 void surf_parse_init_callbacks(void)
367 {
368     sg_platf_init();
369 }
370
371 void surf_parse_reset_callbacks(void)
372 {
373   surf_parse_free_callbacks();
374   surf_parse_init_callbacks();
375 }
376
377 void surf_parse_free_callbacks(void)
378 {
379   sg_platf_exit();
380 }
381
382 /* Stag and Etag parse functions */
383
384 void STag_surfxml_platform(void) {
385   XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
386
387   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
388       "You're using an ancient XML file.\n"
389       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
390       "instead of MBytes, MFlops and seconds.\n"
391
392       "Use simgrid_update_xml to update your file automatically. "
393       "This program is installed automatically with SimGrid, or "
394       "available in the tools/ directory of the source archive.\n"
395
396       "Please check also out the SURF section of the ChangeLog for "
397       "the 3.1 version for more information. \n"
398
399       "Last, do not forget to also update your values for "
400       "the calls to MSG_task_create (if any).");
401   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
402       "You're using an old XML file.\n"
403       "Use simgrid_update_xml to update your file automatically. "
404       "This program is installed automatically with SimGrid, or "
405       "available in the tools/ directory of the source archive.");
406   xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
407       "Changes introduced in SimGrid 3.13:\n"
408       "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
409       "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
410       "  - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
411       "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
412       "\n\n"
413       "Use simgrid_update_xml to update your file automatically. "
414       "This program is installed automatically with SimGrid, or "
415       "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
416
417   sg_platf_begin();
418 }
419 void ETag_surfxml_platform(void){
420   sg_platf_end();
421 }
422
423 void STag_surfxml_host(void){
424   AS_TAG = 0;
425   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
426 }
427
428 void STag_surfxml_prop(void)
429 {
430   if(AS_TAG){
431     if (!as_current_property_set){
432       xbt_assert(as_prop_nb < 1024, "Number of AS property reach the limit!!!");
433       as_current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
434       as_name_tab[as_prop_nb] = xbt_strdup(A_surfxml_AS_id);
435       as_dict_tab[as_prop_nb] = as_current_property_set;
436       XBT_DEBUG("PUSH prop set %p for AS '%s'",as_dict_tab[as_prop_nb],as_name_tab[as_prop_nb]);
437       as_prop_nb++;
438     }
439     XBT_DEBUG("add prop %s=%s into current AS property set", A_surfxml_prop_id, A_surfxml_prop_value);
440     xbt_dict_set(as_current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
441   }
442   else{
443     if (!current_property_set)
444        current_property_set = xbt_dict_new(); // Maybe, it should raise an error
445     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), xbt_free_f);
446     XBT_DEBUG("add prop %s=%s into current property set", A_surfxml_prop_id, A_surfxml_prop_value);
447   }
448 }
449
450 void ETag_surfxml_host(void)    {
451   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
452   char* buf;
453   memset(&host,0,sizeof(host));
454
455
456   host.properties = current_property_set;
457
458   host.id = A_surfxml_host_id;
459
460   buf = A_surfxml_host_speed;
461   XBT_DEBUG("Buffer: %s", buf);
462   host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
463   if (strchr(buf, ',') == NULL){
464     double speed = surf_parse_get_speed(A_surfxml_host_speed,"speed of host", host.id);
465     xbt_dynar_push_as(host.speed_peak,double, speed);
466   }
467   else {
468     xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
469     unsigned int i;
470     for (i = 0; i < xbt_dynar_length(pstate_list); i++) {
471       double speed;
472       char* speed_str;
473
474       xbt_dynar_get_cpy(pstate_list, i, &speed_str);
475       xbt_str_trim(speed_str, NULL);
476       speed = surf_parse_get_speed(speed_str,"speed of host", host.id);
477       xbt_dynar_push_as(host.speed_peak, double, speed);
478       XBT_DEBUG("Speed value: %f", speed);
479     }
480     xbt_dynar_free(&pstate_list);
481   }
482
483   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
484   host.speed_scale = surf_parse_get_double( A_surfxml_host_availability);
485   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
486   host.speed_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file);
487   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file);
488   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
489
490   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
491         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
492   if (A_surfxml_host_state == A_surfxml_host_state_ON)
493     host.initiallyOn = 1;
494   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
495     host.initiallyOn = 1;
496   host.coord = A_surfxml_host_coordinates;
497
498   sg_platf_new_host(&host);
499   xbt_dynar_free(&host.speed_peak);
500   current_property_set = NULL;
501 }
502
503 void STag_surfxml_host___link(void){
504   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
505   s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
506   memset(&host_link,0,sizeof(host_link));
507
508   host_link.id        = A_surfxml_host___link_id;
509   host_link.link_up   = A_surfxml_host___link_up;
510   host_link.link_down = A_surfxml_host___link_down;
511   sg_platf_new_netcard(&host_link);
512 }
513
514 void STag_surfxml_router(void){
515   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
516   memset(&router, 0, sizeof(router));
517
518   router.id    = A_surfxml_router_id;
519   router.coord = A_surfxml_router_coordinates;
520
521   sg_platf_new_router(&router);
522 }
523
524 void ETag_surfxml_cluster(void){
525   s_sg_platf_cluster_cbarg_t cluster = SG_PLATF_CLUSTER_INITIALIZER;
526   memset(&cluster,0,sizeof(cluster));
527   cluster.properties = as_current_property_set;
528
529   cluster.id          = A_surfxml_cluster_id;
530   cluster.prefix      = A_surfxml_cluster_prefix;
531   cluster.suffix      = A_surfxml_cluster_suffix;
532   cluster.radical     = A_surfxml_cluster_radical;
533   cluster.speed       = surf_parse_get_speed(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
534   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
535   cluster.bw          = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
536   cluster.lat         = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
537   if(strcmp(A_surfxml_cluster_bb___bw,""))
538     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
539   if(strcmp(A_surfxml_cluster_bb___lat,""))
540     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
541   if(strcmp(A_surfxml_cluster_limiter___link,""))
542     cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
543   if(strcmp(A_surfxml_cluster_loopback___bw,""))
544     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
545   if(strcmp(A_surfxml_cluster_loopback___lat,""))
546     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
547
548   switch(AX_surfxml_cluster_topology){
549   case A_surfxml_cluster_topology_FLAT:
550     cluster.topology= SURF_CLUSTER_FLAT ;
551     break;
552   case A_surfxml_cluster_topology_TORUS:
553     cluster.topology= SURF_CLUSTER_TORUS ;
554     break;
555   case A_surfxml_cluster_topology_FAT___TREE:
556     cluster.topology = SURF_CLUSTER_FAT_TREE;
557     break;
558   default:
559     surf_parse_error("Invalid cluster topology for cluster %s",
560                      cluster.id);
561     break;
562   }
563   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
564   cluster.router_id = A_surfxml_cluster_router___id;
565
566   switch (AX_surfxml_cluster_sharing___policy) {
567   case A_surfxml_cluster_sharing___policy_SHARED:
568     cluster.sharing_policy = SURF_LINK_SHARED;
569     break;
570   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
571     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
572     break;
573   case A_surfxml_cluster_sharing___policy_FATPIPE:
574     cluster.sharing_policy = SURF_LINK_FATPIPE;
575     break;
576   default:
577     surf_parse_error("Invalid cluster sharing policy for cluster %s",
578                      cluster.id);
579     break;
580   }
581   switch (AX_surfxml_cluster_bb___sharing___policy) {
582   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
583     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
584     break;
585   case A_surfxml_cluster_bb___sharing___policy_SHARED:
586     cluster.bb_sharing_policy = SURF_LINK_SHARED;
587     break;
588   default:
589     surf_parse_error("Invalid bb sharing policy in cluster %s",
590                      cluster.id);
591     break;
592   }
593
594   cluster.availability_trace = A_surfxml_cluster_availability___file;
595   cluster.state_trace = A_surfxml_cluster_state___file;
596   sg_platf_new_cluster(&cluster);
597
598   current_property_set = NULL;
599 }
600
601 void STag_surfxml_cluster(void){
602   parse_after_config();
603   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
604 }
605
606 void STag_surfxml_cabinet(void){
607   parse_after_config();
608   s_sg_platf_cabinet_cbarg_t cabinet = SG_PLATF_CABINET_INITIALIZER;
609   memset(&cabinet,0,sizeof(cabinet));
610   cabinet.id      = A_surfxml_cabinet_id;
611   cabinet.prefix  = A_surfxml_cabinet_prefix;
612   cabinet.suffix  = A_surfxml_cabinet_suffix;
613   cabinet.speed   = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
614   cabinet.bw      = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
615   cabinet.lat     = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
616   cabinet.radical = A_surfxml_cabinet_radical;
617
618   sg_platf_new_cabinet(&cabinet);
619 }
620
621 void STag_surfxml_peer(void){
622   parse_after_config();
623   s_sg_platf_peer_cbarg_t peer = SG_PLATF_PEER_INITIALIZER;
624   memset(&peer,0,sizeof(peer));
625   peer.id                 = A_surfxml_peer_id;
626   peer.speed              = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
627   peer.bw_in              = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
628   peer.bw_out             = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
629   peer.lat                = surf_parse_get_time(A_surfxml_peer_lat, "lat of peer", peer.id);
630   peer.coord              = A_surfxml_peer_coordinates;
631   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
632   peer.state_trace        = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
633
634   sg_platf_new_peer(&peer);
635 }
636
637 void STag_surfxml_link(void){
638   AS_TAG = 0;
639   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
640 }
641
642 void ETag_surfxml_link(void){
643   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
644   memset(&link,0,sizeof(link));
645
646   link.properties = current_property_set;
647
648   link.id                                            = A_surfxml_link_id;
649   link.bandwidth                                     = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id);
650   //printf("Link bandwidth [%g]\n", link.bandwidth);
651   link.bandwidth_trace                               = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
652   link.latency                                       = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id);
653   //printf("Link latency [%g]\n", link.latency);
654   link.latency_trace                                 = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
655
656   switch (A_surfxml_link_state) {
657   case A_surfxml_link_state_ON:
658     link.initiallyOn = 1;
659     break;
660   case A_surfxml_link_state_OFF:
661     link.initiallyOn = 0;
662     break;
663   default:
664     surf_parse_error("invalid state for link %s", link.id);
665     break;
666   }
667   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file);
668
669   switch (A_surfxml_link_sharing___policy) {
670   case A_surfxml_link_sharing___policy_SHARED:
671     link.policy = SURF_LINK_SHARED;
672     break;
673   case A_surfxml_link_sharing___policy_FATPIPE:
674      link.policy = SURF_LINK_FATPIPE;
675      break;
676   case A_surfxml_link_sharing___policy_FULLDUPLEX:
677      link.policy = SURF_LINK_FULLDUPLEX;
678      break;
679   default:
680     surf_parse_error("Invalid sharing policy in link %s", link.id);
681     break;
682   }
683
684   sg_platf_new_link(&link);
685
686   current_property_set = NULL;
687 }
688
689 void STag_surfxml_link___ctn(void){
690
691   char *link_id;
692   switch (A_surfxml_link___ctn_direction) {
693   case AU_surfxml_link___ctn_direction:
694   case A_surfxml_link___ctn_direction_NONE:
695     link_id = xbt_strdup(A_surfxml_link___ctn_id);
696     break;
697   case A_surfxml_link___ctn_direction_UP:
698     link_id = bprintf("%s_UP", A_surfxml_link___ctn_id);
699     break;
700   case A_surfxml_link___ctn_direction_DOWN:
701     link_id = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
702     break;
703   }
704
705   // FIXME we should push the surf link object but it don't
706   // work because of model rulebased
707   xbt_dynar_push(parsed_link_list, &link_id);
708 }
709
710 void ETag_surfxml_backbone(void){
711   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
712   memset(&link,0,sizeof(link));
713
714   link.properties = NULL;
715
716   link.id = A_surfxml_backbone_id;
717   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id);
718   link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id);
719   link.initiallyOn = 1;
720   link.policy = SURF_LINK_SHARED;
721
722   sg_platf_new_link(&link);
723   routing_cluster_add_backbone(sg_link_by_name(A_surfxml_backbone_id));
724 }
725
726 void STag_surfxml_route(void){
727   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
728       "Missing end-points while defining route \"%s\"->\"%s\"",
729       A_surfxml_route_src, A_surfxml_route_dst);
730   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
731 }
732
733 void STag_surfxml_ASroute(void){
734   xbt_assert(strlen(A_surfxml_ASroute_src) > 0
735              || strlen(A_surfxml_ASroute_dst) > 0
736              || strlen(A_surfxml_ASroute_gw___src) > 0
737              || strlen(A_surfxml_ASroute_gw___dst) > 0,
738              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
739              A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
740              A_surfxml_ASroute_gw___src, A_surfxml_ASroute_gw___dst);
741   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
742 }
743
744 void STag_surfxml_bypassRoute(void){
745   xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0
746              || strlen(A_surfxml_bypassRoute_dst) > 0,
747              "Missing end-points while defining bupass route \"%s\"->\"%s\"",
748              A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
749   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
750 }
751
752 void STag_surfxml_bypassASroute(void){
753   xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0
754              || strlen(A_surfxml_bypassASroute_dst) > 0
755              || strlen(A_surfxml_bypassASroute_gw___src) > 0
756              || strlen(A_surfxml_bypassASroute_gw___dst) > 0,
757              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
758              A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
759              A_surfxml_bypassASroute_gw___src,A_surfxml_bypassASroute_gw___dst);
760   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
761 }
762
763 void ETag_surfxml_route(void){
764   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
765   memset(&route,0,sizeof(route));
766
767   route.src       = A_surfxml_route_src;
768   route.dst       = A_surfxml_route_dst;
769   route.gw_src    = NULL;
770   route.gw_dst    = NULL;
771   route.link_list = parsed_link_list;
772
773   switch (A_surfxml_route_symmetrical) {
774   case AU_surfxml_route_symmetrical:
775   case A_surfxml_route_symmetrical_YES:
776     route.symmetrical = TRUE;
777     break;
778   case A_surfxml_route_symmetrical_NO:
779     route.symmetrical = FALSE;;
780     break;
781   }
782
783   sg_platf_new_route(&route);
784   parsed_link_list = NULL;
785 }
786
787 void ETag_surfxml_ASroute(void){
788   s_sg_platf_route_cbarg_t ASroute = SG_PLATF_ROUTE_INITIALIZER;
789   memset(&ASroute,0,sizeof(ASroute));
790
791   ASroute.src = A_surfxml_ASroute_src;
792   ASroute.dst = A_surfxml_ASroute_dst;
793
794   ASroute.gw_src = sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___src);
795   ASroute.gw_dst = sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___dst);
796
797   if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)
798     surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",
799                      A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);
800   if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)
801     surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",
802                      A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);
803
804   ASroute.link_list = parsed_link_list;
805
806   switch (A_surfxml_ASroute_symmetrical) {
807   case AU_surfxml_ASroute_symmetrical:
808   case A_surfxml_ASroute_symmetrical_YES:
809     ASroute.symmetrical = TRUE;
810     break;
811   case A_surfxml_ASroute_symmetrical_NO:
812     ASroute.symmetrical = FALSE;
813     break;
814   }
815
816   sg_platf_new_ASroute(&ASroute);
817   parsed_link_list = NULL;
818 }
819
820 void ETag_surfxml_bypassRoute(void){
821   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
822   memset(&route,0,sizeof(route));
823
824   route.src = A_surfxml_bypassRoute_src;
825   route.dst = A_surfxml_bypassRoute_dst;
826   route.gw_src = NULL;
827   route.gw_dst = NULL;
828   route.link_list = parsed_link_list;
829   route.symmetrical = FALSE;
830
831   sg_platf_new_bypassRoute(&route);
832   parsed_link_list = NULL;
833 }
834
835 void ETag_surfxml_bypassASroute(void){
836   s_sg_platf_route_cbarg_t ASroute = SG_PLATF_ROUTE_INITIALIZER;
837   memset(&ASroute,0,sizeof(ASroute));
838
839   ASroute.src         = A_surfxml_bypassASroute_src;
840   ASroute.dst         = A_surfxml_bypassASroute_dst;
841   ASroute.link_list   = parsed_link_list;
842   ASroute.symmetrical = FALSE;
843
844   ASroute.gw_src = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___src);
845   ASroute.gw_dst = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
846
847   sg_platf_new_bypassASroute(&ASroute);
848   parsed_link_list = NULL;
849 }
850
851 void ETag_surfxml_trace(void){
852   s_sg_platf_trace_cbarg_t trace = SG_PLATF_TRACE_INITIALIZER;
853   memset(&trace,0,sizeof(trace));
854
855   trace.id = A_surfxml_trace_id;
856   trace.file = A_surfxml_trace_file;
857   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
858   trace.pc_data = surfxml_pcdata;
859
860   sg_platf_new_trace(&trace);
861 }
862
863 void STag_surfxml_trace___connect(void){
864   parse_after_config();
865   s_sg_platf_trace_connect_cbarg_t trace_connect = SG_PLATF_TRACE_CONNECT_INITIALIZER;
866   memset(&trace_connect,0,sizeof(trace_connect));
867
868   trace_connect.element = A_surfxml_trace___connect_element;
869   trace_connect.trace = A_surfxml_trace___connect_trace;
870
871   switch (A_surfxml_trace___connect_kind) {
872   case AU_surfxml_trace___connect_kind:
873   case A_surfxml_trace___connect_kind_SPEED:
874     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_SPEED;
875     break;
876   case A_surfxml_trace___connect_kind_BANDWIDTH:
877     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
878     break;
879   case A_surfxml_trace___connect_kind_HOST___AVAIL:
880     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
881     break;
882   case A_surfxml_trace___connect_kind_LATENCY:
883     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
884     break;
885   case A_surfxml_trace___connect_kind_LINK___AVAIL:
886     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
887     break;
888   }
889   sg_platf_trace_connect(&trace_connect);
890 }
891
892 void STag_surfxml_AS(void){
893   parse_after_config();
894   AS_TAG                   = 1;
895   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
896   AS.id                    = A_surfxml_AS_id;
897   AS.routing               = (int)A_surfxml_AS_routing;
898
899   as_current_property_set = NULL;
900
901   sg_platf_new_AS_begin(&AS);
902 }
903 void ETag_surfxml_AS(void){
904   if(as_prop_nb){
905     char *name      = as_name_tab[as_prop_nb-1];
906     xbt_dict_t dict = (xbt_dict_t) as_dict_tab[as_prop_nb-1];
907     as_prop_nb--;
908     XBT_DEBUG("POP prop %p for AS '%s'",dict,name);
909     xbt_lib_set(as_router_lib,
910         name,
911       ROUTING_PROP_ASR_LEVEL,
912       dict);
913     xbt_free(name);
914   }
915   sg_platf_new_AS_end();
916 }
917
918 void STag_surfxml_config(void){
919   AS_TAG = 0;
920   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
921   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
922   if (_sg_cfg_init_status == 2) {
923     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
924   }
925 }
926 void ETag_surfxml_config(void){
927   xbt_dict_cursor_t cursor = NULL;
928   char *key;
929   char *elem;
930   char *cfg;
931   xbt_dict_foreach(current_property_set, cursor, key, elem) {
932     cfg = bprintf("%s:%s",key,elem);
933     if(xbt_cfg_is_default_value(_sg_cfg_set, key))
934       xbt_cfg_set_parse(_sg_cfg_set, cfg);
935     else
936       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
937     free(cfg);
938   }
939   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
940
941   xbt_dict_free(&current_property_set);
942   current_property_set = NULL;
943 }
944
945 static int argc;
946 static char **argv;
947
948 void STag_surfxml_process(void){
949   AS_TAG  = 0;
950   argc    = 1;
951   argv    = xbt_new(char *, 1);
952   argv[0] = xbt_strdup(A_surfxml_process_function);
953   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
954 }
955
956 void ETag_surfxml_process(void){
957   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
958   memset(&process,0,sizeof(process));
959
960   process.argc       = argc;
961   process.argv       = (const char **)argv;
962   process.properties = current_property_set;
963   process.host       = A_surfxml_process_host;
964   process.function   = A_surfxml_process_function;
965   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
966   process.kill_time  = surf_parse_get_double(A_surfxml_process_kill___time);
967
968   switch (A_surfxml_process_on___failure) {
969   case AU_surfxml_process_on___failure:
970   case A_surfxml_process_on___failure_DIE:
971     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
972     break;
973   case A_surfxml_process_on___failure_RESTART:
974     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
975     break;
976   }
977
978   sg_platf_new_process(&process);
979   current_property_set = NULL;
980 }
981
982 void STag_surfxml_argument(void){
983   argc++;
984   argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
985   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
986 }
987
988 void STag_surfxml_model___prop(void){
989   if (!current_model_property_set)
990     current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
991
992   xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), NULL);
993 }
994
995 void STag_surfxml_gpu(void) {}
996 void ETag_surfxml_gpu(void) {}
997
998 /* nothing to do in those functions */
999 void ETag_surfxml_prop(void){}
1000 void STag_surfxml_random(void){}
1001 void ETag_surfxml_random(void){}
1002 void ETag_surfxml_trace___connect(void){}
1003 void STag_surfxml_trace(void){parse_after_config();}
1004 void ETag_surfxml_router(void){}
1005 void ETag_surfxml_host___link(void){}
1006 void ETag_surfxml_cabinet(void){}
1007 void ETag_surfxml_peer(void){}
1008 void STag_surfxml_backbone(void){}
1009 void ETag_surfxml_link___ctn(void){}
1010 void ETag_surfxml_argument(void){}
1011 void ETag_surfxml_model___prop(void){}
1012
1013 /* Open and Close parse file */
1014
1015 void surf_parse_open(const char *file)
1016 {
1017   static int warned = 0;        /* warn only once */
1018   if (!file) {
1019     if (!warned) {
1020       XBT_WARN
1021           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
1022               "If it is not what you want, go fix your code.");
1023       warned = 1;
1024     }
1025     return;
1026   }
1027
1028   if (!surf_input_buffer_stack)
1029     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
1030   if (!surf_file_to_parse_stack)
1031     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
1032
1033   if (!surf_parsed_filename_stack)
1034     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1035
1036   surf_parsed_filename = xbt_strdup(file);
1037   char *dir = xbt_dirname(file);
1038   xbt_dynar_push(surf_path, &dir);
1039
1040   surf_file_to_parse = surf_fopen(file, "r");
1041   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1042   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1043   surf_parse__switch_to_buffer(surf_input_buffer);
1044   surf_parse_lineno = 1;
1045 }
1046
1047 void surf_parse_close(void)
1048 {
1049   xbt_dynar_free(&surf_input_buffer_stack);
1050   xbt_dynar_free(&surf_file_to_parse_stack);
1051   xbt_dynar_free(&surf_parsed_filename_stack);
1052   if (surf_parsed_filename) {
1053     char *dir = NULL;
1054     xbt_dynar_pop(surf_path, &dir);
1055     free(dir);
1056   }
1057
1058   free(surf_parsed_filename);
1059   surf_parsed_filename = NULL;
1060
1061   if (surf_file_to_parse) {
1062     surf_parse__delete_buffer(surf_input_buffer);
1063     fclose(surf_file_to_parse);
1064     surf_file_to_parse = NULL; //Must be reset for Bypass
1065   }
1066 }
1067
1068 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1069
1070 static int _surf_parse(void) {
1071   return surf_parse_lex();
1072 }
1073 int_f_void_t surf_parse = _surf_parse;
1074
1075
1076 /* Prop tag functions */
1077
1078 /**
1079  * With XML parser
1080  */
1081 xbt_dict_t get_as_router_properties(const char* name)
1082 {
1083   return (xbt_dict_t)xbt_lib_get_or_null(as_router_lib, name, ROUTING_PROP_ASR_LEVEL);
1084 }
1085