Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
236376bf198d2cf158f72d0c6e9b3f35ddb96648
[simgrid.git] / src / simgrid / host.cpp
1 /* Copyright (c) 2013-201. 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 <simgrid/simix.hpp>
8
9 #include "xbt/dict.h"
10 #include "simgrid/host.h"
11 #include "simgrid/Host.hpp"
12 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
13
14 #include "src/simix/smx_private.hpp"
15 #include "src/surf/host_interface.hpp"
16
17 size_t sg_host_count()
18 {
19   return xbt_dict_length(host_list);
20 }
21
22 const char *sg_host_get_name(sg_host_t host)
23 {
24         return host->getName().c_str();
25 }
26
27 void* sg_host_extension_get(sg_host_t host, size_t ext)
28 {
29   return host->extension(ext);
30 }
31
32 size_t sg_host_extension_create(void(*deleter)(void*))
33 {
34   return simgrid::Host::extension_create(deleter);
35 }
36
37 sg_host_t sg_host_by_name(const char *name)
38 {
39   return simgrid::Host::by_name_or_null(name);
40 }
41
42 sg_host_t sg_host_by_name_or_create(const char *name)
43 {
44   return simgrid::Host::by_name_or_create(name);
45 }
46
47 xbt_dynar_t sg_hosts_as_dynar(void)
48 {
49         xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
50
51   xbt_dict_cursor_t cursor = nullptr;
52   const char* name = nullptr;
53   simgrid::Host* host = nullptr;
54         xbt_dict_foreach(host_list, cursor, name, host)
55                 if(routing_get_network_element_type(name) == SURF_NETWORK_ELEMENT_HOST)
56                         xbt_dynar_push(res, &host);
57         return res;
58 }
59
60 // ========= Layering madness ==============
61
62 int MSG_HOST_LEVEL;
63 int SD_HOST_LEVEL;
64 int SIMIX_HOST_LEVEL;
65 int ROUTING_HOST_LEVEL;
66 int USER_HOST_LEVEL;
67
68 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme by initializing that level in msg when used
69 #include "src/simdag/simdag_private.h" // __SD_workstation_destroy. FIXME: killme by initializing that level in simdag when used
70 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme by initializing that level in simix when used
71 #include "src/surf/cpu_interface.hpp"
72 #include "src/surf/surf_routing.hpp"
73
74 void sg_host_init()
75 {
76   MSG_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
77     __MSG_host_priv_free((msg_host_priv_t) p);
78   });
79
80   ROUTING_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
81           delete static_cast<simgrid::surf::NetCard*>(p);
82   });
83
84   SD_HOST_LEVEL = simgrid::Host::extension_create(__SD_workstation_destroy);
85   SIMIX_HOST_LEVEL = simgrid::Host::extension_create(SIMIX_host_destroy);
86   USER_HOST_LEVEL = simgrid::Host::extension_create(NULL);
87 }
88
89 // ========== User data Layer ==========
90 void *sg_host_user(sg_host_t host) {
91   return host->extension(USER_HOST_LEVEL);
92 }
93 void sg_host_user_set(sg_host_t host, void* userdata) {
94   host->extension_set(USER_HOST_LEVEL,userdata);
95 }
96 void sg_host_user_destroy(sg_host_t host) {
97   host->extension_set(USER_HOST_LEVEL, nullptr);
98 }
99
100 // ========== MSG Layer ==============
101 msg_host_priv_t sg_host_msg(sg_host_t host) {
102         return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
103 }
104 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
105   host->extension_set(MSG_HOST_LEVEL, smx_host);
106 }
107 // ========== SimDag Layer ==============
108 SD_workstation_priv_t sg_host_sd(sg_host_t host) {
109   return (SD_workstation_priv_t) host->extension(SD_HOST_LEVEL);
110 }
111 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
112   host->extension_set(SD_HOST_LEVEL, smx_host);
113 }
114 void sg_host_sd_destroy(sg_host_t host) {
115   host->extension_set(SD_HOST_LEVEL, nullptr);
116 }
117
118 // ========== Simix layer =============
119 smx_host_priv_t sg_host_simix(sg_host_t host){
120   return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
121 }
122 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
123   host->extension_set(SIMIX_HOST_LEVEL, smx_host);
124 }
125 void sg_host_simix_destroy(sg_host_t host) {
126   host->extension_set(SIMIX_HOST_LEVEL, nullptr);
127 }
128
129 // =========== user-level functions ===============
130 // ================================================
131
132 double sg_host_get_available_speed(sg_host_t host){
133   return surf_host_get_available_speed(host);
134 }
135 /** @brief Returns the state of a host.
136  *  @return 1 if the host is active or 0 if it has crashed.
137  */
138 int sg_host_is_on(sg_host_t host) {
139         return host->pimpl_cpu->isOn();
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->pimpl_cpu->getNbPStates();
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->getPState();
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 namespace simgrid {
166
167 Host::Host(std::string const& id)
168   : name_(id)
169 {
170 }
171
172 Host::~Host()
173 {
174 }
175
176 /** Start the host if it is off */
177 void Host::turnOn()
178 {
179   simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
180 }
181
182 /** Stop the host if it is on */
183 void Host::turnOff()
184 {
185   simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
186 }
187
188 bool Host::isOn() {
189   return pimpl_cpu->isOn();
190 }
191 bool Host::isOff() {
192   return ! pimpl_cpu->isOn();
193 }
194
195
196 /** Get the properties assigned to a host */
197 xbt_dict_t Host::getProperties()
198 {
199   return simgrid::simix::kernel(std::bind(sg_host_get_properties, this));
200 }
201
202 /** Get the processes attached to the host */
203 xbt_swag_t Host::getProcessList()
204 {
205   return simgrid::simix::kernel([&]() {
206     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
207   });
208 }
209
210 /** Get the peak power of a host */
211 double Host::getCurrentPowerPeak()
212 {
213   return simgrid::simix::kernel(
214     std::bind(surf_host_get_current_power_peak, this));
215 }
216
217 /** Get one power peak (in flops/s) of a host at a given pstate */
218 double Host::getPowerPeakAt(int pstate_index)
219 {
220   return simgrid::simix::kernel(
221     std::bind(surf_host_get_power_peak_at, this, pstate_index));
222 }
223
224 /** @brief Get the speed of the cpu associated to a host */
225 double Host::getSpeed() {
226         return pimpl_cpu->getSpeed(1.0);
227 }
228 /** @brief Returns the number of core of the processor. */
229 int Host::getCoreAmount() {
230         return pimpl_cpu->getCore();
231 }
232
233 Host* Host::by_name_or_null(const char* name)
234 {
235   return (Host*) xbt_dict_get_or_null(host_list, name);
236 }
237
238 Host* Host::by_name_or_create(const char* name)
239 {
240   Host* host = by_name_or_null(name);
241   if (host == nullptr) {
242     host = new Host(name);
243     xbt_dict_set(host_list, name, host, NULL);
244   }
245   return host;
246 }
247
248 /** @brief Set the pstate at which the host should run */
249 void Host::setPState(int pstate_index)
250 {
251   simgrid::simix::kernel(std::bind(
252       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
253   ));
254 }
255 /** @brief Retrieve the pstate at which the host is currently running */
256 int Host::getPState()
257 {
258   return pimpl_cpu->getPState();
259 }
260
261 void Host::getParams(vm_params_t params)
262 {
263   simgrid::simix::kernel([&]() {
264     this->extension<simgrid::surf::Host>()->getParams(params);
265   });
266 }
267
268 void Host::setParams(vm_params_t params)
269 {
270   simgrid::simix::kernel([&]() {
271     this->extension<simgrid::surf::Host>()->setParams(params);
272   });
273 }
274
275 /**
276  * \ingroup simix_storage_management
277  * \brief Returns the list of storages mounted on an host.
278  * \return a dict containing all storages mounted on the host
279  */
280 xbt_dict_t Host::getMountedStorageList()
281 {
282   return simgrid::simix::kernel([&] {
283     return this->extension<simgrid::surf::Host>()->getMountedStorageList();
284   });
285 }
286
287 /**
288  * \ingroup simix_storage_management
289  * \brief Returns the list of storages attached to an host.
290  * \return a dict containing all storages attached to the host
291  */
292 xbt_dynar_t Host::getAttachedStorageList()
293 {
294   return simgrid::simix::kernel([&] {
295     return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
296   });
297 }
298
299 }