Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
482f21fe2ef97085adcdc3c661201a0c81354729
[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 /** @brief Get the properties of an host */
166 xbt_dict_t sg_host_get_properties(sg_host_t host) {
167   return host->getProperties();
168 }
169
170
171 namespace simgrid {
172
173 Host::Host(std::string const& id)
174   : name_(id)
175 {
176 }
177
178 Host::~Host()
179 {
180 }
181
182 /** Start the host if it is off */
183 void Host::turnOn()
184 {
185   simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
186 }
187
188 /** Stop the host if it is on */
189 void Host::turnOff()
190 {
191   simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
192 }
193
194 bool Host::isOn() {
195   return pimpl_cpu->isOn();
196 }
197 bool Host::isOff() {
198   return ! pimpl_cpu->isOn();
199 }
200
201
202 /** Get the properties assigned to a host */
203 xbt_dict_t Host::getProperties()
204 {
205   return simgrid::simix::kernel(std::bind(&simgrid::surf::Host::getProperties, this->extension(simgrid::surf::Host::EXTENSION_ID)));
206 }
207
208 /** Get the processes attached to the host */
209 xbt_swag_t Host::getProcessList()
210 {
211   return simgrid::simix::kernel([&]() {
212     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
213   });
214 }
215
216 /** Get the peak power of a host */
217 double Host::getCurrentPowerPeak()
218 {
219   return simgrid::simix::kernel(
220     std::bind(surf_host_get_current_power_peak, this));
221 }
222
223 /** Get one power peak (in flops/s) of a host at a given pstate */
224 double Host::getPowerPeakAt(int pstate_index)
225 {
226   return simgrid::simix::kernel(
227     std::bind(surf_host_get_power_peak_at, this, pstate_index));
228 }
229
230 /** @brief Get the speed of the cpu associated to a host */
231 double Host::getSpeed() {
232         return pimpl_cpu->getSpeed(1.0);
233 }
234 /** @brief Returns the number of core of the processor. */
235 int Host::getCoreAmount() {
236         return pimpl_cpu->getCore();
237 }
238
239 Host* Host::by_name_or_null(const char* name)
240 {
241   return (Host*) xbt_dict_get_or_null(host_list, name);
242 }
243
244 Host* Host::by_name_or_create(const char* name)
245 {
246   Host* host = by_name_or_null(name);
247   if (host == nullptr) {
248     host = new Host(name);
249     xbt_dict_set(host_list, name, host, NULL);
250   }
251   return host;
252 }
253
254 /** @brief Set the pstate at which the host should run */
255 void Host::setPState(int pstate_index)
256 {
257   simgrid::simix::kernel(std::bind(
258       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
259   ));
260 }
261 /** @brief Retrieve the pstate at which the host is currently running */
262 int Host::getPState()
263 {
264   return pimpl_cpu->getPState();
265 }
266
267 void Host::getParams(vm_params_t params)
268 {
269   simgrid::simix::kernel([&]() {
270     this->extension<simgrid::surf::Host>()->getParams(params);
271   });
272 }
273
274 void Host::setParams(vm_params_t params)
275 {
276   simgrid::simix::kernel([&]() {
277     this->extension<simgrid::surf::Host>()->setParams(params);
278   });
279 }
280
281 /**
282  * \ingroup simix_storage_management
283  * \brief Returns the list of storages mounted on an host.
284  * \return a dict containing all storages mounted on the host
285  */
286 xbt_dict_t Host::getMountedStorageList()
287 {
288   return simgrid::simix::kernel([&] {
289     return this->extension<simgrid::surf::Host>()->getMountedStorageList();
290   });
291 }
292
293 /**
294  * \ingroup simix_storage_management
295  * \brief Returns the list of storages attached to an host.
296  * \return a dict containing all storages attached to the host
297  */
298 xbt_dynar_t Host::getAttachedStorageList()
299 {
300   return simgrid::simix::kernel([&] {
301     return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
302   });
303 }
304
305 }