Logo AND Algorithmique Numérique Distribuée

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