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.id = A_surfxml_storage_id;
303 storage.type_id = A_surfxml_storage_typeId;
304 storage.content = A_surfxml_storage_content;
305 storage.content_type = A_surfxml_storage_content___type;
306 storage.properties = current_property_set;
307 storage.attach = A_surfxml_storage_attach;
308 sg_platf_new_storage(&storage);
309 current_property_set = nullptr;
311 void STag_surfxml_storage___type()
314 XBT_DEBUG("STag_surfxml_storage___type");
315 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
316 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
318 void ETag_surfxml_storage___type()
320 s_sg_platf_storage_type_cbarg_t storage_type;
321 memset(&storage_type,0,sizeof(storage_type));
323 storage_type.content = A_surfxml_storage___type_content;
324 storage_type.content_type = A_surfxml_storage___type_content___type;
325 storage_type.id = A_surfxml_storage___type_id;
326 storage_type.model = A_surfxml_storage___type_model;
327 storage_type.properties = current_property_set;
328 storage_type.model_properties = current_model_property_set;
329 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size,
330 "size of storage type", storage_type.id);
331 sg_platf_new_storage_type(&storage_type);
332 current_property_set = nullptr;
333 current_model_property_set = nullptr;
335 void STag_surfxml_mount()
337 XBT_DEBUG("STag_surfxml_mount");
339 void ETag_surfxml_mount()
341 s_sg_platf_mount_cbarg_t mount;
342 memset(&mount,0,sizeof(mount));
344 mount.name = A_surfxml_mount_name;
345 mount.storageId = A_surfxml_mount_storageId;
346 sg_platf_new_mount(&mount);
350 * Stuff relative to the <include> tag
352 static xbt_dynar_t surf_input_buffer_stack = nullptr;
353 static xbt_dynar_t surf_file_to_parse_stack = nullptr;
354 static xbt_dynar_t surf_parsed_filename_stack = nullptr;
356 void STag_surfxml_include()
358 parse_after_config();
359 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
360 xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
361 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
363 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
365 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
366 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
368 xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
369 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
370 surf_parse_push_buffer_state(surf_input_buffer);
375 void ETag_surfxml_include() {
376 /* Nothing to do when done with reading the include tag.
377 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
380 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
382 * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
383 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
384 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
385 * error message in that case.
387 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
388 * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
389 * on a line. One day maybe, if the maya allow it.
391 int ETag_surfxml_include_state()
394 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
396 if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
399 // Yeah, we were in an <include> Restore state and proceed.
400 fclose(surf_file_to_parse);
401 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
402 surf_parse_pop_buffer_state();
403 xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
405 // Restore the filename for error messages
406 free(surf_parsed_filename);
407 xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
412 /* Stag and Etag parse functions */
414 void STag_surfxml_platform() {
415 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
417 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
418 "You're using an ancient XML file.\n"
419 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
420 "instead of MBytes, MFlops and seconds.\n"
422 "Use simgrid_update_xml to update your file automatically. "
423 "This program is installed automatically with SimGrid, or "
424 "available in the tools/ directory of the source archive.\n"
426 "Please check also out the SURF section of the ChangeLog for "
427 "the 3.1 version for more information. \n"
429 "Last, do not forget to also update your values for "
430 "the calls to MSG_task_create (if any).");
431 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
432 "You're using an old XML file.\n"
433 "Use simgrid_update_xml to update your file automatically. "
434 "This program is installed automatically with SimGrid, or "
435 "available in the tools/ directory of the source archive.");
436 xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
437 "Changes introduced in SimGrid 3.13:\n"
438 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
439 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
440 " - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
441 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
443 "Use simgrid_update_xml to update your file automatically. "
444 "This program is installed automatically with SimGrid, or "
445 "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
449 void ETag_surfxml_platform(){
453 void STag_surfxml_host(){
455 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
458 void STag_surfxml_prop()
460 if (AS_TAG) { // We need to retrieve the most recently opened AS
461 XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
462 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
464 netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
467 if (!current_property_set)
468 current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
469 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
470 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
471 current_property_set);
475 void ETag_surfxml_host() {
476 s_sg_platf_host_cbarg_t host;
477 memset(&host,0,sizeof(host));
481 host.properties = current_property_set;
483 host.id = A_surfxml_host_id;
485 buf = A_surfxml_host_speed;
486 XBT_DEBUG("Buffer: %s", buf);
487 if (strchr(buf, ',') == nullptr){
488 double speed = surf_parse_get_speed(A_surfxml_host_speed,"speed of host", host.id);
489 host.speed_per_pstate.push_back(speed);
492 xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
495 xbt_dynar_foreach(pstate_list, i, speed_str) {
496 xbt_str_trim(speed_str, nullptr);
497 double speed = surf_parse_get_speed(speed_str,"speed of host", host.id);
498 host.speed_per_pstate.push_back(speed);
499 XBT_DEBUG("Speed value: %f", speed);
501 xbt_dynar_free(&pstate_list);
504 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
505 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
506 host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
507 host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
508 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
509 host.coord = A_surfxml_host_coordinates;
511 sg_platf_new_host(&host);
512 current_property_set = nullptr;
515 void STag_surfxml_host___link(){
516 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
517 s_sg_platf_host_link_cbarg_t host_link;
518 memset(&host_link,0,sizeof(host_link));
520 host_link.id = A_surfxml_host___link_id;
521 host_link.link_up = A_surfxml_host___link_up;
522 host_link.link_down = A_surfxml_host___link_down;
523 sg_platf_new_hostlink(&host_link);
526 void STag_surfxml_router(){
527 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
530 void ETag_surfxml_cluster(){
531 s_sg_platf_cluster_cbarg_t cluster;
532 memset(&cluster,0,sizeof(cluster));
533 cluster.properties = current_property_set;
535 cluster.id = A_surfxml_cluster_id;
536 cluster.prefix = A_surfxml_cluster_prefix;
537 cluster.suffix = A_surfxml_cluster_suffix;
538 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
539 cluster.speed = surf_parse_get_speed(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
540 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
541 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
542 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
543 if(strcmp(A_surfxml_cluster_bb___bw,""))
544 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
545 if(strcmp(A_surfxml_cluster_bb___lat,""))
546 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
547 if(strcmp(A_surfxml_cluster_limiter___link,""))
548 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
549 if(strcmp(A_surfxml_cluster_loopback___bw,""))
550 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
551 if(strcmp(A_surfxml_cluster_loopback___lat,""))
552 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
554 switch(AX_surfxml_cluster_topology){
555 case A_surfxml_cluster_topology_FLAT:
556 cluster.topology= SURF_CLUSTER_FLAT ;
558 case A_surfxml_cluster_topology_TORUS:
559 cluster.topology= SURF_CLUSTER_TORUS ;
561 case A_surfxml_cluster_topology_FAT___TREE:
562 cluster.topology = SURF_CLUSTER_FAT_TREE;
564 case A_surfxml_cluster_topology_DRAGONFLY:
565 cluster.topology= SURF_CLUSTER_DRAGONFLY ;
568 surf_parse_error("Invalid cluster topology for cluster %s",
572 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
573 cluster.router_id = A_surfxml_cluster_router___id;
575 switch (AX_surfxml_cluster_sharing___policy) {
576 case A_surfxml_cluster_sharing___policy_SHARED:
577 cluster.sharing_policy = SURF_LINK_SHARED;
579 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
580 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
582 case A_surfxml_cluster_sharing___policy_FATPIPE:
583 cluster.sharing_policy = SURF_LINK_FATPIPE;
586 surf_parse_error("Invalid cluster sharing policy for cluster %s",
590 switch (AX_surfxml_cluster_bb___sharing___policy) {
591 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
592 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
594 case A_surfxml_cluster_bb___sharing___policy_SHARED:
595 cluster.bb_sharing_policy = SURF_LINK_SHARED;
598 surf_parse_error("Invalid bb sharing policy in cluster %s",
603 sg_platf_new_cluster(&cluster);
605 current_property_set = nullptr;
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 link.id = A_surfxml_link_id;
659 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id);
660 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
661 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id);
662 link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
663 link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
665 switch (A_surfxml_link_sharing___policy) {
666 case A_surfxml_link_sharing___policy_SHARED:
667 link.policy = SURF_LINK_SHARED;
669 case A_surfxml_link_sharing___policy_FATPIPE:
670 link.policy = SURF_LINK_FATPIPE;
672 case A_surfxml_link_sharing___policy_FULLDUPLEX:
673 link.policy = SURF_LINK_FULLDUPLEX;
676 surf_parse_error("Invalid sharing policy in link %s", link.id);
680 sg_platf_new_link(&link);
682 current_property_set = nullptr;
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 = Link::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 = Link::byName(link_name);
698 case A_surfxml_link___ctn_direction_DOWN:
699 link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
700 link = Link::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);
719 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id);
720 link.policy = SURF_LINK_SHARED;
722 sg_platf_new_link(&link);
723 routing_cluster_add_backbone(sg_link_by_name(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<Link*>();
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<Link*>();
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<Link*>();
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);
830 void ETag_surfxml_bypassASroute(){
831 s_sg_platf_route_cbarg_t ASroute;
832 memset(&ASroute,0,sizeof(ASroute));
834 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
835 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
836 ASroute.link_list = new std::vector<Link*>();
837 for (auto link: parsed_link_list)
838 ASroute.link_list->push_back(link);
839 parsed_link_list.clear();
841 ASroute.symmetrical = false;
843 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
844 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
846 sg_platf_new_bypassRoute(&ASroute);
849 void ETag_surfxml_trace(){
850 s_sg_platf_trace_cbarg_t trace;
851 memset(&trace,0,sizeof(trace));
853 trace.id = A_surfxml_trace_id;
854 trace.file = A_surfxml_trace_file;
855 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
856 trace.pc_data = surfxml_pcdata;
858 sg_platf_new_trace(&trace);
861 void STag_surfxml_trace___connect(){
862 parse_after_config();
863 s_sg_platf_trace_connect_cbarg_t trace_connect;
864 memset(&trace_connect,0,sizeof(trace_connect));
866 trace_connect.element = A_surfxml_trace___connect_element;
867 trace_connect.trace = A_surfxml_trace___connect_trace;
869 switch (A_surfxml_trace___connect_kind) {
870 case AU_surfxml_trace___connect_kind:
871 case A_surfxml_trace___connect_kind_SPEED:
872 trace_connect.kind = SURF_TRACE_CONNECT_KIND_SPEED;
874 case A_surfxml_trace___connect_kind_BANDWIDTH:
875 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
877 case A_surfxml_trace___connect_kind_HOST___AVAIL:
878 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
880 case A_surfxml_trace___connect_kind_LATENCY:
881 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
883 case A_surfxml_trace___connect_kind_LINK___AVAIL:
884 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
887 sg_platf_trace_connect(&trace_connect);
890 void STag_surfxml_AS(){
891 parse_after_config();
893 s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
895 sg_platf_new_AS_begin(&AS);
897 void ETag_surfxml_AS(){
898 sg_platf_new_AS_seal();
901 void STag_surfxml_config(){
903 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
904 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
905 if (_sg_cfg_init_status == 2) {
906 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
909 void ETag_surfxml_config(){
910 xbt_dict_cursor_t cursor = nullptr;
914 xbt_dict_foreach(current_property_set, cursor, key, elem) {
915 cfg = bprintf("%s:%s",key,elem);
916 if(xbt_cfg_is_default_value(key))
917 xbt_cfg_set_parse(cfg);
919 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
922 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
924 xbt_dict_free(¤t_property_set);
925 current_property_set = nullptr;
931 void STag_surfxml_process(){
934 argv = xbt_new(char *, 1);
935 argv[0] = xbt_strdup(A_surfxml_process_function);
936 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
939 void ETag_surfxml_process(){
940 s_sg_platf_process_cbarg_t process;
941 memset(&process,0,sizeof(process));
944 process.argv = (const char **)argv;
945 process.properties = current_property_set;
946 process.host = A_surfxml_process_host;
947 process.function = A_surfxml_process_function;
948 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
949 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
951 switch (A_surfxml_process_on___failure) {
952 case AU_surfxml_process_on___failure:
953 case A_surfxml_process_on___failure_DIE:
954 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
956 case A_surfxml_process_on___failure_RESTART:
957 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
961 sg_platf_new_process(&process);
963 for (int i = 0; i != argc; ++i)
968 current_property_set = nullptr;
971 void STag_surfxml_argument(){
973 argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
974 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
977 void STag_surfxml_model___prop(){
978 if (!current_model_property_set)
979 current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
981 xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
984 void ETag_surfxml_prop(){/* Nothing to do */}
985 void STag_surfxml_random(){/* Nothing to do */}
986 void ETag_surfxml_random(){/* Nothing to do */}
987 void ETag_surfxml_trace___connect(){/* Nothing to do */}
988 void STag_surfxml_trace(){parse_after_config();}
989 void ETag_surfxml_router(){/*Nothing to do*/}
990 void ETag_surfxml_host___link(){/* Nothing to do */}
991 void ETag_surfxml_cabinet(){/* Nothing to do */}
992 void ETag_surfxml_peer(){/* Nothing to do */}
993 void STag_surfxml_backbone(){/* Nothing to do */}
994 void ETag_surfxml_link___ctn(){/* Nothing to do */}
995 void ETag_surfxml_argument(){/* Nothing to do */}
996 void ETag_surfxml_model___prop(){/* Nothing to do */}
998 /* Open and Close parse file */
999 void surf_parse_open(const char *file)
1001 xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1003 if (!surf_input_buffer_stack)
1004 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), nullptr);
1005 if (!surf_file_to_parse_stack)
1006 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), nullptr);
1008 if (!surf_parsed_filename_stack)
1009 surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1011 surf_parsed_filename = xbt_strdup(file);
1012 char *dir = xbt_dirname(file);
1013 xbt_dynar_push(surf_path, &dir);
1015 surf_file_to_parse = surf_fopen(file, "r");
1016 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1017 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1018 surf_parse__switch_to_buffer(surf_input_buffer);
1019 surf_parse_lineno = 1;
1022 void surf_parse_close()
1024 xbt_dynar_free(&surf_input_buffer_stack);
1025 xbt_dynar_free(&surf_file_to_parse_stack);
1026 xbt_dynar_free(&surf_parsed_filename_stack);
1027 if (surf_parsed_filename) {
1028 char *dir = nullptr;
1029 xbt_dynar_pop(surf_path, &dir);
1033 free(surf_parsed_filename);
1034 surf_parsed_filename = nullptr;
1036 if (surf_file_to_parse) {
1037 surf_parse__delete_buffer(surf_input_buffer);
1038 fclose(surf_file_to_parse);
1039 surf_file_to_parse = nullptr; //Must be reset for Bypass
1043 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1044 static int _surf_parse() {
1045 return surf_parse_lex();
1048 int_f_void_t surf_parse = _surf_parse;