Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
seriously, me
[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 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
12
13 #include "src/simix/smx_private.hpp"
14 #include "src/surf/host_interface.hpp"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
17
18 size_t sg_host_count()
19 {
20   return xbt_dict_length(host_list);
21 }
22 /** @brief Returns the host list
23  *
24  * Uses sg_host_count() to know the array size.
25  *
26  * \return an array of \ref sg_host_t containing all the hosts in the platform.
27  * \remark The host order in this array is generally different from the
28  * creation/declaration order in the XML platform (we use a hash table
29  * internally).
30  * \see sg_host_count()
31  */
32 sg_host_t *sg_host_list(void) {
33   xbt_assert(sg_host_count() > 0, "There is no host!");
34   return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
35 }
36
37 const char *sg_host_get_name(sg_host_t host)
38 {
39   return host->name().c_str();
40 }
41
42 void* sg_host_extension_get(sg_host_t host, size_t ext)
43 {
44   return host->extension(ext);
45 }
46
47 size_t sg_host_extension_create(void(*deleter)(void*))
48 {
49   return simgrid::s4u::Host::extension_create(deleter);
50 }
51
52 sg_host_t sg_host_by_name(const char *name)
53 {
54   return simgrid::s4u::Host::by_name_or_null(name);
55 }
56
57 sg_host_t sg_host_by_name_or_create(const char *name)
58 {
59   return simgrid::s4u::Host::by_name_or_create(name);
60 }
61
62 xbt_dynar_t sg_hosts_as_dynar(void)
63 {
64   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
65
66   xbt_dict_cursor_t cursor = nullptr;
67   const char* name = nullptr;
68   simgrid::s4u::Host* host = nullptr;
69   xbt_dict_foreach(host_list, cursor, name, host)
70     if (host && host->pimpl_netcard && host->pimpl_netcard->getRcType() == SURF_NETWORK_ELEMENT_HOST)
71        xbt_dynar_push(res, &host);
72   return res;
73 }
74
75 // ========= Layering madness ==============*
76
77 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme by initializing that level in msg when used
78 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme by initializing that level in simix when used
79 #include "src/surf/cpu_interface.hpp"
80 #include "src/surf/surf_routing.hpp"
81
82 void sg_host_init()
83 {
84   MSG_HOST_LEVEL = simgrid::s4u::Host::extension_create([](void *p) {
85     __MSG_host_priv_free((msg_host_priv_t) p);
86   });
87
88   ROUTING_HOST_LEVEL = simgrid::s4u::Host::extension_create([](void *p) {
89     delete static_cast<simgrid::surf::NetCard*>(p);
90   });
91
92   SD_HOST_LEVEL = simgrid::s4u::Host::extension_create(NULL);
93   SIMIX_HOST_LEVEL = simgrid::s4u::Host::extension_create(SIMIX_host_destroy);
94   USER_HOST_LEVEL = simgrid::s4u::Host::extension_create(NULL);
95 }
96
97 // ========== User data Layer ==========
98 void *sg_host_user(sg_host_t host) {
99   return host->extension(USER_HOST_LEVEL);
100 }
101 void sg_host_user_set(sg_host_t host, void* userdata) {
102   host->extension_set(USER_HOST_LEVEL,userdata);
103 }
104 void sg_host_user_destroy(sg_host_t host) {
105   host->extension_set(USER_HOST_LEVEL, nullptr);
106 }
107
108 // ========== MSG Layer ==============
109 msg_host_priv_t sg_host_msg(sg_host_t host) {
110   return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
111 }
112 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
113   host->extension_set(MSG_HOST_LEVEL, smx_host);
114 }
115
116 // ========== Simix layer =============
117 smx_host_priv_t sg_host_simix(sg_host_t host){
118   return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
119 }
120 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
121   host->extension_set(SIMIX_HOST_LEVEL, smx_host);
122 }
123 void sg_host_simix_destroy(sg_host_t host) {
124   host->extension_set(SIMIX_HOST_LEVEL, nullptr);
125 }
126
127 // ========= storage related functions ============
128 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
129   return host->extension<simgrid::surf::Host>()->getMountedStorageList();
130 }
131
132 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
133   return host->extension<simgrid::surf::Host>()->getAttachedStorageList();
134 }
135
136
137 // =========== user-level functions ===============
138 // ================================================
139
140 /** @brief Returns the total speed of a host
141  */
142 double sg_host_speed(sg_host_t host)
143 {
144   return host->speed();
145 }
146
147 double sg_host_get_available_speed(sg_host_t host){
148   return surf_host_get_available_speed(host);
149 }
150 /** @brief Returns the number of cores of a host
151 */
152 int sg_host_core_count(sg_host_t host) {
153   return host->core_count();
154 }
155
156 /** @brief Returns the state of a host.
157  *  @return 1 if the host is active or 0 if it has crashed.
158  */
159 int sg_host_is_on(sg_host_t host) {
160   return host->is_on();
161 }
162
163 /** @brief Returns the number of power states for a host.
164  *
165  *  See also @ref SURF_plugin_energy.
166  */
167 int sg_host_get_nb_pstates(sg_host_t host) {
168   return host->pstates_count();
169 }
170
171 /** @brief Gets the pstate at which that host currently runs.
172  *
173  *  See also @ref SURF_plugin_energy.
174  */
175 int sg_host_get_pstate(sg_host_t host) {
176   return host->pstate();
177 }
178 /** @brief Sets the pstate at which that host should run.
179  *
180  *  See also @ref SURF_plugin_energy.
181  */
182 void sg_host_set_pstate(sg_host_t host,int pstate) {
183   host->set_pstate(pstate);
184 }
185
186 /** @brief Get the properties of an host */
187 xbt_dict_t sg_host_get_properties(sg_host_t host) {
188   return host->properties();
189 }
190
191 /** \ingroup m_host_management
192  * \brief Returns the value of a given host property
193  *
194  * \param host a host
195  * \param name a property name
196  * \return value of a property (or NULL if property not set)
197 */
198 const char *sg_host_get_property_value(sg_host_t host, const char *name)
199 {
200   return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
201 }
202
203 /** @brief Displays debugging informations about a host */
204 void sg_host_dump(sg_host_t host)
205 {
206   xbt_dict_t props;
207   xbt_dict_cursor_t cursor=NULL;
208   char *key,*data;
209
210   XBT_INFO("Displaying host %s", sg_host_get_name(host));
211   XBT_INFO("  - speed: %.0f", sg_host_speed(host));
212   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
213   props = sg_host_get_properties(host);
214
215   if (!xbt_dict_is_empty(props)){
216     XBT_INFO("  - properties:");
217
218     xbt_dict_foreach(props,cursor,key,data) {
219       XBT_INFO("    %s->%s",key,data);
220     }
221   }
222 }