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::s4u::As*)> asCreatedCallbacks;
33 }} // namespace simgrid::surf
36 * @ingroup SURF_build_api
37 * @brief A library containing all known hosts
39 xbt_dict_t host_list = nullptr;
41 int COORD_HOST_LEVEL=0; //Coordinates level
43 int MSG_FILE_LEVEL; //Msg file level
45 int SIMIX_STORAGE_LEVEL; //Simix storage level
46 int MSG_STORAGE_LEVEL; //Msg storage level
48 xbt_lib_t as_router_lib;
49 int ROUTING_ASR_LEVEL; //Routing level
50 int COORD_ASR_LEVEL; //Coordinates level
51 int NS3_ASR_LEVEL; //host node for ns3
52 int ROUTING_PROP_ASR_LEVEL; //Where the properties are stored
54 /** @brief Retrieve a netcard from its name
56 * Netcards are the thing that connect host or routers to the network
58 simgrid::surf::NetCard *sg_netcard_by_name_or_null(const char *name)
60 sg_host_t h = sg_host_by_name(name);
61 simgrid::surf::NetCard *netcard = h==NULL ? NULL: h->pimpl_netcard;
63 netcard = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
68 simgrid::surf::RoutingPlatf *routing_platf = NULL;
71 /** The current AS in the parsing */
72 static simgrid::s4u::As *current_routing = NULL;
73 simgrid::s4u::As* routing_get_current()
75 return current_routing;
78 /** @brief Add a link connecting an host to the rest of its AS (which must be cluster or vivaldi) */
79 void sg_platf_new_hostlink(sg_platf_host_link_cbarg_t netcard_arg)
81 simgrid::surf::NetCard *netcard = sg_host_by_name(netcard_arg->id)->pimpl_netcard;
82 xbt_assert(netcard, "Host '%s' not found!", netcard_arg->id);
83 xbt_assert(dynamic_cast<simgrid::surf::AsCluster*>(current_routing) ||
84 dynamic_cast<simgrid::surf::AsVivaldi*>(current_routing),
85 "Only hosts from Cluster and Vivaldi ASes can get a host_link.");
87 s_surf_parsing_link_up_down_t link_up_down;
88 link_up_down.link_up = Link::byName(netcard_arg->link_up);
89 link_up_down.link_down = Link::byName(netcard_arg->link_down);
91 xbt_assert(link_up_down.link_up, "Link '%s' not found!",netcard_arg->link_up);
92 xbt_assert(link_up_down.link_down, "Link '%s' not found!",netcard_arg->link_down);
94 // If dynar is is greater than netcard id and if the host_link is already defined
95 if((int)xbt_dynar_length(current_routing->upDownLinks) > netcard->id() &&
96 xbt_dynar_get_as(current_routing->upDownLinks, netcard->id(), void*))
97 surf_parse_error("Host_link for '%s' is already defined!",netcard_arg->id);
99 XBT_DEBUG("Push Host_link for host '%s' to position %d", netcard->name(), netcard->id());
100 xbt_dynar_set_as(current_routing->upDownLinks, netcard->id(), s_surf_parsing_link_up_down_t, link_up_down);
103 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace)
105 tmgr_trace_t tmgr_trace;
106 if (!trace->file || strcmp(trace->file, "") != 0) {
107 tmgr_trace = tmgr_trace_new_from_file(trace->file);
109 xbt_assert(strcmp(trace->pc_data, ""),
110 "Trace '%s' must have either a content, or point to a file on disk.",trace->id);
111 tmgr_trace = tmgr_trace_new_from_string(trace->id, trace->pc_data, trace->periodicity);
113 xbt_dict_set(traces_set_list, trace->id, (void *) tmgr_trace, NULL);
117 * \brief Make a new routing component to the platform
119 * Add a new autonomous system to the platform. Any elements (such as host,
120 * router or sub-AS) added after this call and before the corresponding call
121 * to sg_platf_new_AS_close() will be added to this AS.
123 * Once this function was called, the configuration concerning the used
124 * models cannot be changed anymore.
126 * @param AS_id name of this autonomous system. Must be unique in the platform
127 * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
129 void routing_AS_begin(sg_platf_AS_cbarg_t AS)
131 XBT_DEBUG("routing_AS_begin");
133 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, AS->id, ROUTING_ASR_LEVEL),
134 "Refusing to create a second AS called \"%s\".", AS->id);
136 _sg_cfg_init_status = 2; /* HACK: direct access to the global controlling the level of configuration to prevent
137 * any further config now that we created some real content */
140 /* search the routing model */
141 simgrid::s4u::As *new_as = NULL;
143 case A_surfxml_AS_routing_Cluster: new_as = new simgrid::surf::AsCluster(AS->id); break;
144 case A_surfxml_AS_routing_ClusterTorus: new_as = new simgrid::surf::AsClusterTorus(AS->id); break;
145 case A_surfxml_AS_routing_ClusterFatTree: new_as = new simgrid::surf::AsClusterFatTree(AS->id); break;
146 case A_surfxml_AS_routing_Dijkstra: new_as = new simgrid::surf::AsDijkstra(AS->id, 0); break;
147 case A_surfxml_AS_routing_DijkstraCache: new_as = new simgrid::surf::AsDijkstra(AS->id, 1); break;
148 case A_surfxml_AS_routing_Floyd: new_as = new simgrid::surf::AsFloyd(AS->id); break;
149 case A_surfxml_AS_routing_Full: new_as = new simgrid::surf::AsFull(AS->id); break;
150 case A_surfxml_AS_routing_None: new_as = new simgrid::surf::AsNone(AS->id); break;
151 case A_surfxml_AS_routing_Vivaldi: new_as = new simgrid::surf::AsVivaldi(AS->id); break;
152 default: xbt_die("Not a valid model!"); break;
155 /* make a new routing component */
156 simgrid::surf::NetCard *netcard = new simgrid::surf::NetCardImpl(new_as->name(), SURF_NETWORK_ELEMENT_AS, current_routing);
158 if (current_routing == NULL && routing_platf->root_ == NULL) {
159 /* it is the first one */
160 routing_platf->root_ = new_as;
162 } else if (current_routing != NULL && routing_platf->root_ != NULL) {
164 xbt_assert(!xbt_dict_get_or_null(current_routing->children(), AS->id),
165 "The AS \"%s\" already exists", AS->id);
166 /* it is a part of the tree */
167 new_as->father_ = current_routing;
168 /* set the father behavior */
169 if (current_routing->hierarchy_ == simgrid::s4u::As::ROUTING_NULL)
170 current_routing->hierarchy_ = simgrid::s4u::As::ROUTING_RECURSIVE;
171 /* add to the sons dictionary */
172 xbt_dict_set(current_routing->children(), AS->id, (void *) new_as, NULL);
173 /* add to the father element list */
174 netcard->setId(current_routing->addComponent(netcard));
176 THROWF(arg_error, 0, "All defined components must belong to a AS");
179 xbt_lib_set(as_router_lib, netcard->name(), ROUTING_ASR_LEVEL, (void *) netcard);
180 XBT_DEBUG("Having set name '%s' id '%d'", new_as->name(), netcard->id());
182 /* set the new current component of the tree */
183 current_routing = new_as;
184 current_routing->netcard_ = netcard;
186 simgrid::surf::netcardCreatedCallbacks(netcard);
187 simgrid::surf::asCreatedCallbacks(new_as);
191 * \brief Specify that the current description of AS is finished
193 * Once you've declared all the content of your AS, you have to close
194 * it with this call. Your AS is not usable until you call this function.
196 void routing_AS_end()
198 xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
199 current_routing->Seal();
200 current_routing = current_routing->father();
207 * \brief Find a route between hosts
209 * \param src the network_element_t for src host
210 * \param dst the network_element_t for dst host
211 * \param route where to store the list of links.
212 * If *route=NULL, create a short lived dynar. Else, fill the provided dynar
213 * \param latency where to store the latency experienced on the path (or NULL if not interested)
214 * It is the caller responsability to initialize latency to 0 (we add to provided route)
217 * walk through the routing components tree and find a route between hosts
218 * by calling each "get_route" function in each routing component.
220 void RoutingPlatf::getRouteAndLatency(NetCard *src, NetCard *dst, std::vector<Link*> * route, double *latency)
222 XBT_DEBUG("getRouteAndLatency from %s to %s", src->name(), dst->name());
224 s4u::As::getRouteRecursive(src, dst, route, latency);
227 static xbt_dynar_t _recursiveGetOneLinkRoutes(s4u::As *as)
229 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
231 //adding my one link routes
232 xbt_dynar_t onelink_mine = as->getOneLinkRoutes();
234 xbt_dynar_merge(&ret,&onelink_mine);
238 xbt_dict_cursor_t cursor = NULL;
240 xbt_dict_foreach(as->children(), cursor, key, rc_child) {
241 xbt_dynar_t onelink_child = _recursiveGetOneLinkRoutes(rc_child);
243 xbt_dynar_merge(&ret,&onelink_child);
248 xbt_dynar_t RoutingPlatf::getOneLinkRoutes(){
249 return _recursiveGetOneLinkRoutes(root_);
255 /** @brief create the root AS */
256 void routing_model_create(Link *loopback)
258 routing_platf = new simgrid::surf::RoutingPlatf(loopback);
261 /* ************************************************************************** */
262 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
264 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
265 simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
267 xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
268 xbt_assert(nullptr == cluster->backbone_, "Cluster %s already has a backbone link!", cluster->name());
270 cluster->backbone_ = bb;
271 XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name());
274 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
277 char *groups , *host_id , *link_id = NULL;
279 xbt_dynar_t radical_elements;
280 xbt_dynar_t radical_ends;
283 radical_elements = xbt_str_split(cabinet->radical, ",");
284 xbt_dynar_foreach(radical_elements, iter, groups) {
286 radical_ends = xbt_str_split(groups, "-");
287 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
289 switch (xbt_dynar_length(radical_ends)) {
294 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
297 surf_parse_error("Malformed radical");
300 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
301 memset(&host, 0, sizeof(host));
303 host.core_amount = 1;
305 s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
306 memset(&link, 0, sizeof(link));
307 link.policy = SURF_LINK_FULLDUPLEX;
308 link.latency = cabinet->lat;
309 link.bandwidth = cabinet->bw;
311 s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
312 memset(&host_link, 0, sizeof(host_link));
314 for (i = start; i <= end; i++) {
315 host_id = bprintf("%s%d%s",cabinet->prefix,i,cabinet->suffix);
316 link_id = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
319 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
320 xbt_dynar_push(host.speed_peak,&cabinet->speed);
321 sg_platf_new_host(&host);
322 xbt_dynar_free(&host.speed_peak);
323 sg_platf_new_link(&link);
325 char* link_up = bprintf("%s_UP",link_id);
326 char* link_down = bprintf("%s_DOWN",link_id);
327 host_link.id = host_id;
328 host_link.link_up = link_up;
329 host_link.link_down = link_down;
330 sg_platf_new_hostlink(&host_link);
338 xbt_dynar_free(&radical_ends);
340 xbt_dynar_free(&radical_elements);
343 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
345 using simgrid::surf::NetCard;
346 using simgrid::surf::AsCluster;
348 char *host_id = NULL;
349 char *link_id = NULL;
350 char *router_id = NULL;
353 host_id = bprintf("peer_%s", peer->id);
354 link_id = bprintf("link_%s", peer->id);
355 router_id = bprintf("router_%s", peer->id);
357 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
358 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
360 AS.routing = A_surfxml_AS_routing_Cluster;
361 sg_platf_new_AS_begin(&AS);
363 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
364 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
365 memset(&host, 0, sizeof(host));
368 host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
369 xbt_dynar_push(host.speed_peak,&peer->speed);
371 //host.power_peak = peer->power;
372 host.speed_trace = peer->availability_trace;
373 host.state_trace = peer->state_trace;
374 host.core_amount = 1;
375 sg_platf_new_host(&host);
376 xbt_dynar_free(&host.speed_peak);
378 s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
379 memset(&link, 0, sizeof(link));
380 link.policy = SURF_LINK_SHARED;
381 link.latency = peer->lat;
383 char* link_up = bprintf("%s_UP",link_id);
384 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up,
385 peer->bw_out, peer->lat);
387 link.bandwidth = peer->bw_out;
388 sg_platf_new_link(&link);
390 char* link_down = bprintf("%s_DOWN",link_id);
391 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down,
392 peer->bw_in, peer->lat);
394 link.bandwidth = peer->bw_in;
395 sg_platf_new_link(&link);
397 XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
398 s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
399 memset(&host_link, 0, sizeof(host_link));
400 host_link.id = host_id;
401 host_link.link_up = link_up;
402 host_link.link_down = link_down;
403 sg_platf_new_hostlink(&host_link);
405 XBT_DEBUG("<router id=\"%s\"/>", router_id);
406 s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
407 memset(&router, 0, sizeof(router));
408 router.id = router_id;
409 router.coord = peer->coord;
410 sg_platf_new_router(&router);
411 static_cast<AsCluster*>(current_routing)->router_ = static_cast<NetCard*>(xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL));
414 sg_platf_new_AS_end();
417 //xbt_dynar_free(&tab_elements_num);
425 static void check_disk_attachment()
427 xbt_lib_cursor_t cursor;
430 simgrid::surf::NetCard *host_elm;
431 xbt_lib_foreach(storage_lib, cursor, key, data) {
432 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
433 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));
434 host_elm = sg_netcard_by_name_or_null(storage->p_attach);
436 surf_parse_error("Unable to attach storage %s: host %s doesn't exist.", storage->getName(), storage->p_attach);
441 void routing_register_callbacks()
443 simgrid::surf::on_postparse.connect(check_disk_attachment);
445 instr_routing_define_callbacks();
448 /** \brief Frees all memory allocated by the routing module */
449 void routing_exit(void) {
450 delete routing_platf;
453 simgrid::surf::RoutingPlatf::RoutingPlatf(simgrid::surf::Link *loopback)
454 : loopback_(loopback)
457 simgrid::surf::RoutingPlatf::~RoutingPlatf()