Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move last functions in msg_host.cpp to the S4U C interface
[simgrid.git] / src / simgrid / host.cpp
1 /* Copyright (c) 2013-2018. 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 "simgrid/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 = simgrid::s4u::Engine::getInstance()->getAllHosts();
41
42   sg_host_t* res = (sg_host_t*)malloc(sizeof(sg_host_t) * hosts.size());
43   memcpy(res, hosts.data(), sizeof(sg_host_t) * hosts.size());
44
45   return res;
46 }
47
48 const char *sg_host_get_name(sg_host_t host)
49 {
50   return host->getCname();
51 }
52
53 void* sg_host_extension_get(sg_host_t host, size_t ext)
54 {
55   return host->extension(ext);
56 }
57
58 size_t sg_host_extension_create(void(*deleter)(void*))
59 {
60   return simgrid::s4u::Host::extension_create(deleter);
61 }
62
63 sg_host_t sg_host_by_name(const char *name)
64 {
65   return simgrid::s4u::Host::by_name_or_null(name);
66 }
67
68 static int hostcmp_voidp(const void* pa, const void* pb)
69 {
70   return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->getCname(),
71                 (*static_cast<simgrid::s4u::Host* const*>(pb))->getCname());
72 }
73
74 xbt_dynar_t sg_hosts_as_dynar()
75 {
76   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
77
78   std::vector<simgrid::s4u::Host*> list = simgrid::s4u::Engine::getInstance()->getAllHosts();
79
80   for (auto const& host : list) {
81     if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
82       xbt_dynar_push(res, &host);
83   }
84   xbt_dynar_sort(res, hostcmp_voidp);
85   return res;
86 }
87
88 // ========= Layering madness ==============*
89
90 // ========== User data Layer ==========
91 void *sg_host_user(sg_host_t host) {
92   return host->extension(USER_HOST_LEVEL);
93 }
94 void sg_host_user_set(sg_host_t host, void* userdata) {
95   host->extension_set(USER_HOST_LEVEL,userdata);
96 }
97 void sg_host_user_destroy(sg_host_t host) {
98   host->extension_set(USER_HOST_LEVEL, nullptr);
99 }
100
101 // ========= storage related functions ============
102 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
103   xbt_assert((host != nullptr), "Invalid parameters");
104   xbt_dict_t res = xbt_dict_new_homogeneous(nullptr);
105   for (auto const& elm : host->getMountedStorages()) {
106     const char* mount_name = elm.first.c_str();
107     sg_storage_t storage   = elm.second;
108     xbt_dict_set(res, mount_name, (void*)storage->getCname(), nullptr);
109   }
110
111   return res;
112 }
113
114 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
115   std::vector<const char*>* storage_vector = new std::vector<const char*>();
116   xbt_dynar_t storage_dynar = xbt_dynar_new(sizeof(const char*), nullptr);
117   host->getAttachedStorages(storage_vector);
118   for (auto const& name : *storage_vector)
119     xbt_dynar_push(storage_dynar, &name);
120   delete storage_vector;
121   return storage_dynar;
122 }
123
124 // =========== user-level functions ===============
125 // ================================================
126 /** @brief Returns the total speed of a host */
127 double sg_host_speed(sg_host_t host)
128 {
129   return host->getSpeed();
130 }
131
132 /** \brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref plugin_energy.
133  *
134  * \param  host host to test
135  * \param pstate_index pstate to test
136  * \return Returns the processor speed associated with pstate_index
137  */
138 double sg_host_get_pstate_speed(sg_host_t host, int pstate_index)
139 {
140   return host->getPstateSpeed(pstate_index);
141 }
142
143 /** \ingroup m_host_management
144  * \brief Return the number of cores.
145  *
146  * \param host a host
147  * \return the number of cores
148  */
149 int sg_host_core_count(sg_host_t host)
150 {
151   return host->getCoreCount();
152 }
153
154 double sg_host_get_available_speed(sg_host_t host)
155 {
156   return host->pimpl_cpu->getAvailableSpeed();
157 }
158
159 /** @brief Returns the number of power states for a host.
160  *
161  *  See also @ref plugin_energy.
162  */
163 int sg_host_get_nb_pstates(sg_host_t host) {
164   return host->getPstatesCount();
165 }
166
167 /** @brief Gets the pstate at which that host currently runs.
168  *
169  *  See also @ref plugin_energy.
170  */
171 int sg_host_get_pstate(sg_host_t host) {
172   return host->getPstate();
173 }
174 /** @brief Sets the pstate at which that host should run.
175  *
176  *  See also @ref plugin_energy.
177  */
178 void sg_host_set_pstate(sg_host_t host,int pstate) {
179   host->setPstate(pstate);
180 }
181
182 /** \ingroup m_host_management
183  *
184  * \brief Start the host if it is off
185  *
186  * See also #sg_host_is_on() and #sg_host_is_off() to test the current state of the host and @ref plugin_energy
187  * for more info on DVFS.
188  */
189 void sg_host_turn_on(sg_host_t host)
190 {
191   host->turnOn();
192 }
193
194 /** \ingroup m_host_management
195  *
196  * \brief Stop the host if it is on
197  *
198  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref plugin_energy
199  * for more info on DVFS.
200  */
201 void sg_host_turn_off(sg_host_t host)
202 {
203   host->turnOff();
204 }
205
206 /** @ingroup m_host_management
207  * @brief Determine if a host is up and running.
208  *
209  * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_energy for more
210  * info on DVFS.
211  *
212  * @param host host to test
213  * @return Returns true if the host is up and running, and false if it's currently down
214  */
215 int sg_host_is_on(sg_host_t host)
216 {
217   return host->isOn();
218 }
219
220 /** @ingroup m_host_management
221  * @brief Determine if a host is currently off.
222  *
223  * See also #sg_host_turn_on() and #sg_host_turn_off() to switch the host ON and OFF and @ref plugin_energy for more
224  * info on DVFS.
225  */
226 int sg_host_is_off(sg_host_t host)
227 {
228   return host->isOff();
229 }
230
231 /** @brief Get the properties of an host */
232 xbt_dict_t sg_host_get_properties(sg_host_t host) {
233   xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
234   std::map<std::string, std::string>* props = host->getProperties();
235   if (props == nullptr)
236     return nullptr;
237   for (auto const& elm : *props) {
238     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
239   }
240   return as_dict;
241 }
242
243 /** \ingroup m_host_management
244  * \brief Returns the value of a given host property
245  *
246  * \param host a host
247  * \param name a property name
248  * \return value of a property (or nullptr if property not set)
249 */
250 const char *sg_host_get_property_value(sg_host_t host, const char *name)
251 {
252   return host->getProperty(name);
253 }
254
255 void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
256 {
257   host->setProperty(name, value);
258 }
259
260 /**
261  * \brief Find a route between two hosts
262  *
263  * \param from where from
264  * \param to where to
265  * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
266  */
267 void sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links)
268 {
269   std::vector<simgrid::s4u::Link*> vlinks;
270   from->routeTo(to, vlinks, nullptr);
271   for (auto const& link : vlinks)
272     xbt_dynar_push(links, &link);
273 }
274 /**
275  * \brief Find the latency of the route between two hosts
276  *
277  * \param from where from
278  * \param to where to
279  */
280 double sg_host_route_latency(sg_host_t from, sg_host_t to)
281 {
282   std::vector<simgrid::s4u::Link*> vlinks;
283   double res = 0;
284   from->routeTo(to, vlinks, &res);
285   return res;
286 }
287 /**
288  * \brief Find the bandwitdh of the route between two hosts
289  *
290  * \param from where from
291  * \param to where to
292  */
293 double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
294 {
295   double min_bandwidth = -1.0;
296
297   std::vector<simgrid::s4u::Link*> vlinks;
298   from->routeTo(to, vlinks, nullptr);
299   for (auto const& link : vlinks) {
300     double bandwidth = link->bandwidth();
301     if (bandwidth < min_bandwidth || min_bandwidth < 0.0)
302       min_bandwidth = bandwidth;
303   }
304   return min_bandwidth;
305 }
306
307 /** @brief Displays debugging information about a host */
308 void sg_host_dump(sg_host_t host)
309 {
310   XBT_INFO("Displaying host %s", host->getCname());
311   XBT_INFO("  - speed: %.0f", host->getSpeed());
312   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
313   std::map<std::string, std::string>* props = host->getProperties();
314
315   if (not props->empty()) {
316     XBT_INFO("  - properties:");
317     for (auto const& elm : *props) {
318       XBT_INFO("    %s->%s", elm.first.c_str(), elm.second.c_str());
319     }
320   }
321 }
322
323 /** \brief Return the list of actors attached to an host.
324  *
325  * \param host a host
326  * \param whereto a dynar in which we should push actors living on that host
327  */
328 void sg_host_get_actor_list(sg_host_t host, xbt_dynar_t whereto)
329 {
330   for (auto& actor : host->extension<simgrid::simix::Host>()->process_list) {
331     s4u_Actor* p = actor.ciface();
332     xbt_dynar_push(whereto, &p);
333   }
334 }
335
336 } // extern "C"