Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename AsGeneric into AsRoutedGraph
[simgrid.git] / src / surf / surf_routing_RoutedGraph.cpp
1 /* Copyright (c) 2009-2015. 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 "surf_routing_RoutedGraph.hpp"
8
9 #include <cstdlib>
10
11 #include <algorithm>
12
13 #include <xbt/dict.h>
14 #include <xbt/log.h>
15 #include <xbt/sysdep.h>
16 #include <xbt/dynar.h>
17 #include <xbt/graph.h>
18
19 #include "simgrid/platf_interface.h"    // platform creation API internal interface
20
21 #include "surf_routing_private.hpp"
22 #include "network_interface.hpp"
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
25
26 static int no_bypassroute_declared = 1;
27
28 void routing_route_free(sg_platf_route_cbarg_t route)
29 {
30   if (route) {
31     xbt_dynar_free(&route->link_list);
32     xbt_free(route);
33   }
34 }
35
36 namespace simgrid {
37 namespace surf {
38   
39 AsRoutedGraph::AsRoutedGraph(const char*name)
40   : As(name)
41 {
42 }
43
44 AsRoutedGraph::~AsRoutedGraph()
45 {
46   xbt_dict_free(&bypassRoutes_);
47 }
48
49 void AsRoutedGraph::parseBypassroute(sg_platf_route_cbarg_t e_route)
50 {
51   char *src = (char*)(e_route->src);
52   char *dst = (char*)(e_route->dst);
53
54   if(e_route->gw_dst)
55     XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
56   else
57     XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
58   xbt_dict_t dict_bypassRoutes = bypassRoutes_;
59   char *route_name;
60
61   route_name = bprintf("%s#%s", src, dst);
62   xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
63       "Invalid count of links, must be greater than zero (%s,%s)",
64       src, dst);
65   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
66       "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
67       src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
68
69   sg_platf_route_cbarg_t new_e_route = NULL;
70   if(e_route->gw_dst)
71     new_e_route =  newExtendedRoute(SURF_ROUTING_RECURSIVE, e_route, 1);
72   else
73     new_e_route =  newExtendedRoute(SURF_ROUTING_BASE, e_route, 1);
74
75   xbt_dynar_free(&(e_route->link_list));
76
77   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
78   no_bypassroute_declared = 0;
79   xbt_free(route_name);
80 }
81
82 }
83 }
84
85 /* ************************************************************************** */
86 /* *********************** GENERIC BUSINESS METHODS ************************* */
87
88 static const char *instr_node_name(xbt_node_t node)
89 {
90   void *data = xbt_graph_node_get_data(node);
91   char *str = (char *) data;
92   return str;
93 }
94
95 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
96                               xbt_dict_t nodes)
97 {
98   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
99   if (ret)
100     return ret;
101
102   ret = xbt_graph_new_node(graph, xbt_strdup(name));
103   xbt_dict_set(nodes, name, ret, NULL);
104   return ret;
105 }
106
107 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
108                               xbt_dict_t edges)
109 {
110   xbt_edge_t ret;
111
112   const char *sn = instr_node_name(s);
113   const char *dn = instr_node_name(d);
114   int len = strlen(sn) + strlen(dn) + 1;
115   char *name = (char *) xbt_malloc(len * sizeof(char));
116
117
118   snprintf(name, len, "%s%s", sn, dn);
119   ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
120   if (ret == NULL) {
121     snprintf(name, len, "%s%s", dn, sn);
122     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
123   }
124
125   if (ret == NULL) {
126     ret = xbt_graph_new_edge(graph, s, d, NULL);
127     xbt_dict_set(edges, name, ret, NULL);
128   }
129   free(name);
130   return ret;
131 }
132
133 namespace simgrid {
134 namespace surf {
135
136 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
137 {
138   int src, dst;
139   int table_size = xbt_dynar_length(vertices_);
140
141
142   for (src = 0; src < table_size; src++) {
143     NetCard *my_src =
144         xbt_dynar_get_as(vertices_, src, NetCard*);
145     for (dst = 0; dst < table_size; dst++) {
146       if (src == dst)
147         continue;
148       NetCard *my_dst =
149           xbt_dynar_get_as(vertices_, dst, NetCard*);
150
151       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
152       route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
153
154       getRouteAndLatency(my_src, my_dst, route, NULL);
155
156       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
157
158       unsigned int cpt;
159       void *link;
160
161       xbt_node_t current, previous;
162       const char *previous_name, *current_name;
163
164       if (route->gw_src) {
165         previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
166         previous_name = route->gw_src->name();
167       } else {
168         previous = new_xbt_graph_node(graph, my_src->name(), nodes);
169         previous_name = my_src->name();
170       }
171
172       xbt_dynar_foreach(route->link_list, cpt, link) {
173         const char *link_name = static_cast<simgrid::surf::Resource*>(
174           link)->getName();
175         current = new_xbt_graph_node(graph, link_name, nodes);
176         current_name = link_name;
177         new_xbt_graph_edge(graph, previous, current, edges);
178         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
179         previous = current;
180         previous_name = current_name;
181       }
182
183       if (route->gw_dst) {
184         current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
185         current_name = route->gw_dst->name();
186       } else {
187         current = new_xbt_graph_node(graph, my_dst->name(), nodes);
188         current_name = my_dst->name();
189       }
190       new_xbt_graph_edge(graph, previous, current, edges);
191       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
192
193       xbt_dynar_free (&(route->link_list));
194       xbt_free (route);
195     }
196   }
197 }
198
199 sg_platf_route_cbarg_t AsRoutedGraph::getBypassRoute(NetCard *src,
200                                                NetCard *dst,
201                                                double *lat)
202 {
203   // If never set a bypass route return NULL without any further computations
204   XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
205   if (no_bypassroute_declared)
206     return NULL;
207
208   sg_platf_route_cbarg_t e_route_bypass = NULL;
209   xbt_dict_t dict_bypassRoutes = bypassRoutes_;
210
211   if(dst->containingAS() == this && src->containingAS() == this ){
212     char *route_name = bprintf("%s#%s", src->name(), dst->name());
213     e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
214     if(e_route_bypass)
215       XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
216     free(route_name);
217   }
218   else{
219     As *src_as, *dst_as;
220     int index_src, index_dst;
221     xbt_dynar_t path_src = NULL;
222     xbt_dynar_t path_dst = NULL;
223     As *current = NULL;
224     As **current_src = NULL;
225     As **current_dst = NULL;
226
227     if (src == NULL || dst == NULL)
228       xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
229               src ? src->name() : "(null)",
230               dst ? dst->name() : "(null)", name_);
231
232     src_as = src->containingAS();
233     dst_as = dst->containingAS();
234
235     /* (2) find the path to the root routing component */
236     path_src = xbt_dynar_new(sizeof(As*), NULL);
237     current = src_as;
238     while (current != NULL) {
239       xbt_dynar_push(path_src, &current);
240       current = current->father_;
241     }
242     path_dst = xbt_dynar_new(sizeof(As*), NULL);
243     current = dst_as;
244     while (current != NULL) {
245       xbt_dynar_push(path_dst, &current);
246       current = current->father_;
247     }
248
249     /* (3) find the common father */
250     index_src = path_src->used - 1;
251     index_dst = path_dst->used - 1;
252     current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
253     current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
254     while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
255       xbt_dynar_pop_ptr(path_src);
256       xbt_dynar_pop_ptr(path_dst);
257       index_src--;
258       index_dst--;
259       current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
260       current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
261     }
262
263     int max_index_src = path_src->used - 1;
264     int max_index_dst = path_dst->used - 1;
265
266     int max_index = std::max(max_index_src, max_index_dst);
267     int i, max;
268
269     for (max = 0; max <= max_index; max++) {
270       for (i = 0; i < max; i++) {
271         if (i <= max_index_src && max <= max_index_dst) {
272           char *route_name = bprintf("%s#%s",
273               (*(As **)
274                   (xbt_dynar_get_ptr(path_src, i)))->name_,
275                   (*(As **)
276                       (xbt_dynar_get_ptr(path_dst, max)))->name_);
277           e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
278           xbt_free(route_name);
279         }
280         if (e_route_bypass)
281           break;
282         if (max <= max_index_src && i <= max_index_dst) {
283           char *route_name = bprintf("%s#%s",
284               (*(As **)
285                   (xbt_dynar_get_ptr(path_src, max)))->name_,
286                   (*(As **)
287                       (xbt_dynar_get_ptr(path_dst, i)))->name_);
288           e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
289           xbt_free(route_name);
290         }
291         if (e_route_bypass)
292           break;
293       }
294
295       if (e_route_bypass)
296         break;
297
298       if (max <= max_index_src && max <= max_index_dst) {
299         char *route_name = bprintf("%s#%s",
300             (*(As **)
301                 (xbt_dynar_get_ptr(path_src, max)))->name_,
302                 (*(As **)
303                     (xbt_dynar_get_ptr(path_dst, max)))->name_);
304         e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
305         xbt_free(route_name);
306       }
307       if (e_route_bypass)
308         break;
309     }
310
311     xbt_dynar_free(&path_src);
312     xbt_dynar_free(&path_dst);
313   }
314
315   sg_platf_route_cbarg_t new_e_route = NULL;
316   if (e_route_bypass) {
317     Link* link;
318     unsigned int cpt = 0;
319     new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
320     new_e_route->gw_src = e_route_bypass->gw_src;
321     new_e_route->gw_dst = e_route_bypass->gw_dst;
322     new_e_route->link_list =
323         xbt_dynar_new(sizeof(Link*), NULL);
324     xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
325       xbt_dynar_push(new_e_route->link_list, &link);
326       if (lat)
327         *lat += link->getLatency();
328     }
329   }
330
331   return new_e_route;
332 }
333
334 /* ************************************************************************** */
335 /* ************************* GENERIC AUX FUNCTIONS ************************** */
336 /* change a route containing link names into a route containing link entities */
337 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
338       sg_platf_route_cbarg_t routearg, int change_order) {
339
340   sg_platf_route_cbarg_t result;
341   char *link_name;
342   unsigned int cpt;
343
344   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
345   result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
346
347   xbt_assert(hierarchy == SURF_ROUTING_BASE
348       || hierarchy == SURF_ROUTING_RECURSIVE,
349       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
350
351   if (hierarchy == SURF_ROUTING_RECURSIVE) {
352
353     xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
354
355     /* remember not erase the gateway names */
356     result->gw_src = routearg->gw_src;
357     result->gw_dst = routearg->gw_dst;
358   }
359
360   xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
361
362     Link *link = Link::byName(link_name);
363     if (link) {
364       if (change_order)
365         xbt_dynar_push(result->link_list, &link);
366       else
367         xbt_dynar_unshift(result->link_list, &link);
368     } else
369       THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
370   }
371
372   return result;
373 }
374
375 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
376 {
377   xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
378   xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
379
380   As *src_as = src->containingAS();
381   As *dst_as = dst->containingAS();
382
383   xbt_assert(src_as == dst_as, "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
384         src->name(), src_as->name_, dst->name(), dst_as->name_);
385
386   xbt_assert(this == dst_as,
387       "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
388         src->name(), dst->name(),  src_as->name_, dst_as->name_,  name_);
389 }
390 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
391   const char *srcName = route->src;
392   const char *dstName = route->dst;
393   NetCard *src = sg_netcard_by_name_or_null(srcName);
394   NetCard *dst = sg_netcard_by_name_or_null(dstName);
395
396   if(!route->gw_dst && !route->gw_src) {
397     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
398     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
399     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
400     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
401     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
402         "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
403     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
404         "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
405   } else {
406     XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
407     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
408         "When defining an ASroute, src must be an AS but '%s' is not", srcName);
409     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
410         "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
411
412     xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
413         "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
414     xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
415         "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
416
417     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
418
419     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
420         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
421     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
422         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
423     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
424         srcName,route->gw_src->name(), dstName,route->gw_dst->name());
425   }
426 }
427
428 }
429 }