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