Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a command-line option to choose the routing schema to use
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009 The SimGrid team. All rights reserved.                */
2
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. */
5
6 #include "surf_private.h"
7 #include "xbt/dynar.h"
8 #include "xbt/str.h"
9 #include "xbt/config.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");// FIXME: connect this under windows
12
13 routing_t used_routing = NULL;
14
15 /* Prototypes of each model */
16 static void routing_model_full_create(size_t size_of_link,void *loopback);
17
18 /* Definition of each model */
19 struct model_type {
20   const char *name;
21   const char *desc;
22   void (*fun)(size_t,void*);
23 };
24 struct model_type models[] =
25 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)", routing_model_full_create },
26     {NULL,NULL}};
27
28
29 void routing_model_create(size_t size_of_links, void* loopback) {
30
31   char * wanted=xbt_cfg_get_string(_surf_cfg_set,"routing");
32   int cpt;
33   for (cpt=0;models[cpt].name;cpt++) {
34     if (!strcmp(wanted,models[cpt].name)) {
35       (*(models[cpt].fun))(size_of_links,loopback);
36       return;
37     }
38   }
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);
43   exit(1);
44 }
45
46 /* ************************************************************************** */
47 /* *************************** FULL ROUTING ********************************* */
48 typedef struct {
49   s_routing_t generic_routing;
50   xbt_dynar_t *routing_table;
51   void *loopback;
52   size_t size_of_link;
53 } s_routing_full_t,*routing_full_t;
54
55 #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
56
57 /*
58  * Parsing
59  */
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);
65 }
66 static int src_id = -1;
67 static int dst_id = -1;
68 static void routing_full_parse_Sroute_set_endpoints(void)
69 {
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;
74 }
75 static void routing_full_parse_Eroute(void)
76 {
77   char *name;
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);
81     free(name);
82   }
83 }
84
85
86 static void routing_full_parse_end(void) {
87   routing_full_t routing = (routing_full_t) used_routing;
88   int nb_link = 0;
89   unsigned int cpt = 0;
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;
95   int i,j;
96
97   int host_count = routing->generic_routing.host_count;
98
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);
104
105   /* Put the routes in position */
106   xbt_dict_foreach(route_table, cursor, key, data) {
107     nb_link = 0;
108     links = (xbt_dynar_t) data;
109     keys = xbt_str_split_str(key, sep);
110
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);
114
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);
119       if (link)
120         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
121       else
122         THROW1(mismatch_error,0,"Link %s not found", link_name);
123     }
124   }
125
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);
130
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);
135 }
136
137 /*
138  * Business methods
139  */
140 static xbt_dynar_t routing_full_get_route(int src,int dst) {
141   return ROUTE_FULL(src,dst);
142 }
143
144 static void routing_full_finalize(void) {
145   routing_full_t routing = (routing_full_t)used_routing;
146   int i,j;
147
148   if (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);
154     free(routing);
155     routing=NULL;
156   }
157 }
158
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;
168
169   /* Set it in position */
170   used_routing = (routing_t) routing;
171
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);
179 }