Logo AND Algorithmique Numérique Distribuée

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