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/signal.hpp>
13 #include "src/surf/HostImpl.hpp"
14 #include "surf/surf.h"
16 #include "src/simix/smx_private.h"
18 #include "src/include/simgrid/sg_config.h"
19 #include "src/surf/xml/platf_private.hpp"
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
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"
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
37 XBT_PRIVATE xbt_dynar_t mount_list = NULL;
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;
49 static int surf_parse_models_setup_already_called = 0;
51 /** The current AS in the parsing */
52 static simgrid::surf::AsImpl *current_routing = NULL;
53 static simgrid::surf::AsImpl* routing_get_current()
55 return current_routing;
58 /** Module management function: creates all internal data structures */
59 void sg_platf_init(void) {
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();
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();
73 /** @brief Add an "host" to the current AS */
74 void sg_platf_new_host(sg_platf_host_cbarg_t host)
76 xbt_assert(! sg_host_by_name(host->id), "Refusing to create a second host named '%s'.", host->id);
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;
82 simgrid::surf::NetCard *netcard =
83 new simgrid::surf::NetCardImpl(host->id, simgrid::surf::NetCard::Type::Host, current_routing);
85 sg_host_t h = simgrid::s4u::Host::by_name_or_create(host->id);
86 h->pimpl_netcard = netcard;
89 xbt_lib_set(storage_lib, host->id, ROUTING_STORAGE_HOST_LEVEL, (void *) mount_list);
93 if (host->coord && strcmp(host->coord, "")) {
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);
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);
107 xbt_dynar_free(&ctn_str);
108 xbt_dynar_shrink(ctn, 0);
109 h->extension_set(COORD_HOST_LEVEL, (void *) ctn);
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);
119 if (host->pstate != 0)
120 cpu->setPState(host->pstate);
122 simgrid::s4u::Host::onCreation(*h);
124 if (TRACE_is_enabled() && TRACE_needs_platform())
125 sg_instr_new_host(host);
128 /** @brief Add a "router" to the network element list */
129 void sg_platf_new_router(sg_platf_router_cbarg_t router)
131 simgrid::surf::AsImpl* current_routing = routing_get_current();
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);
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());
143 if (router->coord && strcmp(router->coord, "")) {
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);
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);
161 if (TRACE_is_enabled() && TRACE_needs_platform())
162 sg_instr_new_router(router);
165 void sg_platf_new_link(sg_platf_link_cbarg_t link){
166 std::vector<char*> names;
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));
172 names.push_back(xbt_strdup(link->id));
174 for (auto link_name : names) {
175 Link *l = surf_network_model->createLink(link_name, link->bandwidth, link->latency, link->policy, link->properties);
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);
187 simgrid::surf::on_link(link);
190 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
192 using simgrid::surf::AsCluster;
193 using simgrid::surf::AsClusterTorus;
194 using simgrid::surf::AsClusterFatTree;
196 char *host_id, *groups, *link_id = NULL;
197 xbt_dict_t patterns = NULL;
200 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
201 s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
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);
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;
218 switch (cluster->topology) {
219 case SURF_CLUSTER_TORUS:
220 AS.routing = A_surfxml_AS_routing_ClusterTorus;
222 case SURF_CLUSTER_FAT_TREE:
223 AS.routing = A_surfxml_AS_routing_ClusterFatTree;
226 AS.routing = A_surfxml_AS_routing_Cluster;
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);
235 if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
236 current_as->nb_links_per_node_++;
237 current_as->has_loopback_ = 1;
240 if(cluster->limiter_link!=0){
241 current_as->nb_links_per_node_++;
242 current_as->has_limiter_ = 1;
246 xbt_dynar_t radical_elements = xbt_str_split(cluster->radical, ",");
247 xbt_dynar_foreach(radical_elements, iter, groups) {
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 *));
253 switch (xbt_dynar_length(radical_ends)) {
258 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
261 surf_parse_error("Malformed radical");
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);
268 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, cluster->speed);
270 memset(&host, 0, sizeof(host));
272 if ((cluster->properties != NULL) && (!xbt_dict_is_empty(cluster->properties))) {
273 xbt_dict_cursor_t cursor=NULL;
275 host.properties = xbt_dict_new();
277 xbt_dict_foreach(cluster->properties,cursor,key,data) {
278 xbt_dict_set(host.properties, key, xbt_strdup(data),free);
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);
289 XBT_DEBUG("\tavailability_file=\"\"");
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);
299 XBT_DEBUG("\tstate_file=\"\"");
302 host.speed_per_pstate = xbt_dynar_new(sizeof(double), NULL);
303 xbt_dynar_push(host.speed_per_pstate,&cluster->speed);
306 //host.power_peak = cluster->power;
307 host.core_amount = cluster->core_amount;
309 sg_platf_new_host(&host);
310 xbt_dynar_free(&host.speed_per_pstate);
311 XBT_DEBUG("</host>");
313 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id, cluster->bw, cluster->lat);
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
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);
328 memset(&link, 0, sizeof(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);
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);
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);
345 memset(&link, 0, sizeof(link));
347 link.bandwidth = cluster->limiter_link;
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);
353 xbt_dynar_set(current_as->privateLinks_, rankId * current_as->nb_links_per_node_ + current_as->has_loopback_ , &info_lim);
356 //call the cluster function that adds the others links
357 if (cluster->topology == SURF_CLUSTER_FAT_TREE) {
358 ((AsClusterFatTree*) current_as)->addProcessingNode(i);
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_ );
369 xbt_dynar_free(&radical_ends);
371 xbt_dynar_free(&radical_elements);
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();
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
380 XBT_DEBUG("<router id=\"%s\"/>", cluster->router_id);
382 s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
383 memset(&router, 0, sizeof(router));
384 router.id = cluster->router_id;
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);
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);
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;
403 sg_platf_new_link(&link);
405 routing_cluster_add_backbone(Link::byName(link_backbone));
411 sg_platf_new_AS_end();
413 xbt_dict_free(&patterns); // no op if it were never set
415 simgrid::surf::on_cluster(cluster);
417 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
418 simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
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());
423 cluster->backbone_ = bb;
424 XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name());
427 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
430 char *groups , *host_id , *link_id = NULL;
432 xbt_dynar_t radical_elements;
433 xbt_dynar_t radical_ends;
436 radical_elements = xbt_str_split(cabinet->radical, ",");
437 xbt_dynar_foreach(radical_elements, iter, groups) {
439 radical_ends = xbt_str_split(groups, "-");
440 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
442 switch (xbt_dynar_length(radical_ends)) {
447 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
450 surf_parse_error("Malformed radical");
453 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
454 memset(&host, 0, sizeof(host));
456 host.core_amount = 1;
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;
464 s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
465 memset(&host_link, 0, sizeof(host_link));
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);
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);
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);
491 xbt_dynar_free(&radical_ends);
493 xbt_dynar_free(&radical_elements);
496 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
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);
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);
504 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
509 xbt_lib_set(storage_lib, storage->id, ROUTING_STORAGE_LEVEL, (void *) xbt_strdup(storage->type_id));
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);
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",
524 ((storage_type_t) stype)->model,
525 ((storage_type_t) stype)->type_id,
527 storage->content_type,
528 storage->properties);
530 surf_storage_model->createStorage(storage->id,
531 ((storage_type_t) stype)->type_id,
533 storage->content_type,
537 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
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);
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;
551 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
552 "content '%s', and content_type '%s'",
555 storage_type->content,
556 storage_type->content_type);
558 xbt_lib_set(storage_type_lib,
560 ROUTING_STORAGE_TYPE_LEVEL,
564 static void mount_free(void *p)
566 mount_t mnt = (mount_t) p;
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);
574 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
577 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
578 mnt.name = xbt_strdup(mount->name);
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);
584 xbt_dynar_push(mount_list, &mnt);
587 void sg_platf_new_route(sg_platf_route_cbarg_t route)
589 routing_get_current()->addRoute(route);
592 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
594 routing_get_current()->addBypassRoute(bypassRoute);
597 void sg_platf_new_process(sg_platf_process_cbarg_t process)
599 xbt_assert(simix_global,"Cannot create process without SIMIX.");
601 sg_host_t host = sg_host_by_name(process->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);
607 xbt_dynar_t all_hosts = xbt_dynar_sort_strings(sg_hosts_as_dynar());
608 simgrid::s4u::Host* host;
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';
617 xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop
620 msg->data[msg->used-3]='\0';
621 xbt_die("%s", msg->data);
623 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
624 xbt_assert(parse_code, "Function '%s' unknown", process->function);
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;
630 smx_process_arg_t arg = NULL;
631 smx_process_t process_created = NULL;
633 arg = xbt_new0(s_smx_process_arg_t, 1);
634 arg->code = parse_code;
636 arg->hostname = sg_host_get_name(host);
637 arg->argc = process->argc;
638 arg->argv = xbt_new(char *,process->argc);
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);
648 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
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;
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;
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);
666 } else { // start_time <= SIMIX_get_clock()
667 XBT_DEBUG("Starting Process %s(%s) right now", arg->name, sg_host_get_name(host));
669 if (simix_global->create_process_function)
670 process_created = simix_global->create_process_function(
674 sg_host_get_name(host),
677 (char**)(process->argv),
678 current_property_set,
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);
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) {
689 current_property_set = NULL;
692 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
694 using simgrid::surf::NetCard;
695 using simgrid::surf::AsCluster;
697 char *host_id = bprintf("peer_%s", peer->id);
698 char *router_id = bprintf("router_%s", peer->id);
702 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
703 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
705 AS.routing = A_surfxml_AS_routing_Cluster;
706 sg_platf_new_AS_begin(&AS);
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));
713 host.speed_per_pstate = xbt_dynar_new(sizeof(double), NULL);
714 xbt_dynar_push(host.speed_per_pstate,&peer->speed);
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);
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;
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);
730 link.bandwidth = peer->bw_out;
731 sg_platf_new_link(&link);
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);
736 link.bandwidth = peer->bw_in;
737 sg_platf_new_link(&link);
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);
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));
758 sg_platf_new_AS_end();
765 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
767 void sg_platf_end() {
768 simgrid::surf::on_postparse();
771 /* Pick the right models for CPU, net and host, and call their model_init_preparse */
772 static void surf_config_models_setup()
774 const char *host_model_name;
775 const char *vm_model_name;
778 char *network_model_name = NULL;
779 char *cpu_model_name = NULL;
781 char *storage_model_name = NULL;
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");
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.
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);
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")) {
807 xbt_assert(cpu_model_name,
808 "Set a cpu model to use with the 'compound' host model");
810 xbt_assert(network_model_name,
811 "Set a network model to use with the 'compound' host model");
813 if(surf_cpu_model_init_preparse){
814 surf_cpu_model_init_preparse();
817 find_model_description(surf_cpu_model_description, cpu_model_name);
818 surf_cpu_model_description[cpu_id].model_init_preparse();
822 find_model_description(surf_network_model_description,
824 surf_network_model_description[network_id].model_init_preparse();
827 XBT_DEBUG("Call host_model_init");
828 surf_host_model_description[host_id].model_init_preparse();
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();
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();
841 * \brief Make a new routing component to the platform
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.
847 * Once this function was called, the configuration concerning the used
848 * models cannot be changed anymore.
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
853 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
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
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.
863 surf_parse_models_setup_already_called = 1;
864 surf_config_models_setup();
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);
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 */
874 /* search the routing model */
875 simgrid::surf::AsImpl *new_as = NULL;
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;
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);
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) {
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);
906 THROWF(arg_error, 0, "All defined components must belong to a AS");
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());
912 /* set the new current component of the tree */
913 current_routing = new_as;
914 current_routing->netcard_ = netcard;
916 simgrid::surf::asCreatedCallbacks(new_as);
917 if (TRACE_is_enabled())
918 sg_instr_AS_begin(AS);
922 * \brief Specify that the current description of AS is finished
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.
927 void sg_platf_new_AS_end()
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());
933 if (TRACE_is_enabled())
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)
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.");
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);
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);
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);
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);