Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do the Right Thing for the host_(get/set)_pstate
[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
16 size_t sg_host_count()
17 {
18   return xbt_dict_length(host_list);
19 }
20
21 const char *sg_host_get_name(sg_host_t host)
22 {
23         return host->getName().c_str();
24 }
25
26 void* sg_host_extension_get(sg_host_t host, size_t ext)
27 {
28   return host->extension(ext);
29 }
30
31 size_t sg_host_extension_create(void(*deleter)(void*))
32 {
33   return simgrid::Host::extension_create(deleter);
34 }
35
36 sg_host_t sg_host_by_name(const char *name)
37 {
38   return simgrid::Host::by_name_or_null(name);
39 }
40
41 sg_host_t sg_host_by_name_or_create(const char *name)
42 {
43   return simgrid::Host::by_name_or_create(name);
44 }
45
46 xbt_dynar_t sg_hosts_as_dynar(void)
47 {
48         xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
49
50   xbt_dict_cursor_t cursor = nullptr;
51   const char* name = nullptr;
52   simgrid::Host* host = nullptr;
53         xbt_dict_foreach(host_list, cursor, name, host)
54                 if(routing_get_network_element_type(name) == SURF_NETWORK_ELEMENT_HOST)
55                         xbt_dynar_push(res, &host);
56         return res;
57 }
58
59 // ========= Layering madness ==============
60
61 int MSG_HOST_LEVEL;
62 int SD_HOST_LEVEL;
63 int SIMIX_HOST_LEVEL;
64 int ROUTING_HOST_LEVEL;
65 int USER_HOST_LEVEL;
66
67 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme by initializing that level in msg when used
68 #include "src/simdag/simdag_private.h" // __SD_workstation_destroy. FIXME: killme by initializing that level in simdag when used
69 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme by initializing that level in simix when used
70 #include "src/surf/cpu_interface.hpp"
71 #include "src/surf/surf_routing.hpp"
72
73 void sg_host_init()
74 {
75   MSG_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
76     __MSG_host_priv_free((msg_host_priv_t) p);
77   });
78
79   ROUTING_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
80           delete static_cast<simgrid::surf::NetCard*>(p);
81   });
82
83   SD_HOST_LEVEL = simgrid::Host::extension_create(__SD_workstation_destroy);
84   SIMIX_HOST_LEVEL = simgrid::Host::extension_create(SIMIX_host_destroy);
85   USER_HOST_LEVEL = simgrid::Host::extension_create(NULL);
86 }
87
88 // ========== User data Layer ==========
89 void *sg_host_user(sg_host_t host) {
90   return host->extension(USER_HOST_LEVEL);
91 }
92 void sg_host_user_set(sg_host_t host, void* userdata) {
93   host->extension_set(USER_HOST_LEVEL,userdata);
94 }
95 void sg_host_user_destroy(sg_host_t host) {
96   host->extension_set(USER_HOST_LEVEL, nullptr);
97 }
98
99 // ========== MSG Layer ==============
100 msg_host_priv_t sg_host_msg(sg_host_t host) {
101         return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
102 }
103 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
104   host->extension_set(MSG_HOST_LEVEL, smx_host);
105 }
106 void sg_host_msg_destroy(sg_host_t host) {
107   host->extension_set(MSG_HOST_LEVEL, nullptr);
108 }
109 // ========== SimDag Layer ==============
110 SD_workstation_priv_t sg_host_sd(sg_host_t host) {
111   return (SD_workstation_priv_t) host->extension(SD_HOST_LEVEL);
112 }
113 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
114   host->extension_set(SD_HOST_LEVEL, smx_host);
115 }
116 void sg_host_sd_destroy(sg_host_t host) {
117   host->extension_set(SD_HOST_LEVEL, nullptr);
118 }
119
120 // ========== Simix layer =============
121 smx_host_priv_t sg_host_simix(sg_host_t host){
122   return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
123 }
124 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
125   host->extension_set(SIMIX_HOST_LEVEL, smx_host);
126 }
127 void sg_host_simix_destroy(sg_host_t host) {
128   host->extension_set(SIMIX_HOST_LEVEL, nullptr);
129 }
130
131 // =========== user-level functions ===============
132 // ================================================
133
134 double sg_host_get_available_speed(sg_host_t host){
135   return surf_host_get_available_speed(host);
136 }
137 /** @brief Returns the state of a host.
138  *  @return 1 if the host is active or 0 if it has crashed.
139  */
140 int sg_host_is_on(sg_host_t host) {
141         return host->p_cpu->isOn();
142 }
143
144 /** @brief Returns the total energy consumed by the host (in Joules).
145  *
146  *  See also @ref SURF_plugin_energy.
147  */
148 double sg_host_get_consumed_energy(sg_host_t host) {
149         return surf_host_get_consumed_energy(host);
150 }
151
152 /** @brief Returns the number of power states for a host.
153  *
154  *  See also @ref SURF_plugin_energy.
155  */
156 int sg_host_get_nb_pstates(sg_host_t host) {
157   return host->p_cpu->getNbPStates();
158 }
159
160 /** @brief Gets the pstate at which that host currently runs.
161  *
162  *  See also @ref SURF_plugin_energy.
163  */
164 int sg_host_get_pstate(sg_host_t host) {
165   return host->getPState();
166 }
167 /** @brief Sets the pstate at which that host should run.
168  *
169  *  See also @ref SURF_plugin_energy.
170  */
171 void sg_host_set_pstate(sg_host_t host,int pstate) {
172   host->setPState(pstate);
173 }
174
175 namespace simgrid {
176
177 Host::Host(std::string const& id)
178   : name_(id)
179 {
180 }
181
182 Host::~Host()
183 {
184 }
185
186 /** Start the host if it is off */
187 void Host::turnOn()
188 {
189   simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
190 }
191
192 /** Stop the host if it is on */
193 void Host::turnOff()
194 {
195   /* Go to that function to follow the code flow through the simcall barrier */
196   if (0) simcall_HANDLER_host_off(&SIMIX_process_self()->simcall, this);
197   simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
198 }
199
200 bool Host::isOn() {
201   return p_cpu->isOn();
202 }
203 bool Host::isOff() {
204   return ! p_cpu->isOn();
205 }
206
207
208 /** Get the properties assigned to a host */
209 xbt_dict_t Host::getProperties()
210 {
211   return simgrid::simix::kernel(std::bind(SIMIX_host_get_properties, this));
212 }
213
214 /** Get the processes attached to the host */
215 xbt_swag_t Host::getProcessList()
216 {
217   return simgrid::simix::kernel(std::bind(SIMIX_host_get_process_list, this));
218 }
219
220 /** Get the peak power of a host */
221 double Host::getCurrentPowerPeak()
222 {
223   return simgrid::simix::kernel(
224     std::bind(SIMIX_host_get_current_power_peak, this));
225 }
226
227 /** Get one power peak (in flops/s) of a host at a given pstate */
228 double Host::getPowerPeakAt(int pstate_index)
229 {
230   return simgrid::simix::kernel(
231     std::bind(SIMIX_host_get_power_peak_at, this, pstate_index));
232 }
233
234 /** @brief Get the speed of the cpu associated to a host */
235 double Host::getSpeed() {
236         return p_cpu->getSpeed(1.0);
237 }
238 /** @brief Returns the number of core of the processor. */
239 int Host::getCoreAmount() {
240         return p_cpu->getCore();
241 }
242
243 Host* Host::by_name_or_null(const char* name)
244 {
245   return (Host*) xbt_dict_get_or_null(host_list, name);
246 }
247
248 Host* Host::by_name_or_create(const char* name)
249 {
250   Host* host = by_name_or_null(name);
251   if (host == nullptr) {
252     host = new Host(name);
253     xbt_dict_set(host_list, name, host, NULL);
254   }
255   return host;
256 }
257
258 /** @brief Set the pstate at which the host should run */
259 void Host::setPState(int pstate_index)
260 {
261   simgrid::simix::kernel(std::bind(
262       &simgrid::surf::Cpu::setPState, p_cpu, pstate_index
263   ));
264 }
265 /** @brief Retrieve the pstate at which the host is currently running */
266 int Host::getPState()
267 {
268   return p_cpu->getPState();
269 }
270
271 /** Get the amount of watt dissipated at the given pstate when the host is idling */
272 double Host::getWattMinAt(int pstate)
273 {
274   return simgrid::simix::kernel(std::bind(
275       surf_host_get_wattmin_at, this, pstate
276   ));
277 }
278
279 /** Get the amount of watt dissipated at the given pstate when the host burns CPU at 100% */
280 double Host::getWattMaxAt(int pstate)
281 {
282   return simgrid::simix::kernel(std::bind(
283       surf_host_get_wattmax_at, this, pstate
284   ));
285 }
286
287 void Host::getParams(vm_params_t params)
288 {
289   simgrid::simix::kernel(std::bind(SIMIX_host_get_params, this, params));
290 }
291
292 void Host::setParams(vm_params_t params)
293 {
294   simgrid::simix::kernel(std::bind(SIMIX_host_set_params, this, params));
295 }
296
297 /**
298  * \ingroup simix_storage_management
299  * \brief Returns the list of storages mounted on an host.
300  * \return a dict containing all storages mounted on the host
301  */
302 xbt_dict_t Host::getMountedStorageList()
303 {
304   return simgrid::simix::kernel(std::bind(
305     SIMIX_host_get_mounted_storage_list, this
306   ));
307 }
308
309 /**
310  * \ingroup simix_storage_management
311  * \brief Returns the list of storages attached to an host.
312  * \return a dict containing all storages attached to the host
313  */
314 xbt_dynar_t Host::getAttachedStorageList()
315 {
316   return simgrid::simix::kernel(std::bind(
317     SIMIX_host_get_attached_storage_list, this
318   ));
319 }
320
321 }