Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / s4u / s4u_Host.cpp
1 /* Copyright (c) 2006-2022. 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/host.h>
8 #include <simgrid/kernel/routing/NetPoint.hpp>
9 #include <simgrid/s4u/Comm.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/Exec.hpp>
12 #include <simgrid/s4u/Host.hpp>
13 #include <simgrid/s4u/VirtualMachine.hpp>
14 #include <xbt/parse_units.hpp>
15
16 #include "src/kernel/resource/StandardLinkImpl.hpp"
17 #include "src/kernel/resource/VirtualMachineImpl.hpp"
18 #include "src/surf/HostImpl.hpp"
19
20 #include <string>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_host, s4u, "Logging specific to the S4U hosts");
23 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
24
25 namespace simgrid {
26
27 template class xbt::Extendable<s4u::Host>;
28
29 namespace s4u {
30
31 xbt::signal<void(Host&)> Host::on_creation;
32 xbt::signal<void(Host const&)> Host::on_destruction;
33 xbt::signal<void(Host const&)> Host::on_state_change;
34 xbt::signal<void(Host const&)> Host::on_speed_change;
35
36 Host* Host::set_cpu(kernel::resource::CpuImpl* cpu)
37 {
38   pimpl_cpu_ = cpu;
39   return this;
40 }
41
42 #ifndef DOXYGEN
43 Host* Host::set_netpoint(kernel::routing::NetPoint* netpoint)
44 {
45   pimpl_netpoint_ = netpoint;
46   return this;
47 }
48
49 Host::~Host()
50 {
51   if (pimpl_netpoint_ != nullptr) // not removed yet by a children class
52     Engine::get_instance()->netpoint_unregister(pimpl_netpoint_);
53   delete pimpl_cpu_;
54 }
55 #endif
56
57 /** @brief Fire the required callbacks and destroy the object
58  *
59  * Don't delete directly a host, call h->destroy() instead.
60  *
61  * This is cumbersome but this is the simplest solution to ensure that the on_destruction() callback receives a valid
62  * object (because of the destructor order in a class hierarchy).
63  */
64 void Host::destroy()
65 {
66   kernel::actor::simcall([this] { this->pimpl_->destroy(); });
67 }
68
69 Host* Host::by_name(const std::string& name)
70 {
71   return Engine::get_instance()->host_by_name(name);
72 }
73 Host* Host::by_name_or_null(const std::string& name)
74 {
75   return Engine::get_instance()->host_by_name_or_null(name);
76 }
77
78 Host* Host::current()
79 {
80   kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
81   xbt_assert(self != nullptr, "Cannot call Host::current() from the maestro context");
82   return self->get_host();
83 }
84
85 xbt::string const& Host::get_name() const
86 {
87   return this->pimpl_->get_name();
88 }
89
90 const char* Host::get_cname() const
91 {
92   return this->pimpl_->get_cname();
93 }
94
95 void Host::turn_on()
96 {
97   if (not is_on()) {
98     kernel::actor::simcall([this] {
99       this->pimpl_cpu_->turn_on();
100       this->pimpl_->turn_on();
101       on_state_change(*this);
102     });
103   }
104 }
105
106 /** @brief Stop the host if it is on */
107 void Host::turn_off()
108 {
109   if (is_on()) {
110     const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
111     kernel::actor::simcall([this, self] {
112       for (VirtualMachine* const& vm : kernel::resource::VirtualMachineImpl::allVms_)
113         if (vm->get_pm() == this) {
114           vm->shutdown();
115           vm->turn_off();
116         }
117       this->pimpl_cpu_->turn_off();
118       this->pimpl_->turn_off(self);
119
120       on_state_change(*this);
121     });
122   }
123 }
124
125 bool Host::is_on() const
126 {
127   return this->pimpl_cpu_->is_on();
128 }
129
130 unsigned long Host::get_pstate_count() const
131 {
132   return this->pimpl_cpu_->get_pstate_count();
133 }
134
135 /**
136  * @brief Return a copy of the list of actors that are executing on this host.
137  *
138  * Daemons and regular actors are all mixed in this list.
139  */
140 std::vector<ActorPtr> Host::get_all_actors() const
141 {
142   return pimpl_->get_all_actors();
143 }
144
145 /** @brief Returns how many actors (daemonized or not) have been launched on this host */
146 size_t Host::get_actor_count() const
147 {
148   return pimpl_->get_actor_count();
149 }
150
151 /**
152  * @brief Find a route toward another host
153  *
154  * @param dest [IN] where to
155  * @param links [OUT] where to store the list of links (must exist, cannot be nullptr).
156  * @param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
157  *                It is the caller responsibility to initialize latency to 0 (we add to provided route)
158  *
159  * walk through the routing components tree and find a route between hosts
160  * by calling each "get_route" function in each routing component.
161  */
162 void Host::route_to(const Host* dest, std::vector<Link*>& links, double* latency) const
163 {
164   std::vector<kernel::resource::StandardLinkImpl*> linkImpls;
165   this->route_to(dest, linkImpls, latency);
166   for (auto* l : linkImpls)
167     links.push_back(l->get_iface());
168 }
169
170 /** @brief Just like Host::routeTo, but filling an array of link implementations */
171 void Host::route_to(const Host* dest, std::vector<kernel::resource::StandardLinkImpl*>& links, double* latency) const
172 {
173   kernel::routing::NetZoneImpl::get_global_route(pimpl_netpoint_, dest->get_netpoint(), links, latency);
174   if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
175     XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", get_cname(), dest->get_cname(),
176                (latency == nullptr ? -1 : *latency));
177     for (auto const* link : links)
178       XBT_CDEBUG(surf_route, "  Link '%s'", link->get_cname());
179   }
180 }
181
182 /** @brief Returns the networking zone englobing that host */
183 NetZone* Host::get_englobing_zone() const
184 {
185   return pimpl_netpoint_->get_englobing_zone()->get_iface();
186 }
187
188 #ifndef DOXYGEN
189 void Host::sendto(Host* dest, double byte_amount) // XBT_ATTRIB_DEPRECATED_v331
190 {
191   Comm::sendto_async(this, dest, byte_amount)->wait();
192 }
193
194 CommPtr Host::sendto_async(Host* dest, double byte_amount) // XBT_ATTRIB_DEPRECATED_v331
195 {
196   return Comm::sendto_async(this, dest, byte_amount);
197 }
198 #endif
199
200 /** Get the properties assigned to a host */
201 const std::unordered_map<std::string, std::string>* Host::get_properties() const
202 {
203   return this->pimpl_->get_properties();
204 }
205
206 /** Retrieve the property value (or nullptr if not set) */
207 const char* Host::get_property(const std::string& key) const
208 {
209   return this->pimpl_->get_property(key);
210 }
211
212 Host* Host::set_property(const std::string& key, const std::string& value)
213 {
214   kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
215   return this;
216 }
217
218 Host* Host::set_properties(const std::unordered_map<std::string, std::string>& properties)
219 {
220   kernel::actor::simcall([this, &properties] { this->pimpl_->set_properties(properties); });
221   return this;
222 }
223
224 /** Specify a profile turning the host on and off according to an exhaustive list or a stochastic law.
225  * The profile must contain boolean values. */
226 Host* Host::set_state_profile(kernel::profile::Profile* p)
227 {
228   kernel::actor::simcall([this, p] { pimpl_cpu_->set_state_profile(p); });
229   return this;
230 }
231 /** Specify a profile modeling the external load according to an exhaustive list or a stochastic law.
232  *
233  * Each event of the profile represent a peak speed change that is due to external load. The values are given as a rate
234  * of the initial value. This means that the actual value is obtained by multiplying the initial value (the peek speed
235  * at this pstate level) by the rate coming from the profile.
236  */
237 Host* Host::set_speed_profile(kernel::profile::Profile* p)
238 {
239   kernel::actor::simcall([this, p] { pimpl_cpu_->set_speed_profile(p); });
240   return this;
241 }
242
243 /** @brief Get the peak processor speed (in flops/s), at the specified pstate  */
244 double Host::get_pstate_speed(unsigned long pstate_index) const
245 {
246   return this->pimpl_cpu_->get_pstate_peak_speed(pstate_index);
247 }
248
249 double Host::get_speed() const
250 {
251   return this->pimpl_cpu_->get_speed(1.0);
252 }
253 double Host::get_load() const
254 {
255   return this->pimpl_cpu_->get_load();
256 }
257 double Host::get_available_speed() const
258 {
259   return this->pimpl_cpu_->get_speed_ratio();
260 }
261
262 Host* Host::set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
263 {
264   kernel::actor::simcall([this, policy, &cb] { pimpl_cpu_->set_sharing_policy(policy, cb); });
265   return this;
266 }
267
268 Host::SharingPolicy Host::get_sharing_policy() const
269 {
270   return this->pimpl_cpu_->get_sharing_policy();
271 }
272
273 int Host::get_core_count() const
274 {
275   return this->pimpl_cpu_->get_core_count();
276 }
277
278 Host* Host::set_core_count(int core_count)
279 {
280   kernel::actor::simcall([this, core_count] { this->pimpl_cpu_->set_core_count(core_count); });
281   return this;
282 }
283
284 Host* Host::set_pstate_speed(const std::vector<double>& speed_per_state)
285 {
286   kernel::actor::simcall([this, &speed_per_state] { pimpl_cpu_->set_pstate_speed(speed_per_state); });
287   return this;
288 }
289
290 std::vector<double> Host::convert_pstate_speed_vector(const std::vector<std::string>& speed_per_state)
291 {
292   std::vector<double> speed_list;
293   speed_list.reserve(speed_per_state.size());
294   for (const auto& speed_str : speed_per_state) {
295     try {
296       double speed = xbt_parse_get_speed("", 0, speed_str, "");
297       speed_list.push_back(speed);
298     } catch (const simgrid::ParseError&) {
299       throw std::invalid_argument(std::string("Invalid speed value: ") + speed_str);
300     }
301   }
302   return speed_list;
303 }
304
305 Host* Host::set_pstate_speed(const std::vector<std::string>& speed_per_state)
306 {
307   set_pstate_speed(Host::convert_pstate_speed_vector(speed_per_state));
308   return this;
309 }
310
311 /** @brief Set the pstate at which the host should run */
312 Host* Host::set_pstate(unsigned long pstate_index)
313 {
314   kernel::actor::simcall([this, pstate_index] { this->pimpl_cpu_->set_pstate(pstate_index); });
315   return this;
316 }
317
318 /** @brief Retrieve the pstate at which the host is currently running */
319 unsigned long Host::get_pstate() const
320 {
321   return this->pimpl_cpu_->get_pstate();
322 }
323
324 Host* Host::set_factor_cb(const std::function<CpuFactorCb>& cb)
325 {
326   kernel::actor::simcall([this, &cb] { pimpl_cpu_->set_factor_cb(cb); });
327   return this;
328 }
329
330 Host* Host::set_coordinates(const std::string& coords)
331 {
332   if (not coords.empty())
333     kernel::actor::simcall([this, coords] { this->pimpl_netpoint_->set_coordinates(coords); });
334   return this;
335 }
336 std::vector<Disk*> Host::get_disks() const
337 {
338   return this->pimpl_->get_disks();
339 }
340
341 Disk* Host::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
342 {
343   return kernel::actor::simcall([this, &name, read_bandwidth, write_bandwidth] {
344     auto* disk = pimpl_->create_disk(name, read_bandwidth, write_bandwidth);
345     pimpl_->add_disk(disk);
346     return disk;
347   });
348 }
349
350 Disk* Host::create_disk(const std::string& name, const std::string& read_bandwidth, const std::string& write_bandwidth)
351 {
352   double d_read;
353   try {
354     d_read = xbt_parse_get_bandwidth("", 0, read_bandwidth, "");
355   } catch (const simgrid::ParseError&) {
356     throw std::invalid_argument(std::string("Impossible to create disk: ") + name +
357                                 std::string(". Invalid read bandwidth: ") + read_bandwidth);
358   }
359   double d_write;
360   try {
361     d_write = xbt_parse_get_bandwidth("", 0, write_bandwidth, "");
362   } catch (const simgrid::ParseError&) {
363     throw std::invalid_argument(std::string("Impossible to create disk: ") + name +
364                                 std::string(". Invalid write bandwidth: ") + write_bandwidth);
365   }
366   return create_disk(name, d_read, d_write);
367 }
368
369 void Host::add_disk(const Disk* disk)
370 {
371   kernel::actor::simcall([this, disk] { this->pimpl_->add_disk(disk); });
372 }
373
374 void Host::remove_disk(const std::string& disk_name)
375 {
376   kernel::actor::simcall([this, disk_name] { this->pimpl_->remove_disk(disk_name); });
377 }
378
379 VirtualMachine* Host::create_vm(const std::string& name, int core_amount)
380 {
381   return new VirtualMachine(name, this, core_amount);
382 }
383
384 VirtualMachine* Host::create_vm(const std::string& name, int core_amount, size_t ramsize)
385 {
386   return new VirtualMachine(name, this, core_amount, ramsize);
387 }
388
389 ExecPtr Host::exec_init(double flops) const
390 {
391   return this_actor::exec_init(flops);
392 }
393
394 ExecPtr Host::exec_async(double flops) const
395 {
396   return this_actor::exec_async(flops);
397 }
398
399 void Host::execute(double flops) const
400 {
401   execute(flops, 1.0 /* priority */);
402 }
403
404 void Host::execute(double flops, double priority) const
405 {
406   this_actor::exec_init(flops)->set_priority(1 / priority)->vetoable_start()->wait();
407 }
408
409 Host* Host::seal()
410 {
411   kernel::actor::simcall([this]() { this->pimpl_->seal(); });
412   simgrid::s4u::Host::on_creation(*this); // notify the signal
413   return this;
414 }
415
416 } // namespace s4u
417 } // namespace simgrid
418
419 /* **************************** Public C interface *************************** */
420 size_t sg_host_count()
421 {
422   return simgrid::s4u::Engine::get_instance()->get_host_count();
423 }
424 sg_host_t* sg_host_list()
425 {
426   const simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
427   size_t host_count             = e->get_host_count();
428
429   xbt_assert(host_count > 0, "There is no host!");
430   std::vector<simgrid::s4u::Host*> hosts = e->get_all_hosts();
431
432   auto* res = xbt_new(sg_host_t, hosts.size());
433   std::copy(begin(hosts), end(hosts), res);
434
435   return res;
436 }
437
438 const char* sg_host_get_name(const_sg_host_t host)
439 {
440   return host->get_cname();
441 }
442
443 void* sg_host_extension_get(const_sg_host_t host, size_t ext)
444 {
445   return host->extension(ext);
446 }
447
448 size_t sg_host_extension_create(void (*deleter)(void*))
449 {
450   return simgrid::s4u::Host::extension_create(deleter);
451 }
452
453 sg_host_t sg_host_by_name(const char* name)
454 {
455   return simgrid::s4u::Host::by_name_or_null(name);
456 }
457
458 // ========= Layering madness ==============*
459
460 // ========== User data Layer ==========
461 void* sg_host_get_data(const_sg_host_t host)
462 {
463   return host->get_data();
464 }
465 void sg_host_set_data(sg_host_t host, void* userdata)
466 {
467   host->set_data(userdata);
468 }
469
470 // ========= Disk related functions ============
471 void sg_host_get_disks(const_sg_host_t host, unsigned int* disk_count, sg_disk_t** disks)
472 {
473   std::vector<sg_disk_t> list = host->get_disks();
474   *disk_count                 = list.size();
475   *disks                      = xbt_new(sg_disk_t, list.size());
476   std::copy(begin(list), end(list), *disks);
477 }
478
479 // =========== user-level functions ===============
480 // ================================================
481 /** @brief Returns the total speed of a host */
482 double sg_host_get_speed(const_sg_host_t host)
483 {
484   return host->get_speed();
485 }
486
487 /** @brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref plugin_host_energy.
488  *
489  * @param  host host to test
490  * @param pstate_index pstate to test
491  * @return Returns the processor speed associated with pstate_index
492  */
493 double sg_host_get_pstate_speed(const_sg_host_t host, unsigned long pstate_index)
494 {
495   return host->get_pstate_speed(pstate_index);
496 }
497
498 /** @ingroup m_host_management
499  * @brief Return the number of cores.
500  *
501  * @param host a host
502  * @return the number of cores
503  */
504 int sg_host_core_count(const_sg_host_t host)
505 {
506   return host->get_core_count();
507 }
508
509 double sg_host_get_available_speed(const_sg_host_t host)
510 {
511   return host->get_available_speed();
512 }
513
514 /** @brief Returns the number of power states for a host.
515  *
516  *  See also @ref plugin_host_energy.
517  */
518 unsigned long sg_host_get_nb_pstates(const_sg_host_t host)
519 {
520   return host->get_pstate_count();
521 }
522
523 /** @brief Gets the pstate at which that host currently runs.
524  *
525  *  See also @ref plugin_host_energy.
526  */
527 unsigned long sg_host_get_pstate(const_sg_host_t host)
528 {
529   return host->get_pstate();
530 }
531 /** @brief Sets the pstate at which that host should run.
532  *
533  *  See also @ref plugin_host_energy.
534  */
535 void sg_host_set_pstate(sg_host_t host, unsigned long pstate)
536 {
537   host->set_pstate(pstate);
538 }
539
540 /** @ingroup m_host_management
541  *
542  * @brief Start the host if it is off
543  *
544  * See also #sg_host_is_on() to test the current state of the host and @ref plugin_host_energy
545  * for more info on DVFS.
546  */
547 void sg_host_turn_on(sg_host_t host)
548 {
549   host->turn_on();
550 }
551
552 /** @ingroup m_host_management
553  *
554  * @brief Stop the host if it is on
555  *
556  * See also #MSG_host_is_on() to test the current state of the host and @ref plugin_host_energy
557  * for more info on DVFS.
558  */
559 void sg_host_turn_off(sg_host_t host)
560 {
561   host->turn_off();
562 }
563
564 /** @ingroup m_host_management
565  * @brief Determine if a host is up and running.
566  *
567  * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_host_energy for
568  * more info on DVFS.
569  *
570  * @param host host to test
571  * @return Returns true if the host is up and running, and false if it's currently down
572  */
573 int sg_host_is_on(const_sg_host_t host)
574 {
575   return host->is_on();
576 }
577
578 /** @brief Get the properties of a host */
579 xbt_dict_t sg_host_get_properties(const_sg_host_t host)
580 {
581   const std::unordered_map<std::string, std::string>* props = host->get_properties();
582   xbt_dict_t as_dict                                        = xbt_dict_new_homogeneous(xbt_free_f);
583
584   if (props == nullptr)
585     return nullptr;
586   for (auto const& elm : *props) {
587     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()));
588   }
589   return as_dict;
590 }
591
592 /** @ingroup m_host_management
593  * @brief Returns the value of a given host property
594  *
595  * @param host a host
596  * @param name a property name
597  * @return value of a property (or nullptr if property not set)
598  */
599 const char* sg_host_get_property_value(const_sg_host_t host, const char* name)
600 {
601   return host->get_property(name);
602 }
603
604 void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
605 {
606   host->set_property(name, value);
607 }
608
609 /**
610  * @brief Find a route between two hosts
611  *
612  * @param from where from
613  * @param to where to
614  * @param links [OUT] where to store the list of links (must exist, cannot be nullptr).
615  */
616 void sg_host_get_route(const_sg_host_t from, const_sg_host_t to, xbt_dynar_t links)
617 {
618   std::vector<simgrid::s4u::Link*> vlinks;
619   from->route_to(to, vlinks, nullptr);
620   for (auto const& link : vlinks)
621     xbt_dynar_push(links, &link);
622 }
623 /**
624  * @brief Find the latency of the route between two hosts
625  *
626  * @param from where from
627  * @param to where to
628  */
629 double sg_host_get_route_latency(const_sg_host_t from, const_sg_host_t to)
630 {
631   std::vector<simgrid::s4u::Link*> vlinks;
632   double res = 0;
633   from->route_to(to, vlinks, &res);
634   return res;
635 }
636 /**
637  * @brief Find the bandwidth of the route between two hosts
638  *
639  * @param from where from
640  * @param to where to
641  */
642 double sg_host_get_route_bandwidth(const_sg_host_t from, const_sg_host_t to)
643 {
644   double min_bandwidth = -1.0;
645
646   std::vector<simgrid::s4u::Link*> vlinks;
647   from->route_to(to, vlinks, nullptr);
648   for (auto const& link : vlinks) {
649     double bandwidth = link->get_bandwidth();
650     if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
651       min_bandwidth = bandwidth;
652   }
653   return min_bandwidth;
654 }
655
656 void sg_host_sendto(sg_host_t from, sg_host_t to, double byte_amount)
657 {
658   simgrid::s4u::Comm::sendto(from, to, byte_amount);
659 }
660
661 /** @brief Displays debugging information about a host */
662 void sg_host_dump(const_sg_host_t host)
663 {
664   XBT_INFO("Displaying host %s", host->get_cname());
665   XBT_INFO("  - speed: %.0f", host->get_speed());
666   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
667   const std::unordered_map<std::string, std::string>* props = host->get_properties();
668
669   if (not props->empty()) {
670     XBT_INFO("  - properties:");
671     for (auto const& elm : *props) {
672       XBT_INFO("    %s->%s", elm.first.c_str(), elm.second.c_str());
673     }
674   }
675 }
676
677 /** @brief Return the list of actors attached to a host.
678  *
679  * @param host a host
680  * @param whereto a dynar in which we should push actors living on that host
681  */
682 void sg_host_get_actor_list(const_sg_host_t host, xbt_dynar_t whereto)
683 {
684   auto const actors = host->get_all_actors();
685   for (auto const& actor : actors)
686     xbt_dynar_push(whereto, &actor);
687 }
688
689 sg_host_t sg_host_self()
690 {
691   return simgrid::s4u::Actor::is_maestro() ? nullptr : simgrid::kernel::actor::ActorImpl::self()->get_host();
692 }
693
694 /* needs to be public and without simcall for exceptions and logging events */
695 const char* sg_host_self_get_name()
696 {
697   const char* res = "";
698   if (not simgrid::s4u::Actor::is_maestro()) {
699     const simgrid::s4u::Host* host = simgrid::kernel::actor::ActorImpl::self()->get_host();
700     if (host != nullptr)
701       res = host->get_cname();
702   }
703   return res;
704 }
705
706 double sg_host_get_load(const_sg_host_t host)
707 {
708   return host->get_load();
709 }