Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[energy] sanitize what's a simcall and what's not; inline some calls
[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 void sg_host_msg_destroy(sg_host_t host) {
108   host->extension_set(MSG_HOST_LEVEL, nullptr);
109 }
110 // ========== SimDag Layer ==============
111 SD_workstation_priv_t sg_host_sd(sg_host_t host) {
112   return (SD_workstation_priv_t) host->extension(SD_HOST_LEVEL);
113 }
114 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
115   host->extension_set(SD_HOST_LEVEL, smx_host);
116 }
117 void sg_host_sd_destroy(sg_host_t host) {
118   host->extension_set(SD_HOST_LEVEL, nullptr);
119 }
120
121 // ========== Simix layer =============
122 smx_host_priv_t sg_host_simix(sg_host_t host){
123   return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
124 }
125 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
126   host->extension_set(SIMIX_HOST_LEVEL, smx_host);
127 }
128 void sg_host_simix_destroy(sg_host_t host) {
129   host->extension_set(SIMIX_HOST_LEVEL, nullptr);
130 }
131
132 // =========== user-level functions ===============
133 // ================================================
134
135 double sg_host_get_available_speed(sg_host_t host){
136   return surf_host_get_available_speed(host);
137 }
138 /** @brief Returns the state of a host.
139  *  @return 1 if the host is active or 0 if it has crashed.
140  */
141 int sg_host_is_on(sg_host_t host) {
142         return host->pimpl_cpu->isOn();
143 }
144
145 /** @brief Returns the number of power states for a host.
146  *
147  *  See also @ref SURF_plugin_energy.
148  */
149 int sg_host_get_nb_pstates(sg_host_t host) {
150   return host->pimpl_cpu->getNbPStates();
151 }
152
153 /** @brief Gets the pstate at which that host currently runs.
154  *
155  *  See also @ref SURF_plugin_energy.
156  */
157 int sg_host_get_pstate(sg_host_t host) {
158   return host->getPState();
159 }
160 /** @brief Sets the pstate at which that host should run.
161  *
162  *  See also @ref SURF_plugin_energy.
163  */
164 void sg_host_set_pstate(sg_host_t host,int pstate) {
165   host->setPState(pstate);
166 }
167
168 namespace simgrid {
169
170 Host::Host(std::string const& id)
171   : name_(id)
172 {
173 }
174
175 Host::~Host()
176 {
177 }
178
179 /** Start the host if it is off */
180 void Host::turnOn()
181 {
182   simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
183 }
184
185 /** Stop the host if it is on */
186 void Host::turnOff()
187 {
188   /* Go to that function to follow the code flow through the simcall barrier */
189   if (0) simcall_HANDLER_host_off(&SIMIX_process_self()->simcall, this);
190   simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
191 }
192
193 bool Host::isOn() {
194   return pimpl_cpu->isOn();
195 }
196 bool Host::isOff() {
197   return ! pimpl_cpu->isOn();
198 }
199
200
201 /** Get the properties assigned to a host */
202 xbt_dict_t Host::getProperties()
203 {
204   return simgrid::simix::kernel(std::bind(sg_host_get_properties, this));
205 }
206
207 /** Get the processes attached to the host */
208 xbt_swag_t Host::getProcessList()
209 {
210   return simgrid::simix::kernel([&]() {
211     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
212   });
213 }
214
215 /** Get the peak power of a host */
216 double Host::getCurrentPowerPeak()
217 {
218   return simgrid::simix::kernel(
219     std::bind(surf_host_get_current_power_peak, this));
220 }
221
222 /** Get one power peak (in flops/s) of a host at a given pstate */
223 double Host::getPowerPeakAt(int pstate_index)
224 {
225   return simgrid::simix::kernel(
226     std::bind(surf_host_get_power_peak_at, this, pstate_index));
227 }
228
229 /** @brief Get the speed of the cpu associated to a host */
230 double Host::getSpeed() {
231         return pimpl_cpu->getSpeed(1.0);
232 }
233 /** @brief Returns the number of core of the processor. */
234 int Host::getCoreAmount() {
235         return pimpl_cpu->getCore();
236 }
237
238 Host* Host::by_name_or_null(const char* name)
239 {
240   return (Host*) xbt_dict_get_or_null(host_list, name);
241 }
242
243 Host* Host::by_name_or_create(const char* name)
244 {
245   Host* host = by_name_or_null(name);
246   if (host == nullptr) {
247     host = new Host(name);
248     xbt_dict_set(host_list, name, host, NULL);
249   }
250   return host;
251 }
252
253 /** @brief Set the pstate at which the host should run */
254 void Host::setPState(int pstate_index)
255 {
256   simgrid::simix::kernel(std::bind(
257       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
258   ));
259 }
260 /** @brief Retrieve the pstate at which the host is currently running */
261 int Host::getPState()
262 {
263   return pimpl_cpu->getPState();
264 }
265
266 void Host::getParams(vm_params_t params)
267 {
268   simgrid::simix::kernel([&]() {
269     this->extension<simgrid::surf::Host>()->getParams(params);
270   });
271 }
272
273 void Host::setParams(vm_params_t params)
274 {
275   simgrid::simix::kernel([&]() {
276     this->extension<simgrid::surf::Host>()->setParams(params);
277   });
278 }
279
280 /**
281  * \ingroup simix_storage_management
282  * \brief Returns the list of storages mounted on an host.
283  * \return a dict containing all storages mounted on the host
284  */
285 xbt_dict_t Host::getMountedStorageList()
286 {
287   return simgrid::simix::kernel([&] {
288     return this->extension<simgrid::surf::Host>()->getMountedStorageList();
289   });
290 }
291
292 /**
293  * \ingroup simix_storage_management
294  * \brief Returns the list of storages attached to an host.
295  * \return a dict containing all storages attached to the host
296  */
297 xbt_dynar_t Host::getAttachedStorageList()
298 {
299   return simgrid::simix::kernel([&] {
300     return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
301   });
302 }
303
304 }