Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
hide a global data container
[simgrid.git] / src / simgrid / host.cpp
1 /* Copyright (c) 2013-2016. 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 <xbt/Extendable.hpp>
10 #include <simgrid/s4u/host.hpp>
11
12 #include "src/surf/HostImpl.hpp"
13 #include "surf/surf.h" // routing_get_network_element_type FIXME:killme
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
16
17 extern xbt_dict_t host_list; // FIXME:killme don't dupplicate the content of s4u::Host this way
18
19 void sg_host_exit()
20 {
21   xbt_dict_free(&host_list);
22 }
23
24 size_t sg_host_count()
25 {
26   return xbt_dict_length(host_list);
27 }
28 /** @brief Returns the host list
29  *
30  * Uses sg_host_count() to know the array size.
31  *
32  * \return an array of \ref sg_host_t containing all the hosts in the platform.
33  * \remark The host order in this array is generally different from the
34  * creation/declaration order in the XML platform (we use a hash table
35  * internally).
36  * \see sg_host_count()
37  */
38 sg_host_t *sg_host_list() {
39   xbt_assert(sg_host_count() > 0, "There is no host!");
40   return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
41 }
42
43 const char *sg_host_get_name(sg_host_t host)
44 {
45   return host->name().c_str();
46 }
47
48 void* sg_host_extension_get(sg_host_t host, size_t ext)
49 {
50   return host->extension(ext);
51 }
52
53 size_t sg_host_extension_create(void(*deleter)(void*))
54 {
55   return simgrid::s4u::Host::extension_create(deleter);
56 }
57
58 sg_host_t sg_host_by_name(const char *name)
59 {
60   return simgrid::s4u::Host::by_name_or_null(name);
61 }
62
63 xbt_dynar_t sg_hosts_as_dynar()
64 {
65   xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr);
66
67   xbt_dict_cursor_t cursor = nullptr;
68   const char* name = nullptr;
69   simgrid::s4u::Host* host = nullptr;
70   xbt_dict_foreach(host_list, cursor, name, host)
71     if (host && host->pimpl_netcard && host->pimpl_netcard->isHost())
72        xbt_dynar_push(res, &host);
73   return res;
74 }
75
76 // ========= Layering madness ==============*
77
78 #include "src/surf/cpu_interface.hpp"
79 #include "src/surf/surf_routing.hpp"
80
81 // ========== User data Layer ==========
82 void *sg_host_user(sg_host_t host) {
83   return host->extension(USER_HOST_LEVEL);
84 }
85 void sg_host_user_set(sg_host_t host, void* userdata) {
86   host->extension_set(USER_HOST_LEVEL,userdata);
87 }
88 void sg_host_user_destroy(sg_host_t host) {
89   host->extension_set(USER_HOST_LEVEL, nullptr);
90 }
91
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);
95 }
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);
98 }
99
100 // ========== Simix layer =============
101 #include "src/simix/smx_host_private.h"
102 smx_host_priv_t sg_host_simix(sg_host_t host){
103   return host->extension<simgrid::simix::Host>();
104 }
105
106 // ========= storage related functions ============
107 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t host){
108   return host->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
109 }
110
111 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t host){
112   return host->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
113 }
114
115
116 // =========== user-level functions ===============
117 // ================================================
118 /** @brief Returns the total speed of a host */
119 double sg_host_speed(sg_host_t host)
120 {
121   return host->speed();
122 }
123
124 double sg_host_get_available_speed(sg_host_t host)
125 {
126   return host->pimpl_cpu->getAvailableSpeed();
127 }
128
129 /** @brief Returns the number of power states for a host.
130  *
131  *  See also @ref SURF_plugin_energy.
132  */
133 int sg_host_get_nb_pstates(sg_host_t host) {
134   return host->pstatesCount();
135 }
136
137 /** @brief Gets the pstate at which that host currently runs.
138  *
139  *  See also @ref SURF_plugin_energy.
140  */
141 int sg_host_get_pstate(sg_host_t host) {
142   return host->pstate();
143 }
144 /** @brief Sets the pstate at which that host should run.
145  *
146  *  See also @ref SURF_plugin_energy.
147  */
148 void sg_host_set_pstate(sg_host_t host,int pstate) {
149   host->setPstate(pstate);
150 }
151
152 /** @brief Get the properties of an host */
153 xbt_dict_t sg_host_get_properties(sg_host_t host) {
154   return host->properties();
155 }
156
157 /** \ingroup m_host_management
158  * \brief Returns the value of a given host property
159  *
160  * \param host a host
161  * \param name a property name
162  * \return value of a property (or nullptr if property not set)
163 */
164 const char *sg_host_get_property_value(sg_host_t host, const char *name)
165 {
166   return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
167 }
168
169 /** @brief Displays debugging information about a host */
170 void sg_host_dump(sg_host_t host)
171 {
172   xbt_dict_t props;
173   xbt_dict_cursor_t cursor=nullptr;
174   char *key,*data;
175
176   XBT_INFO("Displaying host %s", sg_host_get_name(host));
177   XBT_INFO("  - speed: %.0f", host->speed());
178   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
179   props = sg_host_get_properties(host);
180
181   if (!xbt_dict_is_empty(props)){
182     XBT_INFO("  - properties:");
183
184     xbt_dict_foreach(props,cursor,key,data) {
185       XBT_INFO("    %s->%s",key,data);
186     }
187   }
188 }