Logo AND Algorithmique Numérique Distribuée

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