Logo AND Algorithmique Numérique Distribuée

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