1 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved. */
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. */
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>
16 #include "simgrid/simix.hpp"
17 #include "src/kernel/resource/HostImpl.hpp"
18 #include "src/kernel/resource/StandardLinkImpl.hpp"
19 #include "src/kernel/resource/VirtualMachineImpl.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_host, s4u, "Logging specific to the S4U hosts");
24 XBT_LOG_EXTERNAL_CATEGORY(ker_platform);
28 template class xbt::Extendable<s4u::Host>;
33 xbt::signal<void(Host&)> Host::on_creation;
34 xbt::signal<void(Host const&)> Host::on_destruction;
35 xbt::signal<void(Host const&)> Host::on_onoff;
36 xbt::signal<void(Host const&)> Host::on_speed_change;
37 xbt::signal<void(kernel::resource::CpuAction&, kernel::resource::Action::State)> Host::on_exec_state_change;
40 Host* Host::set_cpu(kernel::resource::CpuImpl* cpu)
47 Host* Host::set_netpoint(kernel::routing::NetPoint* netpoint)
49 pimpl_netpoint_ = netpoint;
55 if (pimpl_netpoint_ != nullptr) // not removed yet by a children class
56 Engine::get_instance()->netpoint_unregister(pimpl_netpoint_);
61 /** @brief Fire the required callbacks and destroy the object
63 * Don't delete directly a host, call h->destroy() instead.
65 * This is cumbersome but this is the simplest solution to ensure that the on_destruction() callback receives a valid
66 * object (because of the destructor order in a class hierarchy).
70 kernel::actor::simcall_answered([this] { this->pimpl_->destroy(); });
73 Host* Host::by_name(const std::string& name)
75 return Engine::get_instance()->host_by_name(name);
77 Host* Host::by_name_or_null(const std::string& name)
79 return Engine::get_instance()->host_by_name_or_null(name);
84 const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
85 xbt_assert(self != nullptr, "Cannot call Host::current() from the maestro context");
86 return self->get_host();
89 std::string const& Host::get_name() const
91 return this->pimpl_->get_name();
94 const char* Host::get_cname() const
96 return this->pimpl_->get_cname();
102 kernel::actor::simcall_answered([this] {
103 this->pimpl_cpu_->turn_on();
104 this->pimpl_->turn_on();
106 on_this_onoff(*this);
111 /** @brief Stop the host if it is on */
112 void Host::turn_off()
115 const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
116 kernel::actor::simcall_answered([this, self] {
117 this->pimpl_cpu_->turn_off();
118 this->pimpl_->turn_off(self);
121 on_this_onoff(*this);
126 bool Host::is_on() const
128 return this->pimpl_cpu_->is_on();
131 unsigned long Host::get_pstate_count() const
133 return this->pimpl_cpu_->get_pstate_count();
137 * @brief Return a copy of the list of actors that are executing on this host.
139 * Daemons and regular actors are all mixed in this list.
141 std::vector<ActorPtr> Host::get_all_actors() const
143 return pimpl_->get_all_actors();
146 /** @brief Returns how many actors (daemonized or not) have been launched on this host */
147 size_t Host::get_actor_count() const
149 return pimpl_->get_actor_count();
153 * @brief Find a route toward another host
155 * @param dest [IN] where to
156 * @param links [OUT] where to store the list of links (must exist, cannot be nullptr).
157 * @param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
158 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
160 * walk through the routing components tree and find a route between hosts
161 * by calling each "get_route" function in each routing component.
163 void Host::route_to(const Host* dest, std::vector<Link*>& links, double* latency) const
165 std::vector<kernel::resource::StandardLinkImpl*> linkImpls;
166 this->route_to(dest, linkImpls, latency);
167 for (auto* l : linkImpls)
168 links.push_back(l->get_iface());
170 std::pair<std::vector<Link*>, double> Host::route_to(const Host* dest) const
172 std::vector<kernel::resource::StandardLinkImpl*> linkImpls;
173 std::vector<Link*> links;
175 this->route_to(dest, linkImpls, &latency);
176 for (auto* l : linkImpls)
177 links.push_back(l->get_iface());
178 return std::make_pair(links, latency);
181 /** @brief Just like Host::routeTo, but filling an array of link implementations */
182 void Host::route_to(const Host* dest, std::vector<kernel::resource::StandardLinkImpl*>& links, double* latency) const
184 kernel::routing::NetZoneImpl::get_global_route(pimpl_netpoint_, dest->get_netpoint(), links, latency);
185 if (XBT_LOG_ISENABLED(ker_platform, xbt_log_priority_debug)) {
186 XBT_CDEBUG(ker_platform, "Route from '%s' to '%s' (latency: %f):", get_cname(), dest->get_cname(),
187 (latency == nullptr ? -1 : *latency));
188 for (auto const* link : links)
189 XBT_CDEBUG(ker_platform, " Link '%s'", link->get_cname());
193 /** @brief Returns the networking zone englobing that host */
194 NetZone* Host::get_englobing_zone() const
196 return pimpl_netpoint_->get_englobing_zone()->get_iface();
199 /** Get the properties assigned to a host */
200 const std::unordered_map<std::string, std::string>* Host::get_properties() const
202 return this->pimpl_->get_properties();
205 /** Retrieve the property value (or nullptr if not set) */
206 const char* Host::get_property(const std::string& key) const
208 return this->pimpl_->get_property(key);
211 Host* Host::set_property(const std::string& key, const std::string& value)
213 kernel::actor::simcall_object_access(pimpl_, [this, &key, &value] { this->pimpl_->set_property(key, value); });
217 Host* Host::set_properties(const std::unordered_map<std::string, std::string>& properties)
219 kernel::actor::simcall_object_access(pimpl_, [this, &properties] { this->pimpl_->set_properties(properties); });
223 int Host::get_concurrency_limit() const
225 return pimpl_cpu_->get_concurrency_limit();
228 Host* Host::set_concurrency_limit(int limit)
230 kernel::actor::simcall_object_access(pimpl_cpu_, [this, limit] { pimpl_cpu_->set_concurrency_limit(limit); });
234 /** Specify a profile turning the host on and off according to an exhaustive list or a stochastic law.
235 * The profile must contain boolean values. */
236 Host* Host::set_state_profile(kernel::profile::Profile* p)
238 kernel::actor::simcall_object_access(pimpl_, [this, p] { pimpl_cpu_->set_state_profile(p); });
241 /** Specify a profile modeling the external load according to an exhaustive list or a stochastic law.
243 * Each event of the profile represent a peak speed change that is due to external load. The values are given as a rate
244 * of the initial value. This means that the actual value is obtained by multiplying the initial value (the peek speed
245 * at this pstate level) by the rate coming from the profile.
247 Host* Host::set_speed_profile(kernel::profile::Profile* p)
249 kernel::actor::simcall_object_access(pimpl_, [this, p] { pimpl_cpu_->set_speed_profile(p); });
253 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
254 double Host::get_pstate_speed(unsigned long pstate_index) const
256 return this->pimpl_cpu_->get_pstate_peak_speed(pstate_index);
259 double Host::get_speed() const
261 return this->pimpl_cpu_->get_speed(1.0);
263 double Host::get_load() const
265 return this->pimpl_cpu_->get_load();
267 double Host::get_available_speed() const
269 return this->pimpl_cpu_->get_speed_ratio();
272 Host* Host::set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
274 kernel::actor::simcall_object_access(pimpl_, [this, policy, &cb] { pimpl_cpu_->set_sharing_policy(policy, cb); });
278 Host::SharingPolicy Host::get_sharing_policy() const
280 return this->pimpl_cpu_->get_sharing_policy();
283 int Host::get_core_count() const
285 return this->pimpl_cpu_->get_core_count();
288 Host* Host::set_core_count(int core_count)
290 kernel::actor::simcall_object_access(pimpl_, [this, core_count] { this->pimpl_cpu_->set_core_count(core_count); });
294 Host* Host::set_pstate_speed(const std::vector<double>& speed_per_state)
296 kernel::actor::simcall_object_access(pimpl_,
297 [this, &speed_per_state] { pimpl_cpu_->set_pstate_speed(speed_per_state); });
301 std::vector<double> Host::convert_pstate_speed_vector(const std::vector<std::string>& speed_per_state)
303 std::vector<double> speed_list;
304 speed_list.reserve(speed_per_state.size());
305 for (const auto& speed_str : speed_per_state) {
307 double speed = xbt_parse_get_speed("", 0, speed_str, "");
308 speed_list.push_back(speed);
309 } catch (const simgrid::ParseError&) {
310 throw std::invalid_argument("Invalid speed value: " + speed_str);
316 Host* Host::set_pstate_speed(const std::vector<std::string>& speed_per_state)
318 set_pstate_speed(Host::convert_pstate_speed_vector(speed_per_state));
322 /** @brief Set the pstate at which the host should run */
323 Host* Host::set_pstate(unsigned long pstate_index)
325 kernel::actor::simcall_object_access(pimpl_, [this, pstate_index] { this->pimpl_cpu_->set_pstate(pstate_index); });
329 /** @brief Retrieve the pstate at which the host is currently running */
330 unsigned long Host::get_pstate() const
332 return this->pimpl_cpu_->get_pstate();
335 Host* Host::set_factor_cb(const std::function<CpuFactorCb>& cb)
337 kernel::actor::simcall_object_access(pimpl_, [this, &cb] { pimpl_cpu_->set_factor_cb(cb); });
341 Host* Host::set_coordinates(const std::string& coords)
343 if (not coords.empty())
344 kernel::actor::simcall_object_access(pimpl_, [this, coords] { this->pimpl_netpoint_->set_coordinates(coords); });
347 std::vector<Disk*> Host::get_disks() const
349 return this->pimpl_->get_disks();
352 Disk* Host::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
354 return kernel::actor::simcall_answered([this, &name, read_bandwidth, write_bandwidth] {
355 auto* disk = pimpl_->create_disk(name, read_bandwidth, write_bandwidth);
356 pimpl_->add_disk(disk);
361 Disk* Host::create_disk(const std::string& name, const std::string& read_bandwidth, const std::string& write_bandwidth)
365 d_read = xbt_parse_get_bandwidth("", 0, read_bandwidth, "");
366 } catch (const simgrid::ParseError&) {
367 throw std::invalid_argument("Impossible to create disk: " + name + ". Invalid read bandwidth: " + read_bandwidth);
371 d_write = xbt_parse_get_bandwidth("", 0, write_bandwidth, "");
372 } catch (const simgrid::ParseError&) {
373 throw std::invalid_argument("Impossible to create disk: " + name + ". Invalid write bandwidth: " + write_bandwidth);
375 return create_disk(name, d_read, d_write);
378 void Host::add_disk(const Disk* disk)
380 kernel::actor::simcall_answered([this, disk] { this->pimpl_->add_disk(disk); });
383 void Host::remove_disk(const std::string& disk_name)
385 kernel::actor::simcall_answered([this, disk_name] { this->pimpl_->remove_disk(disk_name); });
388 VirtualMachine* Host::create_vm(const std::string& name, int core_amount)
390 return kernel::actor::simcall_answered(
391 [this, &name, core_amount] { return this->pimpl_->create_vm(name, core_amount); });
394 VirtualMachine* Host::create_vm(const std::string& name, int core_amount, size_t ramsize)
396 return kernel::actor::simcall_answered(
397 [this, &name, core_amount, ramsize] { return this->pimpl_->create_vm(name, core_amount, ramsize); });
400 VirtualMachine* Host::vm_by_name_or_null(const std::string& name)
402 simgrid::kernel::resource::VirtualMachineImpl* vm = this->pimpl_->get_vm_by_name_or_null(name);
403 return vm ? vm->get_iface() : nullptr;
406 ExecPtr Host::exec_init(double flops) const
408 return this_actor::exec_init(flops);
411 ExecPtr Host::exec_async(double flops) const
413 return this_actor::exec_async(flops);
416 void Host::execute(double flops) const
418 execute(flops, 1.0 /* priority */);
421 void Host::execute(double flops, double priority) const
423 this_actor::exec_init(flops)->set_priority(1 / priority)->start()->wait();
428 kernel::actor::simcall_answered([this]() { this->pimpl_->seal(); });
429 simgrid::s4u::Host::on_creation(*this); // notify the signal
434 } // namespace simgrid
436 /* **************************** Public C interface *************************** */
437 size_t sg_host_count()
439 return simgrid::s4u::Engine::get_instance()->get_host_count();
441 sg_host_t* sg_host_list()
443 const simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
444 size_t host_count = e->get_host_count();
446 xbt_assert(host_count > 0, "There is no host!");
447 std::vector<simgrid::s4u::Host*> hosts = e->get_all_hosts();
449 auto* res = xbt_new(sg_host_t, hosts.size());
450 std::copy(begin(hosts), end(hosts), res);
455 const char* sg_host_get_name(const_sg_host_t host)
457 return host->get_cname();
460 void* sg_host_extension_get(const_sg_host_t host, size_t ext)
462 return host->extension(ext);
465 size_t sg_host_extension_create(void (*deleter)(void*))
467 return simgrid::s4u::Host::extension_create(deleter);
470 sg_host_t sg_host_by_name(const char* name)
472 return simgrid::s4u::Host::by_name_or_null(name);
475 /** @brief Retrieve a VM running on a given host from its name, or return NULL if no VM matches*/
476 sg_vm_t sg_vm_by_name(sg_host_t host, const char* name)
478 return host->vm_by_name_or_null(name);
481 // ========= Layering madness ==============*
483 // ========== User data Layer ==========
484 void* sg_host_get_data(const_sg_host_t host)
486 return host->get_data<void>();
488 void sg_host_set_data(sg_host_t host, void* userdata)
490 host->set_data(userdata);
493 // ========= Disk related functions ============
494 void sg_host_get_disks(const_sg_host_t host, unsigned int* disk_count, sg_disk_t** disks)
496 std::vector<sg_disk_t> list = host->get_disks();
497 *disk_count = list.size();
498 *disks = xbt_new(sg_disk_t, list.size());
499 std::copy(begin(list), end(list), *disks);
502 // =========== user-level functions ===============
503 // ================================================
504 /** @brief Returns the total speed of a host */
505 double sg_host_get_speed(const_sg_host_t host)
507 return host->get_speed();
510 /** @brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref plugin_host_energy.
512 * @param host host to test
513 * @param pstate_index pstate to test
514 * @return Returns the processor speed associated with pstate_index
516 double sg_host_get_pstate_speed(const_sg_host_t host, unsigned long pstate_index)
518 return host->get_pstate_speed(pstate_index);
521 /** @ingroup m_host_management
522 * @brief Return the number of cores.
525 * @return the number of cores
527 int sg_host_core_count(const_sg_host_t host)
529 return host->get_core_count();
532 double sg_host_get_available_speed(const_sg_host_t host)
534 return host->get_available_speed();
537 /** @brief Returns the number of power states for a host.
539 * See also @ref plugin_host_energy.
541 unsigned long sg_host_get_nb_pstates(const_sg_host_t host)
543 return host->get_pstate_count();
546 /** @brief Gets the pstate at which that host currently runs.
548 * See also @ref plugin_host_energy.
550 unsigned long sg_host_get_pstate(const_sg_host_t host)
552 return host->get_pstate();
554 /** @brief Sets the pstate at which that host should run.
556 * See also @ref plugin_host_energy.
558 void sg_host_set_pstate(sg_host_t host, unsigned long pstate)
560 host->set_pstate(pstate);
563 /** @ingroup m_host_management
565 * @brief Start the host if it is off
567 * See also #sg_host_is_on() to test the current state of the host and @ref plugin_host_energy
568 * for more info on DVFS.
570 void sg_host_turn_on(sg_host_t host)
575 /** @ingroup m_host_management
577 * @brief Stop the host if it is on
579 * See also #sg_host_is_on() to test the current state of the host and @ref plugin_host_energy
580 * for more info on DVFS.
582 void sg_host_turn_off(sg_host_t host)
587 /** @ingroup m_host_management
588 * @brief Determine if a host is up and running.
590 * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_host_energy for
593 * @param host host to test
594 * @return Returns true if the host is up and running, and false if it's currently down
596 int sg_host_is_on(const_sg_host_t host)
598 return host->is_on();
601 /** @brief Get the properties of a host */
602 xbt_dict_t sg_host_get_properties(const_sg_host_t host)
604 const std::unordered_map<std::string, std::string>* props = host->get_properties();
605 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
607 if (props == nullptr)
609 for (auto const& [key, value] : *props) {
610 xbt_dict_set(as_dict, key.c_str(), xbt_strdup(value.c_str()));
615 /** @ingroup m_host_management
616 * @brief Returns the value of a given host property
619 * @param name a property name
620 * @return value of a property (or nullptr if property not set)
622 const char* sg_host_get_property_value(const_sg_host_t host, const char* name)
624 return host->get_property(name);
627 void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
629 host->set_property(name, value);
633 * @brief Find a route between two hosts
635 * @param from where from
637 * @param links [OUT] where to store the list of links (must exist, cannot be nullptr).
639 void sg_host_get_route(const_sg_host_t from, const_sg_host_t to, xbt_dynar_t links)
641 std::vector<simgrid::s4u::Link*> vlinks;
642 from->route_to(to, vlinks, nullptr);
643 for (auto const& link : vlinks)
644 xbt_dynar_push(links, &link);
647 * @brief Find the latency of the route between two hosts
649 * @param from where from
652 double sg_host_get_route_latency(const_sg_host_t from, const_sg_host_t to)
654 std::vector<simgrid::s4u::Link*> vlinks;
656 from->route_to(to, vlinks, &res);
660 * @brief Find the bandwidth of the route between two hosts
662 * @param from where from
665 double sg_host_get_route_bandwidth(const_sg_host_t from, const_sg_host_t to)
667 double min_bandwidth = -1.0;
669 std::vector<simgrid::s4u::Link*> vlinks;
670 from->route_to(to, vlinks, nullptr);
671 for (auto const& link : vlinks) {
672 double bandwidth = link->get_bandwidth();
673 if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
674 min_bandwidth = bandwidth;
676 return min_bandwidth;
679 void sg_host_sendto(sg_host_t from, sg_host_t to, double byte_amount)
681 simgrid::s4u::Comm::sendto(from, to, byte_amount);
684 /** @brief Return the list of actors attached to a host.
687 * @param whereto a dynar in which we should push actors living on that host
689 void sg_host_get_actor_list(const_sg_host_t host, xbt_dynar_t whereto)
691 auto const actors = host->get_all_actors();
692 for (auto const& actor : actors)
693 xbt_dynar_push(whereto, &actor);
696 sg_host_t sg_host_self()
698 return simgrid::s4u::Actor::is_maestro() ? nullptr : simgrid::kernel::actor::ActorImpl::self()->get_host();
701 /* needs to be public and without simcall for exceptions and logging events */
702 const char* sg_host_self_get_name()
704 const char* res = "";
705 if (not simgrid::s4u::Actor::is_maestro()) {
706 const simgrid::s4u::Host* host = simgrid::kernel::actor::ActorImpl::self()->get_host();
708 res = host->get_cname();
713 double sg_host_get_load(const_sg_host_t host)
715 return host->get_load();