Logo AND Algorithmique Numérique Distribuée

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