Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
hello clang, that's another ForEach macro of ours
[simgrid.git] / src / simgrid / host.cpp
1 /* Copyright (c) 2013-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/dict.h"
8 #include "simgrid/host.h"
9 #include <xbt/Extendable.hpp>
10 #include <simgrid/s4u/host.hpp>
11
12 #include "src/surf/HostImpl.hpp"
13 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
16
17 extern xbt_dict_t host_list; // FIXME:killme don't dupplicate the content of s4u::Host this way
18
19 void sg_host_exit()
20 {
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);
26 }
27
28 size_t sg_host_count()
29 {
30   return xbt_dict_length(host_list);
31 }
32 /** @brief Returns the host list
33  *
34  * Uses sg_host_count() to know the array size.
35  *
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
39  * internally).
40  * \see sg_host_count()
41  */
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());
45 }
46
47 const char *sg_host_get_name(sg_host_t host)
48 {
49   return host->name().c_str();
50 }
51
52 void* sg_host_extension_get(sg_host_t host, size_t ext)
53 {
54   return host->extension(ext);
55 }
56
57 size_t sg_host_extension_create(void(*deleter)(void*))
58 {
59   return simgrid::s4u::Host::extension_create(deleter);
60 }
61
62 sg_host_t sg_host_by_name(const char *name)
63 {
64   return simgrid::s4u::Host::by_name_or_null(name);
65 }
66
67 xbt_dynar_t sg_hosts_as_dynar()
68 {
69   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
70
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);
77   return res;
78 }
79
80 // ========= Layering madness ==============*
81
82 #include "src/surf/cpu_interface.hpp"
83 #include "src/surf/surf_routing.hpp"
84
85 // ========== User data Layer ==========
86 void *sg_host_user(sg_host_t host) {
87   return host->extension(USER_HOST_LEVEL);
88 }
89 void sg_host_user_set(sg_host_t host, void* userdata) {
90   host->extension_set(USER_HOST_LEVEL,userdata);
91 }
92 void sg_host_user_destroy(sg_host_t host) {
93   host->extension_set(USER_HOST_LEVEL, nullptr);
94 }
95
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);
99 }
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);
102 }
103
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>();
108 }
109
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();
113 }
114
115 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
116   return host->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
117 }
118
119
120 // =========== user-level functions ===============
121 // ================================================
122 /** @brief Returns the total speed of a host */
123 double sg_host_speed(sg_host_t host)
124 {
125   return host->speed();
126 }
127
128 double sg_host_get_available_speed(sg_host_t host)
129 {
130   return host->pimpl_cpu->getAvailableSpeed();
131 }
132
133 /** @brief Returns the number of power states for a host.
134  *
135  *  See also @ref SURF_plugin_energy.
136  */
137 int sg_host_get_nb_pstates(sg_host_t host) {
138   return host->pstatesCount();
139 }
140
141 /** @brief Gets the pstate at which that host currently runs.
142  *
143  *  See also @ref SURF_plugin_energy.
144  */
145 int sg_host_get_pstate(sg_host_t host) {
146   return host->pstate();
147 }
148 /** @brief Sets the pstate at which that host should run.
149  *
150  *  See also @ref SURF_plugin_energy.
151  */
152 void sg_host_set_pstate(sg_host_t host,int pstate) {
153   host->setPstate(pstate);
154 }
155
156 /** @brief Get the properties of an host */
157 xbt_dict_t sg_host_get_properties(sg_host_t host) {
158   return host->properties();
159 }
160
161 /** \ingroup m_host_management
162  * \brief Returns the value of a given host property
163  *
164  * \param host a host
165  * \param name a property name
166  * \return value of a property (or nullptr if property not set)
167 */
168 const char *sg_host_get_property_value(sg_host_t host, const char *name)
169 {
170   return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
171 }
172
173 /** @brief Displays debugging information about a host */
174 void sg_host_dump(sg_host_t host)
175 {
176   xbt_dict_t props;
177   xbt_dict_cursor_t cursor=nullptr;
178   char *key,*data;
179
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);
184
185   if (!xbt_dict_is_empty(props)){
186     XBT_INFO("  - properties:");
187
188     xbt_dict_foreach(props,cursor,key,data) {
189       XBT_INFO("    %s->%s",key,data);
190     }
191   }
192 }