1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "simgrid/s4u/engine.hpp"
13 #include "simgrid/s4u/host.hpp"
14 #include "simgrid/s4u/storage.hpp"
15 #include "simgrid/simix.hpp"
16 #include "src/kernel/routing/NetPoint.hpp"
17 #include "src/msg/msg_private.h"
18 #include "src/simix/ActorImpl.hpp"
19 #include "src/simix/smx_private.h"
20 #include "src/surf/HostImpl.hpp"
21 #include "src/surf/cpu_interface.hpp"
24 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
26 int USER_HOST_LEVEL = -1;
31 template class Extendable<simgrid::s4u::Host>;
36 std::map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
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 simgrid::xbt::signal<void(Host&)> Host::onSpeedChange;
43 Host::Host(const char* name)
46 xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
47 host_list[name_] = this;
48 new simgrid::surf::HostImpl(this);
53 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
56 if (pimpl_netpoint != nullptr) // not removed yet by a children class
57 simgrid::s4u::Engine::instance()->netpointUnregister(pimpl_netpoint);
62 /** @brief Fire the required callbacks and destroy the object
64 * Don't delete directly an Host, call h->destroy() instead.
66 * This is cumbersome but this is the simplest solution to ensure that the
67 * onDestruction() callback receives a valid object (because of the destructor
68 * order in a class hierarchy).
72 if (!currentlyDestroying_) {
73 currentlyDestroying_ = true;
75 host_list.erase(name_);
80 Host* Host::by_name(std::string name)
82 return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
84 Host* Host::by_name_or_null(const char* name)
86 return by_name_or_null(std::string(name));
88 Host* Host::by_name_or_null(std::string name)
90 if (host_list.find(name) == host_list.end())
92 return host_list.at(name);
95 Host *Host::current(){
96 smx_actor_t smx_proc = SIMIX_process_self();
97 if (smx_proc == nullptr)
98 xbt_die("Cannot call Host::current() from the maestro context");
99 return smx_proc->host;
102 void Host::turnOn() {
104 simgrid::simix::kernelImmediate([this] {
105 this->extension<simgrid::simix::Host>()->turnOn();
106 this->pimpl_cpu->turnOn();
107 onStateChange(*this);
112 void Host::turnOff() {
114 smx_actor_t self = SIMIX_process_self();
115 simgrid::simix::kernelImmediate([this, self] {
116 SIMIX_host_off(this, self);
117 onStateChange(*this);
123 return this->pimpl_cpu->isOn();
126 int Host::pstatesCount() const {
127 return this->pimpl_cpu->getNbPStates();
131 * \brief Return the list of actors attached to an host.
133 * \param whereto a vector in which we should push actors living on that host
135 void Host::actorList(std::vector<ActorPtr>* whereto)
137 smx_actor_t actor = NULL;
138 xbt_swag_foreach(actor, this->extension<simgrid::simix::Host>()->process_list)
140 whereto->push_back(actor->ciface());
145 * \brief Find a route toward another host
147 * \param dest [IN] where to
148 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
149 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
150 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
151 * \pre links!=nullptr
153 * walk through the routing components tree and find a route between hosts
154 * by calling each "get_route" function in each routing component.
156 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
158 std::vector<surf::LinkImpl*> linkImpls;
159 this->routeTo(dest, &linkImpls, latency);
160 for (surf::LinkImpl* l : linkImpls)
161 links->push_back(&l->piface_);
163 /** @brief Just like Host::routeTo, but filling an array of link implementations */
164 void Host::routeTo(Host* dest, std::vector<surf::LinkImpl*>* links, double* latency)
166 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(pimpl_netpoint, dest->pimpl_netpoint, links, latency);
167 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
168 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
169 (latency == nullptr ? -1 : *latency));
170 for (auto link : *links)
171 XBT_CDEBUG(surf_route, "Link %s", link->cname());
175 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
176 if (mounts == nullptr) {
177 mounts = new boost::unordered_map<std::string, Storage*> ();
179 xbt_dict_t dict = this->mountedStoragesAsDict();
181 xbt_dict_cursor_t cursor;
184 xbt_dict_foreach(dict, cursor, mountname, storagename) {
185 mounts->insert({mountname, &Storage::byName(storagename)});
187 xbt_dict_free(&dict);
193 /** Get the properties assigned to a host */
194 xbt_dict_t Host::properties() {
195 return simgrid::simix::kernelImmediate([this] {
196 return this->pimpl_->getProperties();
200 /** Retrieve the property value (or nullptr if not set) */
201 const char*Host::property(const char*key) {
202 return this->pimpl_->getProperty(key);
204 void Host::setProperty(const char*key, const char *value){
205 simgrid::simix::kernelImmediate([this, key, value] {
206 this->pimpl_->setProperty(key, value);
210 /** Get the processes attached to the host */
211 void Host::processes(std::vector<ActorPtr>* list)
213 smx_actor_t actor = NULL;
214 xbt_swag_foreach(actor, this->extension<simgrid::simix::Host>()->process_list) {
215 list->push_back(actor->iface());
219 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
220 double Host::getPstateSpeed(int pstate_index)
222 return simgrid::simix::kernelImmediate([this, pstate_index] {
223 return this->pimpl_cpu->getPstateSpeed(pstate_index);
227 /** @brief Get the peak processor speed (in flops/s), at the current pstate */
228 double Host::speed() {
229 return pimpl_cpu->getSpeed(1.0);
232 /** @brief Returns the number of core of the processor. */
233 int Host::coreCount() {
234 return pimpl_cpu->coreCount();
237 /** @brief Set the pstate at which the host should run */
238 void Host::setPstate(int pstate_index)
240 simgrid::simix::kernelImmediate([this, pstate_index] {
241 this->pimpl_cpu->setPState(pstate_index);
244 /** @brief Retrieve the pstate at which the host is currently running */
247 return this->pimpl_cpu->getPState();
251 * \ingroup simix_storage_management
252 * \brief Returns the list of storages mounted on an host.
253 * \return a dict containing all storages mounted on the host
255 xbt_dict_t Host::mountedStoragesAsDict()
257 return simgrid::simix::kernelImmediate([this] {
258 return this->pimpl_->getMountedStorageList();
263 * \ingroup simix_storage_management
264 * \brief Returns the list of storages attached to an host.
265 * \return a dict containing all storages attached to the host
267 void Host::attachedStorages(std::vector<const char*>* storages)
269 simgrid::simix::kernelImmediate([this, storages] {
270 this->pimpl_->getAttachedStorageList(storages);
274 } // namespace simgrid