Logo AND Algorithmique Numérique Distribuée

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