Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
427af14b3f649998e07cc5b7d70f091877a7144b
[simgrid.git] / src / surf / surf_routing_full.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 "src/surf/surf_routing_private.hpp"
8 #include "src/surf/surf_routing_full.hpp"
9 #include "src/surf/network_interface.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
12
13 #define TO_ROUTE_FULL(i,j) routingTable_[(i)+(j)*table_size]
14
15 namespace simgrid {
16 namespace surf {
17   AsFull::AsFull(const char*name)
18     : AsGeneric(name)
19   {
20   }
21
22 void AsFull::Seal() {
23   int i;
24   sg_platf_route_cbarg_t e_route;
25
26   /* set utils vars */
27   int table_size = (int)xbt_dynar_length(vertices_);
28
29   /* Create table if necessary */
30   if (!routingTable_)
31     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
32
33   /* Add the loopback if needed */
34   if (routing_platf->loopback_ && hierarchy_ == SURF_ROUTING_BASE) {
35     for (i = 0; i < table_size; i++) {
36       e_route = TO_ROUTE_FULL(i, i);
37       if (!e_route) {
38         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
39         e_route->gw_src = NULL;
40         e_route->gw_dst = NULL;
41         e_route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
42         xbt_dynar_push(e_route->link_list, &routing_platf->loopback_);
43         TO_ROUTE_FULL(i, i) = e_route;
44       }
45     }
46   }
47 }
48
49 AsFull::~AsFull(){
50   if (routingTable_) {
51     int table_size = (int)xbt_dynar_length(vertices_);
52     int i, j;
53     /* Delete routing table */
54     for (i = 0; i < table_size; i++)
55       for (j = 0; j < table_size; j++) {
56         if (TO_ROUTE_FULL(i,j)){
57           xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
58           xbt_free(TO_ROUTE_FULL(i,j));
59         }
60       }
61     xbt_free(routingTable_);
62   }
63 }
64
65 xbt_dynar_t AsFull::getOneLinkRoutes()
66 {
67   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
68
69   int src, dst;
70   int table_size = xbt_dynar_length(vertices_);
71
72   for(src=0; src < table_size; src++) {
73     for(dst=0; dst< table_size; dst++) {
74       sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
75       if (route) {
76         if (xbt_dynar_length(route->link_list) == 1) {
77           void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
78           Onelink *onelink;
79           if (hierarchy_ == SURF_ROUTING_BASE) {
80           NetCard *tmp_src = xbt_dynar_get_as(vertices_, src, sg_netcard_t);
81             tmp_src->setId(src);
82           NetCard *tmp_dst = xbt_dynar_get_as(vertices_, dst, sg_netcard_t);
83           tmp_dst->setId(dst);
84             onelink = new Onelink(link, tmp_src, tmp_dst);
85           } else if (hierarchy_ == SURF_ROUTING_RECURSIVE)
86             onelink = new Onelink(link, route->gw_src, route->gw_dst);
87           else
88             onelink = new Onelink(link, NULL, NULL);
89           xbt_dynar_push(ret, &onelink);
90           XBT_DEBUG("Push route from '%d' to '%d'",
91               src,
92               dst);
93         }
94       }
95     }
96   }
97   return ret;
98 }
99
100 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
101 {
102   XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
103       src->name(),
104       src->id(),
105       dst->name(),
106       dst->id());
107
108   /* set utils vars */
109   size_t table_size = xbt_dynar_length(vertices_);
110
111   sg_platf_route_cbarg_t e_route = NULL;
112   void *link;
113   unsigned int cpt = 0;
114
115   e_route = TO_ROUTE_FULL(src->id(), dst->id());
116
117   if (e_route) {
118     res->gw_src = e_route->gw_src;
119     res->gw_dst = e_route->gw_dst;
120     xbt_dynar_foreach(e_route->link_list, cpt, link) {
121       xbt_dynar_push(res->link_list, &link);
122       if (lat)
123         *lat += static_cast<Link*>(link)->getLatency();
124     }
125   }
126 }
127
128 static int full_pointer_resource_cmp(const void *a, const void *b)
129 {
130   return a != b;
131 }
132
133 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
134 {
135   const char *src = route->src;
136   const char *dst = route->dst;
137   NetCard *src_net_elm = sg_netcard_by_name_or_null(src);
138   NetCard *dst_net_elm = sg_netcard_by_name_or_null(dst);
139
140   parseRouteCheckParams(route);
141
142   size_t table_size = xbt_dynar_length(vertices_);
143
144   if (!routingTable_)
145     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
146
147   /* Check that the route does not already exist */
148   if (route->gw_dst) // AS route (to adapt the error message, if any)
149     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
150         "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
151         src,route->gw_src->name(),dst,route->gw_dst->name());
152   else
153     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
154         "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src,dst);
155
156   /* Add the route to the base */
157   TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()) = newExtendedRoute(hierarchy_, route, 1);
158   xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id())->link_list, 0);
159
160   if (route->symmetrical == TRUE) {
161     if (route->gw_dst && route->gw_src) {
162       NetCard* gw_tmp = route->gw_src;
163       route->gw_src = route->gw_dst;
164       route->gw_dst = gw_tmp;
165     }
166     if (TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())) {
167       char *link_name;
168       unsigned int i;
169       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(Link*), NULL);
170       for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
171         link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
172         void *link = Link::byName(link_name);
173         xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
174         xbt_dynar_push(link_route_to_test, &link);
175       }
176       xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list,
177           link_route_to_test,
178           full_pointer_resource_cmp),
179           "The route between \"%s\" and \"%s\" already exists", src,
180           dst);
181     } else {
182       if (!route->gw_dst && !route->gw_src)
183         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
184       else
185         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
186             dst, route->gw_src->name(), src, route->gw_dst->name());
187       TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()) = newExtendedRoute(hierarchy_, route, 0);
188       xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list, 0);
189     }
190   }
191   xbt_dynar_free(&route->link_list);
192 }
193
194 }
195 }