Logo AND Algorithmique Numérique Distribuée

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