1 /* Copyright (c) 2006-2021. 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 #ifndef SIMGRID_S4U_HOST_HPP
7 #define SIMGRID_S4U_HOST_HPP
9 #include <simgrid/forward.h>
10 #include <xbt/Extendable.hpp>
11 #include <xbt/signal.hpp>
12 #include <xbt/string.hpp>
15 #include <unordered_map>
19 extern template class XBT_PUBLIC xbt::Extendable<s4u::Host>;
26 * Some physical resource with computing and networking capabilities on which Actors execute.
29 * All hosts are automatically created during the call of the method
30 * :cpp:func:`simgrid::s4u::Engine::load_platform()`.
31 * You cannot create a host yourself.
33 * You can retrieve a particular host using :cpp:func:`simgrid::s4u::Host::by_name()`
34 * and actors can retrieve the host on which they run using :cpp:func:`simgrid::s4u::Host::current()` or
35 * :cpp:func:`simgrid::s4u::this_actor::get_host()`
38 class XBT_PUBLIC Host : public xbt::Extendable<Host> {
40 friend vm::VMModel; // Use the pimpl_cpu to compute the VM sharing
41 friend vm::VirtualMachineImpl; // creates the the pimpl_cpu
42 friend kernel::routing::NetZoneImpl;
45 explicit Host(const std::string& name);
48 virtual ~Host(); // Call destroy() instead of manually deleting it.
49 Host* set_netpoint(kernel::routing::NetPoint* netpoint);
53 /** Called on each newly created host */
54 static xbt::signal<void(Host&)> on_creation;
55 /** Called just before destructing a host */
56 static xbt::signal<void(Host const&)> on_destruction;
57 /** Called when the machine is turned on or off (called AFTER the change) */
58 static xbt::signal<void(Host const&)> on_state_change;
59 /** Called when the speed of the machine is changed (called AFTER the change)
60 * (either because of a pstate switch or because of an external load event coming from the profile) */
61 static xbt::signal<void(Host const&)> on_speed_change;
63 virtual void destroy();
66 Host(Host const&) = delete;
67 Host& operator=(Host const&) = delete;
70 /** Retrieve a host from its name, or return nullptr */
71 static Host* by_name_or_null(const std::string& name);
72 /** Retrieve a host from its name, or die */
73 static Host* by_name(const std::string& name);
74 /** Retrieves the host on which the running actor is located */
75 static Host* current();
77 /** Retrieves the name of that host as a C++ string */
78 xbt::string const& get_name() const { return name_; }
79 /** Retrieves the name of that host as a C string */
80 const char* get_cname() const { return name_.c_str(); }
82 kernel::routing::NetPoint* get_netpoint() const { return pimpl_netpoint_; }
84 int get_actor_count() const;
85 std::vector<ActorPtr> get_all_actors() const;
87 /** Turns that host on if it was previously off
89 * This call does nothing if the host is already on. If it was off, all actors which were marked 'autorestart' on that
90 * host will be restarted automatically (note that this may differ from the actors that were initially running on the
93 * All other Host's properties are left unchanged; in particular, the pstate is left unchanged and not reset to its
97 /** Turns that host off. All actors are forcefully stopped. */
99 /** Returns if that host is currently up and running */
102 const char* get_property(const std::string& key) const;
103 Host* set_property(const std::string& key, const std::string& value);
104 const std::unordered_map<std::string, std::string>* get_properties() const;
105 Host* set_properties(const std::unordered_map<std::string, std::string>& properties);
107 void set_state_profile(kernel::profile::Profile* p);
108 void set_speed_profile(kernel::profile::Profile* p);
110 /** @brief Get the peak computing speed in flops/s at the current pstate, NOT taking the external load into account.
112 * The amount of flops per second available for computing depends on several things:
113 * - The current pstate determines the maximal peak computing speed (use @ref get_pstate_speed() to retrieve the
114 * computing speed you would get at another pstate)
115 * - If you declared an external load (with @ref simgrid::surf::Cpu::set_speed_profile()), you must multiply the
116 * result of get_speed() by get_available_speed() to retrieve what a new computation would get.
118 * The remaining speed is then shared between the executions located on this host.
119 * You can retrieve the amount of tasks currently running on this host with @ref get_load().
121 * The host may have multiple cores, and your executions may be able to use more than a single core.
123 * Finally, executions of priority 2 get twice the amount of flops than executions of priority 1.
125 double get_speed() const;
126 /** @brief Get the available speed ratio, between 0 and 1.
128 * This accounts for external load (see @ref simgrid::surf::Cpu::set_speed_profile()).
130 double get_available_speed() const;
132 /** Returns the number of core of the processor. */
133 int get_core_count() const;
134 Host* set_core_count(int core_count);
136 /** Returns the current computation load (in flops per second)
138 * The external load (coming from an availability trace) is not taken in account.
139 * You may also be interested in the load plugin.
141 double get_load() const;
143 double get_pstate_speed(int pstate_index) const;
144 int get_pstate_count() const;
145 void set_pstate(int pstate_index);
146 int get_pstate() const;
148 std::vector<Disk*> get_disks() const;
149 Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
150 void add_disk(const Disk* disk);
151 void remove_disk(const std::string& disk_name);
153 void route_to(const Host* dest, std::vector<Link*>& links, double* latency) const;
154 void route_to(const Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency) const;
157 XBT_ATTRIB_DEPRECATED_v331("Please use Comm::sendto()") void sendto(Host* dest, double byte_amount);
159 XBT_ATTRIB_DEPRECATED_v331("Please use Comm::sendto_async()") CommPtr sendto_async(Host* dest, double byte_amount);
161 XBT_ATTRIB_DEPRECATED_v330("Please use Host::sendto()") void send_to(Host* dest, double byte_amount);
164 NetZone* get_englobing_zone();
165 /** Block the calling actor on an execution located on the called host
167 * It is not a problem if the actor is not located on the called host.
168 * The actor will not be migrated in this case. Such remote execution are easy in simulation.
170 void execute(double flops) const;
171 /** Start an asynchronous computation on that host (possibly remote) */
172 ExecPtr exec_init(double flops_amounts) const;
173 ExecPtr exec_async(double flops_amounts) const;
175 /** Block the calling actor on an execution located on the called host (with explicit priority) */
176 void execute(double flops, double priority) const;
179 xbt::string name_{"noname"};
180 kernel::routing::NetPoint* pimpl_netpoint_ = nullptr;
184 /** DO NOT USE DIRECTLY (@todo: these should be protected, once our code is clean) */
185 kernel::resource::Cpu* pimpl_cpu = nullptr;
186 // TODO, this could be a unique_ptr
187 surf::HostImpl* pimpl_ = nullptr;
191 } // namespace simgrid
193 #endif /* SIMGRID_S4U_HOST_HPP */