Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make member variables "private" (Sonar).
[simgrid.git] / include / simgrid / s4u / Host.hpp
1 /* Copyright (c) 2006-2023. 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 #ifndef SIMGRID_S4U_HOST_HPP
7 #define SIMGRID_S4U_HOST_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/kernel/resource/Action.hpp>
11 #include <xbt/Extendable.hpp>
12 #include <xbt/signal.hpp>
13
14 #include <map>
15 #include <unordered_map>
16
17 namespace simgrid {
18
19 extern template class XBT_PUBLIC xbt::Extendable<s4u::Host>;
20
21 namespace s4u {
22 /** @ingroup s4u_api
23  *
24  * @tableofcontents
25  *
26  * Some physical resource with computing and networking capabilities on which Actors execute.
27  *
28  * @beginrst
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.
32  *
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()`
36  * @endrst
37  */
38 class XBT_PUBLIC Host : public xbt::Extendable<Host> {
39 #ifndef DOXYGEN
40   friend kernel::resource::VMModel;            // Use the pimpl_cpu to compute the VM sharing
41   friend kernel::resource::VirtualMachineImpl; // creates the the pimpl_cpu
42   friend kernel::routing::NetZoneImpl;
43   friend kernel::resource::HostImpl; // call destructor from private implementation
44
45   // The private implementation, that never changes
46   kernel::resource::HostImpl* const pimpl_;
47
48   kernel::resource::CpuImpl* pimpl_cpu_      = nullptr;
49   kernel::routing::NetPoint* pimpl_netpoint_ = nullptr;
50 #ifndef DOXYGEN
51   friend kernel::resource::CpuAction; // signal exec_state_changed
52 #endif
53
54   static xbt::signal<void(Host&)> on_creation;
55   static xbt::signal<void(Host const&)> on_destruction;
56   xbt::signal<void(Host const&)> on_this_destruction;
57   static xbt::signal<void(kernel::resource::CpuAction&, kernel::resource::Action::State)> on_exec_state_change;
58
59 public:
60   explicit Host(kernel::resource::HostImpl* pimpl) : pimpl_(pimpl) {}
61
62 protected:
63   virtual ~Host(); // Call destroy() instead of manually deleting it.
64   Host* set_netpoint(kernel::routing::NetPoint* netpoint);
65
66 public:
67   static xbt::signal<void(Host const&)> on_speed_change;
68   xbt::signal<void(Host const&)> on_this_speed_change;
69   static xbt::signal<void(Host const&)> on_onoff;
70   xbt::signal<void(Host const&)> on_this_onoff;
71
72 #endif
73   /** \static Add a callback fired on each newly created host */
74   static void on_creation_cb(const std::function<void(Host&)>& cb) { on_creation.connect(cb); }
75   /** \static Add a callback fired when any machine is turned on or off (called AFTER the change) */
76   static void on_onoff_cb(const std::function<void(Host const&)>& cb)
77   {
78     on_onoff.connect(cb);
79   }
80   XBT_ATTRIB_DEPRECATED_v338("Please use on_onoff_cb() instead") static void on_state_change_cb(
81       const std::function<void(Host const&)>& cb)
82   {
83     on_onoff.connect(cb);
84   }
85   /** Add a callback fired when this specific machine is turned on or off (called AFTER the change) */
86   void on_this_onoff_cb(const std::function<void(Host const&)>& cb)
87   {
88     on_this_onoff.connect(cb);
89   }
90   /** \static Add a callback fired when the speed of any machine is changed (called AFTER the change)
91    * (either because of a pstate switch or because of an external load event coming from the profile) */
92   static void on_speed_change_cb(const std::function<void(Host const&)>& cb) { on_speed_change.connect(cb); }
93   /** Add a callback fired when the speed of this specific machine is changed (called AFTER the change)
94    * (either because of a pstate switch or because of an external load event coming from the profile) */
95   void on_this_speed_change_cb(const std::function<void(Host const&)>& cb)
96   {
97     on_this_speed_change.connect(cb);
98   }
99   /** \static Add a callback fired just before destructing any host */
100   static void on_destruction_cb(const std::function<void(Host const&)>& cb) { on_destruction.connect(cb); }
101   /** Add a callback fired just before destructing this specific host */
102   void on_this_destruction_cb(const std::function<void(Host const&)>& cb)
103   {
104     on_this_destruction.connect(cb);
105   }
106   /** \static Add a callback fired when the state of any exec activity changes */
107   static void on_exec_state_change_cb(
108       const std::function<void(kernel::resource::CpuAction&, kernel::resource::Action::State previous)>& cb)
109   {
110     on_exec_state_change.connect(cb);
111   }
112
113   virtual void destroy();
114 #ifndef DOXYGEN
115   // No copy/move
116   Host(Host const&) = delete;
117   Host& operator=(Host const&) = delete;
118 #endif
119
120   /** \static Retrieve a host from its name, or return nullptr */
121   static Host* by_name_or_null(const std::string& name);
122   /** \static Retrieve a host from its name, or die */
123   static Host* by_name(const std::string& name);
124   /** \static Retrieves the host on which the running actor is located */
125   static Host* current();
126
127   /** Retrieves the name of that host as a C++ string */
128   std::string const& get_name() const;
129   /** Retrieves the name of that host as a C string */
130   const char* get_cname() const;
131
132   Host* set_cpu(kernel::resource::CpuImpl* cpu);
133   kernel::resource::CpuImpl* get_cpu() const { return pimpl_cpu_; }
134   kernel::routing::NetPoint* get_netpoint() const { return pimpl_netpoint_; }
135   /**
136    * @brief Callback to set CPU factor
137    *
138    * This callback offers a flexible way to create variability in CPU executions
139    *
140    * @param flops Execution size in flops
141    * @return Multiply factor
142    */
143   using CpuFactorCb = double(double flops);
144   /**
145    * @brief Configure the factor callback to the CPU associated to this host
146    */
147   Host* set_factor_cb(const std::function<CpuFactorCb>& cb);
148
149   size_t get_actor_count() const;
150   std::vector<ActorPtr> get_all_actors() const;
151
152   /** Turns that host on if it was previously off
153    *
154    * This call does nothing if the host is already on. If it was off, all actors which were marked 'autorestart' on that
155    * host will be restarted automatically (note that this may differ from the actors that were initially running on the
156    * host).
157    *
158    * All other Host's properties are left unchanged; in particular, the pstate is left unchanged and not reset to its
159    * initial value.
160    */
161   void turn_on();
162   /** Turns that host off. All actors are forcefully stopped. */
163   void turn_off();
164   /** Returns if that host is currently up and running */
165   bool is_on() const;
166
167   const char* get_property(const std::string& key) const;
168   Host* set_property(const std::string& key, const std::string& value);
169   const std::unordered_map<std::string, std::string>* get_properties() const;
170   Host* set_properties(const std::unordered_map<std::string, std::string>& properties);
171
172   Host* set_state_profile(kernel::profile::Profile* p);
173   Host* set_speed_profile(kernel::profile::Profile* p);
174
175   /**
176    * @brief Set the max amount of executions that can take place on this host at the same time
177    *
178    * Use -1 to set no limit.
179    */
180   Host* set_concurrency_limit(int limit);
181   int get_concurrency_limit() const;
182
183   /** \static @brief Convert the CPU's speed from string to double */
184   static std::vector<double> convert_pstate_speed_vector(const std::vector<std::string>& speed_per_state);
185   /**
186    * @brief Set the CPU's speed
187    *
188    * @param speed_per_state list of powers for this processor (default power is at index 0)
189    */
190   Host* set_pstate_speed(const std::vector<double>& speed_per_state);
191   /**
192    * @brief Set the CPU's speed (string version)
193    *
194    * @throw std::invalid_argument if speed format is incorrect.
195    */
196   Host* set_pstate_speed(const std::vector<std::string>& speed_per_state);
197
198   /** @brief Get the peak computing speed in flops/s at the current pstate, NOT taking the external load into account.
199    *
200    *  The amount of flops per second available for computing depends on several things:
201    *    - The current pstate determines the maximal peak computing speed (use
202    *      @verbatim embed:rst:inline :cpp:func:`get_pstate_speed() <simgrid::s4u::Host::get_pstate_speed>` @endverbatim
203    *      to retrieve the computing speed you would get at another pstate)
204    *    - If you declared an external load (with
205    *      @verbatim embed:rst:inline :cpp:func:`set_speed_profile() <simgrid::s4u::Host::set_speed_profile>` @endverbatim ),
206    *      you must multiply the result of
207    *      @verbatim embed:rst:inline :cpp:func:`get_speed() <simgrid::s4u::Host::get_speed>` @endverbatim by
208    *      @verbatim embed:rst:inline :cpp:func:`get_available_speed() <simgrid::s4u::Host::get_available_speed>` @endverbatim
209    *      to retrieve what a new computation would get.
210    *
211    *  The remaining speed is then shared between the executions located on this host.
212    *  You can retrieve the amount of tasks currently running on this host with
213    *  @verbatim embed:rst:inline :cpp:func:`get_load() <simgrid::s4u::Host::get_load>` @endverbatim .
214    *
215    *  The host may have multiple cores, and your executions may be able to use more than a single core.
216    *
217    *  Finally, executions of priority 2 get twice the amount of flops than executions of priority 1.
218    */
219   double get_speed() const;
220   /** @brief Get the available speed ratio, between 0 and 1.
221    *
222    * This accounts for external load (see
223    * @verbatim embed:rst:inline :cpp:func:`set_speed_profile() <simgrid::s4u::Host::set_speed_profile>` @endverbatim ).
224    */
225   double get_available_speed() const;
226
227   /** Returns the number of core of the processor. */
228   int get_core_count() const;
229   Host* set_core_count(int core_count);
230
231   enum class SharingPolicy { NONLINEAR = 1, LINEAR = 0 };
232   /**
233    * @brief Describes how the CPU is shared between concurrent tasks
234    *
235    * Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
236    *
237    * @param policy Sharing policy
238    * @param cb Callback for NONLINEAR policies
239    */
240   Host* set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
241   SharingPolicy get_sharing_policy() const;
242
243   /** Returns the current computation load (in flops per second)
244    *
245    * The external load (coming from an availability trace) is not taken in account.
246    * You may also be interested in the load plugin.
247    */
248   double get_load() const;
249
250   unsigned long get_pstate_count() const;
251   unsigned long get_pstate() const;
252   double get_pstate_speed(unsigned long pstate_index) const;
253   Host* set_pstate(unsigned long pstate_index);
254   Host* set_coordinates(const std::string& coords);
255
256   std::vector<Disk*> get_disks() const;
257   /**
258    * @brief Create and add disk in the host
259    *
260    * @param name Disk name
261    * @param read_bandwidth Reading speed of the disk
262    * @param write_bandwidth Writing speed of the disk
263    */
264   Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
265   /**
266    * @brief Human-friendly version of create_disk function.
267    *
268    * @throw std::invalid_argument if read/write speeds are incorrect
269    */
270   Disk* create_disk(const std::string& name, const std::string& read_bandwidth, const std::string& write_bandwidth);
271   void add_disk(const Disk* disk);
272   void remove_disk(const std::string& disk_name);
273
274   VirtualMachine* create_vm(const std::string& name, int core_amount);
275   VirtualMachine* create_vm(const std::string& name, int core_amount, size_t ramsize);
276   /** Retrieve a VM running on this host from its name, or return nullptr */
277   VirtualMachine* vm_by_name_or_null(const std::string& name);
278
279   void route_to(const Host* dest, std::vector<Link*>& links, double* latency) const;
280   void route_to(const Host* dest, std::vector<kernel::resource::StandardLinkImpl*>& links, double* latency) const;
281   std::pair<std::vector<Link*>, double> route_to(const Host* dest) const;
282
283   /**
284    * @brief Seal this host
285    * No more configuration is allowed after the seal
286    */
287   Host* seal();
288
289   NetZone* get_englobing_zone() const;
290   /** Block the calling actor on an execution located on the called host
291    *
292    * It is not a problem if the actor is not located on the called host.
293    * The actor will not be migrated in this case. Such remote execution are easy in simulation.
294    */
295   void execute(double flops) const;
296   /** Start an asynchronous computation on that host (possibly remote) */
297   ExecPtr exec_init(double flops_amounts) const;
298   ExecPtr exec_async(double flops_amounts) const;
299
300   /** Block the calling actor on an execution located on the called host (with explicit priority) */
301   void execute(double flops, double priority) const;
302   kernel::resource::HostImpl* get_impl() const { return pimpl_; }
303 };
304 } // namespace s4u
305 } // namespace simgrid
306
307 #endif /* SIMGRID_S4U_HOST_HPP */