Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid unsafe things
[simgrid.git] / src / msg / msg_host.cpp
1 /* Copyright (c) 2004-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/s4u/host.hpp"
8 #include "src/msg/msg_private.h"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg);
11
12 simgrid::xbt::Extension<simgrid::s4u::Host, simgrid::MsgHostExt> simgrid::MsgHostExt::EXTENSION_ID;
13
14 int sg_storage_max_file_descriptors = 1024;
15
16 /** @addtogroup m_host_management
17  * (#msg_host_t) and the functions for managing it.
18  *  
19  *  A <em>location</em> (or <em>host</em>) is any possible place where  a process may run. Thus it may be represented
20  *  as a <em>physical resource with computing capabilities</em>, some <em>mailboxes</em> to enable running process to
21  *  communicate with remote ones, and some <em>private data</em> that can be only accessed by local process.
22  *  \see msg_host_t
23  */
24
25 /********************************* Host **************************************/
26 /** \ingroup m_host_management
27  * \brief Finds a msg_host_t using its name.
28  *
29  * This is a name directory service
30  * \param name the name of an host.
31  * \return the corresponding host
32  */
33 msg_host_t MSG_host_by_name(const char *name)
34 {
35   return simgrid::s4u::Host::by_name_or_null(name);
36 }
37
38 /** \ingroup m_host_management
39  *
40  * \brief Set the user data of a #msg_host_t.
41  *
42  * This functions attach \a data to \a host if it is possible.
43  */
44 msg_error_t MSG_host_set_data(msg_host_t host, void *data) {
45   sg_host_user_set(host, data);
46   return MSG_OK;
47 }
48
49 /** \ingroup m_host_management
50  *
51  * \brief Return the user data of a #msg_host_t.
52  *
53  * This functions returns the user data associated to \a host if it is possible.
54  */
55 void *MSG_host_get_data(msg_host_t host) {
56   return sg_host_user(host);
57 }
58
59 /** \ingroup m_host_management
60  *
61  * \brief Return the location on which the current process is executed.
62  */
63 msg_host_t MSG_host_self()
64 {
65   return MSG_process_get_host(nullptr);
66 }
67
68 /** \ingroup m_host_management
69  *
70  * \brief Start the host if it is off
71  *
72  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref SURF_plugin_energy
73  * for more info on DVFS.
74  */
75 void MSG_host_on(msg_host_t host)
76 {
77   host->turnOn();
78 }
79
80 /** \ingroup m_host_management
81  *
82  * \brief Stop the host if it is on
83  *
84  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref SURF_plugin_energy
85  * for more info on DVFS.
86  */
87 void MSG_host_off(msg_host_t host)
88 {
89   host->turnOff();
90 }
91
92 /** \ingroup m_host_management
93  * \brief Return the current number MSG hosts.
94  */
95 int MSG_get_host_number()
96 {
97   return sg_host_count();
98 }
99
100 /** \ingroup m_host_management
101  * \brief Return a dynar containing all the hosts declared at a given point of time
102  * \remark The host order in the returned array is generally different from the host creation/declaration order in the
103  *         XML platform (we use a hash table internally)
104  */
105 xbt_dynar_t MSG_hosts_as_dynar() {
106   return sg_hosts_as_dynar();
107 }
108
109 /** \ingroup m_host_management
110  * \brief Return the speed of the processor (in flop/s), regardless of the current load on the machine.
111  */
112 double MSG_host_get_speed(msg_host_t host) {
113   return host->speed();
114 }
115
116 /** \ingroup m_host_management
117  * \brief Return the speed of the processor (in flop/s), regardless of the current load on the machine.
118  * Deprecated: use MSG_host_get_speed
119  */
120 double MSG_get_host_speed(msg_host_t host) {
121   XBT_WARN("MSG_get_host_speed is deprecated: use MSG_host_get_speed");
122   return MSG_host_get_speed(host);
123 }
124
125 /** \ingroup m_host_management
126  * \brief Return the number of cores.
127  *
128  * \param host a host
129  * \return the number of cores
130  */
131 int MSG_host_get_core_number(msg_host_t host) {
132   return host->coreCount();
133 }
134
135 /** \ingroup m_host_management
136  * \brief Return the list of processes attached to an host.
137  *
138  * \param host a host
139  * \return a swag with the attached processes
140  */
141 xbt_swag_t MSG_host_get_process_list(msg_host_t host)
142 {
143   xbt_assert((host != nullptr), "Invalid parameters");
144   return host->processes();
145 }
146
147 /** \ingroup m_host_management
148  * \brief Returns the value of a given host property
149  *
150  * \param host a host
151  * \param name a property name
152  * \return value of a property (or nullptr if property not set)
153  */
154 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
155 {
156   return static_cast<const char*>(xbt_dict_get_or_null(MSG_host_get_properties(host), name));
157 }
158
159 /** \ingroup m_host_management
160  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
161  *
162  * \param host a host
163  * \return a dict containing the properties
164  */
165 xbt_dict_t MSG_host_get_properties(msg_host_t host)
166 {
167   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
168   return host->properties();
169 }
170
171 /** \ingroup m_host_management
172  * \brief Change the value of a given host property
173  *
174  * \param host a host
175  * \param name a property name
176  * \param value what to change the property to
177  */
178 void MSG_host_set_property_value(msg_host_t host, const char* name, char* value)
179 {
180   xbt_dict_set(MSG_host_get_properties(host), name, value, nullptr);
181 }
182
183 /** @ingroup m_host_management
184  * @brief Determine if a host is up and running.
185  *
186  * See also #MSG_host_on() and #MSG_host_off() to switch the host ON and OFF and @ref SURF_plugin_energy for more info on DVFS.
187  *
188  * @param host host to test
189  * @return Returns true if the host is up and running, and false if it's currently down
190  */
191 int MSG_host_is_on(msg_host_t host)
192 {
193   return host->isOn();
194 }
195
196 /** @ingroup m_host_management
197  * @brief Determine if a host is currently off.
198  *
199  * See also #MSG_host_on() and #MSG_host_off() to switch the host ON and OFF and @ref SURF_plugin_energy for more info on DVFS.
200  */
201 int MSG_host_is_off(msg_host_t host)
202 {
203   return host->isOff();
204 }
205
206 /** \ingroup m_host_management
207  * \brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref SURF_plugin_energy.
208  *
209  * \param  host host to test
210  * \param pstate_index pstate to test
211  * \return Returns the processor speed associated with pstate_index
212  */
213 double MSG_host_get_power_peak_at(msg_host_t host, int pstate_index) {
214   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
215   return host->getPstateSpeed(pstate_index);
216 }
217
218 /** \ingroup m_host_management
219  * \brief Return the current speed of the processor (in flop/s)
220  *
221  * \param  host host to test
222  * \return Returns the current processor speed
223  */
224 double MSG_host_get_current_power_peak(msg_host_t host) {
225   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
226   return host->getPstateSpeedCurrent();
227 }
228
229 /** \ingroup m_host_management
230  * \brief Return the total count of pstates defined for a host. See also @ref SURF_plugin_energy.
231  *
232  * \param  host host to test
233  */
234 int MSG_host_get_nb_pstates(msg_host_t host) {
235   return sg_host_get_nb_pstates(host);
236 }
237
238 /** \ingroup m_host_management
239  * \brief Return the list of mount point names on an host.
240  * \param host a host
241  * \return a dict containing all mount point on the host (mount_name => msg_storage_t)
242  */
243 xbt_dict_t MSG_host_get_mounted_storage_list(msg_host_t host)
244 {
245   xbt_assert((host != nullptr), "Invalid parameters");
246   return host->mountedStoragesAsDict();
247 }
248
249 /** \ingroup m_host_management
250  * \brief Return the list of storages attached to an host.
251  * \param host a host
252  * \return a dynar containing all storages (name) attached to the host
253  */
254 xbt_dynar_t MSG_host_get_attached_storage_list(msg_host_t host)
255 {
256   xbt_assert((host != nullptr), "Invalid parameters");
257   return host->attachedStorages();
258 }
259
260 /** \ingroup m_host_management
261  * \brief Return the content of mounted storages on an host.
262  * \param host a host
263  * \return a dict containing content (as a dict) of all storages mounted on the host
264  */
265 xbt_dict_t MSG_host_get_storage_content(msg_host_t host)
266 {
267   xbt_assert((host != nullptr), "Invalid parameters");
268   xbt_dict_t contents = xbt_dict_new_homogeneous(nullptr);
269   msg_storage_t storage;
270   char* storage_name;
271   char* mount_name;
272   xbt_dict_cursor_t cursor = nullptr;
273
274   xbt_dict_t storage_list = host->mountedStoragesAsDict();
275
276   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
277     storage = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib,storage_name));
278     xbt_dict_t content = simcall_storage_get_content(storage);
279     xbt_dict_set(contents,mount_name, content,nullptr);
280   }
281   xbt_dict_free(&storage_list);
282   return contents;
283 }