Logo AND Algorithmique Numérique Distribuée

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