1 /* Copyright (c) 2006-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <stdarg.h> /* va_arg */
11 #include "simgrid/link.h"
12 #include "simgrid/s4u/engine.hpp"
13 #include "simgrid/sg_config.h"
14 #include "src/kernel/routing/NetPoint.hpp"
15 #include "src/surf/network_interface.hpp"
16 #include "src/surf/surf_private.h"
23 #include "src/surf/xml/platf_private.hpp"
24 #include <boost/algorithm/string.hpp>
25 #include <boost/algorithm/string/classification.hpp>
26 #include <boost/algorithm/string/split.hpp>
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
33 int ETag_surfxml_include_state();
35 #include "simgrid_dtd.c"
37 char* surf_parsed_filename = nullptr; // to locate parse error messages
39 std::vector<simgrid::surf::LinkImpl*> parsed_link_list; /* temporary store of current list link of a route */
44 void surf_parse_assert(bool cond, const char *fmt, ...) {
48 int lineno = surf_parse_lineno;
49 char *msg = bvprintf(fmt,va);
52 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
54 xbt_die("Exiting now");
57 void surf_parse_error(const char *fmt, ...) {
60 int lineno = surf_parse_lineno;
61 char *msg = bvprintf(fmt,va);
64 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
66 xbt_die("Exiting now");
68 void surf_parse_warn(const char *fmt, ...) {
71 char *msg = bvprintf(fmt,va);
73 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
77 double surf_parse_get_double(const char *string) {
79 int ret = sscanf(string, "%lg", &res);
81 surf_parse_error("%s is not a double", string);
85 int surf_parse_get_int(const char *string) {
87 int ret = sscanf(string, "%d", &res);
89 surf_parse_error("%s is not an integer", string);
93 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
94 static std::vector<int>* explodesRadical(const char* radicals)
96 std::vector<int>* exploded = new std::vector<int>();
99 std::vector<std::string> radical_elements;
100 boost::split(radical_elements, radicals, boost::is_any_of(","));
101 for (auto group : radical_elements) {
102 std::vector<std::string> radical_ends;
103 boost::split(radical_ends, group, boost::is_any_of("-"));
104 int start = surf_parse_get_int((radical_ends.front()).c_str());
107 switch (radical_ends.size()) {
112 end = surf_parse_get_int((radical_ends.back()).c_str());
115 surf_parse_error("Malformed radical: %s", group.c_str());
118 for (int i = start; i <= end; i++)
119 exploded->push_back(i);
130 /* Note: field `unit' for the last element of parameter `units' should be nullptr. */
131 static double surf_parse_get_value_with_unit(const char *string, const struct unit_scale *units,
132 const char *entity_kind, const char *name, const char *error_msg, const char *default_unit)
137 double res = strtod(string, &ptr);
139 surf_parse_error("value out of range: %s", string);
141 surf_parse_error("cannot parse number: %s", string);
142 if (ptr[0] == '\0') {
144 return res; // Ok, 0 can be unit-less
146 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
147 ptr = (char*)default_unit;
149 for (i = 0; units[i].unit != nullptr && strcmp(ptr, units[i].unit) != 0; i++);
151 if (units[i].unit != nullptr)
152 res *= units[i].scale;
154 surf_parse_error("unknown unit: %s", ptr);
158 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
160 const struct unit_scale units[] = {
161 { "w", 7 * 24 * 60 * 60 },
162 { "d", 24 * 60 * 60 },
172 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
173 "Append 's' to your time to get seconds", "s");
176 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
178 const struct unit_scale units[] = {
179 { "EiB", pow(1024, 6) },
180 { "PiB", pow(1024, 5) },
181 { "TiB", pow(1024, 4) },
182 { "GiB", pow(1024, 3) },
183 { "MiB", pow(1024, 2) },
192 { "Eib", 0.125 * pow(1024, 6) },
193 { "Pib", 0.125 * pow(1024, 5) },
194 { "Tib", 0.125 * pow(1024, 4) },
195 { "Gib", 0.125 * pow(1024, 3) },
196 { "Mib", 0.125 * pow(1024, 2) },
197 { "Kib", 0.125 * 1024 },
198 { "Eb", 0.125 * 1e18 },
199 { "Pb", 0.125 * 1e15 },
200 { "Tb", 0.125 * 1e12 },
201 { "Gb", 0.125 * 1e9 },
202 { "Mb", 0.125 * 1e6 },
203 { "kb", 0.125 * 1e3 },
207 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
208 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
211 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
213 const struct unit_scale units[] = {
214 { "EiBps", pow(1024, 6) },
215 { "PiBps", pow(1024, 5) },
216 { "TiBps", pow(1024, 4) },
217 { "GiBps", pow(1024, 3) },
218 { "MiBps", pow(1024, 2) },
227 { "Eibps", 0.125 * pow(1024, 6) },
228 { "Pibps", 0.125 * pow(1024, 5) },
229 { "Tibps", 0.125 * pow(1024, 4) },
230 { "Gibps", 0.125 * pow(1024, 3) },
231 { "Mibps", 0.125 * pow(1024, 2) },
232 { "Kibps", 0.125 * 1024 },
233 { "Tbps", 0.125 * 1e12 },
234 { "Gbps", 0.125 * 1e9 },
235 { "Mbps", 0.125 * 1e6 },
236 { "kbps", 0.125 * 1e3 },
240 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
241 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
244 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
246 const struct unit_scale units[] = {
247 { "yottaflops", 1e24 },
249 { "zettaflops", 1e21 },
251 { "exaflops", 1e18 },
253 { "petaflops", 1e15 },
255 { "teraflops", 1e12 },
257 { "gigaflops", 1e9 },
259 { "megaflops", 1e6 },
261 { "kiloflops", 1e3 },
267 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
268 "Append 'f' or 'flops' to your speed to get flop per second", "f");
271 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const char* id){
273 std::vector<double> speed_per_pstate;
275 if (strchr(speeds, ',') == nullptr){
276 double speed = surf_parse_get_speed(speeds, entity_kind, id);
277 speed_per_pstate.push_back(speed);
279 std::vector<std::string> pstate_list;
280 boost::split(pstate_list, speeds, boost::is_any_of(","));
281 for (auto speed_str : pstate_list) {
282 boost::trim(speed_str);
283 double speed = surf_parse_get_speed(speed_str.c_str(), entity_kind, id);
284 speed_per_pstate.push_back(speed);
285 XBT_DEBUG("Speed value: %f", speed);
288 return speed_per_pstate;
292 * All the callback lists that can be overridden anywhere.
293 * (this list should probably be reduced to the bare minimum to allow the models to work)
296 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
298 /* The default current property receiver. Setup in the corresponding opening callbacks. */
299 xbt_dict_t current_property_set = nullptr;
300 xbt_dict_t current_model_property_set = nullptr;
301 int AS_TAG = 0; // Whether we just opened an AS tag (to see what to do with the properties)
303 /* dictionary of random generator data */
304 xbt_dict_t random_data_list = nullptr;
306 YY_BUFFER_STATE surf_input_buffer;
307 FILE *surf_file_to_parse = nullptr;
310 * Stuff relative to storage
312 void STag_surfxml_storage()
315 XBT_DEBUG("STag_surfxml_storage");
316 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
318 void ETag_surfxml_storage()
320 s_sg_platf_storage_cbarg_t storage;
321 memset(&storage,0,sizeof(storage));
323 storage.properties = current_property_set;
324 current_property_set = nullptr;
326 storage.id = A_surfxml_storage_id;
327 storage.type_id = A_surfxml_storage_typeId;
328 storage.content = A_surfxml_storage_content;
329 storage.content_type = A_surfxml_storage_content___type;
330 storage.attach = A_surfxml_storage_attach;
331 sg_platf_new_storage(&storage);
333 void STag_surfxml_storage___type()
336 XBT_DEBUG("STag_surfxml_storage___type");
337 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
338 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
340 void ETag_surfxml_storage___type()
342 s_sg_platf_storage_type_cbarg_t storage_type;
343 memset(&storage_type,0,sizeof(storage_type));
345 storage_type.properties = current_property_set;
346 current_property_set = nullptr;
348 storage_type.model_properties = current_model_property_set;
349 current_model_property_set = nullptr;
351 storage_type.content = A_surfxml_storage___type_content;
352 storage_type.content_type = A_surfxml_storage___type_content___type;
353 storage_type.id = A_surfxml_storage___type_id;
354 storage_type.model = A_surfxml_storage___type_model;
355 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size,
356 "size of storage type", storage_type.id);
357 sg_platf_new_storage_type(&storage_type);
359 void STag_surfxml_mount()
361 XBT_DEBUG("STag_surfxml_mount");
363 void ETag_surfxml_mount()
365 s_sg_platf_mount_cbarg_t mount;
366 memset(&mount,0,sizeof(mount));
368 mount.name = A_surfxml_mount_name;
369 mount.storageId = A_surfxml_mount_storageId;
370 sg_platf_new_mount(&mount);
374 * Stuff relative to the <include> tag
376 static std::vector<YY_BUFFER_STATE> surf_input_buffer_stack;
377 static std::vector<FILE*> surf_file_to_parse_stack;
378 static std::vector<char*> surf_parsed_filename_stack;
380 void STag_surfxml_include()
382 parse_after_config();
383 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
384 surf_parsed_filename_stack.push_back(surf_parsed_filename); // save old file name
385 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
387 surf_file_to_parse_stack.push_back(surf_file_to_parse); // save old file descriptor
389 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
390 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
392 surf_input_buffer_stack.push_back(surf_input_buffer);
393 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
394 surf_parse_push_buffer_state(surf_input_buffer);
399 void ETag_surfxml_include() {
400 /* Nothing to do when done with reading the include tag.
401 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
404 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
406 * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
407 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
408 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
409 * error message in that case.
411 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
412 * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
413 * on a line. One day maybe, if the maya allow it.
415 int ETag_surfxml_include_state()
418 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
420 if (surf_input_buffer_stack.empty()) // nope, that's a true premature EOF. Let the parser die verbosely.
423 // Yeah, we were in an <include> Restore state and proceed.
424 fclose(surf_file_to_parse);
425 surf_file_to_parse_stack.pop_back();
426 surf_parse_pop_buffer_state();
427 surf_input_buffer_stack.pop_back();
429 // Restore the filename for error messages
430 free(surf_parsed_filename);
431 surf_parsed_filename_stack.pop_back();
436 /* Stag and Etag parse functions */
438 void STag_surfxml_platform() {
439 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
441 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
442 "You're using an ancient XML file.\n"
443 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
444 "instead of MBytes, MFlops and seconds.\n"
446 "Use simgrid_update_xml to update your file automatically. "
447 "This program is installed automatically with SimGrid, or "
448 "available in the tools/ directory of the source archive.\n"
450 "Please check also out the SURF section of the ChangeLog for "
451 "the 3.1 version for more information. \n"
453 "Last, do not forget to also update your values for "
454 "the calls to MSG_task_create (if any).");
455 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
456 "You're using an old XML file.\n"
457 "Use simgrid_update_xml to update your file automatically. "
458 "This program is installed automatically with SimGrid, or "
459 "available in the tools/ directory of the source archive.");
460 xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
461 "Changes introduced in SimGrid 3.13:\n"
462 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
463 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
464 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
465 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
467 "Use simgrid_update_xml to update your file automatically. "
468 "This program is installed automatically with SimGrid, or "
469 "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
473 void ETag_surfxml_platform(){
477 void STag_surfxml_host(){
479 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
482 void STag_surfxml_prop()
484 if (AS_TAG) { // We need to retrieve the most recently opened AS
485 XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
486 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
488 netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
491 if (!current_property_set)
492 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
493 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
494 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
495 current_property_set);
499 void ETag_surfxml_host() {
500 s_sg_platf_host_cbarg_t host;
501 memset(&host,0,sizeof(host));
503 host.properties = current_property_set;
504 current_property_set = nullptr;
506 host.id = A_surfxml_host_id;
508 host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
510 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
511 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
512 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
513 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
514 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
515 host.coord = A_surfxml_host_coordinates;
517 sg_platf_new_host(&host);
520 void STag_surfxml_host___link(){
521 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
522 s_sg_platf_host_link_cbarg_t host_link;
523 memset(&host_link,0,sizeof(host_link));
525 host_link.id = A_surfxml_host___link_id;
526 host_link.link_up = A_surfxml_host___link_up;
527 host_link.link_down = A_surfxml_host___link_down;
528 sg_platf_new_hostlink(&host_link);
531 void STag_surfxml_router(){
532 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
535 void ETag_surfxml_cluster(){
536 s_sg_platf_cluster_cbarg_t cluster;
537 memset(&cluster,0,sizeof(cluster));
538 cluster.properties = current_property_set;
539 current_property_set = nullptr;
541 cluster.id = A_surfxml_cluster_id;
542 cluster.prefix = A_surfxml_cluster_prefix;
543 cluster.suffix = A_surfxml_cluster_suffix;
544 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
545 cluster.speeds = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
546 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
547 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
548 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
549 if(strcmp(A_surfxml_cluster_bb___bw,""))
550 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
551 if(strcmp(A_surfxml_cluster_bb___lat,""))
552 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
553 if(strcmp(A_surfxml_cluster_limiter___link,""))
554 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
555 if(strcmp(A_surfxml_cluster_loopback___bw,""))
556 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
557 if(strcmp(A_surfxml_cluster_loopback___lat,""))
558 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
560 switch(AX_surfxml_cluster_topology){
561 case A_surfxml_cluster_topology_FLAT:
562 cluster.topology= SURF_CLUSTER_FLAT ;
564 case A_surfxml_cluster_topology_TORUS:
565 cluster.topology= SURF_CLUSTER_TORUS ;
567 case A_surfxml_cluster_topology_FAT___TREE:
568 cluster.topology = SURF_CLUSTER_FAT_TREE;
570 case A_surfxml_cluster_topology_DRAGONFLY:
571 cluster.topology= SURF_CLUSTER_DRAGONFLY ;
574 surf_parse_error("Invalid cluster topology for cluster %s",
578 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
579 cluster.router_id = A_surfxml_cluster_router___id;
581 switch (AX_surfxml_cluster_sharing___policy) {
582 case A_surfxml_cluster_sharing___policy_SHARED:
583 cluster.sharing_policy = SURF_LINK_SHARED;
585 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
586 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
588 case A_surfxml_cluster_sharing___policy_FATPIPE:
589 cluster.sharing_policy = SURF_LINK_FATPIPE;
592 surf_parse_error("Invalid cluster sharing policy for cluster %s", cluster.id);
595 switch (AX_surfxml_cluster_bb___sharing___policy) {
596 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
597 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
599 case A_surfxml_cluster_bb___sharing___policy_SHARED:
600 cluster.bb_sharing_policy = SURF_LINK_SHARED;
603 surf_parse_error("Invalid bb sharing policy in cluster %s", cluster.id);
607 sg_platf_new_cluster(&cluster);
610 void STag_surfxml_cluster(){
612 parse_after_config();
613 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
616 void STag_surfxml_cabinet(){
617 parse_after_config();
618 s_sg_platf_cabinet_cbarg_t cabinet;
619 memset(&cabinet,0,sizeof(cabinet));
620 cabinet.id = A_surfxml_cabinet_id;
621 cabinet.prefix = A_surfxml_cabinet_prefix;
622 cabinet.suffix = A_surfxml_cabinet_suffix;
623 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
624 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
625 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
626 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
628 sg_platf_new_cabinet(&cabinet);
631 void STag_surfxml_peer(){
632 parse_after_config();
633 s_sg_platf_peer_cbarg_t peer;
634 memset(&peer,0,sizeof(peer));
635 peer.id = A_surfxml_peer_id;
636 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
637 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
638 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
639 peer.coord = A_surfxml_peer_coordinates;
640 peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
641 peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
643 if (A_surfxml_peer_lat[0] != '\0')
644 XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
647 sg_platf_new_peer(&peer);
650 void STag_surfxml_link(){
652 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
655 void ETag_surfxml_link(){
656 LinkCreationArgs link;
658 link.properties = current_property_set;
659 current_property_set = nullptr;
661 link.id = std::string(A_surfxml_link_id);
662 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
663 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
664 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
665 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
666 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
668 switch (A_surfxml_link_sharing___policy) {
669 case A_surfxml_link_sharing___policy_SHARED:
670 link.policy = SURF_LINK_SHARED;
672 case A_surfxml_link_sharing___policy_FATPIPE:
673 link.policy = SURF_LINK_FATPIPE;
675 case A_surfxml_link_sharing___policy_FULLDUPLEX:
676 link.policy = SURF_LINK_FULLDUPLEX;
679 surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
683 sg_platf_new_link(&link);
686 void STag_surfxml_link___ctn(){
688 simgrid::surf::LinkImpl* link = nullptr;
689 char *link_name=nullptr;
690 switch (A_surfxml_link___ctn_direction) {
691 case AU_surfxml_link___ctn_direction:
692 case A_surfxml_link___ctn_direction_NONE:
693 link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
695 case A_surfxml_link___ctn_direction_UP:
696 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
697 link = simgrid::surf::LinkImpl::byName(link_name);
699 case A_surfxml_link___ctn_direction_DOWN:
700 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
701 link = simgrid::surf::LinkImpl::byName(link_name);
704 xbt_free(link_name); // no-op if it's already nullptr
706 const char* dirname = "";
707 switch (A_surfxml_link___ctn_direction) {
708 case A_surfxml_link___ctn_direction_UP:
709 dirname = " (upward)";
711 case A_surfxml_link___ctn_direction_DOWN:
712 dirname = " (downward)";
717 surf_parse_assert(link != nullptr, "No such link: '%s'%s", A_surfxml_link___ctn_id, dirname);
718 parsed_link_list.push_back(link);
721 void ETag_surfxml_backbone(){
722 LinkCreationArgs link;
724 link.properties = nullptr;
725 link.id = std::string(A_surfxml_backbone_id);
726 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
727 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
728 link.policy = SURF_LINK_SHARED;
730 sg_platf_new_link(&link);
731 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
734 void STag_surfxml_route(){
735 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
736 A_surfxml_route_src);
737 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
738 A_surfxml_route_dst);
741 void STag_surfxml_ASroute(){
742 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
743 A_surfxml_route_src);
744 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
745 A_surfxml_route_dst);
747 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
748 A_surfxml_ASroute_gw___src);
749 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
750 A_surfxml_ASroute_gw___dst);
753 void STag_surfxml_bypassRoute(){
754 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
755 A_surfxml_bypassRoute_src);
756 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
757 A_surfxml_bypassRoute_dst);
760 void STag_surfxml_bypassASroute(){
761 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
762 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
763 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
764 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
765 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
766 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
767 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
768 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
771 void ETag_surfxml_route(){
772 s_sg_platf_route_cbarg_t route;
773 memset(&route,0,sizeof(route));
775 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
776 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
777 route.gw_src = nullptr;
778 route.gw_dst = nullptr;
779 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
780 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
782 for (auto link: parsed_link_list)
783 route.link_list->push_back(link);
784 parsed_link_list.clear();
786 sg_platf_new_route(&route);
787 delete route.link_list;
790 void ETag_surfxml_ASroute(){
791 s_sg_platf_route_cbarg_t ASroute;
792 memset(&ASroute,0,sizeof(ASroute));
794 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
795 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
797 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
798 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
800 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
802 for (auto link: parsed_link_list)
803 ASroute.link_list->push_back(link);
804 parsed_link_list.clear();
806 switch (A_surfxml_ASroute_symmetrical) {
807 case AU_surfxml_ASroute_symmetrical:
808 case A_surfxml_ASroute_symmetrical_YES:
809 ASroute.symmetrical = true;
811 case A_surfxml_ASroute_symmetrical_NO:
812 ASroute.symmetrical = false;
816 sg_platf_new_route(&ASroute);
817 delete ASroute.link_list;
820 void ETag_surfxml_bypassRoute(){
821 s_sg_platf_route_cbarg_t route;
822 memset(&route,0,sizeof(route));
824 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
825 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
826 route.gw_src = nullptr;
827 route.gw_dst = nullptr;
828 route.symmetrical = false;
829 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
831 for (auto link: parsed_link_list)
832 route.link_list->push_back(link);
833 parsed_link_list.clear();
835 sg_platf_new_bypassRoute(&route);
836 delete route.link_list;
839 void ETag_surfxml_bypassASroute(){
840 s_sg_platf_route_cbarg_t ASroute;
841 memset(&ASroute,0,sizeof(ASroute));
843 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
844 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
845 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
846 for (auto link: parsed_link_list)
847 ASroute.link_list->push_back(link);
848 parsed_link_list.clear();
850 ASroute.symmetrical = false;
852 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
853 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
855 sg_platf_new_bypassRoute(&ASroute);
856 delete ASroute.link_list;
859 void ETag_surfxml_trace(){
860 s_sg_platf_trace_cbarg_t trace;
861 memset(&trace,0,sizeof(trace));
863 trace.id = A_surfxml_trace_id;
864 trace.file = A_surfxml_trace_file;
865 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
866 trace.pc_data = surfxml_pcdata;
868 sg_platf_new_trace(&trace);
871 void STag_surfxml_trace___connect(){
872 parse_after_config();
873 s_sg_platf_trace_connect_cbarg_t trace_connect;
874 memset(&trace_connect,0,sizeof(trace_connect));
876 trace_connect.element = A_surfxml_trace___connect_element;
877 trace_connect.trace = A_surfxml_trace___connect_trace;
879 switch (A_surfxml_trace___connect_kind) {
880 case AU_surfxml_trace___connect_kind:
881 case A_surfxml_trace___connect_kind_SPEED:
882 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
884 case A_surfxml_trace___connect_kind_BANDWIDTH:
885 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
887 case A_surfxml_trace___connect_kind_HOST___AVAIL:
888 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
890 case A_surfxml_trace___connect_kind_LATENCY:
891 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
893 case A_surfxml_trace___connect_kind_LINK___AVAIL:
894 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
897 sg_platf_trace_connect(&trace_connect);
900 void STag_surfxml_AS(){
901 parse_after_config();
903 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
905 sg_platf_new_AS_begin(&AS);
907 void ETag_surfxml_AS(){
908 sg_platf_new_AS_seal();
911 void STag_surfxml_config(){
913 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
914 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
915 if (_sg_cfg_init_status == 2) {
916 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
919 void ETag_surfxml_config(){
920 xbt_dict_cursor_t cursor = nullptr;
923 xbt_dict_foreach(current_property_set, cursor, key, elem) {
924 if (xbt_cfg_is_default_value(key)) {
925 char* cfg = bprintf("%s:%s", key, elem);
926 xbt_cfg_set_parse(cfg);
929 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
931 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
933 xbt_dict_free(¤t_property_set);
934 current_property_set = nullptr;
940 void STag_surfxml_process(){
943 argv = xbt_new(char *, 1);
944 argv[0] = xbt_strdup(A_surfxml_process_function);
945 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
948 void ETag_surfxml_process(){
949 s_sg_platf_process_cbarg_t process;
950 memset(&process,0,sizeof(process));
953 process.argv = (const char **)argv;
954 process.properties = current_property_set;
955 process.host = A_surfxml_process_host;
956 process.function = A_surfxml_process_function;
957 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
958 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
960 switch (A_surfxml_process_on___failure) {
961 case AU_surfxml_process_on___failure:
962 case A_surfxml_process_on___failure_DIE:
963 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
965 case A_surfxml_process_on___failure_RESTART:
966 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
970 sg_platf_new_process(&process);
972 for (int i = 0; i != argc; ++i)
977 current_property_set = nullptr;
980 void STag_surfxml_argument(){
982 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
983 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
986 void STag_surfxml_model___prop(){
987 if (!current_model_property_set)
988 current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
990 xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
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 xbt_dynar_push(surf_path, &dir);
1016 surf_file_to_parse = surf_fopen(file, "r");
1017 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1018 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1019 surf_parse__switch_to_buffer(surf_input_buffer);
1020 surf_parse_lineno = 1;
1023 void surf_parse_close()
1025 if (surf_parsed_filename) {
1026 char *dir = nullptr;
1027 xbt_dynar_pop(surf_path, &dir);
1031 free(surf_parsed_filename);
1032 surf_parsed_filename = nullptr;
1034 if (surf_file_to_parse) {
1035 surf_parse__delete_buffer(surf_input_buffer);
1036 fclose(surf_file_to_parse);
1037 surf_file_to_parse = nullptr; //Must be reset for Bypass
1041 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1042 static int _surf_parse() {
1043 return surf_parse_lex();
1046 int_f_void_t surf_parse = _surf_parse;