1 /* Copyright (c) 2009-2011, 2013-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "surf_routing.hpp"
8 #include "surf_routing_cluster.hpp"
10 #include "simgrid/sg_config.h"
11 #include "storage_interface.hpp"
13 #include "src/surf/surf_routing_cluster_torus.hpp"
14 #include "src/surf/surf_routing_cluster_fat_tree.hpp"
15 #include "src/surf/surf_routing_dijkstra.hpp"
16 #include "src/surf/surf_routing_floyd.hpp"
17 #include "src/surf/surf_routing_full.hpp"
18 #include "src/surf/surf_routing_vivaldi.hpp"
19 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
29 simgrid::xbt::signal<void(simgrid::surf::NetCard*)> netcardCreatedCallbacks;
30 simgrid::xbt::signal<void(simgrid::surf::As*)> asCreatedCallbacks;
32 As::As(const char*name)
33 : name_(xbt_strdup(name))
37 xbt_dict_cursor_t cursor = NULL;
40 xbt_dict_foreach(children_, cursor, key, elem) {
45 xbt_dict_free(&children_);
46 xbt_dynar_free(&vertices_);
47 xbt_dynar_free(&upDownLinks);
48 for (auto &kv : bypassRoutes_)
58 /** @brief Get the common ancestor and its first childs in each line leading to src and dst */
59 static void find_common_ancestors(NetCard *src, NetCard *dst,
60 /* OUT */ As **common_ancestor, As **src_ancestor, As **dst_ancestor)
62 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
63 simgrid::surf::As *path_src[ROUTING_HIERARCHY_MAXDEPTH];
64 simgrid::surf::As *path_dst[ROUTING_HIERARCHY_MAXDEPTH];
67 simgrid::surf::As *current_src;
68 simgrid::surf::As *current_dst;
69 simgrid::surf::As *father;
71 /* (1) find the path to root of src and dst*/
72 simgrid::surf::As *src_as = src->containingAS();
73 simgrid::surf::As *dst_as = dst->containingAS();
75 xbt_assert(src_as, "Host %s must be in an AS", src->name());
76 xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
78 /* (2) find the path to the root routing component */
79 for (simgrid::surf::As *current = src_as; current != NULL; current = current->father_) {
80 xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
81 path_src[index_src++] = current;
83 for (simgrid::surf::As *current = dst_as; current != NULL; current = current->father_) {
84 xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH,"ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
85 path_dst[index_dst++] = current;
88 /* (3) find the common father.
89 * Before that, index_src and index_dst may be different, they both point to NULL in path_src/path_dst
90 * So we move them down simultaneously as long as they point to the same content.
93 current_src = path_src[--index_src];
94 current_dst = path_dst[--index_dst];
95 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
97 /* (4) if we did not find a difference (index_src or index_dst went to 0), both elements are in the same AS */
98 if (current_src == current_dst)
100 else // we found a difference
101 father = path_src[index_src + 1];
103 /* (5) result generation */
104 *common_ancestor = father; /* the common father of src and dst */
105 *src_ancestor = current_src; /* the first different father of src */
106 *dst_ancestor = current_dst; /* the first different father of dst */
107 #undef ROUTING_HIERARCHY_MAXDEPTH
111 xbt_dynar_t As::getOneLinkRoutes() {
115 int As::addComponent(NetCard *elm) {
116 xbt_dynar_push_as(vertices_, NetCard*, elm);
117 return xbt_dynar_length(vertices_)-1;
120 void As::addRoute(sg_platf_route_cbarg_t /*route*/){
121 xbt_die("AS %s does not accept new routes (wrong class).",name_);
124 /* PRECONDITION: this is the common ancestor of src and dst */
125 std::vector<Link*> *As::getBypassRoute(NetCard *src, NetCard *dst)
127 // If never set a bypass route return NULL without any further computations
128 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
129 if (bypassRoutes_.empty())
132 std::vector<Link*> *bypassedRoute = nullptr;
134 if(dst->containingAS() == this && src->containingAS() == this ){
135 char *route_name = bprintf("%s#%s", src->name(), dst->name());
136 if (bypassRoutes_.find(route_name) != bypassRoutes_.end()) {
137 bypassedRoute = bypassRoutes_.at(route_name);
138 XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
141 return bypassedRoute;
144 int index_src, index_dst;
145 As **current_src = NULL;
146 As **current_dst = NULL;
148 As *src_as = src->containingAS();
149 As *dst_as = dst->containingAS();
151 /* (2) find the path to the root routing component */
152 xbt_dynar_t path_src = xbt_dynar_new(sizeof(As*), NULL);
153 As *current = src_as;
154 while (current != NULL) {
155 xbt_dynar_push(path_src, ¤t);
156 current = current->father_;
158 xbt_dynar_t path_dst = xbt_dynar_new(sizeof(As*), NULL);
160 while (current != NULL) {
161 xbt_dynar_push(path_dst, ¤t);
162 current = current->father_;
165 /* (3) find the common father */
166 index_src = path_src->used - 1;
167 index_dst = path_dst->used - 1;
168 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
169 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
170 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
171 xbt_dynar_pop_ptr(path_src);
172 xbt_dynar_pop_ptr(path_dst);
175 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
176 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
179 int max_index_src = path_src->used - 1;
180 int max_index_dst = path_dst->used - 1;
182 int max_index = std::max(max_index_src, max_index_dst);
184 for (int max = 0; max <= max_index; max++) {
185 for (int i = 0; i < max; i++) {
186 if (i <= max_index_src && max <= max_index_dst) {
187 char *route_name = bprintf("%s#%s",
188 (*(As **) (xbt_dynar_get_ptr(path_src, i)))->name_,
189 (*(As **) (xbt_dynar_get_ptr(path_dst, max)))->name_);
190 if (bypassRoutes_.find(route_name) != bypassRoutes_.end())
191 bypassedRoute = bypassRoutes_.at(route_name);
192 xbt_free(route_name);
196 if (max <= max_index_src && i <= max_index_dst) {
197 char *route_name = bprintf("%s#%s",
198 (*(As **) (xbt_dynar_get_ptr(path_src, max)))->name_,
199 (*(As **) (xbt_dynar_get_ptr(path_dst, i)))->name_);
200 if (bypassRoutes_.find(route_name) != bypassRoutes_.end())
201 bypassedRoute = bypassRoutes_.at(route_name);
202 xbt_free(route_name);
211 if (max <= max_index_src && max <= max_index_dst) {
212 char *route_name = bprintf("%s#%s",
213 (*(As **) (xbt_dynar_get_ptr(path_src, max)))->name_,
214 (*(As **) (xbt_dynar_get_ptr(path_dst, max)))->name_);
216 if (bypassRoutes_.find(route_name) != bypassRoutes_.end())
217 bypassedRoute = bypassRoutes_.at(route_name);
218 xbt_free(route_name);
224 xbt_dynar_free(&path_src);
225 xbt_dynar_free(&path_dst);
227 return bypassedRoute;
230 void As::addBypassRoute(sg_platf_route_cbarg_t e_route){
231 const char *src = e_route->src;
232 const char *dst = e_route->dst;
234 char *route_name = bprintf("%s#%s", src, dst);
236 /* Argument validity checks */
237 if (e_route->gw_dst) {
238 XBT_DEBUG("Load bypassASroute from %s@%s to %s@%s",
239 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
240 xbt_assert(!e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
241 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
242 xbt_assert(bypassRoutes_.find(route_name) == bypassRoutes_.end(), "The bypass route between %s@%s and %s@%s already exists.",
243 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
245 XBT_DEBUG("Load bypassRoute from %s to %s", src, dst);
246 xbt_assert(!e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.", src, dst);
247 xbt_assert(bypassRoutes_.find(route_name) == bypassRoutes_.end(), "The bypass route between %s and %s already exists.", src, dst);
250 /* Build a copy that will be stored in the dict */
251 std::vector<Link*> *newRoute = new std::vector<Link*>();
252 for (auto link: *e_route->link_list)
253 newRoute->push_back(link);
256 bypassRoutes_.insert({route_name, newRoute});
257 xbt_free(route_name);
260 }} // namespace simgrid::surf
263 * @ingroup SURF_build_api
264 * @brief A library containing all known hosts
266 xbt_dict_t host_list;
268 int COORD_HOST_LEVEL=0; //Coordinates level
270 int MSG_FILE_LEVEL; //Msg file level
272 int SIMIX_STORAGE_LEVEL; //Simix storage level
273 int MSG_STORAGE_LEVEL; //Msg storage level
275 xbt_lib_t as_router_lib;
276 int ROUTING_ASR_LEVEL; //Routing level
277 int COORD_ASR_LEVEL; //Coordinates level
278 int NS3_ASR_LEVEL; //host node for ns3
279 int ROUTING_PROP_ASR_LEVEL; //Where the properties are stored
281 /** @brief Retrieve a netcard from its name
283 * Netcards are the thing that connect host or routers to the network
285 simgrid::surf::NetCard *sg_netcard_by_name_or_null(const char *name)
287 sg_host_t h = sg_host_by_name(name);
288 simgrid::surf::NetCard *netcard = h==NULL ? NULL: h->pimpl_netcard;
290 netcard = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
295 simgrid::surf::RoutingPlatf *routing_platf = NULL;
298 /** The current AS in the parsing */
299 static simgrid::surf::As *current_routing = NULL;
300 simgrid::surf::As* routing_get_current()
302 return current_routing;
305 /** @brief Add a link connecting an host to the rest of its AS (which must be cluster or vivaldi) */
306 void sg_platf_new_hostlink(sg_platf_host_link_cbarg_t netcard_arg)
308 simgrid::surf::NetCard *netcard = sg_host_by_name(netcard_arg->id)->pimpl_netcard;
309 xbt_assert(netcard, "Host '%s' not found!", netcard_arg->id);
310 xbt_assert(dynamic_cast<simgrid::surf::AsCluster*>(current_routing) ||
311 dynamic_cast<simgrid::surf::AsVivaldi*>(current_routing),
312 "Only hosts from Cluster and Vivaldi ASes can get a host_link.");
314 s_surf_parsing_link_up_down_t link_up_down;
315 link_up_down.link_up = Link::byName(netcard_arg->link_up);
316 link_up_down.link_down = Link::byName(netcard_arg->link_down);
318 xbt_assert(link_up_down.link_up, "Link '%s' not found!",netcard_arg->link_up);
319 xbt_assert(link_up_down.link_down, "Link '%s' not found!",netcard_arg->link_down);
321 // If dynar is is greater than netcard id and if the host_link is already defined
322 if((int)xbt_dynar_length(current_routing->upDownLinks) > netcard->id() &&
323 xbt_dynar_get_as(current_routing->upDownLinks, netcard->id(), void*))
324 surf_parse_error("Host_link for '%s' is already defined!",netcard_arg->id);
326 XBT_DEBUG("Push Host_link for host '%s' to position %d", netcard->name(), netcard->id());
327 xbt_dynar_set_as(current_routing->upDownLinks, netcard->id(), s_surf_parsing_link_up_down_t, link_up_down);
330 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace)
332 tmgr_trace_t tmgr_trace;
333 if (!trace->file || strcmp(trace->file, "") != 0) {
334 tmgr_trace = tmgr_trace_new_from_file(trace->file);
336 xbt_assert(strcmp(trace->pc_data, ""),
337 "Trace '%s' must have either a content, or point to a file on disk.",trace->id);
338 tmgr_trace = tmgr_trace_new_from_string(trace->id, trace->pc_data, trace->periodicity);
340 xbt_dict_set(traces_set_list, trace->id, (void *) tmgr_trace, NULL);
344 * \brief Make a new routing component to the platform
346 * Add a new autonomous system to the platform. Any elements (such as host,
347 * router or sub-AS) added after this call and before the corresponding call
348 * to sg_platf_new_AS_close() will be added to this AS.
350 * Once this function was called, the configuration concerning the used
351 * models cannot be changed anymore.
353 * @param AS_id name of this autonomous system. Must be unique in the platform
354 * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
356 void routing_AS_begin(sg_platf_AS_cbarg_t AS)
358 XBT_DEBUG("routing_AS_begin");
360 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, AS->id, ROUTING_ASR_LEVEL),
361 "Refusing to create a second AS called \"%s\".", AS->id);
363 _sg_cfg_init_status = 2; /* HACK: direct access to the global controlling the level of configuration to prevent
364 * any further config now that we created some real content */
367 /* search the routing model */
368 simgrid::surf::As *new_as = NULL;
370 case A_surfxml_AS_routing_Cluster: new_as = new simgrid::surf::AsCluster(AS->id); break;
371 case A_surfxml_AS_routing_ClusterTorus: new_as = new simgrid::surf::AsClusterTorus(AS->id); break;
372 case A_surfxml_AS_routing_ClusterFatTree: new_as = new simgrid::surf::AsClusterFatTree(AS->id); break;
373 case A_surfxml_AS_routing_Dijkstra: new_as = new simgrid::surf::AsDijkstra(AS->id, 0); break;
374 case A_surfxml_AS_routing_DijkstraCache: new_as = new simgrid::surf::AsDijkstra(AS->id, 1); break;
375 case A_surfxml_AS_routing_Floyd: new_as = new simgrid::surf::AsFloyd(AS->id); break;
376 case A_surfxml_AS_routing_Full: new_as = new simgrid::surf::AsFull(AS->id); break;
377 case A_surfxml_AS_routing_None: new_as = new simgrid::surf::AsNone(AS->id); break;
378 case A_surfxml_AS_routing_Vivaldi: new_as = new simgrid::surf::AsVivaldi(AS->id); break;
379 default: xbt_die("Not a valid model!"); break;
382 /* make a new routing component */
383 simgrid::surf::NetCard *netcard = new simgrid::surf::NetCardImpl(new_as->name_, SURF_NETWORK_ELEMENT_AS, current_routing);
385 if (current_routing == NULL && routing_platf->root_ == NULL) {
386 /* it is the first one */
387 new_as->father_ = NULL;
388 routing_platf->root_ = new_as;
390 } else if (current_routing != NULL && routing_platf->root_ != NULL) {
392 xbt_assert(!xbt_dict_get_or_null(current_routing->children_, AS->id),
393 "The AS \"%s\" already exists", AS->id);
394 /* it is a part of the tree */
395 new_as->father_ = current_routing;
396 /* set the father behavior */
397 if (current_routing->hierarchy_ == SURF_ROUTING_NULL)
398 current_routing->hierarchy_ = SURF_ROUTING_RECURSIVE;
399 /* add to the sons dictionary */
400 xbt_dict_set(current_routing->children_, AS->id, (void *) new_as, NULL);
401 /* add to the father element list */
402 netcard->setId(current_routing->addComponent(netcard));
404 THROWF(arg_error, 0, "All defined components must belong to a AS");
407 xbt_lib_set(as_router_lib, netcard->name(), ROUTING_ASR_LEVEL, (void *) netcard);
408 XBT_DEBUG("Having set name '%s' id '%d'", new_as->name_, netcard->id());
410 /* set the new current component of the tree */
411 current_routing = new_as;
412 current_routing->netcard_ = netcard;
414 simgrid::surf::netcardCreatedCallbacks(netcard);
415 simgrid::surf::asCreatedCallbacks(new_as);
419 * \brief Specify that the current description of AS is finished
421 * Once you've declared all the content of your AS, you have to close
422 * it with this call. Your AS is not usable until you call this function.
424 void routing_AS_end()
426 xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
427 current_routing->Seal();
428 current_routing = current_routing->father_;
435 * \brief Recursive function for getRouteAndLatency
437 * \param src the source host
438 * \param dst the destination host
439 * \param links Where to store the links and the gw information
440 * \param latency If not NULL, the latency of all links will be added in it
442 void As::getRouteRecursive(NetCard *src, NetCard *dst,
443 /* OUT */ std::vector<Link*> * links, double *latency)
445 s_sg_platf_route_cbarg_t route;
446 memset(&route,0,sizeof(route));
448 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
450 /* Find how src and dst are interconnected */
451 simgrid::surf::As *common_ancestor, *src_ancestor, *dst_ancestor;
452 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
453 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
454 common_ancestor->name_, src_ancestor->name_, dst_ancestor->name_);
456 /* Check whether a direct bypass is defined. If so, use it and bail out */
457 std::vector<Link*> *bypassed_route = common_ancestor->getBypassRoute(src, dst);
458 if (nullptr != bypassed_route) {
459 for (Link *link : *bypassed_route) {
460 links->push_back(link);
462 *latency += link->getLatency();
467 /* If src and dst are in the same AS, life is good */
468 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
469 route.link_list = links;
470 common_ancestor->getRouteAndLatency(src, dst, &route, latency);
474 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
476 route.link_list = new std::vector<Link*>();
478 common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
479 xbt_assert((route.gw_src != NULL) && (route.gw_dst != NULL),
480 "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
482 /* If source gateway is not our source, we have to recursively find our way up to this point */
483 if (src != route.gw_src)
484 getRouteRecursive(src, route.gw_src, links, latency);
485 for (auto link: *route.link_list)
486 links->push_back(link);
488 /* If dest gateway is not our destination, we have to recursively find our way from this point */
489 if (route.gw_dst != dst)
490 getRouteRecursive(route.gw_dst, dst, links, latency);
495 * \brief Find a route between hosts
497 * \param src the network_element_t for src host
498 * \param dst the network_element_t for dst host
499 * \param route where to store the list of links.
500 * If *route=NULL, create a short lived dynar. Else, fill the provided dynar
501 * \param latency where to store the latency experienced on the path (or NULL if not interested)
502 * It is the caller responsability to initialize latency to 0 (we add to provided route)
505 * walk through the routing components tree and find a route between hosts
506 * by calling each "get_route" function in each routing component.
508 void RoutingPlatf::getRouteAndLatency(NetCard *src, NetCard *dst, std::vector<Link*> * route, double *latency)
510 XBT_DEBUG("getRouteAndLatency from %s to %s", src->name(), dst->name());
512 As::getRouteRecursive(src, dst, route, latency);
515 static xbt_dynar_t _recursiveGetOneLinkRoutes(As *rc)
517 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
519 //adding my one link routes
520 xbt_dynar_t onelink_mine = rc->getOneLinkRoutes();
522 xbt_dynar_merge(&ret,&onelink_mine);
526 xbt_dict_cursor_t cursor = NULL;
528 xbt_dict_foreach(rc->children_, cursor, key, rc_child) {
529 xbt_dynar_t onelink_child = _recursiveGetOneLinkRoutes(rc_child);
531 xbt_dynar_merge(&ret,&onelink_child);
536 xbt_dynar_t RoutingPlatf::getOneLinkRoutes(){
537 return _recursiveGetOneLinkRoutes(root_);
543 /** @brief create the root AS */
544 void routing_model_create(Link *loopback)
546 routing_platf = new simgrid::surf::RoutingPlatf(loopback);
549 /* ************************************************************************** */
550 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
552 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
553 simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
555 xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
556 xbt_assert(nullptr == cluster->backbone_, "Cluster %s already has a backbone link!", cluster->name_);
558 cluster->backbone_ = bb;
559 XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name_);
562 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
565 char *groups , *host_id , *link_id = NULL;
567 xbt_dynar_t radical_elements;
568 xbt_dynar_t radical_ends;
571 radical_elements = xbt_str_split(cabinet->radical, ",");
572 xbt_dynar_foreach(radical_elements, iter, groups) {
574 radical_ends = xbt_str_split(groups, "-");
575 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
577 switch (xbt_dynar_length(radical_ends)) {
582 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
585 surf_parse_error("Malformed radical");
588 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
589 memset(&host, 0, sizeof(host));
591 host.core_amount = 1;
593 s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
594 memset(&link, 0, sizeof(link));
595 link.policy = SURF_LINK_FULLDUPLEX;
596 link.latency = cabinet->lat;
597 link.bandwidth = cabinet->bw;
599 s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
600 memset(&host_link, 0, sizeof(host_link));
602 for (i = start; i <= end; i++) {
603 host_id = bprintf("%s%d%s",cabinet->prefix,i,cabinet->suffix);
604 link_id = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
607 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
608 xbt_dynar_push(host.speed_peak,&cabinet->speed);
609 sg_platf_new_host(&host);
610 xbt_dynar_free(&host.speed_peak);
611 sg_platf_new_link(&link);
613 char* link_up = bprintf("%s_UP",link_id);
614 char* link_down = bprintf("%s_DOWN",link_id);
615 host_link.id = host_id;
616 host_link.link_up = link_up;
617 host_link.link_down = link_down;
618 sg_platf_new_hostlink(&host_link);
626 xbt_dynar_free(&radical_ends);
628 xbt_dynar_free(&radical_elements);
631 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
633 using simgrid::surf::NetCard;
634 using simgrid::surf::AsCluster;
636 char *host_id = NULL;
637 char *link_id = NULL;
638 char *router_id = NULL;
641 host_id = bprintf("peer_%s", peer->id);
642 link_id = bprintf("link_%s", peer->id);
643 router_id = bprintf("router_%s", peer->id);
645 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
646 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
648 AS.routing = A_surfxml_AS_routing_Cluster;
649 sg_platf_new_AS_begin(&AS);
651 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
652 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
653 memset(&host, 0, sizeof(host));
656 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
657 xbt_dynar_push(host.speed_peak,&peer->speed);
659 //host.power_peak = peer->power;
660 host.speed_trace = peer->availability_trace;
661 host.state_trace = peer->state_trace;
662 host.core_amount = 1;
663 sg_platf_new_host(&host);
664 xbt_dynar_free(&host.speed_peak);
666 s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
667 memset(&link, 0, sizeof(link));
668 link.policy = SURF_LINK_SHARED;
669 link.latency = peer->lat;
671 char* link_up = bprintf("%s_UP",link_id);
672 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up,
673 peer->bw_out, peer->lat);
675 link.bandwidth = peer->bw_out;
676 sg_platf_new_link(&link);
678 char* link_down = bprintf("%s_DOWN",link_id);
679 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down,
680 peer->bw_in, peer->lat);
682 link.bandwidth = peer->bw_in;
683 sg_platf_new_link(&link);
685 XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
686 s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
687 memset(&host_link, 0, sizeof(host_link));
688 host_link.id = host_id;
689 host_link.link_up = link_up;
690 host_link.link_down = link_down;
691 sg_platf_new_hostlink(&host_link);
693 XBT_DEBUG("<router id=\"%s\"/>", router_id);
694 s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
695 memset(&router, 0, sizeof(router));
696 router.id = router_id;
697 router.coord = peer->coord;
698 sg_platf_new_router(&router);
699 static_cast<AsCluster*>(current_routing)->router_ = static_cast<NetCard*>(xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL));
702 sg_platf_new_AS_end();
705 //xbt_dynar_free(&tab_elements_num);
713 static void check_disk_attachment()
715 xbt_lib_cursor_t cursor;
718 simgrid::surf::NetCard *host_elm;
719 xbt_lib_foreach(storage_lib, cursor, key, data) {
720 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
721 simgrid::surf::Storage *storage = static_cast<simgrid::surf::Storage*>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
722 host_elm = sg_netcard_by_name_or_null(storage->p_attach);
724 surf_parse_error("Unable to attach storage %s: host %s doesn't exist.", storage->getName(), storage->p_attach);
729 void routing_register_callbacks()
731 simgrid::surf::on_postparse.connect(check_disk_attachment);
733 instr_routing_define_callbacks();
736 /** \brief Frees all memory allocated by the routing module */
737 void routing_exit(void) {
738 delete routing_platf;
744 RoutingPlatf::RoutingPlatf(Link *loopback)
745 : loopback_(loopback)
748 RoutingPlatf::~RoutingPlatf()
756 AS_t surf_AS_get_routing_root() {
757 return routing_platf->root_;
760 const char *surf_AS_get_name(simgrid::surf::As *as) {
764 static simgrid::surf::As *surf_AS_recursive_get_by_name(simgrid::surf::As *current, const char * name)
766 xbt_dict_cursor_t cursor = NULL;
769 simgrid::surf::As *tmp = NULL;
771 if(!strcmp(current->name_, name))
774 xbt_dict_foreach(current->children_, cursor, key, elem) {
775 tmp = surf_AS_recursive_get_by_name(elem, name);
783 simgrid::surf::As *surf_AS_get_by_name(const char * name)
785 simgrid::surf::As *as = surf_AS_recursive_get_by_name(routing_platf->root_, name);
787 XBT_WARN("Impossible to find an AS with name %s, please check your input", name);
791 xbt_dict_t surf_AS_get_children(simgrid::surf::As *as)
793 return as->children_;
796 xbt_dynar_t surf_AS_get_hosts(simgrid::surf::As *as)
798 xbt_dynar_t elms = as->vertices_;
799 int count = xbt_dynar_length(elms);
800 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t), NULL);
801 for (int index = 0; index < count; index++) {
803 xbt_dynar_get_as(elms, index, simgrid::surf::NetCard*);
804 sg_host_t delm = simgrid::s4u::Host::by_name_or_null(relm->name());
806 xbt_dynar_push(res, &delm);
812 void surf_AS_get_graph(AS_t as, xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges) {
813 as->getGraph(graph, nodes, edges);