1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved. */
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. */
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"
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>
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
22 int ETag_surfxml_include_state();
24 #include "simgrid_dtd.c"
26 char* surf_parsed_filename = nullptr; // to locate parse error messages
28 std::vector<simgrid::surf::LinkImpl*> parsed_link_list; /* temporary store of current list link of a route */
33 void surf_parse_assert(bool cond, const char *fmt, ...) {
37 int lineno = surf_parse_lineno;
38 char *msg = bvprintf(fmt,va);
41 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
43 xbt_die("Exiting now");
46 void surf_parse_error(const char *fmt, ...) {
49 int lineno = surf_parse_lineno;
50 char *msg = bvprintf(fmt,va);
53 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
55 xbt_die("Exiting now");
57 void surf_parse_warn(const char *fmt, ...) {
60 char *msg = bvprintf(fmt,va);
62 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
66 double surf_parse_get_double(const char *string) {
68 int ret = sscanf(string, "%lg", &res);
70 surf_parse_error("%s is not a double", string);
74 int surf_parse_get_int(const char *string) {
76 int ret = sscanf(string, "%d", &res);
78 surf_parse_error("%s is not an integer", string);
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)
85 std::vector<int>* exploded = new std::vector<int>();
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());
96 switch (radical_ends.size()) {
101 end = surf_parse_get_int((radical_ends.back()).c_str());
104 surf_parse_error("Malformed radical: %s", group.c_str());
107 for (int i = start; i <= end; i++)
108 exploded->push_back(i);
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)
126 double res = strtod(string, &ptr);
128 surf_parse_error("value out of range: %s", string);
130 surf_parse_error("cannot parse number: %s", string);
131 if (ptr[0] == '\0') {
133 return res; // Ok, 0 can be unit-less
135 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
136 ptr = (char*)default_unit;
138 for (i = 0; units[i].unit != nullptr && strcmp(ptr, units[i].unit) != 0; i++);
140 if (units[i].unit != nullptr)
141 res *= units[i].scale;
143 surf_parse_error("unknown unit: %s", ptr);
147 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
149 const struct unit_scale units[] = {
150 { "w", 7 * 24 * 60 * 60 },
151 { "d", 24 * 60 * 60 },
161 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
162 "Append 's' to your time to get seconds", "s");
165 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
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) },
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 },
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");
200 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
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) },
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 },
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");
233 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
235 const struct unit_scale units[] = {
236 { "yottaflops", 1e24 },
238 { "zettaflops", 1e21 },
240 { "exaflops", 1e18 },
242 { "petaflops", 1e15 },
244 { "teraflops", 1e12 },
246 { "gigaflops", 1e9 },
248 { "megaflops", 1e6 },
250 { "kiloflops", 1e3 },
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");
260 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const char* id){
262 std::vector<double> speed_per_pstate;
264 if (strchr(speeds, ',') == nullptr){
265 double speed = surf_parse_get_speed(speeds, entity_kind, id);
266 speed_per_pstate.push_back(speed);
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);
277 return speed_per_pstate;
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)
285 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
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 ZONE_TAG = 0; // Whether we just opened a zone tag (to see what to do with the properties)
292 /* dictionary of random generator data */
293 xbt_dict_t random_data_list = nullptr;
295 YY_BUFFER_STATE surf_input_buffer;
296 FILE *surf_file_to_parse = nullptr;
299 * Stuff relative to storage
301 void STag_surfxml_storage()
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)");
307 void ETag_surfxml_storage()
309 s_sg_platf_storage_cbarg_t storage;
310 memset(&storage,0,sizeof(storage));
312 storage.properties = current_property_set;
313 current_property_set = nullptr;
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);
322 void STag_surfxml_storage___type()
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)");
329 void ETag_surfxml_storage___type()
331 s_sg_platf_storage_type_cbarg_t storage_type;
332 memset(&storage_type,0,sizeof(storage_type));
334 storage_type.properties = current_property_set;
335 current_property_set = nullptr;
337 storage_type.model_properties = current_model_property_set;
338 current_model_property_set = nullptr;
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);
348 void STag_surfxml_mount()
350 XBT_DEBUG("STag_surfxml_mount");
352 void ETag_surfxml_mount()
354 s_sg_platf_mount_cbarg_t mount;
355 memset(&mount,0,sizeof(mount));
357 mount.name = A_surfxml_mount_name;
358 mount.storageId = A_surfxml_mount_storageId;
359 sg_platf_new_mount(&mount);
363 * Stuff relative to the <include> tag
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;
369 void STag_surfxml_include()
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);
376 surf_file_to_parse_stack.push_back(surf_file_to_parse); // save old file descriptor
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);
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);
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 */
393 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
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.
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.
404 int ETag_surfxml_include_state()
407 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
409 if (surf_input_buffer_stack.empty()) // nope, that's a true premature EOF. Let the parser die verbosely.
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();
418 // Restore the filename for error messages
419 free(surf_parsed_filename);
420 surf_parsed_filename_stack.pop_back();
425 /* Stag and Etag parse functions */
427 void STag_surfxml_platform() {
428 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
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"
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"
439 "Please check also out the SURF section of the ChangeLog for "
440 "the 3.1 version for more information. \n"
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),
450 "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
451 "Changes introduced in SimGrid 3.13:\n"
452 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
453 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
454 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
455 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
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 surf_parsed_filename, version);
462 XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
463 "That's fine, the new version is backward compatible. \n\n"
464 "Use simgrid_update_xml to update your file automatically. "
465 "This program is installed automatically with SimGrid, or "
466 "available in the tools/ directory of the source archive.",
467 version, surf_parsed_filename);
469 xbt_assert(version <= 4.1, "******* FILE %s COMES FROM THE FUTURE (v:%.1f) *********\n "
470 "The most recent formalism that this version of SimGrid understands is v4.1.\n"
471 "Please update your code, or use another, more adapted, file.",
472 surf_parsed_filename, version);
476 void ETag_surfxml_platform(){
480 void STag_surfxml_host(){
482 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
485 void STag_surfxml_prop()
487 if (ZONE_TAG) { // We need to retrieve the most recently opened zone
488 XBT_DEBUG("Set zone property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
489 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_zone_id);
491 netzone->setProperty(A_surfxml_prop_id, A_surfxml_prop_value);
494 if (!current_property_set)
495 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
496 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
497 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
498 current_property_set);
502 void ETag_surfxml_host() {
503 s_sg_platf_host_cbarg_t host;
504 memset(&host,0,sizeof(host));
506 host.properties = current_property_set;
507 current_property_set = nullptr;
509 host.id = A_surfxml_host_id;
511 host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
513 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
514 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
515 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
516 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
517 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
518 host.coord = A_surfxml_host_coordinates;
520 sg_platf_new_host(&host);
523 void STag_surfxml_host___link(){
524 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
525 s_sg_platf_host_link_cbarg_t host_link;
526 memset(&host_link,0,sizeof(host_link));
528 host_link.id = A_surfxml_host___link_id;
529 host_link.link_up = A_surfxml_host___link_up;
530 host_link.link_down = A_surfxml_host___link_down;
531 sg_platf_new_hostlink(&host_link);
534 void STag_surfxml_router(){
535 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
538 void ETag_surfxml_cluster(){
539 s_sg_platf_cluster_cbarg_t cluster;
540 memset(&cluster,0,sizeof(cluster));
541 cluster.properties = current_property_set;
542 current_property_set = nullptr;
544 cluster.id = A_surfxml_cluster_id;
545 cluster.prefix = A_surfxml_cluster_prefix;
546 cluster.suffix = A_surfxml_cluster_suffix;
547 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
548 cluster.speeds = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
549 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
550 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
551 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
552 if(strcmp(A_surfxml_cluster_bb___bw,""))
553 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
554 if(strcmp(A_surfxml_cluster_bb___lat,""))
555 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
556 if(strcmp(A_surfxml_cluster_limiter___link,""))
557 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
558 if(strcmp(A_surfxml_cluster_loopback___bw,""))
559 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
560 if(strcmp(A_surfxml_cluster_loopback___lat,""))
561 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
563 switch(AX_surfxml_cluster_topology){
564 case A_surfxml_cluster_topology_FLAT:
565 cluster.topology= SURF_CLUSTER_FLAT ;
567 case A_surfxml_cluster_topology_TORUS:
568 cluster.topology= SURF_CLUSTER_TORUS ;
570 case A_surfxml_cluster_topology_FAT___TREE:
571 cluster.topology = SURF_CLUSTER_FAT_TREE;
573 case A_surfxml_cluster_topology_DRAGONFLY:
574 cluster.topology= SURF_CLUSTER_DRAGONFLY ;
577 surf_parse_error("Invalid cluster topology for cluster %s",
581 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
582 cluster.router_id = A_surfxml_cluster_router___id;
584 switch (AX_surfxml_cluster_sharing___policy) {
585 case A_surfxml_cluster_sharing___policy_SHARED:
586 cluster.sharing_policy = SURF_LINK_SHARED;
588 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
589 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
591 case A_surfxml_cluster_sharing___policy_FATPIPE:
592 cluster.sharing_policy = SURF_LINK_FATPIPE;
595 surf_parse_error("Invalid cluster sharing policy for cluster %s", cluster.id);
598 switch (AX_surfxml_cluster_bb___sharing___policy) {
599 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
600 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
602 case A_surfxml_cluster_bb___sharing___policy_SHARED:
603 cluster.bb_sharing_policy = SURF_LINK_SHARED;
606 surf_parse_error("Invalid bb sharing policy in cluster %s", cluster.id);
610 sg_platf_new_cluster(&cluster);
613 void STag_surfxml_cluster(){
615 parse_after_config();
616 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
619 void STag_surfxml_cabinet(){
620 parse_after_config();
621 s_sg_platf_cabinet_cbarg_t cabinet;
622 memset(&cabinet,0,sizeof(cabinet));
623 cabinet.id = A_surfxml_cabinet_id;
624 cabinet.prefix = A_surfxml_cabinet_prefix;
625 cabinet.suffix = A_surfxml_cabinet_suffix;
626 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
627 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
628 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
629 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
631 sg_platf_new_cabinet(&cabinet);
634 void STag_surfxml_peer(){
635 parse_after_config();
636 s_sg_platf_peer_cbarg_t peer;
637 memset(&peer,0,sizeof(peer));
638 peer.id = A_surfxml_peer_id;
639 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
640 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
641 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
642 peer.coord = A_surfxml_peer_coordinates;
643 peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
644 peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
646 if (A_surfxml_peer_lat[0] != '\0')
647 XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
650 sg_platf_new_peer(&peer);
653 void STag_surfxml_link(){
655 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
658 void ETag_surfxml_link(){
659 LinkCreationArgs link;
661 link.properties = current_property_set;
662 current_property_set = nullptr;
664 link.id = std::string(A_surfxml_link_id);
665 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
666 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
667 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
668 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
669 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
671 switch (A_surfxml_link_sharing___policy) {
672 case A_surfxml_link_sharing___policy_SHARED:
673 link.policy = SURF_LINK_SHARED;
675 case A_surfxml_link_sharing___policy_FATPIPE:
676 link.policy = SURF_LINK_FATPIPE;
678 case A_surfxml_link_sharing___policy_FULLDUPLEX:
679 link.policy = SURF_LINK_FULLDUPLEX;
682 surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
686 sg_platf_new_link(&link);
689 void STag_surfxml_link___ctn(){
691 simgrid::surf::LinkImpl* link = nullptr;
692 char *link_name=nullptr;
693 switch (A_surfxml_link___ctn_direction) {
694 case AU_surfxml_link___ctn_direction:
695 case A_surfxml_link___ctn_direction_NONE:
696 link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
698 case A_surfxml_link___ctn_direction_UP:
699 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
700 link = simgrid::surf::LinkImpl::byName(link_name);
702 case A_surfxml_link___ctn_direction_DOWN:
703 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
704 link = simgrid::surf::LinkImpl::byName(link_name);
707 xbt_free(link_name); // no-op if it's already nullptr
709 const char* dirname = "";
710 switch (A_surfxml_link___ctn_direction) {
711 case A_surfxml_link___ctn_direction_UP:
712 dirname = " (upward)";
714 case A_surfxml_link___ctn_direction_DOWN:
715 dirname = " (downward)";
720 surf_parse_assert(link != nullptr, "No such link: '%s'%s", A_surfxml_link___ctn_id, dirname);
721 parsed_link_list.push_back(link);
724 void ETag_surfxml_backbone(){
725 LinkCreationArgs link;
727 link.properties = nullptr;
728 link.id = std::string(A_surfxml_backbone_id);
729 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
730 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
731 link.policy = SURF_LINK_SHARED;
733 sg_platf_new_link(&link);
734 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
737 void STag_surfxml_route(){
738 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
739 A_surfxml_route_src);
740 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
741 A_surfxml_route_dst);
744 void STag_surfxml_ASroute(){
745 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
746 A_surfxml_ASroute_src);
747 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
748 A_surfxml_ASroute_dst);
750 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
751 A_surfxml_ASroute_gw___src);
752 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
753 A_surfxml_ASroute_gw___dst);
755 void STag_surfxml_zoneRoute(){
756 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src), "zoneRoute src='%s' does name a node.",
757 A_surfxml_zoneRoute_src);
758 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst), "zoneRoute dst='%s' does name a node.",
759 A_surfxml_zoneRoute_dst);
761 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src), "zoneRoute gw_src='%s' does name a node.",
762 A_surfxml_zoneRoute_gw___src);
763 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst), "zoneRoute gw_dst='%s' does name a node.",
764 A_surfxml_zoneRoute_gw___dst);
767 void STag_surfxml_bypassRoute(){
768 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
769 A_surfxml_bypassRoute_src);
770 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
771 A_surfxml_bypassRoute_dst);
774 void STag_surfxml_bypassASroute(){
775 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
776 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
777 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
778 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
779 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
780 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
781 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
782 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
784 void STag_surfxml_bypassZoneRoute(){
785 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src),
786 "bypassASroute src='%s' does name a node.", A_surfxml_bypassZoneRoute_src);
787 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst),
788 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassZoneRoute_dst);
789 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src),
790 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassZoneRoute_gw___src);
791 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst),
792 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassZoneRoute_gw___dst);
795 void ETag_surfxml_route(){
796 s_sg_platf_route_cbarg_t route;
797 memset(&route,0,sizeof(route));
799 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
800 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
801 route.gw_src = nullptr;
802 route.gw_dst = nullptr;
803 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
804 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
806 for (auto link: parsed_link_list)
807 route.link_list->push_back(link);
808 parsed_link_list.clear();
810 sg_platf_new_route(&route);
811 delete route.link_list;
814 void ETag_surfxml_ASroute()
816 AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
817 AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
818 AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
819 AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
820 AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
821 ETag_surfxml_zoneRoute();
823 void ETag_surfxml_zoneRoute()
825 s_sg_platf_route_cbarg_t ASroute;
826 memset(&ASroute,0,sizeof(ASroute));
828 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
829 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
831 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
832 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
834 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
836 for (auto link: parsed_link_list)
837 ASroute.link_list->push_back(link);
838 parsed_link_list.clear();
840 switch (A_surfxml_zoneRoute_symmetrical) {
841 case AU_surfxml_zoneRoute_symmetrical:
842 case A_surfxml_zoneRoute_symmetrical_YES:
843 ASroute.symmetrical = true;
845 case A_surfxml_zoneRoute_symmetrical_NO:
846 ASroute.symmetrical = false;
850 sg_platf_new_route(&ASroute);
851 delete ASroute.link_list;
854 void ETag_surfxml_bypassRoute(){
855 s_sg_platf_route_cbarg_t route;
856 memset(&route,0,sizeof(route));
858 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
859 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
860 route.gw_src = nullptr;
861 route.gw_dst = nullptr;
862 route.symmetrical = false;
863 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
865 for (auto link: parsed_link_list)
866 route.link_list->push_back(link);
867 parsed_link_list.clear();
869 sg_platf_new_bypassRoute(&route);
870 delete route.link_list;
873 void ETag_surfxml_bypassASroute()
875 AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
876 AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
877 AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
878 AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
879 ETag_surfxml_bypassZoneRoute();
881 void ETag_surfxml_bypassZoneRoute()
883 s_sg_platf_route_cbarg_t ASroute;
884 memset(&ASroute,0,sizeof(ASroute));
886 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
887 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
888 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
889 for (auto link: parsed_link_list)
890 ASroute.link_list->push_back(link);
891 parsed_link_list.clear();
893 ASroute.symmetrical = false;
895 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
896 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
898 sg_platf_new_bypassRoute(&ASroute);
899 delete ASroute.link_list;
902 void ETag_surfxml_trace(){
903 s_sg_platf_trace_cbarg_t trace;
904 memset(&trace,0,sizeof(trace));
906 trace.id = A_surfxml_trace_id;
907 trace.file = A_surfxml_trace_file;
908 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
909 trace.pc_data = surfxml_pcdata;
911 sg_platf_new_trace(&trace);
914 void STag_surfxml_trace___connect(){
915 parse_after_config();
916 s_sg_platf_trace_connect_cbarg_t trace_connect;
917 memset(&trace_connect,0,sizeof(trace_connect));
919 trace_connect.element = A_surfxml_trace___connect_element;
920 trace_connect.trace = A_surfxml_trace___connect_trace;
922 switch (A_surfxml_trace___connect_kind) {
923 case AU_surfxml_trace___connect_kind:
924 case A_surfxml_trace___connect_kind_SPEED:
925 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
927 case A_surfxml_trace___connect_kind_BANDWIDTH:
928 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
930 case A_surfxml_trace___connect_kind_HOST___AVAIL:
931 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
933 case A_surfxml_trace___connect_kind_LATENCY:
934 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
936 case A_surfxml_trace___connect_kind_LINK___AVAIL:
937 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
940 sg_platf_trace_connect(&trace_connect);
943 void STag_surfxml_AS(){
944 AX_surfxml_zone_id = AX_surfxml_AS_id;
945 AX_surfxml_zone_routing = (AT_surfxml_zone_routing)AX_surfxml_AS_routing;
948 void ETag_surfxml_AS(){
951 void STag_surfxml_zone(){
952 parse_after_config();
954 s_sg_platf_AS_cbarg_t AS = { A_surfxml_zone_id, (int)A_surfxml_zone_routing};
956 sg_platf_new_AS_begin(&AS);
958 void ETag_surfxml_zone(){
959 sg_platf_new_AS_seal();
962 void STag_surfxml_config(){
964 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
965 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
966 if (_sg_cfg_init_status == 2) {
967 surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
971 void ETag_surfxml_config(){
972 xbt_dict_cursor_t cursor = nullptr;
975 xbt_dict_foreach(current_property_set, cursor, key, elem) {
976 if (xbt_cfg_is_default_value(key)) {
977 char* cfg = bprintf("%s:%s", key, elem);
978 xbt_cfg_set_parse(cfg);
981 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
983 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
985 xbt_dict_free(¤t_property_set);
986 current_property_set = nullptr;
992 void STag_surfxml_process()
994 AX_surfxml_actor_function = AX_surfxml_process_function;
995 STag_surfxml_actor();
997 void STag_surfxml_actor()
1001 argv = xbt_new(char *, 1);
1002 argv[0] = xbt_strdup(A_surfxml_actor_function);
1003 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
1006 void ETag_surfxml_process()
1008 AX_surfxml_actor_host = AX_surfxml_process_host;
1009 AX_surfxml_actor_function = AX_surfxml_process_function;
1010 AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
1011 AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
1012 AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
1013 ETag_surfxml_actor();
1015 void ETag_surfxml_actor()
1017 s_sg_platf_process_cbarg_t actor;
1018 memset(&actor,0,sizeof(actor));
1021 actor.argv = (const char **)argv;
1022 actor.properties = current_property_set;
1023 actor.host = A_surfxml_actor_host;
1024 actor.function = A_surfxml_actor_function;
1025 actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
1026 actor.kill_time = surf_parse_get_double(A_surfxml_actor_kill___time);
1028 switch (A_surfxml_actor_on___failure) {
1029 case AU_surfxml_actor_on___failure:
1030 case A_surfxml_actor_on___failure_DIE:
1031 actor.on_failure = SURF_ACTOR_ON_FAILURE_DIE;
1033 case A_surfxml_actor_on___failure_RESTART:
1034 actor.on_failure = SURF_ACTOR_ON_FAILURE_RESTART;
1038 sg_platf_new_process(&actor);
1040 for (int i = 0; i != argc; ++i)
1045 current_property_set = nullptr;
1048 void STag_surfxml_argument(){
1050 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
1051 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
1054 void STag_surfxml_model___prop(){
1055 if (!current_model_property_set)
1056 current_model_property_set = new std::map<std::string, std::string>();
1058 current_model_property_set->insert(
1059 {std::string(A_surfxml_model___prop_id), std::string(A_surfxml_model___prop_value)});
1062 void ETag_surfxml_prop(){/* Nothing to do */}
1063 void STag_surfxml_random(){/* Nothing to do */}
1064 void ETag_surfxml_random(){/* Nothing to do */}
1065 void ETag_surfxml_trace___connect(){/* Nothing to do */}
1066 void STag_surfxml_trace(){parse_after_config();}
1067 void ETag_surfxml_router(){/*Nothing to do*/}
1068 void ETag_surfxml_host___link(){/* Nothing to do */}
1069 void ETag_surfxml_cabinet(){/* Nothing to do */}
1070 void ETag_surfxml_peer(){/* Nothing to do */}
1071 void STag_surfxml_backbone(){/* Nothing to do */}
1072 void ETag_surfxml_link___ctn(){/* Nothing to do */}
1073 void ETag_surfxml_argument(){/* Nothing to do */}
1074 void ETag_surfxml_model___prop(){/* Nothing to do */}
1076 /* Open and Close parse file */
1077 void surf_parse_open(const char *file)
1079 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1081 surf_parsed_filename = xbt_strdup(file);
1082 char* dir = xbt_dirname(file);
1083 surf_path.push_back(std::string(dir));
1086 surf_file_to_parse = surf_fopen(file, "r");
1087 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1088 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1089 surf_parse__switch_to_buffer(surf_input_buffer);
1090 surf_parse_lineno = 1;
1093 void surf_parse_close()
1095 if (surf_parsed_filename) {
1096 surf_path.pop_back();
1099 free(surf_parsed_filename);
1100 surf_parsed_filename = nullptr;
1102 if (surf_file_to_parse) {
1103 surf_parse__delete_buffer(surf_input_buffer);
1104 fclose(surf_file_to_parse);
1105 surf_file_to_parse = nullptr; //Must be reset for Bypass
1109 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1110 static int _surf_parse() {
1111 return surf_parse_lex();
1114 int_f_void_t surf_parse = _surf_parse;