Logo AND Algorithmique Numérique Distribuée

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