Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] correct place to keep route allocation + free
[simgrid.git] / src / surf / surf_routing_generic.c
1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/platf_interface.h"    // platform creation API internal interface
8
9 #include "surf_routing_private.h"
10 #include "surf/surf_routing.h"
11 #include "surf/surfxml_parse_values.h"
12 #include "xbt/graph.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
15
16 static int no_bypassroute_declared = 1;
17
18 AS_t model_generic_create_sized(size_t childsize) {
19   AS_t new_component = model_none_create_sized(childsize);
20
21   new_component->parse_PU = generic_parse_PU;
22   new_component->parse_AS = generic_parse_AS;
23   new_component->parse_route = NULL;
24   new_component->parse_ASroute = NULL;
25   new_component->parse_bypassroute = generic_parse_bypassroute;
26   new_component->get_route_and_latency = NULL;
27   new_component->get_onelink_routes = NULL;
28   new_component->get_bypass_route =
29       generic_get_bypassroute;
30   new_component->finalize = model_generic_finalize;
31   new_component->bypassRoutes = xbt_dict_new_homogeneous((void (*)(void *)) generic_free_route);
32
33   return new_component;
34 }
35 void model_generic_finalize(AS_t as) {
36   xbt_dict_free(&as->bypassRoutes);
37   model_none_finalize(as);
38 }
39
40 int generic_parse_PU(AS_t as, sg_routing_edge_t elm)
41 {
42   XBT_DEBUG("Load process unit \"%s\"", elm->name);
43   xbt_dynar_push_as(as->index_network_elm,sg_routing_edge_t,elm);
44   return xbt_dynar_length(as->index_network_elm)-1;
45 }
46
47 int generic_parse_AS(AS_t as, sg_routing_edge_t elm)
48 {
49   XBT_DEBUG("Load Autonomous system \"%s\"", elm->name);
50   xbt_dynar_push_as(as->index_network_elm,sg_routing_edge_t,elm);
51   return xbt_dynar_length(as->index_network_elm)-1;
52 }
53
54 void generic_parse_bypassroute(AS_t rc, sg_platf_route_cbarg_t e_route)
55 {
56   char *src = (char*)(e_route->src);
57   char *dst = (char*)(e_route->dst);
58
59   if(e_route->gw_dst)
60     XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
61   else
62     XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
63   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
64   char *route_name;
65
66   route_name = bprintf("%s#%s", src, dst);
67   xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
68       "Invalid count of links, must be greater than zero (%s,%s)",
69       src, dst);
70   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
71       "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
72       src, e_route->gw_src->name, dst, e_route->gw_dst->name);
73
74   sg_platf_route_cbarg_t new_e_route = NULL;
75   if(e_route->gw_dst)
76     new_e_route =  generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 1);
77   else
78     new_e_route =  generic_new_extended_route(SURF_ROUTING_BASE, e_route, 1);
79
80   xbt_dynar_free(&(e_route->link_list));
81
82   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
83   no_bypassroute_declared = 0;
84   xbt_free(route_name);
85 }
86
87 /* ************************************************************************** */
88 /* *********************** GENERIC BUSINESS METHODS ************************* */
89
90 xbt_dynar_t generic_get_onelink_routes(AS_t rc) { // FIXME: kill that stub
91   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
92   return NULL;
93 }
94
95 static const char *instr_node_name(xbt_node_t node)
96 {
97   void *data = xbt_graph_node_get_data(node);
98   char *str = (char *) data;
99   return str;
100 }
101
102 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
103                               xbt_dict_t nodes)
104 {
105   xbt_node_t ret = xbt_dict_get_or_null(nodes, name);
106   if (ret)
107     return ret;
108
109   ret = xbt_graph_new_node(graph, xbt_strdup(name));
110   xbt_dict_set(nodes, name, ret, NULL);
111   return ret;
112 }
113
114 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
115                               xbt_dict_t edges)
116 {
117   xbt_edge_t ret;
118
119   const char *sn = instr_node_name(s);
120   const char *dn = instr_node_name(d);
121   int len = strlen(sn) + strlen(dn) + 1;
122   char *name = (char *) xbt_malloc(len * sizeof(char));
123
124
125   snprintf(name, len, "%s%s", sn, dn);
126   ret = xbt_dict_get_or_null(edges, name);
127   if (ret == NULL) {
128     snprintf(name, len, "%s%s", dn, sn);
129     ret = xbt_dict_get_or_null(edges, name);
130   }
131
132   if (ret == NULL) {
133     ret = xbt_graph_new_edge(graph, s, d, NULL);
134     xbt_dict_set(edges, name, ret, NULL);
135   }
136   free(name);
137   return ret;
138 }
139
140 void generic_get_graph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
141                        AS_t rc)
142 {
143   int src, dst;
144   int table_size = xbt_dynar_length(rc->index_network_elm);
145
146
147   for (src = 0; src < table_size; src++) {
148     sg_routing_edge_t my_src =
149         xbt_dynar_get_as(rc->index_network_elm, src, sg_routing_edge_t);
150     for (dst = 0; dst < table_size; dst++) {
151       if (src == dst)
152         continue;
153       sg_routing_edge_t my_dst =
154           xbt_dynar_get_as(rc->index_network_elm, dst, sg_routing_edge_t);
155
156       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
157       route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
158
159       rc->get_route_and_latency(rc, my_src, my_dst, route, NULL);
160
161       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name, my_dst->name);
162
163       if (route) {
164         unsigned int cpt;
165         void *link;
166
167         xbt_node_t current, previous;
168         const char *previous_name, *current_name;
169
170         if (route->gw_src) {
171           previous = new_xbt_graph_node(graph, route->gw_src->name, nodes);
172           previous_name = route->gw_src->name;
173         } else {
174           previous = new_xbt_graph_node(graph, my_src->name, nodes);
175           previous_name = my_src->name;
176         }
177
178         xbt_dynar_foreach(route->link_list, cpt, link) {
179           char *link_name = ((surf_resource_t) link)->name;
180           current = new_xbt_graph_node(graph, link_name, nodes);
181           current_name = link_name;
182           new_xbt_graph_edge(graph, previous, current, edges);
183           XBT_DEBUG ("  %s -> %s", previous_name, current_name);
184           previous = current;
185           previous_name = current_name;
186         }
187
188         if (route->gw_dst) {
189           current = new_xbt_graph_node(graph, route->gw_dst->name, nodes);
190           current_name = route->gw_dst->name;
191         } else {
192           current = new_xbt_graph_node(graph, my_dst->name, nodes);
193           current_name = my_dst->name;
194         }
195         new_xbt_graph_edge(graph, previous, current, edges);
196         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
197       }
198       xbt_dynar_free (&(route->link_list));
199       xbt_free (route);
200     }
201   }
202 }
203
204 sg_platf_route_cbarg_t generic_get_bypassroute(AS_t rc, sg_routing_edge_t src,
205                                                sg_routing_edge_t dst,
206                                                double *lat)
207 {
208   // If never set a bypass route return NULL without any further computations
209   XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name, dst->name);
210   if (no_bypassroute_declared)
211     return NULL;
212
213   sg_platf_route_cbarg_t e_route_bypass = NULL;
214   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
215
216   if(dst->rc_component == rc && src->rc_component == rc ){
217     char *route_name = bprintf("%s#%s", src->name, dst->name);
218     e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
219     if(e_route_bypass)
220       XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
221     free(route_name);
222   }
223   else{
224     AS_t src_as, dst_as;
225     int index_src, index_dst;
226     xbt_dynar_t path_src = NULL;
227     xbt_dynar_t path_dst = NULL;
228     AS_t current = NULL;
229     AS_t *current_src = NULL;
230     AS_t *current_dst = NULL;
231
232     if (src == NULL || dst == NULL)
233       xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
234           src->name, dst->name, rc->name);
235
236     src_as = src->rc_component;
237     dst_as = dst->rc_component;
238
239     /* (2) find the path to the root routing component */
240     path_src = xbt_dynar_new(sizeof(AS_t), NULL);
241     current = src_as;
242     while (current != NULL) {
243       xbt_dynar_push(path_src, &current);
244       current = current->routing_father;
245     }
246     path_dst = xbt_dynar_new(sizeof(AS_t), NULL);
247     current = dst_as;
248     while (current != NULL) {
249       xbt_dynar_push(path_dst, &current);
250       current = current->routing_father;
251     }
252
253     /* (3) find the common father */
254     index_src = path_src->used - 1;
255     index_dst = path_dst->used - 1;
256     current_src = xbt_dynar_get_ptr(path_src, index_src);
257     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
258     while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
259       xbt_dynar_pop_ptr(path_src);
260       xbt_dynar_pop_ptr(path_dst);
261       index_src--;
262       index_dst--;
263       current_src = xbt_dynar_get_ptr(path_src, index_src);
264       current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
265     }
266
267     int max_index_src = path_src->used - 1;
268     int max_index_dst = path_dst->used - 1;
269
270     int max_index = max(max_index_src, max_index_dst);
271     int i, max;
272
273     for (max = 0; max <= max_index; max++) {
274       for (i = 0; i < max; i++) {
275         if (i <= max_index_src && max <= max_index_dst) {
276           char *route_name = bprintf("%s#%s",
277               (*(AS_t *)
278                   (xbt_dynar_get_ptr(path_src, i)))->name,
279                   (*(AS_t *)
280                       (xbt_dynar_get_ptr(path_dst, max)))->name);
281           e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
282           xbt_free(route_name);
283         }
284         if (e_route_bypass)
285           break;
286         if (max <= max_index_src && i <= max_index_dst) {
287           char *route_name = bprintf("%s#%s",
288               (*(AS_t *)
289                   (xbt_dynar_get_ptr(path_src, max)))->name,
290                   (*(AS_t *)
291                       (xbt_dynar_get_ptr(path_dst, i)))->name);
292           e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
293           xbt_free(route_name);
294         }
295         if (e_route_bypass)
296           break;
297       }
298
299       if (e_route_bypass)
300         break;
301
302       if (max <= max_index_src && max <= max_index_dst) {
303         char *route_name = bprintf("%s#%s",
304             (*(AS_t *)
305                 (xbt_dynar_get_ptr(path_src, max)))->name,
306                 (*(AS_t *)
307                     (xbt_dynar_get_ptr(path_dst, max)))->name);
308         e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
309         xbt_free(route_name);
310       }
311       if (e_route_bypass)
312         break;
313     }
314
315     xbt_dynar_free(&path_src);
316     xbt_dynar_free(&path_dst);
317   }
318
319   sg_platf_route_cbarg_t new_e_route = NULL;
320   if (e_route_bypass) {
321     void *link;
322     unsigned int cpt = 0;
323     new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
324     new_e_route->gw_src = e_route_bypass->gw_src;
325     new_e_route->gw_dst = e_route_bypass->gw_dst;
326     new_e_route->link_list =
327         xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
328     xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
329       xbt_dynar_push(new_e_route->link_list, &link);
330       if (lat)
331         *lat += surf_network_model->extension.network.get_link_latency(link);
332     }
333   }
334
335   return new_e_route;
336 }
337
338 /* ************************************************************************** */
339 /* ************************* GENERIC AUX FUNCTIONS ************************** */
340 /* change a route containing link names into a route containing link entities */
341 sg_platf_route_cbarg_t
342 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
343     sg_platf_route_cbarg_t routearg, int change_order) {
344
345   sg_platf_route_cbarg_t result;
346   char *link_name;
347   unsigned int cpt;
348
349   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
350   result->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
351
352   xbt_assert(hierarchy == SURF_ROUTING_BASE
353       || hierarchy == SURF_ROUTING_RECURSIVE,
354       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
355
356   if (hierarchy == SURF_ROUTING_RECURSIVE) {
357
358     xbt_assert(routearg->gw_src && routearg->gw_dst,
359         "NULL is obviously a bad gateway");
360
361     /* remeber not erase the gateway names */
362     result->gw_src = routearg->gw_src;
363     result->gw_dst = routearg->gw_dst;
364   }
365
366   xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
367
368     void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
369     if (link) {
370       if (change_order)
371         xbt_dynar_push(result->link_list, &link);
372       else
373         xbt_dynar_unshift(result->link_list, &link);
374     } else
375       THROWF(mismatch_error, 0, "Link %s not found", link_name);
376   }
377
378   return result;
379 }
380
381 void generic_free_route(sg_platf_route_cbarg_t route)
382 {
383   if (route) {
384     xbt_dynar_free(&route->link_list);
385     xbt_free(route);
386   }
387 }
388
389 static AS_t generic_as_exist(AS_t find_from,
390     AS_t to_find)
391 {
392   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
393   xbt_dict_cursor_t cursor = NULL;
394   char *key;
395   int found = 0;
396   AS_t elem;
397   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
398     if (to_find == elem || generic_as_exist(elem, to_find)) {
399       found = 1;
400       break;
401     }
402   }
403   if (found)
404     return to_find;
405   return NULL;
406 }
407
408 AS_t
409 generic_autonomous_system_exist(AS_t rc, char *element)
410 {
411   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
412   AS_t element_as, result, elem;
413   xbt_dict_cursor_t cursor = NULL;
414   char *key;
415   element_as = ((sg_routing_edge_t)
416       xbt_lib_get_or_null(as_router_lib, element,
417           ROUTING_ASR_LEVEL))->rc_component;
418   result = ((AS_t) - 1);
419   if (element_as != rc)
420     result = generic_as_exist(rc, element_as);
421
422   int found = 0;
423   if (result) {
424     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
425       found = !strcmp(elem->name, element);
426       if (found)
427         break;
428     }
429     if (found)
430       return element_as;
431   }
432   return NULL;
433 }
434
435 AS_t
436 generic_processing_units_exist(AS_t rc, char *element)
437 {
438   AS_t element_as;
439   element_as = ((sg_routing_edge_t)
440       xbt_lib_get_or_null(host_lib,
441           element, ROUTING_HOST_LEVEL))->rc_component;
442   if (element_as == rc)
443     return element_as;
444   return generic_as_exist(rc, element_as);
445 }
446
447 void generic_src_dst_check(AS_t rc, sg_routing_edge_t src,
448     sg_routing_edge_t dst)
449 {
450
451   sg_routing_edge_t src_data = src;
452   sg_routing_edge_t dst_data = dst;
453
454   if (src_data == NULL || dst_data == NULL)
455     xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
456         src->name,
457         dst->name,
458         rc->name);
459
460   AS_t src_as =
461       (src_data)->rc_component;
462   AS_t dst_as =
463       (dst_data)->rc_component;
464
465   if (src_as != dst_as)
466     xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
467         src->name, src_as->name,
468         dst->name, dst_as->name);
469
470   if (rc != dst_as)
471     xbt_die
472     ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
473         src->name,
474         dst->name,
475         src_as->name,
476         dst_as->name,
477         rc->name);
478 }