Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in cluster parsing
[simgrid.git] / src / surf / sg_platf.cpp
1 /* Copyright (c) 2006-2014. 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 "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "xbt/RngStream.h"
12 #include <xbt/signal.hpp>
13 #include "src/surf/HostImpl.hpp"
14 #include "surf/surf.h"
15
16 #include "src/simix/smx_private.h"
17
18 #include "src/include/simgrid/sg_config.h"
19 #include "src/surf/xml/platf_private.hpp"
20
21 #include "src/surf/cpu_interface.hpp"
22 #include "src/surf/network_interface.hpp"
23 #include "surf/surf_routing.h" // FIXME: brain dead public header
24
25 #include "src/surf/AsImpl.hpp"
26 #include "src/surf/AsCluster.hpp"
27 #include "src/surf/AsClusterTorus.hpp"
28 #include "src/surf/AsClusterFatTree.hpp"
29 #include "src/surf/AsDijkstra.hpp"
30 #include "src/surf/AsFloyd.hpp"
31 #include "src/surf/AsFull.hpp"
32 #include "src/surf/AsNone.hpp"
33 #include "src/surf/AsVivaldi.hpp"
34
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
36
37 XBT_PRIVATE xbt_dynar_t mount_list = NULL;
38
39 namespace simgrid {
40 namespace surf {
41
42 simgrid::xbt::signal<void(sg_platf_link_cbarg_t)> on_link;
43 simgrid::xbt::signal<void(sg_platf_cluster_cbarg_t)> on_cluster;
44 simgrid::xbt::signal<void(void)> on_postparse;
45
46 }
47 }
48
49 static int surf_parse_models_setup_already_called = 0;
50
51 /** The current AS in the parsing */
52 static simgrid::surf::AsImpl *current_routing = NULL;
53 static simgrid::surf::AsImpl* routing_get_current()
54 {
55   return current_routing;
56 }
57
58 /** Module management function: creates all internal data structures */
59 void sg_platf_init(void) {
60 }
61
62 /** Module management function: frees all internal data structures */
63 void sg_platf_exit(void) {
64   simgrid::surf::on_link.disconnect_all_slots();
65   simgrid::surf::on_cluster.disconnect_all_slots();
66   simgrid::surf::on_postparse.disconnect_all_slots();
67
68   /* make sure that we will reinit the models while loading the platf once reinited */
69   surf_parse_models_setup_already_called = 0;
70   surf_parse_lex_destroy();
71 }
72
73 /** @brief Add an "host" to the current AS */
74 void sg_platf_new_host(sg_platf_host_cbarg_t host)
75 {
76   xbt_assert(! sg_host_by_name(host->id), "Refusing to create a second host named '%s'.", host->id);
77
78   simgrid::surf::AsImpl* current_routing = routing_get_current();
79   if (current_routing->hierarchy_ == simgrid::surf::AsImpl::RoutingMode::unset)
80     current_routing->hierarchy_ = simgrid::surf::AsImpl::RoutingMode::base;
81
82   simgrid::surf::NetCard *netcard =
83       new simgrid::surf::NetCardImpl(host->id, simgrid::surf::NetCard::Type::Host, current_routing);
84
85   sg_host_t h = simgrid::s4u::Host::by_name_or_create(host->id);
86   h->pimpl_netcard = netcard;
87
88   if(mount_list){
89     xbt_lib_set(storage_lib, host->id, ROUTING_STORAGE_HOST_LEVEL, (void *) mount_list);
90     mount_list = NULL;
91   }
92
93   if (host->coord && strcmp(host->coord, "")) {
94     unsigned int cursor;
95     char*str;
96
97     xbt_assert(COORD_HOST_LEVEL, "To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
98     /* Pre-parse the host coordinates -- FIXME factorize with routers by overloading the routing->parse_PU function*/
99     xbt_dynar_t ctn_str = xbt_str_split_str(host->coord, " ");
100     xbt_assert(xbt_dynar_length(ctn_str)==3,"Coordinates of %s must have 3 dimensions", host->id);
101
102     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
103     xbt_dynar_foreach(ctn_str,cursor, str) {
104       double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
105       xbt_dynar_push(ctn,&val);
106     }
107     xbt_dynar_free(&ctn_str);
108     xbt_dynar_shrink(ctn, 0);
109     h->extension_set(COORD_HOST_LEVEL, (void *) ctn);
110   }
111
112   simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu( h, host->speed_per_pstate, host->core_amount);
113   if (host->state_trace)
114     cpu->setStateTrace(host->state_trace);
115   if (host->speed_trace)
116     cpu->setSpeedTrace(host->speed_trace);
117   surf_host_model->createHost(host->id, netcard, cpu, host->properties)->attach(h);
118
119   if (host->pstate != 0)
120     cpu->setPState(host->pstate);
121
122   simgrid::s4u::Host::onCreation(*h);
123
124   if (TRACE_is_enabled() && TRACE_needs_platform())
125     sg_instr_new_host(host);
126 }
127
128 /** @brief Add a "router" to the network element list */
129 void sg_platf_new_router(sg_platf_router_cbarg_t router)
130 {
131   simgrid::surf::AsImpl* current_routing = routing_get_current();
132
133   if (current_routing->hierarchy_ == simgrid::surf::AsImpl::RoutingMode::unset)
134     current_routing->hierarchy_ = simgrid::surf::AsImpl::RoutingMode::base;
135   xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
136              "Refusing to create a router named '%s': this name already describes a node.", router->id);
137
138   simgrid::surf::NetCard *netcard =
139     new simgrid::surf::NetCardImpl(router->id, simgrid::surf::NetCard::Type::Router, current_routing);
140   xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) netcard);
141   XBT_DEBUG("Having set name '%s' id '%d'", router->id, netcard->id());
142
143   if (router->coord && strcmp(router->coord, "")) {
144     unsigned int cursor;
145     char*str;
146
147     xbt_assert(COORD_ASR_LEVEL, "To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
148     /* Pre-parse the host coordinates */
149     xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
150     xbt_assert(xbt_dynar_length(ctn_str)==3,"Coordinates of %s must have 3 dimensions", router->id);
151     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
152     xbt_dynar_foreach(ctn_str,cursor, str) {
153       double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
154       xbt_dynar_push(ctn,&val);
155     }
156     xbt_dynar_free(&ctn_str);
157     xbt_dynar_shrink(ctn, 0);
158     xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
159   }
160
161   if (TRACE_is_enabled() && TRACE_needs_platform())
162     sg_instr_new_router(router);
163 }
164
165 void sg_platf_new_link(sg_platf_link_cbarg_t link){
166   std::vector<char*> names;
167
168   if (link->policy == SURF_LINK_FULLDUPLEX) {
169     names.push_back(bprintf("%s_UP", link->id));
170     names.push_back(bprintf("%s_DOWN", link->id));
171   } else {
172     names.push_back(xbt_strdup(link->id));
173   }
174   for (auto link_name : names) {
175     Link *l = surf_network_model->createLink(link_name, link->bandwidth, link->latency, link->policy, link->properties);
176
177     if (link->latency_trace)
178       l->setLatencyTrace(link->latency_trace);
179     if (link->bandwidth_trace)
180       l->setBandwidthTrace(link->bandwidth_trace);
181     if (link->state_trace)
182       l->setStateTrace(link->state_trace);
183
184     xbt_free(link_name);
185   }
186
187   simgrid::surf::on_link(link);
188 }
189
190 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
191 {
192   using simgrid::surf::AsCluster;
193   using simgrid::surf::AsClusterTorus;
194   using simgrid::surf::AsClusterFatTree;
195
196   char *host_id, *groups, *link_id = NULL;
197   xbt_dict_t patterns = NULL;
198   int rankId=0;
199
200   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
201   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
202
203   unsigned int iter;
204
205   if ((cluster->availability_trace && strcmp(cluster->availability_trace, ""))
206       || (cluster->state_trace && strcmp(cluster->state_trace, ""))) {
207     patterns = xbt_dict_new_homogeneous(xbt_free_f);
208     xbt_dict_set(patterns, "id", xbt_strdup(cluster->id), NULL);
209     xbt_dict_set(patterns, "prefix", xbt_strdup(cluster->prefix), NULL);
210     xbt_dict_set(patterns, "suffix", xbt_strdup(cluster->suffix), NULL);
211   }
212
213   /* Parse the topology attributes.
214    * Nothing to do in a vanilla cluster, but that's another story for torus and flat_trees */
215   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
216   AS.id = cluster->id;
217
218   switch (cluster->topology) {
219   case SURF_CLUSTER_TORUS:
220     AS.routing = A_surfxml_AS_routing_ClusterTorus;
221     break;
222   case SURF_CLUSTER_FAT_TREE:
223     AS.routing = A_surfxml_AS_routing_ClusterFatTree;
224     break;
225   default:
226     AS.routing = A_surfxml_AS_routing_Cluster;
227     break;
228   }
229
230   // What an inventive way of initializing the AS that I have as ancestor :-(
231   sg_platf_new_AS_begin(&AS);
232   simgrid::surf::AsCluster *current_as = static_cast<AsCluster*>(routing_get_current());
233   current_as->parse_specific_arguments(cluster);
234
235   if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
236     current_as->nb_links_per_node_++;
237     current_as->has_loopback_ = 1;
238   }
239
240   if(cluster->limiter_link!=0){
241     current_as->nb_links_per_node_++;
242     current_as->has_limiter_ = 1;
243   }
244
245   //Make all hosts
246   xbt_dynar_t radical_elements = xbt_str_split(cluster->radical, ",");
247   xbt_dynar_foreach(radical_elements, iter, groups) {
248
249     xbt_dynar_t radical_ends = xbt_str_split(groups, "-");
250     int start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
251     int end;
252
253     switch (xbt_dynar_length(radical_ends)) {
254     case 1:
255       end = start;
256       break;
257     case 2:
258       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
259       break;
260     default:
261       surf_parse_error("Malformed radical");
262       break;
263     }
264     for (int i = start; i <= end; i++) {
265       host_id = bprintf("%s%d%s", cluster->prefix, i, cluster->suffix);
266       link_id = bprintf("%s_link_%d", cluster->id, i);
267
268       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, cluster->speed);
269
270       memset(&host, 0, sizeof(host));
271       host.id = host_id;
272       if ((cluster->properties != NULL) && (!xbt_dict_is_empty(cluster->properties))) {
273         xbt_dict_cursor_t cursor=NULL;
274         char *key,*data;
275         host.properties = xbt_dict_new();
276
277         xbt_dict_foreach(cluster->properties,cursor,key,data) {
278           xbt_dict_set(host.properties, key, xbt_strdup(data),free);
279         }
280       }
281       if (cluster->availability_trace && strcmp(cluster->availability_trace, "")) {
282         xbt_dict_set(patterns, "radical", bprintf("%d", i), NULL);
283         char *avail_file = xbt_str_varsubst(cluster->availability_trace, patterns);
284         XBT_DEBUG("\tavailability_file=\"%s\"", avail_file);
285         if (avail_file && avail_file[0])
286           host.speed_trace = tmgr_trace_new_from_file(avail_file);
287         xbt_free(avail_file);
288       } else {
289         XBT_DEBUG("\tavailability_file=\"\"");
290       }
291
292       if (cluster->state_trace && strcmp(cluster->state_trace, "")) {
293         char *avail_file = xbt_str_varsubst(cluster->state_trace, patterns);
294         XBT_DEBUG("\tstate_file=\"%s\"", avail_file);
295         if (avail_file && avail_file[0])
296           host.state_trace = tmgr_trace_new_from_file(avail_file);
297         xbt_free(avail_file);
298       } else {
299         XBT_DEBUG("\tstate_file=\"\"");
300       }
301
302       host.speed_per_pstate = xbt_dynar_new(sizeof(double), NULL);
303       xbt_dynar_push(host.speed_per_pstate,&cluster->speed);
304       host.pstate = 0;
305
306       //host.power_peak = cluster->power;
307       host.core_amount = cluster->core_amount;
308       host.coord = "";
309       sg_platf_new_host(&host);
310       xbt_dynar_free(&host.speed_per_pstate);
311       XBT_DEBUG("</host>");
312
313       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id, cluster->bw, cluster->lat);
314
315       s_surf_parsing_link_up_down_t info_lim, info_loop;
316       // All links are saved in a matrix;
317       // every row describes a single node; every node
318       // may have multiple links.
319       // the first column may store a link from x to x if p_has_loopback is set
320       // the second column may store a limiter link if p_has_limiter is set
321       // other columns are to store one or more link for the node
322
323       //add a loopback link
324       if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
325         char *tmp_link = bprintf("%s_loopback", link_id);
326         XBT_DEBUG("<loopback\tid=\"%s\"\tbw=\"%f\"/>", tmp_link, cluster->limiter_link);
327
328         memset(&link, 0, sizeof(link));
329         link.id        = tmp_link;
330         link.bandwidth = cluster->loopback_bw;
331         link.latency   = cluster->loopback_lat;
332         link.policy    = SURF_LINK_FATPIPE;
333         sg_platf_new_link(&link);
334         info_loop.link_up = info_loop.link_down = Link::byName(tmp_link);
335         free(tmp_link);
336         auto as_cluster = static_cast<AsCluster*>(current_as);
337         xbt_dynar_set(as_cluster->privateLinks_, rankId*as_cluster->nb_links_per_node_, &info_loop);
338       }
339
340       //add a limiter link (shared link to account for maximal bandwidth of the node)
341       if(cluster->limiter_link!=0){
342         char *tmp_link = bprintf("%s_limiter", link_id);
343         XBT_DEBUG("<limiter\tid=\"%s\"\tbw=\"%f\"/>", tmp_link, cluster->limiter_link);
344
345         memset(&link, 0, sizeof(link));
346         link.id = tmp_link;
347         link.bandwidth = cluster->limiter_link;
348         link.latency = 0;
349         link.policy = SURF_LINK_SHARED;
350         sg_platf_new_link(&link);
351         info_lim.link_up = info_lim.link_down = Link::byName(tmp_link);
352         free(tmp_link);
353         xbt_dynar_set(current_as->privateLinks_, rankId * current_as->nb_links_per_node_ + current_as->has_loopback_ , &info_lim);
354       }
355
356       //call the cluster function that adds the others links
357       if (cluster->topology == SURF_CLUSTER_FAT_TREE) {
358         ((AsClusterFatTree*) current_as)->addProcessingNode(i);
359       }
360       else {
361       current_as->create_links_for_node(cluster, i, rankId,
362           rankId*current_as->nb_links_per_node_ + current_as->has_loopback_ + current_as->has_limiter_ );
363       }
364       xbt_free(link_id);
365       xbt_free(host_id);
366       rankId++;
367     }
368
369     xbt_dynar_free(&radical_ends);
370   }
371   xbt_dynar_free(&radical_elements);
372
373   // For fat trees, the links must be created once all nodes have been added
374   if(cluster->topology == SURF_CLUSTER_FAT_TREE) {
375     static_cast<simgrid::surf::AsClusterFatTree*>(current_as)->create_links();
376   }
377   // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written,
378   // and it's very useful to connect clusters together
379   XBT_DEBUG(" ");
380   XBT_DEBUG("<router id=\"%s\"/>", cluster->router_id);
381   char *newid = NULL;
382   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
383   memset(&router, 0, sizeof(router));
384   router.id = cluster->router_id;
385   router.coord = "";
386   if (!router.id || !strcmp(router.id, ""))
387     router.id = newid = bprintf("%s%s_router%s", cluster->prefix, cluster->id, cluster->suffix);
388   sg_platf_new_router(&router);
389   current_as->router_ = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL);
390   free(newid);
391
392   //Make the backbone
393   if ((cluster->bb_bw != 0) || (cluster->bb_lat != 0)) {
394     char *link_backbone = bprintf("%s_backbone", cluster->id);
395     XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone, cluster->bb_bw, cluster->bb_lat);
396
397     memset(&link, 0, sizeof(link));
398     link.id        = link_backbone;
399     link.bandwidth = cluster->bb_bw;
400     link.latency   = cluster->bb_lat;
401     link.policy    = cluster->bb_sharing_policy;
402
403     sg_platf_new_link(&link);
404
405     routing_cluster_add_backbone(Link::byName(link_backbone));
406
407     free(link_backbone);
408   }
409
410   XBT_DEBUG("</AS>");
411   sg_platf_new_AS_end();
412   XBT_DEBUG(" ");
413   xbt_dict_free(&patterns); // no op if it were never set
414
415   simgrid::surf::on_cluster(cluster);
416 }
417 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
418   simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
419
420   xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
421   xbt_assert(nullptr == cluster->backbone_, "Cluster %s already has a backbone link!", cluster->name());
422
423   cluster->backbone_ = bb;
424   XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name());
425 }
426
427 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
428 {
429   int start, end, i;
430   char *groups , *host_id , *link_id = NULL;
431   unsigned int iter;
432   xbt_dynar_t radical_elements;
433   xbt_dynar_t radical_ends;
434
435   //Make all hosts
436   radical_elements = xbt_str_split(cabinet->radical, ",");
437   xbt_dynar_foreach(radical_elements, iter, groups) {
438
439     radical_ends = xbt_str_split(groups, "-");
440     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
441
442     switch (xbt_dynar_length(radical_ends)) {
443     case 1:
444       end = start;
445       break;
446     case 2:
447       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
448       break;
449     default:
450       surf_parse_error("Malformed radical");
451       break;
452     }
453     s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
454     memset(&host, 0, sizeof(host));
455     host.pstate        = 0;
456     host.core_amount   = 1;
457
458     s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
459     memset(&link, 0, sizeof(link));
460     link.policy    = SURF_LINK_FULLDUPLEX;
461     link.latency   = cabinet->lat;
462     link.bandwidth = cabinet->bw;
463
464     s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
465     memset(&host_link, 0, sizeof(host_link));
466
467     for (i = start; i <= end; i++) {
468       host_id                      = bprintf("%s%d%s",cabinet->prefix,i,cabinet->suffix);
469       link_id                      = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
470       host.id                      = host_id;
471       link.id                      = link_id;
472       host.speed_per_pstate = xbt_dynar_new(sizeof(double), NULL);
473       xbt_dynar_push(host.speed_per_pstate,&cabinet->speed);
474       sg_platf_new_host(&host);
475       xbt_dynar_free(&host.speed_per_pstate);
476       sg_platf_new_link(&link);
477
478       char* link_up       = bprintf("%s_UP",link_id);
479       char* link_down     = bprintf("%s_DOWN",link_id);
480       host_link.id        = host_id;
481       host_link.link_up   = link_up;
482       host_link.link_down = link_down;
483       sg_platf_new_hostlink(&host_link);
484
485       free(host_id);
486       free(link_id);
487       free(link_up);
488       free(link_down);
489     }
490
491     xbt_dynar_free(&radical_ends);
492   }
493   xbt_dynar_free(&radical_elements);
494 }
495
496 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
497 {
498   xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
499                "Refusing to add a second storage named \"%s\"", storage->id);
500
501   void* stype = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
502   xbt_assert(stype,"No storage type '%s'", storage->type_id);
503
504   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
505       storage->id,
506       storage->type_id,
507       storage->content);
508
509   xbt_lib_set(storage_lib, storage->id, ROUTING_STORAGE_LEVEL, (void *) xbt_strdup(storage->type_id));
510
511   // if storage content is not specified use the content of storage_type if any
512   if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
513     storage->content = ((storage_type_t) stype)->content;
514     storage->content_type = ((storage_type_t) stype)->content_type;
515     XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
516         storage->id,((storage_type_t) stype)->content_type,
517         ((storage_type_t) stype)->type_id);
518   }
519
520   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
521       "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
522       "\n\t\tproperties '%p''\n",
523       storage->id,
524       ((storage_type_t) stype)->model,
525       ((storage_type_t) stype)->type_id,
526       storage->content,
527       storage->content_type,
528     storage->properties);
529
530   surf_storage_model->createStorage(storage->id,
531                                      ((storage_type_t) stype)->type_id,
532                                      storage->content,
533                                      storage->content_type,
534                    storage->properties,
535                                      storage->attach);
536 }
537 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
538
539   xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
540                "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
541
542   storage_type_t stype = xbt_new0(s_storage_type_t, 1);
543   stype->model = xbt_strdup(storage_type->model);
544   stype->properties = storage_type->properties;
545   stype->content = xbt_strdup(storage_type->content);
546   stype->content_type = xbt_strdup(storage_type->content_type);
547   stype->type_id = xbt_strdup(storage_type->id);
548   stype->size = storage_type->size;
549   stype->model_properties = storage_type->model_properties;
550
551   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
552       "content '%s', and content_type '%s'",
553       stype->type_id,
554       stype->model,
555       storage_type->content,
556       storage_type->content_type);
557
558   xbt_lib_set(storage_type_lib,
559       stype->type_id,
560       ROUTING_STORAGE_TYPE_LEVEL,
561       (void *) stype);
562 }
563
564 static void mount_free(void *p)
565 {
566   mount_t mnt = (mount_t) p;
567   xbt_free(mnt->name);
568 }
569
570 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
571   xbt_assert(xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL),
572       "Cannot mount non-existent disk \"%s\"", mount->storageId);
573
574   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
575
576   s_mount_t mnt;
577   mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
578   mnt.name = xbt_strdup(mount->name);
579
580   if(!mount_list){
581     XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
582     mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
583   }
584   xbt_dynar_push(mount_list, &mnt);
585 }
586
587 void sg_platf_new_route(sg_platf_route_cbarg_t route)
588 {
589   routing_get_current()->addRoute(route);
590 }
591
592 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
593 {
594   routing_get_current()->addBypassRoute(bypassRoute);
595 }
596
597 void sg_platf_new_process(sg_platf_process_cbarg_t process)
598 {
599   xbt_assert(simix_global,"Cannot create process without SIMIX.");
600
601   sg_host_t host = sg_host_by_name(process->host);
602   if (!host) {
603     // The requested host does not exist. Do a nice message to the user
604     char *tmp = bprintf("Cannot create process '%s': host '%s' does not exist\nExisting hosts: '",process->function, process->host);
605     xbt_strbuff_t msg = xbt_strbuff_new_from(tmp);
606     free(tmp);
607     xbt_dynar_t all_hosts = xbt_dynar_sort_strings(sg_hosts_as_dynar());
608     simgrid::s4u::Host* host;
609     unsigned int cursor;
610     xbt_dynar_foreach(all_hosts,cursor, host) {
611       xbt_strbuff_append(msg,host->name().c_str());
612       xbt_strbuff_append(msg,"', '");
613       if (msg->used > 1024) {
614         msg->data[msg->used-3]='\0';
615         msg->used -= 3;
616
617         xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop
618       }
619     }
620     msg->data[msg->used-3]='\0';
621     xbt_die("%s", msg->data);
622   }
623   xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
624   xbt_assert(parse_code, "Function '%s' unknown", process->function);
625
626   double start_time = process->start_time;
627   double kill_time  = process->kill_time;
628   int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
629
630   smx_process_arg_t arg = NULL;
631   smx_process_t process_created = NULL;
632
633   arg = xbt_new0(s_smx_process_arg_t, 1);
634   arg->code = parse_code;
635   arg->data = NULL;
636   arg->hostname = sg_host_get_name(host);
637   arg->argc = process->argc;
638   arg->argv = xbt_new(char *,process->argc);
639   int i;
640   for (i=0; i<process->argc; i++)
641     arg->argv[i] = xbt_strdup(process->argv[i]);
642   arg->name = xbt_strdup(arg->argv[0]);
643   arg->kill_time = kill_time;
644   arg->properties = current_property_set;
645   if (!sg_host_simix(host)->boot_processes) {
646     sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
647   }
648   xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
649
650   if (start_time > SIMIX_get_clock()) {
651     arg = xbt_new0(s_smx_process_arg_t, 1);
652     arg->name = (char*)(process->argv)[0];
653     arg->code = parse_code;
654     arg->data = NULL;
655     arg->hostname = sg_host_get_name(host);
656     arg->argc = process->argc;
657     arg->argv = (char**)(process->argv);
658     arg->kill_time = kill_time;
659     arg->properties = current_property_set;
660
661     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
662            arg->hostname, start_time);
663     SIMIX_timer_set(start_time, [](void* arg) {
664       SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
665     }, arg);
666   } else {                      // start_time <= SIMIX_get_clock()
667     XBT_DEBUG("Starting Process %s(%s) right now", arg->name, sg_host_get_name(host));
668
669     if (simix_global->create_process_function)
670       process_created = simix_global->create_process_function(
671           arg->name,
672                                             parse_code,
673                                             NULL,
674                                             sg_host_get_name(host),
675                                             kill_time,
676                                             process->argc,
677                                             (char**)(process->argv),
678                                             current_property_set,
679                                             auto_restart, NULL);
680     else
681       process_created = simcall_process_create(arg->name, parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
682           (char**)process->argv, current_property_set,auto_restart);
683
684     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
685     if (!process_created) {
686       return;
687     }
688   }
689   current_property_set = NULL;
690 }
691
692 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
693 {
694   using simgrid::surf::NetCard;
695   using simgrid::surf::AsCluster;
696
697   char *host_id = bprintf("peer_%s", peer->id);
698   char *router_id = bprintf("router_%s", peer->id);
699
700   XBT_DEBUG(" ");
701
702   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
703   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
704   AS.id                    = peer->id;
705   AS.routing               = A_surfxml_AS_routing_Cluster;
706   sg_platf_new_AS_begin(&AS);
707
708   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
709   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
710   memset(&host, 0, sizeof(host));
711   host.id = host_id;
712
713   host.speed_per_pstate = xbt_dynar_new(sizeof(double), NULL);
714   xbt_dynar_push(host.speed_per_pstate,&peer->speed);
715   host.pstate = 0;
716   host.speed_trace = peer->availability_trace;
717   host.state_trace = peer->state_trace;
718   host.core_amount = 1;
719   sg_platf_new_host(&host);
720   xbt_dynar_free(&host.speed_per_pstate);
721
722   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
723   memset(&link, 0, sizeof(link));
724   link.policy  = SURF_LINK_SHARED;
725   link.latency = peer->lat;
726
727   char* link_up = bprintf("link_%s_UP",peer->id);
728   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up, peer->bw_out, peer->lat);
729   link.id = link_up;
730   link.bandwidth = peer->bw_out;
731   sg_platf_new_link(&link);
732
733   char* link_down = bprintf("link_%s_DOWN",peer->id);
734   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down, peer->bw_in, peer->lat);
735   link.id = link_down;
736   link.bandwidth = peer->bw_in;
737   sg_platf_new_link(&link);
738
739   XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
740   s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
741   memset(&host_link, 0, sizeof(host_link));
742   host_link.id        = host_id;
743   host_link.link_up   = link_up;
744   host_link.link_down = link_down;
745   sg_platf_new_hostlink(&host_link);
746   free(link_up);
747   free(link_down);
748
749   XBT_DEBUG("<router id=\"%s\"/>", router_id);
750   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
751   memset(&router, 0, sizeof(router));
752   router.id = router_id;
753   router.coord = peer->coord;
754   sg_platf_new_router(&router);
755   static_cast<AsCluster*>(current_routing)->router_ = static_cast<NetCard*>(xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL));
756
757   XBT_DEBUG("</AS>");
758   sg_platf_new_AS_end();
759   XBT_DEBUG(" ");
760
761   free(router_id);
762   free(host_id);
763 }
764
765 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
766
767 void sg_platf_end() {
768   simgrid::surf::on_postparse();
769 }
770
771 /* Pick the right models for CPU, net and host, and call their model_init_preparse */
772 static void surf_config_models_setup()
773 {
774   const char *host_model_name;
775   const char *vm_model_name;
776   int host_id = -1;
777   int vm_id = -1;
778   char *network_model_name = NULL;
779   char *cpu_model_name = NULL;
780   int storage_id = -1;
781   char *storage_model_name = NULL;
782
783   host_model_name = xbt_cfg_get_string(_sg_cfg_set, "host/model");
784   vm_model_name = xbt_cfg_get_string(_sg_cfg_set, "vm/model");
785   network_model_name = xbt_cfg_get_string(_sg_cfg_set, "network/model");
786   cpu_model_name = xbt_cfg_get_string(_sg_cfg_set, "cpu/model");
787   storage_model_name = xbt_cfg_get_string(_sg_cfg_set, "storage/model");
788
789   /* Check whether we use a net/cpu model differing from the default ones, in which case
790    * we should switch to the "compound" host model to correctly dispatch stuff to
791    * the right net/cpu models.
792    */
793
794   if ((!xbt_cfg_is_default_value(_sg_cfg_set, "network/model") ||
795        !xbt_cfg_is_default_value(_sg_cfg_set, "cpu/model")) &&
796       xbt_cfg_is_default_value(_sg_cfg_set, "host/model")) {
797     host_model_name = "compound";
798     xbt_cfg_set_string(_sg_cfg_set, "host/model", host_model_name);
799   }
800
801   XBT_DEBUG("host model: %s", host_model_name);
802   host_id = find_model_description(surf_host_model_description, host_model_name);
803   if (!strcmp(host_model_name, "compound")) {
804     int network_id = -1;
805     int cpu_id = -1;
806
807     xbt_assert(cpu_model_name,
808                 "Set a cpu model to use with the 'compound' host model");
809
810     xbt_assert(network_model_name,
811                 "Set a network model to use with the 'compound' host model");
812
813     if(surf_cpu_model_init_preparse){
814       surf_cpu_model_init_preparse();
815     } else {
816       cpu_id =
817           find_model_description(surf_cpu_model_description, cpu_model_name);
818       surf_cpu_model_description[cpu_id].model_init_preparse();
819     }
820
821     network_id =
822         find_model_description(surf_network_model_description,
823                                network_model_name);
824     surf_network_model_description[network_id].model_init_preparse();
825   }
826
827   XBT_DEBUG("Call host_model_init");
828   surf_host_model_description[host_id].model_init_preparse();
829
830   XBT_DEBUG("Call vm_model_init");
831   vm_id = find_model_description(surf_vm_model_description, vm_model_name);
832   surf_vm_model_description[vm_id].model_init_preparse();
833
834   XBT_DEBUG("Call storage_model_init");
835   storage_id = find_model_description(surf_storage_model_description, storage_model_name);
836   surf_storage_model_description[storage_id].model_init_preparse();
837
838 }
839
840 /**
841  * \brief Make a new routing component to the platform
842  *
843  * Add a new autonomous system to the platform. Any elements (such as host,
844  * router or sub-AS) added after this call and before the corresponding call
845  * to sg_platf_new_AS_close() will be added to this AS.
846  *
847  * Once this function was called, the configuration concerning the used
848  * models cannot be changed anymore.
849  *
850  * @param AS_id name of this autonomous system. Must be unique in the platform
851  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
852  */
853 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
854 {
855   if (!surf_parse_models_setup_already_called) {
856     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
857      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
858      *
859      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
860      * (FIXME: check it out by creating a file beginning with one of these tags)
861      * but cluster and peer create ASes internally, so putting the code in there is ok.
862      */
863     surf_parse_models_setup_already_called = 1;
864     surf_config_models_setup();
865   }
866
867   xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, AS->id, ROUTING_ASR_LEVEL),
868       "Refusing to create a second AS called \"%s\".", AS->id);
869
870   _sg_cfg_init_status = 2; /* HACK: direct access to the global controlling the level of configuration to prevent
871                             * any further config now that we created some real content */
872
873
874   /* search the routing model */
875   simgrid::surf::AsImpl *new_as = NULL;
876   switch(AS->routing){
877     case A_surfxml_AS_routing_Cluster:        new_as = new simgrid::surf::AsCluster(AS->id);        break;
878     case A_surfxml_AS_routing_ClusterTorus:   new_as = new simgrid::surf::AsClusterTorus(AS->id);   break;
879     case A_surfxml_AS_routing_ClusterFatTree: new_as = new simgrid::surf::AsClusterFatTree(AS->id); break;
880     case A_surfxml_AS_routing_Dijkstra:       new_as = new simgrid::surf::AsDijkstra(AS->id, 0);    break;
881     case A_surfxml_AS_routing_DijkstraCache:  new_as = new simgrid::surf::AsDijkstra(AS->id, 1);    break;
882     case A_surfxml_AS_routing_Floyd:          new_as = new simgrid::surf::AsFloyd(AS->id);          break;
883     case A_surfxml_AS_routing_Full:           new_as = new simgrid::surf::AsFull(AS->id);           break;
884     case A_surfxml_AS_routing_None:           new_as = new simgrid::surf::AsNone(AS->id);           break;
885     case A_surfxml_AS_routing_Vivaldi:        new_as = new simgrid::surf::AsVivaldi(AS->id);        break;
886     default:                                  xbt_die("Not a valid model!");                        break;
887   }
888
889   /* make a new routing component */
890   simgrid::surf::NetCard *netcard = new simgrid::surf::NetCardImpl(new_as->name(), simgrid::surf::NetCard::Type::As, current_routing);
891
892   if (current_routing == NULL && routing_platf->root_ == NULL) { /* it is the first one */
893     routing_platf->root_ = new_as;
894   } else if (current_routing != NULL && routing_platf->root_ != NULL) {
895
896     xbt_assert(!xbt_dict_get_or_null(current_routing->children(), AS->id),
897                "The AS \"%s\" already exists", AS->id);
898     /* it is a part of the tree */
899     new_as->father_ = current_routing;
900     /* set the father behavior */
901     if (current_routing->hierarchy_ == simgrid::surf::AsImpl::RoutingMode::unset)
902       current_routing->hierarchy_ = simgrid::surf::AsImpl::RoutingMode::recursive;
903     /* add to the sons dictionary */
904     xbt_dict_set(current_routing->children(), AS->id, (void *) new_as, NULL);
905   } else {
906     THROWF(arg_error, 0, "All defined components must belong to a AS");
907   }
908
909   xbt_lib_set(as_router_lib, netcard->name(), ROUTING_ASR_LEVEL, (void *) netcard);
910   XBT_DEBUG("Having set name '%s' id '%d'", new_as->name(), netcard->id());
911
912   /* set the new current component of the tree */
913   current_routing = new_as;
914   current_routing->netcard_ = netcard;
915
916   simgrid::surf::asCreatedCallbacks(new_as);
917   if (TRACE_is_enabled())
918     sg_instr_AS_begin(AS);
919 }
920
921 /**
922  * \brief Specify that the current description of AS is finished
923  *
924  * Once you've declared all the content of your AS, you have to close
925  * it with this call. Your AS is not usable until you call this function.
926  */
927 void sg_platf_new_AS_end()
928 {
929   xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
930   current_routing->seal();
931   current_routing = static_cast<simgrid::surf::AsImpl*>(current_routing->father());
932
933   if (TRACE_is_enabled())
934     sg_instr_AS_end();
935 }
936
937 /** @brief Add a link connecting an host to the rest of its AS (which must be cluster or vivaldi) */
938 void sg_platf_new_hostlink(sg_platf_host_link_cbarg_t hostlink)
939 {
940   simgrid::surf::NetCard *netcard = sg_host_by_name(hostlink->id)->pimpl_netcard;
941   xbt_assert(netcard, "Host '%s' not found!", hostlink->id);
942   xbt_assert(dynamic_cast<simgrid::surf::AsCluster*>(current_routing),
943       "Only hosts from Cluster and Vivaldi ASes can get an host_link.");
944
945   s_surf_parsing_link_up_down_t link_up_down;
946   link_up_down.link_up = Link::byName(hostlink->link_up);
947   link_up_down.link_down = Link::byName(hostlink->link_down);
948
949   xbt_assert(link_up_down.link_up, "Link '%s' not found!",hostlink->link_up);
950   xbt_assert(link_up_down.link_down, "Link '%s' not found!",hostlink->link_down);
951
952   // If dynar is is greater than netcard id and if the host_link is already defined
953   auto as_cluster = static_cast<simgrid::surf::AsCluster*>(current_routing);
954   if((int)xbt_dynar_length(as_cluster->privateLinks_) > netcard->id() &&
955       xbt_dynar_get_as(as_cluster->privateLinks_, netcard->id(), void*))
956   surf_parse_error("Host_link for '%s' is already defined!",hostlink->id);
957
958   XBT_DEBUG("Push Host_link for host '%s' to position %d", netcard->name(), netcard->id());
959   xbt_dynar_set_as(as_cluster->privateLinks_, netcard->id(), s_surf_parsing_link_up_down_t, link_up_down);
960 }