Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
less char*, more std::string
[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/functional.hpp>
13 #include <xbt/signal.hpp>
14 #include "src/surf/HostImpl.hpp"
15 #include "surf/surf.h"
16
17 #include "src/simix/smx_private.h"
18
19 #include "src/include/simgrid/sg_config.h"
20 #include "src/surf/xml/platf_private.hpp"
21
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
26
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"
37
38 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
39
40 XBT_PRIVATE xbt_dynar_t mount_list = nullptr;
41
42 namespace simgrid {
43 namespace surf {
44
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;
48
49 }
50 }
51
52 static int surf_parse_models_setup_already_called = 0;
53
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()
57 {
58   return current_routing;
59 }
60
61 /** Module management function: creates all internal data structures */
62 void sg_platf_init() {
63 }
64
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();
70
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();
74 }
75
76 /** @brief Add an host to the current AS */
77 void sg_platf_new_host(sg_platf_host_cbarg_t host)
78 {
79   simgrid::kernel::routing::AsImpl* current_routing = routing_get_current();
80
81   simgrid::s4u::Host* h = new simgrid::s4u::Host(host->id);
82   current_routing->attachHost(h);
83
84   if (host->coord && strcmp(host->coord, "")) {
85     unsigned int cursor;
86     char*str;
87
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);
92
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);
97     }
98     xbt_dynar_free(&ctn_str);
99     xbt_dynar_shrink(ctn, 0);
100     h->extension_set(COORD_HOST_LEVEL, (void *) ctn);
101   }
102
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);
108
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));
111
112   mount_list = nullptr;
113
114   if (host->properties) {
115     xbt_dict_cursor_t cursor=nullptr;
116     char *key,*data;
117     xbt_dict_foreach(host->properties,cursor,key,data)
118       h->setProperty(key,data);
119     xbt_dict_free(&host->properties);
120   }
121
122   if (host->pstate != 0)
123     cpu->setPState(host->pstate);
124
125   simgrid::s4u::Host::onCreation(*h);
126
127   if (TRACE_is_enabled() && TRACE_needs_platform())
128     sg_instr_new_host(host);
129 }
130
131 /** @brief Add a "router" to the network element list */
132 void sg_platf_new_router(sg_platf_router_cbarg_t router)
133 {
134   using simgrid::kernel::routing::AsCluster;
135   simgrid::kernel::routing::AsImpl* current_routing = routing_get_current();
136
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);
141
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());
146
147   if (router->coord && strcmp(router->coord, "")) {
148     unsigned int cursor;
149     char*str;
150
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);
159     }
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);
163   }
164
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));
168
169   if (TRACE_is_enabled() && TRACE_needs_platform())
170     sg_instr_new_router(router);
171 }
172
173 void sg_platf_new_link(sg_platf_link_cbarg_t link){
174   std::vector<char*> names;
175
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));
179   } else {
180     names.push_back(xbt_strdup(link->id));
181   }
182   for (auto link_name : names) {
183     Link *l = surf_network_model->createLink(link_name, link->bandwidth, link->latency, link->policy, link->properties);
184
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);
191
192     xbt_free(link_name);
193   }
194
195   simgrid::surf::on_link(link);
196 }
197
198 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
199 {
200   using simgrid::kernel::routing::AsCluster;
201   using simgrid::kernel::routing::AsClusterDragonfly;
202   using simgrid::kernel::routing::AsClusterFatTree;
203   using simgrid::kernel::routing::AsClusterTorus;
204
205   int rankId=0;
206
207   s_sg_platf_link_cbarg_t link;
208
209   // What an inventive way of initializing the AS that I have as ancestor :-(
210   s_sg_platf_AS_cbarg_t AS;
211   AS.id = cluster->id;
212   switch (cluster->topology) {
213   case SURF_CLUSTER_TORUS:
214     AS.routing = A_surfxml_AS_routing_ClusterTorus;
215     break;
216   case SURF_CLUSTER_DRAGONFLY:
217     AS.routing = A_surfxml_AS_routing_ClusterDragonfly;
218     break;
219   case SURF_CLUSTER_FAT_TREE:
220     AS.routing = A_surfxml_AS_routing_ClusterFatTree;
221     break;
222   default:
223     AS.routing = A_surfxml_AS_routing_Cluster;
224     break;
225   }
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);
229
230   if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
231     current_as->linkCountPerNode_++;
232     current_as->hasLoopback_ = 1;
233   }
234
235   if(cluster->limiter_link!=0){
236     current_as->linkCountPerNode_++;
237     current_as->hasLimiter_ = 1;
238   }
239
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);
243
244     XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, cluster->speed);
245
246     s_sg_platf_host_cbarg_t host;
247     memset(&host, 0, sizeof(host));
248     host.id = host_id;
249     if ((cluster->properties != nullptr) && (!xbt_dict_is_empty(cluster->properties))) {
250       xbt_dict_cursor_t cursor=nullptr;
251       char *key,*data;
252       host.properties = xbt_dict_new();
253
254       xbt_dict_foreach(cluster->properties,cursor,key,data) {
255         xbt_dict_set(host.properties, key, xbt_strdup(data),free);
256       }
257     }
258
259     host.speed_per_pstate.push_back(cluster->speed);
260     host.pstate = 0;
261     host.core_amount = cluster->core_amount;
262     host.coord = "";
263     sg_platf_new_host(&host);
264     XBT_DEBUG("</host>");
265
266     XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id, cluster->bw, cluster->lat);
267
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
275
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);
280
281       memset(&link, 0, sizeof(link));
282       link.id        = tmp_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);
289       free(tmp_link);
290
291       auto as_cluster = static_cast<AsCluster*>(current_as);
292       as_cluster->privateLinks_.insert({rankId*as_cluster->linkCountPerNode_, info_loop});
293     }
294
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);
299
300       memset(&link, 0, sizeof(link));
301       link.id = tmp_link;
302       link.bandwidth = cluster->limiter_link;
303       link.latency = 0;
304       link.policy = SURF_LINK_SHARED;
305       sg_platf_new_link(&link);
306       info_lim.linkUp = info_lim.linkDown = Link::byName(tmp_link);
307       free(tmp_link);
308       current_as->privateLinks_.insert(
309           {rankId * current_as->linkCountPerNode_ + current_as->hasLoopback_ , info_lim});
310     }
311
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);
315     }
316     else {
317       current_as->create_links_for_node(cluster, i, rankId,
318           rankId*current_as->linkCountPerNode_ + current_as->hasLoopback_ + current_as->hasLimiter_ );
319     }
320     xbt_free(link_id);
321     xbt_free(host_id);
322     rankId++;
323   }
324
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
327   XBT_DEBUG(" ");
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);
337   free(newid);
338
339   //Make the backbone
340   if ((cluster->bb_bw != 0) || (cluster->bb_lat != 0)) {
341
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;
347
348     XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link.id, cluster->bb_bw, cluster->bb_lat);
349     sg_platf_new_link(&link);
350
351     routing_cluster_add_backbone(Link::byName(link.id));
352     free((char*)link.id);
353   }
354
355   XBT_DEBUG("</AS>");
356   sg_platf_new_AS_seal();
357
358   simgrid::surf::on_cluster(cluster);
359   delete cluster->radicals;
360 }
361 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
362   simgrid::kernel::routing::AsCluster *cluster = dynamic_cast<simgrid::kernel::routing::AsCluster*>(current_routing);
363
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());
366
367   cluster->backbone_ = bb;
368   XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name());
369 }
370
371 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
372 {
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));
377     host.pstate           = 0;
378     host.core_amount      = 1;
379     host.id               = hostname;
380     host.speed_per_pstate.push_back(cabinet->speed);
381     sg_platf_new_host(&host);
382
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);
391
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);
400
401     free(hostname);
402   }
403   delete cabinet->radicals;
404 }
405
406 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
407 {
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);
410
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);
413
414   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
415       storage->id,
416       storage->type_id,
417       storage->content);
418
419   xbt_lib_set(storage_lib, storage->id, ROUTING_STORAGE_LEVEL, (void *) xbt_strdup(storage->type_id));
420
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);
428   }
429
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",
433       storage->id,
434       ((storage_type_t) stype)->model,
435       ((storage_type_t) stype)->type_id,
436       storage->content,
437       storage->content_type,
438     storage->properties);
439
440   surf_storage_model->createStorage(storage->id,
441                                      ((storage_type_t) stype)->type_id,
442                                      storage->content,
443                                      storage->content_type,
444                    storage->properties,
445                                      storage->attach);
446 }
447 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
448
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);
451
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;
460
461   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
462       "content '%s', and content_type '%s'",
463       stype->type_id,
464       stype->model,
465       storage_type->content,
466       storage_type->content_type);
467
468   xbt_lib_set(storage_type_lib,
469       stype->type_id,
470       ROUTING_STORAGE_TYPE_LEVEL,
471       (void *) stype);
472 }
473
474 static void mount_free(void *p)
475 {
476   mount_t mnt = (mount_t) p;
477   xbt_free(mnt->name);
478 }
479
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);
483
484   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
485
486   s_mount_t mnt;
487   mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
488   mnt.name = xbt_strdup(mount->name);
489
490   if(!mount_list){
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);
493   }
494   xbt_dynar_push(mount_list, &mnt);
495 }
496
497 void sg_platf_new_route(sg_platf_route_cbarg_t route)
498 {
499   routing_get_current()->addRoute(route);
500 }
501
502 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
503 {
504   routing_get_current()->addBypassRoute(bypassRoute);
505 }
506
507 void sg_platf_new_process(sg_platf_process_cbarg_t process)
508 {
509   sg_host_t host = sg_host_by_name(process->host);
510   if (!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);
514     free(tmp);
515     xbt_dynar_t all_hosts = xbt_dynar_sort_strings(sg_hosts_as_dynar());
516     simgrid::s4u::Host* host;
517     unsigned int cursor;
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';
523         msg->used -= 3;
524
525         xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop
526         break;
527       }
528     }
529     msg->data[msg->used-3]='\0';
530     xbt_die("%s", msg->data);
531   }
532   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(process->function);
533   xbt_assert(factory, "Function '%s' unknown", process->function);
534
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;
538
539   std::vector<std::string> args(process->argv, process->argv + process->argc);
540   std::function<void()> code = factory(std::move(args));
541
542   smx_process_arg_t arg = nullptr;
543   smx_actor_t process_created = nullptr;
544
545   arg = new simgrid::simix::ProcessArg();
546   arg->name = std::string(process->argv[0]);
547   arg->code = code;
548   arg->data = nullptr;
549   arg->host = host;
550   arg->kill_time = kill_time;
551   arg->properties = current_property_set;
552
553   sg_host_simix(host)->boot_processes.push_back(arg);
554
555   if (start_time > SIMIX_get_clock()) {
556
557     arg = new simgrid::simix::ProcessArg();
558     arg->name = std::string(process->argv[0]);
559     arg->code = std::move(code);
560     arg->data = nullptr;
561     arg->host = host;
562     arg->kill_time = kill_time;
563     arg->properties = current_property_set;
564
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(
569                                             arg->name.c_str(),
570                                             std::move(arg->code),
571                                             arg->data,
572                                             arg->host,
573                                             arg->kill_time,
574                                             arg->properties,
575                                             arg->auto_restart,
576                                             nullptr);
577       delete arg;
578     });
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));
582
583     process_created = simix_global->create_process_function(
584         arg->name.c_str(), std::move(code), nullptr,
585         host, kill_time,
586         current_property_set, auto_restart, nullptr);
587
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) {
590       return;
591     }
592   }
593   current_property_set = nullptr;
594 }
595
596 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
597 {
598   using simgrid::kernel::routing::NetCard;
599   using simgrid::kernel::routing::AsCluster;
600
601   char *host_id = bprintf("peer_%s", peer->id);
602   char *router_id = bprintf("router_%s", peer->id);
603
604   XBT_DEBUG(" ");
605
606   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
607   s_sg_platf_AS_cbarg_t AS;
608   AS.id      = peer->id;
609   AS.routing = A_surfxml_AS_routing_Cluster;
610   sg_platf_new_AS_begin(&AS);
611
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));
615   host.id = host_id;
616
617   host.speed_per_pstate.push_back(peer->speed);
618   host.pstate = 0;
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);
623
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;
628
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);
631   link.id = link_up;
632   link.bandwidth = peer->bw_out;
633   sg_platf_new_link(&link);
634
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);
637   link.id = link_down;
638   link.bandwidth = peer->bw_in;
639   sg_platf_new_link(&link);
640
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);
648   free(link_up);
649   free(link_down);
650
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);
657
658   XBT_DEBUG("</AS>");
659   sg_platf_new_AS_seal();
660   XBT_DEBUG(" ");
661
662   free(router_id);
663   free(host_id);
664 }
665
666 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
667
668 void sg_platf_end() {
669   simgrid::surf::on_postparse();
670 }
671
672 /* Pick the right models for CPU, net and host, and call their model_init_preparse */
673 static void surf_config_models_setup()
674 {
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");
680
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);
686   }
687
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");
692
693     int cpu_id = find_model_description(surf_cpu_model_description, cpu_model_name);
694     surf_cpu_model_description[cpu_id].model_init_preparse();
695
696     int network_id = find_model_description(surf_network_model_description, network_model_name);
697     surf_network_model_description[network_id].model_init_preparse();
698   }
699
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();
703
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();
707
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();
711 }
712
713 /**
714  * \brief Add an AS to the platform
715  *
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.
719  *
720  * Once this function was called, the configuration concerning the used
721  * models cannot be changed anymore.
722  *
723  * @param AS the parameters defining the AS to build.
724  */
725 simgrid::s4u::As * sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
726 {
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
730      *
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.
734      */
735     surf_parse_models_setup_already_called = 1;
736     surf_config_models_setup();
737   }
738
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 */
741
742
743   /* search the routing model */
744   simgrid::kernel::routing::AsImpl *new_as = nullptr;
745   switch(AS->routing){
746     case A_surfxml_AS_routing_Cluster:
747       new_as = new simgrid::kernel::routing::AsCluster(current_routing, AS->id);
748       break;
749     case A_surfxml_AS_routing_ClusterDragonfly:
750       new_as = new simgrid::kernel::routing::AsClusterDragonfly(current_routing, AS->id);
751       break;
752     case A_surfxml_AS_routing_ClusterTorus:
753       new_as = new simgrid::kernel::routing::AsClusterTorus(current_routing, AS->id);
754       break;
755     case A_surfxml_AS_routing_ClusterFatTree:
756       new_as = new simgrid::kernel::routing::AsClusterFatTree(current_routing, AS->id);
757       break;
758     case A_surfxml_AS_routing_Dijkstra:
759       new_as = new simgrid::kernel::routing::AsDijkstra(current_routing, AS->id, 0);
760       break;
761     case A_surfxml_AS_routing_DijkstraCache:
762       new_as = new simgrid::kernel::routing::AsDijkstra(current_routing, AS->id, 1);
763       break;
764     case A_surfxml_AS_routing_Floyd:
765       new_as = new simgrid::kernel::routing::AsFloyd(current_routing, AS->id);
766       break;
767     case A_surfxml_AS_routing_Full:
768       new_as = new simgrid::kernel::routing::AsFull(current_routing, AS->id);
769       break;
770     case A_surfxml_AS_routing_None:
771       new_as = new simgrid::kernel::routing::AsNone(current_routing, AS->id);
772       break;
773     case A_surfxml_AS_routing_Vivaldi:
774       new_as = new simgrid::kernel::routing::AsVivaldi(current_routing, AS->id);
775       break;
776     default:
777       xbt_die("Not a valid model!");
778       break;
779   }
780
781
782   if (current_routing == nullptr && routing_platf->root_ == nullptr) { /* it is the first one */
783     routing_platf->root_ = new_as;
784
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);
791
792   } else {
793     THROWF(arg_error, 0, "All defined components must belong to a AS");
794   }
795
796   /* set the new current component of the tree */
797   current_routing = new_as;
798
799   simgrid::kernel::routing::asCreatedCallbacks(new_as);
800   if (TRACE_is_enabled())
801     sg_instr_AS_begin(AS);
802
803   return new_as;
804 }
805
806 /**
807  * \brief Specify that the description of the current AS is finished
808  *
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.
811  */
812 void sg_platf_new_AS_seal()
813 {
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());
817
818   if (TRACE_is_enabled())
819     sg_instr_AS_end();
820 }
821
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)
824 {
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.");
829
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);
833
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);
836
837   auto as_cluster = static_cast<simgrid::kernel::routing::AsCluster*>(current_routing);
838
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);
841
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});
844 }