Logo AND Algorithmique Numérique Distribuée

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