Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
complete a bit the C wrapper of s4u::Host
[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 <vector>
8
9 #include "xbt/dict.h"
10 #include "simgrid/host.h"
11 #include <xbt/Extendable.hpp>
12 #include <simgrid/s4u/host.hpp>
13
14 #include "src/kernel/routing/NetPoint.hpp"
15 #include "src/surf/HostImpl.hpp"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
18
19 extern std::unordered_map<std::string, simgrid::s4u::Host*>
20     host_list; // FIXME: don't duplicate the content of s4u::Host this way
21
22 void sg_host_exit()
23 {
24   /* copy all names to not modify the map while iterating over it.
25    *
26    * Plus, the hosts are destroyed in the lexicographic order to ensure
27    * that the output is reproducible: we don't want to kill them in the
28    * pointer order as it could be platform-dependent, which would break
29    * the tests.
30    */
31   std::vector<std::string> names = std::vector<std::string>();
32   for (auto kv : host_list)
33     names.push_back(kv.second->name());
34
35   std::sort(names.begin(), names.end());
36
37   for (auto name : names)
38     host_list.at(name)->destroy();
39
40   // host_list.clear(); This would be sufficient if the dict would contain smart_ptr. It's now useless
41 }
42
43 size_t sg_host_count()
44 {
45   return host_list.size();
46 }
47 /** @brief Returns the host list
48  *
49  * Uses sg_host_count() to know the array size.
50  *
51  * \return an array of \ref sg_host_t containing all the hosts in the platform.
52  * \remark The host order in this array is generally different from the
53  * creation/declaration order in the XML platform (we use a hash table
54  * internally).
55  * \see sg_host_count()
56  */
57 sg_host_t *sg_host_list() {
58   xbt_assert(sg_host_count() > 0, "There is no host!");
59   return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
60 }
61
62 const char *sg_host_get_name(sg_host_t host)
63 {
64   return host->cname();
65 }
66
67 void* sg_host_extension_get(sg_host_t host, size_t ext)
68 {
69   return host->extension(ext);
70 }
71
72 size_t sg_host_extension_create(void(*deleter)(void*))
73 {
74   return simgrid::s4u::Host::extension_create(deleter);
75 }
76
77 sg_host_t sg_host_by_name(const char *name)
78 {
79   return simgrid::s4u::Host::by_name_or_null(name);
80 }
81
82 static int hostcmp_voidp(const void* pa, const void* pb)
83 {
84   return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->cname(),
85                 (*static_cast<simgrid::s4u::Host* const*>(pb))->cname());
86 }
87
88 xbt_dynar_t sg_hosts_as_dynar()
89 {
90   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
91
92   for (auto kv : host_list) {
93     simgrid::s4u::Host* host = kv.second;
94     if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
95       xbt_dynar_push(res, &host);
96   }
97   xbt_dynar_sort(res, hostcmp_voidp);
98   return res;
99 }
100
101 // ========= Layering madness ==============*
102
103 #include "src/surf/cpu_interface.hpp"
104
105 // ========== User data Layer ==========
106 void *sg_host_user(sg_host_t host) {
107   return host->extension(USER_HOST_LEVEL);
108 }
109 void sg_host_user_set(sg_host_t host, void* userdata) {
110   host->extension_set(USER_HOST_LEVEL,userdata);
111 }
112 void sg_host_user_destroy(sg_host_t host) {
113   host->extension_set(USER_HOST_LEVEL, nullptr);
114 }
115
116 // ========== MSG Layer ==============
117 msg_host_priv_t sg_host_msg(sg_host_t host) {
118   return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
119 }
120 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
121   host->extension_set(MSG_HOST_LEVEL, smx_host);
122 }
123
124 // ========== Simix layer =============
125 #include "src/simix/smx_host_private.h"
126 smx_host_priv_t sg_host_simix(sg_host_t host){
127   return host->extension<simgrid::simix::Host>();
128 }
129
130 // ========= storage related functions ============
131 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
132   return host->pimpl_->getMountedStorageList();
133 }
134
135 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
136   return host->pimpl_->getAttachedStorageList();
137 }
138
139
140 // =========== user-level functions ===============
141 // ================================================
142 /** @brief Returns the total speed of a host */
143 double sg_host_speed(sg_host_t host)
144 {
145   return host->speed();
146 }
147
148 double sg_host_get_available_speed(sg_host_t host)
149 {
150   return host->pimpl_cpu->getAvailableSpeed();
151 }
152
153 /** @brief Returns the number of power states for a host.
154  *
155  *  See also @ref SURF_plugin_energy.
156  */
157 int sg_host_get_nb_pstates(sg_host_t host) {
158   return host->pstatesCount();
159 }
160
161 /** @brief Gets the pstate at which that host currently runs.
162  *
163  *  See also @ref SURF_plugin_energy.
164  */
165 int sg_host_get_pstate(sg_host_t host) {
166   return host->pstate();
167 }
168 /** @brief Sets the pstate at which that host should run.
169  *
170  *  See also @ref SURF_plugin_energy.
171  */
172 void sg_host_set_pstate(sg_host_t host,int pstate) {
173   host->setPstate(pstate);
174 }
175
176 /** @brief Get the properties of an host */
177 xbt_dict_t sg_host_get_properties(sg_host_t host) {
178   return host->properties();
179 }
180
181 /** \ingroup m_host_management
182  * \brief Returns the value of a given host property
183  *
184  * \param host a host
185  * \param name a property name
186  * \return value of a property (or nullptr if property not set)
187 */
188 const char *sg_host_get_property_value(sg_host_t host, const char *name)
189 {
190   return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
191 }
192 /**
193  * \brief Find a route between two hosts
194  *
195  * \param from where from
196  * \param to where to
197  * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
198  */
199 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
200 {
201   std::vector<simgrid::s4u::Link*> vlinks;
202   from->routeTo(to, &vlinks, nullptr);
203   for (auto link : vlinks)
204     xbt_dynar_push(links, &link);
205 }
206 /**
207  * \brief Find the latency of the route between two hosts
208  *
209  * \param from where from
210  * \param to where to
211  */
212 double sg_host_route_latency(sg_host_t from, sg_host_t to)
213 {
214   std::vector<simgrid::s4u::Link*> vlinks;
215   double res = 0;
216   from->routeTo(to, &vlinks, &res);
217   return res;
218 }
219 /**
220  * \brief Find the bandwitdh of the route between two hosts
221  *
222  * \param from where from
223  * \param to where to
224  */
225 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
226 {
227   std::vector<simgrid::s4u::Link*> vlinks;
228   from->routeTo(to, &vlinks, nullptr);
229   double res = 0;
230   for (auto link : vlinks)
231     res += link->bandwidth();
232   return res;
233 }
234
235 /** @brief Displays debugging information about a host */
236 void sg_host_dump(sg_host_t host)
237 {
238   xbt_dict_t props;
239   xbt_dict_cursor_t cursor=nullptr;
240   char *key,*data;
241
242   XBT_INFO("Displaying host %s", host->cname());
243   XBT_INFO("  - speed: %.0f", host->speed());
244   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
245   props = host->properties();
246
247   if (!xbt_dict_is_empty(props)){
248     XBT_INFO("  - properties:");
249
250     xbt_dict_foreach(props,cursor,key,data) {
251       XBT_INFO("    %s->%s",key,data);
252     }
253   }
254 }