Logo AND Algorithmique Numérique Distribuée

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