Logo AND Algorithmique Numérique Distribuée

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