1 /* Copyright (c) 2013-2018. 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 "simgrid/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");
22 size_t sg_host_count()
24 return simgrid::s4u::Engine::getInstance()->getHostCount();
26 /** @brief Returns the host list
28 * Uses sg_host_count() to know the array size.
30 * \return an array of \ref sg_host_t containing all the hosts in the platform.
31 * \remark The host order in this array is generally different from the
32 * creation/declaration order in the XML platform (we use a hash table
34 * \see sg_host_count()
36 sg_host_t *sg_host_list() {
37 xbt_assert(sg_host_count() > 0, "There is no host!");
38 std::vector<simgrid::s4u::Host*> hosts = simgrid::s4u::Engine::getInstance()->getAllHosts();
40 sg_host_t* res = (sg_host_t*)malloc(sizeof(sg_host_t) * hosts.size());
41 memcpy(res, hosts.data(), sizeof(sg_host_t) * hosts.size());
46 const char *sg_host_get_name(sg_host_t host)
48 return host->getCname();
51 void* sg_host_extension_get(sg_host_t host, size_t ext)
53 return host->extension(ext);
56 size_t sg_host_extension_create(void(*deleter)(void*))
58 return simgrid::s4u::Host::extension_create(deleter);
61 sg_host_t sg_host_by_name(const char *name)
63 return simgrid::s4u::Host::by_name_or_null(name);
66 static int hostcmp_voidp(const void* pa, const void* pb)
68 return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->getCname(),
69 (*static_cast<simgrid::s4u::Host* const*>(pb))->getCname());
72 xbt_dynar_t sg_hosts_as_dynar()
74 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
76 std::vector<simgrid::s4u::Host*> list = simgrid::s4u::Engine::getInstance()->getAllHosts();
78 for (auto const& host : list) {
79 if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
80 xbt_dynar_push(res, &host);
82 xbt_dynar_sort(res, hostcmp_voidp);
86 // ========= Layering madness ==============*
88 // ========== User data Layer ==========
89 void *sg_host_user(sg_host_t host) {
90 return host->extension(USER_HOST_LEVEL);
92 void sg_host_user_set(sg_host_t host, void* userdata) {
93 host->extension_set(USER_HOST_LEVEL,userdata);
95 void sg_host_user_destroy(sg_host_t host) {
96 host->extension_set(USER_HOST_LEVEL, nullptr);
99 // ========= storage related functions ============
100 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
101 xbt_assert((host != nullptr), "Invalid parameters");
102 xbt_dict_t res = xbt_dict_new_homogeneous(nullptr);
103 for (auto const& elm : host->getMountedStorages()) {
104 const char* mount_name = elm.first.c_str();
105 sg_storage_t storage = elm.second;
106 xbt_dict_set(res, mount_name, (void*)storage->getCname(), nullptr);
112 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
113 std::vector<const char*>* storage_vector = new std::vector<const char*>();
114 xbt_dynar_t storage_dynar = xbt_dynar_new(sizeof(const char*), nullptr);
115 host->getAttachedStorages(storage_vector);
116 for (auto const& name : *storage_vector)
117 xbt_dynar_push(storage_dynar, &name);
118 delete storage_vector;
119 return storage_dynar;
122 // =========== user-level functions ===============
123 // ================================================
124 /** @brief Returns the total speed of a host */
125 double sg_host_speed(sg_host_t host)
127 return host->getSpeed();
130 /** \brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref plugin_energy.
132 * \param host host to test
133 * \param pstate_index pstate to test
134 * \return Returns the processor speed associated with pstate_index
136 double sg_host_get_pstate_speed(sg_host_t host, int pstate_index)
138 return host->getPstateSpeed(pstate_index);
141 /** \ingroup m_host_management
142 * \brief Return the number of cores.
145 * \return the number of cores
147 int sg_host_core_count(sg_host_t host)
149 return host->getCoreCount();
152 double sg_host_get_available_speed(sg_host_t host)
154 return host->pimpl_cpu->get_available_speed();
157 /** @brief Returns the number of power states for a host.
159 * See also @ref plugin_energy.
161 int sg_host_get_nb_pstates(sg_host_t host) {
162 return host->getPstatesCount();
165 /** @brief Gets the pstate at which that host currently runs.
167 * See also @ref plugin_energy.
169 int sg_host_get_pstate(sg_host_t host) {
170 return host->getPstate();
172 /** @brief Sets the pstate at which that host should run.
174 * See also @ref plugin_energy.
176 void sg_host_set_pstate(sg_host_t host,int pstate) {
177 host->setPstate(pstate);
180 /** \ingroup m_host_management
182 * \brief Start the host if it is off
184 * See also #sg_host_is_on() and #sg_host_is_off() to test the current state of the host and @ref plugin_energy
185 * for more info on DVFS.
187 void sg_host_turn_on(sg_host_t host)
192 /** \ingroup m_host_management
194 * \brief Stop the host if it is on
196 * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref plugin_energy
197 * for more info on DVFS.
199 void sg_host_turn_off(sg_host_t host)
204 /** @ingroup m_host_management
205 * @brief Determine if a host is up and running.
207 * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_energy for more
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 sg_host_is_on(sg_host_t host)
218 /** @ingroup m_host_management
219 * @brief Determine if a host is currently off.
221 * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_energy for more
224 int sg_host_is_off(sg_host_t host)
226 return host->isOff();
229 /** @brief Get the properties of an host */
230 xbt_dict_t sg_host_get_properties(sg_host_t host) {
231 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
232 std::map<std::string, std::string>* props = host->getProperties();
233 if (props == nullptr)
235 for (auto const& elm : *props) {
236 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
241 /** \ingroup m_host_management
242 * \brief Returns the value of a given host property
245 * \param name a property name
246 * \return value of a property (or nullptr if property not set)
248 const char *sg_host_get_property_value(sg_host_t host, const char *name)
250 return host->getProperty(name);
253 void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
255 host->setProperty(name, value);
259 * \brief Find a route between two hosts
261 * \param from where from
263 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
265 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
267 std::vector<simgrid::s4u::Link*> vlinks;
268 from->routeTo(to, vlinks, nullptr);
269 for (auto const& link : vlinks)
270 xbt_dynar_push(links, &link);
273 * \brief Find the latency of the route between two hosts
275 * \param from where from
278 double sg_host_route_latency(sg_host_t from, sg_host_t to)
280 std::vector<simgrid::s4u::Link*> vlinks;
282 from->routeTo(to, vlinks, &res);
286 * \brief Find the bandwitdh of the route between two hosts
288 * \param from where from
291 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
293 double min_bandwidth = -1.0;
295 std::vector<simgrid::s4u::Link*> vlinks;
296 from->routeTo(to, vlinks, nullptr);
297 for (auto const& link : vlinks) {
298 double bandwidth = link->bandwidth();
299 if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
300 min_bandwidth = bandwidth;
302 return min_bandwidth;
305 /** @brief Displays debugging information about a host */
306 void sg_host_dump(sg_host_t host)
308 XBT_INFO("Displaying host %s", host->getCname());
309 XBT_INFO(" - speed: %.0f", host->getSpeed());
310 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
311 std::map<std::string, std::string>* props = host->getProperties();
313 if (not props->empty()) {
314 XBT_INFO(" - properties:");
315 for (auto const& elm : *props) {
316 XBT_INFO(" %s->%s", elm.first.c_str(), elm.second.c_str());
321 /** \brief Return the list of actors attached to an host.
324 * \param whereto a dynar in which we should push actors living on that host
326 void sg_host_get_actor_list(sg_host_t host, xbt_dynar_t whereto)
328 for (auto& actor : host->extension<simgrid::simix::Host>()->process_list) {
329 s4u_Actor* p = actor.ciface();
330 xbt_dynar_push(whereto, &p);
334 sg_host_t sg_host_self()
336 smx_actor_t process = SIMIX_process_self();
337 return (process == nullptr) ? nullptr : process->host;