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/surf/HostImpl.hpp"
15 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
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 dupplicate 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)
64 return host->name().c_str();
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))->name().c_str(),
85 (*static_cast<simgrid::s4u::Host* const*>(pb))->name().c_str());
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_netcard && host->pimpl_netcard->isHost())
95 xbt_dynar_push(res, &host);
97 xbt_dynar_sort(res, hostcmp_voidp);
101 // ========= Layering madness ==============*
103 #include "src/surf/cpu_interface.hpp"
104 #include "src/surf/surf_routing.hpp"
106 // ========== User data Layer ==========
107 void *sg_host_user(sg_host_t host) {
108 return host->extension(USER_HOST_LEVEL);
110 void sg_host_user_set(sg_host_t host, void* userdata) {
111 host->extension_set(USER_HOST_LEVEL,userdata);
113 void sg_host_user_destroy(sg_host_t host) {
114 host->extension_set(USER_HOST_LEVEL, nullptr);
117 // ========== MSG Layer ==============
118 msg_host_priv_t sg_host_msg(sg_host_t host) {
119 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
121 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
122 host->extension_set(MSG_HOST_LEVEL, smx_host);
125 // ========== Simix layer =============
126 #include "src/simix/smx_host_private.h"
127 smx_host_priv_t sg_host_simix(sg_host_t host){
128 return host->extension<simgrid::simix::Host>();
131 // ========= storage related functions ============
132 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
133 return host->pimpl_->getMountedStorageList();
136 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
137 return host->pimpl_->getAttachedStorageList();
141 // =========== user-level functions ===============
142 // ================================================
143 /** @brief Returns the total speed of a host */
144 double sg_host_speed(sg_host_t host)
146 return host->speed();
149 double sg_host_get_available_speed(sg_host_t host)
151 return host->pimpl_cpu->getAvailableSpeed();
154 /** @brief Returns the number of power states for a host.
156 * See also @ref SURF_plugin_energy.
158 int sg_host_get_nb_pstates(sg_host_t host) {
159 return host->pstatesCount();
162 /** @brief Gets the pstate at which that host currently runs.
164 * See also @ref SURF_plugin_energy.
166 int sg_host_get_pstate(sg_host_t host) {
167 return host->pstate();
169 /** @brief Sets the pstate at which that host should run.
171 * See also @ref SURF_plugin_energy.
173 void sg_host_set_pstate(sg_host_t host,int pstate) {
174 host->setPstate(pstate);
177 /** @brief Get the properties of an host */
178 xbt_dict_t sg_host_get_properties(sg_host_t host) {
179 return host->properties();
182 /** \ingroup m_host_management
183 * \brief Returns the value of a given host property
186 * \param name a property name
187 * \return value of a property (or nullptr if property not set)
189 const char *sg_host_get_property_value(sg_host_t host, const char *name)
191 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
194 /** @brief Displays debugging information about a host */
195 void sg_host_dump(sg_host_t host)
198 xbt_dict_cursor_t cursor=nullptr;
201 XBT_INFO("Displaying host %s", sg_host_get_name(host));
202 XBT_INFO(" - speed: %.0f", host->speed());
203 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
204 props = sg_host_get_properties(host);
206 if (!xbt_dict_is_empty(props)){
207 XBT_INFO(" - properties:");
209 xbt_dict_foreach(props,cursor,key,data) {
210 XBT_INFO(" %s->%s",key,data);