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/surf/HostImpl.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
19 extern std::unordered_map<std::string, simgrid::s4u::Host*>
20 host_list; // FIXME: don't duplicate the content of s4u::Host this way
24 /* copy all names to not modify the map while iterating over it.
26 * Plus, the hosts are destroyed in the lexicographic order to ensure
27 * that the output is reproducible: we don't want to kill them in the
28 * pointer order as it could be platform-dependent, which would break
31 std::vector<std::string> names = std::vector<std::string>();
32 for (auto kv : host_list)
33 names.push_back(kv.second->name());
35 std::sort(names.begin(), names.end());
37 for (auto name : names)
38 host_list.at(name)->destroy();
40 // host_list.clear(); This would be sufficient if the dict would contain smart_ptr. It's now useless
43 size_t sg_host_count()
45 return host_list.size();
47 /** @brief Returns the host list
49 * Uses sg_host_count() to know the array size.
51 * \return an array of \ref sg_host_t containing all the hosts in the platform.
52 * \remark The host order in this array is generally different from the
53 * creation/declaration order in the XML platform (we use a hash table
55 * \see sg_host_count()
57 sg_host_t *sg_host_list() {
58 xbt_assert(sg_host_count() > 0, "There is no host!");
59 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
62 const char *sg_host_get_name(sg_host_t host)
67 void* sg_host_extension_get(sg_host_t host, size_t ext)
69 return host->extension(ext);
72 size_t sg_host_extension_create(void(*deleter)(void*))
74 return simgrid::s4u::Host::extension_create(deleter);
77 sg_host_t sg_host_by_name(const char *name)
79 return simgrid::s4u::Host::by_name_or_null(name);
82 static int hostcmp_voidp(const void* pa, const void* pb)
84 return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->cname(),
85 (*static_cast<simgrid::s4u::Host* const*>(pb))->cname());
88 xbt_dynar_t sg_hosts_as_dynar()
90 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
92 for (auto kv : host_list) {
93 simgrid::s4u::Host* host = kv.second;
94 if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
95 xbt_dynar_push(res, &host);
97 xbt_dynar_sort(res, hostcmp_voidp);
101 // ========= Layering madness ==============*
103 #include "src/surf/cpu_interface.hpp"
105 // ========== User data Layer ==========
106 void *sg_host_user(sg_host_t host) {
107 return host->extension(USER_HOST_LEVEL);
109 void sg_host_user_set(sg_host_t host, void* userdata) {
110 host->extension_set(USER_HOST_LEVEL,userdata);
112 void sg_host_user_destroy(sg_host_t host) {
113 host->extension_set(USER_HOST_LEVEL, nullptr);
116 // ========== MSG Layer ==============
117 msg_host_priv_t sg_host_msg(sg_host_t host) {
118 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
120 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
121 host->extension_set(MSG_HOST_LEVEL, smx_host);
124 // ========== Simix layer =============
125 #include "src/simix/smx_host_private.h"
126 smx_host_priv_t sg_host_simix(sg_host_t host){
127 return host->extension<simgrid::simix::Host>();
130 // ========= storage related functions ============
131 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
132 return host->pimpl_->getMountedStorageList();
135 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
136 return host->pimpl_->getAttachedStorageList();
140 // =========== user-level functions ===============
141 // ================================================
142 /** @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)
150 return host->pimpl_cpu->getAvailableSpeed();
153 /** @brief Returns the number of power states for a host.
155 * See also @ref SURF_plugin_energy.
157 int sg_host_get_nb_pstates(sg_host_t host) {
158 return host->pstatesCount();
161 /** @brief Gets the pstate at which that host currently runs.
163 * See also @ref SURF_plugin_energy.
165 int sg_host_get_pstate(sg_host_t host) {
166 return host->pstate();
168 /** @brief Sets the pstate at which that host should run.
170 * See also @ref SURF_plugin_energy.
172 void sg_host_set_pstate(sg_host_t host,int pstate) {
173 host->setPstate(pstate);
176 /** @brief Get the properties of an host */
177 xbt_dict_t sg_host_get_properties(sg_host_t host) {
178 return host->properties();
181 /** \ingroup m_host_management
182 * \brief Returns the value of a given host property
185 * \param name a property name
186 * \return value of a property (or nullptr if property not set)
188 const char *sg_host_get_property_value(sg_host_t host, const char *name)
190 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
193 /** @brief Displays debugging information about a host */
194 void sg_host_dump(sg_host_t host)
197 xbt_dict_cursor_t cursor=nullptr;
200 XBT_INFO("Displaying host %s", host->cname());
201 XBT_INFO(" - speed: %.0f", host->speed());
202 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
203 props = host->properties();
205 if (!xbt_dict_is_empty(props)){
206 XBT_INFO(" - properties:");
208 xbt_dict_foreach(props,cursor,key,data) {
209 XBT_INFO(" %s->%s",key,data);