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"
17 #include "src/surf/cpu_interface.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
21 // FIXME: The following duplicates the content of s4u::Host
22 extern std::map<std::string, simgrid::s4u::Host*> host_list;
28 /* copy all names to not modify the map while iterating over it.
30 * Plus, the hosts are destroyed in the lexicographic order to ensure
31 * that the output is reproducible: we don't want to kill them in the
32 * pointer order as it could be platform-dependent, which would break
35 std::vector<std::string> names = std::vector<std::string>();
36 for (auto kv : host_list)
37 names.push_back(kv.second->name());
39 std::sort(names.begin(), names.end());
41 for (auto name : names)
42 host_list.at(name)->destroy();
44 // host_list.clear(); This would be sufficient if the dict would contain smart_ptr. It's now useless
47 size_t sg_host_count()
49 return host_list.size();
51 /** @brief Returns the host list
53 * Uses sg_host_count() to know the array size.
55 * \return an array of \ref sg_host_t containing all the hosts in the platform.
56 * \remark The host order in this array is generally different from the
57 * creation/declaration order in the XML platform (we use a hash table
59 * \see sg_host_count()
61 sg_host_t *sg_host_list() {
62 xbt_assert(sg_host_count() > 0, "There is no host!");
63 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
66 const char *sg_host_get_name(sg_host_t host)
71 void* sg_host_extension_get(sg_host_t host, size_t ext)
73 return host->extension(ext);
76 size_t sg_host_extension_create(void(*deleter)(void*))
78 return simgrid::s4u::Host::extension_create(deleter);
81 sg_host_t sg_host_by_name(const char *name)
83 return simgrid::s4u::Host::by_name_or_null(name);
86 static int hostcmp_voidp(const void* pa, const void* pb)
88 return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->cname(),
89 (*static_cast<simgrid::s4u::Host* const*>(pb))->cname());
92 xbt_dynar_t sg_hosts_as_dynar()
94 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
96 for (auto kv : host_list) {
97 simgrid::s4u::Host* host = kv.second;
98 if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
99 xbt_dynar_push(res, &host);
101 xbt_dynar_sort(res, hostcmp_voidp);
105 // ========= Layering madness ==============*
107 // ========== User data Layer ==========
108 void *sg_host_user(sg_host_t host) {
109 return host->extension(USER_HOST_LEVEL);
111 void sg_host_user_set(sg_host_t host, void* userdata) {
112 host->extension_set(USER_HOST_LEVEL,userdata);
114 void sg_host_user_destroy(sg_host_t host) {
115 host->extension_set(USER_HOST_LEVEL, nullptr);
118 // ========= storage related functions ============
119 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
120 return host->pimpl_->getMountedStorageList();
123 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
124 xbt_dynar_t storage_dynar = xbt_dynar_new(sizeof(const char*), nullptr);
125 for (auto name : host->attachedStorages())
126 xbt_dynar_push(storage_dynar, &name);
127 return storage_dynar;
130 // =========== user-level functions ===============
131 // ================================================
132 /** @brief Returns the total speed of a host */
133 double sg_host_speed(sg_host_t host)
135 return host->speed();
138 double sg_host_get_available_speed(sg_host_t host)
140 return host->pimpl_cpu->getAvailableSpeed();
143 /** @brief Returns the number of power states for a host.
145 * See also @ref SURF_plugin_energy.
147 int sg_host_get_nb_pstates(sg_host_t host) {
148 return host->pstatesCount();
151 /** @brief Gets the pstate at which that host currently runs.
153 * See also @ref SURF_plugin_energy.
155 int sg_host_get_pstate(sg_host_t host) {
156 return host->pstate();
158 /** @brief Sets the pstate at which that host should run.
160 * See also @ref SURF_plugin_energy.
162 void sg_host_set_pstate(sg_host_t host,int pstate) {
163 host->setPstate(pstate);
166 /** @brief Get the properties of an host */
167 xbt_dict_t sg_host_get_properties(sg_host_t host) {
168 return host->properties();
171 /** \ingroup m_host_management
172 * \brief Returns the value of a given host property
175 * \param name a property name
176 * \return value of a property (or nullptr if property not set)
178 const char *sg_host_get_property_value(sg_host_t host, const char *name)
180 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
183 * \brief Find a route between two hosts
185 * \param from where from
187 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
189 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
191 std::vector<simgrid::s4u::Link*> vlinks;
192 from->routeTo(to, &vlinks, nullptr);
193 for (auto link : vlinks)
194 xbt_dynar_push(links, &link);
197 * \brief Find the latency of the route between two hosts
199 * \param from where from
202 double sg_host_route_latency(sg_host_t from, sg_host_t to)
204 std::vector<simgrid::s4u::Link*> vlinks;
206 from->routeTo(to, &vlinks, &res);
210 * \brief Find the bandwitdh of the route between two hosts
212 * \param from where from
215 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
217 double min_bandwidth = -1.0;
219 std::vector<simgrid::s4u::Link*> vlinks;
220 from->routeTo(to, &vlinks, nullptr);
221 for (auto link : vlinks) {
222 double bandwidth = link->bandwidth();
223 if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
224 min_bandwidth = bandwidth;
226 return min_bandwidth;
229 /** @brief Displays debugging information about a host */
230 void sg_host_dump(sg_host_t host)
233 xbt_dict_cursor_t cursor=nullptr;
236 XBT_INFO("Displaying host %s", host->cname());
237 XBT_INFO(" - speed: %.0f", host->speed());
238 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
239 props = host->properties();
241 if (!xbt_dict_is_empty(props)){
242 XBT_INFO(" - properties:");
244 xbt_dict_foreach(props,cursor,key,data) {
245 XBT_INFO(" %s->%s",key,data);