1 /* Copyright (c) 2013-2015. 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 "simgrid/Host.hpp"
10 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
12 size_t sg_host_count()
14 return xbt_dict_length(host_list);
17 void* sg_host_get_facet(sg_host_t host, size_t facet)
19 return host->facet(facet);
22 const char *sg_host_get_name(sg_host_t host)
24 return host->id().c_str();
27 size_t sg_host_add_level(void(*deleter)(void*))
29 return simgrid::Host::add_level(deleter);
32 sg_host_t sg_host_by_name(const char *name)
34 return simgrid::Host::find_host(name);
37 sg_host_t sg_host_by_name_or_create(const char *name)
39 return simgrid::Host::get_host(name);
42 xbt_dynar_t sg_hosts_as_dynar(void)
44 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
46 xbt_dict_cursor_t cursor = nullptr;
47 const char* name = nullptr;
48 simgrid::Host* host = nullptr;
49 xbt_dict_foreach(host_list, cursor, name, host)
50 if(routing_get_network_element_type(name) == SURF_NETWORK_ELEMENT_HOST)
51 xbt_dynar_push(res, &host);
55 // ========= Layering madness ==============
60 int ROUTING_HOST_LEVEL;
64 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme
65 #include "src/simdag/private.h" // __SD_workstation_destroy. FIXME: killme
66 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme
67 #include "src/surf/cpu_interface.hpp"
68 #include "src/surf/surf_routing.hpp"
70 static XBT_INLINE void surf_cpu_free(void *r) {
71 delete static_cast<simgrid::surf::Cpu*>(r);
73 static XBT_INLINE void routing_asr_host_free(void *p) {
74 delete static_cast<simgrid::surf::RoutingEdge*>(p);
79 MSG_HOST_LEVEL = simgrid::Host::add_level([](void *p) {
80 __MSG_host_priv_free((msg_host_priv_t) p);
82 SD_HOST_LEVEL = simgrid::Host::add_level(__SD_workstation_destroy);
83 SIMIX_HOST_LEVEL = simgrid::Host::add_level(SIMIX_host_destroy);
84 SURF_CPU_LEVEL = simgrid::Host::add_level(surf_cpu_free);
85 ROUTING_HOST_LEVEL = simgrid::Host::add_level(routing_asr_host_free);
86 USER_HOST_LEVEL = simgrid::Host::add_level(NULL);
89 // ========== User data Layer ==========
90 void *sg_host_user(sg_host_t host) {
91 return host->facet(USER_HOST_LEVEL);
93 void sg_host_user_set(sg_host_t host, void* userdata) {
94 host->set_facet(USER_HOST_LEVEL,userdata);
96 void sg_host_user_destroy(sg_host_t host) {
97 host->set_facet(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->facet(MSG_HOST_LEVEL);
104 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
105 host->set_facet(MSG_HOST_LEVEL, smx_host);
107 void sg_host_msg_destroy(sg_host_t host) {
108 host->set_facet(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->facet(SD_HOST_LEVEL);
114 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
115 host->set_facet(SD_HOST_LEVEL, smx_host);
117 void sg_host_sd_destroy(sg_host_t host) {
118 host->set_facet(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->facet(SIMIX_HOST_LEVEL);
125 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
126 host->set_facet(SIMIX_HOST_LEVEL, smx_host);
128 void sg_host_simix_destroy(sg_host_t host) {
129 host->set_facet(SIMIX_HOST_LEVEL, nullptr);
132 // ========== SURF CPU ============
133 surf_cpu_t sg_host_surfcpu(sg_host_t host) {
134 return (surf_cpu_t) host->facet(SURF_CPU_LEVEL);
136 void sg_host_surfcpu_set(sg_host_t host, surf_cpu_t cpu) {
137 host->set_facet(SURF_CPU_LEVEL, cpu);
139 void sg_host_surfcpu_register(sg_host_t host, surf_cpu_t cpu)
141 surf_callback_emit(simgrid::surf::cpuCreatedCallbacks, cpu);
142 surf_callback_emit(simgrid::surf::cpuStateChangedCallbacks, cpu, SURF_RESOURCE_ON, cpu->getState());
143 sg_host_surfcpu_set(host, cpu);
145 void sg_host_surfcpu_destroy(sg_host_t host) {
146 host->set_facet(SURF_CPU_LEVEL, nullptr);
148 // ========== RoutingEdge ============
149 surf_RoutingEdge *sg_host_edge(sg_host_t host) {
150 return (surf_RoutingEdge*) host->facet(ROUTING_HOST_LEVEL);
152 void sg_host_edge_set(sg_host_t host, surf_RoutingEdge *edge) {
153 host->set_facet(ROUTING_HOST_LEVEL, edge);
155 void sg_host_edge_destroy(sg_host_t host, int do_callback) {
156 host->set_facet(ROUTING_HOST_LEVEL, nullptr, do_callback);
159 // =========== user-level functions ===============
160 // ================================================
161 double sg_host_get_speed(sg_host_t host){
162 return surf_host_get_speed(host, 1.0);
165 double sg_host_get_available_speed(sg_host_t host){
166 return surf_host_get_available_speed(host);
168 /** @brief Returns the number of core of the processor. */
169 int sg_host_get_core(sg_host_t host) {
170 return surf_host_get_core(host);
172 /** @brief Returns the state of a host.
173 * @return 1 if the host is active or 0 if it has crashed.
175 int sg_host_get_state(sg_host_t host) {
176 return surf_host_get_state(surf_host_resource_priv(host));
179 /** @brief Returns the total energy consumed by the host (in Joules).
181 * See also @ref SURF_plugin_energy.
183 double sg_host_get_consumed_energy(sg_host_t host) {
184 return surf_host_get_consumed_energy(host);
187 /** @brief Returns the number of power states for a host.
189 * See also @ref SURF_plugin_energy.
191 int sg_host_get_nb_pstates(sg_host_t host) {
192 return surf_host_get_nb_pstates(host);
195 /** @brief Gets the pstate at which that host currently runs.
197 * See also @ref SURF_plugin_energy.
199 int sg_host_get_pstate(sg_host_t host) {
200 return surf_host_get_pstate(host);
205 Host::Host(std::string id)
214 Host* Host::find_host(const char* name)
216 return (Host*) xbt_dict_get_or_null(host_list, name);
219 Host* Host::get_host(const char* name)
221 Host* host = find_host(name);
222 if (host == nullptr) {
223 host = new Host(name);
224 xbt_dict_set(host_list, name, host, NULL);