Logo AND Algorithmique Numérique Distribuée

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