Logo AND Algorithmique Numérique Distribuée

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