Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use simgrid::Host instead of xbt_dictelt_t for root main object
[simgrid.git] / src / simgrid / host.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/dict.h"
8 #include "simgrid/host.h"
9 #include "simgrid/Host.hpp"
10 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
11
12 size_t sg_host_count()
13 {
14   return xbt_dict_length(host_list);
15 }
16
17 void* sg_host_get_facet(sg_host_t host, size_t facet)
18 {
19   return host->facet(facet);
20 }
21
22 const char *sg_host_get_name(sg_host_t host)
23 {
24         return host->id().c_str();
25 }
26
27 size_t sg_host_add_level(void(*deleter)(void*))
28 {
29   return simgrid::Host::add_level(deleter);
30 }
31
32 sg_host_t sg_host_by_name(const char *name)
33 {
34   return simgrid::Host::find_host(name);
35 }
36
37 sg_host_t sg_host_by_name_or_create(const char *name)
38 {
39   return simgrid::Host::get_host(name);
40 }
41
42 xbt_dynar_t sg_hosts_as_dynar(void)
43 {
44         xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),NULL);
45
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);
52         return res;
53 }
54
55 // ========= Layering madness ==============
56
57 int MSG_HOST_LEVEL;
58 int SD_HOST_LEVEL;
59 int SIMIX_HOST_LEVEL;
60 int ROUTING_HOST_LEVEL;
61 int SURF_CPU_LEVEL;
62 int USER_HOST_LEVEL;
63
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"
69
70 static XBT_INLINE void surf_cpu_free(void *r) {
71   delete static_cast<simgrid::surf::Cpu*>(r);
72 }
73 static XBT_INLINE void routing_asr_host_free(void *p) {
74   delete static_cast<simgrid::surf::RoutingEdge*>(p);
75 }
76
77 void sg_host_init()
78 {
79   MSG_HOST_LEVEL = simgrid::Host::add_level([](void *p) {
80     __MSG_host_priv_free((msg_host_priv_t) p);
81   });
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);
87 }
88
89 // ========== User data Layer ==========
90 void *sg_host_user(sg_host_t host) {
91   return host->facet(USER_HOST_LEVEL);
92 }
93 void sg_host_user_set(sg_host_t host, void* userdata) {
94   host->set_facet(USER_HOST_LEVEL,userdata);
95 }
96 void sg_host_user_destroy(sg_host_t host) {
97   host->set_facet(USER_HOST_LEVEL, nullptr);
98 }
99
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);
103 }
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);
106 }
107 void sg_host_msg_destroy(sg_host_t host) {
108   host->set_facet(MSG_HOST_LEVEL, nullptr);
109 }
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);
113 }
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);
116 }
117 void sg_host_sd_destroy(sg_host_t host) {
118   host->set_facet(SD_HOST_LEVEL, nullptr);
119 }
120
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);
124 }
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);
127 }
128 void sg_host_simix_destroy(sg_host_t host) {
129   host->set_facet(SIMIX_HOST_LEVEL, nullptr);
130 }
131
132 // ========== SURF CPU ============
133 surf_cpu_t sg_host_surfcpu(sg_host_t host) {
134         return (surf_cpu_t) host->facet(SURF_CPU_LEVEL);
135 }
136 void sg_host_surfcpu_set(sg_host_t host, surf_cpu_t cpu) {
137   host->set_facet(SURF_CPU_LEVEL, cpu);
138 }
139 void sg_host_surfcpu_register(sg_host_t host, surf_cpu_t cpu)
140 {
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);
144 }
145 void sg_host_surfcpu_destroy(sg_host_t host) {
146   host->set_facet(SURF_CPU_LEVEL, nullptr);
147 }
148 // ========== RoutingEdge ============
149 surf_RoutingEdge *sg_host_edge(sg_host_t host) {
150         return (surf_RoutingEdge*) host->facet(ROUTING_HOST_LEVEL);
151 }
152 void sg_host_edge_set(sg_host_t host, surf_RoutingEdge *edge) {
153   host->set_facet(ROUTING_HOST_LEVEL, edge);
154 }
155 void sg_host_edge_destroy(sg_host_t host, int do_callback) {
156   host->set_facet(ROUTING_HOST_LEVEL, nullptr, do_callback);
157 }
158
159 // =========== user-level functions ===============
160 // ================================================
161 double sg_host_get_speed(sg_host_t host){
162   return surf_host_get_speed(host, 1.0);
163 }
164
165 double sg_host_get_available_speed(sg_host_t host){
166   return surf_host_get_available_speed(host);
167 }
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);
171 }
172 /** @brief Returns the state of a host.
173  *  @return 1 if the host is active or 0 if it has crashed.
174  */
175 int sg_host_get_state(sg_host_t host) {
176         return surf_host_get_state(surf_host_resource_priv(host));
177 }
178
179 /** @brief Returns the total energy consumed by the host (in Joules).
180  *
181  *  See also @ref SURF_plugin_energy.
182  */
183 double sg_host_get_consumed_energy(sg_host_t host) {
184         return surf_host_get_consumed_energy(host);
185 }
186
187 /** @brief Returns the number of power states for a host.
188  *
189  *  See also @ref SURF_plugin_energy.
190  */
191 int sg_host_get_nb_pstates(sg_host_t host) {
192         return surf_host_get_nb_pstates(host);
193 }
194
195 /** @brief Gets the pstate at which that host currently runs.
196  *
197  *  See also @ref SURF_plugin_energy.
198  */
199 int sg_host_get_pstate(sg_host_t host) {
200         return surf_host_get_pstate(host);
201 }
202
203 namespace simgrid {
204
205 Host::Host(std::string id)
206   : id_(std::move(id))
207 {
208 }
209
210 Host::~Host()
211 {
212 }
213
214 Host* Host::find_host(const char* name)
215 {
216   return (Host*) xbt_dict_get_or_null(host_list, name);
217 }
218
219 Host* Host::get_host(const char* name)
220 {
221   Host* host = find_host(name);
222   if (host == nullptr) {
223     host = new Host(name);
224     xbt_dict_set(host_list, name, host, NULL);
225   }
226   return host;
227 }
228
229 }