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 // ========== 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);
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);
114 void sg_host_sd_destroy(sg_host_t host) {
115 host->extension_set(SD_HOST_LEVEL, nullptr);
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);
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);
125 void sg_host_simix_destroy(sg_host_t host) {
126 host->extension_set(SIMIX_HOST_LEVEL, nullptr);
129 // =========== user-level functions ===============
130 // ================================================
132 double sg_host_get_available_speed(sg_host_t host){
133 return surf_host_get_available_speed(host);
135 /** @brief Returns the state of a host.
136 * @return 1 if the host is active or 0 if it has crashed.
138 int sg_host_is_on(sg_host_t host) {
139 return host->pimpl_cpu->isOn();
142 /** @brief Returns the number of power states for a host.
144 * See also @ref SURF_plugin_energy.
146 int sg_host_get_nb_pstates(sg_host_t host) {
147 return host->pimpl_cpu->getNbPStates();
150 /** @brief Gets the pstate at which that host currently runs.
152 * See also @ref SURF_plugin_energy.
154 int sg_host_get_pstate(sg_host_t host) {
155 return host->getPState();
157 /** @brief Sets the pstate at which that host should run.
159 * See also @ref SURF_plugin_energy.
161 void sg_host_set_pstate(sg_host_t host,int pstate) {
162 host->setPState(pstate);
167 Host::Host(std::string const& id)
176 /** Start the host if it is off */
179 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
182 /** Stop the host if it is on */
185 simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
189 return pimpl_cpu->isOn();
192 return ! pimpl_cpu->isOn();
196 /** Get the properties assigned to a host */
197 xbt_dict_t Host::getProperties()
199 return simgrid::simix::kernel(std::bind(sg_host_get_properties, this));
202 /** Get the processes attached to the host */
203 xbt_swag_t Host::getProcessList()
205 return simgrid::simix::kernel([&]() {
206 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
210 /** Get the peak power of a host */
211 double Host::getCurrentPowerPeak()
213 return simgrid::simix::kernel(
214 std::bind(surf_host_get_current_power_peak, this));
217 /** Get one power peak (in flops/s) of a host at a given pstate */
218 double Host::getPowerPeakAt(int pstate_index)
220 return simgrid::simix::kernel(
221 std::bind(surf_host_get_power_peak_at, this, pstate_index));
224 /** @brief Get the speed of the cpu associated to a host */
225 double Host::getSpeed() {
226 return pimpl_cpu->getSpeed(1.0);
228 /** @brief Returns the number of core of the processor. */
229 int Host::getCoreAmount() {
230 return pimpl_cpu->getCore();
233 Host* Host::by_name_or_null(const char* name)
235 return (Host*) xbt_dict_get_or_null(host_list, name);
238 Host* Host::by_name_or_create(const char* name)
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);
248 /** @brief Set the pstate at which the host should run */
249 void Host::setPState(int pstate_index)
251 simgrid::simix::kernel(std::bind(
252 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
255 /** @brief Retrieve the pstate at which the host is currently running */
256 int Host::getPState()
258 return pimpl_cpu->getPState();
261 void Host::getParams(vm_params_t params)
263 simgrid::simix::kernel([&]() {
264 this->extension<simgrid::surf::Host>()->getParams(params);
268 void Host::setParams(vm_params_t params)
270 simgrid::simix::kernel([&]() {
271 this->extension<simgrid::surf::Host>()->setParams(params);
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
280 xbt_dict_t Host::getMountedStorageList()
282 return simgrid::simix::kernel([&] {
283 return this->extension<simgrid::surf::Host>()->getMountedStorageList();
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
292 xbt_dynar_t Host::getAttachedStorageList()
294 return simgrid::simix::kernel([&] {
295 return this->extension<simgrid::surf::Host>()->getAttachedStorageList();