Logo AND Algorithmique Numérique Distribuée

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