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. */
8 #include "simgrid/host.h"
9 #include <xbt/Extendable.hpp>
10 #include <simgrid/s4u/host.hpp>
12 #include "src/surf/HostImpl.hpp"
13 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
17 size_t sg_host_count()
19 return xbt_dict_length(host_list);
21 /** @brief Returns the host list
23 * Uses sg_host_count() to know the array size.
25 * \return an array of \ref sg_host_t containing all the hosts in the platform.
26 * \remark The host order in this array is generally different from the
27 * creation/declaration order in the XML platform (we use a hash table
29 * \see sg_host_count()
31 sg_host_t *sg_host_list(void) {
32 xbt_assert(sg_host_count() > 0, "There is no host!");
33 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
36 const char *sg_host_get_name(sg_host_t host)
38 return host->name().c_str();
41 void* sg_host_extension_get(sg_host_t host, size_t ext)
43 return host->extension(ext);
46 size_t sg_host_extension_create(void(*deleter)(void*))
48 return simgrid::s4u::Host::extension_create(deleter);
51 sg_host_t sg_host_by_name(const char *name)
53 return simgrid::s4u::Host::by_name_or_null(name);
56 sg_host_t sg_host_by_name_or_create(const char *name)
58 return simgrid::s4u::Host::by_name_or_create(name);
61 xbt_dynar_t sg_hosts_as_dynar(void)
63 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
65 xbt_dict_cursor_t cursor = nullptr;
66 const char* name = nullptr;
67 simgrid::s4u::Host* host = nullptr;
68 xbt_dict_foreach(host_list, cursor, name, host)
69 if (host && host->pimpl_netcard && host->pimpl_netcard->isHost())
70 xbt_dynar_push(res, &host);
74 // ========= Layering madness ==============*
76 #include "src/surf/cpu_interface.hpp"
77 #include "src/surf/surf_routing.hpp"
79 // ========== User data Layer ==========
80 void *sg_host_user(sg_host_t host) {
81 return host->extension(USER_HOST_LEVEL);
83 void sg_host_user_set(sg_host_t host, void* userdata) {
84 host->extension_set(USER_HOST_LEVEL,userdata);
86 void sg_host_user_destroy(sg_host_t host) {
87 host->extension_set(USER_HOST_LEVEL, nullptr);
90 // ========== MSG Layer ==============
91 msg_host_priv_t sg_host_msg(sg_host_t host) {
92 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
94 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
95 host->extension_set(MSG_HOST_LEVEL, smx_host);
98 // ========== Simix layer =============
99 smx_host_priv_t sg_host_simix(sg_host_t host){
100 return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
102 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
103 host->extension_set(SIMIX_HOST_LEVEL, smx_host);
105 void sg_host_simix_destroy(sg_host_t host) {
106 host->extension_set(SIMIX_HOST_LEVEL, nullptr);
109 // ========= storage related functions ============
110 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
111 return host->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
114 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
115 return host->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
119 // =========== user-level functions ===============
120 // ================================================
121 /** @brief Returns the total speed of a host */
122 double sg_host_speed(sg_host_t host)
124 return host->speed();
127 double sg_host_get_available_speed(sg_host_t host)
129 return host->pimpl_cpu->getAvailableSpeed();
132 /** @brief Returns the number of power states for a host.
134 * See also @ref SURF_plugin_energy.
136 int sg_host_get_nb_pstates(sg_host_t host) {
137 return host->pstatesCount();
140 /** @brief Gets the pstate at which that host currently runs.
142 * See also @ref SURF_plugin_energy.
144 int sg_host_get_pstate(sg_host_t host) {
145 return host->pstate();
147 /** @brief Sets the pstate at which that host should run.
149 * See also @ref SURF_plugin_energy.
151 void sg_host_set_pstate(sg_host_t host,int pstate) {
152 host->setPstate(pstate);
155 /** @brief Get the properties of an host */
156 xbt_dict_t sg_host_get_properties(sg_host_t host) {
157 return host->properties();
160 /** \ingroup m_host_management
161 * \brief Returns the value of a given host property
164 * \param name a property name
165 * \return value of a property (or nullptr if property not set)
167 const char *sg_host_get_property_value(sg_host_t host, const char *name)
169 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
172 /** @brief Displays debugging information about a host */
173 void sg_host_dump(sg_host_t host)
176 xbt_dict_cursor_t cursor=nullptr;
179 XBT_INFO("Displaying host %s", sg_host_get_name(host));
180 XBT_INFO(" - speed: %.0f", host->speed());
181 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
182 props = sg_host_get_properties(host);
184 if (!xbt_dict_is_empty(props)){
185 XBT_INFO(" - properties:");
187 xbt_dict_foreach(props,cursor,key,data) {
188 XBT_INFO(" %s->%s",key,data);