1 /* Copyright (c) 2004-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/s4u/host.hpp"
8 #include "src/msg/msg_private.h"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg);
14 int sg_storage_max_file_descriptors = 1024;
16 /** @addtogroup m_host_management
17 * (#msg_host_t) and the functions for managing it.
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.
25 /********************************* Host **************************************/
26 msg_host_t __MSG_host_create(sg_host_t host) // FIXME: don't return our parameter
28 msg_host_priv_t priv = xbt_new0(s_msg_host_priv_t, 1);
30 priv->file_descriptor_table = nullptr;
32 sg_host_msg_set(host,priv);
37 /** \ingroup m_host_management
38 * \brief Finds a msg_host_t using its name.
40 * This is a name directory service
41 * \param name the name of an host.
42 * \return the corresponding host
44 msg_host_t MSG_host_by_name(const char *name)
46 return simgrid::s4u::Host::by_name_or_null(name);
49 /** \ingroup m_host_management
51 * \brief Set the user data of a #msg_host_t.
53 * This functions attach \a data to \a host if it is possible.
55 msg_error_t MSG_host_set_data(msg_host_t host, void *data) {
56 sg_host_user_set(host, data);
60 /** \ingroup m_host_management
62 * \brief Return the user data of a #msg_host_t.
64 * This functions returns the user data associated to \a host if it is possible.
66 void *MSG_host_get_data(msg_host_t host) {
67 return sg_host_user(host);
70 /** \ingroup m_host_management
72 * \brief Return the location on which the current process is executed.
74 msg_host_t MSG_host_self()
76 return MSG_process_get_host(nullptr);
79 /** \ingroup m_host_management
81 * \brief Start the host if it is off
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.
86 void MSG_host_on(msg_host_t host)
91 /** \ingroup m_host_management
93 * \brief Stop the host if it is on
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.
98 void MSG_host_off(msg_host_t host)
104 * \brief Frees private data of a host (internal call only)
106 void __MSG_host_priv_free(msg_host_priv_t priv)
110 delete priv->file_descriptor_table;
114 /** \ingroup m_host_management
115 * \brief Return the current number MSG hosts.
117 int MSG_get_host_number()
119 return sg_host_count();
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)
127 xbt_dynar_t MSG_hosts_as_dynar() {
128 return sg_hosts_as_dynar();
131 /** \ingroup m_host_management
132 * \brief Return the speed of the processor (in flop/s), regardless of the current load on the machine.
134 double MSG_host_get_speed(msg_host_t host) {
135 return host->speed();
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
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);
147 /** \ingroup m_host_management
148 * \brief Return the number of cores.
151 * \return the number of cores
153 int MSG_host_get_core_number(msg_host_t host) {
154 return host->coreCount();
157 /** \ingroup m_host_management
158 * \brief Return the list of processes attached to an host.
161 * \return a swag with the attached processes
163 xbt_swag_t MSG_host_get_process_list(msg_host_t host)
165 xbt_assert((host != nullptr), "Invalid parameters");
166 return host->processes();
169 /** \ingroup m_host_management
170 * \brief Returns the value of a given host property
173 * \param name a property name
174 * \return value of a property (or nullptr if property not set)
176 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
178 return static_cast<const char*>(xbt_dict_get_or_null(MSG_host_get_properties(host), name));
181 /** \ingroup m_host_management
182 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
185 * \return a dict containing the properties
187 xbt_dict_t MSG_host_get_properties(msg_host_t host)
189 xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
190 return host->properties();
193 /** \ingroup m_host_management
194 * \brief Change the value of a given host property
197 * \param name a property name
198 * \param value what to change the property to
200 void MSG_host_set_property_value(msg_host_t host, const char* name, char* value)
202 xbt_dict_set(MSG_host_get_properties(host), name, value, nullptr);
205 /** @ingroup m_host_management
206 * @brief Determine if a host is up and running.
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.
210 * @param host host to test
211 * @return Returns true if the host is up and running, and false if it's currently down
213 int MSG_host_is_on(msg_host_t host)
218 /** @ingroup m_host_management
219 * @brief Determine if a host is currently off.
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.
223 int MSG_host_is_off(msg_host_t host)
225 return host->isOff();
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.
231 * \param host host to test
232 * \param pstate_index pstate to test
233 * \return Returns the processor speed associated with pstate_index
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);
240 /** \ingroup m_host_management
241 * \brief Return the current speed of the processor (in flop/s)
243 * \param host host to test
244 * \return Returns the current processor speed
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();
251 /** \ingroup m_host_management
252 * \brief Return the total count of pstates defined for a host. See also @ref SURF_plugin_energy.
254 * \param host host to test
256 int MSG_host_get_nb_pstates(msg_host_t host) {
257 return sg_host_get_nb_pstates(host);
260 /** \ingroup m_host_management
261 * \brief Return the list of mount point names on an host.
263 * \return a dict containing all mount point on the host (mount_name => msg_storage_t)
265 xbt_dict_t MSG_host_get_mounted_storage_list(msg_host_t host)
267 xbt_assert((host != nullptr), "Invalid parameters");
268 return host->mountedStoragesAsDict();
271 /** \ingroup m_host_management
272 * \brief Return the list of storages attached to an host.
274 * \return a dynar containing all storages (name) attached to the host
276 xbt_dynar_t MSG_host_get_attached_storage_list(msg_host_t host)
278 xbt_assert((host != nullptr), "Invalid parameters");
279 return host->attachedStorages();
282 /** \ingroup m_host_management
283 * \brief Return the content of mounted storages on an host.
285 * \return a dict containing content (as a dict) of all storages mounted on the host
287 xbt_dict_t MSG_host_get_storage_content(msg_host_t host)
289 xbt_assert((host != nullptr), "Invalid parameters");
290 xbt_dict_t contents = xbt_dict_new_homogeneous(nullptr);
291 msg_storage_t storage;
294 xbt_dict_cursor_t cursor = nullptr;
296 xbt_dict_t storage_list = host->mountedStoragesAsDict();
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);
303 xbt_dict_free(&storage_list);
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.
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();
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);