Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not allocate/free properties
[simgrid.git] / src / surf / sg_platf.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/kernel/routing/ClusterZone.hpp"
8 #include "simgrid/kernel/routing/DijkstraZone.hpp"
9 #include "simgrid/kernel/routing/DragonflyZone.hpp"
10 #include "simgrid/kernel/routing/EmptyZone.hpp"
11 #include "simgrid/kernel/routing/FatTreeZone.hpp"
12 #include "simgrid/kernel/routing/FloydZone.hpp"
13 #include "simgrid/kernel/routing/FullZone.hpp"
14 #include "simgrid/kernel/routing/NetPoint.hpp"
15 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
16 #include "simgrid/kernel/routing/TorusZone.hpp"
17 #include "simgrid/kernel/routing/VivaldiZone.hpp"
18 #include "simgrid/kernel/routing/WifiZone.hpp"
19 #include "simgrid/s4u/Engine.hpp"
20 #include "src/include/simgrid/sg_config.hpp"
21 #include "src/include/surf/surf.hpp"
22 #include "src/kernel/EngineImpl.hpp"
23 #include "src/kernel/resource/DiskImpl.hpp"
24 #include "src/kernel/resource/profile/Profile.hpp"
25 #include "src/simix/smx_private.hpp"
26 #include "src/surf/HostImpl.hpp"
27 #include "src/surf/xml/platf_private.hpp"
28
29 #include <string>
30
31 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
32
33 namespace simgrid {
34 namespace kernel {
35 namespace routing {
36 xbt::signal<void(ClusterCreationArgs const&)> on_cluster_creation;
37 } // namespace routing
38 } // namespace kernel
39 } // namespace simgrid
40
41 /** The current NetZone in the parsing */
42 static simgrid::kernel::routing::NetZoneImpl* current_routing = nullptr;
43 static simgrid::kernel::routing::NetZoneImpl* routing_get_current()
44 {
45   return current_routing;
46 }
47
48 /** Module management function: creates all internal data structures */
49 void sg_platf_init()
50 {
51   // Do nothing: just for symmetry of user code
52 }
53
54 /** Module management function: frees all internal data structures */
55 void sg_platf_exit()
56 {
57   simgrid::kernel::routing::on_cluster_creation.disconnect_slots();
58   simgrid::s4u::Engine::on_platform_created.disconnect_slots();
59
60   surf_parse_lex_destroy();
61 }
62
63 /** @brief Add a host to the current NetZone */
64 void sg_platf_new_host(const simgrid::kernel::routing::HostCreationArgs* args)
65 {
66   simgrid::s4u::Host* host = routing_get_current()
67                                  ->create_host(args->id, args->speed_per_pstate)
68                                  ->set_coordinates(args->coord)
69                                  ->set_properties(args->properties);
70
71   host->get_impl()->set_disks(args->disks);
72
73   host->pimpl_cpu->set_core_count(args->core_amount)
74       ->set_state_profile(args->state_trace)
75       ->set_speed_profile(args->speed_trace);
76
77   host->seal();
78
79   /* When energy plugin is activated, changing the pstate requires to already have the HostEnergy extension whose
80    * allocation is triggered by the on_creation signal. Then set_pstate must be called after the signal emition */
81   if (args->pstate != 0)
82     host->set_pstate(args->pstate);
83 }
84
85 /** @brief Add a "router" to the network element list */
86 simgrid::kernel::routing::NetPoint* sg_platf_new_router(const std::string& name, const std::string& coords)
87 {
88   xbt_assert(nullptr == simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(name),
89              "Refusing to create a router named '%s': this name already describes a node.", name.c_str());
90
91   auto* netpoint = new simgrid::kernel::routing::NetPoint(name, simgrid::kernel::routing::NetPoint::Type::Router);
92   netpoint->set_englobing_zone(current_routing);
93   XBT_DEBUG("Router '%s' has the id %u", netpoint->get_cname(), netpoint->id());
94
95   if (not coords.empty())
96     netpoint->set_coordinates(coords);
97
98   return netpoint;
99 }
100
101 static void sg_platf_new_link(const simgrid::kernel::routing::LinkCreationArgs* args, const std::string& link_name)
102 {
103   routing_get_current()
104       ->create_link(link_name, args->bandwidths)
105       ->set_properties(args->properties)
106       ->get_impl() // this call to get_impl saves some simcalls but can be removed
107       ->set_sharing_policy(args->policy)
108       ->set_state_profile(args->state_trace)
109       ->set_latency_profile(args->latency_trace)
110       ->set_bandwidth_profile(args->bandwidth_trace)
111       ->set_latency(args->latency)
112       ->seal();
113 }
114
115 void sg_platf_new_link(const simgrid::kernel::routing::LinkCreationArgs* link)
116 {
117   if (link->policy == simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX) {
118     sg_platf_new_link(link, link->id + "_UP");
119     sg_platf_new_link(link, link->id + "_DOWN");
120   } else {
121     sg_platf_new_link(link, link->id);
122   }
123 }
124
125 void sg_platf_new_cluster(simgrid::kernel::routing::ClusterCreationArgs* cluster)
126 {
127   using simgrid::kernel::routing::ClusterZone;
128   using simgrid::kernel::routing::DragonflyZone;
129   using simgrid::kernel::routing::FatTreeZone;
130   using simgrid::kernel::routing::TorusZone;
131
132   int rankId = 0;
133
134   // What an inventive way of initializing the NetZone that I have as ancestor :-(
135   simgrid::kernel::routing::ZoneCreationArgs zone;
136   zone.id = cluster->id;
137   switch (cluster->topology) {
138     case simgrid::kernel::routing::ClusterTopology::TORUS:
139       zone.routing = "ClusterTorus";
140       break;
141     case simgrid::kernel::routing::ClusterTopology::DRAGONFLY:
142       zone.routing = "ClusterDragonfly";
143       break;
144     case simgrid::kernel::routing::ClusterTopology::FAT_TREE:
145       zone.routing = "ClusterFatTree";
146       break;
147     default:
148       zone.routing = "Cluster";
149       break;
150   }
151   sg_platf_new_Zone_begin(&zone);
152   auto* current_zone = static_cast<ClusterZone*>(routing_get_current());
153   current_zone->parse_specific_arguments(cluster);
154   for (auto const& elm : cluster->properties)
155     current_zone->get_iface()->set_property(elm.first, elm.second);
156
157   if (cluster->loopback_bw > 0 || cluster->loopback_lat > 0) {
158     current_zone->set_loopback();
159   }
160
161   if (cluster->limiter_link > 0) {
162     current_zone->set_limiter();
163   }
164
165   for (int const& i : cluster->radicals) {
166     std::string host_id = std::string(cluster->prefix) + std::to_string(i) + cluster->suffix;
167
168     XBT_DEBUG("<host\tid=\"%s\"\tspeed=\"%f\">", host_id.c_str(), cluster->speeds.front());
169     current_zone->create_host(host_id, cluster->speeds)
170         ->set_core_count(cluster->core_amount)
171         ->set_properties(cluster->properties)
172         ->seal();
173
174     XBT_DEBUG("</host>");
175
176     std::string link_id = std::string(cluster->id) + "_link_" + std::to_string(i);
177     XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id.c_str(), cluster->bw, cluster->lat);
178
179     // All links are saved in a matrix;
180     // every row describes a single node; every node may have multiple links.
181     // the first column may store a link from x to x if p_has_loopback is set
182     // the second column may store a limiter link if p_has_limiter is set
183     // other columns are to store one or more link for the node
184
185     // add a loopback link
186     if (cluster->loopback_bw > 0 || cluster->loopback_lat > 0) {
187       std::string loopback_name = link_id + "_loopback";
188       XBT_DEBUG("<loopback\tid=\"%s\"\tbw=\"%f\"/>", loopback_name.c_str(), cluster->loopback_bw);
189
190       auto* loopback = current_zone->create_link(loopback_name, std::vector<double>{cluster->loopback_bw})
191                            ->set_sharing_policy(simgrid::s4u::Link::SharingPolicy::FATPIPE)
192                            ->set_latency(cluster->loopback_lat)
193                            ->seal()
194                            ->get_impl();
195
196       current_zone->add_private_link_at(current_zone->node_pos(rankId), {loopback, loopback});
197     }
198
199     // add a limiter link (shared link to account for maximal bandwidth of the node)
200     if (cluster->limiter_link > 0) {
201       std::string limiter_name = std::string(link_id) + "_limiter";
202       XBT_DEBUG("<limiter\tid=\"%s\"\tbw=\"%f\"/>", limiter_name.c_str(), cluster->limiter_link);
203
204       auto* limiter =
205           current_zone->create_link(limiter_name, std::vector<double>{cluster->limiter_link})->seal()->get_impl();
206
207       current_zone->add_private_link_at(current_zone->node_pos_with_loopback(rankId), {limiter, limiter});
208     }
209
210     // call the cluster function that adds the others links
211     if (cluster->topology == simgrid::kernel::routing::ClusterTopology::FAT_TREE) {
212       static_cast<FatTreeZone*>(current_zone)->add_processing_node(i);
213     } else {
214       current_zone->create_links_for_node(cluster, i, rankId, current_zone->node_pos_with_loopback_limiter(rankId));
215     }
216     rankId++;
217   }
218
219   // Add a router.
220   XBT_DEBUG(" ");
221   XBT_DEBUG("<router id=\"%s\"/>", cluster->router_id.c_str());
222   if (cluster->router_id.empty())
223     cluster->router_id = std::string(cluster->prefix) + cluster->id + "_router" + cluster->suffix;
224   current_zone->set_router(sg_platf_new_router(cluster->router_id, ""));
225
226   // Make the backbone
227   if ((cluster->bb_bw > 0) || (cluster->bb_lat > 0)) {
228     std::string bb_name = std::string(cluster->id) + "_backbone";
229     XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/> <!--backbone -->", bb_name.c_str(), cluster->bb_bw,
230               cluster->bb_lat);
231
232     auto* backbone = current_zone->create_link(bb_name, std::vector<double>{cluster->bb_bw})
233                          ->set_sharing_policy(cluster->bb_sharing_policy)
234                          ->set_latency(cluster->bb_lat)
235                          ->seal();
236     current_zone->set_backbone(backbone->get_impl());
237   }
238
239   XBT_DEBUG("</zone>");
240   sg_platf_new_Zone_seal();
241
242   simgrid::kernel::routing::on_cluster_creation(*cluster);
243 }
244
245 void routing_cluster_add_backbone(simgrid::kernel::resource::LinkImpl* bb)
246 {
247   auto* cluster = dynamic_cast<simgrid::kernel::routing::ClusterZone*>(current_routing);
248
249   xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
250   xbt_assert(not cluster->has_backbone(), "Cluster %s already has a backbone link!", cluster->get_cname());
251
252   cluster->set_backbone(bb);
253   XBT_DEBUG("Add a backbone to zone '%s'", current_routing->get_cname());
254 }
255
256 void sg_platf_new_cabinet(const simgrid::kernel::routing::CabinetCreationArgs* cabinet)
257 {
258   auto* zone = static_cast<simgrid::kernel::routing::ClusterZone*>(routing_get_current());
259   for (int const& radical : cabinet->radicals) {
260     std::string id           = cabinet->prefix + std::to_string(radical) + cabinet->suffix;
261     simgrid::s4u::Host* host = zone->create_host(id, std::vector<double>{cabinet->speed})->seal();
262
263     simgrid::s4u::Link* link_up =
264         zone->create_link("link_" + id + "_UP", std::vector<double>{cabinet->bw})->set_latency(cabinet->lat)->seal();
265     simgrid::s4u::Link* link_down =
266         zone->create_link("link_" + id + "_DOWN", std::vector<double>{cabinet->bw})->set_latency(cabinet->lat)->seal();
267
268     zone->add_private_link_at(host->get_netpoint()->id(), {link_up->get_impl(), link_down->get_impl()});
269   }
270 }
271
272 simgrid::kernel::resource::DiskImpl* sg_platf_new_disk(const simgrid::kernel::routing::DiskCreationArgs* disk)
273 {
274   simgrid::kernel::resource::DiskImpl* pimpl = routing_get_current()
275                                                    ->create_disk(disk->id, disk->read_bw, disk->write_bw)
276                                                    ->set_properties(disk->properties)
277                                                    ->get_impl();
278
279   pimpl->seal();
280   simgrid::s4u::Disk::on_creation(*pimpl->get_iface());
281   return pimpl;
282 }
283
284 void sg_platf_new_route(simgrid::kernel::routing::RouteCreationArgs* route)
285 {
286   routing_get_current()->add_route(route->src, route->dst, route->gw_src, route->gw_dst, route->link_list,
287                                    route->symmetrical);
288 }
289
290 void sg_platf_new_bypassRoute(simgrid::kernel::routing::RouteCreationArgs* bypassRoute)
291 {
292   routing_get_current()->add_bypass_route(bypassRoute->src, bypassRoute->dst, bypassRoute->gw_src, bypassRoute->gw_dst,
293                                           bypassRoute->link_list, bypassRoute->symmetrical);
294 }
295
296 void sg_platf_new_actor(simgrid::kernel::routing::ActorCreationArgs* actor)
297 {
298   sg_host_t host = sg_host_by_name(actor->host);
299   if (not host) {
300     // The requested host does not exist. Do a nice message to the user
301     std::string msg = std::string("Cannot create actor '") + actor->function + "': host '" + actor->host +
302                       "' does not exist\nExisting hosts: '";
303
304     std::vector<simgrid::s4u::Host*> list = simgrid::s4u::Engine::get_instance()->get_all_hosts();
305
306     for (auto const& some_host : list) {
307       msg += some_host->get_name();
308       msg += "', '";
309       if (msg.length() > 1024) {
310         msg.pop_back(); // remove trailing quote
311         msg += "...(list truncated)......";
312         break;
313       }
314     }
315     xbt_die("%s", msg.c_str());
316   }
317   const simgrid::kernel::actor::ActorCodeFactory& factory =
318       simgrid::kernel::EngineImpl::get_instance()->get_function(actor->function);
319   xbt_assert(factory, "Error while creating an actor from the XML file: Function '%s' not registered", actor->function);
320
321   double start_time = actor->start_time;
322   double kill_time  = actor->kill_time;
323   bool auto_restart = actor->restart_on_failure;
324
325   std::string actor_name                 = actor->args[0];
326   simgrid::kernel::actor::ActorCode code = factory(std::move(actor->args));
327
328   auto* arg = new simgrid::kernel::actor::ProcessArg(actor_name, code, nullptr, host, kill_time, actor->properties,
329                                                      auto_restart);
330
331   host->get_impl()->add_actor_at_boot(arg);
332
333   if (start_time > SIMIX_get_clock()) {
334     arg = new simgrid::kernel::actor::ProcessArg(actor_name, code, nullptr, host, kill_time, actor->properties,
335                                                  auto_restart);
336
337     XBT_DEBUG("Process %s@%s will be started at time %f", arg->name.c_str(), arg->host->get_cname(), start_time);
338     simgrid::simix::Timer::set(start_time, [arg, auto_restart]() {
339       simgrid::kernel::actor::ActorImplPtr new_actor = simgrid::kernel::actor::ActorImpl::create(
340           arg->name.c_str(), arg->code, arg->data, arg->host, arg->properties, nullptr);
341       if (arg->kill_time >= 0)
342         new_actor->set_kill_time(arg->kill_time);
343       if (auto_restart)
344         new_actor->set_auto_restart(auto_restart);
345       delete arg;
346     });
347   } else { // start_time <= SIMIX_get_clock()
348     XBT_DEBUG("Starting Process %s(%s) right now", arg->name.c_str(), host->get_cname());
349
350     try {
351       simgrid::kernel::actor::ActorImplPtr new_actor = nullptr;
352       new_actor =
353           simgrid::kernel::actor::ActorImpl::create(arg->name.c_str(), code, nullptr, host, arg->properties, nullptr);
354       /* The actor creation will fail if the host is currently dead, but that's fine */
355       if (arg->kill_time >= 0)
356         new_actor->set_kill_time(arg->kill_time);
357       if (auto_restart)
358         new_actor->set_auto_restart(auto_restart);
359     } catch (simgrid::HostFailureException const&) {
360       XBT_WARN("Deployment includes some initially turned off Hosts ... nevermind.");
361     }
362   }
363 }
364
365 void sg_platf_new_peer(const simgrid::kernel::routing::PeerCreationArgs* peer)
366 {
367   auto* zone = dynamic_cast<simgrid::kernel::routing::VivaldiZone*>(current_routing);
368   xbt_assert(zone, "<peer> tag can only be used in Vivaldi netzones.");
369
370   simgrid::s4u::Host* host = zone->create_host(peer->id, std::vector<double>{peer->speed})
371                                  ->set_state_profile(peer->state_trace)
372                                  ->set_speed_profile(peer->speed_trace)
373                                  ->set_coordinates(peer->coord)
374                                  ->seal();
375
376   zone->set_peer_link(host->get_netpoint(), peer->bw_in, peer->bw_out);
377 }
378 /**
379  * @brief Auxiliary function to build the object NetZoneImpl
380  *
381  * Builds the objects, setting its father properties and root netzone if needed
382  * @param zone the parameters defining the Zone to build.
383  * @return Pointer to recently created netzone
384  */
385 static simgrid::kernel::routing::NetZoneImpl*
386 sg_platf_create_zone(const simgrid::kernel::routing::ZoneCreationArgs* zone)
387 {
388   /* search the routing model */
389   simgrid::kernel::routing::NetZoneImpl* new_zone = nullptr;
390
391   if (strcasecmp(zone->routing.c_str(), "Cluster") == 0) {
392     new_zone = new simgrid::kernel::routing::ClusterZone(zone->id);
393   } else if (strcasecmp(zone->routing.c_str(), "ClusterDragonfly") == 0) {
394     new_zone = new simgrid::kernel::routing::DragonflyZone(zone->id);
395   } else if (strcasecmp(zone->routing.c_str(), "ClusterTorus") == 0) {
396     new_zone = new simgrid::kernel::routing::TorusZone(zone->id);
397   } else if (strcasecmp(zone->routing.c_str(), "ClusterFatTree") == 0) {
398     new_zone = new simgrid::kernel::routing::FatTreeZone(zone->id);
399   } else if (strcasecmp(zone->routing.c_str(), "Dijkstra") == 0) {
400     new_zone = new simgrid::kernel::routing::DijkstraZone(zone->id, false);
401   } else if (strcasecmp(zone->routing.c_str(), "DijkstraCache") == 0) {
402     new_zone = new simgrid::kernel::routing::DijkstraZone(zone->id, true);
403   } else if (strcasecmp(zone->routing.c_str(), "Floyd") == 0) {
404     new_zone = new simgrid::kernel::routing::FloydZone(zone->id);
405   } else if (strcasecmp(zone->routing.c_str(), "Full") == 0) {
406     new_zone = new simgrid::kernel::routing::FullZone(zone->id);
407   } else if (strcasecmp(zone->routing.c_str(), "None") == 0) {
408     new_zone = new simgrid::kernel::routing::EmptyZone(zone->id);
409   } else if (strcasecmp(zone->routing.c_str(), "Vivaldi") == 0) {
410     new_zone = new simgrid::kernel::routing::VivaldiZone(zone->id);
411   } else if (strcasecmp(zone->routing.c_str(), "Wifi") == 0) {
412     new_zone = new simgrid::kernel::routing::WifiZone(zone->id);
413   } else {
414     xbt_die("Not a valid model!");
415   }
416   new_zone->set_parent(current_routing);
417
418   return new_zone;
419 }
420
421 /**
422  * @brief Add a Zone to the platform
423  *
424  * Add a new autonomous system to the platform. Any elements (such as host, router or sub-Zone) added after this call
425  * and before the corresponding call to sg_platf_new_Zone_seal() will be added to this Zone.
426  *
427  * Once this function was called, the configuration concerning the used models cannot be changed anymore.
428  *
429  * @param zone the parameters defining the Zone to build.
430  */
431 simgrid::kernel::routing::NetZoneImpl* sg_platf_new_Zone_begin(const simgrid::kernel::routing::ZoneCreationArgs* zone)
432 {
433   /* First create the zone.
434    * This order is important to assure that root netzone is set when models are setting
435    * the default mode for each resource (CPU, network, etc)
436    */
437   auto* new_zone = sg_platf_create_zone(zone);
438
439   _sg_cfg_init_status = 2; /* HACK: direct access to the global controlling the level of configuration to prevent
440                             * any further config now that we created some real content */
441
442   /* set the new current component of the tree */
443   current_routing = new_zone;
444   simgrid::s4u::NetZone::on_creation(*new_zone->get_iface()); // notify the signal
445
446   return new_zone;
447 }
448
449 void sg_platf_new_Zone_set_properties(const std::unordered_map<std::string, std::string>& props)
450 {
451   xbt_assert(current_routing, "Cannot set properties of the current Zone: none under construction");
452
453   current_routing->set_properties(props);
454 }
455
456 /**
457  * @brief Specify that the description of the current Zone is finished
458  *
459  * Once you've declared all the content of your Zone, you have to seal
460  * it with this call. Your Zone is not usable until you call this function.
461  */
462 void sg_platf_new_Zone_seal()
463 {
464   xbt_assert(current_routing, "Cannot seal the current Zone: zone under construction");
465   current_routing->seal();
466   simgrid::s4u::NetZone::on_seal(*current_routing->get_iface());
467   current_routing = current_routing->get_parent();
468 }
469
470 /** @brief Add a link connecting a host to the rest of its Zone (which must be cluster or vivaldi) */
471 void sg_platf_new_hostlink(const simgrid::kernel::routing::HostLinkCreationArgs* hostlink)
472 {
473   const simgrid::kernel::routing::NetPoint* netpoint = simgrid::s4u::Host::by_name(hostlink->id)->get_netpoint();
474   xbt_assert(netpoint, "Host '%s' not found!", hostlink->id.c_str());
475   xbt_assert(dynamic_cast<simgrid::kernel::routing::ClusterZone*>(current_routing),
476              "Only hosts from Cluster and Vivaldi Zones can get a host_link.");
477
478   const simgrid::s4u::Link* linkUp   = simgrid::s4u::Link::by_name_or_null(hostlink->link_up);
479   const simgrid::s4u::Link* linkDown = simgrid::s4u::Link::by_name_or_null(hostlink->link_down);
480
481   xbt_assert(linkUp, "Link '%s' not found!", hostlink->link_up.c_str());
482   xbt_assert(linkDown, "Link '%s' not found!", hostlink->link_down.c_str());
483
484   auto* cluster_zone = static_cast<simgrid::kernel::routing::ClusterZone*>(current_routing);
485
486   if (cluster_zone->private_link_exists_at(netpoint->id()))
487     surf_parse_error(std::string("Host_link for '") + hostlink->id.c_str() + "' is already defined!");
488
489   XBT_DEBUG("Push Host_link for host '%s' to position %u", netpoint->get_cname(), netpoint->id());
490   cluster_zone->add_private_link_at(netpoint->id(), {linkUp->get_impl(), linkDown->get_impl()});
491 }
492
493 void sg_platf_new_trace(simgrid::kernel::routing::ProfileCreationArgs* profile)
494 {
495   simgrid::kernel::profile::Profile* mgr_profile;
496   if (not profile->file.empty()) {
497     mgr_profile = simgrid::kernel::profile::Profile::from_file(profile->file);
498   } else {
499     xbt_assert(not profile->pc_data.empty(), "Trace '%s' must have either a content, or point to a file on disk.",
500                profile->id.c_str());
501     mgr_profile = simgrid::kernel::profile::Profile::from_string(profile->id, profile->pc_data, profile->periodicity);
502   }
503   traces_set_list.insert({profile->id, mgr_profile});
504 }