1 /* Copyright (c) 2006-2014. 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. */
11 #include "xbt/RngStream.h"
12 #include <xbt/functional.hpp>
13 #include <xbt/signal.hpp>
14 #include "src/surf/HostImpl.hpp"
15 #include "surf/surf.h"
17 #include "src/simix/smx_private.h"
19 #include "src/include/simgrid/sg_config.h"
20 #include "src/surf/xml/platf_private.hpp"
22 #include "src/surf/HostImpl.hpp"
23 #include "src/surf/cpu_interface.hpp"
24 #include "src/surf/network_interface.hpp"
25 #include "surf/surf_routing.h" // FIXME: brain dead public header
27 #include "src/kernel/routing/AsImpl.hpp"
28 #include "src/kernel/routing/AsCluster.hpp"
29 #include "src/kernel/routing/AsClusterTorus.hpp"
30 #include "src/kernel/routing/AsClusterFatTree.hpp"
31 #include "src/kernel/routing/AsClusterDragonfly.hpp"
32 #include "src/kernel/routing/AsDijkstra.hpp"
33 #include "src/kernel/routing/AsFloyd.hpp"
34 #include "src/kernel/routing/AsFull.hpp"
35 #include "src/kernel/routing/AsNone.hpp"
36 #include "src/kernel/routing/AsVivaldi.hpp"
38 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
40 XBT_PRIVATE xbt_dynar_t mount_list = nullptr;
45 simgrid::xbt::signal<void(sg_platf_link_cbarg_t)> on_link;
46 simgrid::xbt::signal<void(sg_platf_cluster_cbarg_t)> on_cluster;
47 simgrid::xbt::signal<void(void)> on_postparse;
52 static int surf_parse_models_setup_already_called = 0;
54 /** The current AS in the parsing */
55 static simgrid::kernel::routing::AsImpl *current_routing = nullptr;
56 static simgrid::kernel::routing::AsImpl *routing_get_current()
58 return current_routing;
61 /** Module management function: creates all internal data structures */
62 void sg_platf_init() {
65 /** Module management function: frees all internal data structures */
66 void sg_platf_exit() {
67 simgrid::surf::on_link.disconnect_all_slots();
68 simgrid::surf::on_cluster.disconnect_all_slots();
69 simgrid::surf::on_postparse.disconnect_all_slots();
71 /* make sure that we will reinit the models while loading the platf once reinited */
72 surf_parse_models_setup_already_called = 0;
73 surf_parse_lex_destroy();
76 /** @brief Add an host to the current AS */
77 void sg_platf_new_host(sg_platf_host_cbarg_t host)
79 simgrid::kernel::routing::AsImpl* current_routing = routing_get_current();
81 simgrid::s4u::Host* h = new simgrid::s4u::Host(host->id);
82 current_routing->attachHost(h);
84 if (host->coord && strcmp(host->coord, "")) {
88 xbt_assert(COORD_HOST_LEVEL, "To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
89 /* Pre-parse the host coordinates -- FIXME factorize with routers by overloading the routing->parse_PU function*/
90 xbt_dynar_t ctn_str = xbt_str_split_str(host->coord, " ");
91 xbt_assert(xbt_dynar_length(ctn_str)==3,"Coordinates of %s must have 3 dimensions", host->id);
93 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),nullptr);
94 xbt_dynar_foreach(ctn_str,cursor, str) {
95 double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
96 xbt_dynar_push(ctn,&val);
98 xbt_dynar_free(&ctn_str);
99 xbt_dynar_shrink(ctn, 0);
100 h->extension_set(COORD_HOST_LEVEL, (void *) ctn);
103 simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu( h, &host->speed_per_pstate, host->core_amount);
104 if (host->state_trace)
105 cpu->setStateTrace(host->state_trace);
106 if (host->speed_trace)
107 cpu->setSpeedTrace(host->speed_trace);
109 new simgrid::surf::HostImpl(h, mount_list);
110 xbt_lib_set(storage_lib, host->id, ROUTING_STORAGE_HOST_LEVEL, static_cast<void*>(mount_list));
112 mount_list = nullptr;
114 if (host->properties) {
115 xbt_dict_cursor_t cursor=nullptr;
117 xbt_dict_foreach(host->properties,cursor,key,data)
118 h->setProperty(key,data);
119 xbt_dict_free(&host->properties);
122 if (host->pstate != 0)
123 cpu->setPState(host->pstate);
125 simgrid::s4u::Host::onCreation(*h);
127 if (TRACE_is_enabled() && TRACE_needs_platform())
128 sg_instr_new_host(host);
131 /** @brief Add a "router" to the network element list */
132 void sg_platf_new_router(sg_platf_router_cbarg_t router)
134 using simgrid::kernel::routing::AsCluster;
135 simgrid::kernel::routing::AsImpl* current_routing = routing_get_current();
137 if (current_routing->hierarchy_ == simgrid::kernel::routing::AsImpl::RoutingMode::unset)
138 current_routing->hierarchy_ = simgrid::kernel::routing::AsImpl::RoutingMode::base;
139 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
140 "Refusing to create a router named '%s': this name already describes a node.", router->id);
142 simgrid::kernel::routing::NetCard* netcard =
143 new simgrid::kernel::routing::NetCardImpl(router->id, simgrid::kernel::routing::NetCard::Type::Router, current_routing);
144 xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, netcard);
145 XBT_DEBUG("Router '%s' has the id %d", router->id, netcard->id());
147 if (router->coord && strcmp(router->coord, "")) {
151 xbt_assert(COORD_ASR_LEVEL, "To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
152 /* Pre-parse the host coordinates */
153 xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
154 xbt_assert(xbt_dynar_length(ctn_str)==3,"Coordinates of %s must have 3 dimensions", router->id);
155 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),nullptr);
156 xbt_dynar_foreach(ctn_str,cursor, str) {
157 double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
158 xbt_dynar_push(ctn,&val);
160 xbt_dynar_free(&ctn_str);
161 xbt_dynar_shrink(ctn, 0);
162 xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
165 auto cluster = dynamic_cast<AsCluster*>(current_routing);
166 if(cluster != nullptr)
167 cluster->router_ = static_cast<simgrid::kernel::routing::NetCard*>(xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL));
169 if (TRACE_is_enabled() && TRACE_needs_platform())
170 sg_instr_new_router(router);
173 void sg_platf_new_link(sg_platf_link_cbarg_t link){
174 std::vector<char*> names;
176 if (link->policy == SURF_LINK_FULLDUPLEX) {
177 names.push_back(bprintf("%s_UP", link->id));
178 names.push_back(bprintf("%s_DOWN", link->id));
180 names.push_back(xbt_strdup(link->id));
182 for (auto link_name : names) {
183 Link *l = surf_network_model->createLink(link_name, link->bandwidth, link->latency, link->policy, link->properties);
185 if (link->latency_trace)
186 l->setLatencyTrace(link->latency_trace);
187 if (link->bandwidth_trace)
188 l->setBandwidthTrace(link->bandwidth_trace);
189 if (link->state_trace)
190 l->setStateTrace(link->state_trace);
195 simgrid::surf::on_link(link);
198 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
200 using simgrid::kernel::routing::AsCluster;
201 using simgrid::kernel::routing::AsClusterDragonfly;
202 using simgrid::kernel::routing::AsClusterFatTree;
203 using simgrid::kernel::routing::AsClusterTorus;
207 s_sg_platf_link_cbarg_t link;
209 // What an inventive way of initializing the AS that I have as ancestor :-(
210 s_sg_platf_AS_cbarg_t AS;
212 switch (cluster->topology) {
213 case SURF_CLUSTER_TORUS:
214 AS.routing = A_surfxml_AS_routing_ClusterTorus;
216 case SURF_CLUSTER_DRAGONFLY:
217 AS.routing = A_surfxml_AS_routing_ClusterDragonfly;
219 case SURF_CLUSTER_FAT_TREE:
220 AS.routing = A_surfxml_AS_routing_ClusterFatTree;
223 AS.routing = A_surfxml_AS_routing_Cluster;
226 sg_platf_new_AS_begin(&AS);
227 simgrid::kernel::routing::AsCluster *current_as = static_cast<AsCluster*>(routing_get_current());
228 current_as->parse_specific_arguments(cluster);
230 if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
231 current_as->linkCountPerNode_++;
232 current_as->hasLoopback_ = 1;
235 if(cluster->limiter_link!=0){
236 current_as->linkCountPerNode_++;
237 current_as->hasLimiter_ = 1;
240 for (int i : *cluster->radicals) {
241 char * host_id = bprintf("%s%d%s", cluster->prefix, i, cluster->suffix);
242 char * link_id = bprintf("%s_link_%d", cluster->id, i);
244 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, cluster->speed);
246 s_sg_platf_host_cbarg_t host;
247 memset(&host, 0, sizeof(host));
249 if ((cluster->properties != nullptr) && (!xbt_dict_is_empty(cluster->properties))) {
250 xbt_dict_cursor_t cursor=nullptr;
252 host.properties = xbt_dict_new();
254 xbt_dict_foreach(cluster->properties,cursor,key,data) {
255 xbt_dict_set(host.properties, key, xbt_strdup(data),free);
259 host.speed_per_pstate.push_back(cluster->speed);
261 host.core_amount = cluster->core_amount;
263 sg_platf_new_host(&host);
264 XBT_DEBUG("</host>");
266 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id, cluster->bw, cluster->lat);
268 s_surf_parsing_link_up_down_t info_lim;
269 s_surf_parsing_link_up_down_t info_loop;
270 // All links are saved in a matrix;
271 // every row describes a single node; every node may have multiple links.
272 // the first column may store a link from x to x if p_has_loopback is set
273 // the second column may store a limiter link if p_has_limiter is set
274 // other columns are to store one or more link for the node
276 //add a loopback link
277 if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
278 char *tmp_link = bprintf("%s_loopback", link_id);
279 XBT_DEBUG("<loopback\tid=\"%s\"\tbw=\"%f\"/>", tmp_link, cluster->loopback_bw);
281 memset(&link, 0, sizeof(link));
283 link.bandwidth = cluster->loopback_bw;
284 link.latency = cluster->loopback_lat;
285 link.policy = SURF_LINK_FATPIPE;
286 sg_platf_new_link(&link);
287 info_loop.linkUp = Link::byName(tmp_link);
288 info_loop.linkDown = Link::byName(tmp_link);
291 auto as_cluster = static_cast<AsCluster*>(current_as);
292 as_cluster->privateLinks_.insert({rankId*as_cluster->linkCountPerNode_, info_loop});
295 //add a limiter link (shared link to account for maximal bandwidth of the node)
296 if(cluster->limiter_link!=0){
297 char *tmp_link = bprintf("%s_limiter", link_id);
298 XBT_DEBUG("<limiter\tid=\"%s\"\tbw=\"%f\"/>", tmp_link, cluster->limiter_link);
300 memset(&link, 0, sizeof(link));
302 link.bandwidth = cluster->limiter_link;
304 link.policy = SURF_LINK_SHARED;
305 sg_platf_new_link(&link);
306 info_lim.linkUp = info_lim.linkDown = Link::byName(tmp_link);
308 current_as->privateLinks_.insert(
309 {rankId * current_as->linkCountPerNode_ + current_as->hasLoopback_ , info_lim});
312 //call the cluster function that adds the others links
313 if (cluster->topology == SURF_CLUSTER_FAT_TREE) {
314 static_cast<AsClusterFatTree*>(current_as)->addProcessingNode(i);
317 current_as->create_links_for_node(cluster, i, rankId,
318 rankId*current_as->linkCountPerNode_ + current_as->hasLoopback_ + current_as->hasLimiter_ );
325 // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written,
326 // and it's very useful to connect clusters together
328 XBT_DEBUG("<router id=\"%s\"/>", cluster->router_id);
329 char *newid = nullptr;
330 s_sg_platf_router_cbarg_t router;
331 memset(&router, 0, sizeof(router));
332 router.id = cluster->router_id;
333 if (!router.id || !strcmp(router.id, ""))
334 router.id = newid = bprintf("%s%s_router%s", cluster->prefix, cluster->id, cluster->suffix);
335 sg_platf_new_router(&router);
336 current_as->router_ = (simgrid::kernel::routing::NetCard*) xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL);
340 if ((cluster->bb_bw != 0) || (cluster->bb_lat != 0)) {
342 memset(&link, 0, sizeof(link));
343 link.id = bprintf("%s_backbone", cluster->id);
344 link.bandwidth = cluster->bb_bw;
345 link.latency = cluster->bb_lat;
346 link.policy = cluster->bb_sharing_policy;
348 XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link.id, cluster->bb_bw, cluster->bb_lat);
349 sg_platf_new_link(&link);
351 routing_cluster_add_backbone(Link::byName(link.id));
352 free((char*)link.id);
356 sg_platf_new_AS_seal();
358 simgrid::surf::on_cluster(cluster);
359 delete cluster->radicals;
361 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
362 simgrid::kernel::routing::AsCluster *cluster = dynamic_cast<simgrid::kernel::routing::AsCluster*>(current_routing);
364 xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
365 xbt_assert(nullptr == cluster->backbone_, "Cluster %s already has a backbone link!", cluster->name());
367 cluster->backbone_ = bb;
368 XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name());
371 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
373 for (int radical : *cabinet->radicals) {
374 char *hostname = bprintf("%s%d%s", cabinet->prefix, radical, cabinet->suffix);
375 s_sg_platf_host_cbarg_t host;
376 memset(&host, 0, sizeof(host));
378 host.core_amount = 1;
380 host.speed_per_pstate.push_back(cabinet->speed);
381 sg_platf_new_host(&host);
383 s_sg_platf_link_cbarg_t link;
384 memset(&link, 0, sizeof(link));
385 link.policy = SURF_LINK_FULLDUPLEX;
386 link.latency = cabinet->lat;
387 link.bandwidth = cabinet->bw;
388 link.id = bprintf("link_%s",hostname);
389 sg_platf_new_link(&link);
390 free((char*)link.id);
392 s_sg_platf_host_link_cbarg_t host_link;
393 memset(&host_link, 0, sizeof(host_link));
394 host_link.id = hostname;
395 host_link.link_up = bprintf("link_%s_UP",hostname);
396 host_link.link_down = bprintf("link_%s_DOWN",hostname);
397 sg_platf_new_hostlink(&host_link);
398 free((char*)host_link.link_up);
399 free((char*)host_link.link_down);
403 delete cabinet->radicals;
406 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
408 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
409 "Refusing to add a second storage named \"%s\"", storage->id);
411 void* stype = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
412 xbt_assert(stype,"No storage type '%s'", storage->type_id);
414 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
419 xbt_lib_set(storage_lib, storage->id, ROUTING_STORAGE_LEVEL, (void *) xbt_strdup(storage->type_id));
421 // if storage content is not specified use the content of storage_type if any
422 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
423 storage->content = ((storage_type_t) stype)->content;
424 storage->content_type = ((storage_type_t) stype)->content_type;
425 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
426 storage->id,((storage_type_t) stype)->content_type,
427 ((storage_type_t) stype)->type_id);
430 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
431 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
432 "\n\t\tproperties '%p''\n",
434 ((storage_type_t) stype)->model,
435 ((storage_type_t) stype)->type_id,
437 storage->content_type,
438 storage->properties);
440 surf_storage_model->createStorage(storage->id,
441 ((storage_type_t) stype)->type_id,
443 storage->content_type,
447 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
449 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
450 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
452 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
453 stype->model = xbt_strdup(storage_type->model);
454 stype->properties = storage_type->properties;
455 stype->content = xbt_strdup(storage_type->content);
456 stype->content_type = xbt_strdup(storage_type->content_type);
457 stype->type_id = xbt_strdup(storage_type->id);
458 stype->size = storage_type->size;
459 stype->model_properties = storage_type->model_properties;
461 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
462 "content '%s', and content_type '%s'",
465 storage_type->content,
466 storage_type->content_type);
468 xbt_lib_set(storage_type_lib,
470 ROUTING_STORAGE_TYPE_LEVEL,
474 static void mount_free(void *p)
476 mount_t mnt = (mount_t) p;
480 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
481 xbt_assert(xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL),
482 "Cannot mount non-existent disk \"%s\"", mount->storageId);
484 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
487 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
488 mnt.name = xbt_strdup(mount->name);
491 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
492 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
494 xbt_dynar_push(mount_list, &mnt);
497 void sg_platf_new_route(sg_platf_route_cbarg_t route)
499 routing_get_current()->addRoute(route);
502 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
504 routing_get_current()->addBypassRoute(bypassRoute);
507 void sg_platf_new_process(sg_platf_process_cbarg_t process)
509 sg_host_t host = sg_host_by_name(process->host);
511 // The requested host does not exist. Do a nice message to the user
512 char *tmp = bprintf("Cannot create process '%s': host '%s' does not exist\nExisting hosts: '",process->function, process->host);
513 xbt_strbuff_t msg = xbt_strbuff_new_from(tmp);
515 xbt_dynar_t all_hosts = xbt_dynar_sort_strings(sg_hosts_as_dynar());
516 simgrid::s4u::Host* host;
518 xbt_dynar_foreach(all_hosts,cursor, host) {
519 xbt_strbuff_append(msg,host->name().c_str());
520 xbt_strbuff_append(msg,"', '");
521 if (msg->used > 1024) {
522 msg->data[msg->used-3]='\0';
525 xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop
529 msg->data[msg->used-3]='\0';
530 xbt_die("%s", msg->data);
532 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(process->function);
533 xbt_assert(factory, "Function '%s' unknown", process->function);
535 double start_time = process->start_time;
536 double kill_time = process->kill_time;
537 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
539 std::vector<std::string> args(process->argv, process->argv + process->argc);
540 std::function<void()> code = factory(std::move(args));
542 smx_process_arg_t arg = nullptr;
543 smx_actor_t process_created = nullptr;
545 arg = new simgrid::simix::ProcessArg();
546 arg->name = std::string(process->argv[0]);
550 arg->kill_time = kill_time;
551 arg->properties = current_property_set;
553 sg_host_simix(host)->boot_processes.push_back(arg);
555 if (start_time > SIMIX_get_clock()) {
557 arg = new simgrid::simix::ProcessArg();
558 arg->name = std::string(process->argv[0]);
559 arg->code = std::move(code);
562 arg->kill_time = kill_time;
563 arg->properties = current_property_set;
565 XBT_DEBUG("Process %s@%s will be started at time %f",
566 arg->name.c_str(), arg->host->name().c_str(), start_time);
567 SIMIX_timer_set(start_time, [=]() {
568 simix_global->create_process_function(
570 std::move(arg->code),
579 } else { // start_time <= SIMIX_get_clock()
580 XBT_DEBUG("Starting Process %s(%s) right now",
581 arg->name.c_str(), sg_host_get_name(host));
583 process_created = simix_global->create_process_function(
584 arg->name.c_str(), std::move(code), nullptr,
586 current_property_set, auto_restart, nullptr);
588 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
589 if (!process_created) {
593 current_property_set = nullptr;
596 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
598 using simgrid::kernel::routing::NetCard;
599 using simgrid::kernel::routing::AsCluster;
601 char *host_id = bprintf("peer_%s", peer->id);
602 char *router_id = bprintf("router_%s", peer->id);
606 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
607 s_sg_platf_AS_cbarg_t AS;
609 AS.routing = A_surfxml_AS_routing_Cluster;
610 sg_platf_new_AS_begin(&AS);
612 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
613 s_sg_platf_host_cbarg_t host;
614 memset(&host, 0, sizeof(host));
617 host.speed_per_pstate.push_back(peer->speed);
619 host.speed_trace = peer->availability_trace;
620 host.state_trace = peer->state_trace;
621 host.core_amount = 1;
622 sg_platf_new_host(&host);
624 s_sg_platf_link_cbarg_t link;
625 memset(&link, 0, sizeof(link));
626 link.policy = SURF_LINK_SHARED;
627 link.latency = peer->lat;
629 char* link_up = bprintf("link_%s_UP",peer->id);
630 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up, peer->bw_out, peer->lat);
632 link.bandwidth = peer->bw_out;
633 sg_platf_new_link(&link);
635 char* link_down = bprintf("link_%s_DOWN",peer->id);
636 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down, peer->bw_in, peer->lat);
638 link.bandwidth = peer->bw_in;
639 sg_platf_new_link(&link);
641 XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
642 s_sg_platf_host_link_cbarg_t host_link;
643 memset(&host_link, 0, sizeof(host_link));
644 host_link.id = host_id;
645 host_link.link_up = link_up;
646 host_link.link_down = link_down;
647 sg_platf_new_hostlink(&host_link);
651 XBT_DEBUG("<router id=\"%s\"/>", router_id);
652 s_sg_platf_router_cbarg_t router;
653 memset(&router, 0, sizeof(router));
654 router.id = router_id;
655 router.coord = peer->coord;
656 sg_platf_new_router(&router);
659 sg_platf_new_AS_seal();
666 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
668 void sg_platf_end() {
669 simgrid::surf::on_postparse();
672 /* Pick the right models for CPU, net and host, and call their model_init_preparse */
673 static void surf_config_models_setup()
675 const char* host_model_name = xbt_cfg_get_string("host/model");
676 const char* vm_model_name = xbt_cfg_get_string("vm/model");
677 const char* network_model_name = xbt_cfg_get_string("network/model");
678 const char* cpu_model_name = xbt_cfg_get_string("cpu/model");
679 const char* storage_model_name = xbt_cfg_get_string("storage/model");
681 /* The compound host model is needed when using non-default net/cpu models */
682 if ((!xbt_cfg_is_default_value("network/model") || !xbt_cfg_is_default_value("cpu/model")) &&
683 xbt_cfg_is_default_value("host/model")) {
684 host_model_name = "compound";
685 xbt_cfg_set_string("host/model", host_model_name);
688 XBT_DEBUG("host model: %s", host_model_name);
689 if (!strcmp(host_model_name, "compound")) {
690 xbt_assert(cpu_model_name, "Set a cpu model to use with the 'compound' host model");
691 xbt_assert(network_model_name, "Set a network model to use with the 'compound' host model");
693 int cpu_id = find_model_description(surf_cpu_model_description, cpu_model_name);
694 surf_cpu_model_description[cpu_id].model_init_preparse();
696 int network_id = find_model_description(surf_network_model_description, network_model_name);
697 surf_network_model_description[network_id].model_init_preparse();
700 XBT_DEBUG("Call host_model_init");
701 int host_id = find_model_description(surf_host_model_description, host_model_name);
702 surf_host_model_description[host_id].model_init_preparse();
704 XBT_DEBUG("Call vm_model_init");
705 int vm_id = find_model_description(surf_vm_model_description, vm_model_name);
706 surf_vm_model_description[vm_id].model_init_preparse();
708 XBT_DEBUG("Call storage_model_init");
709 int storage_id = find_model_description(surf_storage_model_description, storage_model_name);
710 surf_storage_model_description[storage_id].model_init_preparse();
714 * \brief Add an AS to the platform
716 * Add a new autonomous system to the platform. Any elements (such as host,
717 * router or sub-AS) added after this call and before the corresponding call
718 * to sg_platf_new_AS_seal() will be added to this AS.
720 * Once this function was called, the configuration concerning the used
721 * models cannot be changed anymore.
723 * @param AS the parameters defining the AS to build.
725 simgrid::s4u::As * sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
727 if (!surf_parse_models_setup_already_called) {
728 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
729 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
731 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
732 * (FIXME: check it out by creating a file beginning with one of these tags)
733 * but cluster and peer create ASes internally, so putting the code in there is ok.
735 surf_parse_models_setup_already_called = 1;
736 surf_config_models_setup();
739 _sg_cfg_init_status = 2; /* HACK: direct access to the global controlling the level of configuration to prevent
740 * any further config now that we created some real content */
743 /* search the routing model */
744 simgrid::kernel::routing::AsImpl *new_as = nullptr;
746 case A_surfxml_AS_routing_Cluster:
747 new_as = new simgrid::kernel::routing::AsCluster(current_routing, AS->id);
749 case A_surfxml_AS_routing_ClusterDragonfly:
750 new_as = new simgrid::kernel::routing::AsClusterDragonfly(current_routing, AS->id);
752 case A_surfxml_AS_routing_ClusterTorus:
753 new_as = new simgrid::kernel::routing::AsClusterTorus(current_routing, AS->id);
755 case A_surfxml_AS_routing_ClusterFatTree:
756 new_as = new simgrid::kernel::routing::AsClusterFatTree(current_routing, AS->id);
758 case A_surfxml_AS_routing_Dijkstra:
759 new_as = new simgrid::kernel::routing::AsDijkstra(current_routing, AS->id, 0);
761 case A_surfxml_AS_routing_DijkstraCache:
762 new_as = new simgrid::kernel::routing::AsDijkstra(current_routing, AS->id, 1);
764 case A_surfxml_AS_routing_Floyd:
765 new_as = new simgrid::kernel::routing::AsFloyd(current_routing, AS->id);
767 case A_surfxml_AS_routing_Full:
768 new_as = new simgrid::kernel::routing::AsFull(current_routing, AS->id);
770 case A_surfxml_AS_routing_None:
771 new_as = new simgrid::kernel::routing::AsNone(current_routing, AS->id);
773 case A_surfxml_AS_routing_Vivaldi:
774 new_as = new simgrid::kernel::routing::AsVivaldi(current_routing, AS->id);
777 xbt_die("Not a valid model!");
782 if (current_routing == nullptr && routing_platf->root_ == nullptr) { /* it is the first one */
783 routing_platf->root_ = new_as;
785 } else if (current_routing != nullptr && routing_platf->root_ != nullptr) {
786 /* set the father behavior */
787 if (current_routing->hierarchy_ == simgrid::kernel::routing::AsImpl::RoutingMode::unset)
788 current_routing->hierarchy_ = simgrid::kernel::routing::AsImpl::RoutingMode::recursive;
789 /* add to the sons dictionary */
790 xbt_dict_set(current_routing->children(), AS->id, (void *) new_as, nullptr);
793 THROWF(arg_error, 0, "All defined components must belong to a AS");
796 /* set the new current component of the tree */
797 current_routing = new_as;
799 simgrid::kernel::routing::asCreatedCallbacks(new_as);
800 if (TRACE_is_enabled())
801 sg_instr_AS_begin(AS);
807 * \brief Specify that the description of the current AS is finished
809 * Once you've declared all the content of your AS, you have to seal
810 * it with this call. Your AS is not usable until you call this function.
812 void sg_platf_new_AS_seal()
814 xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
815 current_routing->seal();
816 current_routing = static_cast<simgrid::kernel::routing::AsImpl*>(current_routing->father());
818 if (TRACE_is_enabled())
822 /** @brief Add a link connecting an host to the rest of its AS (which must be cluster or vivaldi) */
823 void sg_platf_new_hostlink(sg_platf_host_link_cbarg_t hostlink)
825 simgrid::kernel::routing::NetCard *netcard = sg_host_by_name(hostlink->id)->pimpl_netcard;
826 xbt_assert(netcard, "Host '%s' not found!", hostlink->id);
827 xbt_assert(dynamic_cast<simgrid::kernel::routing::AsCluster*>(current_routing),
828 "Only hosts from Cluster and Vivaldi ASes can get an host_link.");
830 s_surf_parsing_link_up_down_t link_up_down;
831 link_up_down.linkUp = Link::byName(hostlink->link_up);
832 link_up_down.linkDown = Link::byName(hostlink->link_down);
834 xbt_assert(link_up_down.linkUp, "Link '%s' not found!",hostlink->link_up);
835 xbt_assert(link_up_down.linkDown, "Link '%s' not found!",hostlink->link_down);
837 auto as_cluster = static_cast<simgrid::kernel::routing::AsCluster*>(current_routing);
839 if (as_cluster->privateLinks_.find(netcard->id()) != as_cluster->privateLinks_.end())
840 surf_parse_error("Host_link for '%s' is already defined!",hostlink->id);
842 XBT_DEBUG("Push Host_link for host '%s' to position %d", netcard->name().c_str(), netcard->id());
843 as_cluster->privateLinks_.insert({netcard->id(), link_up_down});