Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3d8550f13736f4b8ccd423f6c16684ed5617aa73
[simgrid.git] / src / s4u / s4u_host.cpp
1 /* Copyright (c) 2006-2014. 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 <string>
8 #include <functional>
9 #include <stdexcept>
10
11 #include <unordered_map>
12
13 #include "simgrid/simix.hpp"
14 #include "src/surf/HostImpl.hpp"
15 #include "xbt/log.h"
16 #include "src/msg/msg_private.h"
17 #include "src/simix/ActorImpl.hpp"
18 #include "src/simix/smx_private.h"
19 #include "src/surf/cpu_interface.hpp"
20 #include "simgrid/s4u/host.hpp"
21 #include "simgrid/s4u/storage.hpp"
22
23 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
24
25 std::unordered_map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
26
27 int MSG_HOST_LEVEL = -1;
28 int USER_HOST_LEVEL = -1;
29
30 namespace simgrid {
31
32 namespace xbt {
33 template class Extendable<simgrid::s4u::Host>;
34 }
35
36 namespace s4u {
37
38 simgrid::xbt::signal<void(Host&)> Host::onCreation;
39 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
40 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
41
42 Host::Host(const char* name)
43   : name_(name)
44 {
45   xbt_assert(sg_host_by_name(name) == nullptr, "Refusing to create a second host named '%s'.", name);
46   host_list[name_] = this;
47 }
48
49 Host::~Host()
50 {
51   xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
52
53   delete pimpl_;
54   delete pimpl_cpu;
55   delete pimpl_netcard;
56   delete mounts;
57 }
58
59 /** @brief Fire the required callbacks and destroy the object
60  *
61  * Don't delete directly an Host, call h->destroy() instead.
62  *
63  * This is cumbersome but this is the simplest solution to ensure that the
64  * onDestruction() callback receives a valid object (because of the destructor
65  * order in a class hierarchy).
66  */
67 void Host::destroy()
68 {
69   if (!currentlyDestroying_) {
70     currentlyDestroying_ = true;
71     onDestruction(*this);
72     host_list.erase(name_);
73     delete this;
74   }
75 }
76
77 Host* Host::by_name(std::string name)
78 {
79   return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
80 }
81 Host* Host::by_name_or_null(const char* name)
82 {
83   return by_name_or_null(std::string(name));
84 }
85 Host* Host::by_name_or_null(std::string name)
86 {
87   if (host_list.find(name) == host_list.end())
88     return nullptr;
89   return host_list.at(name);
90 }
91
92 Host *Host::current(){
93   smx_actor_t smx_proc = SIMIX_process_self();
94   if (smx_proc == nullptr)
95     xbt_die("Cannot call Host::current() from the maestro context");
96   return smx_proc->host;
97 }
98
99 void Host::turnOn() {
100   if (isOff()) {
101     simgrid::simix::kernelImmediate([&]{
102       this->extension<simgrid::simix::Host>()->turnOn();
103       this->pimpl_cpu->turnOn();
104     });
105   }
106 }
107
108 void Host::turnOff() {
109   if (isOn()) {
110     simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
111   }
112 }
113
114 bool Host::isOn() {
115   return this->pimpl_cpu->isOn();
116 }
117
118 int Host::pstatesCount() const {
119   return this->pimpl_cpu->getNbPStates();
120 }
121
122 /**
123  * \brief Find a route toward another host
124  *
125  * \param dest [IN] where to
126  * \param route [OUT] where to store the list of links (must exist, cannot be nullptr).
127  * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
128  *                It is the caller responsibility to initialize latency to 0 (we add to provided route)
129  * \pre route!=nullptr
130  *
131  * walk through the routing components tree and find a route between hosts
132  * by calling each "get_route" function in each routing component.
133  */
134 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
135 {
136   simgrid::kernel::routing::AsImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
137   if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
138     XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
139                (latency == nullptr ? -1 : *latency));
140     for (auto link : *links)
141       XBT_CDEBUG(surf_route, "Link %s", link->getName());
142   }
143 }
144
145 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
146   if (mounts == nullptr) {
147     mounts = new boost::unordered_map<std::string, Storage*> ();
148
149     xbt_dict_t dict = this->mountedStoragesAsDict();
150
151     xbt_dict_cursor_t cursor;
152     char *mountname;
153     char *storagename;
154     xbt_dict_foreach(dict, cursor, mountname, storagename) {
155       mounts->insert({mountname, &Storage::byName(storagename)});
156     }
157     xbt_dict_free(&dict);
158   }
159
160   return *mounts;
161 }
162
163 /** Get the properties assigned to a host */
164 xbt_dict_t Host::properties() {
165   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
166 }
167
168 /** Retrieve the property value (or nullptr if not set) */
169 const char*Host::property(const char*key) {
170   return this->pimpl_->getProperty(key);
171 }
172 void Host::setProperty(const char*key, const char *value){
173   simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
174 }
175
176 /** Get the processes attached to the host */
177 xbt_swag_t Host::processes()
178 {
179   return simgrid::simix::kernelImmediate([&]() {
180     return this->extension<simgrid::simix::Host>()->process_list;
181   });
182 }
183
184 /** Get the peak power of a host */
185 double Host::getPstateSpeedCurrent()
186 {
187   return simgrid::simix::kernelImmediate([&] {
188     return this->pimpl_cpu->getPstateSpeedCurrent();
189   });
190 }
191
192 /** Get one power peak (in flops/s) of a host at a given pstate */
193 double Host::getPstateSpeed(int pstate_index)
194 {
195   return simgrid::simix::kernelImmediate([&] {
196     return this->pimpl_cpu->getPstateSpeed(pstate_index);
197   });
198 }
199
200 /** @brief Get the speed of the cpu associated to a host */
201 double Host::speed() {
202   return pimpl_cpu->getSpeed(1.0);
203 }
204 /** @brief Returns the number of core of the processor. */
205 int Host::coreCount() {
206   return pimpl_cpu->coreCount();
207 }
208
209 /** @brief Set the pstate at which the host should run */
210 void Host::setPstate(int pstate_index)
211 {
212   simgrid::simix::kernelImmediate(std::bind(
213       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
214   ));
215 }
216 /** @brief Retrieve the pstate at which the host is currently running */
217 int Host::pstate()
218 {
219   return pimpl_cpu->getPState();
220 }
221
222 /**
223  * \ingroup simix_storage_management
224  * \brief Returns the list of storages mounted on an host.
225  * \return a dict containing all storages mounted on the host
226  */
227 xbt_dict_t Host::mountedStoragesAsDict()
228 {
229   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
230 }
231
232 /**
233  * \ingroup simix_storage_management
234  * \brief Returns the list of storages attached to an host.
235  * \return a dict containing all storages attached to the host
236  */
237 xbt_dynar_t Host::attachedStorages()
238 {
239   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
240 }
241
242 } // namespace simgrid
243 } // namespace s4u