1 /* Copyright (c) 2013-2016. 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. */
8 #include "simgrid/host.h"
9 #include <xbt/Extendable.hpp>
10 #include <simgrid/s4u/host.hpp>
12 #include "src/surf/HostImplem.hpp"
13 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
15 #include "src/simix/smx_private.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
19 size_t sg_host_count()
21 return xbt_dict_length(host_list);
23 /** @brief Returns the host list
25 * Uses sg_host_count() to know the array size.
27 * \return an array of \ref sg_host_t containing all the hosts in the platform.
28 * \remark The host order in this array is generally different from the
29 * creation/declaration order in the XML platform (we use a hash table
31 * \see sg_host_count()
33 sg_host_t *sg_host_list(void) {
34 xbt_assert(sg_host_count() > 0, "There is no host!");
35 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
38 const char *sg_host_get_name(sg_host_t host)
40 return host->name().c_str();
43 void* sg_host_extension_get(sg_host_t host, size_t ext)
45 return host->extension(ext);
48 size_t sg_host_extension_create(void(*deleter)(void*))
50 return simgrid::s4u::Host::extension_create(deleter);
53 sg_host_t sg_host_by_name(const char *name)
55 return simgrid::s4u::Host::by_name_or_null(name);
58 sg_host_t sg_host_by_name_or_create(const char *name)
60 return simgrid::s4u::Host::by_name_or_create(name);
63 xbt_dynar_t sg_hosts_as_dynar(void)
65 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
67 xbt_dict_cursor_t cursor = nullptr;
68 const char* name = nullptr;
69 simgrid::s4u::Host* host = nullptr;
70 xbt_dict_foreach(host_list, cursor, name, host)
71 if (host && host->pimpl_netcard && host->pimpl_netcard->getRcType() == SURF_NETWORK_ELEMENT_HOST)
72 xbt_dynar_push(res, &host);
76 // ========= Layering madness ==============*
78 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme by initializing that level in msg when used
79 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme by initializing that level in simix when used
80 #include "src/surf/cpu_interface.hpp"
81 #include "src/surf/surf_routing.hpp"
85 MSG_HOST_LEVEL = simgrid::s4u::Host::extension_create([](void *p) {
86 __MSG_host_priv_free((msg_host_priv_t) p);
89 ROUTING_HOST_LEVEL = simgrid::s4u::Host::extension_create([](void *p) {
90 delete static_cast<simgrid::surf::NetCard*>(p);
93 SD_HOST_LEVEL = simgrid::s4u::Host::extension_create(NULL);
94 SIMIX_HOST_LEVEL = simgrid::s4u::Host::extension_create(SIMIX_host_destroy);
95 USER_HOST_LEVEL = simgrid::s4u::Host::extension_create(NULL);
98 // ========== User data Layer ==========
99 void *sg_host_user(sg_host_t host) {
100 return host->extension(USER_HOST_LEVEL);
102 void sg_host_user_set(sg_host_t host, void* userdata) {
103 host->extension_set(USER_HOST_LEVEL,userdata);
105 void sg_host_user_destroy(sg_host_t host) {
106 host->extension_set(USER_HOST_LEVEL, nullptr);
109 // ========== MSG Layer ==============
110 msg_host_priv_t sg_host_msg(sg_host_t host) {
111 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
113 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
114 host->extension_set(MSG_HOST_LEVEL, smx_host);
117 // ========== Simix layer =============
118 smx_host_priv_t sg_host_simix(sg_host_t host){
119 return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
121 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
122 host->extension_set(SIMIX_HOST_LEVEL, smx_host);
124 void sg_host_simix_destroy(sg_host_t host) {
125 host->extension_set(SIMIX_HOST_LEVEL, nullptr);
128 // ========= storage related functions ============
129 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
130 return host->extension<simgrid::surf::HostImplem>()->getMountedStorageList();
133 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
134 return host->extension<simgrid::surf::HostImplem>()->getAttachedStorageList();
138 // =========== user-level functions ===============
139 // ================================================
141 /** @brief Returns the total speed of a host
143 double sg_host_speed(sg_host_t host)
145 return host->speed();
148 double sg_host_get_available_speed(sg_host_t host){
149 return surf_host_get_available_speed(host);
151 /** @brief Returns the number of cores of a host
153 int sg_host_core_count(sg_host_t host) {
154 return host->core_count();
157 /** @brief Returns the state of a host.
158 * @return 1 if the host is active or 0 if it has crashed.
160 int sg_host_is_on(sg_host_t host) {
164 /** @brief Returns the number of power states for a host.
166 * See also @ref SURF_plugin_energy.
168 int sg_host_get_nb_pstates(sg_host_t host) {
169 return host->pstatesCount();
172 /** @brief Gets the pstate at which that host currently runs.
174 * See also @ref SURF_plugin_energy.
176 int sg_host_get_pstate(sg_host_t host) {
177 return host->pstate();
179 /** @brief Sets the pstate at which that host should run.
181 * See also @ref SURF_plugin_energy.
183 void sg_host_set_pstate(sg_host_t host,int pstate) {
184 host->setPstate(pstate);
187 /** @brief Get the properties of an host */
188 xbt_dict_t sg_host_get_properties(sg_host_t host) {
189 return host->properties();
192 /** \ingroup m_host_management
193 * \brief Returns the value of a given host property
196 * \param name a property name
197 * \return value of a property (or NULL if property not set)
199 const char *sg_host_get_property_value(sg_host_t host, const char *name)
201 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
204 /** @brief Displays debugging informations about a host */
205 void sg_host_dump(sg_host_t host)
208 xbt_dict_cursor_t cursor=NULL;
211 XBT_INFO("Displaying host %s", sg_host_get_name(host));
212 XBT_INFO(" - speed: %.0f", sg_host_speed(host));
213 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
214 props = sg_host_get_properties(host);
216 if (!xbt_dict_is_empty(props)){
217 XBT_INFO(" - properties:");
219 xbt_dict_foreach(props,cursor,key,data) {
220 XBT_INFO(" %s->%s",key,data);