Logo AND Algorithmique Numérique Distribuée

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