1 /* Copyright (c) 2013-201. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include <simgrid/simix.hpp>
10 #include "simgrid/host.h"
11 #include "simgrid/Host.hpp"
12 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
14 #include "src/simix/smx_private.hpp"
15 #include "src/surf/host_interface.hpp"
17 size_t sg_host_count()
19 return xbt_dict_length(host_list);
22 const char *sg_host_get_name(sg_host_t host)
24 return host->getName().c_str();
27 void* sg_host_extension_get(sg_host_t host, size_t ext)
29 return host->extension(ext);
32 size_t sg_host_extension_create(void(*deleter)(void*))
34 return simgrid::Host::extension_create(deleter);
37 sg_host_t sg_host_by_name(const char *name)
39 return simgrid::Host::by_name_or_null(name);
42 sg_host_t sg_host_by_name_or_create(const char *name)
44 return simgrid::Host::by_name_or_create(name);
47 xbt_dynar_t sg_hosts_as_dynar(void)
49 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
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);
60 // ========= Layering madness ==============
65 int ROUTING_HOST_LEVEL;
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"
76 MSG_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
77 __MSG_host_priv_free((msg_host_priv_t) p);
80 ROUTING_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
81 delete static_cast<simgrid::surf::NetCard*>(p);
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);
89 // ========== User data Layer ==========
90 void *sg_host_user(sg_host_t host) {
91 return host->extension(USER_HOST_LEVEL);
93 void sg_host_user_set(sg_host_t host, void* userdata) {
94 host->extension_set(USER_HOST_LEVEL,userdata);
96 void sg_host_user_destroy(sg_host_t host) {
97 host->extension_set(USER_HOST_LEVEL, nullptr);
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);
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);
107 void sg_host_msg_destroy(sg_host_t host) {
108 host->extension_set(MSG_HOST_LEVEL, nullptr);
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);
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);
117 void sg_host_sd_destroy(sg_host_t host) {
118 host->extension_set(SD_HOST_LEVEL, nullptr);
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);
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);
128 void sg_host_simix_destroy(sg_host_t host) {
129 host->extension_set(SIMIX_HOST_LEVEL, nullptr);
132 // =========== user-level functions ===============
133 // ================================================
135 double sg_host_get_available_speed(sg_host_t host){
136 return surf_host_get_available_speed(host);
138 /** @brief Returns the state of a host.
139 * @return 1 if the host is active or 0 if it has crashed.
141 int sg_host_is_on(sg_host_t host) {
142 return host->pimpl_cpu->isOn();
145 /** @brief Returns the number of power states for a host.
147 * See also @ref SURF_plugin_energy.
149 int sg_host_get_nb_pstates(sg_host_t host) {
150 return host->pimpl_cpu->getNbPStates();
153 /** @brief Gets the pstate at which that host currently runs.
155 * See also @ref SURF_plugin_energy.
157 int sg_host_get_pstate(sg_host_t host) {
158 return host->getPState();
160 /** @brief Sets the pstate at which that host should run.
162 * See also @ref SURF_plugin_energy.
164 void sg_host_set_pstate(sg_host_t host,int pstate) {
165 host->setPState(pstate);
170 Host::Host(std::string const& id)
179 /** Start the host if it is off */
182 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
185 /** Stop the host if it is on */
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);
194 return pimpl_cpu->isOn();
197 return ! pimpl_cpu->isOn();
201 /** Get the properties assigned to a host */
202 xbt_dict_t Host::getProperties()
204 return simgrid::simix::kernel(std::bind(sg_host_get_properties, this));
207 /** Get the processes attached to the host */
208 xbt_swag_t Host::getProcessList()
210 return simgrid::simix::kernel([&]() {
211 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
215 /** Get the peak power of a host */
216 double Host::getCurrentPowerPeak()
218 return simgrid::simix::kernel(
219 std::bind(surf_host_get_current_power_peak, this));
222 /** Get one power peak (in flops/s) of a host at a given pstate */
223 double Host::getPowerPeakAt(int pstate_index)
225 return simgrid::simix::kernel(
226 std::bind(surf_host_get_power_peak_at, this, pstate_index));
229 /** @brief Get the speed of the cpu associated to a host */
230 double Host::getSpeed() {
231 return pimpl_cpu->getSpeed(1.0);
233 /** @brief Returns the number of core of the processor. */
234 int Host::getCoreAmount() {
235 return pimpl_cpu->getCore();
238 Host* Host::by_name_or_null(const char* name)
240 return (Host*) xbt_dict_get_or_null(host_list, name);
243 Host* Host::by_name_or_create(const char* name)
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);
253 /** @brief Set the pstate at which the host should run */
254 void Host::setPState(int pstate_index)
256 simgrid::simix::kernel(std::bind(
257 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
260 /** @brief Retrieve the pstate at which the host is currently running */
261 int Host::getPState()
263 return pimpl_cpu->getPState();
266 void Host::getParams(vm_params_t params)
268 simgrid::simix::kernel([&]() {
269 this->extension<simgrid::surf::Host>()->getParams(params);
273 void Host::setParams(vm_params_t params)
275 simgrid::simix::kernel([&]() {
276 this->extension<simgrid::surf::Host>()->setParams(params);
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
285 xbt_dict_t Host::getMountedStorageList()
287 return simgrid::simix::kernel([&] {
288 return this->extension<simgrid::surf::Host>()->getMountedStorageList();
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
297 xbt_dynar_t Host::getAttachedStorageList()
299 return simgrid::simix::kernel([&] {
300 return this->extension<simgrid::surf::Host>()->getAttachedStorageList();