Logo AND Algorithmique Numérique Distribuée

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