Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not allocate/free properties
[simgrid.git] / src / surf / xml / surfxml_sax_cb.cpp
1 /* Copyright (c) 2006-2021. 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/Exception.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/sg_config.hpp"
10 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/xml/platf_private.hpp"
15 #include "surf/surf.hpp"
16 #include "xbt/file.hpp"
17 #include "xbt/parse_units.hpp"
18
19 #include <boost/algorithm/string/classification.hpp>
20 #include <boost/algorithm/string/split.hpp>
21 #include <string>
22 #include <tuple>
23 #include <unordered_map>
24 #include <vector>
25
26 #include "simgrid_dtd.c"
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
29
30 std::string surf_parsed_filename; // Currently parsed file (for the error messages)
31 std::vector<simgrid::kernel::resource::LinkImpl*>
32     parsed_link_list; /* temporary store of current link list of a route */
33 std::vector<simgrid::kernel::resource::DiskImpl*> parsed_disk_list; /* temporary store of current disk list of a host */
34 /*
35  * Helping functions
36  */
37 void surf_parse_assert(bool cond, const std::string& msg)
38 {
39   if (not cond)
40     surf_parse_error(msg);
41 }
42
43 void surf_parse_error(const std::string& msg)
44 {
45   throw simgrid::ParseError(surf_parsed_filename, surf_parse_lineno, msg);
46 }
47
48 void surf_parse_assert_netpoint(const std::string& hostname, const std::string& pre, const std::string& post)
49 {
50   if (simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(hostname) != nullptr) // found
51     return;
52
53   std::string msg = pre + hostname + post + " Existing netpoints: \n";
54
55   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
56       simgrid::s4u::Engine::get_instance()->get_all_netpoints();
57   std::sort(netpoints.begin(), netpoints.end(),
58             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
59               return a->get_name() < b->get_name();
60             });
61   bool first = true;
62   for (auto const& np : netpoints) {
63     if (np->is_netzone())
64       continue;
65
66     if (not first)
67       msg += ",";
68     first = false;
69     msg += "'" + np->get_name() + "'";
70     if (msg.length() > 4096) {
71       msg.pop_back(); // remove trailing quote
72       msg += "...(list truncated)......";
73       break;
74     }
75   }
76   surf_parse_error(msg);
77 }
78
79 double surf_parse_get_double(const std::string& s)
80 {
81   try {
82     return std::stod(s);
83   } catch (const std::invalid_argument&) {
84     surf_parse_error(s + " is not a double");
85   }
86 }
87
88 int surf_parse_get_int(const std::string& s)
89 {
90   try {
91     return std::stoi(s);
92   } catch (const std::invalid_argument&) {
93     surf_parse_error(s + " is not a double");
94   }
95 }
96
97 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
98 static void explodesRadical(const std::string& radicals, std::vector<int>* exploded)
99 {
100   // Make all hosts
101   std::vector<std::string> radical_elements;
102   boost::split(radical_elements, radicals, boost::is_any_of(","));
103   for (auto const& group : radical_elements) {
104     std::vector<std::string> radical_ends;
105     boost::split(radical_ends, group, boost::is_any_of("-"));
106     int start = surf_parse_get_int(radical_ends.front());
107     int end   = 0;
108
109     switch (radical_ends.size()) {
110       case 1:
111         end = start;
112         break;
113       case 2:
114         end = surf_parse_get_int(radical_ends.back());
115         break;
116       default:
117         surf_parse_error(std::string("Malformed radical: ") + group);
118     }
119     for (int i = start; i <= end; i++)
120       exploded->push_back(i);
121   }
122 }
123
124
125 /*
126  * All the callback lists that can be overridden anywhere.
127  * (this list should probably be reduced to the bare minimum to allow the models to work)
128  */
129
130 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
131
132 std::vector<std::unordered_map<std::string, std::string>> property_sets;
133
134 /* The default current property receiver. Setup in the corresponding opening callbacks. */
135 std::unordered_map<std::string, std::string> current_model_property_set;
136
137 FILE *surf_file_to_parse = nullptr;
138
139 /* Stuff relative to storage */
140 void STag_surfxml_storage()
141 {
142   xbt_die("<storage> tag was removed in SimGrid v3.27. Please stop using it now.");
143 }
144
145 void ETag_surfxml_storage()
146 {
147   /* Won't happen since <storage> is now removed since v3.27. */
148 }
149 void STag_surfxml_storage___type()
150 {
151   xbt_die("<storage_type> tag was removed in SimGrid v3.27. Please stop using it now.");
152 }
153 void ETag_surfxml_storage___type()
154 {
155   /* Won't happen since <storage_type> is now removed since v3.27. */
156 }
157
158 void STag_surfxml_mount()
159 {
160   xbt_die("<mount> tag was removed in SimGrid v3.27. Please stop using it now.");
161 }
162
163 void ETag_surfxml_mount()
164 {
165   /* Won't happen since <mount> is now removed since v3.27. */
166 }
167
168 void STag_surfxml_include()
169 {
170   xbt_die("<include> tag was removed in SimGrid v3.18. Please stop using it now.");
171 }
172
173 void ETag_surfxml_include()
174 {
175   /* Won't happen since <include> is now removed since v3.18. */
176 }
177
178 /* Stag and Etag parse functions */
179 void STag_surfxml_platform() {
180   double version = surf_parse_get_double(A_surfxml_platform_version);
181
182   surf_parse_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
183       "You're using an ancient XML file.\n"
184       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
185       "instead of MBytes, MFlops and seconds.\n"
186
187       "Use simgrid_update_xml to update your file automatically. "
188       "This program is installed automatically with SimGrid, or "
189       "available in the tools/ directory of the source archive.\n"
190
191       "Please check also out the SURF section of the ChangeLog for "
192       "the 3.1 version for more information. \n"
193
194       "Last, do not forget to also update your values for "
195       "the calls to MSG_task_create (if any).");
196   surf_parse_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
197       "You're using an old XML file.\n"
198       "Use simgrid_update_xml to update your file automatically. "
199       "This program is installed automatically with SimGrid, or "
200       "available in the tools/ directory of the source archive.");
201   surf_parse_assert((version >= 4.0),
202              std::string("******* THIS FILE IS TOO OLD (v:")+std::to_string(version)+") *********\n "
203              "Changes introduced in SimGrid 3.13:\n"
204              "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
205              "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
206              "  - DOCTYPE now point to the rignt URL.\n"
207              "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
208              "\n\n"
209              "Use simgrid_update_xml to update your file automatically. "
210              "This program is installed automatically with SimGrid, or "
211              "available in the tools/ directory of the source archive.");
212   if (version < 4.1) {
213     XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
214              "That's fine, the new version is backward compatible. \n\n"
215              "Use simgrid_update_xml to update your file automatically to get rid of this warning. "
216              "This program is installed automatically with SimGrid, or "
217              "available in the tools/ directory of the source archive.",
218              version, surf_parsed_filename.c_str());
219   }
220   surf_parse_assert(version <= 4.1,
221              std::string("******* THIS FILE COMES FROM THE FUTURE (v:")+std::to_string(version)+") *********\n "
222              "The most recent formalism that this version of SimGrid understands is v4.1.\n"
223              "Please update your code, or use another, more adapted, file.");
224 }
225 void ETag_surfxml_platform(){
226   simgrid::s4u::Engine::on_platform_created();
227 }
228
229 void STag_surfxml_host(){
230   property_sets.push_back(std::unordered_map<std::string, std::string>());
231 }
232
233 void STag_surfxml_prop()
234 {
235   property_sets.back().insert({A_surfxml_prop_id, A_surfxml_prop_value});
236   XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
237             &(property_sets.back()));
238 }
239
240 void ETag_surfxml_host()    {
241   simgrid::kernel::routing::HostCreationArgs host;
242
243   host.properties = property_sets.back();
244   property_sets.pop_back();
245
246   host.id = A_surfxml_host_id;
247
248   host.speed_per_pstate =
249       xbt_parse_get_all_speeds(surf_parsed_filename, surf_parse_lineno, A_surfxml_host_speed, "speed of host", host.id);
250
251   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
252   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
253
254   if (A_surfxml_host_availability___file[0] != '\0') {
255     XBT_WARN("The availability_file attribute in <host> is now deprecated. Please, use 'speed_file' instead.");
256     host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_availability___file);
257   }
258   if (A_surfxml_host_speed___file[0] != '\0')
259     host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_speed___file);
260   host.state_trace = A_surfxml_host_state___file[0]
261                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_host_state___file)
262                          : nullptr;
263   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
264   host.coord       = A_surfxml_host_coordinates;
265   host.disks.swap(parsed_disk_list);
266
267   sg_platf_new_host(&host);
268 }
269
270 void STag_surfxml_disk() {
271   property_sets.push_back(std::unordered_map<std::string, std::string>());
272 }
273
274 void ETag_surfxml_disk() {
275   simgrid::kernel::routing::DiskCreationArgs disk;
276   disk.properties = property_sets.back();
277   property_sets.pop_back();
278
279   disk.id       = A_surfxml_disk_id;
280   disk.read_bw  = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_disk_read___bw,
281                                          "read_bw of disk ", disk.id);
282   disk.write_bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_disk_write___bw,
283                                           "write_bw of disk ", disk.id);
284
285   parsed_disk_list.push_back(sg_platf_new_disk(&disk));
286 }
287
288 void STag_surfxml_host___link(){
289   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
290   simgrid::kernel::routing::HostLinkCreationArgs host_link;
291
292   host_link.id        = A_surfxml_host___link_id;
293   host_link.link_up   = A_surfxml_host___link_up;
294   host_link.link_down = A_surfxml_host___link_down;
295   sg_platf_new_hostlink(&host_link);
296 }
297
298 void STag_surfxml_router(){
299   sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
300 }
301
302 void ETag_surfxml_cluster(){
303   simgrid::kernel::routing::ClusterCreationArgs cluster;
304   cluster.properties = property_sets.back();
305   property_sets.pop_back();
306
307   cluster.id          = A_surfxml_cluster_id;
308   cluster.prefix      = A_surfxml_cluster_prefix;
309   cluster.suffix      = A_surfxml_cluster_suffix;
310   explodesRadical(A_surfxml_cluster_radical, &cluster.radicals);
311
312   cluster.speeds      = xbt_parse_get_all_speeds(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_speed,
313                                             "speed of cluster", cluster.id);
314   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
315   cluster.bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bw, "bw of cluster",
316                                        cluster.id);
317   cluster.lat =
318       xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_lat, "lat of cluster", cluster.id);
319   if(strcmp(A_surfxml_cluster_bb___bw,""))
320     cluster.bb_bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bb___bw,
321                                             "bb_bw of cluster", cluster.id);
322   if(strcmp(A_surfxml_cluster_bb___lat,""))
323     cluster.bb_lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bb___lat,
324                                         "bb_lat of cluster", cluster.id);
325   if(strcmp(A_surfxml_cluster_limiter___link,""))
326     cluster.limiter_link =
327         xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_limiter___link,
328                                 "limiter_link of cluster", cluster.id);
329   if(strcmp(A_surfxml_cluster_loopback___bw,""))
330     cluster.loopback_bw = xbt_parse_get_bandwidth(
331         surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
332   if(strcmp(A_surfxml_cluster_loopback___lat,""))
333     cluster.loopback_lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_loopback___lat,
334                                               "loopback_lat of cluster", cluster.id);
335
336   switch(AX_surfxml_cluster_topology){
337   case A_surfxml_cluster_topology_FLAT:
338     cluster.topology = simgrid::kernel::routing::ClusterTopology::FLAT;
339     break;
340   case A_surfxml_cluster_topology_TORUS:
341     cluster.topology = simgrid::kernel::routing::ClusterTopology::TORUS;
342     break;
343   case A_surfxml_cluster_topology_FAT___TREE:
344     cluster.topology = simgrid::kernel::routing::ClusterTopology::FAT_TREE;
345     break;
346   case A_surfxml_cluster_topology_DRAGONFLY:
347     cluster.topology = simgrid::kernel::routing::ClusterTopology::DRAGONFLY;
348     break;
349   default:
350     surf_parse_error(std::string("Invalid cluster topology for cluster ") + cluster.id);
351   }
352   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
353   cluster.router_id = A_surfxml_cluster_router___id;
354
355   switch (AX_surfxml_cluster_sharing___policy) {
356   case A_surfxml_cluster_sharing___policy_SHARED:
357     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
358     break;
359   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
360     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
361     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
362     break;
363   case A_surfxml_cluster_sharing___policy_SPLITDUPLEX:
364     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
365     break;
366   case A_surfxml_cluster_sharing___policy_FATPIPE:
367     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
368     break;
369   default:
370     surf_parse_error(std::string("Invalid cluster sharing policy for cluster ") + cluster.id);
371   }
372   switch (AX_surfxml_cluster_bb___sharing___policy) {
373   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
374     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
375     break;
376   case A_surfxml_cluster_bb___sharing___policy_SHARED:
377     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
378     break;
379   default:
380     surf_parse_error(std::string("Invalid bb sharing policy in cluster ") + cluster.id);
381   }
382
383   sg_platf_new_cluster(&cluster);
384 }
385
386 void STag_surfxml_cluster(){
387   property_sets.push_back(std::unordered_map<std::string, std::string>());
388 }
389
390 void STag_surfxml_cabinet(){
391   simgrid::kernel::routing::CabinetCreationArgs cabinet;
392   cabinet.id      = A_surfxml_cabinet_id;
393   cabinet.prefix  = A_surfxml_cabinet_prefix;
394   cabinet.suffix  = A_surfxml_cabinet_suffix;
395   cabinet.speed   = xbt_parse_get_speed(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_speed,
396                                       "speed of cabinet", cabinet.id.c_str());
397   cabinet.bw  = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_bw, "bw of cabinet",
398                                        cabinet.id.c_str());
399   cabinet.lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_lat, "lat of cabinet",
400                                    cabinet.id.c_str());
401   explodesRadical(A_surfxml_cabinet_radical, &cabinet.radicals);
402
403   sg_platf_new_cabinet(&cabinet);
404 }
405
406 void STag_surfxml_peer(){
407   simgrid::kernel::routing::PeerCreationArgs peer;
408
409   peer.id          = std::string(A_surfxml_peer_id);
410   peer.speed       = xbt_parse_get_speed(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_speed, "speed of peer",
411                                    peer.id.c_str());
412   peer.bw_in = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_bw___in, "bw_in of peer",
413                                        peer.id.c_str());
414   peer.bw_out      = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_bw___out,
415                                         "bw_out of peer", peer.id.c_str());
416   peer.coord       = A_surfxml_peer_coordinates;
417   peer.speed_trace = nullptr;
418   if (A_surfxml_peer_availability___file[0] != '\0') {
419     XBT_WARN("The availability_file attribute in <peer> is now deprecated. Please, use 'speed_file' instead.");
420     peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_availability___file);
421   }
422   if (A_surfxml_peer_speed___file[0] != '\0')
423     peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_speed___file);
424   peer.state_trace = A_surfxml_peer_state___file[0]
425                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_state___file)
426                          : nullptr;
427
428   if (A_surfxml_peer_lat[0] != '\0')
429     XBT_WARN("The latency attribute in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
430              A_surfxml_peer_lat);
431
432   sg_platf_new_peer(&peer);
433 }
434
435 void STag_surfxml_link(){
436   property_sets.push_back(std::unordered_map<std::string, std::string>());
437 }
438
439 void ETag_surfxml_link(){
440   simgrid::kernel::routing::LinkCreationArgs link;
441
442   link.properties = property_sets.back();
443   property_sets.pop_back();
444
445   link.id                  = std::string(A_surfxml_link_id);
446   link.bandwidths          = xbt_parse_get_bandwidths(surf_parsed_filename, surf_parse_lineno, A_surfxml_link_bandwidth,
447                                              "bandwidth of link", link.id.c_str());
448   link.bandwidth_trace     = A_surfxml_link_bandwidth___file[0]
449                              ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_bandwidth___file)
450                              : nullptr;
451   link.latency = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_link_latency, "latency of link",
452                                     link.id.c_str());
453   link.latency_trace       = A_surfxml_link_latency___file[0]
454                            ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_latency___file)
455                            : nullptr;
456   link.state_trace = A_surfxml_link_state___file[0]
457                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_state___file)
458                          : nullptr;
459
460   switch (A_surfxml_link_sharing___policy) {
461   case A_surfxml_link_sharing___policy_SHARED:
462     link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
463     break;
464   case A_surfxml_link_sharing___policy_FATPIPE:
465     link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
466     break;
467   case A_surfxml_link_sharing___policy_FULLDUPLEX:
468     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
469     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
470     break;
471   case A_surfxml_link_sharing___policy_SPLITDUPLEX:
472     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
473     break;
474   case A_surfxml_link_sharing___policy_WIFI:
475     link.policy = simgrid::s4u::Link::SharingPolicy::WIFI;
476     break;
477   default:
478     surf_parse_error(std::string("Invalid sharing policy in link ") + link.id);
479   }
480
481   sg_platf_new_link(&link);
482 }
483
484 void STag_surfxml_link___ctn()
485 {
486   simgrid::kernel::resource::LinkImpl* link = nullptr;
487   switch (A_surfxml_link___ctn_direction) {
488   case AU_surfxml_link___ctn_direction:
489   case A_surfxml_link___ctn_direction_NONE:
490     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id))->get_impl();
491     break;
492   case A_surfxml_link___ctn_direction_UP:
493     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_UP")->get_impl();
494     break;
495   case A_surfxml_link___ctn_direction_DOWN:
496     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_DOWN")->get_impl();
497     break;
498   default:
499     surf_parse_error(std::string("Invalid direction for link ") + A_surfxml_link___ctn_id);
500   }
501
502   const char* dirname;
503   switch (A_surfxml_link___ctn_direction) {
504     case A_surfxml_link___ctn_direction_UP:
505       dirname = " (upward)";
506       break;
507     case A_surfxml_link___ctn_direction_DOWN:
508       dirname = " (downward)";
509       break;
510     default:
511       dirname = "";
512   }
513   surf_parse_assert(link != nullptr, std::string("No such link: '") + A_surfxml_link___ctn_id + "'" + dirname);
514   parsed_link_list.push_back(link);
515 }
516
517 void ETag_surfxml_backbone(){
518   simgrid::kernel::routing::LinkCreationArgs link;
519
520   link.id = std::string(A_surfxml_backbone_id);
521   link.bandwidths.push_back(xbt_parse_get_bandwidth(
522       surf_parsed_filename, surf_parse_lineno, A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str()));
523   link.latency    = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_backbone_latency,
524                                     "latency of backbone", link.id.c_str());
525   link.policy     = simgrid::s4u::Link::SharingPolicy::SHARED;
526
527   sg_platf_new_link(&link);
528   routing_cluster_add_backbone(simgrid::s4u::Link::by_name(std::string(A_surfxml_backbone_id))->get_impl());
529 }
530
531 void STag_surfxml_route(){
532   surf_parse_assert_netpoint(A_surfxml_route_src, "Route src='", "' does name a node.");
533   surf_parse_assert_netpoint(A_surfxml_route_dst, "Route dst='", "' does name a node.");
534 }
535
536 void STag_surfxml_ASroute(){
537   surf_parse_assert_netpoint(A_surfxml_ASroute_src, "ASroute src='", "' does name a node.");
538   surf_parse_assert_netpoint(A_surfxml_ASroute_dst, "ASroute dst='", "' does name a node.");
539
540   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___src, "ASroute gw_src='", "' does name a node.");
541   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___dst, "ASroute gw_dst='", "' does name a node.");
542 }
543 void STag_surfxml_zoneRoute(){
544   surf_parse_assert_netpoint(A_surfxml_zoneRoute_src, "zoneRoute src='", "' does name a node.");
545   surf_parse_assert_netpoint(A_surfxml_zoneRoute_dst, "zoneRoute dst='", "' does name a node.");
546   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___src, "zoneRoute gw_src='", "' does name a node.");
547   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___dst, "zoneRoute gw_dst='", "' does name a node.");
548 }
549
550 void STag_surfxml_bypassRoute(){
551   surf_parse_assert_netpoint(A_surfxml_bypassRoute_src, "bypassRoute src='", "' does name a node.");
552   surf_parse_assert_netpoint(A_surfxml_bypassRoute_dst, "bypassRoute dst='", "' does name a node.");
553 }
554
555 void STag_surfxml_bypassASroute(){
556   surf_parse_assert_netpoint(A_surfxml_bypassASroute_src, "bypassASroute src='", "' does name a node.");
557   surf_parse_assert_netpoint(A_surfxml_bypassASroute_dst, "bypassASroute dst='", "' does name a node.");
558   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___src, "bypassASroute gw_src='", "' does name a node.");
559   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___dst, "bypassASroute gw_dst='", "' does name a node.");
560 }
561 void STag_surfxml_bypassZoneRoute(){
562   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_src, "bypassZoneRoute src='", "' does name a node.");
563   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_dst, "bypassZoneRoute dst='", "' does name a node.");
564   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___src, "bypassZoneRoute gw_src='", "' does name a node.");
565   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___dst, "bypassZoneRoute gw_dst='", "' does name a node.");
566 }
567
568 void ETag_surfxml_route(){
569   simgrid::kernel::routing::RouteCreationArgs route;
570
571   route.src         = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
572   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
573   route.symmetrical = (A_surfxml_route_symmetrical == AU_surfxml_route_symmetrical ||
574                        A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES ||
575                        A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_yes);
576
577   route.link_list.swap(parsed_link_list);
578
579   sg_platf_new_route(&route);
580 }
581
582 void ETag_surfxml_ASroute()
583 {
584   AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
585   AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
586   AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
587   AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
588   AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
589   ETag_surfxml_zoneRoute();
590 }
591 void ETag_surfxml_zoneRoute()
592 {
593   simgrid::kernel::routing::RouteCreationArgs ASroute;
594
595   ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
596   ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
597
598   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
599   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
600
601   ASroute.link_list.swap(parsed_link_list);
602
603   ASroute.symmetrical = (A_surfxml_zoneRoute_symmetrical == AU_surfxml_zoneRoute_symmetrical ||
604                          A_surfxml_zoneRoute_symmetrical == A_surfxml_zoneRoute_symmetrical_YES ||
605                          A_surfxml_zoneRoute_symmetrical == A_surfxml_zoneRoute_symmetrical_yes);
606
607   sg_platf_new_route(&ASroute);
608 }
609
610 void ETag_surfxml_bypassRoute(){
611   simgrid::kernel::routing::RouteCreationArgs route;
612
613   route.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
614   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
615   route.symmetrical = false;
616
617   route.link_list.swap(parsed_link_list);
618
619   sg_platf_new_bypassRoute(&route);
620 }
621
622 void ETag_surfxml_bypassASroute()
623 {
624   AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
625   AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
626   AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
627   AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
628   ETag_surfxml_bypassZoneRoute();
629 }
630 void ETag_surfxml_bypassZoneRoute()
631 {
632   simgrid::kernel::routing::RouteCreationArgs ASroute;
633
634   ASroute.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
635   ASroute.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
636   ASroute.link_list.swap(parsed_link_list);
637
638   ASroute.symmetrical = false;
639
640   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
641   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
642
643   sg_platf_new_bypassRoute(&ASroute);
644 }
645
646 void ETag_surfxml_trace(){
647   simgrid::kernel::routing::ProfileCreationArgs trace;
648
649   trace.id = A_surfxml_trace_id;
650   trace.file = A_surfxml_trace_file;
651   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
652   trace.pc_data = surfxml_pcdata;
653
654   sg_platf_new_trace(&trace);
655 }
656
657 void STag_surfxml_trace___connect()
658 {
659   simgrid::kernel::routing::TraceConnectCreationArgs trace_connect;
660
661   trace_connect.element = A_surfxml_trace___connect_element;
662   trace_connect.trace = A_surfxml_trace___connect_trace;
663
664   switch (A_surfxml_trace___connect_kind) {
665   case AU_surfxml_trace___connect_kind:
666   case A_surfxml_trace___connect_kind_SPEED:
667     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::SPEED;
668     break;
669   case A_surfxml_trace___connect_kind_BANDWIDTH:
670     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::BANDWIDTH;
671     break;
672   case A_surfxml_trace___connect_kind_HOST___AVAIL:
673     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL;
674     break;
675   case A_surfxml_trace___connect_kind_LATENCY:
676     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LATENCY;
677     break;
678   case A_surfxml_trace___connect_kind_LINK___AVAIL:
679     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL;
680     break;
681   default:
682     surf_parse_error("Invalid trace kind");
683   }
684   sg_platf_trace_connect(&trace_connect);
685 }
686
687 void STag_surfxml_AS()
688 {
689   AX_surfxml_zone_id = AX_surfxml_AS_id;
690   AX_surfxml_zone_routing = AX_surfxml_AS_routing;
691   STag_surfxml_zone();
692 }
693
694 void ETag_surfxml_AS()
695 {
696   ETag_surfxml_zone();
697 }
698
699 void STag_surfxml_zone()
700 {
701   property_sets.push_back(std::unordered_map<std::string, std::string>());
702   simgrid::kernel::routing::ZoneCreationArgs zone;
703   zone.id      = A_surfxml_zone_id;
704   zone.routing = A_surfxml_zone_routing;
705   sg_platf_new_Zone_begin(&zone);
706 }
707
708 void ETag_surfxml_zone()
709 {
710   sg_platf_new_Zone_set_properties(property_sets.back());
711   property_sets.pop_back();
712
713   sg_platf_new_Zone_seal();
714 }
715
716 void STag_surfxml_config()
717 {
718   property_sets.push_back(std::unordered_map<std::string, std::string>());
719   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
720   if (_sg_cfg_init_status == 2) {
721     surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
722                      "<link>, etc).");
723   }
724 }
725
726 void ETag_surfxml_config()
727 {
728   // Sort config elements before applying.
729   // That's a little waste of time, but not doing so would break the tests
730   auto current_property_set = property_sets.back();
731
732   std::vector<std::string> keys;
733   for (auto const& kv : current_property_set) {
734     keys.push_back(kv.first);
735   }
736   std::sort(keys.begin(), keys.end());
737   for (std::string key : keys) {
738     if (simgrid::config::is_default(key.c_str())) {
739       std::string cfg = key + ":" + current_property_set.at(key);
740       simgrid::config::set_parse(cfg);
741     } else
742       XBT_INFO("The custom configuration '%s' is already defined by user!", key.c_str());
743   }
744   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
745
746   property_sets.pop_back();
747 }
748
749 static std::vector<std::string> arguments;
750
751 void STag_surfxml_process()
752 {
753   AX_surfxml_actor_function = AX_surfxml_process_function;
754   STag_surfxml_actor();
755 }
756
757 void STag_surfxml_actor()
758 {
759   property_sets.push_back(std::unordered_map<std::string, std::string>());
760   arguments.assign(1, A_surfxml_actor_function);
761 }
762
763 void ETag_surfxml_process()
764 {
765   AX_surfxml_actor_host = AX_surfxml_process_host;
766   AX_surfxml_actor_function = AX_surfxml_process_function;
767   AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
768   AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
769   AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
770   ETag_surfxml_actor();
771 }
772
773 void ETag_surfxml_actor()
774 {
775   simgrid::kernel::routing::ActorCreationArgs actor;
776
777   actor.properties = property_sets.back();
778   property_sets.pop_back();
779
780   actor.args.swap(arguments);
781   actor.host       = A_surfxml_actor_host;
782   actor.function   = A_surfxml_actor_function;
783   actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
784   actor.kill_time  = surf_parse_get_double(A_surfxml_actor_kill___time);
785
786   switch (A_surfxml_actor_on___failure) {
787   case AU_surfxml_actor_on___failure:
788   case A_surfxml_actor_on___failure_DIE:
789     actor.restart_on_failure = false;
790     break;
791   case A_surfxml_actor_on___failure_RESTART:
792     actor.restart_on_failure = true;
793     break;
794   default:
795     surf_parse_error("Invalid on failure behavior");
796   }
797
798   sg_platf_new_actor(&actor);
799 }
800
801 void STag_surfxml_argument(){
802   arguments.emplace_back(A_surfxml_argument_value);
803 }
804
805 void STag_surfxml_model___prop(){
806   current_model_property_set.insert({A_surfxml_model___prop_id, A_surfxml_model___prop_value});
807 }
808
809 void ETag_surfxml_prop(){/* Nothing to do */}
810 void STag_surfxml_random(){/* Nothing to do */}
811 void ETag_surfxml_random(){/* Nothing to do */}
812 void ETag_surfxml_trace___connect(){/* Nothing to do */}
813 void STag_surfxml_trace()
814 { /* Nothing to do */
815 }
816 void ETag_surfxml_router(){/*Nothing to do*/}
817 void ETag_surfxml_host___link(){/* Nothing to do */}
818 void ETag_surfxml_cabinet(){/* Nothing to do */}
819 void ETag_surfxml_peer(){/* Nothing to do */}
820 void STag_surfxml_backbone(){/* Nothing to do */}
821 void ETag_surfxml_link___ctn(){/* Nothing to do */}
822 void ETag_surfxml_argument(){/* Nothing to do */}
823 void ETag_surfxml_model___prop(){/* Nothing to do */}
824
825 /* Open and Close parse file */
826 YY_BUFFER_STATE surf_input_buffer;
827
828 void surf_parse_open(const std::string& file)
829 {
830   surf_parsed_filename = file;
831   std::string dir      = simgrid::xbt::Path(file).get_dir_name();
832   surf_path.push_back(dir);
833
834   surf_file_to_parse = surf_fopen(file, "r");
835   if (surf_file_to_parse == nullptr)
836     throw std::invalid_argument(std::string("Unable to open '") + file + "' from '" + simgrid::xbt::Path().get_name() +
837                                 "'. Does this file exist?");
838   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
839   surf_parse__switch_to_buffer(surf_input_buffer);
840   surf_parse_lineno = 1;
841 }
842
843 void surf_parse_close()
844 {
845   surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
846
847   if (surf_file_to_parse) {
848     surf_parse__delete_buffer(surf_input_buffer);
849     fclose(surf_file_to_parse);
850     surf_file_to_parse = nullptr; //Must be reset for Bypass
851   }
852 }
853
854 /* Call the lexer to parse the currently opened file */
855 void surf_parse()
856 {
857   bool err = surf_parse_lex();
858   surf_parse_assert(not err, "Flex returned an error code");
859 }