Logo AND Algorithmique Numérique Distribuée

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