Logo AND Algorithmique Numérique Distribuée

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