Logo AND Algorithmique Numérique Distribuée

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