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. */
8 #include <stdarg.h> /* va_arg */
10 #include "simgrid/link.h"
11 #include "simgrid/s4u/engine.hpp"
12 #include "simgrid/sg_config.h"
13 #include "src/kernel/routing/NetPoint.hpp"
14 #include "src/surf/network_interface.hpp"
15 #include "src/surf/surf_private.h"
22 #include "src/surf/xml/platf_private.hpp"
23 #include <boost/algorithm/string.hpp>
24 #include <boost/algorithm/string/classification.hpp>
25 #include <boost/algorithm/string/split.hpp>
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
32 int ETag_surfxml_include_state();
34 #include "simgrid_dtd.c"
36 char* surf_parsed_filename = nullptr; // to locate parse error messages
38 std::vector<simgrid::surf::LinkImpl*> parsed_link_list; /* temporary store of current list link of a route */
43 void surf_parse_assert(bool cond, const char *fmt, ...) {
47 int lineno = surf_parse_lineno;
48 char *msg = bvprintf(fmt,va);
51 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
53 xbt_die("Exiting now");
56 void surf_parse_error(const char *fmt, ...) {
59 int lineno = surf_parse_lineno;
60 char *msg = bvprintf(fmt,va);
63 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
65 xbt_die("Exiting now");
67 void surf_parse_warn(const char *fmt, ...) {
70 char *msg = bvprintf(fmt,va);
72 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
76 double surf_parse_get_double(const char *string) {
78 int ret = sscanf(string, "%lg", &res);
80 surf_parse_error("%s is not a double", string);
84 int surf_parse_get_int(const char *string) {
86 int ret = sscanf(string, "%d", &res);
88 surf_parse_error("%s is not an integer", string);
92 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
93 static std::vector<int>* explodesRadical(const char* radicals)
95 std::vector<int>* exploded = new std::vector<int>();
98 std::vector<std::string> radical_elements;
99 boost::split(radical_elements, radicals, boost::is_any_of(","));
100 for (auto group : radical_elements) {
101 std::vector<std::string> radical_ends;
102 boost::split(radical_ends, group, boost::is_any_of("-"));
103 int start = surf_parse_get_int((radical_ends.front()).c_str());
106 switch (radical_ends.size()) {
111 end = surf_parse_get_int((radical_ends.back()).c_str());
114 surf_parse_error("Malformed radical: %s", group.c_str());
117 for (int i = start; i <= end; i++)
118 exploded->push_back(i);
129 /* Note: field `unit' for the last element of parameter `units' should be nullptr. */
130 static double surf_parse_get_value_with_unit(const char *string, const struct unit_scale *units,
131 const char *entity_kind, const char *name, const char *error_msg, const char *default_unit)
136 double res = strtod(string, &ptr);
138 surf_parse_error("value out of range: %s", string);
140 surf_parse_error("cannot parse number: %s", string);
141 if (ptr[0] == '\0') {
143 return res; // Ok, 0 can be unit-less
145 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
146 ptr = (char*)default_unit;
148 for (i = 0; units[i].unit != nullptr && strcmp(ptr, units[i].unit) != 0; i++);
150 if (units[i].unit != nullptr)
151 res *= units[i].scale;
153 surf_parse_error("unknown unit: %s", ptr);
157 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
159 const struct unit_scale units[] = {
160 { "w", 7 * 24 * 60 * 60 },
161 { "d", 24 * 60 * 60 },
171 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
172 "Append 's' to your time to get seconds", "s");
175 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
177 const struct unit_scale units[] = {
178 { "EiB", pow(1024, 6) },
179 { "PiB", pow(1024, 5) },
180 { "TiB", pow(1024, 4) },
181 { "GiB", pow(1024, 3) },
182 { "MiB", pow(1024, 2) },
191 { "Eib", 0.125 * pow(1024, 6) },
192 { "Pib", 0.125 * pow(1024, 5) },
193 { "Tib", 0.125 * pow(1024, 4) },
194 { "Gib", 0.125 * pow(1024, 3) },
195 { "Mib", 0.125 * pow(1024, 2) },
196 { "Kib", 0.125 * 1024 },
197 { "Eb", 0.125 * 1e18 },
198 { "Pb", 0.125 * 1e15 },
199 { "Tb", 0.125 * 1e12 },
200 { "Gb", 0.125 * 1e9 },
201 { "Mb", 0.125 * 1e6 },
202 { "kb", 0.125 * 1e3 },
206 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
207 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
210 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
212 const struct unit_scale units[] = {
213 { "EiBps", pow(1024, 6) },
214 { "PiBps", pow(1024, 5) },
215 { "TiBps", pow(1024, 4) },
216 { "GiBps", pow(1024, 3) },
217 { "MiBps", pow(1024, 2) },
226 { "Eibps", 0.125 * pow(1024, 6) },
227 { "Pibps", 0.125 * pow(1024, 5) },
228 { "Tibps", 0.125 * pow(1024, 4) },
229 { "Gibps", 0.125 * pow(1024, 3) },
230 { "Mibps", 0.125 * pow(1024, 2) },
231 { "Kibps", 0.125 * 1024 },
232 { "Tbps", 0.125 * 1e12 },
233 { "Gbps", 0.125 * 1e9 },
234 { "Mbps", 0.125 * 1e6 },
235 { "kbps", 0.125 * 1e3 },
239 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
240 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
243 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
245 const struct unit_scale units[] = {
246 { "yottaflops", 1e24 },
248 { "zettaflops", 1e21 },
250 { "exaflops", 1e18 },
252 { "petaflops", 1e15 },
254 { "teraflops", 1e12 },
256 { "gigaflops", 1e9 },
258 { "megaflops", 1e6 },
260 { "kiloflops", 1e3 },
266 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
267 "Append 'f' or 'flops' to your speed to get flop per second", "f");
270 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const char* id){
272 std::vector<double> speed_per_pstate;
274 if (strchr(speeds, ',') == nullptr){
275 double speed = surf_parse_get_speed(speeds, entity_kind, id);
276 speed_per_pstate.push_back(speed);
278 std::vector<std::string> pstate_list;
279 boost::split(pstate_list, speeds, boost::is_any_of(","));
280 for (auto speed_str : pstate_list) {
281 boost::trim(speed_str);
282 double speed = surf_parse_get_speed(speed_str.c_str(), entity_kind, id);
283 speed_per_pstate.push_back(speed);
284 XBT_DEBUG("Speed value: %f", speed);
287 return speed_per_pstate;
291 * All the callback lists that can be overridden anywhere.
292 * (this list should probably be reduced to the bare minimum to allow the models to work)
295 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
297 /* The default current property receiver. Setup in the corresponding opening callbacks. */
298 xbt_dict_t current_property_set = nullptr;
299 std::map<std::string, std::string>* current_model_property_set = nullptr;
300 int AS_TAG = 0; // Whether we just opened an AS tag (to see what to do with the properties)
302 /* dictionary of random generator data */
303 xbt_dict_t random_data_list = nullptr;
305 YY_BUFFER_STATE surf_input_buffer;
306 FILE *surf_file_to_parse = nullptr;
309 * Stuff relative to storage
311 void STag_surfxml_storage()
314 XBT_DEBUG("STag_surfxml_storage");
315 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
317 void ETag_surfxml_storage()
319 s_sg_platf_storage_cbarg_t storage;
320 memset(&storage,0,sizeof(storage));
322 storage.properties = current_property_set;
323 current_property_set = nullptr;
325 storage.id = A_surfxml_storage_id;
326 storage.type_id = A_surfxml_storage_typeId;
327 storage.content = A_surfxml_storage_content;
328 storage.content_type = A_surfxml_storage_content___type;
329 storage.attach = A_surfxml_storage_attach;
330 sg_platf_new_storage(&storage);
332 void STag_surfxml_storage___type()
335 XBT_DEBUG("STag_surfxml_storage___type");
336 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
337 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
339 void ETag_surfxml_storage___type()
341 s_sg_platf_storage_type_cbarg_t storage_type;
342 memset(&storage_type,0,sizeof(storage_type));
344 storage_type.properties = current_property_set;
345 current_property_set = nullptr;
347 storage_type.model_properties = current_model_property_set;
348 current_model_property_set = nullptr;
350 storage_type.content = A_surfxml_storage___type_content;
351 storage_type.content_type = A_surfxml_storage___type_content___type;
352 storage_type.id = A_surfxml_storage___type_id;
353 storage_type.model = A_surfxml_storage___type_model;
354 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size,
355 "size of storage type", storage_type.id);
356 sg_platf_new_storage_type(&storage_type);
358 void STag_surfxml_mount()
360 XBT_DEBUG("STag_surfxml_mount");
362 void ETag_surfxml_mount()
364 s_sg_platf_mount_cbarg_t mount;
365 memset(&mount,0,sizeof(mount));
367 mount.name = A_surfxml_mount_name;
368 mount.storageId = A_surfxml_mount_storageId;
369 sg_platf_new_mount(&mount);
373 * Stuff relative to the <include> tag
375 static std::vector<YY_BUFFER_STATE> surf_input_buffer_stack;
376 static std::vector<FILE*> surf_file_to_parse_stack;
377 static std::vector<char*> surf_parsed_filename_stack;
379 void STag_surfxml_include()
381 parse_after_config();
382 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
383 surf_parsed_filename_stack.push_back(surf_parsed_filename); // save old file name
384 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
386 surf_file_to_parse_stack.push_back(surf_file_to_parse); // save old file descriptor
388 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
389 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
391 surf_input_buffer_stack.push_back(surf_input_buffer);
392 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
393 surf_parse_push_buffer_state(surf_input_buffer);
398 void ETag_surfxml_include() {
399 /* Nothing to do when done with reading the include tag.
400 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
403 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
405 * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
406 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
407 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
408 * error message in that case.
410 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
411 * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
412 * on a line. One day maybe, if the maya allow it.
414 int ETag_surfxml_include_state()
417 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
419 if (surf_input_buffer_stack.empty()) // nope, that's a true premature EOF. Let the parser die verbosely.
422 // Yeah, we were in an <include> Restore state and proceed.
423 fclose(surf_file_to_parse);
424 surf_file_to_parse_stack.pop_back();
425 surf_parse_pop_buffer_state();
426 surf_input_buffer_stack.pop_back();
428 // Restore the filename for error messages
429 free(surf_parsed_filename);
430 surf_parsed_filename_stack.pop_back();
435 /* Stag and Etag parse functions */
437 void STag_surfxml_platform() {
438 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
440 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
441 "You're using an ancient XML file.\n"
442 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
443 "instead of MBytes, MFlops and seconds.\n"
445 "Use simgrid_update_xml to update your file automatically. "
446 "This program is installed automatically with SimGrid, or "
447 "available in the tools/ directory of the source archive.\n"
449 "Please check also out the SURF section of the ChangeLog for "
450 "the 3.1 version for more information. \n"
452 "Last, do not forget to also update your values for "
453 "the calls to MSG_task_create (if any).");
454 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
455 "You're using an old XML file.\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.");
459 xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
460 "Changes introduced in SimGrid 3.13:\n"
461 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
462 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
463 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
464 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
466 "Use simgrid_update_xml to update your file automatically. "
467 "This program is installed automatically with SimGrid, or "
468 "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
472 void ETag_surfxml_platform(){
476 void STag_surfxml_host(){
478 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
481 void STag_surfxml_prop()
483 if (AS_TAG) { // We need to retrieve the most recently opened AS
484 XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
485 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
487 netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
490 if (!current_property_set)
491 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
492 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
493 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
494 current_property_set);
498 void ETag_surfxml_host() {
499 s_sg_platf_host_cbarg_t host;
500 memset(&host,0,sizeof(host));
502 host.properties = current_property_set;
503 current_property_set = nullptr;
505 host.id = A_surfxml_host_id;
507 host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
509 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
510 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
511 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
512 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
513 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
514 host.coord = A_surfxml_host_coordinates;
516 sg_platf_new_host(&host);
519 void STag_surfxml_host___link(){
520 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
521 s_sg_platf_host_link_cbarg_t host_link;
522 memset(&host_link,0,sizeof(host_link));
524 host_link.id = A_surfxml_host___link_id;
525 host_link.link_up = A_surfxml_host___link_up;
526 host_link.link_down = A_surfxml_host___link_down;
527 sg_platf_new_hostlink(&host_link);
530 void STag_surfxml_router(){
531 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
534 void ETag_surfxml_cluster(){
535 s_sg_platf_cluster_cbarg_t cluster;
536 memset(&cluster,0,sizeof(cluster));
537 cluster.properties = current_property_set;
538 current_property_set = nullptr;
540 cluster.id = A_surfxml_cluster_id;
541 cluster.prefix = A_surfxml_cluster_prefix;
542 cluster.suffix = A_surfxml_cluster_suffix;
543 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
544 cluster.speeds = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
545 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
546 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
547 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
548 if(strcmp(A_surfxml_cluster_bb___bw,""))
549 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
550 if(strcmp(A_surfxml_cluster_bb___lat,""))
551 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
552 if(strcmp(A_surfxml_cluster_limiter___link,""))
553 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
554 if(strcmp(A_surfxml_cluster_loopback___bw,""))
555 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
556 if(strcmp(A_surfxml_cluster_loopback___lat,""))
557 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
559 switch(AX_surfxml_cluster_topology){
560 case A_surfxml_cluster_topology_FLAT:
561 cluster.topology= SURF_CLUSTER_FLAT ;
563 case A_surfxml_cluster_topology_TORUS:
564 cluster.topology= SURF_CLUSTER_TORUS ;
566 case A_surfxml_cluster_topology_FAT___TREE:
567 cluster.topology = SURF_CLUSTER_FAT_TREE;
569 case A_surfxml_cluster_topology_DRAGONFLY:
570 cluster.topology= SURF_CLUSTER_DRAGONFLY ;
573 surf_parse_error("Invalid cluster topology for cluster %s",
577 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
578 cluster.router_id = A_surfxml_cluster_router___id;
580 switch (AX_surfxml_cluster_sharing___policy) {
581 case A_surfxml_cluster_sharing___policy_SHARED:
582 cluster.sharing_policy = SURF_LINK_SHARED;
584 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
585 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
587 case A_surfxml_cluster_sharing___policy_FATPIPE:
588 cluster.sharing_policy = SURF_LINK_FATPIPE;
591 surf_parse_error("Invalid cluster sharing policy for cluster %s", cluster.id);
594 switch (AX_surfxml_cluster_bb___sharing___policy) {
595 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
596 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
598 case A_surfxml_cluster_bb___sharing___policy_SHARED:
599 cluster.bb_sharing_policy = SURF_LINK_SHARED;
602 surf_parse_error("Invalid bb sharing policy in cluster %s", cluster.id);
606 sg_platf_new_cluster(&cluster);
609 void STag_surfxml_cluster(){
611 parse_after_config();
612 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
615 void STag_surfxml_cabinet(){
616 parse_after_config();
617 s_sg_platf_cabinet_cbarg_t cabinet;
618 memset(&cabinet,0,sizeof(cabinet));
619 cabinet.id = A_surfxml_cabinet_id;
620 cabinet.prefix = A_surfxml_cabinet_prefix;
621 cabinet.suffix = A_surfxml_cabinet_suffix;
622 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
623 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
624 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
625 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
627 sg_platf_new_cabinet(&cabinet);
630 void STag_surfxml_peer(){
631 parse_after_config();
632 s_sg_platf_peer_cbarg_t peer;
633 memset(&peer,0,sizeof(peer));
634 peer.id = A_surfxml_peer_id;
635 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
636 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
637 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
638 peer.coord = A_surfxml_peer_coordinates;
639 peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
640 peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
642 if (A_surfxml_peer_lat[0] != '\0')
643 XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
646 sg_platf_new_peer(&peer);
649 void STag_surfxml_link(){
651 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
654 void ETag_surfxml_link(){
655 LinkCreationArgs link;
657 link.properties = current_property_set;
658 current_property_set = nullptr;
660 link.id = std::string(A_surfxml_link_id);
661 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
662 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
663 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
664 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
665 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
667 switch (A_surfxml_link_sharing___policy) {
668 case A_surfxml_link_sharing___policy_SHARED:
669 link.policy = SURF_LINK_SHARED;
671 case A_surfxml_link_sharing___policy_FATPIPE:
672 link.policy = SURF_LINK_FATPIPE;
674 case A_surfxml_link_sharing___policy_FULLDUPLEX:
675 link.policy = SURF_LINK_FULLDUPLEX;
678 surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
682 sg_platf_new_link(&link);
685 void STag_surfxml_link___ctn(){
687 simgrid::surf::LinkImpl* link = nullptr;
688 char *link_name=nullptr;
689 switch (A_surfxml_link___ctn_direction) {
690 case AU_surfxml_link___ctn_direction:
691 case A_surfxml_link___ctn_direction_NONE:
692 link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
694 case A_surfxml_link___ctn_direction_UP:
695 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
696 link = simgrid::surf::LinkImpl::byName(link_name);
698 case A_surfxml_link___ctn_direction_DOWN:
699 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
700 link = simgrid::surf::LinkImpl::byName(link_name);
703 xbt_free(link_name); // no-op if it's already nullptr
705 const char* dirname = "";
706 switch (A_surfxml_link___ctn_direction) {
707 case A_surfxml_link___ctn_direction_UP:
708 dirname = " (upward)";
710 case A_surfxml_link___ctn_direction_DOWN:
711 dirname = " (downward)";
716 surf_parse_assert(link != nullptr, "No such link: '%s'%s", A_surfxml_link___ctn_id, dirname);
717 parsed_link_list.push_back(link);
720 void ETag_surfxml_backbone(){
721 LinkCreationArgs link;
723 link.properties = nullptr;
724 link.id = std::string(A_surfxml_backbone_id);
725 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
726 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
727 link.policy = SURF_LINK_SHARED;
729 sg_platf_new_link(&link);
730 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
733 void STag_surfxml_route(){
734 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
735 A_surfxml_route_src);
736 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
737 A_surfxml_route_dst);
740 void STag_surfxml_ASroute(){
741 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
742 A_surfxml_route_src);
743 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
744 A_surfxml_route_dst);
746 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
747 A_surfxml_ASroute_gw___src);
748 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
749 A_surfxml_ASroute_gw___dst);
752 void STag_surfxml_bypassRoute(){
753 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
754 A_surfxml_bypassRoute_src);
755 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
756 A_surfxml_bypassRoute_dst);
759 void STag_surfxml_bypassASroute(){
760 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
761 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
762 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
763 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
764 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
765 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
766 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
767 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
770 void ETag_surfxml_route(){
771 s_sg_platf_route_cbarg_t route;
772 memset(&route,0,sizeof(route));
774 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
775 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
776 route.gw_src = nullptr;
777 route.gw_dst = nullptr;
778 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
779 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
781 for (auto link: parsed_link_list)
782 route.link_list->push_back(link);
783 parsed_link_list.clear();
785 sg_platf_new_route(&route);
786 delete route.link_list;
789 void ETag_surfxml_ASroute(){
790 s_sg_platf_route_cbarg_t ASroute;
791 memset(&ASroute,0,sizeof(ASroute));
793 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
794 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
796 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
797 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
799 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
801 for (auto link: parsed_link_list)
802 ASroute.link_list->push_back(link);
803 parsed_link_list.clear();
805 switch (A_surfxml_ASroute_symmetrical) {
806 case AU_surfxml_ASroute_symmetrical:
807 case A_surfxml_ASroute_symmetrical_YES:
808 ASroute.symmetrical = true;
810 case A_surfxml_ASroute_symmetrical_NO:
811 ASroute.symmetrical = false;
815 sg_platf_new_route(&ASroute);
816 delete ASroute.link_list;
819 void ETag_surfxml_bypassRoute(){
820 s_sg_platf_route_cbarg_t route;
821 memset(&route,0,sizeof(route));
823 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
824 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
825 route.gw_src = nullptr;
826 route.gw_dst = nullptr;
827 route.symmetrical = false;
828 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
830 for (auto link: parsed_link_list)
831 route.link_list->push_back(link);
832 parsed_link_list.clear();
834 sg_platf_new_bypassRoute(&route);
835 delete route.link_list;
838 void ETag_surfxml_bypassASroute(){
839 s_sg_platf_route_cbarg_t ASroute;
840 memset(&ASroute,0,sizeof(ASroute));
842 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
843 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
844 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
845 for (auto link: parsed_link_list)
846 ASroute.link_list->push_back(link);
847 parsed_link_list.clear();
849 ASroute.symmetrical = false;
851 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
852 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
854 sg_platf_new_bypassRoute(&ASroute);
855 delete ASroute.link_list;
858 void ETag_surfxml_trace(){
859 s_sg_platf_trace_cbarg_t trace;
860 memset(&trace,0,sizeof(trace));
862 trace.id = A_surfxml_trace_id;
863 trace.file = A_surfxml_trace_file;
864 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
865 trace.pc_data = surfxml_pcdata;
867 sg_platf_new_trace(&trace);
870 void STag_surfxml_trace___connect(){
871 parse_after_config();
872 s_sg_platf_trace_connect_cbarg_t trace_connect;
873 memset(&trace_connect,0,sizeof(trace_connect));
875 trace_connect.element = A_surfxml_trace___connect_element;
876 trace_connect.trace = A_surfxml_trace___connect_trace;
878 switch (A_surfxml_trace___connect_kind) {
879 case AU_surfxml_trace___connect_kind:
880 case A_surfxml_trace___connect_kind_SPEED:
881 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
883 case A_surfxml_trace___connect_kind_BANDWIDTH:
884 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
886 case A_surfxml_trace___connect_kind_HOST___AVAIL:
887 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
889 case A_surfxml_trace___connect_kind_LATENCY:
890 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
892 case A_surfxml_trace___connect_kind_LINK___AVAIL:
893 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
896 sg_platf_trace_connect(&trace_connect);
899 void STag_surfxml_AS(){
900 parse_after_config();
902 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
904 sg_platf_new_AS_begin(&AS);
906 void ETag_surfxml_AS(){
907 sg_platf_new_AS_seal();
910 void STag_surfxml_config(){
912 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
913 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
914 if (_sg_cfg_init_status == 2) {
915 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
918 void ETag_surfxml_config(){
919 xbt_dict_cursor_t cursor = nullptr;
922 xbt_dict_foreach(current_property_set, cursor, key, elem) {
923 if (xbt_cfg_is_default_value(key)) {
924 char* cfg = bprintf("%s:%s", key, elem);
925 xbt_cfg_set_parse(cfg);
928 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
930 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
932 xbt_dict_free(¤t_property_set);
933 current_property_set = nullptr;
939 void STag_surfxml_process(){
942 argv = xbt_new(char *, 1);
943 argv[0] = xbt_strdup(A_surfxml_process_function);
944 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
947 void ETag_surfxml_process(){
948 s_sg_platf_process_cbarg_t process;
949 memset(&process,0,sizeof(process));
952 process.argv = (const char **)argv;
953 process.properties = current_property_set;
954 process.host = A_surfxml_process_host;
955 process.function = A_surfxml_process_function;
956 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
957 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
959 switch (A_surfxml_process_on___failure) {
960 case AU_surfxml_process_on___failure:
961 case A_surfxml_process_on___failure_DIE:
962 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
964 case A_surfxml_process_on___failure_RESTART:
965 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
969 sg_platf_new_process(&process);
971 for (int i = 0; i != argc; ++i)
976 current_property_set = nullptr;
979 void STag_surfxml_argument(){
981 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
982 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
985 void STag_surfxml_model___prop(){
986 if (!current_model_property_set)
987 current_model_property_set = new std::map<std::string, std::string>();
989 current_model_property_set->insert(
990 {std::string(A_surfxml_model___prop_id), std::string(A_surfxml_model___prop_value)});
993 void ETag_surfxml_prop(){/* Nothing to do */}
994 void STag_surfxml_random(){/* Nothing to do */}
995 void ETag_surfxml_random(){/* Nothing to do */}
996 void ETag_surfxml_trace___connect(){/* Nothing to do */}
997 void STag_surfxml_trace(){parse_after_config();}
998 void ETag_surfxml_router(){/*Nothing to do*/}
999 void ETag_surfxml_host___link(){/* Nothing to do */}
1000 void ETag_surfxml_cabinet(){/* Nothing to do */}
1001 void ETag_surfxml_peer(){/* Nothing to do */}
1002 void STag_surfxml_backbone(){/* Nothing to do */}
1003 void ETag_surfxml_link___ctn(){/* Nothing to do */}
1004 void ETag_surfxml_argument(){/* Nothing to do */}
1005 void ETag_surfxml_model___prop(){/* Nothing to do */}
1007 /* Open and Close parse file */
1008 void surf_parse_open(const char *file)
1010 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1012 surf_parsed_filename = xbt_strdup(file);
1013 char* dir = xbt_dirname(file);
1014 surf_path.push_back(std::string(dir));
1017 surf_file_to_parse = surf_fopen(file, "r");
1018 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1019 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1020 surf_parse__switch_to_buffer(surf_input_buffer);
1021 surf_parse_lineno = 1;
1024 void surf_parse_close()
1026 if (surf_parsed_filename) {
1027 surf_path.pop_back();
1030 free(surf_parsed_filename);
1031 surf_parsed_filename = nullptr;
1033 if (surf_file_to_parse) {
1034 surf_parse__delete_buffer(surf_input_buffer);
1035 fclose(surf_file_to_parse);
1036 surf_file_to_parse = nullptr; //Must be reset for Bypass
1040 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1041 static int _surf_parse() {
1042 return surf_parse_lex();
1045 int_f_void_t surf_parse = _surf_parse;