1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. 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 */
15 #include "surf/surfxml_parse.h"
16 #include "surf/surf_private.h"
17 #include "simgrid/sg_config.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
20 "Logging specific to the SURF parsing module");
22 int ETag_surfxml_include_state(void);
24 #include "simgrid_dtd.c"
26 char* surf_parsed_filename = NULL; // to locate parse error messages
28 xbt_dynar_t parsed_link_list = NULL; /* temporary store of current list link of a route */
29 extern AS_t current_routing;
33 void surf_parse_error(const char *fmt, ...) {
36 char *msg = bvprintf(fmt,va);
38 xbt_die("Parse error at %s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
40 void surf_parse_warn(const char *fmt, ...) {
43 char *msg = bvprintf(fmt,va);
45 XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
49 double surf_parse_get_double(const char *string) {
51 int ret = sscanf(string, "%lg", &res);
53 surf_parse_error("%s is not a double", string);
54 //printf("Parsed double [%lg] %s\n", res, string);
58 int surf_parse_get_int(const char *string) {
60 int ret = sscanf(string, "%d", &res);
62 surf_parse_error("%s is not an integer", string);
71 /* Note: field `unit' for the last element of parametre `units' should be
73 static double surf_parse_get_value_with_unit(const char *string,
74 const struct unit_scale *units)
80 res = strtod(string, &ptr);
82 surf_parse_error("value out of range: %s", string);
84 surf_parse_error("cannot parse number: %s", string);
85 for (i = 0; units[i].unit != NULL && strcmp(ptr, units[i].unit) != 0; i++) {
87 if (units[i].unit != NULL)
88 res *= units[i].scale;
90 surf_parse_error("unknown unit: %s", ptr);
94 double surf_parse_get_time(const char *string)
96 const struct unit_scale units[] = {
97 { "w", 7 * 24 * 60 * 60 },
98 { "d", 24 * 60 * 60 },
102 { "", 1.0 }, /* default unit is seconds */
109 return surf_parse_get_value_with_unit(string, units);
112 double surf_parse_get_size(const char *string)
114 const struct unit_scale units[] = {
115 { "TiB", pow(1024, 4) },
116 { "GiB", pow(1024, 3) },
117 { "MiB", pow(1024, 2) },
124 { "", 1.0 }, /* default unit is bytes*/
125 { "Tib", 0.125 * pow(1024, 4) },
126 { "Gib", 0.125 * pow(1024, 3) },
127 { "Mib", 0.125 * pow(1024, 2) },
128 { "Kib", 0.125 * 1024 },
129 { "Tb", 0.125 * 1e12 },
130 { "Gb", 0.125 * 1e9 },
131 { "Mb", 0.125 * 1e6 },
132 { "kb", 0.125 * 1e3 },
136 return surf_parse_get_value_with_unit(string, units);
139 double surf_parse_get_bandwidth(const char *string)
141 const struct unit_scale units[] = {
142 { "TiBps", pow(1024, 4) },
143 { "GiBps", pow(1024, 3) },
144 { "MiBps", pow(1024, 2) },
151 { "", 1.0 }, /* default unit is bytes per second */
152 { "Tibps", 0.125 * pow(1024, 4) },
153 { "Gibps", 0.125 * pow(1024, 3) },
154 { "Mibps", 0.125 * pow(1024, 2) },
155 { "Kibps", 0.125 * 1024 },
156 { "Tbps", 0.125 * 1e12 },
157 { "Gbps", 0.125 * 1e9 },
158 { "Mbps", 0.125 * 1e6 },
159 { "kbps", 0.125 * 1e3 },
163 return surf_parse_get_value_with_unit(string, units);
166 double surf_parse_get_power(const char *string)
168 const struct unit_scale units[] = {
169 { "yottaflops", 1e24 },
171 { "zettaflops", 1e21 },
173 { "exaflops", 1e18 },
175 { "petaflops", 1e15 },
177 { "teraflops", 1e12 },
179 { "gigaflops", 1e9 },
181 { "megaflops", 1e6 },
183 { "kiloflops", 1e3 },
187 { "", 1.0 }, /* default unit is flops */
190 return surf_parse_get_value_with_unit(string, units);
194 * All the callback lists that can be overridden anywhere.
195 * (this list should probably be reduced to the bare minimum to allow the models to work)
198 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
200 /* The default current property receiver. Setup in the corresponding opening callbacks. */
201 xbt_dict_t current_property_set = NULL;
202 xbt_dict_t as_current_property_set = NULL;
204 char* as_name_tab[1024];
205 void* as_dict_tab[1024];
209 /* dictionary of random generator data */
210 xbt_dict_t random_data_list = NULL;
212 YY_BUFFER_STATE surf_input_buffer;
213 FILE *surf_file_to_parse = NULL;
215 static void init_randomness(void);
216 static void add_randomness(void);
219 * Stuff relative to storage
221 void STag_surfxml_storage(void)
224 XBT_DEBUG("STag_surfxml_storage");
225 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
227 void ETag_surfxml_storage(void)
229 s_sg_platf_storage_cbarg_t storage;
230 memset(&storage,0,sizeof(storage));
232 storage.id = A_surfxml_storage_id;
233 storage.type_id = A_surfxml_storage_typeId;
234 storage.content = A_surfxml_storage_content;
235 storage.content_type = A_surfxml_storage_content___type;
236 storage.properties = current_property_set;
237 sg_platf_new_storage(&storage);
238 current_property_set = NULL;
240 void STag_surfxml_storage___type(void)
243 XBT_DEBUG("STag_surfxml_storage___type");
244 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
246 void ETag_surfxml_storage___type(void)
248 s_sg_platf_storage_type_cbarg_t storage_type;
249 memset(&storage_type,0,sizeof(storage_type));
251 storage_type.content = A_surfxml_storage___type_content;
252 storage_type.content_type = A_surfxml_storage___type_content___type;
253 storage_type.id = A_surfxml_storage___type_id;
254 storage_type.model = A_surfxml_storage___type_model;
255 storage_type.properties = current_property_set;
256 storage_type.size = surf_parse_get_size(A_surfxml_storage___type_size);
257 sg_platf_new_storage_type(&storage_type);
258 current_property_set = NULL;
260 void STag_surfxml_mstorage(void)
262 XBT_DEBUG("STag_surfxml_mstorage");
264 void ETag_surfxml_mstorage(void)
266 s_sg_platf_mstorage_cbarg_t mstorage;
267 memset(&mstorage,0,sizeof(mstorage));
269 mstorage.name = A_surfxml_mstorage_name;
270 mstorage.type_id = A_surfxml_mstorage_typeId;
271 sg_platf_new_mstorage(&mstorage);
273 void STag_surfxml_mount(void)
275 XBT_DEBUG("STag_surfxml_mount");
277 void ETag_surfxml_mount(void)
279 s_sg_platf_mount_cbarg_t mount;
280 memset(&mount,0,sizeof(mount));
282 mount.name = A_surfxml_mount_name;
283 mount.storageId = A_surfxml_mount_storageId;
284 sg_platf_new_mount(&mount);
288 * Stuff relative to the <include> tag
290 static xbt_dynar_t surf_input_buffer_stack = NULL;
291 static xbt_dynar_t surf_file_to_parse_stack = NULL;
292 static xbt_dynar_t surf_parsed_filename_stack = NULL;
294 void STag_surfxml_include(void)
296 XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
297 xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
298 surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
300 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
302 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
303 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
304 A_surfxml_include_file);
305 xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
306 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
307 surf_parse_push_buffer_state(surf_input_buffer);
312 void ETag_surfxml_include(void) {
313 /* Nothing to do when done with reading the include tag.
314 * Instead, the handling should be deferred until the EOF of current buffer -- see below */
317 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
319 * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
320 * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
321 * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
322 * error message in that case.
324 * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
325 * directly: a command line flag could instruct it to do the correct thing when #include is encountered
326 * on a line. One day maybe, if the maya allow it.
328 int ETag_surfxml_include_state(void)
331 XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
333 if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
336 // Yeah, we were in an <include> Restore state and proceed.
337 fclose(surf_file_to_parse);
338 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
339 surf_parse_pop_buffer_state();
340 xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
342 // Restore the filename for error messages
343 free(surf_parsed_filename);
344 xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
350 void surf_parse_init_callbacks(void)
355 void surf_parse_reset_callbacks(void)
357 surf_parse_free_callbacks();
358 surf_parse_init_callbacks();
361 void surf_parse_free_callbacks(void)
366 /* Stag and Etag parse functions */
368 void STag_surfxml_platform(void) {
369 _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
371 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
372 "You're using an ancient XML file.\n"
373 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
374 "instead of MBytes, MFlops and seconds.\n"
376 "Use simgrid_update_xml to update your file automatically. "
377 "This program is installed automatically with SimGrid, or "
378 "available in the tools/ directory of the source archive.\n"
380 "Please check also out the SURF section of the ChangeLog for "
381 "the 3.1 version for more information. \n"
383 "Last, do not forget to also update your values for "
384 "the calls to MSG_task_create (if any).");
385 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
386 "You're using an old XML file.\n"
387 "Use simgrid_update_xml to update your file automatically. "
388 "This program is installed automatically with SimGrid, or "
389 "available in the tools/ directory of the source archive.");
393 void ETag_surfxml_platform(void){
397 void STag_surfxml_host(void){
399 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
402 void STag_surfxml_prop(void)
405 if (!as_current_property_set){
406 xbt_assert(as_prop_nb < 1024, "Number of AS property reach the limit!!!");
407 as_current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
408 as_name_tab[as_prop_nb] = xbt_strdup(A_surfxml_AS_id);
409 as_dict_tab[as_prop_nb] = as_current_property_set;
410 XBT_DEBUG("PUSH prop %p for AS '%s'",as_dict_tab[as_prop_nb],as_name_tab[as_prop_nb]);
413 xbt_dict_set(as_current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
416 if (!current_property_set)
417 current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
418 xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
422 void ETag_surfxml_host(void) {
423 s_sg_platf_host_cbarg_t host;
425 memset(&host,0,sizeof(host));
428 host.properties = current_property_set;
430 host.id = A_surfxml_host_id;
432 buf = A_surfxml_host_power;
433 XBT_DEBUG("Buffer: %s", buf);
434 host.power_peak = xbt_dynar_new(sizeof(double), NULL);
435 if (strchr(buf, ',') == NULL){
436 double power_value = get_cpu_power(A_surfxml_host_power);
437 xbt_dynar_push_as(host.power_peak,double, power_value);
440 xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
442 for (i = 0; i < xbt_dynar_length(pstate_list); i++) {
444 char* power_value_str;
446 xbt_dynar_get_cpy(pstate_list, i, &power_value_str);
447 xbt_str_trim(power_value_str, NULL);
448 power_value = get_cpu_power(power_value_str);
449 xbt_dynar_push_as(host.power_peak, double, power_value);
450 XBT_DEBUG("Power value: %lf", power_value);
454 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
455 //host.power_peak = get_cpu_power(A_surfxml_host_power);
456 host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
457 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
458 host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file);
459 host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file);
460 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
462 xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
463 (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
464 if (A_surfxml_host_state == A_surfxml_host_state_ON)
465 host.initial_state = SURF_RESOURCE_ON;
466 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
467 host.initial_state = SURF_RESOURCE_OFF;
468 host.coord = A_surfxml_host_coordinates;
470 sg_platf_new_host(&host);
471 current_property_set = NULL;
474 void STag_surfxml_host___link(void){
475 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
476 s_sg_platf_host_link_cbarg_t host_link;
477 memset(&host_link,0,sizeof(host_link));
479 host_link.id = A_surfxml_host___link_id;
480 host_link.link_up = A_surfxml_host___link_up;
481 host_link.link_down = A_surfxml_host___link_down;
482 sg_platf_new_host_link(&host_link);
485 void STag_surfxml_router(void){
486 s_sg_platf_router_cbarg_t router;
487 memset(&router, 0, sizeof(router));
489 router.id = A_surfxml_router_id;
490 router.coord = A_surfxml_router_coordinates;
492 sg_platf_new_router(&router);
495 void ETag_surfxml_cluster(void){
496 s_sg_platf_cluster_cbarg_t cluster;
497 memset(&cluster,0,sizeof(cluster));
498 cluster.properties = current_property_set;
500 cluster.id = A_surfxml_cluster_id;
501 cluster.prefix = A_surfxml_cluster_prefix;
502 cluster.suffix = A_surfxml_cluster_suffix;
503 cluster.radical = A_surfxml_cluster_radical;
504 cluster.power = surf_parse_get_power(A_surfxml_cluster_power);
505 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
506 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw);
507 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat);
508 if(strcmp(A_surfxml_cluster_bb___bw,""))
509 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw);
510 if(strcmp(A_surfxml_cluster_bb___lat,""))
511 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat);
512 if(strcmp(A_surfxml_cluster_limiter___link,""))
513 cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
514 if(strcmp(A_surfxml_cluster_loopback___bw,""))
515 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw);
516 if(strcmp(A_surfxml_cluster_loopback___lat,""))
517 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat);
518 cluster.router_id = A_surfxml_cluster_router___id;
520 switch (AX_surfxml_cluster_sharing___policy) {
521 case A_surfxml_cluster_sharing___policy_SHARED:
522 cluster.sharing_policy = SURF_LINK_SHARED;
524 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
525 cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
527 case A_surfxml_cluster_sharing___policy_FATPIPE:
528 cluster.sharing_policy = SURF_LINK_FATPIPE;
531 surf_parse_error("Invalid cluster sharing policy for cluster %s",
535 switch (AX_surfxml_cluster_bb___sharing___policy) {
536 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
537 cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
539 case A_surfxml_cluster_bb___sharing___policy_SHARED:
540 cluster.bb_sharing_policy = SURF_LINK_SHARED;
543 surf_parse_error("Invalid bb sharing policy in cluster %s",
548 cluster.availability_trace = A_surfxml_cluster_availability___file;
549 cluster.state_trace = A_surfxml_cluster_state___file;
550 sg_platf_new_cluster(&cluster);
552 current_property_set = NULL;
555 void STag_surfxml_cluster(void){
556 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
559 void STag_surfxml_cabinet(void){
560 s_sg_platf_cabinet_cbarg_t cabinet;
561 memset(&cabinet,0,sizeof(cabinet));
562 cabinet.id = A_surfxml_cabinet_id;
563 cabinet.prefix = A_surfxml_cabinet_prefix;
564 cabinet.suffix = A_surfxml_cabinet_suffix;
565 cabinet.power = surf_parse_get_power(A_surfxml_cabinet_power);
566 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw);
567 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat);
568 cabinet.radical = A_surfxml_cabinet_radical;
570 sg_platf_new_cabinet(&cabinet);
573 void STag_surfxml_peer(void){
574 s_sg_platf_peer_cbarg_t peer;
575 memset(&peer,0,sizeof(peer));
576 peer.id = A_surfxml_peer_id;
577 peer.power = surf_parse_get_power(A_surfxml_peer_power);
578 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in);
579 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out);
580 peer.lat = surf_parse_get_time(A_surfxml_peer_lat);
581 peer.coord = A_surfxml_peer_coordinates;
582 peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
583 peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
585 sg_platf_new_peer(&peer);
588 void STag_surfxml_link(void){
590 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
593 void ETag_surfxml_link(void){
594 s_sg_platf_link_cbarg_t link;
595 memset(&link,0,sizeof(link));
597 link.properties = current_property_set;
599 link.id = A_surfxml_link_id;
600 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth);
601 //printf("Link bandwidth [%lg]\n", link.bandwidth);
602 link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
603 link.latency = surf_parse_get_time(A_surfxml_link_latency);
604 //printf("Link latency [%lg]\n", link.latency);
605 link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
607 switch (A_surfxml_link_state) {
608 case A_surfxml_link_state_ON:
609 link.state = SURF_RESOURCE_ON;
611 case A_surfxml_link_state_OFF:
612 link.state = SURF_RESOURCE_OFF;
615 surf_parse_error("invalid state for link %s", link.id);
618 link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file);
620 switch (A_surfxml_link_sharing___policy) {
621 case A_surfxml_link_sharing___policy_SHARED:
622 link.policy = SURF_LINK_SHARED;
624 case A_surfxml_link_sharing___policy_FATPIPE:
625 link.policy = SURF_LINK_FATPIPE;
627 case A_surfxml_link_sharing___policy_FULLDUPLEX:
628 link.policy = SURF_LINK_FULLDUPLEX;
631 surf_parse_error("Invalid sharing policy in link %s", link.id);
635 sg_platf_new_link(&link);
637 current_property_set = NULL;
640 void STag_surfxml_link___ctn(void){
643 switch (A_surfxml_link___ctn_direction) {
644 case AU_surfxml_link___ctn_direction:
645 case A_surfxml_link___ctn_direction_NONE:
646 link_id = xbt_strdup(A_surfxml_link___ctn_id);
648 case A_surfxml_link___ctn_direction_UP:
649 link_id = bprintf("%s_UP", A_surfxml_link___ctn_id);
651 case A_surfxml_link___ctn_direction_DOWN:
652 link_id = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
656 // FIXME we should push the surf link object but it don't
657 // work because of model rulebased
658 xbt_dynar_push(parsed_link_list, &link_id);
661 void ETag_surfxml_backbone(void){
662 s_sg_platf_link_cbarg_t link;
663 memset(&link,0,sizeof(link));
665 link.properties = NULL;
667 link.id = A_surfxml_backbone_id;
668 link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth);
669 link.latency = surf_parse_get_time(A_surfxml_backbone_latency);
670 link.state = SURF_RESOURCE_ON;
671 link.policy = SURF_LINK_SHARED;
673 sg_platf_new_link(&link);
674 routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
677 void STag_surfxml_route(void){
678 xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
679 "Missing end-points while defining route \"%s\"->\"%s\"",
680 A_surfxml_route_src, A_surfxml_route_dst);
681 parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
684 void STag_surfxml_ASroute(void){
685 xbt_assert(strlen(A_surfxml_ASroute_src) > 0
686 || strlen(A_surfxml_ASroute_dst) > 0
687 || strlen(A_surfxml_ASroute_gw___src) > 0
688 || strlen(A_surfxml_ASroute_gw___dst) > 0,
689 "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
690 A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
691 A_surfxml_ASroute_gw___src, A_surfxml_ASroute_gw___dst);
692 parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
695 void STag_surfxml_bypassRoute(void){
696 xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0
697 || strlen(A_surfxml_bypassRoute_dst) > 0,
698 "Missing end-points while defining bupass route \"%s\"->\"%s\"",
699 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
700 parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
703 void STag_surfxml_bypassASroute(void){
704 xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0
705 || strlen(A_surfxml_bypassASroute_dst) > 0
706 || strlen(A_surfxml_bypassASroute_gw___src) > 0
707 || strlen(A_surfxml_bypassASroute_gw___dst) > 0,
708 "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
709 A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
710 A_surfxml_bypassASroute_gw___src,A_surfxml_bypassASroute_gw___dst);
711 parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
714 void ETag_surfxml_route(void){
715 s_sg_platf_route_cbarg_t route;
716 memset(&route,0,sizeof(route));
718 route.src = A_surfxml_route_src;
719 route.dst = A_surfxml_route_dst;
722 route.link_list = parsed_link_list;
724 switch (A_surfxml_route_symmetrical) {
725 case AU_surfxml_route_symmetrical:
726 case A_surfxml_route_symmetrical_YES:
727 route.symmetrical = TRUE;
729 case A_surfxml_route_symmetrical_NO:
730 route.symmetrical = FALSE;;
734 sg_platf_new_route(&route);
735 parsed_link_list = NULL;
738 void ETag_surfxml_ASroute(void){
739 s_sg_platf_route_cbarg_t ASroute;
740 memset(&ASroute,0,sizeof(ASroute));
742 ASroute.src = A_surfxml_ASroute_src;
743 ASroute.dst = A_surfxml_ASroute_dst;
745 ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___src);
746 ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___dst);
748 ASroute.link_list = parsed_link_list;
750 switch (A_surfxml_ASroute_symmetrical) {
751 case AU_surfxml_ASroute_symmetrical:
752 case A_surfxml_ASroute_symmetrical_YES:
753 ASroute.symmetrical = TRUE;
755 case A_surfxml_ASroute_symmetrical_NO:
756 ASroute.symmetrical = FALSE;
760 sg_platf_new_ASroute(&ASroute);
761 parsed_link_list = NULL;
764 void ETag_surfxml_bypassRoute(void){
765 s_sg_platf_route_cbarg_t route;
766 memset(&route,0,sizeof(route));
768 route.src = A_surfxml_bypassRoute_src;
769 route.dst = A_surfxml_bypassRoute_dst;
772 route.link_list = parsed_link_list;
773 route.symmetrical = FALSE;
775 sg_platf_new_bypassRoute(&route);
776 parsed_link_list = NULL;
779 void ETag_surfxml_bypassASroute(void){
780 s_sg_platf_route_cbarg_t ASroute;
781 memset(&ASroute,0,sizeof(ASroute));
783 ASroute.src = A_surfxml_bypassASroute_src;
784 ASroute.dst = A_surfxml_bypassASroute_dst;
785 ASroute.link_list = parsed_link_list;
786 ASroute.symmetrical = FALSE;
788 ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___src);
789 ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
791 sg_platf_new_bypassASroute(&ASroute);
792 parsed_link_list = NULL;
795 void ETag_surfxml_trace(void){
796 s_sg_platf_trace_cbarg_t trace;
797 memset(&trace,0,sizeof(trace));
799 trace.id = A_surfxml_trace_id;
800 trace.file = A_surfxml_trace_file;
801 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
802 trace.pc_data = surfxml_pcdata;
804 sg_platf_new_trace(&trace);
807 void STag_surfxml_trace___connect(void){
808 s_sg_platf_trace_connect_cbarg_t trace_connect;
809 memset(&trace_connect,0,sizeof(trace_connect));
811 trace_connect.element = A_surfxml_trace___connect_element;
812 trace_connect.trace = A_surfxml_trace___connect_trace;
814 switch (A_surfxml_trace___connect_kind) {
815 case AU_surfxml_trace___connect_kind:
816 case A_surfxml_trace___connect_kind_POWER:
817 trace_connect.kind = SURF_TRACE_CONNECT_KIND_POWER;
819 case A_surfxml_trace___connect_kind_BANDWIDTH:
820 trace_connect.kind = SURF_TRACE_CONNECT_KIND_BANDWIDTH;
822 case A_surfxml_trace___connect_kind_HOST___AVAIL:
823 trace_connect.kind = SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
825 case A_surfxml_trace___connect_kind_LATENCY:
826 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LATENCY;
828 case A_surfxml_trace___connect_kind_LINK___AVAIL:
829 trace_connect.kind = SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
832 sg_platf_trace_connect(&trace_connect);
835 void STag_surfxml_AS(void){
837 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
838 AS.id = A_surfxml_AS_id;
839 AS.routing = (int)A_surfxml_AS_routing;
841 as_current_property_set = NULL;
843 sg_platf_new_AS_begin(&AS);
845 void ETag_surfxml_AS(void){
847 char *name = as_name_tab[as_prop_nb-1];
848 xbt_dict_t dict = as_dict_tab[as_prop_nb-1];
850 XBT_DEBUG("POP prop %p for AS '%s'",dict,name);
851 xbt_lib_set(as_router_lib,
853 ROUTING_PROP_ASR_LEVEL,
857 sg_platf_new_AS_end();
860 extern int _sg_init_status; /* FIXME: find a proper way to export this at some point */
862 void STag_surfxml_config(void){
864 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
865 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
866 if (_sg_init_status == 2) {
867 surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
870 void ETag_surfxml_config(void){
871 xbt_dict_cursor_t cursor = NULL;
875 xbt_dict_foreach(current_property_set, cursor, key, elem) {
876 cfg = bprintf("%s:%s",key,elem);
877 if(xbt_cfg_is_default_value(_sg_cfg_set, key))
878 xbt_cfg_set_parse(_sg_cfg_set, cfg);
880 XBT_INFO("The custom configuration '%s' is already defined by user!",key);
883 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
884 xbt_dict_free(¤t_property_set);
885 current_property_set = NULL;
891 void STag_surfxml_process(void){
894 argv = xbt_new(char *, 1);
895 argv[0] = xbt_strdup(A_surfxml_process_function);
896 xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
899 void ETag_surfxml_process(void){
900 s_sg_platf_process_cbarg_t process;
901 memset(&process,0,sizeof(process));
904 process.argv = (const char **)argv;
905 process.properties = current_property_set;
906 process.host = A_surfxml_process_host;
907 process.function = A_surfxml_process_function;
908 process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
909 process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
911 switch (A_surfxml_process_on___failure) {
912 case AU_surfxml_process_on___failure:
913 case A_surfxml_process_on___failure_DIE:
914 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
916 case A_surfxml_process_on___failure_RESTART:
917 process.on_failure = SURF_PROCESS_ON_FAILURE_RESTART;
921 sg_platf_new_process(&process);
922 current_property_set = NULL;
925 void STag_surfxml_argument(void){
927 argv = xbt_realloc(argv, (argc) * sizeof(char *));
928 argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
931 /* ***************************************** */
932 /* TUTORIAL: New TAG */
933 void STag_surfxml_gpu(void)
935 XBT_DEBUG("STag_surfxml_gpu");
937 void ETag_surfxml_gpu(void)
939 s_sg_platf_gpu_cbarg_t gpu;
940 memset(&gpu,0,sizeof(gpu));
942 gpu.name = A_surfxml_gpu_name;
944 sg_platf_new_gpu(&gpu);
946 /* ***************************************** */
948 /* nothing to do in those functions */
949 void ETag_surfxml_prop(void){}
950 void STag_surfxml_random(void){}
951 void ETag_surfxml_random(void){}
952 void ETag_surfxml_trace___connect(void){}
953 void STag_surfxml_trace(void){}
954 void ETag_surfxml_router(void){}
955 void ETag_surfxml_host___link(void){}
956 void ETag_surfxml_cabinet(void){}
957 void ETag_surfxml_peer(void){}
958 void STag_surfxml_backbone(void){}
959 void ETag_surfxml_link___ctn(void){}
960 void ETag_surfxml_argument(void){}
962 /* Open and Close parse file */
964 void surf_parse_open(const char *file)
966 static int warned = 0; /* warn only once */
970 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
971 "If it is not what you want, go fix your code.");
977 if (!surf_input_buffer_stack)
978 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
979 if (!surf_file_to_parse_stack)
980 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
982 if (!surf_parsed_filename_stack)
983 surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
984 surf_parsed_filename = xbt_strdup(file);
986 surf_file_to_parse = surf_fopen(file, "r");
987 xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
988 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
989 surf_parse__switch_to_buffer(surf_input_buffer);
990 surf_parse_lineno = 1;
993 void surf_parse_close(void)
995 xbt_dynar_free(&surf_input_buffer_stack);
996 xbt_dynar_free(&surf_file_to_parse_stack);
997 xbt_dynar_free(&surf_parsed_filename_stack);
999 free(surf_parsed_filename);
1000 surf_parsed_filename = NULL;
1002 if (surf_file_to_parse) {
1003 surf_parse__delete_buffer(surf_input_buffer);
1004 fclose(surf_file_to_parse);
1005 surf_file_to_parse = NULL; //Must be reset for Bypass
1009 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1011 static int _surf_parse(void) {
1012 return surf_parse_lex();
1014 int_f_void_t surf_parse = _surf_parse;
1017 /* Prop tag functions */
1023 /* Random tag functions */
1025 double get_cpu_power(const char *power)
1027 double power_scale = 0.0;
1030 random_data_t random = NULL;
1031 /* randomness is inserted like this: power="$rand(my_random)" */
1032 if (((p = strstr(power, "$rand(")) != NULL)
1033 && ((q = strstr(power, ")")) != NULL)) {
1035 generator = xbt_malloc(q - (p + 6) + 1);
1036 memcpy(generator, p + 6, q - (p + 6));
1037 generator[q - (p + 6)] = '\0';
1038 random = xbt_dict_get_or_null(random_data_list, generator);
1039 xbt_assert(random, "Random generator %s undefined", generator);
1040 power_scale = random_generate(random);
1043 power_scale = surf_parse_get_power(power);
1048 double random_min, random_max, random_mean, random_std_deviation;
1049 e_random_generator_t random_generator;
1052 static void init_randomness(void)
1054 random_id = A_surfxml_random_id;
1055 random_min = surf_parse_get_double(A_surfxml_random_min);
1056 random_max = surf_parse_get_double(A_surfxml_random_max);
1057 random_mean = surf_parse_get_double(A_surfxml_random_mean);
1058 random_std_deviation = surf_parse_get_double(A_surfxml_random_std___deviation);
1059 switch (A_surfxml_random_generator) {
1060 case AU_surfxml_random_generator:
1061 case A_surfxml_random_generator_NONE:
1062 random_generator = NONE;
1064 case A_surfxml_random_generator_DRAND48:
1065 random_generator = DRAND48;
1067 case A_surfxml_random_generator_RAND:
1068 random_generator = RAND;
1070 case A_surfxml_random_generator_RNGSTREAM:
1071 random_generator = RNGSTREAM;
1074 surf_parse_error("Invalid random generator");
1079 static void add_randomness(void)
1081 /* If needed aditional properties can be added by using the prop tag */
1082 random_data_t random =
1083 random_new(random_generator, 0, random_min, random_max, random_mean,
1084 random_std_deviation);
1085 xbt_dict_set(random_data_list, random_id, (void *) random,
1090 xbt_dict_t get_as_router_properties(const char* name)
1092 return xbt_lib_get_or_null(as_router_lib, name, ROUTING_PROP_ASR_LEVEL);