Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / surf / surf_routing.cpp
1 /* Copyright (c) 2009-2011, 2013-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.hpp"
8 #include "surf_routing_cluster.hpp"
9
10 #include "simgrid/sg_config.h"
11 #include "storage_interface.hpp"
12
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
20
21 #include <vector>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
24
25 namespace simgrid {
26 namespace surf {
27
28   /* Callbacks */
29   simgrid::xbt::signal<void(simgrid::surf::NetCard*)> netcardCreatedCallbacks;
30   simgrid::xbt::signal<void(simgrid::s4u::As*)> asCreatedCallbacks;
31
32
33 }} // namespace simgrid::surf
34
35 /**
36  * @ingroup SURF_build_api
37  * @brief A library containing all known hosts
38  */
39 xbt_dict_t host_list = nullptr;
40
41 int COORD_HOST_LEVEL=0;         //Coordinates level
42
43 int MSG_FILE_LEVEL;             //Msg file level
44
45 int SIMIX_STORAGE_LEVEL;        //Simix storage level
46 int MSG_STORAGE_LEVEL;          //Msg storage level
47
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
53
54 /** @brief Retrieve a netcard from its name
55  *
56  * Netcards are the thing that connect host or routers to the network
57  */
58 simgrid::surf::NetCard *sg_netcard_by_name_or_null(const char *name)
59 {
60   sg_host_t h = sg_host_by_name(name);
61   simgrid::surf::NetCard *netcard = h==NULL ? NULL: h->pimpl_netcard;
62   if (!netcard)
63     netcard = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
64   return netcard;
65 }
66
67 /* Global vars */
68 simgrid::surf::RoutingPlatf *routing_platf = NULL;
69
70
71 /** The current AS in the parsing */
72 static simgrid::s4u::As *current_routing = NULL;
73 simgrid::s4u::As* routing_get_current()
74 {
75   return current_routing;
76 }
77
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)
80 {
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.");
86
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);
90
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);
93
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);
98
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);
101 }
102
103 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace)
104 {
105   tmgr_trace_t tmgr_trace;
106   if (!trace->file || strcmp(trace->file, "") != 0) {
107     tmgr_trace = tmgr_trace_new_from_file(trace->file);
108   } else {
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);
112   }
113   xbt_dict_set(traces_set_list, trace->id, (void *) tmgr_trace, NULL);
114 }
115
116 /**
117  * \brief Make a new routing component to the platform
118  *
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.
122  *
123  * Once this function was called, the configuration concerning the used
124  * models cannot be changed anymore.
125  *
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
128  */
129 void routing_AS_begin(sg_platf_AS_cbarg_t AS)
130 {
131   XBT_DEBUG("routing_AS_begin");
132
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);
135
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 */
138
139
140   /* search the routing model */
141   simgrid::s4u::As *new_as = NULL;
142   switch(AS->routing){
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;
153   }
154
155   /* make a new routing component */
156   simgrid::surf::NetCard *netcard = new simgrid::surf::NetCardImpl(new_as->name(), SURF_NETWORK_ELEMENT_AS, current_routing);
157
158   if (current_routing == NULL && routing_platf->root_ == NULL) {
159     /* it is the first one */
160     routing_platf->root_ = new_as;
161     netcard->setId(-1);
162   } else if (current_routing != NULL && routing_platf->root_ != NULL) {
163
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));
175   } else {
176     THROWF(arg_error, 0, "All defined components must belong to a AS");
177   }
178
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());
181
182   /* set the new current component of the tree */
183   current_routing = new_as;
184   current_routing->netcard_ = netcard;
185
186   simgrid::surf::netcardCreatedCallbacks(netcard);
187   simgrid::surf::asCreatedCallbacks(new_as);
188 }
189
190 /**
191  * \brief Specify that the current description of AS is finished
192  *
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.
195  */
196 void routing_AS_end()
197 {
198   xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
199   current_routing->Seal();
200   current_routing = current_routing->father();
201 }
202
203 namespace simgrid {
204 namespace surf {
205
206 /**
207  * \brief Find a route between hosts
208  *
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)
215  * \pre route!=NULL
216  *
217  * walk through the routing components tree and find a route between hosts
218  * by calling each "get_route" function in each routing component.
219  */
220 void RoutingPlatf::getRouteAndLatency(NetCard *src, NetCard *dst, std::vector<Link*> * route, double *latency)
221 {
222   XBT_DEBUG("getRouteAndLatency from %s to %s", src->name(), dst->name());
223
224   s4u::As::getRouteRecursive(src, dst, route, latency);
225 }
226
227 static xbt_dynar_t _recursiveGetOneLinkRoutes(s4u::As *as)
228 {
229   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
230
231   //adding my one link routes
232   xbt_dynar_t onelink_mine = as->getOneLinkRoutes();
233   if (onelink_mine)
234     xbt_dynar_merge(&ret,&onelink_mine);
235
236   //recursing
237   char *key;
238   xbt_dict_cursor_t cursor = NULL;
239   AS_t rc_child;
240   xbt_dict_foreach(as->children(), cursor, key, rc_child) {
241     xbt_dynar_t onelink_child = _recursiveGetOneLinkRoutes(rc_child);
242     if (onelink_child)
243       xbt_dynar_merge(&ret,&onelink_child);
244   }
245   return ret;
246 }
247
248 xbt_dynar_t RoutingPlatf::getOneLinkRoutes(){
249   return _recursiveGetOneLinkRoutes(root_);
250 }
251
252 }
253 }
254
255 /** @brief create the root AS */
256 void routing_model_create(Link *loopback)
257 {
258   routing_platf = new simgrid::surf::RoutingPlatf(loopback);
259 }
260
261 /* ************************************************************************** */
262 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
263
264 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
265   simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
266
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());
269
270   cluster->backbone_ = bb;
271   XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name());
272 }
273
274 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
275 {
276   int start, end, i;
277   char *groups , *host_id , *link_id = NULL;
278   unsigned int iter;
279   xbt_dynar_t radical_elements;
280   xbt_dynar_t radical_ends;
281
282   //Make all hosts
283   radical_elements = xbt_str_split(cabinet->radical, ",");
284   xbt_dynar_foreach(radical_elements, iter, groups) {
285
286     radical_ends = xbt_str_split(groups, "-");
287     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
288
289     switch (xbt_dynar_length(radical_ends)) {
290     case 1:
291       end = start;
292       break;
293     case 2:
294       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
295       break;
296     default:
297       surf_parse_error("Malformed radical");
298       break;
299     }
300     s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
301     memset(&host, 0, sizeof(host));
302     host.pstate        = 0;
303     host.core_amount   = 1;
304
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;
310
311     s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
312     memset(&host_link, 0, sizeof(host_link));
313
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);
317       host.id                      = host_id;
318       link.id                      = link_id;
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);
324
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);
331
332       free(host_id);
333       free(link_id);
334       free(link_up);
335       free(link_down);
336     }
337
338     xbt_dynar_free(&radical_ends);
339   }
340   xbt_dynar_free(&radical_elements);
341 }
342
343 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
344 {
345   using simgrid::surf::NetCard;
346   using simgrid::surf::AsCluster;
347
348   char *host_id = NULL;
349   char *link_id = NULL;
350   char *router_id = NULL;
351
352   XBT_DEBUG(" ");
353   host_id = bprintf("peer_%s", peer->id);
354   link_id = bprintf("link_%s", peer->id);
355   router_id = bprintf("router_%s", peer->id);
356
357   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
358   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
359   AS.id                    = peer->id;
360   AS.routing               = A_surfxml_AS_routing_Cluster;
361   sg_platf_new_AS_begin(&AS);
362
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));
366   host.id = host_id;
367
368   host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
369   xbt_dynar_push(host.speed_peak,&peer->speed);
370   host.pstate = 0;
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);
377
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;
382
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);
386   link.id = link_up;
387   link.bandwidth = peer->bw_out;
388   sg_platf_new_link(&link);
389
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);
393   link.id = link_down;
394   link.bandwidth = peer->bw_in;
395   sg_platf_new_link(&link);
396
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);
404
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));
412
413   XBT_DEBUG("</AS>");
414   sg_platf_new_AS_end();
415   XBT_DEBUG(" ");
416
417   //xbt_dynar_free(&tab_elements_num);
418   free(router_id);
419   free(host_id);
420   free(link_id);
421   free(link_up);
422   free(link_down);
423 }
424
425 static void check_disk_attachment()
426 {
427   xbt_lib_cursor_t cursor;
428   char *key;
429   void **data;
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);
435     if(!host_elm)
436       surf_parse_error("Unable to attach storage %s: host %s doesn't exist.", storage->getName(), storage->p_attach);
437     }
438   }
439 }
440
441 void routing_register_callbacks()
442 {
443   simgrid::surf::on_postparse.connect(check_disk_attachment);
444
445   instr_routing_define_callbacks();
446 }
447
448 /** \brief Frees all memory allocated by the routing module */
449 void routing_exit(void) {
450   delete routing_platf;
451 }
452
453 simgrid::surf::RoutingPlatf::RoutingPlatf(simgrid::surf::Link *loopback)
454 : loopback_(loopback)
455 {
456 }
457 simgrid::surf::RoutingPlatf::~RoutingPlatf()
458 {
459   delete root_;
460 }