Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stop using sg_host_list() from C++, and improve its implementation
[simgrid.git] / src / simgrid / host.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <algorithm>
7 #include <vector>
8
9 #include "simgrid/host.h"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "xbt/Extendable.hpp"
13 #include "xbt/dict.h"
14
15 #include "src/kernel/routing/NetPoint.hpp"
16 #include "src/simix/smx_host_private.hpp"
17 #include "src/surf/HostImpl.hpp"
18 #include "src/surf/cpu_interface.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
21
22 extern "C" {
23
24 size_t sg_host_count()
25 {
26   return simgrid::s4u::Engine::getInstance()->getHostCount();
27 }
28 /** @brief Returns the host list
29  *
30  * Uses sg_host_count() to know the array size.
31  *
32  * \return an array of \ref sg_host_t containing all the hosts in the platform.
33  * \remark The host order in this array is generally different from the
34  * creation/declaration order in the XML platform (we use a hash table
35  * internally).
36  * \see sg_host_count()
37  */
38 sg_host_t *sg_host_list() {
39   xbt_assert(sg_host_count() > 0, "There is no host!");
40   std::vector<simgrid::s4u::Host*> hosts;
41   simgrid::s4u::Engine::getInstance()->getHostList(&hosts);
42
43   sg_host_t* res = (sg_host_t*)malloc(sizeof(sg_host_t) * hosts.size());
44   memcpy(res, hosts.data(), sizeof(sg_host_t) * hosts.size());
45
46   return res;
47 }
48
49 const char *sg_host_get_name(sg_host_t host)
50 {
51   return host->getCname();
52 }
53
54 void* sg_host_extension_get(sg_host_t host, size_t ext)
55 {
56   return host->extension(ext);
57 }
58
59 size_t sg_host_extension_create(void(*deleter)(void*))
60 {
61   return simgrid::s4u::Host::extension_create(deleter);
62 }
63
64 sg_host_t sg_host_by_name(const char *name)
65 {
66   return simgrid::s4u::Host::by_name_or_null(name);
67 }
68
69 static int hostcmp_voidp(const void* pa, const void* pb)
70 {
71   return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->getCname(),
72                 (*static_cast<simgrid::s4u::Host* const*>(pb))->getCname());
73 }
74
75 xbt_dynar_t sg_hosts_as_dynar()
76 {
77   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
78
79   std::vector<simgrid::s4u::Host*> list;
80   simgrid::s4u::Engine::getInstance()->getHostList(&list);
81
82   for (auto const& host : list) {
83     if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
84       xbt_dynar_push(res, &host);
85   }
86   xbt_dynar_sort(res, hostcmp_voidp);
87   return res;
88 }
89
90 // ========= Layering madness ==============*
91
92 // ========== User data Layer ==========
93 void *sg_host_user(sg_host_t host) {
94   return host->extension(USER_HOST_LEVEL);
95 }
96 void sg_host_user_set(sg_host_t host, void* userdata) {
97   host->extension_set(USER_HOST_LEVEL,userdata);
98 }
99 void sg_host_user_destroy(sg_host_t host) {
100   host->extension_set(USER_HOST_LEVEL, nullptr);
101 }
102
103 // ========= storage related functions ============
104 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
105   xbt_assert((host != nullptr), "Invalid parameters");
106   xbt_dict_t res = xbt_dict_new_homogeneous(nullptr);
107   for (auto const& elm : host->getMountedStorages()) {
108     const char* mount_name = elm.first.c_str();
109     sg_storage_t storage   = elm.second;
110     xbt_dict_set(res, mount_name, (void*)storage->getCname(), nullptr);
111   }
112
113   return res;
114 }
115
116 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
117   std::vector<const char*>* storage_vector = new std::vector<const char*>();
118   xbt_dynar_t storage_dynar = xbt_dynar_new(sizeof(const char*), nullptr);
119   host->getAttachedStorages(storage_vector);
120   for (auto const& name : *storage_vector)
121     xbt_dynar_push(storage_dynar, &name);
122   delete storage_vector;
123   return storage_dynar;
124 }
125
126 // =========== user-level functions ===============
127 // ================================================
128 /** @brief Returns the total speed of a host */
129 double sg_host_speed(sg_host_t host)
130 {
131   return host->getSpeed();
132 }
133
134 /** \ingroup m_host_management
135  * \brief Return the number of cores.
136  *
137  * \param host a host
138  * \return the number of cores
139  */
140 int sg_host_core_count(sg_host_t host)
141 {
142   return host->getCoreCount();
143 }
144
145 double sg_host_get_available_speed(sg_host_t host)
146 {
147   return host->pimpl_cpu->getAvailableSpeed();
148 }
149
150 /** @brief Returns the number of power states for a host.
151  *
152  *  See also @ref plugin_energy.
153  */
154 int sg_host_get_nb_pstates(sg_host_t host) {
155   return host->getPstatesCount();
156 }
157
158 /** @brief Gets the pstate at which that host currently runs.
159  *
160  *  See also @ref plugin_energy.
161  */
162 int sg_host_get_pstate(sg_host_t host) {
163   return host->getPstate();
164 }
165 /** @brief Sets the pstate at which that host should run.
166  *
167  *  See also @ref plugin_energy.
168  */
169 void sg_host_set_pstate(sg_host_t host,int pstate) {
170   host->setPstate(pstate);
171 }
172
173 /** \ingroup m_host_management
174  *
175  * \brief Start the host if it is off
176  *
177  * See also #sg_host_is_on() and #sg_host_is_off() to test the current state of the host and @ref plugin_energy
178  * for more info on DVFS.
179  */
180 void sg_host_turn_on(sg_host_t host)
181 {
182   host->turnOn();
183 }
184
185 /** \ingroup m_host_management
186  *
187  * \brief Stop the host if it is on
188  *
189  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref plugin_energy
190  * for more info on DVFS.
191  */
192 void sg_host_turn_off(sg_host_t host)
193 {
194   host->turnOff();
195 }
196
197 /** @ingroup m_host_management
198  * @brief Determine if a host is up and running.
199  *
200  * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_energy for more
201  * info on DVFS.
202  *
203  * @param host host to test
204  * @return Returns true if the host is up and running, and false if it's currently down
205  */
206 int sg_host_is_on(sg_host_t host)
207 {
208   return host->isOn();
209 }
210
211 /** @ingroup m_host_management
212  * @brief Determine if a host is currently off.
213  *
214  * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_energy for more
215  * info on DVFS.
216  */
217 int sg_host_is_off(sg_host_t host)
218 {
219   return host->isOff();
220 }
221
222 /** @brief Get the properties of an host */
223 xbt_dict_t sg_host_get_properties(sg_host_t host) {
224   xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
225   std::map<std::string, std::string>* props = host->getProperties();
226   if (props == nullptr)
227     return nullptr;
228   for (auto const& elm : *props) {
229     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
230   }
231   return as_dict;
232 }
233
234 /** \ingroup m_host_management
235  * \brief Returns the value of a given host property
236  *
237  * \param host a host
238  * \param name a property name
239  * \return value of a property (or nullptr if property not set)
240 */
241 const char *sg_host_get_property_value(sg_host_t host, const char *name)
242 {
243   return host->getProperty(name);
244 }
245
246 void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
247 {
248   host->setProperty(name, value);
249 }
250
251 /**
252  * \brief Find a route between two hosts
253  *
254  * \param from where from
255  * \param to where to
256  * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
257  */
258 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
259 {
260   std::vector<simgrid::s4u::Link*> vlinks;
261   from->routeTo(to, vlinks, nullptr);
262   for (auto const& link : vlinks)
263     xbt_dynar_push(links, &link);
264 }
265 /**
266  * \brief Find the latency of the route between two hosts
267  *
268  * \param from where from
269  * \param to where to
270  */
271 double sg_host_route_latency(sg_host_t from, sg_host_t to)
272 {
273   std::vector<simgrid::s4u::Link*> vlinks;
274   double res = 0;
275   from->routeTo(to, vlinks, &res);
276   return res;
277 }
278 /**
279  * \brief Find the bandwitdh of the route between two hosts
280  *
281  * \param from where from
282  * \param to where to
283  */
284 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
285 {
286   double min_bandwidth = -1.0;
287
288   std::vector<simgrid::s4u::Link*> vlinks;
289   from->routeTo(to, vlinks, nullptr);
290   for (auto const& link : vlinks) {
291     double bandwidth = link->bandwidth();
292     if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
293       min_bandwidth = bandwidth;
294   }
295   return min_bandwidth;
296 }
297
298 /** @brief Displays debugging information about a host */
299 void sg_host_dump(sg_host_t host)
300 {
301   XBT_INFO("Displaying host %s", host->getCname());
302   XBT_INFO("  - speed: %.0f", host->getSpeed());
303   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
304   std::map<std::string, std::string>* props = host->getProperties();
305
306   if (not props->empty()) {
307     XBT_INFO("  - properties:");
308     for (auto const& elm : *props) {
309       XBT_INFO("    %s->%s", elm.first.c_str(), elm.second.c_str());
310     }
311   }
312 }
313
314 } // extern "C"