1 /* Copyright (c) 2013-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid/host.h"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "xbt/Extendable.hpp"
15 #include "src/kernel/routing/NetPoint.hpp"
16 #include "src/simix/smx_host_private.hpp"
17 #include "src/surf/HostImpl.hpp"
18 #include "src/surf/cpu_interface.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
24 size_t sg_host_count()
26 return simgrid::s4u::Engine::getInstance()->getHostCount();
28 /** @brief Returns the host list
30 * Uses sg_host_count() to know the array size.
32 * \return an array of \ref sg_host_t containing all the hosts in the platform.
33 * \remark The host order in this array is generally different from the
34 * creation/declaration order in the XML platform (we use a hash table
36 * \see sg_host_count()
38 sg_host_t *sg_host_list() {
39 xbt_assert(sg_host_count() > 0, "There is no host!");
41 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
44 const char *sg_host_get_name(sg_host_t host)
46 return host->getCname();
49 void* sg_host_extension_get(sg_host_t host, size_t ext)
51 return host->extension(ext);
54 size_t sg_host_extension_create(void(*deleter)(void*))
56 return simgrid::s4u::Host::extension_create(deleter);
59 sg_host_t sg_host_by_name(const char *name)
61 return simgrid::s4u::Host::by_name_or_null(name);
64 static int hostcmp_voidp(const void* pa, const void* pb)
66 return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->getCname(),
67 (*static_cast<simgrid::s4u::Host* const*>(pb))->getCname());
70 xbt_dynar_t sg_hosts_as_dynar()
72 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
74 std::vector<simgrid::s4u::Host*> list;
75 simgrid::s4u::Engine::getInstance()->getHostList(&list);
77 for (auto const& host : list) {
78 if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
79 xbt_dynar_push(res, &host);
81 xbt_dynar_sort(res, hostcmp_voidp);
85 // ========= Layering madness ==============*
87 // ========== User data Layer ==========
88 void *sg_host_user(sg_host_t host) {
89 return host->extension(USER_HOST_LEVEL);
91 void sg_host_user_set(sg_host_t host, void* userdata) {
92 host->extension_set(USER_HOST_LEVEL,userdata);
94 void sg_host_user_destroy(sg_host_t host) {
95 host->extension_set(USER_HOST_LEVEL, nullptr);
98 // ========= storage related functions ============
99 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
100 xbt_assert((host != nullptr), "Invalid parameters");
101 xbt_dict_t res = xbt_dict_new_homogeneous(nullptr);
102 for (auto const& elm : host->getMountedStorages()) {
103 const char* mount_name = elm.first.c_str();
104 sg_storage_t storage = elm.second;
105 xbt_dict_set(res, mount_name, (void*)storage->getCname(), nullptr);
111 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
112 std::vector<const char*>* storage_vector = new std::vector<const char*>();
113 xbt_dynar_t storage_dynar = xbt_dynar_new(sizeof(const char*), nullptr);
114 host->getAttachedStorages(storage_vector);
115 for (auto const& name : *storage_vector)
116 xbt_dynar_push(storage_dynar, &name);
117 delete storage_vector;
118 return storage_dynar;
121 // =========== user-level functions ===============
122 // ================================================
123 /** @brief Returns the total speed of a host */
124 double sg_host_speed(sg_host_t host)
126 return host->getSpeed();
129 double sg_host_get_available_speed(sg_host_t host)
131 return host->pimpl_cpu->getAvailableSpeed();
134 /** @brief Returns the number of power states for a host.
136 * See also @ref plugin_energy.
138 int sg_host_get_nb_pstates(sg_host_t host) {
139 return host->getPstatesCount();
142 /** @brief Gets the pstate at which that host currently runs.
144 * See also @ref plugin_energy.
146 int sg_host_get_pstate(sg_host_t host) {
147 return host->getPstate();
149 /** @brief Sets the pstate at which that host should run.
151 * See also @ref plugin_energy.
153 void sg_host_set_pstate(sg_host_t host,int pstate) {
154 host->setPstate(pstate);
157 /** @brief Get the properties of an host */
158 xbt_dict_t sg_host_get_properties(sg_host_t host) {
159 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
160 std::map<std::string, std::string>* props = host->getProperties();
161 if (props == nullptr)
163 for (auto const& elm : *props) {
164 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
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 *sg_host_get_property_value(sg_host_t host, const char *name)
178 return host->getProperty(name);
181 void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
183 host->setProperty(name, value);
187 * \brief Find a route between two hosts
189 * \param from where from
191 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
193 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
195 std::vector<simgrid::s4u::Link*> vlinks;
196 from->routeTo(to, vlinks, nullptr);
197 for (auto const& link : vlinks)
198 xbt_dynar_push(links, &link);
201 * \brief Find the latency of the route between two hosts
203 * \param from where from
206 double sg_host_route_latency(sg_host_t from, sg_host_t to)
208 std::vector<simgrid::s4u::Link*> vlinks;
210 from->routeTo(to, vlinks, &res);
214 * \brief Find the bandwitdh of the route between two hosts
216 * \param from where from
219 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
221 double min_bandwidth = -1.0;
223 std::vector<simgrid::s4u::Link*> vlinks;
224 from->routeTo(to, vlinks, nullptr);
225 for (auto const& link : vlinks) {
226 double bandwidth = link->bandwidth();
227 if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
228 min_bandwidth = bandwidth;
230 return min_bandwidth;
233 /** @brief Displays debugging information about a host */
234 void sg_host_dump(sg_host_t host)
236 XBT_INFO("Displaying host %s", host->getCname());
237 XBT_INFO(" - speed: %.0f", host->getSpeed());
238 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
239 std::map<std::string, std::string>* props = host->getProperties();
241 if (not props->empty()) {
242 XBT_INFO(" - properties:");
243 for (auto const& elm : *props) {
244 XBT_INFO(" %s->%s", elm.first.c_str(), elm.second.c_str());