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. */
8 #include "simgrid/host.h"
9 #include <xbt/Extendable.hpp>
10 #include <simgrid/s4u/host.hpp>
11 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
13 #include "src/simix/smx_private.hpp"
14 #include "src/surf/host_interface.hpp"
16 size_t sg_host_count()
18 return xbt_dict_length(host_list);
21 const char *sg_host_get_name(sg_host_t host)
23 return host->name().c_str();
26 void* sg_host_extension_get(sg_host_t host, size_t ext)
28 return host->extension(ext);
31 size_t sg_host_extension_create(void(*deleter)(void*))
33 return simgrid::s4u::Host::extension_create(deleter);
36 sg_host_t sg_host_by_name(const char *name)
38 return simgrid::s4u::Host::by_name_or_null(name);
41 sg_host_t sg_host_by_name_or_create(const char *name)
43 return simgrid::s4u::Host::by_name_or_create(name);
46 xbt_dynar_t sg_hosts_as_dynar(void)
48 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
50 xbt_dict_cursor_t cursor = nullptr;
51 const char* name = nullptr;
52 simgrid::s4u::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);
59 // ========= Layering madness ==============*
61 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme by initializing that level in msg when used
62 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme by initializing that level in simix when used
63 #include "src/surf/cpu_interface.hpp"
64 #include "src/surf/surf_routing.hpp"
68 MSG_HOST_LEVEL = simgrid::s4u::Host::extension_create([](void *p) {
69 __MSG_host_priv_free((msg_host_priv_t) p);
72 ROUTING_HOST_LEVEL = simgrid::s4u::Host::extension_create([](void *p) {
73 delete static_cast<simgrid::surf::NetCard*>(p);
76 SD_HOST_LEVEL = simgrid::s4u::Host::extension_create(NULL);
77 SIMIX_HOST_LEVEL = simgrid::s4u::Host::extension_create(SIMIX_host_destroy);
78 USER_HOST_LEVEL = simgrid::s4u::Host::extension_create(NULL);
81 // ========== User data Layer ==========
82 void *sg_host_user(sg_host_t host) {
83 return host->extension(USER_HOST_LEVEL);
85 void sg_host_user_set(sg_host_t host, void* userdata) {
86 host->extension_set(USER_HOST_LEVEL,userdata);
88 void sg_host_user_destroy(sg_host_t host) {
89 host->extension_set(USER_HOST_LEVEL, nullptr);
92 // ========== MSG Layer ==============
93 msg_host_priv_t sg_host_msg(sg_host_t host) {
94 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
96 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
97 host->extension_set(MSG_HOST_LEVEL, smx_host);
99 // ========== SimDag Layer ==============
100 SD_workstation_priv_t sg_host_sd(sg_host_t host) {
101 return (SD_workstation_priv_t) host->extension(SD_HOST_LEVEL);
103 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
104 host->extension_set(SD_HOST_LEVEL, smx_host);
106 void sg_host_sd_destroy(sg_host_t host) {
107 host->extension_set(SD_HOST_LEVEL, nullptr);
110 // ========== Simix layer =============
111 smx_host_priv_t sg_host_simix(sg_host_t host){
112 return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
114 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
115 host->extension_set(SIMIX_HOST_LEVEL, smx_host);
117 void sg_host_simix_destroy(sg_host_t host) {
118 host->extension_set(SIMIX_HOST_LEVEL, nullptr);
121 // =========== user-level functions ===============
122 // ================================================
124 double sg_host_get_available_speed(sg_host_t host){
125 return surf_host_get_available_speed(host);
127 /** @brief Returns the state of a host.
128 * @return 1 if the host is active or 0 if it has crashed.
130 int sg_host_is_on(sg_host_t host) {
131 return host->is_on();
134 /** @brief Returns the number of power states for a host.
136 * See also @ref SURF_plugin_energy.
138 int sg_host_get_nb_pstates(sg_host_t host) {
139 return host->pstates_count();
142 /** @brief Gets the pstate at which that host currently runs.
144 * See also @ref SURF_plugin_energy.
146 int sg_host_get_pstate(sg_host_t host) {
147 return host->pstate();
149 /** @brief Sets the pstate at which that host should run.
151 * See also @ref SURF_plugin_energy.
153 void sg_host_set_pstate(sg_host_t host,int pstate) {
154 host->set_pstate(pstate);
157 /** @brief Get the properties of an host */
158 xbt_dict_t sg_host_get_properties(sg_host_t host) {
159 return host->properties();