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 extern xbt_dict_t host_list; // FIXME:killme don't dupplicate the content of s4u::Host this way
21 xbt_dict_cursor_t cursor = nullptr;
22 const char* name = nullptr;
23 simgrid::s4u::Host* host = nullptr;
24 xbt_dict_foreach(host_list, cursor, name, host) host->destroy();
25 xbt_dict_free(&host_list);
28 size_t sg_host_count()
30 return xbt_dict_length(host_list);
32 /** @brief Returns the host list
34 * Uses sg_host_count() to know the array size.
36 * \return an array of \ref sg_host_t containing all the hosts in the platform.
37 * \remark The host order in this array is generally different from the
38 * creation/declaration order in the XML platform (we use a hash table
40 * \see sg_host_count()
42 sg_host_t *sg_host_list() {
43 xbt_assert(sg_host_count() > 0, "There is no host!");
44 return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
47 const char *sg_host_get_name(sg_host_t host)
49 return host->name().c_str();
52 void* sg_host_extension_get(sg_host_t host, size_t ext)
54 return host->extension(ext);
57 size_t sg_host_extension_create(void(*deleter)(void*))
59 return simgrid::s4u::Host::extension_create(deleter);
62 sg_host_t sg_host_by_name(const char *name)
64 return simgrid::s4u::Host::by_name_or_null(name);
67 xbt_dynar_t sg_hosts_as_dynar()
69 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
71 xbt_dict_cursor_t cursor = nullptr;
72 const char* name = nullptr;
73 simgrid::s4u::Host* host = nullptr;
74 xbt_dict_foreach(host_list, cursor, name, host)
75 if (host && host->pimpl_netcard && host->pimpl_netcard->isHost())
76 xbt_dynar_push(res, &host);
80 // ========= Layering madness ==============*
82 #include "src/surf/cpu_interface.hpp"
83 #include "src/surf/surf_routing.hpp"
85 // ========== User data Layer ==========
86 void *sg_host_user(sg_host_t host) {
87 return host->extension(USER_HOST_LEVEL);
89 void sg_host_user_set(sg_host_t host, void* userdata) {
90 host->extension_set(USER_HOST_LEVEL,userdata);
92 void sg_host_user_destroy(sg_host_t host) {
93 host->extension_set(USER_HOST_LEVEL, nullptr);
96 // ========== MSG Layer ==============
97 msg_host_priv_t sg_host_msg(sg_host_t host) {
98 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
100 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
101 host->extension_set(MSG_HOST_LEVEL, smx_host);
104 // ========== Simix layer =============
105 #include "src/simix/smx_host_private.h"
106 smx_host_priv_t sg_host_simix(sg_host_t host){
107 return host->extension<simgrid::simix::Host>();
110 // ========= storage related functions ============
111 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
112 return host->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
115 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
116 return host->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
120 // =========== user-level functions ===============
121 // ================================================
122 /** @brief Returns the total speed of a host */
123 double sg_host_speed(sg_host_t host)
125 return host->speed();
128 double sg_host_get_available_speed(sg_host_t host)
130 return host->pimpl_cpu->getAvailableSpeed();
133 /** @brief Returns the number of power states for a host.
135 * See also @ref SURF_plugin_energy.
137 int sg_host_get_nb_pstates(sg_host_t host) {
138 return host->pstatesCount();
141 /** @brief Gets the pstate at which that host currently runs.
143 * See also @ref SURF_plugin_energy.
145 int sg_host_get_pstate(sg_host_t host) {
146 return host->pstate();
148 /** @brief Sets the pstate at which that host should run.
150 * See also @ref SURF_plugin_energy.
152 void sg_host_set_pstate(sg_host_t host,int pstate) {
153 host->setPstate(pstate);
156 /** @brief Get the properties of an host */
157 xbt_dict_t sg_host_get_properties(sg_host_t host) {
158 return host->properties();
161 /** \ingroup m_host_management
162 * \brief Returns the value of a given host property
165 * \param name a property name
166 * \return value of a property (or nullptr if property not set)
168 const char *sg_host_get_property_value(sg_host_t host, const char *name)
170 return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
173 /** @brief Displays debugging information about a host */
174 void sg_host_dump(sg_host_t host)
177 xbt_dict_cursor_t cursor=nullptr;
180 XBT_INFO("Displaying host %s", sg_host_get_name(host));
181 XBT_INFO(" - speed: %.0f", host->speed());
182 XBT_INFO(" - available speed: %.2f", sg_host_get_available_speed(host));
183 props = sg_host_get_properties(host);
185 if (!xbt_dict_is_empty(props)){
186 XBT_INFO(" - properties:");
188 xbt_dict_foreach(props,cursor,key,data) {
189 XBT_INFO(" %s->%s",key,data);