Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / Host.hpp
1 /* Copyright (c) 2006-2019. 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 namespace xbt {
20 extern template class XBT_PUBLIC Extendable<simgrid::s4u::Host>;
21 }
22 namespace s4u {
23
24 /** @ingroup s4u_api
25  *
26  * @tableofcontents
27  *
28  * Some physical resource with computing and networking capabilities on which Actors execute.
29  *
30  * All hosts are automatically created during the call of the method
31  * @ref simgrid::s4u::Engine::load_platform().
32  * You cannot create a host yourself.
33  *
34  * You can retrieve a particular host using @ref simgrid::s4u::Host::by_name()
35  * and actors can retrieve the host on which they run using @ref simgrid::s4u::Host::current() or
36  * @ref simgrid::s4u::this_actor::get_host().
37  */
38 class XBT_PUBLIC Host : public simgrid::xbt::Extendable<Host> {
39   friend simgrid::vm::VMModel;            // Use the pimpl_cpu to compute the VM sharing
40   friend simgrid::vm::VirtualMachineImpl; // creates the the pimpl_cpu
41
42 public:
43   explicit Host(std::string name);
44
45   /** Host destruction logic */
46 protected:
47   virtual ~Host();
48
49 private:
50   bool currentlyDestroying_ = false;
51
52 public:
53   /*** Called on each newly created host */
54   static simgrid::xbt::signal<void(Host&)> on_creation;
55   /*** Called just before destructing a host */
56   static simgrid::xbt::signal<void(Host&)> on_destruction;
57   /*** Called when the machine is turned on or off (called AFTER the change) */
58   static simgrid::xbt::signal<void(Host&)> 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 simgrid::xbt::signal<void(Host&)> on_speed_change;
62
63   virtual void destroy();
64   // No copy/move
65   Host(Host const&) = delete;
66   Host& operator=(Host const&) = delete;
67
68   /** Retrieve a host from its name, or return nullptr */
69   static Host* by_name_or_null(std::string name);
70   /** Retrieve a host from its name, or die */
71   static s4u::Host* by_name(std::string name);
72   /** Retrieves the host on which the running actor is located */
73   static s4u::Host* current();
74
75   /** Retrieves the name of that host as a C++ string */
76   simgrid::xbt::string const& get_name() const { return name_; }
77   /** Retrieves the name of that host as a C string */
78   const char* get_cname() const { return name_.c_str(); }
79
80   int get_actor_count();
81   std::vector<ActorPtr> get_all_actors();
82
83   /** Turns that host on if it was previously off
84    *
85    * This call does nothing if the host is already on. If it was off, all actors which were
86    * marked 'autorestart' on that host will be restarted automatically (note that this may differ
87    * from the actors that were initially running on the host).
88    *
89    * All other Host's properties are left unchanged; in particular, the pstate is left
90    * unchanged and not reset to its initial value.
91    */
92   void turn_on();
93   /** Turns that host off. All actors are forcefully stopped. */
94   void turn_off();
95   /** Returns if that host is currently up and running */
96   bool is_on() const;
97   /** Returns if that host is currently down and offline */
98   bool is_off() const { return not is_on(); }
99
100   const char* get_property(std::string key) const;
101   void set_property(std::string key, std::string value);
102   std::unordered_map<std::string, std::string>* get_properties();
103
104   void set_state_profile(kernel::profile::Profile* p);
105   void set_speed_profile(kernel::profile::Profile* p);
106
107 #ifndef DOXYGEN
108   /** @deprecated See Host::get_properties() */
109   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_properties()") std::map<std::string, std::string>* getProperties()
110   {
111     std::map<std::string, std::string>* res             = new std::map<std::string, std::string>();
112     std::unordered_map<std::string, std::string>* props = get_properties();
113     for (auto const& kv : *props)
114       res->insert(kv);
115     return res;
116   }
117 #endif
118
119   double get_speed() const;
120   double get_available_speed() const;
121   int get_core_count() const;
122   double get_load() const;
123
124   double get_pstate_speed(int pstate_index) const;
125   int get_pstate_count() const;
126   void set_pstate(int pstate_index);
127   int get_pstate() const;
128
129 #ifndef DOXYGEN
130   /** @deprecated See Host::get_speed() */
131   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_speed() instead.") double getSpeed() { return get_speed(); }
132   /** @deprecated See Host::get_pstate_speed() */
133   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_pstate_speed() instead.") double getPstateSpeed(int pstate_index)
134   {
135     return get_pstate_speed(pstate_index);
136   }
137
138   std::vector<const char*> get_attached_storages() const;
139   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_attached_storages() instead.") void getAttachedStorages(
140       std::vector<const char*>* storages);
141 #endif
142
143   /** Get an associative list [mount point]->[Storage] of all local mount points.
144    *
145    *  This is defined in the platform file, and cannot be modified programatically (yet).
146    */
147   std::unordered_map<std::string, Storage*> const& get_mounted_storages();
148 #ifndef DOXYGEN
149   /** @deprecated See Host::get_mounted_storages() */
150   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_mounted_storages() instead.") std::unordered_map<std::string, Storage*> const& getMountedStorages()
151   {
152     return get_mounted_storages();
153   }
154 #endif
155
156   void route_to(Host* dest, std::vector<Link*>& links, double* latency);
157   void route_to(Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency);
158
159   /** Block the calling actor on an execution located on the called host
160    *
161    * It is not a problem if the actor is not located on the called host.
162    * The actor will not be migrated in this case. Such remote execution are easy in simulation.
163    */
164   void execute(double flops);
165   /** Start an asynchronous computation on that host (possibly remote) */
166   ExecPtr exec_async(double flops_amounts);
167
168   /** Block the calling actor on an execution located on the called host (with explicit priority) */
169   void execute(double flops, double priority);
170
171   // Deprecated functions
172 #ifndef DOXYGEN
173   /** @deprecated See Host::get_name() */
174   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_name()") simgrid::xbt::string const& getName() const
175   {
176     return name_;
177   }
178   /** @deprecated See Host::get_cname() */
179   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_cname()") const char* getCname() const { return name_.c_str(); }
180   /** @deprecated See Host::get_all_actors() */
181   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_all_actors()") void actorList(std::vector<ActorPtr>* whereto);
182   /** @deprecated See Host::get_all_actors() */
183   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_all_actors()") void getProcesses(std::vector<ActorPtr>* list);
184   /** @deprecated See Host::turn_on() */
185   XBT_ATTRIB_DEPRECATED_v323("Please use Host::turn_on()") void turnOn() { turn_on(); }
186   /** @deprecated See Host::turn_off() */
187   XBT_ATTRIB_DEPRECATED_v323("Please use Host::turn_off()") void turnOff() { turn_off(); }
188   /** @deprecated See Host::is_on() */
189   XBT_ATTRIB_DEPRECATED_v323("Please use Host::is_on()") bool isOn() { return is_on(); }
190   /** @deprecated See Host::is_off() */
191   XBT_ATTRIB_DEPRECATED_v323("Please use Host::is_off()") bool isOff() { return is_off(); }
192   /** @deprecated See Host::get_property() */
193   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_property()") const char* getProperty(const char* key)
194   {
195     return get_property(key);
196   }
197   /** @deprecated See Host::set_property() */
198   XBT_ATTRIB_DEPRECATED_v323("Please use Host::set_property()") void setProperty(std::string key, std::string value)
199   {
200     set_property(key, value);
201   }
202   /** @deprecated See Host::set_pstate() */
203   XBT_ATTRIB_DEPRECATED_v323("Please use Host::set_pstate()") void setPstate(int idx) { set_pstate(idx); }
204   /** @deprecated See Host::get_pstate() */
205   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_pstate()") int getPstate() { return get_pstate(); }
206   /** @deprecated See Host::route_to() */
207   XBT_ATTRIB_DEPRECATED_v323("Please use Host::route_to()") void routeTo(Host* dest, std::vector<Link*>& links,
208                                                                          double* latency)
209   {
210     route_to(dest, links, latency);
211   }
212   /** @deprecated See Host::route_to() */
213   XBT_ATTRIB_DEPRECATED_v323("Please use Host::route_to()") void routeTo(
214       Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency)
215   {
216     route_to(dest, links, latency);
217   }
218   /** @deprecated See Host::get_core_count() */
219   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_core_count()") int getCoreCount() { return get_core_count(); }
220   /** @deprecated See Host::get_pstate_count() */
221   XBT_ATTRIB_DEPRECATED_v323("Please use Host::get_pstate_count()") int getPstatesCount() const
222   {
223     return get_pstate_count();
224   }
225 #endif /* !DOXYGEN */
226
227 private:
228   simgrid::xbt::string name_ {"noname"};
229   std::unordered_map<std::string, Storage*>* mounts_ = nullptr; // caching
230
231 public:
232 #ifndef DOXYGEN
233   /** DO NOT USE DIRECTLY (@todo: these should be protected, once our code is clean) */
234   surf::Cpu* pimpl_cpu = nullptr;
235   // TODO, this could be a unique_ptr
236   surf::HostImpl* pimpl_ = nullptr;
237   /** DO NOT USE DIRECTLY (@todo: these should be protected, once our code is clean) */
238   kernel::routing::NetPoint* pimpl_netpoint = nullptr;
239 #endif
240 };
241 }
242 } // namespace simgrid::s4u
243
244 extern int USER_HOST_LEVEL;
245
246 #endif /* SIMGRID_S4U_HOST_HPP */