Logo AND Algorithmique Numérique Distribuée

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