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 const char *sg_host_get_name(sg_host_t host)
19 return host->getName().c_str();
22 void* sg_host_extension_get(sg_host_t host, size_t ext)
24 return host->extension(ext);
27 size_t sg_host_extension_create(void(*deleter)(void*))
29 return simgrid::Host::extension_create(deleter);
32 sg_host_t sg_host_by_name(const char *name)
34 return simgrid::Host::by_name_or_null(name);
37 sg_host_t sg_host_by_name_or_create(const char *name)
39 return simgrid::Host::by_name_or_create(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;
63 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme
64 #include "src/simdag/private.h" // __SD_workstation_destroy. FIXME: killme
65 #include "src/simix/smx_host_private.h" // SIMIX_host_destroy. FIXME: killme
66 #include "src/surf/cpu_interface.hpp"
67 #include "src/surf/surf_routing.hpp"
71 MSG_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
72 __MSG_host_priv_free((msg_host_priv_t) p);
75 ROUTING_HOST_LEVEL = simgrid::Host::extension_create([](void *p) {
76 delete static_cast<simgrid::surf::NetCard*>(p);
79 SD_HOST_LEVEL = simgrid::Host::extension_create(__SD_workstation_destroy);
80 SIMIX_HOST_LEVEL = simgrid::Host::extension_create(SIMIX_host_destroy);
81 USER_HOST_LEVEL = simgrid::Host::extension_create(NULL);
84 // ========== User data Layer ==========
85 void *sg_host_user(sg_host_t host) {
86 return host->extension(USER_HOST_LEVEL);
88 void sg_host_user_set(sg_host_t host, void* userdata) {
89 host->extension_set(USER_HOST_LEVEL,userdata);
91 void sg_host_user_destroy(sg_host_t host) {
92 host->extension_set(USER_HOST_LEVEL, nullptr);
95 // ========== MSG Layer ==============
96 msg_host_priv_t sg_host_msg(sg_host_t host) {
97 return (msg_host_priv_t) host->extension(MSG_HOST_LEVEL);
99 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
100 host->extension_set(MSG_HOST_LEVEL, smx_host);
102 void sg_host_msg_destroy(sg_host_t host) {
103 host->extension_set(MSG_HOST_LEVEL, nullptr);
105 // ========== SimDag Layer ==============
106 SD_workstation_priv_t sg_host_sd(sg_host_t host) {
107 return (SD_workstation_priv_t) host->extension(SD_HOST_LEVEL);
109 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
110 host->extension_set(SD_HOST_LEVEL, smx_host);
112 void sg_host_sd_destroy(sg_host_t host) {
113 host->extension_set(SD_HOST_LEVEL, nullptr);
116 // ========== Simix layer =============
117 smx_host_priv_t sg_host_simix(sg_host_t host){
118 return (smx_host_priv_t) host->extension(SIMIX_HOST_LEVEL);
120 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
121 host->extension_set(SIMIX_HOST_LEVEL, smx_host);
123 void sg_host_simix_destroy(sg_host_t host) {
124 host->extension_set(SIMIX_HOST_LEVEL, nullptr);
127 // ========== RoutingEdge ============
128 surf_NetCard *sg_host_edge(sg_host_t host) {
129 return (surf_NetCard*) host->extension(ROUTING_HOST_LEVEL);
131 void sg_host_edge_set(sg_host_t host, surf_NetCard *edge) {
132 host->extension_set(ROUTING_HOST_LEVEL, edge);
134 void sg_host_edge_destroy(sg_host_t host, int do_callback) {
135 host->extension_set(ROUTING_HOST_LEVEL, nullptr, do_callback);
138 // =========== user-level functions ===============
139 // ================================================
140 double sg_host_get_speed(sg_host_t host){
141 return surf_host_get_speed(host, 1.0);
144 double sg_host_get_available_speed(sg_host_t host){
145 return surf_host_get_available_speed(host);
147 /** @brief Returns the number of core of the processor. */
148 int sg_host_get_core(sg_host_t host) {
149 return surf_host_get_core(host);
151 /** @brief Returns the state of a host.
152 * @return 1 if the host is active or 0 if it has crashed.
154 int sg_host_get_state(sg_host_t host) {
155 return surf_host_get_state(surf_host_resource_priv(host));
158 /** @brief Returns the total energy consumed by the host (in Joules).
160 * See also @ref SURF_plugin_energy.
162 double sg_host_get_consumed_energy(sg_host_t host) {
163 return surf_host_get_consumed_energy(host);
166 /** @brief Returns the number of power states for a host.
168 * See also @ref SURF_plugin_energy.
170 int sg_host_get_nb_pstates(sg_host_t host) {
171 return surf_host_get_nb_pstates(host);
174 /** @brief Gets the pstate at which that host currently runs.
176 * See also @ref SURF_plugin_energy.
178 int sg_host_get_pstate(sg_host_t host) {
179 return surf_host_get_pstate(host);
184 Host::Host(std::string const& id)
193 Host* Host::by_name_or_null(const char* name)
195 return (Host*) xbt_dict_get_or_null(host_list, name);
198 Host* Host::by_name_or_create(const char* name)
200 Host* host = by_name_or_null(name);
201 if (host == nullptr) {
202 host = new Host(name);
203 xbt_dict_set(host_list, name, host, NULL);