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::LinkImpl*> parsed_link_list; /* temporary store of current list link of a route */
38 void surf_parse_assert(bool cond, const char *fmt, ...) {
42 int lineno = surf_parse_lineno;
43 char *msg = bvprintf(fmt,va);
46 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
48 xbt_die("Exiting now");
51 void surf_parse_error(const char *fmt, ...) {
54 int lineno = surf_parse_lineno;
55 char *msg = bvprintf(fmt,va);
58 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
60 xbt_die("Exiting now");
62 void surf_parse_warn(const char *fmt, ...) {
65 char *msg = bvprintf(fmt,va);
67 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
71 double surf_parse_get_double(const char *string) {
73 int ret = sscanf(string, "%lg", &res);
75 surf_parse_error("%s is not a double", string);
79 int surf_parse_get_int(const char *string) {
81 int ret = sscanf(string, "%d", &res);
83 surf_parse_error("%s is not an integer", string);
87 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
88 static std::vector<int>* explodesRadical(const char* radicals)
90 std::vector<int>* exploded = new std::vector<int>();
95 xbt_dynar_t radical_elements = xbt_str_split(radicals, ",");
96 xbt_dynar_foreach (radical_elements, iter, groups) {
98 xbt_dynar_t radical_ends = xbt_str_split(groups, "-");
99 int start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char*));
102 switch (xbt_dynar_length(radical_ends)) {
107 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char*));
110 surf_parse_error("Malformed radical: %s", groups);
114 for (int i = start; i <= end; i++)
115 exploded->push_back(i);
117 xbt_dynar_free(&radical_ends);
119 xbt_dynar_free(&radical_elements);
129 /* Note: field `unit' for the last element of parameter `units' should be nullptr. */
130 static double surf_parse_get_value_with_unit(const char *string, const struct unit_scale *units,
131 const char *entity_kind, const char *name, const char *error_msg, const char *default_unit)
136 double res = strtod(string, &ptr);
138 surf_parse_error("value out of range: %s", string);
140 surf_parse_error("cannot parse number: %s", string);
141 if (ptr[0] == '\0') {
143 return res; // Ok, 0 can be unit-less
145 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s",string, entity_kind, name, error_msg);
146 ptr = (char*)default_unit;
148 for (i = 0; units[i].unit != nullptr && strcmp(ptr, units[i].unit) != 0; i++);
150 if (units[i].unit != nullptr)
151 res *= units[i].scale;
153 surf_parse_error("unknown unit: %s", ptr);
157 double surf_parse_get_time(const char *string, const char *entity_kind, const char *name)
159 const struct unit_scale units[] = {
160 { "w", 7 * 24 * 60 * 60 },
161 { "d", 24 * 60 * 60 },
171 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
172 "Append 's' to your time to get seconds", "s");
175 double surf_parse_get_size(const char *string, const char *entity_kind, const char *name)
177 const struct unit_scale units[] = {
178 { "EiB", pow(1024, 6) },
179 { "PiB", pow(1024, 5) },
180 { "TiB", pow(1024, 4) },
181 { "GiB", pow(1024, 3) },
182 { "MiB", pow(1024, 2) },
191 { "Eib", 0.125 * pow(1024, 6) },
192 { "Pib", 0.125 * pow(1024, 5) },
193 { "Tib", 0.125 * pow(1024, 4) },
194 { "Gib", 0.125 * pow(1024, 3) },
195 { "Mib", 0.125 * pow(1024, 2) },
196 { "Kib", 0.125 * 1024 },
197 { "Eb", 0.125 * 1e18 },
198 { "Pb", 0.125 * 1e15 },
199 { "Tb", 0.125 * 1e12 },
200 { "Gb", 0.125 * 1e9 },
201 { "Mb", 0.125 * 1e6 },
202 { "kb", 0.125 * 1e3 },
206 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
207 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
210 double surf_parse_get_bandwidth(const char *string, const char *entity_kind, const char *name)
212 const struct unit_scale units[] = {
213 { "EiBps", pow(1024, 6) },
214 { "PiBps", pow(1024, 5) },
215 { "TiBps", pow(1024, 4) },
216 { "GiBps", pow(1024, 3) },
217 { "MiBps", pow(1024, 2) },
226 { "Eibps", 0.125 * pow(1024, 6) },
227 { "Pibps", 0.125 * pow(1024, 5) },
228 { "Tibps", 0.125 * pow(1024, 4) },
229 { "Gibps", 0.125 * pow(1024, 3) },
230 { "Mibps", 0.125 * pow(1024, 2) },
231 { "Kibps", 0.125 * 1024 },
232 { "Tbps", 0.125 * 1e12 },
233 { "Gbps", 0.125 * 1e9 },
234 { "Mbps", 0.125 * 1e6 },
235 { "kbps", 0.125 * 1e3 },
239 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
240 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
243 double surf_parse_get_speed(const char *string, const char *entity_kind, const char *name)
245 const struct unit_scale units[] = {
246 { "yottaflops", 1e24 },
248 { "zettaflops", 1e21 },
250 { "exaflops", 1e18 },
252 { "petaflops", 1e15 },
254 { "teraflops", 1e12 },
256 { "gigaflops", 1e9 },
258 { "megaflops", 1e6 },
260 { "kiloflops", 1e3 },
266 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
267 "Append 'f' or 'flops' to your speed to get flop per second", "f");
271 * All the callback lists that can be overridden anywhere.
272 * (this list should probably be reduced to the bare minimum to allow the models to work)
275 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
277 /* The default current property receiver. Setup in the corresponding opening callbacks. */
278 xbt_dict_t current_property_set = nullptr;
279 xbt_dict_t current_model_property_set = nullptr;
280 int AS_TAG = 0; // Whether we just opened an AS tag (to see what to do with the properties)
282 /* dictionary of random generator data */
283 xbt_dict_t random_data_list = nullptr;
285 YY_BUFFER_STATE surf_input_buffer;
286 FILE *surf_file_to_parse = nullptr;
289 * Stuff relative to storage
291 void STag_surfxml_storage()
294 XBT_DEBUG("STag_surfxml_storage");
295 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
297 void ETag_surfxml_storage()
299 s_sg_platf_storage_cbarg_t storage;
300 memset(&storage,0,sizeof(storage));
302 storage.properties = current_property_set;
303 current_property_set = nullptr;
305 storage.id = A_surfxml_storage_id;
306 storage.type_id = A_surfxml_storage_typeId;
307 storage.content = A_surfxml_storage_content;
308 storage.content_type = A_surfxml_storage_content___type;
309 storage.attach = A_surfxml_storage_attach;
310 sg_platf_new_storage(&storage);
312 void STag_surfxml_storage___type()
315 XBT_DEBUG("STag_surfxml_storage___type");
316 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
317 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
319 void ETag_surfxml_storage___type()
321 s_sg_platf_storage_type_cbarg_t storage_type;
322 memset(&storage_type,0,sizeof(storage_type));
324 storage_type.properties = current_property_set;
325 current_property_set = nullptr;
327 storage_type.model_properties = current_model_property_set;
328 current_model_property_set = nullptr;
330 storage_type.content = A_surfxml_storage___type_content;
331 storage_type.content_type = A_surfxml_storage___type_content___type;
332 storage_type.id = A_surfxml_storage___type_id;
333 storage_type.model = A_surfxml_storage___type_model;
334 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size,
335 "size of storage type", storage_type.id);
336 sg_platf_new_storage_type(&storage_type);
338 void STag_surfxml_mount()
340 XBT_DEBUG("STag_surfxml_mount");
342 void ETag_surfxml_mount()
344 s_sg_platf_mount_cbarg_t mount;
345 memset(&mount,0,sizeof(mount));
347 mount.name = A_surfxml_mount_name;
348 mount.storageId = A_surfxml_mount_storageId;
349 sg_platf_new_mount(&mount);
353 * Stuff relative to the <include> tag
355 static xbt_dynar_t surf_input_buffer_stack = nullptr;
356 static xbt_dynar_t surf_file_to_parse_stack = nullptr;
357 static xbt_dynar_t surf_parsed_filename_stack = nullptr;
359 void STag_surfxml_include()
361 parse_after_config();
362 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
363 xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
364 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
366 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
368 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
369 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
371 xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
372 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
373 surf_parse_push_buffer_state(surf_input_buffer);
378 void ETag_surfxml_include() {
379 /* Nothing to do when done with reading the include tag.
380 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
383 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
385 * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
386 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
387 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
388 * error message in that case.
390 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
391 * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
392 * on a line. One day maybe, if the maya allow it.
394 int ETag_surfxml_include_state()
397 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
399 if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
402 // Yeah, we were in an <include> Restore state and proceed.
403 fclose(surf_file_to_parse);
404 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
405 surf_parse_pop_buffer_state();
406 xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
408 // Restore the filename for error messages
409 free(surf_parsed_filename);
410 xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
415 /* Stag and Etag parse functions */
417 void STag_surfxml_platform() {
418 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
420 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
421 "You're using an ancient XML file.\n"
422 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
423 "instead of MBytes, MFlops and seconds.\n"
425 "Use simgrid_update_xml to update your file automatically. "
426 "This program is installed automatically with SimGrid, or "
427 "available in the tools/ directory of the source archive.\n"
429 "Please check also out the SURF section of the ChangeLog for "
430 "the 3.1 version for more information. \n"
432 "Last, do not forget to also update your values for "
433 "the calls to MSG_task_create (if any).");
434 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
435 "You're using an old XML file.\n"
436 "Use simgrid_update_xml to update your file automatically. "
437 "This program is installed automatically with SimGrid, or "
438 "available in the tools/ directory of the source archive.");
439 xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
440 "Changes introduced in SimGrid 3.13:\n"
441 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
442 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
443 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
444 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
446 "Use simgrid_update_xml to update your file automatically. "
447 "This program is installed automatically with SimGrid, or "
448 "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
452 void ETag_surfxml_platform(){
456 void STag_surfxml_host(){
458 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
461 void STag_surfxml_prop()
463 if (AS_TAG) { // We need to retrieve the most recently opened AS
464 XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
465 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
467 netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
470 if (!current_property_set)
471 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
472 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
473 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
474 current_property_set);
478 void ETag_surfxml_host() {
479 s_sg_platf_host_cbarg_t host;
480 memset(&host,0,sizeof(host));
482 host.properties = current_property_set;
483 current_property_set = nullptr;
485 host.id = A_surfxml_host_id;
487 char* buf = A_surfxml_host_speed;
488 XBT_DEBUG("Buffer: %s", buf);
489 if (strchr(buf, ',') == nullptr){
490 double speed = surf_parse_get_speed(A_surfxml_host_speed,"speed of host", host.id);
491 host.speed_per_pstate.push_back(speed);
494 xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
497 xbt_dynar_foreach(pstate_list, i, speed_str) {
498 xbt_str_trim(speed_str, nullptr);
499 double speed = surf_parse_get_speed(speed_str,"speed of host", host.id);
500 host.speed_per_pstate.push_back(speed);
501 XBT_DEBUG("Speed value: %f", speed);
503 xbt_dynar_free(&pstate_list);
506 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
507 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
508 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
509 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
510 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
511 host.coord = A_surfxml_host_coordinates;
513 sg_platf_new_host(&host);
516 void STag_surfxml_host___link(){
517 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
518 s_sg_platf_host_link_cbarg_t host_link;
519 memset(&host_link,0,sizeof(host_link));
521 host_link.id = A_surfxml_host___link_id;
522 host_link.link_up = A_surfxml_host___link_up;
523 host_link.link_down = A_surfxml_host___link_down;
524 sg_platf_new_hostlink(&host_link);
527 void STag_surfxml_router(){
528 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
531 void ETag_surfxml_cluster(){
532 s_sg_platf_cluster_cbarg_t cluster;
533 memset(&cluster,0,sizeof(cluster));
534 cluster.properties = current_property_set;
535 current_property_set = nullptr;
537 cluster.id = A_surfxml_cluster_id;
538 cluster.prefix = A_surfxml_cluster_prefix;
539 cluster.suffix = A_surfxml_cluster_suffix;
540 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
541 cluster.speed = surf_parse_get_speed(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
542 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
543 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
544 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
545 if(strcmp(A_surfxml_cluster_bb___bw,""))
546 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
547 if(strcmp(A_surfxml_cluster_bb___lat,""))
548 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
549 if(strcmp(A_surfxml_cluster_limiter___link,""))
550 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
551 if(strcmp(A_surfxml_cluster_loopback___bw,""))
552 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
553 if(strcmp(A_surfxml_cluster_loopback___lat,""))
554 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
556 switch(AX_surfxml_cluster_topology){
557 case A_surfxml_cluster_topology_FLAT:
558 cluster.topology= SURF_CLUSTER_FLAT ;
560 case A_surfxml_cluster_topology_TORUS:
561 cluster.topology= SURF_CLUSTER_TORUS ;
563 case A_surfxml_cluster_topology_FAT___TREE:
564 cluster.topology = SURF_CLUSTER_FAT_TREE;
566 case A_surfxml_cluster_topology_DRAGONFLY:
567 cluster.topology= SURF_CLUSTER_DRAGONFLY ;
570 surf_parse_error("Invalid cluster topology for cluster %s",
574 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
575 cluster.router_id = A_surfxml_cluster_router___id;
577 switch (AX_surfxml_cluster_sharing___policy) {
578 case A_surfxml_cluster_sharing___policy_SHARED:
579 cluster.sharing_policy = SURF_LINK_SHARED;
581 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
582 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
584 case A_surfxml_cluster_sharing___policy_FATPIPE:
585 cluster.sharing_policy = SURF_LINK_FATPIPE;
588 surf_parse_error("Invalid cluster sharing policy for cluster %s",
592 switch (AX_surfxml_cluster_bb___sharing___policy) {
593 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
594 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
596 case A_surfxml_cluster_bb___sharing___policy_SHARED:
597 cluster.bb_sharing_policy = SURF_LINK_SHARED;
600 surf_parse_error("Invalid bb sharing policy in cluster %s",
605 sg_platf_new_cluster(&cluster);
608 void STag_surfxml_cluster(){
610 parse_after_config();
611 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
614 void STag_surfxml_cabinet(){
615 parse_after_config();
616 s_sg_platf_cabinet_cbarg_t cabinet;
617 memset(&cabinet,0,sizeof(cabinet));
618 cabinet.id = A_surfxml_cabinet_id;
619 cabinet.prefix = A_surfxml_cabinet_prefix;
620 cabinet.suffix = A_surfxml_cabinet_suffix;
621 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
622 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
623 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
624 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
626 sg_platf_new_cabinet(&cabinet);
629 void STag_surfxml_peer(){
630 parse_after_config();
631 s_sg_platf_peer_cbarg_t peer;
632 memset(&peer,0,sizeof(peer));
633 peer.id = A_surfxml_peer_id;
634 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
635 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
636 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
637 peer.coord = A_surfxml_peer_coordinates;
638 peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
639 peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
641 if (A_surfxml_peer_lat[0] != '\0')
642 XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
645 sg_platf_new_peer(&peer);
648 void STag_surfxml_link(){
650 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
653 void ETag_surfxml_link(){
654 s_sg_platf_link_cbarg_t link;
655 memset(&link,0,sizeof(link));
657 link.properties = current_property_set;
658 current_property_set = nullptr;
660 link.id = A_surfxml_link_id;
661 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
662 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
663 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
664 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
665 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
667 switch (A_surfxml_link_sharing___policy) {
668 case A_surfxml_link_sharing___policy_SHARED:
669 link.policy = SURF_LINK_SHARED;
671 case A_surfxml_link_sharing___policy_FATPIPE:
672 link.policy = SURF_LINK_FATPIPE;
674 case A_surfxml_link_sharing___policy_FULLDUPLEX:
675 link.policy = SURF_LINK_FULLDUPLEX;
678 surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
682 sg_platf_new_link(&link);
685 void STag_surfxml_link___ctn(){
687 simgrid::surf::LinkImpl* link;
688 char *link_name=nullptr;
689 switch (A_surfxml_link___ctn_direction) {
690 case AU_surfxml_link___ctn_direction:
691 case A_surfxml_link___ctn_direction_NONE:
692 link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
694 case A_surfxml_link___ctn_direction_UP:
695 link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
696 link = simgrid::surf::LinkImpl::byName(link_name);
698 case A_surfxml_link___ctn_direction_DOWN:
699 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
700 link = simgrid::surf::LinkImpl::byName(link_name);
703 xbt_free(link_name); // no-op if it's already nullptr
705 surf_parse_assert(link!=nullptr,"No such link: '%s'%s", A_surfxml_link___ctn_id,
706 A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_UP?" (upward)":
707 ( A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_DOWN?" (downward)":
709 parsed_link_list.push_back(link);
712 void ETag_surfxml_backbone(){
713 s_sg_platf_link_cbarg_t link;
714 memset(&link,0,sizeof(link));
716 link.properties = nullptr;
717 link.id = A_surfxml_backbone_id;
718 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
719 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
720 link.policy = SURF_LINK_SHARED;
722 sg_platf_new_link(&link);
723 routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
726 void STag_surfxml_route(){
727 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
728 A_surfxml_route_src);
729 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
730 A_surfxml_route_dst);
733 void STag_surfxml_ASroute(){
734 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
735 A_surfxml_route_src);
736 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
737 A_surfxml_route_dst);
739 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
740 A_surfxml_ASroute_gw___src);
741 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
742 A_surfxml_ASroute_gw___dst);
745 void STag_surfxml_bypassRoute(){
746 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
747 A_surfxml_bypassRoute_src);
748 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
749 A_surfxml_bypassRoute_dst);
752 void STag_surfxml_bypassASroute(){
753 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
754 "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
755 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
756 "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
757 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
758 "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
759 surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
760 "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
763 void ETag_surfxml_route(){
764 s_sg_platf_route_cbarg_t route;
765 memset(&route,0,sizeof(route));
767 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
768 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
769 route.gw_src = nullptr;
770 route.gw_dst = nullptr;
771 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
772 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
774 for (auto link: parsed_link_list)
775 route.link_list->push_back(link);
776 parsed_link_list.clear();
778 sg_platf_new_route(&route);
779 delete route.link_list;
782 void ETag_surfxml_ASroute(){
783 s_sg_platf_route_cbarg_t ASroute;
784 memset(&ASroute,0,sizeof(ASroute));
786 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
787 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
789 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
790 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
792 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
794 for (auto link: parsed_link_list)
795 ASroute.link_list->push_back(link);
796 parsed_link_list.clear();
798 switch (A_surfxml_ASroute_symmetrical) {
799 case AU_surfxml_ASroute_symmetrical:
800 case A_surfxml_ASroute_symmetrical_YES:
801 ASroute.symmetrical = true;
803 case A_surfxml_ASroute_symmetrical_NO:
804 ASroute.symmetrical = false;
808 sg_platf_new_route(&ASroute);
809 delete ASroute.link_list;
812 void ETag_surfxml_bypassRoute(){
813 s_sg_platf_route_cbarg_t route;
814 memset(&route,0,sizeof(route));
816 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
817 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
818 route.gw_src = nullptr;
819 route.gw_dst = nullptr;
820 route.symmetrical = false;
821 route.link_list = new std::vector<simgrid::surf::LinkImpl*>();
823 for (auto link: parsed_link_list)
824 route.link_list->push_back(link);
825 parsed_link_list.clear();
827 sg_platf_new_bypassRoute(&route);
828 delete route.link_list;
831 void ETag_surfxml_bypassASroute(){
832 s_sg_platf_route_cbarg_t ASroute;
833 memset(&ASroute,0,sizeof(ASroute));
835 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
836 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
837 ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
838 for (auto link: parsed_link_list)
839 ASroute.link_list->push_back(link);
840 parsed_link_list.clear();
842 ASroute.symmetrical = false;
844 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
845 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
847 sg_platf_new_bypassRoute(&ASroute);
848 delete ASroute.link_list;
851 void ETag_surfxml_trace(){
852 s_sg_platf_trace_cbarg_t trace;
853 memset(&trace,0,sizeof(trace));
855 trace.id = A_surfxml_trace_id;
856 trace.file = A_surfxml_trace_file;
857 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
858 trace.pc_data = surfxml_pcdata;
860 sg_platf_new_trace(&trace);
863 void STag_surfxml_trace___connect(){
864 parse_after_config();
865 s_sg_platf_trace_connect_cbarg_t trace_connect;
866 memset(&trace_connect,0,sizeof(trace_connect));
868 trace_connect.element = A_surfxml_trace___connect_element;
869 trace_connect.trace = A_surfxml_trace___connect_trace;
871 switch (A_surfxml_trace___connect_kind) {
872 case AU_surfxml_trace___connect_kind:
873 case A_surfxml_trace___connect_kind_SPEED:
874 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
876 case A_surfxml_trace___connect_kind_BANDWIDTH:
877 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
879 case A_surfxml_trace___connect_kind_HOST___AVAIL:
880 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
882 case A_surfxml_trace___connect_kind_LATENCY:
883 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
885 case A_surfxml_trace___connect_kind_LINK___AVAIL:
886 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
889 sg_platf_trace_connect(&trace_connect);
892 void STag_surfxml_AS(){
893 parse_after_config();
895 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
897 sg_platf_new_AS_begin(&AS);
899 void ETag_surfxml_AS(){
900 sg_platf_new_AS_seal();
903 void STag_surfxml_config(){
905 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
906 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
907 if (_sg_cfg_init_status == 2) {
908 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
911 void ETag_surfxml_config(){
912 xbt_dict_cursor_t cursor = nullptr;
915 xbt_dict_foreach(current_property_set, cursor, key, elem) {
916 if (xbt_cfg_is_default_value(key)) {
917 char* cfg = bprintf("%s:%s", key, elem);
918 xbt_cfg_set_parse(cfg);
921 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
923 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
925 xbt_dict_free(¤t_property_set);
926 current_property_set = nullptr;
932 void STag_surfxml_process(){
935 argv = xbt_new(char *, 1);
936 argv[0] = xbt_strdup(A_surfxml_process_function);
937 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
940 void ETag_surfxml_process(){
941 s_sg_platf_process_cbarg_t process;
942 memset(&process,0,sizeof(process));
945 process.argv = (const char **)argv;
946 process.properties = current_property_set;
947 process.host = A_surfxml_process_host;
948 process.function = A_surfxml_process_function;
949 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
950 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
952 switch (A_surfxml_process_on___failure) {
953 case AU_surfxml_process_on___failure:
954 case A_surfxml_process_on___failure_DIE:
955 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
957 case A_surfxml_process_on___failure_RESTART:
958 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
962 sg_platf_new_process(&process);
964 for (int i = 0; i != argc; ++i)
969 current_property_set = nullptr;
972 void STag_surfxml_argument(){
974 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
975 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
978 void STag_surfxml_model___prop(){
979 if (!current_model_property_set)
980 current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
982 xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
985 void ETag_surfxml_prop(){/* Nothing to do */}
986 void STag_surfxml_random(){/* Nothing to do */}
987 void ETag_surfxml_random(){/* Nothing to do */}
988 void ETag_surfxml_trace___connect(){/* Nothing to do */}
989 void STag_surfxml_trace(){parse_after_config();}
990 void ETag_surfxml_router(){/*Nothing to do*/}
991 void ETag_surfxml_host___link(){/* Nothing to do */}
992 void ETag_surfxml_cabinet(){/* Nothing to do */}
993 void ETag_surfxml_peer(){/* Nothing to do */}
994 void STag_surfxml_backbone(){/* Nothing to do */}
995 void ETag_surfxml_link___ctn(){/* Nothing to do */}
996 void ETag_surfxml_argument(){/* Nothing to do */}
997 void ETag_surfxml_model___prop(){/* Nothing to do */}
999 /* Open and Close parse file */
1000 void surf_parse_open(const char *file)
1002 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1004 if (!surf_input_buffer_stack)
1005 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), nullptr);
1006 if (!surf_file_to_parse_stack)
1007 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), nullptr);
1009 if (!surf_parsed_filename_stack)
1010 surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1012 surf_parsed_filename = xbt_strdup(file);
1013 char *dir = xbt_dirname(file);
1014 xbt_dynar_push(surf_path, &dir);
1016 surf_file_to_parse = surf_fopen(file, "r");
1017 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1018 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1019 surf_parse__switch_to_buffer(surf_input_buffer);
1020 surf_parse_lineno = 1;
1023 void surf_parse_close()
1025 xbt_dynar_free(&surf_input_buffer_stack);
1026 xbt_dynar_free(&surf_file_to_parse_stack);
1027 xbt_dynar_free(&surf_parsed_filename_stack);
1028 if (surf_parsed_filename) {
1029 char *dir = nullptr;
1030 xbt_dynar_pop(surf_path, &dir);
1034 free(surf_parsed_filename);
1035 surf_parsed_filename = nullptr;
1037 if (surf_file_to_parse) {
1038 surf_parse__delete_buffer(surf_input_buffer);
1039 fclose(surf_file_to_parse);
1040 surf_file_to_parse = nullptr; //Must be reset for Bypass
1044 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1045 static int _surf_parse() {
1046 return surf_parse_lex();
1049 int_f_void_t surf_parse = _surf_parse;