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. */
10 #include "simgrid/host.h"
11 #include <xbt/Extendable.hpp>
12 #include <simgrid/s4u/host.hpp>
14 #include "src/kernel/routing/NetPoint.hpp"
15 #include "src/simix/smx_host_private.h"
16 #include "src/surf/HostImpl.hpp"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
20 // FIXME: The following duplicates the content of s4u::Host
21 extern std::unordered_map<std::string, simgrid::s4u::Host*> host_list;
27 /* copy all names to not modify the map while iterating over it.
29 * Plus, the hosts are destroyed in the lexicographic order to ensure
30 * that the output is reproducible: we don't want to kill them in the
31 * pointer order as it could be platform-dependent, which would break
34 std::vector<std::string> names = std::vector<std::string>();
35 for (auto kv : host_list)
36 names.push_back(kv.second->name());
38 std::sort(names.begin(), names.end());
40 for (auto name : names)
41 host_list.at(name)->destroy();
43 // host_list.clear(); This would be sufficient if the dict would contain smart_ptr. It's now useless
46 size_t sg_host_count()
48 return host_list.size();
50 /** @brief Returns the host list
52 * Uses sg_host_count() to know the array size.
54 * \return an array of \ref sg_host_t containing all the hosts in the platform.
55 * \remark The host order in this array is generally different from the
56 * creation/declaration order in the XML platform (we use a hash table
58 * \see sg_host_count()
60 sg_host_t *sg_host_list() {
61 xbt_assert(sg_host_count() > 0, "There is no host!");
62 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
65 const char *sg_host_get_name(sg_host_t host)
70 void* sg_host_extension_get(sg_host_t host, size_t ext)
72 return host->extension(ext);
75 size_t sg_host_extension_create(void(*deleter)(void*))
77 return simgrid::s4u::Host::extension_create(deleter);
80 sg_host_t sg_host_by_name(const char *name)
82 return simgrid::s4u::Host::by_name_or_null(name);
85 static int hostcmp_voidp(const void* pa, const void* pb)
87 return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->cname(),
88 (*static_cast<simgrid::s4u::Host* const*>(pb))->cname());
91 xbt_dynar_t sg_hosts_as_dynar()
93 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
95 for (auto kv : host_list) {
96 simgrid::s4u::Host* host = kv.second;
97 if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
98 xbt_dynar_push(res, &host);
100 xbt_dynar_sort(res, hostcmp_voidp);
104 // ========= Layering madness ==============*
106 #include "src/surf/cpu_interface.hpp"
108 // ========== User data Layer ==========
109 void *sg_host_user(sg_host_t host) {
110 return host->extension(USER_HOST_LEVEL);
112 void sg_host_user_set(sg_host_t host, void* userdata) {
113 host->extension_set(USER_HOST_LEVEL,userdata);
115 void sg_host_user_destroy(sg_host_t host) {
116 host->extension_set(USER_HOST_LEVEL, nullptr);
119 // ========== MSG Layer ==============
120 msg_host_priv_t sg_host_msg(sg_host_t host) {
121 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
123 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
124 host->extension_set(MSG_HOST_LEVEL, smx_host);
127 // ========== Simix layer =============
128 smx_host_priv_t sg_host_simix(sg_host_t host){
129 return host->extension<simgrid::simix::Host>();
132 // ========= storage related functions ============
133 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
134 return host->pimpl_->getMountedStorageList();
137 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
138 return host->pimpl_->getAttachedStorageList();
142 // =========== user-level functions ===============
143 // ================================================
144 /** @brief Returns the total speed of a host */
145 double sg_host_speed(sg_host_t host)
147 return host->speed();
150 double sg_host_get_available_speed(sg_host_t host)
152 return host->pimpl_cpu->getAvailableSpeed();
155 /** @brief Returns the number of power states for a host.
157 * See also @ref SURF_plugin_energy.
159 int sg_host_get_nb_pstates(sg_host_t host) {
160 return host->pstatesCount();
163 /** @brief Gets the pstate at which that host currently runs.
165 * See also @ref SURF_plugin_energy.
167 int sg_host_get_pstate(sg_host_t host) {
168 return host->pstate();
170 /** @brief Sets the pstate at which that host should run.
172 * See also @ref SURF_plugin_energy.
174 void sg_host_set_pstate(sg_host_t host,int pstate) {
175 host->setPstate(pstate);
178 /** @brief Get the properties of an host */
179 xbt_dict_t sg_host_get_properties(sg_host_t host) {
180 return host->properties();
183 /** \ingroup m_host_management
184 * \brief Returns the value of a given host property
187 * \param name a property name
188 * \return value of a property (or nullptr if property not set)
190 const char *sg_host_get_property_value(sg_host_t host, const char *name)
192 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
195 * \brief Find a route between two hosts
197 * \param from where from
199 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
201 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
203 std::vector<simgrid::s4u::Link*> vlinks;
204 from->routeTo(to, &vlinks, nullptr);
205 for (auto link : vlinks)
206 xbt_dynar_push(links, &link);
209 * \brief Find the latency of the route between two hosts
211 * \param from where from
214 double sg_host_route_latency(sg_host_t from, sg_host_t to)
216 std::vector<simgrid::s4u::Link*> vlinks;
218 from->routeTo(to, &vlinks, &res);
222 * \brief Find the bandwitdh of the route between two hosts
224 * \param from where from
227 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
229 std::vector<simgrid::s4u::Link*> vlinks;
230 from->routeTo(to, &vlinks, nullptr);
232 for (auto link : vlinks)
233 res += link->bandwidth();
237 /** @brief Displays debugging information about a host */
238 void sg_host_dump(sg_host_t host)
241 xbt_dict_cursor_t cursor=nullptr;
244 XBT_INFO("Displaying host %s", host->cname());
245 XBT_INFO(" - speed: %.0f", host->speed());
246 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
247 props = host->properties();
249 if (!xbt_dict_is_empty(props)){
250 XBT_INFO(" - properties:");
252 xbt_dict_foreach(props,cursor,key,data) {
253 XBT_INFO(" %s->%s",key,data);