Logo AND Algorithmique Numérique Distribuée

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