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"
24 #include "src/surf/xml/platf_private.hpp"
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
30 int ETag_surfxml_include_state();
32 #include "simgrid_dtd.c"
34 char* surf_parsed_filename = nullptr; // to locate parse error messages
36 std::vector<simgrid::surf::LinkImpl*> parsed_link_list; /* temporary store of current list link of a route */
41 void surf_parse_assert(bool cond, const char *fmt, ...) {
45 int lineno = surf_parse_lineno;
46 char *msg = bvprintf(fmt,va);
49 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
51 xbt_die("Exiting now");
54 void surf_parse_error(const char *fmt, ...) {
57 int lineno = surf_parse_lineno;
58 char *msg = bvprintf(fmt,va);
61 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
63 xbt_die("Exiting now");
65 void surf_parse_warn(const char *fmt, ...) {
68 char *msg = bvprintf(fmt,va);
70 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
74 double surf_parse_get_double(const char *string) {
76 int ret = sscanf(string, "%lg", &res);
78 surf_parse_error("%s is not a double", string);
82 int surf_parse_get_int(const char *string) {
84 int ret = sscanf(string, "%d", &res);
86 surf_parse_error("%s is not an integer", string);
90 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
91 static std::vector<int>* explodesRadical(const char* radicals)
93 std::vector<int>* exploded = new std::vector<int>();
98 xbt_dynar_t radical_elements = xbt_str_split(radicals, ",");
99 xbt_dynar_foreach (radical_elements, iter, groups) {
101 xbt_dynar_t radical_ends = xbt_str_split(groups, "-");
102 int start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char*));
105 switch (xbt_dynar_length(radical_ends)) {
110 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char*));
113 surf_parse_error("Malformed radical: %s", groups);
117 for (int i = start; i <= end; i++)
118 exploded->push_back(i);
120 xbt_dynar_free(&radical_ends);
122 xbt_dynar_free(&radical_elements);
132 /* Note: field `unit' for the last element of parameter `units' should be nullptr. */
133 static double surf_parse_get_value_with_unit(const char *string, const struct unit_scale *units,
134 const char *entity_kind, const char *name, const char *error_msg, const char *default_unit)
139 double res = strtod(string, &ptr);
141 surf_parse_error("value out of range: %s", string);
143 surf_parse_error("cannot parse number: %s", string);
144 if (ptr[0] == '\0') {
146 return res; // Ok, 0 can be unit-less
148 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
149 ptr = (char*)default_unit;
151 for (i = 0; units[i].unit != nullptr && strcmp(ptr, units[i].unit) != 0; i++);
153 if (units[i].unit != nullptr)
154 res *= units[i].scale;
156 surf_parse_error("unknown unit: %s", ptr);
160 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
162 const struct unit_scale units[] = {
163 { "w", 7 * 24 * 60 * 60 },
164 { "d", 24 * 60 * 60 },
174 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
175 "Append 's' to your time to get seconds", "s");
178 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
180 const struct unit_scale units[] = {
181 { "EiB", pow(1024, 6) },
182 { "PiB", pow(1024, 5) },
183 { "TiB", pow(1024, 4) },
184 { "GiB", pow(1024, 3) },
185 { "MiB", pow(1024, 2) },
194 { "Eib", 0.125 * pow(1024, 6) },
195 { "Pib", 0.125 * pow(1024, 5) },
196 { "Tib", 0.125 * pow(1024, 4) },
197 { "Gib", 0.125 * pow(1024, 3) },
198 { "Mib", 0.125 * pow(1024, 2) },
199 { "Kib", 0.125 * 1024 },
200 { "Eb", 0.125 * 1e18 },
201 { "Pb", 0.125 * 1e15 },
202 { "Tb", 0.125 * 1e12 },
203 { "Gb", 0.125 * 1e9 },
204 { "Mb", 0.125 * 1e6 },
205 { "kb", 0.125 * 1e3 },
209 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
210 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
213 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
215 const struct unit_scale units[] = {
216 { "EiBps", pow(1024, 6) },
217 { "PiBps", pow(1024, 5) },
218 { "TiBps", pow(1024, 4) },
219 { "GiBps", pow(1024, 3) },
220 { "MiBps", pow(1024, 2) },
229 { "Eibps", 0.125 * pow(1024, 6) },
230 { "Pibps", 0.125 * pow(1024, 5) },
231 { "Tibps", 0.125 * pow(1024, 4) },
232 { "Gibps", 0.125 * pow(1024, 3) },
233 { "Mibps", 0.125 * pow(1024, 2) },
234 { "Kibps", 0.125 * 1024 },
235 { "Tbps", 0.125 * 1e12 },
236 { "Gbps", 0.125 * 1e9 },
237 { "Mbps", 0.125 * 1e6 },
238 { "kbps", 0.125 * 1e3 },
242 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
243 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
246 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
248 const struct unit_scale units[] = {
249 { "yottaflops", 1e24 },
251 { "zettaflops", 1e21 },
253 { "exaflops", 1e18 },
255 { "petaflops", 1e15 },
257 { "teraflops", 1e12 },
259 { "gigaflops", 1e9 },
261 { "megaflops", 1e6 },
263 { "kiloflops", 1e3 },
269 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
270 "Append 'f' or 'flops' to your speed to get flop per second", "f");
274 * All the callback lists that can be overridden anywhere.
275 * (this list should probably be reduced to the bare minimum to allow the models to work)
278 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
280 /* The default current property receiver. Setup in the corresponding opening callbacks. */
281 xbt_dict_t current_property_set = nullptr;
282 xbt_dict_t current_model_property_set = nullptr;
283 int AS_TAG = 0; // Whether we just opened an AS tag (to see what to do with the properties)
285 /* dictionary of random generator data */
286 xbt_dict_t random_data_list = nullptr;
288 YY_BUFFER_STATE surf_input_buffer;
289 FILE *surf_file_to_parse = nullptr;
292 * Stuff relative to storage
294 void STag_surfxml_storage()
297 XBT_DEBUG("STag_surfxml_storage");
298 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
300 void ETag_surfxml_storage()
302 s_sg_platf_storage_cbarg_t storage;
303 memset(&storage,0,sizeof(storage));
305 storage.properties = current_property_set;
306 current_property_set = nullptr;
308 storage.id = A_surfxml_storage_id;
309 storage.type_id = A_surfxml_storage_typeId;
310 storage.content = A_surfxml_storage_content;
311 storage.content_type = A_surfxml_storage_content___type;
312 storage.attach = A_surfxml_storage_attach;
313 sg_platf_new_storage(&storage);
315 void STag_surfxml_storage___type()
318 XBT_DEBUG("STag_surfxml_storage___type");
319 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
320 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
322 void ETag_surfxml_storage___type()
324 s_sg_platf_storage_type_cbarg_t storage_type;
325 memset(&storage_type,0,sizeof(storage_type));
327 storage_type.properties = current_property_set;
328 current_property_set = nullptr;
330 storage_type.model_properties = current_model_property_set;
331 current_model_property_set = nullptr;
333 storage_type.content = A_surfxml_storage___type_content;
334 storage_type.content_type = A_surfxml_storage___type_content___type;
335 storage_type.id = A_surfxml_storage___type_id;
336 storage_type.model = A_surfxml_storage___type_model;
337 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size,
338 "size of storage type", storage_type.id);
339 sg_platf_new_storage_type(&storage_type);
341 void STag_surfxml_mount()
343 XBT_DEBUG("STag_surfxml_mount");
345 void ETag_surfxml_mount()
347 s_sg_platf_mount_cbarg_t mount;
348 memset(&mount,0,sizeof(mount));
350 mount.name = A_surfxml_mount_name;
351 mount.storageId = A_surfxml_mount_storageId;
352 sg_platf_new_mount(&mount);
356 * Stuff relative to the <include> tag
358 static xbt_dynar_t surf_input_buffer_stack = nullptr;
359 static xbt_dynar_t surf_file_to_parse_stack = nullptr;
360 static xbt_dynar_t surf_parsed_filename_stack = nullptr;
362 void STag_surfxml_include()
364 parse_after_config();
365 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
366 xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
367 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
369 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
371 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
372 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
374 xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
375 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
376 surf_parse_push_buffer_state(surf_input_buffer);
381 void ETag_surfxml_include() {
382 /* Nothing to do when done with reading the include tag.
383 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
386 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
388 * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
389 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
390 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
391 * error message in that case.
393 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
394 * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
395 * on a line. One day maybe, if the maya allow it.
397 int ETag_surfxml_include_state()
400 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
402 if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
405 // Yeah, we were in an <include> Restore state and proceed.
406 fclose(surf_file_to_parse);
407 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
408 surf_parse_pop_buffer_state();
409 xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
411 // Restore the filename for error messages
412 free(surf_parsed_filename);
413 xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
418 /* Stag and Etag parse functions */
420 void STag_surfxml_platform() {
421 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
423 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
424 "You're using an ancient XML file.\n"
425 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
426 "instead of MBytes, MFlops and seconds.\n"
428 "Use simgrid_update_xml to update your file automatically. "
429 "This program is installed automatically with SimGrid, or "
430 "available in the tools/ directory of the source archive.\n"
432 "Please check also out the SURF section of the ChangeLog for "
433 "the 3.1 version for more information. \n"
435 "Last, do not forget to also update your values for "
436 "the calls to MSG_task_create (if any).");
437 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
438 "You're using an old XML file.\n"
439 "Use simgrid_update_xml to update your file automatically. "
440 "This program is installed automatically with SimGrid, or "
441 "available in the tools/ directory of the source archive.");
442 xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
443 "Changes introduced in SimGrid 3.13:\n"
444 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
445 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
446 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
447 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
449 "Use simgrid_update_xml to update your file automatically. "
450 "This program is installed automatically with SimGrid, or "
451 "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
455 void ETag_surfxml_platform(){
459 void STag_surfxml_host(){
461 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
464 void STag_surfxml_prop()
466 if (AS_TAG) { // We need to retrieve the most recently opened AS
467 XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
468 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
470 netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
473 if (!current_property_set)
474 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
475 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
476 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
477 current_property_set);
481 void ETag_surfxml_host() {
482 s_sg_platf_host_cbarg_t host;
483 memset(&host,0,sizeof(host));
485 host.properties = current_property_set;
486 current_property_set = nullptr;
488 host.id = A_surfxml_host_id;
490 char* buf = A_surfxml_host_speed;
491 XBT_DEBUG("Buffer: %s", buf);
492 if (strchr(buf, ',') == nullptr){
493 double speed = surf_parse_get_speed(A_surfxml_host_speed,"speed of host", host.id);
494 host.speed_per_pstate.push_back(speed);
497 xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
500 xbt_dynar_foreach(pstate_list, i, speed_str) {
501 xbt_str_trim(speed_str, nullptr);
502 double speed = surf_parse_get_speed(speed_str,"speed of host", host.id);
503 host.speed_per_pstate.push_back(speed);
504 XBT_DEBUG("Speed value: %f", speed);
506 xbt_dynar_free(&pstate_list);
509 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
510 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
511 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
512 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
513 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
514 host.coord = A_surfxml_host_coordinates;
516 sg_platf_new_host(&host);
519 void STag_surfxml_host___link(){
520 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
521 s_sg_platf_host_link_cbarg_t host_link;
522 memset(&host_link,0,sizeof(host_link));
524 host_link.id = A_surfxml_host___link_id;
525 host_link.link_up = A_surfxml_host___link_up;
526 host_link.link_down = A_surfxml_host___link_down;
527 sg_platf_new_hostlink(&host_link);
530 void STag_surfxml_router(){
531 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
534 void ETag_surfxml_cluster(){
535 s_sg_platf_cluster_cbarg_t cluster;
536 memset(&cluster,0,sizeof(cluster));
537 cluster.properties = current_property_set;
538 current_property_set = nullptr;
540 cluster.id = A_surfxml_cluster_id;
541 cluster.prefix = A_surfxml_cluster_prefix;
542 cluster.suffix = A_surfxml_cluster_suffix;
543 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
544 cluster.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);
611 void STag_surfxml_cluster(){
613 parse_after_config();
614 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
617 void STag_surfxml_cabinet(){
618 parse_after_config();
619 s_sg_platf_cabinet_cbarg_t cabinet;
620 memset(&cabinet,0,sizeof(cabinet));
621 cabinet.id = A_surfxml_cabinet_id;
622 cabinet.prefix = A_surfxml_cabinet_prefix;
623 cabinet.suffix = A_surfxml_cabinet_suffix;
624 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
625 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
626 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
627 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
629 sg_platf_new_cabinet(&cabinet);
632 void STag_surfxml_peer(){
633 parse_after_config();
634 s_sg_platf_peer_cbarg_t peer;
635 memset(&peer,0,sizeof(peer));
636 peer.id = A_surfxml_peer_id;
637 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
638 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
639 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
640 peer.coord = A_surfxml_peer_coordinates;
641 peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
642 peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
644 if (A_surfxml_peer_lat[0] != '\0')
645 XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
648 sg_platf_new_peer(&peer);
651 void STag_surfxml_link(){
653 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
656 void ETag_surfxml_link(){
657 LinkCreationArgs link;
659 link.properties = current_property_set;
660 current_property_set = nullptr;
662 link.id = std::string(A_surfxml_link_id);
663 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
664 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
665 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
666 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
667 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
669 switch (A_surfxml_link_sharing___policy) {
670 case A_surfxml_link_sharing___policy_SHARED:
671 link.policy = SURF_LINK_SHARED;
673 case A_surfxml_link_sharing___policy_FATPIPE:
674 link.policy = SURF_LINK_FATPIPE;
676 case A_surfxml_link_sharing___policy_FULLDUPLEX:
677 link.policy = SURF_LINK_FULLDUPLEX;
680 surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
684 sg_platf_new_link(&link);
687 void STag_surfxml_link___ctn(){
689 simgrid::surf::LinkImpl* link;
690 char *link_name=nullptr;
691 switch (A_surfxml_link___ctn_direction) {
692 case AU_surfxml_link___ctn_direction:
693 case A_surfxml_link___ctn_direction_NONE:
694 link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
696 case A_surfxml_link___ctn_direction_UP:
697 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
698 link = simgrid::surf::LinkImpl::byName(link_name);
700 case A_surfxml_link___ctn_direction_DOWN:
701 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
702 link = simgrid::surf::LinkImpl::byName(link_name);
705 xbt_free(link_name); // no-op if it's already nullptr
707 surf_parse_assert(link!=nullptr,"No such link: '%s'%s", A_surfxml_link___ctn_id,
708 A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_UP?" (upward)":
709 ( A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_DOWN?" (downward)":
711 parsed_link_list.push_back(link);
714 void ETag_surfxml_backbone(){
715 LinkCreationArgs link;
717 link.properties = nullptr;
718 link.id = std::string(A_surfxml_backbone_id);
719 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
720 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
721 link.policy = SURF_LINK_SHARED;
723 sg_platf_new_link(&link);
724 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
727 void STag_surfxml_route(){
728 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
729 A_surfxml_route_src);
730 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
731 A_surfxml_route_dst);
734 void STag_surfxml_ASroute(){
735 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
736 A_surfxml_route_src);
737 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
738 A_surfxml_route_dst);
740 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
741 A_surfxml_ASroute_gw___src);
742 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
743 A_surfxml_ASroute_gw___dst);
746 void STag_surfxml_bypassRoute(){
747 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
748 A_surfxml_bypassRoute_src);
749 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
750 A_surfxml_bypassRoute_dst);
753 void STag_surfxml_bypassASroute(){
754 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
755 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
756 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
757 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
758 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
759 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
760 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
761 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
764 void ETag_surfxml_route(){
765 s_sg_platf_route_cbarg_t route;
766 memset(&route,0,sizeof(route));
768 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
769 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
770 route.gw_src = nullptr;
771 route.gw_dst = nullptr;
772 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
773 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
775 for (auto link: parsed_link_list)
776 route.link_list->push_back(link);
777 parsed_link_list.clear();
779 sg_platf_new_route(&route);
780 delete route.link_list;
783 void ETag_surfxml_ASroute(){
784 s_sg_platf_route_cbarg_t ASroute;
785 memset(&ASroute,0,sizeof(ASroute));
787 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
788 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
790 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
791 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
793 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
795 for (auto link: parsed_link_list)
796 ASroute.link_list->push_back(link);
797 parsed_link_list.clear();
799 switch (A_surfxml_ASroute_symmetrical) {
800 case AU_surfxml_ASroute_symmetrical:
801 case A_surfxml_ASroute_symmetrical_YES:
802 ASroute.symmetrical = true;
804 case A_surfxml_ASroute_symmetrical_NO:
805 ASroute.symmetrical = false;
809 sg_platf_new_route(&ASroute);
810 delete ASroute.link_list;
813 void ETag_surfxml_bypassRoute(){
814 s_sg_platf_route_cbarg_t route;
815 memset(&route,0,sizeof(route));
817 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
818 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
819 route.gw_src = nullptr;
820 route.gw_dst = nullptr;
821 route.symmetrical = false;
822 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
824 for (auto link: parsed_link_list)
825 route.link_list->push_back(link);
826 parsed_link_list.clear();
828 sg_platf_new_bypassRoute(&route);
829 delete route.link_list;
832 void ETag_surfxml_bypassASroute(){
833 s_sg_platf_route_cbarg_t ASroute;
834 memset(&ASroute,0,sizeof(ASroute));
836 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
837 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
838 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
839 for (auto link: parsed_link_list)
840 ASroute.link_list->push_back(link);
841 parsed_link_list.clear();
843 ASroute.symmetrical = false;
845 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
846 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
848 sg_platf_new_bypassRoute(&ASroute);
849 delete ASroute.link_list;
852 void ETag_surfxml_trace(){
853 s_sg_platf_trace_cbarg_t trace;
854 memset(&trace,0,sizeof(trace));
856 trace.id = A_surfxml_trace_id;
857 trace.file = A_surfxml_trace_file;
858 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
859 trace.pc_data = surfxml_pcdata;
861 sg_platf_new_trace(&trace);
864 void STag_surfxml_trace___connect(){
865 parse_after_config();
866 s_sg_platf_trace_connect_cbarg_t trace_connect;
867 memset(&trace_connect,0,sizeof(trace_connect));
869 trace_connect.element = A_surfxml_trace___connect_element;
870 trace_connect.trace = A_surfxml_trace___connect_trace;
872 switch (A_surfxml_trace___connect_kind) {
873 case AU_surfxml_trace___connect_kind:
874 case A_surfxml_trace___connect_kind_SPEED:
875 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
877 case A_surfxml_trace___connect_kind_BANDWIDTH:
878 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
880 case A_surfxml_trace___connect_kind_HOST___AVAIL:
881 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
883 case A_surfxml_trace___connect_kind_LATENCY:
884 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
886 case A_surfxml_trace___connect_kind_LINK___AVAIL:
887 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
890 sg_platf_trace_connect(&trace_connect);
893 void STag_surfxml_AS(){
894 parse_after_config();
896 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
898 sg_platf_new_AS_begin(&AS);
900 void ETag_surfxml_AS(){
901 sg_platf_new_AS_seal();
904 void STag_surfxml_config(){
906 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
907 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
908 if (_sg_cfg_init_status == 2) {
909 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
912 void ETag_surfxml_config(){
913 xbt_dict_cursor_t cursor = nullptr;
916 xbt_dict_foreach(current_property_set, cursor, key, elem) {
917 if (xbt_cfg_is_default_value(key)) {
918 char* cfg = bprintf("%s:%s", key, elem);
919 xbt_cfg_set_parse(cfg);
922 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
924 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
926 xbt_dict_free(¤t_property_set);
927 current_property_set = nullptr;
933 void STag_surfxml_process(){
936 argv = xbt_new(char *, 1);
937 argv[0] = xbt_strdup(A_surfxml_process_function);
938 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
941 void ETag_surfxml_process(){
942 s_sg_platf_process_cbarg_t process;
943 memset(&process,0,sizeof(process));
946 process.argv = (const char **)argv;
947 process.properties = current_property_set;
948 process.host = A_surfxml_process_host;
949 process.function = A_surfxml_process_function;
950 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
951 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
953 switch (A_surfxml_process_on___failure) {
954 case AU_surfxml_process_on___failure:
955 case A_surfxml_process_on___failure_DIE:
956 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
958 case A_surfxml_process_on___failure_RESTART:
959 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
963 sg_platf_new_process(&process);
965 for (int i = 0; i != argc; ++i)
970 current_property_set = nullptr;
973 void STag_surfxml_argument(){
975 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
976 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
979 void STag_surfxml_model___prop(){
980 if (!current_model_property_set)
981 current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
983 xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
986 void ETag_surfxml_prop(){/* Nothing to do */}
987 void STag_surfxml_random(){/* Nothing to do */}
988 void ETag_surfxml_random(){/* Nothing to do */}
989 void ETag_surfxml_trace___connect(){/* Nothing to do */}
990 void STag_surfxml_trace(){parse_after_config();}
991 void ETag_surfxml_router(){/*Nothing to do*/}
992 void ETag_surfxml_host___link(){/* Nothing to do */}
993 void ETag_surfxml_cabinet(){/* Nothing to do */}
994 void ETag_surfxml_peer(){/* Nothing to do */}
995 void STag_surfxml_backbone(){/* Nothing to do */}
996 void ETag_surfxml_link___ctn(){/* Nothing to do */}
997 void ETag_surfxml_argument(){/* Nothing to do */}
998 void ETag_surfxml_model___prop(){/* Nothing to do */}
1000 /* Open and Close parse file */
1001 void surf_parse_open(const char *file)
1003 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1005 if (!surf_input_buffer_stack)
1006 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), nullptr);
1007 if (!surf_file_to_parse_stack)
1008 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), nullptr);
1010 if (!surf_parsed_filename_stack)
1011 surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1013 surf_parsed_filename = xbt_strdup(file);
1014 char *dir = xbt_dirname(file);
1015 xbt_dynar_push(surf_path, &dir);
1017 surf_file_to_parse = surf_fopen(file, "r");
1018 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1019 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1020 surf_parse__switch_to_buffer(surf_input_buffer);
1021 surf_parse_lineno = 1;
1024 void surf_parse_close()
1026 xbt_dynar_free(&surf_input_buffer_stack);
1027 xbt_dynar_free(&surf_file_to_parse_stack);
1028 xbt_dynar_free(&surf_parsed_filename_stack);
1029 if (surf_parsed_filename) {
1030 char *dir = nullptr;
1031 xbt_dynar_pop(surf_path, &dir);
1035 free(surf_parsed_filename);
1036 surf_parsed_filename = nullptr;
1038 if (surf_file_to_parse) {
1039 surf_parse__delete_buffer(surf_input_buffer);
1040 fclose(surf_file_to_parse);
1041 surf_file_to_parse = nullptr; //Must be reset for Bypass
1045 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1046 static int _surf_parse() {
1047 return surf_parse_lex();
1050 int_f_void_t surf_parse = _surf_parse;