Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / include / simgrid / s4u / Host.hpp
1 /* Copyright (c) 2006-2021. 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 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;
43
44 public:
45   explicit Host(const std::string& name);
46
47 protected:
48   virtual ~Host();
49   void set_netpoint(kernel::routing::NetPoint* netpoint) { pimpl_netpoint_ = netpoint; }
50
51 private:
52   bool currently_destroying_ = false;
53 #endif
54
55 public:
56   /** Called on each newly created host */
57   static xbt::signal<void(Host&)> on_creation;
58   /** Called just before destructing a host */
59   static xbt::signal<void(Host const&)> on_destruction;
60   /** Called when the machine is turned on or off (called AFTER the change) */
61   static xbt::signal<void(Host const&)> on_state_change;
62   /** Called when the speed of the machine is changed (called AFTER the change)
63    * (either because of a pstate switch or because of an external load event coming from the profile) */
64   static xbt::signal<void(Host const&)> on_speed_change;
65
66   virtual void destroy();
67 #ifndef DOXYGEN
68   // No copy/move
69   Host(Host const&) = delete;
70   Host& operator=(Host const&) = delete;
71 #endif
72
73   /** Retrieve a host from its name, or return nullptr */
74   static Host* by_name_or_null(const std::string& name);
75   /** Retrieve a host from its name, or die */
76   static Host* by_name(const std::string& name);
77   /** Retrieves the host on which the running actor is located */
78   static Host* current();
79
80   /** Retrieves the name of that host as a C++ string */
81   xbt::string const& get_name() const { return name_; }
82   /** Retrieves the name of that host as a C string */
83   const char* get_cname() const { return name_.c_str(); }
84
85   kernel::routing::NetPoint* get_netpoint() const { return pimpl_netpoint_; }
86
87   int get_actor_count() const;
88   std::vector<ActorPtr> get_all_actors() const;
89
90   /** Turns that host on if it was previously off
91    *
92    * This call does nothing if the host is already on. If it was off, all actors which were marked 'autorestart' on that
93    * host will be restarted automatically (note that this may differ from the actors that were initially running on the
94    * host).
95    *
96    * All other Host's properties are left unchanged; in particular, the pstate is left unchanged and not reset to its
97    * initial value.
98    */
99   void turn_on();
100   /** Turns that host off. All actors are forcefully stopped. */
101   void turn_off();
102   /** Returns if that host is currently up and running */
103   bool is_on() const;
104
105   const char* get_property(const std::string& key) const;
106   void set_property(const std::string& key, const std::string& value);
107   const std::unordered_map<std::string, std::string>* get_properties() const;
108   void set_properties(const std::unordered_map<std::string, std::string>& properties);
109
110   void set_state_profile(kernel::profile::Profile* p);
111   void set_speed_profile(kernel::profile::Profile* p);
112
113   /** @brief Get the peak computing speed in flops/s at the current pstate, NOT taking the external load into account.
114    *
115    *  The amount of flops per second available for computing depends on several things:
116    *    - The current pstate determines the maximal peak computing speed (use @ref get_pstate_speed() to retrieve the
117    *      computing speed you would get at another pstate)
118    *    - If you declared an external load (with @ref simgrid::surf::Cpu::set_speed_profile()), you must multiply the
119    * result of get_speed() by get_available_speed() to retrieve what a new computation would get.
120    *
121    *  The remaining speed is then shared between the executions located on this host.
122    *  You can retrieve the amount of tasks currently running on this host with @ref get_load().
123    *
124    *  The host may have multiple cores, and your executions may be able to use more than a single core.
125    *
126    *  Finally, executions of priority 2 get twice the amount of flops than executions of priority 1.
127    */
128   double get_speed() const;
129   /** @brief Get the available speed ratio, between 0 and 1.
130    *
131    * This accounts for external load (see @ref simgrid::surf::Cpu::set_speed_profile()).
132    */
133   double get_available_speed() const;
134   /** Returns the number of core of the processor. */
135   int get_core_count() const;
136   /** Returns the current computation load (in flops per second)
137    *
138    * The external load (coming from an availability trace) is not taken in account.
139    * You may also be interested in the load plugin.
140    */
141   double get_load() const;
142
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;
147
148   std::vector<Disk*> get_disks() const;
149   void add_disk(const Disk* disk);
150   void remove_disk(const std::string& disk_name);
151
152   std::vector<const char*> get_attached_storages() const;
153
154   /** Get an associative list [mount point]->[Storage] of all local mount points.
155    *
156    *  This is defined in the platform file, and cannot be modified programmatically (yet).
157    */
158   std::unordered_map<std::string, Storage*> const& get_mounted_storages();
159
160   void route_to(const Host* dest, std::vector<Link*>& links, double* latency) const;
161   void route_to(const Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency) const;
162   /** Do a blocking communication between two arbitrary hosts.
163    *
164    * This starts a blocking communication right away, bypassing the mailbox and actors mechanism.
165    * The calling actor is blocked until the end of the communication; there is really no limit on the hosts involved.
166    * In particular, the actor does not have to be on one of the involved hosts. Enjoy the comfort of the simulator :)
167    */
168   void sendto(Host* dest, double byte_amount);
169
170   /** Do an asynchronous communication between two arbitrary hosts.
171    *
172    * This initializes a communication that completely bypass the mailbox and actors mechanism.
173    * There is really no limit on the hosts involved. In particular, the actor does not have to be on one of the involved
174    * hosts.
175    */
176   ActivityPtr sendto_async(Host* dest, double byte_amount);
177
178 #ifndef DOXYGEN
179   XBT_ATTRIB_DEPRECATED_v330("Please use Host::sendto()") void send_to(Host* dest, double byte_amount)
180   {
181     sendto(dest, byte_amount);
182   }
183 #endif
184
185   NetZone* get_englobing_zone();
186   /** Block the calling actor on an execution located on the called host
187    *
188    * It is not a problem if the actor is not located on the called host.
189    * The actor will not be migrated in this case. Such remote execution are easy in simulation.
190    */
191   void execute(double flops) const;
192   /** Start an asynchronous computation on that host (possibly remote) */
193   ExecPtr exec_async(double flops_amounts) const;
194
195   /** Block the calling actor on an execution located on the called host (with explicit priority) */
196   void execute(double flops, double priority) const;
197
198 private:
199   xbt::string name_{"noname"};
200   std::unordered_map<std::string, Storage*>* mounts_ = nullptr; // caching
201   kernel::routing::NetPoint* pimpl_netpoint_         = nullptr;
202
203 public:
204 #ifndef DOXYGEN
205   /** DO NOT USE DIRECTLY (@todo: these should be protected, once our code is clean) */
206   kernel::resource::Cpu* pimpl_cpu = nullptr;
207   // TODO, this could be a unique_ptr
208   surf::HostImpl* pimpl_ = nullptr;
209 #endif
210 };
211 } // namespace s4u
212 } // namespace simgrid
213
214 #endif /* SIMGRID_S4U_HOST_HPP */