From: quasar Date: Fri, 9 Nov 2007 08:46:51 +0000 (+0000) Subject: adding modifications for 1 pass & for adding routes only when platform end tag reached X-Git-Tag: v3.3~815 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/bea6786798593b78e0d62a2308eacad3284d4537?hp=a7740c25f68f21ce361c63ab639a7fe3cd18042b adding modifications for 1 pass & for adding routes only when platform end tag reached git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5009 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/include/xbt/str.h b/include/xbt/str.h index 49e99fcec9..d51177bb50 100644 --- a/include/xbt/str.h +++ b/include/xbt/str.h @@ -61,6 +61,8 @@ XBT_PUBLIC(void) xbt_str_trim(char* s, const char* char_list); XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep); XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted(const char *s); +XBT_PUBLIC(xbt_dynar_t) xbt_str_split_str(const char *s, const char *sep); + XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep); /* */ diff --git a/src/include/surf/surf.h b/src/include/surf/surf.h index 8ac134c418..a7f7c2a50e 100644 --- a/src/include/surf/surf.h +++ b/src/include/surf/surf.h @@ -549,6 +549,14 @@ XBT_PUBLIC_DATA(xbt_dict_t) current_property_set; /* the prop set for the curren void parse_properties(void); void free_string(void*); +/* Prototypes for functions handling routing and were factorized succesfully from the models */ +void init_route_table(void); +void make_route_table(void); +void parse_route_elem(void); + +/* surf parse file related */ +void parse_platform_file(const char* file); + SG_END_DECL() #endif /* _SURF_SURF_H */ diff --git a/src/simdag/sd_global.c b/src/simdag/sd_global.c index a7950c95c9..df22d65a14 100644 --- a/src/simdag/sd_global.c +++ b/src/simdag/sd_global.c @@ -359,6 +359,8 @@ void SD_create_environment(const char *platform_file) { surf_workstation_model_description[workstation_id]. model_init(platform_file); + parse_platform_file(platform_file); + _sd_init_status = 2; /* now let's create the SD wrappers for workstations and links */ diff --git a/src/simgrid_units_main.c b/src/simgrid_units_main.c index 281291993e..b0bd7c4109 100644 --- a/src/simgrid_units_main.c +++ b/src/simgrid_units_main.c @@ -48,6 +48,7 @@ extern xbt_test_unit_t _xbt_current_unit; /* SGU: BEGIN FILE xbt/xbt_str.c */ void test_split_quoted(void); + void test_split_str(void); /* SGU: END FILE */ /* SGU: BEGIN FILE xbt/config.c */ @@ -113,6 +114,7 @@ int main(int argc, char *argv[]) { /* SGU: BEGIN FILE xbt/xbt_str.c */ suite = xbt_test_suite_by_name("xbt_str","String Handling"); xbt_test_suite_push(suite, "xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted"); + xbt_test_suite_push(suite, "xbt_str_split_str", test_split_str, "test the function xbt_str_split_str"); /* SGU: END FILE */ /* SGU: BEGIN FILE xbt/config.c */ diff --git a/src/simix/smx_deployment.c b/src/simix/smx_deployment.c index f92d8e36f4..2ca297f52f 100644 --- a/src/simix/smx_deployment.c +++ b/src/simix/smx_deployment.c @@ -122,6 +122,7 @@ void SIMIX_launch_application(const char *file) { xbt_assert0(simix_global, "SIMIX_global_init has to be called before SIMIX_launch_application."); + surf_parse_reset_parser(); surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init); surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument); surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties); diff --git a/src/simix/smx_environment.c b/src/simix/smx_environment.c index 65ef32941a..eed850b36b 100644 --- a/src/simix/smx_environment.c +++ b/src/simix/smx_environment.c @@ -9,6 +9,7 @@ #include "private.h" #include "xbt/sysdep.h" #include "xbt/log.h" + XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_environment, simix, "Logging specific to SIMIX (environment)"); @@ -93,11 +94,15 @@ void SIMIX_create_environment(const char *file) surf_cpu_model_description[cpu_id].model_init(file); surf_network_model_description[network_id].model_init(file); + } surf_workstation_model_description[workstation_id]. model_init(file); + /* Parse the platform file */ + parse_platform_file(file); + _simix_init_status = 2; /* inited; don't change settings now */ xbt_dict_foreach(workstation_set, cursor, name, workstation) { diff --git a/src/surf/cpu.c b/src/surf/cpu.c index 75b84d589b..3d2bdc7e51 100644 --- a/src/surf/cpu.c +++ b/src/surf/cpu.c @@ -88,15 +88,11 @@ static void parse_cpu_init(void) } -static void parse_file(const char *file) +static void define_callbacks(const char *file) { surf_parse_reset_parser(); surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init); - surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties); - - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); + surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties); } static void *name_service(const char *name) @@ -454,6 +450,6 @@ void surf_cpu_model_init_Cas01(const char *filename) if (surf_cpu_model) return; surf_cpu_model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_cpu_model); } diff --git a/src/surf/network.c b/src/surf/network.c index 77615d7332..42f92ce719 100644 --- a/src/surf/network.c +++ b/src/surf/network.c @@ -19,6 +19,7 @@ xbt_dict_t link_set = NULL; xbt_dict_t network_card_set = NULL; int card_number = 0; +int host_number = 0; link_CM02_t **routing_table = NULL; int *routing_table_size = NULL; static link_CM02_t loopback = NULL; @@ -26,8 +27,8 @@ static link_CM02_t loopback = NULL; static void create_routing_table(void) { routing_table = - xbt_new0(link_CM02_t *, card_number * card_number); - routing_table_size = xbt_new0(int, card_number * card_number); + xbt_new0(link_CM02_t *, /*card_number * card_number */ host_number * host_number); + routing_table_size = xbt_new0(int, /*card_number * card_number*/ host_number * host_number); } static void link_free(void *nw_link) @@ -152,72 +153,32 @@ static void parse_link_init(void) } -static int nb_link; -static int link_list_capacity; -static link_CM02_t *link_list = NULL; static int src_id = -1; static int dst_id = -1; static void parse_route_set_endpoints(void) { - src_id = network_card_new(A_surfxml_route_src); dst_id = network_card_new(A_surfxml_route_dst); - nb_link = 0; - link_list_capacity = 20; - link_list = xbt_new(link_CM02_t, link_list_capacity); -} -static void parse_route_elem(void) -{ - - if (nb_link == link_list_capacity) { - link_list_capacity *= 2; - link_list = - xbt_realloc(link_list, - (link_list_capacity) * sizeof(link_CM02_t)); - } - link_list[nb_link++] = - xbt_dict_get_or_null(link_set, A_surfxml_link_c_ctn_id); + route_link_list = xbt_dynar_new(sizeof(char *), &free_string); } static void parse_route_set_route(void) { - route_new(src_id, dst_id, link_list, nb_link); + char *name; + if (src_id != -1 && dst_id != -1) { + name = bprintf("%d##%d",src_id, dst_id); + xbt_dict_set(route_table, name, route_link_list, NULL); + free(name); + } } -static void parse_file(const char *file) +static void add_loopback() { int i; - /* Figuring out the network links */ - surf_parse_reset_parser(); - surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init); - surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties); - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); - - /* Figuring out the network cards used */ - surf_parse_reset_parser(); - surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); - - create_routing_table(); - - /* Building the routes */ - surf_parse_reset_parser(); - surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); - surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem); - surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route); - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); - /* Adding loopback if needed */ - - for (i = 0; i < card_number; i++) + for (i = 0; i < host_number; i++) if (!ROUTE_SIZE(i, i)) { if (!loopback) loopback = link_new(xbt_strdup("__MSG_loopback__"), @@ -230,6 +191,54 @@ static void parse_file(const char *file) } } +static void add_route() +{ + xbt_ex_t e; + int nb_link = 0; + int cpt = 0; + int link_list_capacity = 0; + link_CM02_t *link_list = NULL; + + if (routing_table == NULL) create_routing_table(); + + link_list_capacity = xbt_dynar_length(links); + link_list = xbt_new(link_CM02_t, link_list_capacity); + + src_id = atoi(xbt_dynar_get_as(keys, 0, char*)); + dst_id = atoi(xbt_dynar_get_as(keys, 1, char*)); + + char* link = NULL; + xbt_dynar_foreach (links, cpt, link) { + TRY { + link_list[nb_link++] = xbt_dict_get(link_set, link); + } + CATCH(e) { + RETHROW1("Link %s not found (dict raised this exception: %s)", link); + } + } + route_new(src_id, dst_id, link_list, nb_link); + +} + +static void count_hosts(void) +{ + host_number++; +} + +static void define_callbacks(const char *file) +{ + /* Figuring out the network links */ + surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts); + surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init); + surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties); + surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); + surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem); + surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route); + surfxml_add_callback(STag_surfxml_platform_cb_list, &init_route_table); + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route); + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback); +} + static void *name_service(const char *name) { network_card_CM02_t card = xbt_dict_get_or_null(network_card_set, name); @@ -695,7 +704,7 @@ void surf_network_model_init_CM02(const char *filename) if (surf_network_model) return; surf_network_model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_network_model); network_solve = lmm_solve; @@ -710,7 +719,7 @@ void surf_network_model_init_Reno(const char *filename) if (surf_network_model) return; surf_network_model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_network_model); lmm_set_default_protocol_function(func_reno_f, func_reno_fp, @@ -728,7 +737,7 @@ void surf_network_model_init_Vegas(const char *filename) if (surf_network_model) return; surf_network_model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_network_model); lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, @@ -747,7 +756,7 @@ void surf_network_model_init_SDP(const char *filename) if (surf_network_model) return; surf_network_model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_network_model); network_solve = sdp_solve; diff --git a/src/surf/network_gtnets.c b/src/surf/network_gtnets.c index 361e9d79d7..02d05c8f59 100644 --- a/src/surf/network_gtnets.c +++ b/src/surf/network_gtnets.c @@ -219,8 +219,11 @@ static void parse_route_set_endpoints(void) { src_id = network_card_new(A_surfxml_route_src); dst_id = network_card_new(A_surfxml_route_dst); - nb_link = 0; + +/* nb_link = 0; link_name = NULL; +*/ + route_link_list = xbt_dynar_new(sizeof(char *), &free_string); } /* KF*/ @@ -234,21 +237,28 @@ static void parse_route_set_routers(void) } } +//The following is common to all and has been relocated to surfxml_parse /* Parses a route element from the XML: UNMODIFIED BY HC */ -static void parse_route_elem(void) +/*static void parse_route_elem(void) { nb_link++; link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *)); link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name); } +*/ /* Create the route (more than one hops): MODIFIED BY KF */ static void parse_route_set_route(void) { - if (nb_link > 1) +/* if (nb_link > 1) route_new(src_id, dst_id, link_name, nb_link); +*/ + name = bprintf("%d##%d",src_id, dst_id); + xbt_dict_set(route_table, name, route_link_list, NULL); + free(name); } +//This is not used anymore. one hop routes are created in add_route /* Create the one-hope route: BY KF */ static void parse_route_set_onehop_route(void) { @@ -256,23 +266,45 @@ static void parse_route_set_onehop_route(void) route_onehop_new(src_id, dst_id, link_name, nb_link); } -/* Main XML parsing */ -static void parse_file(const char *file) +static void add_route() { - surf_parse_reset_parser(); + xbt_ex_t e; + int cpt = 0; + int i = 0; + + nb_link = xbt_dynar_length(links); + link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *)); + src_id = atoi(xbt_dynar_get_as(keys, 0, char*)); + dst_id = atoi(xbt_dynar_get_as(keys, 1, char*)); + + i = 0; + char* link = NULL; + xbt_dynar_foreach (links, cpt, link) { + TRY { + link_name[i++] = xbt_dict_get(link_set, link); + } + CATCH(e) { + RETHROW1("Link %s not found (dict raised this exception: %s)", link); + } + } + if (nb_link > 1) + route_new(src_id, dst_id, link_name, nb_link); + if (nb_link == 1) + route_onehop_new(src_id, dst_id, link_name, nb_link); +} + +/* Main XML parsing */ +static void define_callbacks(const char *file) +{ surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties); surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers); surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init); surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); surfxml_add_callback(ETag_surfxml_route_element_cb_list, &parse_route_elem); - surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_onehop_route); +/* surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_onehop_route);*/ surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route); - - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); - + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route); } static void *name_service(const char *name) @@ -589,7 +621,7 @@ void surf_network_model_init_GTNETS(const char *filename) if (surf_network_model) return; surf_network_model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_network_model); update_model_description(surf_network_model_description, diff --git a/src/surf/surf.c b/src/surf/surf.c index fe4348c8de..637ebf2b5e 100644 --- a/src/surf/surf.c +++ b/src/surf/surf.c @@ -418,6 +418,7 @@ void surf_exit(void) path_name = NULL; } surf_parse_free_callbacks(); + xbt_dict_free(&route_table); NOW=0; /* Just in case the user plans to restart the simulation afterward */ xbt_exit(); } diff --git a/src/surf/surfxml_parse.c b/src/surf/surfxml_parse.c index 955057c970..49fd6ac65e 100644 --- a/src/surf/surfxml_parse.c +++ b/src/surf/surfxml_parse.c @@ -37,7 +37,12 @@ xbt_dynar_t STag_surfxml_prop_cb_list = NULL; xbt_dynar_t ETag_surfxml_prop_cb_list = NULL; xbt_dict_t current_property_set = NULL; - + +xbt_dict_t route_table = NULL; +xbt_dynar_t route_link_list = NULL; +xbt_dynar_t links = NULL; +xbt_dynar_t keys = NULL; + static xbt_dynar_t surf_input_buffer_stack = NULL; static xbt_dynar_t surf_file_to_parse_stack = NULL; @@ -46,6 +51,23 @@ static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t); YY_BUFFER_STATE surf_input_buffer; FILE *surf_file_to_parse; +void make_route_table(void) +{ + xbt_dict_cursor_t cursor = NULL; + char *key,*data; + const char *sep = "##"; + + xbt_dict_foreach(route_table, cursor, key, data) { + links = (xbt_dynar_t)data; + keys = xbt_str_split_str(key, sep); + surfxml_call_cb_functions(ETag_surfxml_platform_cb_list); + } + + xbt_dict_free(&route_table); + xbt_dynar_free(&links); + +} + void surf_parse_free_callbacks(void) { xbt_dynar_free(&STag_surfxml_platform_cb_list); @@ -145,7 +167,7 @@ void STag_surfxml_platform(void) void ETag_surfxml_platform(void) { - surfxml_call_cb_functions(ETag_surfxml_platform_cb_list); + if (route_table != NULL) make_route_table(); } void STag_surfxml_host(void) @@ -319,3 +341,24 @@ static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list) (*fun)(); } } + +void init_route_table(void) +{ + xbt_dict_free(&route_table); + xbt_dynar_free(&route_link_list); + route_table = xbt_dict_new(); +} + +void parse_route_elem(void) +{ + char *val; + val = xbt_strdup(A_surfxml_link_c_ctn_id); + xbt_dynar_push(route_link_list, &val); +} + +void parse_platform_file(const char* file) +{ + surf_parse_open(file); + xbt_assert1((!surf_parse()), "Parse error in %s", file); + surf_parse_close(); +} diff --git a/src/surf/workstation_KCCFLN05.c b/src/surf/workstation_KCCFLN05.c index 727c460e44..0bd1ea4d92 100644 --- a/src/surf/workstation_KCCFLN05.c +++ b/src/surf/workstation_KCCFLN05.c @@ -996,6 +996,7 @@ static void parse_cpu_init(void) surf_parse_get_double(&max_outgoing_rate, A_surfxml_host_max_outgoing_rate); current_property_set = xbt_dict_new(); + cpu_new(A_surfxml_host_id, power_scale, power_initial, power_trace, state_initial, state_trace, interference_send, interference_recv, interference_send_recv, max_outgoing_rate,current_property_set); @@ -1107,27 +1108,19 @@ static void route_new(int src_id, int dst_id, route->impact_on_dst = impact_on_dst; route->impact_on_src_with_other_recv = impact_on_src_with_other_recv; route->impact_on_dst_with_other_send = impact_on_dst_with_other_send; - } -static int nb_link; -static int link_list_capacity; -static link_KCCFLN05_t *link_list = NULL; static int src_id = -1; static int dst_id = -1; static double impact_on_src; static double impact_on_dst; static double impact_on_src_with_other_recv; static double impact_on_dst_with_other_send; -static int is_first = 1; static void parse_route_set_endpoints(void) { cpu_KCCFLN05_t cpu_tmp = NULL; - if (is_first) create_routing_table(); - is_first = 0; - cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_src); if (cpu_tmp != NULL) { src_id = cpu_tmp->id; @@ -1157,46 +1150,76 @@ static void parse_route_set_endpoints(void) surf_parse_get_double(&impact_on_dst_with_other_send, A_surfxml_route_impact_on_dst_with_other_send); - nb_link = 0; - link_list_capacity = 1; - link_list = xbt_new(link_KCCFLN05_t, link_list_capacity); - + route_link_list = xbt_dynar_new(sizeof(char *), &free_string); } -static void parse_route_elem(void) +static void parse_route_set_route(void) { - xbt_ex_t e; - if (nb_link == link_list_capacity) { - link_list_capacity *= 2; - link_list = - xbt_realloc(link_list, - (link_list_capacity) * - sizeof(link_KCCFLN05_t)); - } - TRY { - link_list[nb_link++] = - xbt_dict_get(link_set, A_surfxml_link_c_ctn_id); - } - CATCH(e) { - RETHROW1("Link %s not found (dict raised this exception: %s)", - A_surfxml_link_c_ctn_id); + char* name; + if (src_id != -1 && dst_id != -1) { + name = bprintf("%d##%d##%lf##%lf##%lf##%lf",src_id, dst_id,impact_on_src, + impact_on_dst, impact_on_src_with_other_recv, + impact_on_dst_with_other_send); + xbt_dict_set(route_table, name, route_link_list, NULL); + free(name); } } -static void parse_route_set_route(void) +static void add_loopback() { - if (src_id != -1 && dst_id != -1) - route_new(src_id, dst_id, link_list, nb_link, impact_on_src, + int i; + /* Adding loopback if needed */ + for (i = 0; i < nb_workstation; i++) { + if (!ROUTE(i, i).size) { + if (!loopback) + loopback = link_new(xbt_strdup("__MSG_loopback__"), + 498000000, NULL, 0.000015, NULL, + SURF_LINK_ON, NULL, + SURF_LINK_FATPIPE, NULL); + ROUTE(i, i).size = 1; + ROUTE(i, i).links = xbt_new0(link_KCCFLN05_t, 1); + ROUTE(i, i).links[0] = loopback; + } + } +} + +static void add_route() +{ + xbt_ex_t e; + int nb_link = 0; + int cpt = 0; + int link_list_capacity = 0; + link_KCCFLN05_t *link_list = NULL; + + if (routing_table == NULL) create_routing_table(); + + link_list_capacity = xbt_dynar_length(links); + link_list = xbt_new(link_KCCFLN05_t, link_list_capacity); + + src_id = atoi(xbt_dynar_get_as(keys, 0, char*)); + dst_id = atoi(xbt_dynar_get_as(keys, 1, char*)); + impact_on_src = atof(xbt_dynar_get_as(keys, 2, char*)); + impact_on_dst = atof(xbt_dynar_get_as(keys, 3, char*)); + impact_on_src_with_other_recv = atof(xbt_dynar_get_as(keys, 4, char*)); + impact_on_dst_with_other_send = atof(xbt_dynar_get_as(keys, 5, char*)); + + char* link = NULL; + xbt_dynar_foreach (links, cpt, link) { + TRY { + link_list[nb_link++] = xbt_dict_get(link_set, link); + } + CATCH(e) { + RETHROW1("Link %s not found (dict raised this exception: %s)", link); + } + } + route_new(src_id, dst_id, link_list, nb_link,impact_on_src, impact_on_dst, impact_on_src_with_other_recv, impact_on_dst_with_other_send); } - -static void parse_file(const char *file) +static void define_callbacks(const char *file) { - int i; - CDEBUG0(surf_parse, "Use the KCCFKN05 model"); /* Adding callback functions */ @@ -1208,25 +1231,9 @@ static void parse_file(const char *file) surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem); surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route); - - /* Parse the file */ - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); - - /* Adding loopback if needed */ - for (i = 0; i < nb_workstation; i++) - if (!ROUTE(i, i).size) { - if (!loopback) - loopback = link_new(xbt_strdup("__MSG_loopback__"), - 498000000, NULL, 0.000015, NULL, - SURF_LINK_ON, NULL, - SURF_LINK_FATPIPE, NULL); - ROUTE(i, i).size = 1; - ROUTE(i, i).links = xbt_new0(link_KCCFLN05_t, 1); - ROUTE(i, i).links[0] = loopback; - } - + surfxml_add_callback(STag_surfxml_platform_cb_list, &init_route_table); + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route); + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback); } /**************************************/ @@ -1331,7 +1338,7 @@ void surf_workstation_model_init_KCCFLN05(const char *filename) xbt_assert0(!surf_network_model, "network model type already defined"); model_init_internal(); - parse_file(filename); + define_callbacks(filename); xbt_dynar_push(model_list, &surf_workstation_model); } diff --git a/src/surf/workstation_ptask_L07.c b/src/surf/workstation_ptask_L07.c index 2e7fa61dea..ddf13c8905 100644 --- a/src/surf/workstation_ptask_L07.c +++ b/src/surf/workstation_ptask_L07.c @@ -841,29 +841,22 @@ static void parse_link_init(void) } static void route_new(int src_id, int dst_id, - link_L07_t * link_list, int nb_link) + link_L07_t * link_list , int nb_link) { route_L07_t route = &(ROUTE(src_id, dst_id)); route->size = nb_link; - route->links = link_list = - xbt_realloc(link_list, sizeof(link_L07_t) * nb_link); + route->links = link_list = xbt_realloc(link_list, sizeof(link_L07_t) * nb_link); } -static int nb_link; -static int link_list_capacity; -static link_L07_t *link_list = NULL; + static int src_id = -1; static int dst_id = -1; -static int is_first =1; static void parse_route_set_endpoints(void) { cpu_L07_t cpu_tmp = NULL; - if (is_first) create_routing_table(); - is_first = 0; - cpu_tmp = (cpu_L07_t) name_service(A_surfxml_route_src); xbt_assert1(cpu_tmp, "Invalid cpu %s", A_surfxml_route_src); if (cpu_tmp != NULL) @@ -874,54 +867,22 @@ static void parse_route_set_endpoints(void) if (cpu_tmp != NULL) dst_id = cpu_tmp->id; - nb_link = 0; - link_list_capacity = 1; - link_list = xbt_new(link_L07_t, link_list_capacity); -} - -static void parse_route_elem(void) -{ - xbt_ex_t e; - if (nb_link == link_list_capacity) { - link_list_capacity *= 2; - link_list = - xbt_realloc(link_list, - (link_list_capacity) * sizeof(link_L07_t)); - } - TRY { - link_list[nb_link++] = - xbt_dict_get(link_set, A_surfxml_link_c_ctn_id); - } - CATCH(e) { - RETHROW1("Link %s not found (dict raised this exception: %s)", - A_surfxml_link_c_ctn_id); - } + route_link_list = xbt_dynar_new(sizeof(char*), &free_string); } static void parse_route_set_route(void) { - if (src_id != -1 && dst_id != -1) - route_new(src_id, dst_id, link_list, nb_link); + char* name; + if (src_id != -1 && dst_id != -1) { + name = bprintf("%d##%d",src_id, dst_id); + xbt_dict_set(route_table, name, route_link_list, NULL); + free(name); + } } -static void parse_file(const char *file) +static void add_loopback(void) { int i; - - /* Adding callback functions */ - surf_parse_reset_parser(); - surfxml_add_callback(STag_surfxml_host_cb_list, &parse_cpu_init); - surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties); - surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init); - surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); - surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem); - surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route); - - /* Parse the file */ - surf_parse_open(file); - xbt_assert1((!surf_parse()), "Parse error in %s", file); - surf_parse_close(); - /* Adding loopback if needed */ for (i = 0; i < nb_workstation; i++) if (!ROUTE(i, i).size) { @@ -937,6 +898,50 @@ static void parse_file(const char *file) } } +static void add_route() +{ + xbt_ex_t e; + int nb_link = 0; + int cpt = 0; + // char* val; + int link_list_capacity = 0; + link_L07_t *link_list = NULL; + + if (routing_table == NULL) create_routing_table(); + + src_id = atoi(xbt_dynar_get_as(keys, 0, char*)); + dst_id = atoi(xbt_dynar_get_as(keys, 1, char*)); + + link_list_capacity = xbt_dynar_length(links); + link_list = xbt_new(link_L07_t, link_list_capacity); + + char* link = NULL; + xbt_dynar_foreach (links, cpt, link) { + TRY { + link_list[nb_link++] = xbt_dict_get(link_set, link); + } + CATCH(e) { + RETHROW1("Link %s not found (dict raised this exception: %s)", link); + } + } + route_new(src_id, dst_id, link_list, nb_link); +} + +static void define_callbacks(const char *file) +{ + /* Adding callback functions */ + surf_parse_reset_parser(); + surfxml_add_callback(STag_surfxml_host_cb_list, &parse_cpu_init); + surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties); + surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init); + surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints); + surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem); + surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route); + surfxml_add_callback(STag_surfxml_platform_cb_list, &init_route_table); + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route); + surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback); +} + /**************************************/ /********* Module creation ***********/ /**************************************/ @@ -1038,7 +1043,7 @@ void surf_workstation_model_init_ptask_L07(const char *filename) xbt_assert0(!surf_network_model, "network model type already defined"); model_init_internal(); - parse_file(filename); + define_callbacks(filename); update_model_description(surf_workstation_model_description, surf_workstation_model_description_size, diff --git a/src/xbt/xbt_str.c b/src/xbt/xbt_str.c index f62ef1b1c2..6167b1f548 100644 --- a/src/xbt/xbt_str.c +++ b/src/xbt/xbt_str.c @@ -233,6 +233,53 @@ xbt_dynar_t xbt_str_split(const char *s, const char *sep) { return res; } +/** + * \brief This functions splits a string after using another string as separator + * For example A##B##C splitted after ## will return the dynar {A,B,C} + * \return An array of dynars containing the string tokens +*/ +xbt_dynar_t xbt_str_split_str(const char *s, const char *sep) { + xbt_dynar_t res = xbt_dynar_new(sizeof(char*), free_string); + int done; + const char *p, *q; + + p = q = s; + done = 0; + + if (s[0] == '\0') + return res; + if (sep[0] == '\0') { + xbt_dynar_push(res, &s); + return res; + } + + while (!done) { + char *to_push; + int v = 0; + //get the start of the first occurence of the substring + q = strstr(p, sep); + //if substring was not found add the entire string + if (NULL == q) { + v = strlen(p); + to_push = malloc(v + 1); + memcpy(to_push, p, v); + to_push[v] = '\0'; + xbt_dynar_push(res, &to_push); + done = 1; + } + else { + //get the appearance + to_push = malloc(q - p + 1); + memcpy(to_push, p, q - p); + //add string terminator + to_push[q - p] = '\0'; + xbt_dynar_push(res, &to_push); + p = q +strlen(sep); + } + } + return res; +} + /** @brief Splits a string into a dynar of strings, taking quotes into account * * It basically does the same argument separation than the shell, where white @@ -364,6 +411,27 @@ XBT_TEST_UNIT("xbt_str_split_quoted",test_split_quoted, "test the function xbt_s mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata"); } + +#define mytest_str(name, input, separator, expected) \ + xbt_test_add0(name); \ + d=xbt_str_split_str(input, separator); \ + s=xbt_str_join(d,"XXX"); \ + xbt_test_assert3(!strcmp(s,expected),\ + "Input (%s) leads to (%s) instead of (%s)", \ + input,s,expected);\ + free(s); \ + xbt_dynar_free(&d); + +XBT_TEST_UNIT("xbt_str_split_str",test_split_str, "test the function xbt_str_split_str") { + xbt_dynar_t d; + char *s; + + mytest_str("Empty string and separator", "", "", ""); + mytest_str("Empty string", "", "##", ""); + mytest_str("Empty separator", "toto", "", "toto"); + mytest_str("String with no separator in it", "toto", "##", "toto"); + mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu"); +} #endif /* SIMGRID_TEST */ /** @brief Join a set of strings as a single string */ diff --git a/src/xbt_str_unit.c b/src/xbt_str_unit.c index d34d8dcec1..579d4b9d1a 100644 --- a/src/xbt_str_unit.c +++ b/src/xbt_str_unit.c @@ -8,7 +8,7 @@ /* GENERATED FILE, DO NOT EDIT */ /*******************************/ -# 339 "xbt/xbt_str.c" +# 386 "xbt/xbt_str.c" #define mytest(name, input, expected) \ xbt_test_add0(name); \ d=xbt_str_split_quoted(input); \ @@ -37,6 +37,27 @@ XBT_TEST_UNIT("xbt_str_split_quoted",test_split_quoted, "test the function xbt_s mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata"); } + +#define mytest_str(name, input, separator, expected) \ + xbt_test_add0(name); \ + d=xbt_str_split_str(input, separator); \ + s=xbt_str_join(d,"XXX"); \ + xbt_test_assert3(!strcmp(s,expected),\ + "Input (%s) leads to (%s) instead of (%s)", \ + input,s,expected);\ + free(s); \ + xbt_dynar_free(&d); + +XBT_TEST_UNIT("xbt_str_split_str",test_split_str, "test the function xbt_str_split_str") { + xbt_dynar_t d; + char *s; + + mytest_str("Empty string and separator", "", "", ""); + mytest_str("Empty string", "", "##", ""); + mytest_str("Empty separator", "toto", "", "toto"); + mytest_str("String with no separator in it", "toto", "##", "toto"); + mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu"); +} /*******************************/ /* GENERATED FILE, DO NOT EDIT */ /*******************************/