Logo AND Algorithmique Numérique Distribuée

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