Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c4c9f2042b0e921ce43a53ff3f682ebffdb6fbe1
[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::by_name_or_null(name);
35 }
36
37 sg_host_t sg_host_by_name_or_create(const char *name)
38 {
39   return simgrid::Host::by_name_or_create(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 USER_HOST_LEVEL;
62
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"
68
69 static XBT_INLINE void surf_cpu_free(void *r) {
70   delete static_cast<simgrid::surf::Cpu*>(r);
71 }
72 static XBT_INLINE void routing_asr_host_free(void *p) {
73   delete static_cast<simgrid::surf::RoutingEdge*>(p);
74 }
75
76 void sg_host_init()
77 {
78   MSG_HOST_LEVEL = simgrid::Host::add_level([](void *p) {
79     __MSG_host_priv_free((msg_host_priv_t) p);
80   });
81   SD_HOST_LEVEL = simgrid::Host::add_level(__SD_workstation_destroy);
82   SIMIX_HOST_LEVEL = simgrid::Host::add_level(SIMIX_host_destroy);
83   simgrid::surf::Cpu::init();
84   ROUTING_HOST_LEVEL = simgrid::Host::add_level(routing_asr_host_free);
85   USER_HOST_LEVEL = simgrid::Host::add_level(NULL);
86 }
87
88 // ========== User data Layer ==========
89 void *sg_host_user(sg_host_t host) {
90   return host->facet(USER_HOST_LEVEL);
91 }
92 void sg_host_user_set(sg_host_t host, void* userdata) {
93   host->set_facet(USER_HOST_LEVEL,userdata);
94 }
95 void sg_host_user_destroy(sg_host_t host) {
96   host->set_facet(USER_HOST_LEVEL, nullptr);
97 }
98
99 // ========== MSG Layer ==============
100 msg_host_priv_t sg_host_msg(sg_host_t host) {
101         return (msg_host_priv_t) host->facet(MSG_HOST_LEVEL);
102 }
103 void sg_host_msg_set(sg_host_t host, msg_host_priv_t smx_host) {
104   host->set_facet(MSG_HOST_LEVEL, smx_host);
105 }
106 void sg_host_msg_destroy(sg_host_t host) {
107   host->set_facet(MSG_HOST_LEVEL, nullptr);
108 }
109 // ========== SimDag Layer ==============
110 SD_workstation_priv_t sg_host_sd(sg_host_t host) {
111   return (SD_workstation_priv_t) host->facet(SD_HOST_LEVEL);
112 }
113 void sg_host_sd_set(sg_host_t host, SD_workstation_priv_t smx_host) {
114   host->set_facet(SD_HOST_LEVEL, smx_host);
115 }
116 void sg_host_sd_destroy(sg_host_t host) {
117   host->set_facet(SD_HOST_LEVEL, nullptr);
118 }
119
120 // ========== Simix layer =============
121 smx_host_priv_t sg_host_simix(sg_host_t host){
122   return (smx_host_priv_t) host->facet(SIMIX_HOST_LEVEL);
123 }
124 void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) {
125   host->set_facet(SIMIX_HOST_LEVEL, smx_host);
126 }
127 void sg_host_simix_destroy(sg_host_t host) {
128   host->set_facet(SIMIX_HOST_LEVEL, nullptr);
129 }
130
131 // ========== SURF CPU ============
132 surf_cpu_t sg_host_surfcpu(sg_host_t host) {
133         return host->facet<simgrid::surf::Cpu>();
134 }
135 void sg_host_surfcpu_set(sg_host_t host, surf_cpu_t cpu) {
136   host->set_facet(simgrid::surf::Cpu::LEVEL, cpu);
137 }
138 void sg_host_surfcpu_destroy(sg_host_t host) {
139   host->set_facet<simgrid::surf::Cpu>(nullptr);
140 }
141 // ========== RoutingEdge ============
142 surf_RoutingEdge *sg_host_edge(sg_host_t host) {
143         return (surf_RoutingEdge*) host->facet(ROUTING_HOST_LEVEL);
144 }
145 void sg_host_edge_set(sg_host_t host, surf_RoutingEdge *edge) {
146   host->set_facet(ROUTING_HOST_LEVEL, edge);
147 }
148 void sg_host_edge_destroy(sg_host_t host, int do_callback) {
149   host->set_facet(ROUTING_HOST_LEVEL, nullptr, do_callback);
150 }
151
152 // =========== user-level functions ===============
153 // ================================================
154 double sg_host_get_speed(sg_host_t host){
155   return surf_host_get_speed(host, 1.0);
156 }
157
158 double sg_host_get_available_speed(sg_host_t host){
159   return surf_host_get_available_speed(host);
160 }
161 /** @brief Returns the number of core of the processor. */
162 int sg_host_get_core(sg_host_t host) {
163         return surf_host_get_core(host);
164 }
165 /** @brief Returns the state of a host.
166  *  @return 1 if the host is active or 0 if it has crashed.
167  */
168 int sg_host_get_state(sg_host_t host) {
169         return surf_host_get_state(surf_host_resource_priv(host));
170 }
171
172 /** @brief Returns the total energy consumed by the host (in Joules).
173  *
174  *  See also @ref SURF_plugin_energy.
175  */
176 double sg_host_get_consumed_energy(sg_host_t host) {
177         return surf_host_get_consumed_energy(host);
178 }
179
180 /** @brief Returns the number of power states for a host.
181  *
182  *  See also @ref SURF_plugin_energy.
183  */
184 int sg_host_get_nb_pstates(sg_host_t host) {
185         return surf_host_get_nb_pstates(host);
186 }
187
188 /** @brief Gets the pstate at which that host currently runs.
189  *
190  *  See also @ref SURF_plugin_energy.
191  */
192 int sg_host_get_pstate(sg_host_t host) {
193         return surf_host_get_pstate(host);
194 }
195
196 namespace simgrid {
197
198 Host::Host(std::string const& id)
199   : id_(id)
200 {
201 }
202
203 Host::~Host()
204 {
205 }
206
207 Host* Host::by_name_or_null(const char* name)
208 {
209   return (Host*) xbt_dict_get_or_null(host_list, name);
210 }
211
212 Host* Host::by_name_or_create(const char* name)
213 {
214   Host* host = by_name_or_null(name);
215   if (host == nullptr) {
216     host = new Host(name);
217     xbt_dict_set(host_list, name, host, NULL);
218   }
219   return host;
220 }
221
222 }