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/NetCard.hpp"
15 #include "src/surf/network_interface.hpp"
16 #include "src/surf/surf_private.h"
23 #include "src/surf/xml/platf_private.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
27 int ETag_surfxml_include_state();
29 #include "simgrid_dtd.c"
31 char* surf_parsed_filename = nullptr; // to locate parse error messages
33 std::vector<simgrid::surf::Link *> parsed_link_list; /* temporary store of current list link of a route */
37 void surf_parse_assert(bool cond, const char *fmt, ...) {
41 int lineno = surf_parse_lineno;
42 char *msg = bvprintf(fmt,va);
45 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
47 xbt_die("Exiting now");
50 void surf_parse_error(const char *fmt, ...) {
53 int lineno = surf_parse_lineno;
54 char *msg = bvprintf(fmt,va);
57 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
59 xbt_die("Exiting now");
61 void surf_parse_warn(const char *fmt, ...) {
64 char *msg = bvprintf(fmt,va);
66 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
70 double surf_parse_get_double(const char *string) {
72 int ret = sscanf(string, "%lg", &res);
74 surf_parse_error("%s is not a double", string);
78 int surf_parse_get_int(const char *string) {
80 int ret = sscanf(string, "%d", &res);
82 surf_parse_error("%s is not an integer", string);
86 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
87 static std::vector<int>* explodesRadical(const char* radicals)
89 std::vector<int>* exploded = new std::vector<int>();
94 xbt_dynar_t radical_elements = xbt_str_split(radicals, ",");
95 xbt_dynar_foreach (radical_elements, iter, groups) {
97 xbt_dynar_t radical_ends = xbt_str_split(groups, "-");
98 int start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char*));
101 switch (xbt_dynar_length(radical_ends)) {
106 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char*));
109 surf_parse_error("Malformed radical: %s", groups);
113 for (int i = start; i <= end; i++)
114 exploded->push_back(i);
116 xbt_dynar_free(&radical_ends);
118 xbt_dynar_free(&radical_elements);
128 /* Note: field `unit' for the last element of parameter `units' should be nullptr. */
129 static double surf_parse_get_value_with_unit(const char *string, const struct unit_scale *units,
130 const char *entity_kind, const char *name, const char *error_msg, const char *default_unit)
135 double res = strtod(string, &ptr);
137 surf_parse_error("value out of range: %s", string);
139 surf_parse_error("cannot parse number: %s", string);
140 if (ptr[0] == '\0') {
142 return res; // Ok, 0 can be unit-less
144 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
145 ptr = (char*)default_unit;
147 for (i = 0; units[i].unit != nullptr && strcmp(ptr, units[i].unit) != 0; i++);
149 if (units[i].unit != nullptr)
150 res *= units[i].scale;
152 surf_parse_error("unknown unit: %s", ptr);
156 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
158 const struct unit_scale units[] = {
159 { "w", 7 * 24 * 60 * 60 },
160 { "d", 24 * 60 * 60 },
170 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
171 "Append 's' to your time to get seconds", "s");
174 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
176 const struct unit_scale units[] = {
177 { "EiB", pow(1024, 6) },
178 { "PiB", pow(1024, 5) },
179 { "TiB", pow(1024, 4) },
180 { "GiB", pow(1024, 3) },
181 { "MiB", pow(1024, 2) },
190 { "Eib", 0.125 * pow(1024, 6) },
191 { "Pib", 0.125 * pow(1024, 5) },
192 { "Tib", 0.125 * pow(1024, 4) },
193 { "Gib", 0.125 * pow(1024, 3) },
194 { "Mib", 0.125 * pow(1024, 2) },
195 { "Kib", 0.125 * 1024 },
196 { "Eb", 0.125 * 1e18 },
197 { "Pb", 0.125 * 1e15 },
198 { "Tb", 0.125 * 1e12 },
199 { "Gb", 0.125 * 1e9 },
200 { "Mb", 0.125 * 1e6 },
201 { "kb", 0.125 * 1e3 },
205 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
206 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
209 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
211 const struct unit_scale units[] = {
212 { "EiBps", pow(1024, 6) },
213 { "PiBps", pow(1024, 5) },
214 { "TiBps", pow(1024, 4) },
215 { "GiBps", pow(1024, 3) },
216 { "MiBps", pow(1024, 2) },
225 { "Eibps", 0.125 * pow(1024, 6) },
226 { "Pibps", 0.125 * pow(1024, 5) },
227 { "Tibps", 0.125 * pow(1024, 4) },
228 { "Gibps", 0.125 * pow(1024, 3) },
229 { "Mibps", 0.125 * pow(1024, 2) },
230 { "Kibps", 0.125 * 1024 },
231 { "Tbps", 0.125 * 1e12 },
232 { "Gbps", 0.125 * 1e9 },
233 { "Mbps", 0.125 * 1e6 },
234 { "kbps", 0.125 * 1e3 },
238 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
239 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
242 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
244 const struct unit_scale units[] = {
245 { "yottaflops", 1e24 },
247 { "zettaflops", 1e21 },
249 { "exaflops", 1e18 },
251 { "petaflops", 1e15 },
253 { "teraflops", 1e12 },
255 { "gigaflops", 1e9 },
257 { "megaflops", 1e6 },
259 { "kiloflops", 1e3 },
265 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
266 "Append 'f' or 'flops' to your speed to get flop per second", "f");
270 * All the callback lists that can be overridden anywhere.
271 * (this list should probably be reduced to the bare minimum to allow the models to work)
274 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
276 /* The default current property receiver. Setup in the corresponding opening callbacks. */
277 xbt_dict_t current_property_set = nullptr;
278 xbt_dict_t current_model_property_set = nullptr;
279 int AS_TAG = 0; // Whether we just opened an AS tag (to see what to do with the properties)
281 /* dictionary of random generator data */
282 xbt_dict_t random_data_list = nullptr;
284 YY_BUFFER_STATE surf_input_buffer;
285 FILE *surf_file_to_parse = nullptr;
288 * Stuff relative to storage
290 void STag_surfxml_storage()
293 XBT_DEBUG("STag_surfxml_storage");
294 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
296 void ETag_surfxml_storage()
298 s_sg_platf_storage_cbarg_t storage;
299 memset(&storage,0,sizeof(storage));
301 storage.id = A_surfxml_storage_id;
302 storage.type_id = A_surfxml_storage_typeId;
303 storage.content = A_surfxml_storage_content;
304 storage.content_type = A_surfxml_storage_content___type;
305 storage.properties = current_property_set;
306 storage.attach = A_surfxml_storage_attach;
307 sg_platf_new_storage(&storage);
308 current_property_set = nullptr;
310 void STag_surfxml_storage___type()
313 XBT_DEBUG("STag_surfxml_storage___type");
314 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
315 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
317 void ETag_surfxml_storage___type()
319 s_sg_platf_storage_type_cbarg_t storage_type;
320 memset(&storage_type,0,sizeof(storage_type));
322 storage_type.content = A_surfxml_storage___type_content;
323 storage_type.content_type = A_surfxml_storage___type_content___type;
324 storage_type.id = A_surfxml_storage___type_id;
325 storage_type.model = A_surfxml_storage___type_model;
326 storage_type.properties = current_property_set;
327 storage_type.model_properties = current_model_property_set;
328 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size,
329 "size of storage type", storage_type.id);
330 sg_platf_new_storage_type(&storage_type);
331 current_property_set = nullptr;
332 current_model_property_set = nullptr;
334 void STag_surfxml_mount()
336 XBT_DEBUG("STag_surfxml_mount");
338 void ETag_surfxml_mount()
340 s_sg_platf_mount_cbarg_t mount;
341 memset(&mount,0,sizeof(mount));
343 mount.name = A_surfxml_mount_name;
344 mount.storageId = A_surfxml_mount_storageId;
345 sg_platf_new_mount(&mount);
349 * Stuff relative to the <include> tag
351 static xbt_dynar_t surf_input_buffer_stack = nullptr;
352 static xbt_dynar_t surf_file_to_parse_stack = nullptr;
353 static xbt_dynar_t surf_parsed_filename_stack = nullptr;
355 void STag_surfxml_include()
357 parse_after_config();
358 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
359 xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
360 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
362 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
364 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
365 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
367 xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
368 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
369 surf_parse_push_buffer_state(surf_input_buffer);
374 void ETag_surfxml_include() {
375 /* Nothing to do when done with reading the include tag.
376 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
379 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
381 * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
382 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
383 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
384 * error message in that case.
386 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
387 * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
388 * on a line. One day maybe, if the maya allow it.
390 int ETag_surfxml_include_state()
393 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
395 if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
398 // Yeah, we were in an <include> Restore state and proceed.
399 fclose(surf_file_to_parse);
400 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
401 surf_parse_pop_buffer_state();
402 xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
404 // Restore the filename for error messages
405 free(surf_parsed_filename);
406 xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
411 /* Stag and Etag parse functions */
413 void STag_surfxml_platform() {
414 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
416 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
417 "You're using an ancient XML file.\n"
418 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
419 "instead of MBytes, MFlops and seconds.\n"
421 "Use simgrid_update_xml to update your file automatically. "
422 "This program is installed automatically with SimGrid, or "
423 "available in the tools/ directory of the source archive.\n"
425 "Please check also out the SURF section of the ChangeLog for "
426 "the 3.1 version for more information. \n"
428 "Last, do not forget to also update your values for "
429 "the calls to MSG_task_create (if any).");
430 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
431 "You're using an old XML file.\n"
432 "Use simgrid_update_xml to update your file automatically. "
433 "This program is installed automatically with SimGrid, or "
434 "available in the tools/ directory of the source archive.");
435 xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
436 "Changes introduced in SimGrid 3.13:\n"
437 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
438 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
439 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
440 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
442 "Use simgrid_update_xml to update your file automatically. "
443 "This program is installed automatically with SimGrid, or "
444 "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
448 void ETag_surfxml_platform(){
452 void STag_surfxml_host(){
454 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
457 void STag_surfxml_prop()
459 if (AS_TAG) { // We need to retrieve the most recently opened AS
460 XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
461 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
463 netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
466 if (!current_property_set)
467 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
468 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
469 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
470 current_property_set);
474 void ETag_surfxml_host() {
475 s_sg_platf_host_cbarg_t host;
476 memset(&host,0,sizeof(host));
480 host.properties = current_property_set;
482 host.id = A_surfxml_host_id;
484 buf = A_surfxml_host_speed;
485 XBT_DEBUG("Buffer: %s", buf);
486 if (strchr(buf, ',') == nullptr){
487 double speed = surf_parse_get_speed(A_surfxml_host_speed,"speed of host", host.id);
488 host.speed_per_pstate.push_back(speed);
491 xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
494 xbt_dynar_foreach(pstate_list, i, speed_str) {
495 xbt_str_trim(speed_str, nullptr);
496 double speed = surf_parse_get_speed(speed_str,"speed of host", host.id);
497 host.speed_per_pstate.push_back(speed);
498 XBT_DEBUG("Speed value: %f", speed);
500 xbt_dynar_free(&pstate_list);
503 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
504 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
505 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
506 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
507 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
508 host.coord = A_surfxml_host_coordinates;
510 sg_platf_new_host(&host);
511 current_property_set = nullptr;
514 void STag_surfxml_host___link(){
515 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
516 s_sg_platf_host_link_cbarg_t host_link;
517 memset(&host_link,0,sizeof(host_link));
519 host_link.id = A_surfxml_host___link_id;
520 host_link.link_up = A_surfxml_host___link_up;
521 host_link.link_down = A_surfxml_host___link_down;
522 sg_platf_new_hostlink(&host_link);
525 void STag_surfxml_router(){
526 s_sg_platf_router_cbarg_t router;
527 memset(&router, 0, sizeof(router));
529 router.id = A_surfxml_router_id;
530 router.coord = A_surfxml_router_coordinates;
532 sg_platf_new_router(&router);
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;
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.speed = surf_parse_get_speed(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",
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",
608 sg_platf_new_cluster(&cluster);
610 current_property_set = nullptr;
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 s_sg_platf_link_cbarg_t link;
660 memset(&link,0,sizeof(link));
662 link.properties = current_property_set;
663 link.id = A_surfxml_link_id;
664 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id);
665 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
666 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id);
667 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
668 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
670 switch (A_surfxml_link_sharing___policy) {
671 case A_surfxml_link_sharing___policy_SHARED:
672 link.policy = SURF_LINK_SHARED;
674 case A_surfxml_link_sharing___policy_FATPIPE:
675 link.policy = SURF_LINK_FATPIPE;
677 case A_surfxml_link_sharing___policy_FULLDUPLEX:
678 link.policy = SURF_LINK_FULLDUPLEX;
681 surf_parse_error("Invalid sharing policy in link %s", link.id);
685 sg_platf_new_link(&link);
687 current_property_set = nullptr;
690 void STag_surfxml_link___ctn(){
692 simgrid::surf::Link *link;
693 char *link_name=nullptr;
694 switch (A_surfxml_link___ctn_direction) {
695 case AU_surfxml_link___ctn_direction:
696 case A_surfxml_link___ctn_direction_NONE:
697 link = Link::byName(A_surfxml_link___ctn_id);
699 case A_surfxml_link___ctn_direction_UP:
700 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
701 link = Link::byName(link_name);
703 case A_surfxml_link___ctn_direction_DOWN:
704 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
705 link = Link::byName(link_name);
708 xbt_free(link_name); // no-op if it's already nullptr
710 surf_parse_assert(link!=nullptr,"No such link: '%s'%s", A_surfxml_link___ctn_id,
711 A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_UP?" (upward)":
712 ( A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_DOWN?" (downward)":
714 parsed_link_list.push_back(link);
717 void ETag_surfxml_backbone(){
718 s_sg_platf_link_cbarg_t link;
719 memset(&link,0,sizeof(link));
721 link.properties = nullptr;
722 link.id = A_surfxml_backbone_id;
723 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id);
724 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id);
725 link.policy = SURF_LINK_SHARED;
727 sg_platf_new_link(&link);
728 routing_cluster_add_backbone(sg_link_by_name(A_surfxml_backbone_id));
731 void STag_surfxml_route(){
732 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_route_src),
733 "Route src='%s' does name a node.", A_surfxml_route_src);
734 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_route_dst),
735 "Route dst='%s' does name a node.", A_surfxml_route_dst);
738 void STag_surfxml_ASroute(){
739 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_src),
740 "ASroute src='%s' does name a node.", A_surfxml_route_src);
741 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_dst),
742 "ASroute dst='%s' does name a node.", A_surfxml_route_dst);
744 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___src),
745 "ASroute gw_src='%s' does name a node.", A_surfxml_ASroute_gw___src);
746 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___dst),
747 "ASroute gw_dst='%s' does name a node.", A_surfxml_ASroute_gw___dst);
750 void STag_surfxml_bypassRoute(){
751 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassRoute_src),
752 "bypassRoute src='%s' does name a node.", A_surfxml_bypassRoute_src);
753 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassRoute_dst),
754 "bypassRoute dst='%s' does name a node.", A_surfxml_bypassRoute_dst);
757 void STag_surfxml_bypassASroute(){
758 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_src),
759 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
760 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_dst),
761 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
762 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___src),
763 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
764 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
765 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
768 void ETag_surfxml_route(){
769 s_sg_platf_route_cbarg_t route;
770 memset(&route,0,sizeof(route));
772 route.src = sg_netcard_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
773 route.dst = sg_netcard_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
774 route.gw_src = nullptr;
775 route.gw_dst = nullptr;
776 route.link_list = new std::vector<Link*>();
777 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
779 for (auto link: parsed_link_list)
780 route.link_list->push_back(link);
781 parsed_link_list.clear();
783 sg_platf_new_route(&route);
784 delete route.link_list;
787 void ETag_surfxml_ASroute(){
788 s_sg_platf_route_cbarg_t ASroute;
789 memset(&ASroute,0,sizeof(ASroute));
791 ASroute.src = sg_netcard_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
792 ASroute.dst = sg_netcard_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
794 ASroute.gw_src = sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
795 ASroute.gw_dst = sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
797 ASroute.link_list = new std::vector<Link*>();
799 for (auto link: parsed_link_list)
800 ASroute.link_list->push_back(link);
801 parsed_link_list.clear();
803 switch (A_surfxml_ASroute_symmetrical) {
804 case AU_surfxml_ASroute_symmetrical:
805 case A_surfxml_ASroute_symmetrical_YES:
806 ASroute.symmetrical = true;
808 case A_surfxml_ASroute_symmetrical_NO:
809 ASroute.symmetrical = false;
813 sg_platf_new_route(&ASroute);
814 delete ASroute.link_list;
817 void ETag_surfxml_bypassRoute(){
818 s_sg_platf_route_cbarg_t route;
819 memset(&route,0,sizeof(route));
821 route.src = sg_netcard_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
822 route.dst = sg_netcard_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
823 route.gw_src = nullptr;
824 route.gw_dst = nullptr;
825 route.symmetrical = false;
826 route.link_list = new std::vector<Link*>();
828 for (auto link: parsed_link_list)
829 route.link_list->push_back(link);
830 parsed_link_list.clear();
832 sg_platf_new_bypassRoute(&route);
835 void ETag_surfxml_bypassASroute(){
836 s_sg_platf_route_cbarg_t ASroute;
837 memset(&ASroute,0,sizeof(ASroute));
839 ASroute.src = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_src);
840 ASroute.dst = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_dst);
841 ASroute.link_list = new std::vector<Link*>();
842 for (auto link: parsed_link_list)
843 ASroute.link_list->push_back(link);
844 parsed_link_list.clear();
846 ASroute.symmetrical = false;
848 ASroute.gw_src = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___src);
849 ASroute.gw_dst = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
851 sg_platf_new_bypassRoute(&ASroute);
854 void ETag_surfxml_trace(){
855 s_sg_platf_trace_cbarg_t trace;
856 memset(&trace,0,sizeof(trace));
858 trace.id = A_surfxml_trace_id;
859 trace.file = A_surfxml_trace_file;
860 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
861 trace.pc_data = surfxml_pcdata;
863 sg_platf_new_trace(&trace);
866 void STag_surfxml_trace___connect(){
867 parse_after_config();
868 s_sg_platf_trace_connect_cbarg_t trace_connect;
869 memset(&trace_connect,0,sizeof(trace_connect));
871 trace_connect.element = A_surfxml_trace___connect_element;
872 trace_connect.trace = A_surfxml_trace___connect_trace;
874 switch (A_surfxml_trace___connect_kind) {
875 case AU_surfxml_trace___connect_kind:
876 case A_surfxml_trace___connect_kind_SPEED:
877 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
879 case A_surfxml_trace___connect_kind_BANDWIDTH:
880 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
882 case A_surfxml_trace___connect_kind_HOST___AVAIL:
883 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
885 case A_surfxml_trace___connect_kind_LATENCY:
886 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
888 case A_surfxml_trace___connect_kind_LINK___AVAIL:
889 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
892 sg_platf_trace_connect(&trace_connect);
895 void STag_surfxml_AS(){
896 parse_after_config();
898 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
900 sg_platf_new_AS_begin(&AS);
902 void ETag_surfxml_AS(){
903 sg_platf_new_AS_seal();
906 void STag_surfxml_config(){
908 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
909 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
910 if (_sg_cfg_init_status == 2) {
911 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
914 void ETag_surfxml_config(){
915 xbt_dict_cursor_t cursor = nullptr;
919 xbt_dict_foreach(current_property_set, cursor, key, elem) {
920 cfg = bprintf("%s:%s",key,elem);
921 if(xbt_cfg_is_default_value(key))
922 xbt_cfg_set_parse(cfg);
924 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
927 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
929 xbt_dict_free(¤t_property_set);
930 current_property_set = nullptr;
936 void STag_surfxml_process(){
939 argv = xbt_new(char *, 1);
940 argv[0] = xbt_strdup(A_surfxml_process_function);
941 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
944 void ETag_surfxml_process(){
945 s_sg_platf_process_cbarg_t process;
946 memset(&process,0,sizeof(process));
949 process.argv = (const char **)argv;
950 process.properties = current_property_set;
951 process.host = A_surfxml_process_host;
952 process.function = A_surfxml_process_function;
953 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
954 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
956 switch (A_surfxml_process_on___failure) {
957 case AU_surfxml_process_on___failure:
958 case A_surfxml_process_on___failure_DIE:
959 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
961 case A_surfxml_process_on___failure_RESTART:
962 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
966 sg_platf_new_process(&process);
968 for (int i = 0; i != argc; ++i)
973 current_property_set = nullptr;
976 void STag_surfxml_argument(){
978 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
979 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
982 void STag_surfxml_model___prop(){
983 if (!current_model_property_set)
984 current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
986 xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
989 void ETag_surfxml_prop(){/* Nothing to do */}
990 void STag_surfxml_random(){/* Nothing to do */}
991 void ETag_surfxml_random(){/* Nothing to do */}
992 void ETag_surfxml_trace___connect(){/* Nothing to do */}
993 void STag_surfxml_trace(){parse_after_config();}
994 void ETag_surfxml_router(){/*Nothing to do*/}
995 void ETag_surfxml_host___link(){/* Nothing to do */}
996 void ETag_surfxml_cabinet(){/* Nothing to do */}
997 void ETag_surfxml_peer(){/* Nothing to do */}
998 void STag_surfxml_backbone(){/* Nothing to do */}
999 void ETag_surfxml_link___ctn(){/* Nothing to do */}
1000 void ETag_surfxml_argument(){/* Nothing to do */}
1001 void ETag_surfxml_model___prop(){/* Nothing to do */}
1003 /* Open and Close parse file */
1004 void surf_parse_open(const char *file)
1006 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1008 if (!surf_input_buffer_stack)
1009 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), nullptr);
1010 if (!surf_file_to_parse_stack)
1011 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), nullptr);
1013 if (!surf_parsed_filename_stack)
1014 surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1016 surf_parsed_filename = xbt_strdup(file);
1017 char *dir = xbt_dirname(file);
1018 xbt_dynar_push(surf_path, &dir);
1020 surf_file_to_parse = surf_fopen(file, "r");
1021 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1022 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1023 surf_parse__switch_to_buffer(surf_input_buffer);
1024 surf_parse_lineno = 1;
1027 void surf_parse_close()
1029 xbt_dynar_free(&surf_input_buffer_stack);
1030 xbt_dynar_free(&surf_file_to_parse_stack);
1031 xbt_dynar_free(&surf_parsed_filename_stack);
1032 if (surf_parsed_filename) {
1033 char *dir = nullptr;
1034 xbt_dynar_pop(surf_path, &dir);
1038 free(surf_parsed_filename);
1039 surf_parsed_filename = nullptr;
1041 if (surf_file_to_parse) {
1042 surf_parse__delete_buffer(surf_input_buffer);
1043 fclose(surf_file_to_parse);
1044 surf_file_to_parse = nullptr; //Must be reset for Bypass
1048 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1049 static int _surf_parse() {
1050 return surf_parse_lex();
1053 int_f_void_t surf_parse = _surf_parse;