Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9ccb263369b661d2d8e0773c9f7052ddd143a70c
[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/surf/HostImpl.hpp"
15 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
18
19 extern std::unordered_map<std::string, simgrid::s4u::Host*>
20     host_list; // FIXME: don't dupplicate the content of s4u::Host this way
21
22 void sg_host_exit()
23 {
24   /* copy all names to not modify the map while iterating over it.
25    *
26    * Plus, the hosts are destroyed in the lexicographic order to ensure
27    * that the output is reproducible: we don't want to kill them in the
28    * pointer order as it could be platform-dependent, which would break
29    * the tests.
30    */
31   std::vector<std::string> names = std::vector<std::string>();
32   for (auto kv : host_list)
33     names.push_back(kv.second->name());
34
35   std::sort(names.begin(), names.end());
36
37   for (auto name : names)
38     host_list.at(name)->destroy();
39
40   // host_list.clear(); This would be sufficient if the dict would contain smart_ptr. It's now useless
41 }
42
43 size_t sg_host_count()
44 {
45   return host_list.size();
46 }
47 /** @brief Returns the host list
48  *
49  * Uses sg_host_count() to know the array size.
50  *
51  * \return an array of \ref sg_host_t containing all the hosts in the platform.
52  * \remark The host order in this array is generally different from the
53  * creation/declaration order in the XML platform (we use a hash table
54  * internally).
55  * \see sg_host_count()
56  */
57 sg_host_t *sg_host_list() {
58   xbt_assert(sg_host_count() > 0, "There is no host!");
59   return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
60 }
61
62 const char *sg_host_get_name(sg_host_t host)
63 {
64   return host->cname();
65 }
66
67 void* sg_host_extension_get(sg_host_t host, size_t ext)
68 {
69   return host->extension(ext);
70 }
71
72 size_t sg_host_extension_create(void(*deleter)(void*))
73 {
74   return simgrid::s4u::Host::extension_create(deleter);
75 }
76
77 sg_host_t sg_host_by_name(const char *name)
78 {
79   return simgrid::s4u::Host::by_name_or_null(name);
80 }
81
82 static int hostcmp_voidp(const void* pa, const void* pb)
83 {
84   return strcmp((*static_cast<simgrid::s4u::Host* const*>(pa))->name().c_str(),
85                 (*static_cast<simgrid::s4u::Host* const*>(pb))->name().c_str());
86 }
87
88 xbt_dynar_t sg_hosts_as_dynar()
89 {
90   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
91
92   for (auto kv : host_list) {
93     simgrid::s4u::Host* host = kv.second;
94     if (host && host->pimpl_netcard && host->pimpl_netcard->isHost())
95        xbt_dynar_push(res, &host);
96   }
97   xbt_dynar_sort(res, hostcmp_voidp);
98   return res;
99 }
100
101 // ========= Layering madness ==============*
102
103 #include "src/surf/cpu_interface.hpp"
104 #include "src/surf/surf_routing.hpp"
105
106 // ========== User data Layer ==========
107 void *sg_host_user(sg_host_t host) {
108   return host->extension(USER_HOST_LEVEL);
109 }
110 void sg_host_user_set(sg_host_t host, void* userdata) {
111   host->extension_set(USER_HOST_LEVEL,userdata);
112 }
113 void sg_host_user_destroy(sg_host_t host) {
114   host->extension_set(USER_HOST_LEVEL, nullptr);
115 }
116
117 // ========== MSG Layer ==============
118 msg_host_priv_t sg_host_msg(sg_host_t host) {
119   return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
120 }
121 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
122   host->extension_set(MSG_HOST_LEVEL, smx_host);
123 }
124
125 // ========== Simix layer =============
126 #include "src/simix/smx_host_private.h"
127 smx_host_priv_t sg_host_simix(sg_host_t host){
128   return host->extension<simgrid::simix::Host>();
129 }
130
131 // ========= storage related functions ============
132 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
133   return host->pimpl_->getMountedStorageList();
134 }
135
136 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
137   return host->pimpl_->getAttachedStorageList();
138 }
139
140
141 // =========== user-level functions ===============
142 // ================================================
143 /** @brief Returns the total speed of a host */
144 double sg_host_speed(sg_host_t host)
145 {
146   return host->speed();
147 }
148
149 double sg_host_get_available_speed(sg_host_t host)
150 {
151   return host->pimpl_cpu->getAvailableSpeed();
152 }
153
154 /** @brief Returns the number of power states for a host.
155  *
156  *  See also @ref SURF_plugin_energy.
157  */
158 int sg_host_get_nb_pstates(sg_host_t host) {
159   return host->pstatesCount();
160 }
161
162 /** @brief Gets the pstate at which that host currently runs.
163  *
164  *  See also @ref SURF_plugin_energy.
165  */
166 int sg_host_get_pstate(sg_host_t host) {
167   return host->pstate();
168 }
169 /** @brief Sets the pstate at which that host should run.
170  *
171  *  See also @ref SURF_plugin_energy.
172  */
173 void sg_host_set_pstate(sg_host_t host,int pstate) {
174   host->setPstate(pstate);
175 }
176
177 /** @brief Get the properties of an host */
178 xbt_dict_t sg_host_get_properties(sg_host_t host) {
179   return host->properties();
180 }
181
182 /** \ingroup m_host_management
183  * \brief Returns the value of a given host property
184  *
185  * \param host a host
186  * \param name a property name
187  * \return value of a property (or nullptr if property not set)
188 */
189 const char *sg_host_get_property_value(sg_host_t host, const char *name)
190 {
191   return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
192 }
193
194 /** @brief Displays debugging information about a host */
195 void sg_host_dump(sg_host_t host)
196 {
197   xbt_dict_t props;
198   xbt_dict_cursor_t cursor=nullptr;
199   char *key,*data;
200
201   XBT_INFO("Displaying host %s", sg_host_get_name(host));
202   XBT_INFO("  - speed: %.0f", host->speed());
203   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
204   props = sg_host_get_properties(host);
205
206   if (!xbt_dict_is_empty(props)){
207     XBT_INFO("  - properties:");
208
209     xbt_dict_foreach(props,cursor,key,data) {
210       XBT_INFO("    %s->%s",key,data);
211     }
212   }
213 }