Logo AND Algorithmique Numérique Distribuée

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