1 /* Copyright (c) 2009 The SimGrid team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "surf_private.h"
9 #include "xbt/config.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");// FIXME: connect this under windows
13 routing_t used_routing = NULL;
15 /* Prototypes of each model */
16 static void routing_model_full_create(size_t size_of_link,void *loopback);
18 /* Definition of each model */
22 void (*fun)(size_t,void*);
24 struct model_type models[] =
25 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)", routing_model_full_create },
29 void routing_model_create(size_t size_of_links, void* loopback) {
31 char * wanted=xbt_cfg_get_string(_surf_cfg_set,"routing");
33 for (cpt=0;models[cpt].name;cpt++) {
34 if (!strcmp(wanted,models[cpt].name)) {
35 (*(models[cpt].fun))(size_of_links,loopback);
39 fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
40 for (cpt=0;models[cpt].name;cpt++)
41 if (!strcmp(wanted,models[cpt].name))
42 fprintf(stderr," %s: %s\n",models[cpt].name,models[cpt].desc);
46 /* ************************************************************************** */
47 /* *************************** FULL ROUTING ********************************* */
49 s_routing_t generic_routing;
50 xbt_dynar_t *routing_table;
53 } s_routing_full_t,*routing_full_t;
55 #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
60 static void routing_full_parse_Shost(void) {
61 int *val = xbt_malloc(sizeof(int));
62 DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
63 *val = used_routing->host_count++;
64 xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
66 static int src_id = -1;
67 static int dst_id = -1;
68 static void routing_full_parse_Sroute_set_endpoints(void)
70 src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
71 dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
72 DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
73 route_action = A_surfxml_route_action;
75 static void routing_full_parse_Eroute(void)
78 if (src_id != -1 && dst_id != -1) {
79 name = bprintf("%x#%x", src_id, dst_id);
80 manage_route(route_table, name, route_action, 0);
86 static void routing_full_parse_end(void) {
87 routing_full_t routing = (routing_full_t) used_routing;
90 xbt_dict_cursor_t cursor = NULL;
91 char *key, *data, *end;
92 const char *sep = "#";
93 xbt_dynar_t links, keys;
94 char *link_name = NULL;
97 int host_count = routing->generic_routing.host_count;
99 /* Create the routing table */
100 routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
101 for (i=0;i<host_count;i++)
102 for (j=0;j<host_count;j++)
103 ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
105 /* Put the routes in position */
106 xbt_dict_foreach(route_table, cursor, key, data) {
108 links = (xbt_dynar_t) data;
109 keys = xbt_str_split_str(key, sep);
111 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
112 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
113 xbt_dynar_free(&keys);
115 DEBUG4("Handle %d %d (from %d hosts): %ld links",
116 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
117 xbt_dynar_foreach(links, cpt, link_name) {
118 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
120 xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
122 THROW1(mismatch_error,0,"Link %s not found", link_name);
126 /* Add the loopback if needed */
127 for (i = 0; i < host_count; i++)
128 if (!xbt_dynar_length(ROUTE_FULL(i, i)))
129 xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
131 /* Shrink the dynar routes (save unused slots) */
132 for (i=0;i<host_count;i++)
133 for (j=0;j<host_count;j++)
134 xbt_dynar_shrink(ROUTE_FULL(i,j),0);
140 static xbt_dynar_t routing_full_get_route(int src,int dst) {
141 return ROUTE_FULL(src,dst);
144 static void routing_full_finalize(void) {
145 routing_full_t routing = (routing_full_t)used_routing;
149 for (i = 0; i < used_routing->host_count; i++)
150 for (j = 0; j < used_routing->host_count; j++)
151 xbt_dynar_free(&ROUTE_FULL(i, j));
152 free(routing->routing_table);
153 xbt_dict_free(&used_routing->host_id);
159 static void routing_model_full_create(size_t size_of_link,void *loopback) {
160 /* initialize our structure */
161 routing_full_t routing = xbt_new0(s_routing_full_t,1);
162 routing->generic_routing.name = "Full";
163 routing->generic_routing.host_count = 0;
164 routing->generic_routing.get_route = routing_full_get_route;
165 routing->generic_routing.finalize = routing_full_finalize;
166 routing->size_of_link = size_of_link;
167 routing->loopback = loopback;
169 /* Set it in position */
170 used_routing = (routing_t) routing;
172 /* Setup the parsing callbacks we need */
173 routing->generic_routing.host_id = xbt_dict_new();
174 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
175 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
176 surfxml_add_callback(STag_surfxml_route_cb_list,
177 &routing_full_parse_Sroute_set_endpoints);
178 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);