Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006-2014. 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 "surf/random_mgr.h"
18 #include "simgrid/sg_config.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 extern AS_t current_routing;
31 /*
32  * Helping functions
33  */
34 void surf_parse_error(const char *fmt, ...) {
35   va_list va;
36   va_start(va,fmt);
37   int lineno = surf_parse_lineno;
38   char *msg = bvprintf(fmt,va);
39   va_end(va);
40   cleanup();
41   surf_exit();
42   xbt_die("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
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_power(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 as_current_property_set = NULL;
207 int AS_TAG = 0;
208 char* as_name_tab[1024];
209 void* as_dict_tab[1024];
210 int as_prop_nb = 0;
211
212
213 /* dictionary of random generator data */
214 xbt_dict_t random_data_list = NULL;
215
216 YY_BUFFER_STATE surf_input_buffer;
217 FILE *surf_file_to_parse = NULL;
218
219 static void init_randomness(void);
220 static void add_randomness(void);
221
222 /*
223  * Stuff relative to storage
224  */
225 void STag_surfxml_storage(void)
226 {
227   AS_TAG = 0;
228   XBT_DEBUG("STag_surfxml_storage");
229   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
230 }
231 void ETag_surfxml_storage(void)
232 {
233   s_sg_platf_storage_cbarg_t storage;
234   memset(&storage,0,sizeof(storage));
235
236   storage.id = A_surfxml_storage_id;
237   storage.type_id = A_surfxml_storage_typeId;
238   storage.content = A_surfxml_storage_content;
239   storage.content_type = A_surfxml_storage_content___type;
240   storage.properties = current_property_set;
241   sg_platf_new_storage(&storage);
242   current_property_set = NULL;
243 }
244 void STag_surfxml_storage___type(void)
245 {
246   AS_TAG = 0;
247   XBT_DEBUG("STag_surfxml_storage___type");
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___type(void)
251 {
252   s_sg_platf_storage_type_cbarg_t storage_type;
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.size = surf_parse_get_size(A_surfxml_storage___type_size);
261   sg_platf_new_storage_type(&storage_type);
262   current_property_set = NULL;
263 }
264 void STag_surfxml_mstorage(void)
265 {
266   XBT_DEBUG("STag_surfxml_mstorage");
267 }
268 void ETag_surfxml_mstorage(void)
269 {
270   s_sg_platf_mstorage_cbarg_t mstorage;
271   memset(&mstorage,0,sizeof(mstorage));
272
273   mstorage.name = A_surfxml_mstorage_name;
274   mstorage.type_id = A_surfxml_mstorage_typeId;
275   sg_platf_new_mstorage(&mstorage);
276 }
277 void STag_surfxml_mount(void)
278 {
279   XBT_DEBUG("STag_surfxml_mount");
280 }
281 void ETag_surfxml_mount(void)
282 {
283   s_sg_platf_mount_cbarg_t mount;
284   memset(&mount,0,sizeof(mount));
285
286   mount.name = A_surfxml_mount_name;
287   mount.storageId = A_surfxml_mount_storageId;
288   sg_platf_new_mount(&mount);
289 }
290
291 /*
292  * Stuff relative to the <include> tag
293  */
294 static xbt_dynar_t surf_input_buffer_stack = NULL;
295 static xbt_dynar_t surf_file_to_parse_stack = NULL;
296 static xbt_dynar_t surf_parsed_filename_stack = NULL;
297
298 void STag_surfxml_include(void)
299 {
300   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
301   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
302   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
303
304   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
305
306   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
307   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
308               A_surfxml_include_file);
309   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
310   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
311   surf_parse_push_buffer_state(surf_input_buffer);
312
313   fflush(NULL);
314 }
315
316 void ETag_surfxml_include(void) {
317 /* Nothing to do when done with reading the include tag.
318  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
319 }
320
321 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
322  *
323  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
324  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
325  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
326  * error message in that case.
327  *
328  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
329  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
330  * on a line. One day maybe, if the maya allow it.
331  */
332 int ETag_surfxml_include_state(void)
333 {
334   fflush(NULL);
335   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
336
337   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
338     return 0;
339
340   // Yeah, we were in an <include> Restore state and proceed.
341   fclose(surf_file_to_parse);
342   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
343   surf_parse_pop_buffer_state();
344   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
345
346   // Restore the filename for error messages
347   free(surf_parsed_filename);
348   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
349
350   return 1;
351 }
352
353
354 void surf_parse_init_callbacks(void)
355 {
356     sg_platf_init();
357 }
358
359 void surf_parse_reset_callbacks(void)
360 {
361   surf_parse_free_callbacks();
362   surf_parse_init_callbacks();
363 }
364
365 void surf_parse_free_callbacks(void)
366 {
367   sg_platf_exit();
368 }
369
370 /* Stag and Etag parse functions */
371
372 void STag_surfxml_platform(void) {
373   _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
374
375   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
376       "You're using an ancient XML file.\n"
377       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
378       "instead of MBytes, MFlops and seconds.\n"
379
380       "Use simgrid_update_xml to update your file automatically. "
381       "This program is installed automatically with SimGrid, or "
382       "available in the tools/ directory of the source archive.\n"
383
384       "Please check also out the SURF section of the ChangeLog for "
385       "the 3.1 version for more information. \n"
386
387       "Last, do not forget to also update your values for "
388       "the calls to MSG_task_create (if any).");
389   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
390       "You're using an old XML file.\n"
391       "Use simgrid_update_xml to update your file automatically. "
392       "This program is installed automatically with SimGrid, or "
393       "available in the tools/ directory of the source archive.");
394
395   sg_platf_begin();
396 }
397 void ETag_surfxml_platform(void){
398   sg_platf_end();
399 }
400
401 void STag_surfxml_host(void){
402   AS_TAG = 0;
403   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
404 }
405
406 void STag_surfxml_prop(void)
407 {
408   if(AS_TAG){
409     if (!as_current_property_set){
410       xbt_assert(as_prop_nb < 1024, "Number of AS property reach the limit!!!");
411       as_current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
412       as_name_tab[as_prop_nb] = xbt_strdup(A_surfxml_AS_id);
413       as_dict_tab[as_prop_nb] = as_current_property_set;
414       XBT_DEBUG("PUSH prop %p for AS '%s'",as_dict_tab[as_prop_nb],as_name_tab[as_prop_nb]);
415       as_prop_nb++;
416     }
417     xbt_dict_set(as_current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
418   }
419   else{
420     if (!current_property_set)
421       current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
422     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
423   }
424 }
425
426 void ETag_surfxml_host(void)    {
427   s_sg_platf_host_cbarg_t host;
428   char* buf;
429   memset(&host,0,sizeof(host));
430
431
432   host.properties = current_property_set;
433
434   host.id = A_surfxml_host_id;
435
436   buf = A_surfxml_host_power;
437   XBT_DEBUG("Buffer: %s", buf);
438   host.power_peak = xbt_dynar_new(sizeof(double), NULL);
439   if (strchr(buf, ',') == NULL){
440           double power_value = get_cpu_power(A_surfxml_host_power);
441           xbt_dynar_push_as(host.power_peak,double, power_value);
442   }
443   else {
444           xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
445           int i;
446           for (i = 0; i < xbt_dynar_length(pstate_list); i++) {
447                   double power_value;
448                   char* power_value_str;
449
450                   xbt_dynar_get_cpy(pstate_list, i, &power_value_str);
451                   xbt_str_trim(power_value_str, NULL);
452                   power_value = get_cpu_power(power_value_str);
453                   xbt_dynar_push_as(host.power_peak, double, power_value);
454                   XBT_DEBUG("Power value: %f", power_value);
455           }
456           xbt_dynar_free(&pstate_list);
457   }
458
459   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
460   //host.power_peak = get_cpu_power(A_surfxml_host_power);
461   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
462   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
463   host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file);
464   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file);
465   host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
466
467   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
468         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
469   if (A_surfxml_host_state == A_surfxml_host_state_ON)
470     host.initial_state = SURF_RESOURCE_ON;
471   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
472     host.initial_state = SURF_RESOURCE_OFF;
473   host.coord = A_surfxml_host_coordinates;
474
475   sg_platf_new_host(&host);
476   current_property_set = NULL;
477 }
478
479 void STag_surfxml_host___link(void){
480   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
481   s_sg_platf_host_link_cbarg_t host_link;
482   memset(&host_link,0,sizeof(host_link));
483
484   host_link.id = A_surfxml_host___link_id;
485   host_link.link_up = A_surfxml_host___link_up;
486   host_link.link_down = A_surfxml_host___link_down;
487   sg_platf_new_host_link(&host_link);
488 }
489
490 void STag_surfxml_router(void){
491   s_sg_platf_router_cbarg_t router;
492   memset(&router, 0, sizeof(router));
493
494   router.id = A_surfxml_router_id;
495   router.coord = A_surfxml_router_coordinates;
496
497   sg_platf_new_router(&router);
498 }
499
500 void ETag_surfxml_cluster(void){
501   s_sg_platf_cluster_cbarg_t cluster;
502   memset(&cluster,0,sizeof(cluster));
503   cluster.properties = current_property_set;
504
505   cluster.id = A_surfxml_cluster_id;
506   cluster.prefix = A_surfxml_cluster_prefix;
507   cluster.suffix = A_surfxml_cluster_suffix;
508   cluster.radical = A_surfxml_cluster_radical;
509   cluster.power = surf_parse_get_power(A_surfxml_cluster_power);
510   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
511   cluster.bw =   surf_parse_get_bandwidth(A_surfxml_cluster_bw);
512   cluster.lat =  surf_parse_get_time(A_surfxml_cluster_lat);
513   if(strcmp(A_surfxml_cluster_bb___bw,""))
514     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw);
515   if(strcmp(A_surfxml_cluster_bb___lat,""))
516     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat);
517   if(strcmp(A_surfxml_cluster_limiter___link,""))
518     cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
519   if(strcmp(A_surfxml_cluster_loopback___bw,""))
520     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw);
521   if(strcmp(A_surfxml_cluster_loopback___lat,""))
522     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat);
523
524   switch(AX_surfxml_cluster_topology){
525   case A_surfxml_cluster_topology_FLAT:
526     cluster.topology= SURF_CLUSTER_FLAT ;
527     break;
528   case A_surfxml_cluster_topology_TORUS:
529     cluster.topology= SURF_CLUSTER_TORUS ;
530     break;
531   default:
532     surf_parse_error("Invalid cluster topology for cluster %s",
533                      cluster.id);
534     break;
535   }
536   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
537   cluster.router_id = A_surfxml_cluster_router___id;
538
539   switch (AX_surfxml_cluster_sharing___policy) {
540   case A_surfxml_cluster_sharing___policy_SHARED:
541     cluster.sharing_policy = SURF_LINK_SHARED;
542     break;
543   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
544     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
545     break;
546   case A_surfxml_cluster_sharing___policy_FATPIPE:
547     cluster.sharing_policy = SURF_LINK_FATPIPE;
548     break;
549   default:
550     surf_parse_error("Invalid cluster sharing policy for cluster %s",
551                      cluster.id);
552     break;
553   }
554   switch (AX_surfxml_cluster_bb___sharing___policy) {
555   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
556     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
557     break;
558   case A_surfxml_cluster_bb___sharing___policy_SHARED:
559     cluster.bb_sharing_policy = SURF_LINK_SHARED;
560     break;
561   default:
562     surf_parse_error("Invalid bb sharing policy in cluster %s",
563                      cluster.id);
564     break;
565   }
566
567   cluster.availability_trace = A_surfxml_cluster_availability___file;
568   cluster.state_trace = A_surfxml_cluster_state___file;
569   sg_platf_new_cluster(&cluster);
570   
571   current_property_set = NULL;
572 }
573
574 void STag_surfxml_cluster(void){ 
575   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
576 }
577
578 void STag_surfxml_cabinet(void){
579   s_sg_platf_cabinet_cbarg_t cabinet;
580   memset(&cabinet,0,sizeof(cabinet));
581   cabinet.id = A_surfxml_cabinet_id;
582   cabinet.prefix = A_surfxml_cabinet_prefix;
583   cabinet.suffix = A_surfxml_cabinet_suffix;
584   cabinet.power = surf_parse_get_power(A_surfxml_cabinet_power);
585   cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw);
586   cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat);
587   cabinet.radical = A_surfxml_cabinet_radical;
588
589   sg_platf_new_cabinet(&cabinet);
590 }
591
592 void STag_surfxml_peer(void){
593   s_sg_platf_peer_cbarg_t peer;
594   memset(&peer,0,sizeof(peer));
595   peer.id = A_surfxml_peer_id;
596   peer.power = surf_parse_get_power(A_surfxml_peer_power);
597   peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in);
598   peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out);
599   peer.lat = surf_parse_get_time(A_surfxml_peer_lat);
600   peer.coord = A_surfxml_peer_coordinates;
601   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
602   peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
603
604   sg_platf_new_peer(&peer);
605 }
606
607 void STag_surfxml_link(void){
608   AS_TAG = 0;
609   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
610 }
611
612 void ETag_surfxml_link(void){
613   s_sg_platf_link_cbarg_t link;
614   memset(&link,0,sizeof(link));
615
616   link.properties = current_property_set;
617
618   link.id = A_surfxml_link_id;
619   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth);
620   //printf("Link bandwidth [%g]\n", link.bandwidth);
621   link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
622   link.latency = surf_parse_get_time(A_surfxml_link_latency);
623   //printf("Link latency [%g]\n", link.latency);
624   link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
625
626   switch (A_surfxml_link_state) {
627   case A_surfxml_link_state_ON:
628     link.state = SURF_RESOURCE_ON;
629     break;
630   case A_surfxml_link_state_OFF:
631     link.state = SURF_RESOURCE_OFF;
632     break;
633   default:
634     surf_parse_error("invalid state for link %s", link.id);
635     break;
636   }
637   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file);
638
639   switch (A_surfxml_link_sharing___policy) {
640   case A_surfxml_link_sharing___policy_SHARED:
641     link.policy = SURF_LINK_SHARED;
642     break;
643   case A_surfxml_link_sharing___policy_FATPIPE:
644      link.policy = SURF_LINK_FATPIPE;
645      break;
646   case A_surfxml_link_sharing___policy_FULLDUPLEX:
647      link.policy = SURF_LINK_FULLDUPLEX;
648      break;
649   default:
650     surf_parse_error("Invalid sharing policy in link %s", link.id);
651     break;
652   }
653
654   sg_platf_new_link(&link);
655
656   current_property_set = NULL;
657 }
658
659 void STag_surfxml_link___ctn(void){
660
661   char *link_id;
662   switch (A_surfxml_link___ctn_direction) {
663   case AU_surfxml_link___ctn_direction:
664   case A_surfxml_link___ctn_direction_NONE:
665     link_id = xbt_strdup(A_surfxml_link___ctn_id);
666     break;
667   case A_surfxml_link___ctn_direction_UP:
668     link_id = bprintf("%s_UP", A_surfxml_link___ctn_id);
669     break;
670   case A_surfxml_link___ctn_direction_DOWN:
671     link_id = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
672     break;
673   }
674
675   // FIXME we should push the surf link object but it don't
676   // work because of model rulebased
677   xbt_dynar_push(parsed_link_list, &link_id);
678 }
679
680 void ETag_surfxml_backbone(void){
681   s_sg_platf_link_cbarg_t link;
682   memset(&link,0,sizeof(link));
683
684   link.properties = NULL;
685
686   link.id = A_surfxml_backbone_id;
687   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth);
688   link.latency = surf_parse_get_time(A_surfxml_backbone_latency);
689   link.state = SURF_RESOURCE_ON;
690   link.policy = SURF_LINK_SHARED;
691
692   sg_platf_new_link(&link);
693   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
694 }
695
696 void STag_surfxml_route(void){
697   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
698       "Missing end-points while defining route \"%s\"->\"%s\"",
699       A_surfxml_route_src, A_surfxml_route_dst);
700   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
701 }
702
703 void STag_surfxml_ASroute(void){
704   xbt_assert(strlen(A_surfxml_ASroute_src) > 0
705              || strlen(A_surfxml_ASroute_dst) > 0
706              || strlen(A_surfxml_ASroute_gw___src) > 0
707              || strlen(A_surfxml_ASroute_gw___dst) > 0,
708              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
709              A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
710              A_surfxml_ASroute_gw___src, A_surfxml_ASroute_gw___dst);
711   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
712 }
713
714 void STag_surfxml_bypassRoute(void){
715   xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0
716              || strlen(A_surfxml_bypassRoute_dst) > 0,
717              "Missing end-points while defining bupass route \"%s\"->\"%s\"",
718              A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
719   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
720 }
721
722 void STag_surfxml_bypassASroute(void){
723   xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0
724              || strlen(A_surfxml_bypassASroute_dst) > 0
725              || strlen(A_surfxml_bypassASroute_gw___src) > 0
726              || strlen(A_surfxml_bypassASroute_gw___dst) > 0,
727              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
728              A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
729              A_surfxml_bypassASroute_gw___src,A_surfxml_bypassASroute_gw___dst);
730   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
731 }
732
733 void ETag_surfxml_route(void){
734   s_sg_platf_route_cbarg_t route;
735   memset(&route,0,sizeof(route));
736
737   route.src = A_surfxml_route_src;
738   route.dst = A_surfxml_route_dst;
739   route.gw_src = NULL;
740   route.gw_dst = NULL;
741   route.link_list = parsed_link_list;
742
743   switch (A_surfxml_route_symmetrical) {
744   case AU_surfxml_route_symmetrical:
745   case A_surfxml_route_symmetrical_YES:
746     route.symmetrical = TRUE;
747     break;
748   case A_surfxml_route_symmetrical_NO:
749     route.symmetrical = FALSE;;
750     break;
751   }
752
753   sg_platf_new_route(&route);
754   parsed_link_list = NULL;
755 }
756
757 void ETag_surfxml_ASroute(void){
758   s_sg_platf_route_cbarg_t ASroute;
759   memset(&ASroute,0,sizeof(ASroute));
760
761   ASroute.src = A_surfxml_ASroute_src;
762   ASroute.dst = A_surfxml_ASroute_dst;
763
764   ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___src);
765   ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___dst);
766
767   ASroute.link_list = parsed_link_list;
768
769   switch (A_surfxml_ASroute_symmetrical) {
770   case AU_surfxml_ASroute_symmetrical:
771   case A_surfxml_ASroute_symmetrical_YES:
772     ASroute.symmetrical = TRUE;
773     break;
774   case A_surfxml_ASroute_symmetrical_NO:
775     ASroute.symmetrical = FALSE;
776     break;
777   }
778
779   sg_platf_new_ASroute(&ASroute);
780   parsed_link_list = NULL;
781 }
782
783 void ETag_surfxml_bypassRoute(void){
784   s_sg_platf_route_cbarg_t route;
785   memset(&route,0,sizeof(route));
786
787   route.src = A_surfxml_bypassRoute_src;
788   route.dst = A_surfxml_bypassRoute_dst;
789   route.gw_src = NULL;
790   route.gw_dst = NULL;
791   route.link_list = parsed_link_list;
792   route.symmetrical = FALSE;
793
794   sg_platf_new_bypassRoute(&route);
795   parsed_link_list = NULL;
796 }
797
798 void ETag_surfxml_bypassASroute(void){
799   s_sg_platf_route_cbarg_t ASroute;
800   memset(&ASroute,0,sizeof(ASroute));
801
802   ASroute.src = A_surfxml_bypassASroute_src;
803   ASroute.dst = A_surfxml_bypassASroute_dst;
804   ASroute.link_list = parsed_link_list;
805   ASroute.symmetrical = FALSE;
806
807   ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___src);
808   ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
809
810   sg_platf_new_bypassASroute(&ASroute);
811   parsed_link_list = NULL;
812 }
813
814 void ETag_surfxml_trace(void){
815   s_sg_platf_trace_cbarg_t trace;
816   memset(&trace,0,sizeof(trace));
817
818   trace.id = A_surfxml_trace_id;
819   trace.file = A_surfxml_trace_file;
820   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
821   trace.pc_data = surfxml_pcdata;
822
823   sg_platf_new_trace(&trace);
824 }
825
826 void STag_surfxml_trace___connect(void){
827   s_sg_platf_trace_connect_cbarg_t trace_connect;
828   memset(&trace_connect,0,sizeof(trace_connect));
829
830   trace_connect.element = A_surfxml_trace___connect_element;
831   trace_connect.trace = A_surfxml_trace___connect_trace;
832
833   switch (A_surfxml_trace___connect_kind) {
834   case AU_surfxml_trace___connect_kind:
835   case A_surfxml_trace___connect_kind_POWER:
836     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_POWER;
837     break;
838   case A_surfxml_trace___connect_kind_BANDWIDTH:
839     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
840     break;
841   case A_surfxml_trace___connect_kind_HOST___AVAIL:
842     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
843     break;
844   case A_surfxml_trace___connect_kind_LATENCY:
845     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
846     break;
847   case A_surfxml_trace___connect_kind_LINK___AVAIL:
848     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
849     break;
850   }
851   sg_platf_trace_connect(&trace_connect);
852 }
853
854 void STag_surfxml_AS(void){
855   AS_TAG = 1;
856   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
857   AS.id = A_surfxml_AS_id;
858   AS.routing = (int)A_surfxml_AS_routing;
859
860   as_current_property_set = NULL;
861
862   sg_platf_new_AS_begin(&AS);
863 }
864 void ETag_surfxml_AS(void){
865   if(as_prop_nb){
866     char *name = as_name_tab[as_prop_nb-1];
867     xbt_dict_t dict = as_dict_tab[as_prop_nb-1];
868     as_prop_nb--;
869     XBT_DEBUG("POP prop %p for AS '%s'",dict,name);
870     xbt_lib_set(as_router_lib,
871         name,
872       ROUTING_PROP_ASR_LEVEL,
873       dict);
874     xbt_free(name);
875   }
876   sg_platf_new_AS_end();
877 }
878
879 void STag_surfxml_config(void){
880   AS_TAG = 0;
881   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
882   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
883   if (_sg_cfg_init_status == 2) {
884     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
885   }
886 }
887 void ETag_surfxml_config(void){
888   xbt_dict_cursor_t cursor = NULL;
889   char *key;
890   char *elem;
891   char *cfg;
892   xbt_dict_foreach(current_property_set, cursor, key, elem) {
893     cfg = bprintf("%s:%s",key,elem);
894     if(xbt_cfg_is_default_value(_sg_cfg_set, key))
895       xbt_cfg_set_parse(_sg_cfg_set, cfg);
896     else
897       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
898     free(cfg);
899   }
900   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
901   xbt_dict_free(&current_property_set);
902   current_property_set = NULL;
903 }
904
905 static int argc;
906 static char **argv;
907
908 void STag_surfxml_process(void){
909   AS_TAG = 0;
910   argc = 1;
911   argv = xbt_new(char *, 1);
912   argv[0] = xbt_strdup(A_surfxml_process_function);
913   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
914 }
915
916 void ETag_surfxml_process(void){
917   s_sg_platf_process_cbarg_t process;
918   memset(&process,0,sizeof(process));
919
920   process.argc = argc;
921   process.argv = (const char **)argv;
922   process.properties = current_property_set;
923   process.host = A_surfxml_process_host;
924   process.function = A_surfxml_process_function;
925   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
926   process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
927
928   switch (A_surfxml_process_on___failure) {
929   case AU_surfxml_process_on___failure:
930   case A_surfxml_process_on___failure_DIE:
931     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
932     break;
933   case A_surfxml_process_on___failure_RESTART:
934     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
935     break;
936   }
937
938   sg_platf_new_process(&process);
939   current_property_set = NULL;
940 }
941
942 void STag_surfxml_argument(void){
943   argc++;
944   argv = xbt_realloc(argv, (argc) * sizeof(char *));
945   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
946 }
947
948 /* ***************************************** */
949 /* TUTORIAL: New TAG                         */
950 void STag_surfxml_gpu(void)
951 {
952   XBT_DEBUG("STag_surfxml_gpu");
953 }
954 void ETag_surfxml_gpu(void)
955 {
956   s_sg_platf_gpu_cbarg_t gpu;
957   memset(&gpu,0,sizeof(gpu));
958
959   gpu.name = A_surfxml_gpu_name;
960
961   sg_platf_new_gpu(&gpu);
962 }
963 /* ***************************************** */
964
965 /* nothing to do in those functions */
966 void ETag_surfxml_prop(void){}
967 void STag_surfxml_random(void){}
968 void ETag_surfxml_random(void){}
969 void ETag_surfxml_trace___connect(void){}
970 void STag_surfxml_trace(void){}
971 void ETag_surfxml_router(void){}
972 void ETag_surfxml_host___link(void){}
973 void ETag_surfxml_cabinet(void){}
974 void ETag_surfxml_peer(void){}
975 void STag_surfxml_backbone(void){}
976 void ETag_surfxml_link___ctn(void){}
977 void ETag_surfxml_argument(void){}
978
979 /* Open and Close parse file */
980
981 void surf_parse_open(const char *file)
982 {
983   static int warned = 0;        /* warn only once */
984   if (!file) {
985     if (!warned) {
986       XBT_WARN
987           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
988               "If it is not what you want, go fix your code.");
989       warned = 1;
990     }
991     return;
992   }
993
994   if (!surf_input_buffer_stack)
995     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
996   if (!surf_file_to_parse_stack)
997     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
998
999   if (!surf_parsed_filename_stack)
1000     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1001   surf_parsed_filename = xbt_strdup(file);
1002
1003   surf_file_to_parse = surf_fopen(file, "r");
1004   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1005   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1006   surf_parse__switch_to_buffer(surf_input_buffer);
1007   surf_parse_lineno = 1;
1008 }
1009
1010 void surf_parse_close(void)
1011 {
1012   xbt_dynar_free(&surf_input_buffer_stack);
1013   xbt_dynar_free(&surf_file_to_parse_stack);
1014   xbt_dynar_free(&surf_parsed_filename_stack);
1015
1016   free(surf_parsed_filename);
1017   surf_parsed_filename = NULL;
1018
1019   if (surf_file_to_parse) {
1020     surf_parse__delete_buffer(surf_input_buffer);
1021     fclose(surf_file_to_parse);
1022     surf_file_to_parse = NULL; //Must be reset for Bypass
1023   }
1024 }
1025
1026 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1027
1028 static int _surf_parse(void) {
1029   return surf_parse_lex();
1030 }
1031 int_f_void_t surf_parse = _surf_parse;
1032
1033
1034 /* Prop tag functions */
1035
1036 /**
1037  * With XML parser
1038  */
1039
1040 /* Random tag functions */
1041
1042 double get_cpu_power(const char *power)
1043 {
1044   double power_scale = 0.0;
1045   const char *p, *q;
1046   char *generator;
1047   random_data_t random = NULL;
1048   /* randomness is inserted like this: power="$rand(my_random)" */
1049   if (((p = strstr(power, "$rand(")) != NULL)
1050       && ((q = strstr(power, ")")) != NULL)) {
1051     if (p < q) {
1052       generator = xbt_malloc(q - (p + 6) + 1);
1053       memcpy(generator, p + 6, q - (p + 6));
1054       generator[q - (p + 6)] = '\0';
1055       random = xbt_dict_get_or_null(random_data_list, generator);
1056       xbt_assert(random, "Random generator %s undefined", generator);
1057       power_scale = random_generate(random);
1058     }
1059   } else {
1060     power_scale = surf_parse_get_power(power);
1061   }
1062   return power_scale;
1063 }
1064
1065 double random_min, random_max, random_mean, random_std_deviation;
1066 e_random_generator_t random_generator;
1067 char *random_id;
1068
1069 static void init_randomness(void)
1070 {
1071   random_id = A_surfxml_random_id;
1072   random_min = surf_parse_get_double(A_surfxml_random_min);
1073   random_max = surf_parse_get_double(A_surfxml_random_max);
1074   random_mean = surf_parse_get_double(A_surfxml_random_mean);
1075   random_std_deviation = surf_parse_get_double(A_surfxml_random_std___deviation);
1076   switch (A_surfxml_random_generator) {
1077   case AU_surfxml_random_generator:
1078   case A_surfxml_random_generator_NONE:
1079     random_generator = NONE;
1080     break;
1081   case A_surfxml_random_generator_DRAND48:
1082     random_generator = DRAND48;
1083     break;
1084   case A_surfxml_random_generator_RAND:
1085     random_generator = RAND;
1086     break;
1087   case A_surfxml_random_generator_RNGSTREAM:
1088     random_generator = RNGSTREAM;
1089     break;
1090   default:
1091     surf_parse_error("Invalid random generator");
1092     break;
1093   }
1094 }
1095
1096 static void add_randomness(void)
1097 {
1098   /* If needed, additional properties can be added by using the prop tag */
1099   random_data_t random =
1100       random_new(random_generator, 0, random_min, random_max, random_mean,
1101                  random_std_deviation);
1102   xbt_dict_set(random_data_list, random_id, (void *) random,
1103                &xbt_free_ref);
1104 }
1105
1106
1107 xbt_dict_t get_as_router_properties(const char* name)
1108 {
1109   return xbt_lib_get_or_null(as_router_lib, name, ROUTING_PROP_ASR_LEVEL);
1110 }
1111