Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make these example files compile
[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 #include <numeric>
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg);
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 msg_host_t __MSG_host_create(sg_host_t host) // FIXME: don't return our parameter
27 {
28   msg_host_priv_t priv = xbt_new0(s_msg_host_priv_t, 1);
29
30   priv->file_descriptor_table = nullptr;
31
32   sg_host_msg_set(host,priv);
33
34   return host;
35 }
36
37 /** \ingroup m_host_management
38  * \brief Finds a msg_host_t using its name.
39  *
40  * This is a name directory service
41  * \param name the name of an host.
42  * \return the corresponding host
43  */
44 msg_host_t MSG_host_by_name(const char *name)
45 {
46   return simgrid::s4u::Host::by_name_or_null(name);
47 }
48
49 /** \ingroup m_host_management
50  *
51  * \brief Set the user data of a #msg_host_t.
52  *
53  * This functions attach \a data to \a host if it is possible.
54  */
55 msg_error_t MSG_host_set_data(msg_host_t host, void *data) {
56   sg_host_user_set(host, data);
57   return MSG_OK;
58 }
59
60 /** \ingroup m_host_management
61  *
62  * \brief Return the user data of a #msg_host_t.
63  *
64  * This functions returns the user data associated to \a host if it is possible.
65  */
66 void *MSG_host_get_data(msg_host_t host) {
67   return sg_host_user(host);
68 }
69
70 /** \ingroup m_host_management
71  *
72  * \brief Return the location on which the current process is executed.
73  */
74 msg_host_t MSG_host_self()
75 {
76   return MSG_process_get_host(nullptr);
77 }
78
79 /** \ingroup m_host_management
80  *
81  * \brief Start the host if it is off
82  *
83  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref SURF_plugin_energy
84  * for more info on DVFS.
85  */
86 void MSG_host_on(msg_host_t host)
87 {
88   host->turnOn();
89 }
90
91 /** \ingroup m_host_management
92  *
93  * \brief Stop the host if it is on
94  *
95  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref SURF_plugin_energy
96  * for more info on DVFS.
97  */
98 void MSG_host_off(msg_host_t host)
99 {
100   host->turnOff();
101 }
102
103 /*
104  * \brief Frees private data of a host (internal call only)
105  */
106 void __MSG_host_priv_free(msg_host_priv_t priv)
107 {
108   if (priv == nullptr)
109     return;
110   delete priv->file_descriptor_table;
111   free(priv);
112 }
113
114 /** \ingroup m_host_management
115  * \brief Return the current number MSG hosts.
116  */
117 int MSG_get_host_number()
118 {
119   return sg_host_count();
120 }
121
122 /** \ingroup m_host_management
123  * \brief Return a dynar containing all the hosts declared at a given point of time
124  * \remark The host order in the returned array is generally different from the host creation/declaration order in the
125  *         XML platform (we use a hash table internally)
126  */
127 xbt_dynar_t MSG_hosts_as_dynar() {
128   return sg_hosts_as_dynar();
129 }
130
131 /** \ingroup m_host_management
132  * \brief Return the speed of the processor (in flop/s), regardless of the current load on the machine.
133  */
134 double MSG_host_get_speed(msg_host_t host) {
135   return host->speed();
136 }
137
138 /** \ingroup m_host_management
139  * \brief Return the speed of the processor (in flop/s), regardless of the current load on the machine.
140  * Deprecated: use MSG_host_get_speed
141  */
142 double MSG_get_host_speed(msg_host_t host) {
143   XBT_WARN("MSG_get_host_speed is deprecated: use MSG_host_get_speed");
144   return MSG_host_get_speed(host);
145 }
146
147 /** \ingroup m_host_management
148  * \brief Return the number of cores.
149  *
150  * \param host a host
151  * \return the number of cores
152  */
153 int MSG_host_get_core_number(msg_host_t host) {
154   return host->coreCount();
155 }
156
157 /** \ingroup m_host_management
158  * \brief Return the list of processes attached to an host.
159  *
160  * \param host a host
161  * \return a swag with the attached processes
162  */
163 xbt_swag_t MSG_host_get_process_list(msg_host_t host)
164 {
165   xbt_assert((host != nullptr), "Invalid parameters");
166   return host->processes();
167 }
168
169 /** \ingroup m_host_management
170  * \brief Returns the value of a given host property
171  *
172  * \param host a host
173  * \param name a property name
174  * \return value of a property (or nullptr if property not set)
175  */
176 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
177 {
178   return static_cast<const char*>(xbt_dict_get_or_null(MSG_host_get_properties(host), name));
179 }
180
181 /** \ingroup m_host_management
182  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
183  *
184  * \param host a host
185  * \return a dict containing the properties
186  */
187 xbt_dict_t MSG_host_get_properties(msg_host_t host)
188 {
189   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
190   return host->properties();
191 }
192
193 /** \ingroup m_host_management
194  * \brief Change the value of a given host property
195  *
196  * \param host a host
197  * \param name a property name
198  * \param value what to change the property to
199  */
200 void MSG_host_set_property_value(msg_host_t host, const char* name, char* value)
201 {
202   xbt_dict_set(MSG_host_get_properties(host), name, value, nullptr);
203 }
204
205 /** @ingroup m_host_management
206  * @brief Determine if a host is up and running.
207  *
208  * 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.
209  *
210  * @param host host to test
211  * @return Returns true if the host is up and running, and false if it's currently down
212  */
213 int MSG_host_is_on(msg_host_t host)
214 {
215   return host->isOn();
216 }
217
218 /** @ingroup m_host_management
219  * @brief Determine if a host is currently off.
220  *
221  * 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.
222  */
223 int MSG_host_is_off(msg_host_t host)
224 {
225   return host->isOff();
226 }
227
228 /** \ingroup m_host_management
229  * \brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref SURF_plugin_energy.
230  *
231  * \param  host host to test
232  * \param pstate_index pstate to test
233  * \return Returns the processor speed associated with pstate_index
234  */
235 double MSG_host_get_power_peak_at(msg_host_t host, int pstate_index) {
236   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
237   return host->getPstateSpeed(pstate_index);
238 }
239
240 /** \ingroup m_host_management
241  * \brief Return the current speed of the processor (in flop/s)
242  *
243  * \param  host host to test
244  * \return Returns the current processor speed
245  */
246 double MSG_host_get_current_power_peak(msg_host_t host) {
247   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
248   return host->getPstateSpeedCurrent();
249 }
250
251 /** \ingroup m_host_management
252  * \brief Return the total count of pstates defined for a host. See also @ref SURF_plugin_energy.
253  *
254  * \param  host host to test
255  */
256 int MSG_host_get_nb_pstates(msg_host_t host) {
257   return sg_host_get_nb_pstates(host);
258 }
259
260 /** \ingroup m_host_management
261  * \brief Return the list of mount point names on an host.
262  * \param host a host
263  * \return a dict containing all mount point on the host (mount_name => msg_storage_t)
264  */
265 xbt_dict_t MSG_host_get_mounted_storage_list(msg_host_t host)
266 {
267   xbt_assert((host != nullptr), "Invalid parameters");
268   return host->mountedStoragesAsDict();
269 }
270
271 /** \ingroup m_host_management
272  * \brief Return the list of storages attached to an host.
273  * \param host a host
274  * \return a dynar containing all storages (name) attached to the host
275  */
276 xbt_dynar_t MSG_host_get_attached_storage_list(msg_host_t host)
277 {
278   xbt_assert((host != nullptr), "Invalid parameters");
279   return host->attachedStorages();
280 }
281
282 /** \ingroup m_host_management
283  * \brief Return the content of mounted storages on an host.
284  * \param host a host
285  * \return a dict containing content (as a dict) of all storages mounted on the host
286  */
287 xbt_dict_t MSG_host_get_storage_content(msg_host_t host)
288 {
289   xbt_assert((host != nullptr), "Invalid parameters");
290   xbt_dict_t contents = xbt_dict_new_homogeneous(nullptr);
291   msg_storage_t storage;
292   char* storage_name;
293   char* mount_name;
294   xbt_dict_cursor_t cursor = nullptr;
295
296   xbt_dict_t storage_list = host->mountedStoragesAsDict();
297
298   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
299     storage = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib,storage_name));
300     xbt_dict_t content = simcall_storage_get_content(storage);
301     xbt_dict_set(contents,mount_name, content,nullptr);
302   }
303   xbt_dict_free(&storage_list);
304   return contents;
305 }
306
307 int __MSG_host_get_file_descriptor_id(msg_host_t host){
308   msg_host_priv_t priv = sg_host_msg(host);
309   if(!priv->file_descriptor_table){
310     priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
311     std::iota (priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
312   }
313   xbt_assert(!priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
314   int desc = priv->file_descriptor_table->back();
315   priv->file_descriptor_table->pop_back();
316   return desc;
317 }
318
319 void __MSG_host_release_file_descriptor_id(msg_host_t host, int id){
320   msg_host_priv_t priv = sg_host_msg(host);
321   priv->file_descriptor_table->push_back(id);
322 }