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"
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 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
529 void ETag_surfxml_cluster(){
530 s_sg_platf_cluster_cbarg_t cluster;
531 memset(&cluster,0,sizeof(cluster));
532 cluster.properties = current_property_set;
534 cluster.id = A_surfxml_cluster_id;
535 cluster.prefix = A_surfxml_cluster_prefix;
536 cluster.suffix = A_surfxml_cluster_suffix;
537 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
538 cluster.speed = surf_parse_get_speed(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
539 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
540 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
541 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
542 if(strcmp(A_surfxml_cluster_bb___bw,""))
543 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
544 if(strcmp(A_surfxml_cluster_bb___lat,""))
545 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
546 if(strcmp(A_surfxml_cluster_limiter___link,""))
547 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
548 if(strcmp(A_surfxml_cluster_loopback___bw,""))
549 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
550 if(strcmp(A_surfxml_cluster_loopback___lat,""))
551 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
553 switch(AX_surfxml_cluster_topology){
554 case A_surfxml_cluster_topology_FLAT:
555 cluster.topology= SURF_CLUSTER_FLAT ;
557 case A_surfxml_cluster_topology_TORUS:
558 cluster.topology= SURF_CLUSTER_TORUS ;
560 case A_surfxml_cluster_topology_FAT___TREE:
561 cluster.topology = SURF_CLUSTER_FAT_TREE;
563 case A_surfxml_cluster_topology_DRAGONFLY:
564 cluster.topology= SURF_CLUSTER_DRAGONFLY ;
567 surf_parse_error("Invalid cluster topology for cluster %s",
571 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
572 cluster.router_id = A_surfxml_cluster_router___id;
574 switch (AX_surfxml_cluster_sharing___policy) {
575 case A_surfxml_cluster_sharing___policy_SHARED:
576 cluster.sharing_policy = SURF_LINK_SHARED;
578 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
579 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
581 case A_surfxml_cluster_sharing___policy_FATPIPE:
582 cluster.sharing_policy = SURF_LINK_FATPIPE;
585 surf_parse_error("Invalid cluster sharing policy for cluster %s",
589 switch (AX_surfxml_cluster_bb___sharing___policy) {
590 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
591 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
593 case A_surfxml_cluster_bb___sharing___policy_SHARED:
594 cluster.bb_sharing_policy = SURF_LINK_SHARED;
597 surf_parse_error("Invalid bb sharing policy in cluster %s",
602 sg_platf_new_cluster(&cluster);
604 current_property_set = nullptr;
607 void STag_surfxml_cluster(){
609 parse_after_config();
610 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
613 void STag_surfxml_cabinet(){
614 parse_after_config();
615 s_sg_platf_cabinet_cbarg_t cabinet;
616 memset(&cabinet,0,sizeof(cabinet));
617 cabinet.id = A_surfxml_cabinet_id;
618 cabinet.prefix = A_surfxml_cabinet_prefix;
619 cabinet.suffix = A_surfxml_cabinet_suffix;
620 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
621 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
622 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
623 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
625 sg_platf_new_cabinet(&cabinet);
628 void STag_surfxml_peer(){
629 parse_after_config();
630 s_sg_platf_peer_cbarg_t peer;
631 memset(&peer,0,sizeof(peer));
632 peer.id = A_surfxml_peer_id;
633 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
634 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
635 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
636 peer.coord = A_surfxml_peer_coordinates;
637 peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
638 peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
640 if (A_surfxml_peer_lat[0] != '\0')
641 XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
644 sg_platf_new_peer(&peer);
647 void STag_surfxml_link(){
649 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
652 void ETag_surfxml_link(){
653 s_sg_platf_link_cbarg_t link;
654 memset(&link,0,sizeof(link));
656 link.properties = current_property_set;
657 link.id = A_surfxml_link_id;
658 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id);
659 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
660 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id);
661 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
662 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
664 switch (A_surfxml_link_sharing___policy) {
665 case A_surfxml_link_sharing___policy_SHARED:
666 link.policy = SURF_LINK_SHARED;
668 case A_surfxml_link_sharing___policy_FATPIPE:
669 link.policy = SURF_LINK_FATPIPE;
671 case A_surfxml_link_sharing___policy_FULLDUPLEX:
672 link.policy = SURF_LINK_FULLDUPLEX;
675 surf_parse_error("Invalid sharing policy in link %s", link.id);
679 sg_platf_new_link(&link);
681 current_property_set = nullptr;
684 void STag_surfxml_link___ctn(){
686 simgrid::surf::Link *link;
687 char *link_name=nullptr;
688 switch (A_surfxml_link___ctn_direction) {
689 case AU_surfxml_link___ctn_direction:
690 case A_surfxml_link___ctn_direction_NONE:
691 link = Link::byName(A_surfxml_link___ctn_id);
693 case A_surfxml_link___ctn_direction_UP:
694 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
695 link = Link::byName(link_name);
697 case A_surfxml_link___ctn_direction_DOWN:
698 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
699 link = Link::byName(link_name);
702 xbt_free(link_name); // no-op if it's already nullptr
704 surf_parse_assert(link!=nullptr,"No such link: '%s'%s", A_surfxml_link___ctn_id,
705 A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_UP?" (upward)":
706 ( A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_DOWN?" (downward)":
708 parsed_link_list.push_back(link);
711 void ETag_surfxml_backbone(){
712 s_sg_platf_link_cbarg_t link;
713 memset(&link,0,sizeof(link));
715 link.properties = nullptr;
716 link.id = A_surfxml_backbone_id;
717 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id);
718 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id);
719 link.policy = SURF_LINK_SHARED;
721 sg_platf_new_link(&link);
722 routing_cluster_add_backbone(sg_link_by_name(A_surfxml_backbone_id));
725 void STag_surfxml_route(){
726 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_route_src),
727 "Route src='%s' does name a node.", A_surfxml_route_src);
728 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_route_dst),
729 "Route dst='%s' does name a node.", A_surfxml_route_dst);
732 void STag_surfxml_ASroute(){
733 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_src),
734 "ASroute src='%s' does name a node.", A_surfxml_route_src);
735 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_dst),
736 "ASroute dst='%s' does name a node.", A_surfxml_route_dst);
738 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___src),
739 "ASroute gw_src='%s' does name a node.", A_surfxml_ASroute_gw___src);
740 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___dst),
741 "ASroute gw_dst='%s' does name a node.", A_surfxml_ASroute_gw___dst);
744 void STag_surfxml_bypassRoute(){
745 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassRoute_src),
746 "bypassRoute src='%s' does name a node.", A_surfxml_bypassRoute_src);
747 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassRoute_dst),
748 "bypassRoute dst='%s' does name a node.", A_surfxml_bypassRoute_dst);
751 void STag_surfxml_bypassASroute(){
752 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_src),
753 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
754 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_dst),
755 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
756 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___src),
757 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
758 surf_parse_assert(sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
759 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
762 void ETag_surfxml_route(){
763 s_sg_platf_route_cbarg_t route;
764 memset(&route,0,sizeof(route));
766 route.src = sg_netcard_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
767 route.dst = sg_netcard_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
768 route.gw_src = nullptr;
769 route.gw_dst = nullptr;
770 route.link_list = new std::vector<Link*>();
771 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
773 for (auto link: parsed_link_list)
774 route.link_list->push_back(link);
775 parsed_link_list.clear();
777 sg_platf_new_route(&route);
778 delete route.link_list;
781 void ETag_surfxml_ASroute(){
782 s_sg_platf_route_cbarg_t ASroute;
783 memset(&ASroute,0,sizeof(ASroute));
785 ASroute.src = sg_netcard_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
786 ASroute.dst = sg_netcard_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
788 ASroute.gw_src = sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
789 ASroute.gw_dst = sg_netcard_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
791 ASroute.link_list = new std::vector<Link*>();
793 for (auto link: parsed_link_list)
794 ASroute.link_list->push_back(link);
795 parsed_link_list.clear();
797 switch (A_surfxml_ASroute_symmetrical) {
798 case AU_surfxml_ASroute_symmetrical:
799 case A_surfxml_ASroute_symmetrical_YES:
800 ASroute.symmetrical = true;
802 case A_surfxml_ASroute_symmetrical_NO:
803 ASroute.symmetrical = false;
807 sg_platf_new_route(&ASroute);
808 delete ASroute.link_list;
811 void ETag_surfxml_bypassRoute(){
812 s_sg_platf_route_cbarg_t route;
813 memset(&route,0,sizeof(route));
815 route.src = sg_netcard_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
816 route.dst = sg_netcard_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
817 route.gw_src = nullptr;
818 route.gw_dst = nullptr;
819 route.symmetrical = false;
820 route.link_list = new std::vector<Link*>();
822 for (auto link: parsed_link_list)
823 route.link_list->push_back(link);
824 parsed_link_list.clear();
826 sg_platf_new_bypassRoute(&route);
829 void ETag_surfxml_bypassASroute(){
830 s_sg_platf_route_cbarg_t ASroute;
831 memset(&ASroute,0,sizeof(ASroute));
833 ASroute.src = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_src);
834 ASroute.dst = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_dst);
835 ASroute.link_list = new std::vector<Link*>();
836 for (auto link: parsed_link_list)
837 ASroute.link_list->push_back(link);
838 parsed_link_list.clear();
840 ASroute.symmetrical = false;
842 ASroute.gw_src = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___src);
843 ASroute.gw_dst = sg_netcard_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
845 sg_platf_new_bypassRoute(&ASroute);
848 void ETag_surfxml_trace(){
849 s_sg_platf_trace_cbarg_t trace;
850 memset(&trace,0,sizeof(trace));
852 trace.id = A_surfxml_trace_id;
853 trace.file = A_surfxml_trace_file;
854 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
855 trace.pc_data = surfxml_pcdata;
857 sg_platf_new_trace(&trace);
860 void STag_surfxml_trace___connect(){
861 parse_after_config();
862 s_sg_platf_trace_connect_cbarg_t trace_connect;
863 memset(&trace_connect,0,sizeof(trace_connect));
865 trace_connect.element = A_surfxml_trace___connect_element;
866 trace_connect.trace = A_surfxml_trace___connect_trace;
868 switch (A_surfxml_trace___connect_kind) {
869 case AU_surfxml_trace___connect_kind:
870 case A_surfxml_trace___connect_kind_SPEED:
871 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
873 case A_surfxml_trace___connect_kind_BANDWIDTH:
874 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
876 case A_surfxml_trace___connect_kind_HOST___AVAIL:
877 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
879 case A_surfxml_trace___connect_kind_LATENCY:
880 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
882 case A_surfxml_trace___connect_kind_LINK___AVAIL:
883 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
886 sg_platf_trace_connect(&trace_connect);
889 void STag_surfxml_AS(){
890 parse_after_config();
892 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
894 sg_platf_new_AS_begin(&AS);
896 void ETag_surfxml_AS(){
897 sg_platf_new_AS_seal();
900 void STag_surfxml_config(){
902 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
903 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
904 if (_sg_cfg_init_status == 2) {
905 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
908 void ETag_surfxml_config(){
909 xbt_dict_cursor_t cursor = nullptr;
913 xbt_dict_foreach(current_property_set, cursor, key, elem) {
914 cfg = bprintf("%s:%s",key,elem);
915 if(xbt_cfg_is_default_value(key))
916 xbt_cfg_set_parse(cfg);
918 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
921 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
923 xbt_dict_free(¤t_property_set);
924 current_property_set = nullptr;
930 void STag_surfxml_process(){
933 argv = xbt_new(char *, 1);
934 argv[0] = xbt_strdup(A_surfxml_process_function);
935 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
938 void ETag_surfxml_process(){
939 s_sg_platf_process_cbarg_t process;
940 memset(&process,0,sizeof(process));
943 process.argv = (const char **)argv;
944 process.properties = current_property_set;
945 process.host = A_surfxml_process_host;
946 process.function = A_surfxml_process_function;
947 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
948 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
950 switch (A_surfxml_process_on___failure) {
951 case AU_surfxml_process_on___failure:
952 case A_surfxml_process_on___failure_DIE:
953 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
955 case A_surfxml_process_on___failure_RESTART:
956 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
960 sg_platf_new_process(&process);
962 for (int i = 0; i != argc; ++i)
967 current_property_set = nullptr;
970 void STag_surfxml_argument(){
972 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
973 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
976 void STag_surfxml_model___prop(){
977 if (!current_model_property_set)
978 current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
980 xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
983 void ETag_surfxml_prop(){/* Nothing to do */}
984 void STag_surfxml_random(){/* Nothing to do */}
985 void ETag_surfxml_random(){/* Nothing to do */}
986 void ETag_surfxml_trace___connect(){/* Nothing to do */}
987 void STag_surfxml_trace(){parse_after_config();}
988 void ETag_surfxml_router(){/*Nothing to do*/}
989 void ETag_surfxml_host___link(){/* Nothing to do */}
990 void ETag_surfxml_cabinet(){/* Nothing to do */}
991 void ETag_surfxml_peer(){/* Nothing to do */}
992 void STag_surfxml_backbone(){/* Nothing to do */}
993 void ETag_surfxml_link___ctn(){/* Nothing to do */}
994 void ETag_surfxml_argument(){/* Nothing to do */}
995 void ETag_surfxml_model___prop(){/* Nothing to do */}
997 /* Open and Close parse file */
998 void surf_parse_open(const char *file)
1000 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1002 if (!surf_input_buffer_stack)
1003 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), nullptr);
1004 if (!surf_file_to_parse_stack)
1005 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), nullptr);
1007 if (!surf_parsed_filename_stack)
1008 surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1010 surf_parsed_filename = xbt_strdup(file);
1011 char *dir = xbt_dirname(file);
1012 xbt_dynar_push(surf_path, &dir);
1014 surf_file_to_parse = surf_fopen(file, "r");
1015 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1016 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1017 surf_parse__switch_to_buffer(surf_input_buffer);
1018 surf_parse_lineno = 1;
1021 void surf_parse_close()
1023 xbt_dynar_free(&surf_input_buffer_stack);
1024 xbt_dynar_free(&surf_file_to_parse_stack);
1025 xbt_dynar_free(&surf_parsed_filename_stack);
1026 if (surf_parsed_filename) {
1027 char *dir = nullptr;
1028 xbt_dynar_pop(surf_path, &dir);
1032 free(surf_parsed_filename);
1033 surf_parsed_filename = nullptr;
1035 if (surf_file_to_parse) {
1036 surf_parse__delete_buffer(surf_input_buffer);
1037 fclose(surf_file_to_parse);
1038 surf_file_to_parse = nullptr; //Must be reset for Bypass
1042 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1043 static int _surf_parse() {
1044 return surf_parse_lex();
1047 int_f_void_t surf_parse = _surf_parse;