Logo AND Algorithmique Numérique Distribuée

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