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.hpp"
18 #include "src/simix/ActorImpl.hpp"
19 #include "src/simix/smx_private.hpp"
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 simgrid::xbt::signal<void(Host&)> Host::onCreation;
37 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
38 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
39 simgrid::xbt::signal<void(Host&)> Host::onSpeedChange;
41 Host::Host(const char* name)
44 xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
45 Engine::getInstance()->addHost(std::string(name_), this);
46 new simgrid::surf::HostImpl(this);
51 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
54 if (pimpl_netpoint != nullptr) // not removed yet by a children class
55 simgrid::s4u::Engine::getInstance()->netpointUnregister(pimpl_netpoint);
60 /** @brief Fire the required callbacks and destroy the object
62 * Don't delete directly an Host, call h->destroy() instead.
64 * This is cumbersome but this is the simplest solution to ensure that the
65 * onDestruction() callback receives a valid object (because of the destructor
66 * order in a class hierarchy).
70 if (not currentlyDestroying_) {
71 currentlyDestroying_ = true;
73 Engine::getInstance()->delHost(std::string(name_));
78 Host* Host::by_name(std::string name)
80 return Engine::getInstance()->hostByName(name);
82 Host* Host::by_name(const char* name)
84 return Engine::getInstance()->hostByName(std::string(name));
86 Host* Host::by_name_or_null(const char* name)
88 return Engine::getInstance()->hostByNameOrNull(std::string(name));
90 Host* Host::by_name_or_null(std::string name)
92 return Engine::getInstance()->hostByNameOrNull(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::getPstatesCount() const
128 return this->pimpl_cpu->getNbPStates();
132 * \brief Return the list of actors attached to an host.
134 * \param whereto a vector in which we should push actors living on that host
136 void Host::actorList(std::vector<ActorPtr>* whereto)
138 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
139 whereto->push_back(actor.ciface());
144 * \brief Find a route toward another host
146 * \param dest [IN] where to
147 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
148 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
149 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
150 * \pre links!=nullptr
152 * walk through the routing components tree and find a route between hosts
153 * by calling each "get_route" function in each routing component.
155 void Host::routeTo(Host* dest, std::vector<Link*>& links, double* latency)
157 std::vector<surf::LinkImpl*> linkImpls;
158 this->routeTo(dest, linkImpls, latency);
159 for (surf::LinkImpl* const& l : linkImpls)
160 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):", getCname(), dest->getCname(),
169 (latency == nullptr ? -1 : *latency));
170 for (auto const& link : links)
171 XBT_CDEBUG(surf_route, "Link %s", link->getCname());
175 /** Get the properties assigned to a host */
176 std::map<std::string, std::string>* Host::getProperties()
178 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
181 /** Retrieve the property value (or nullptr if not set) */
182 const char* Host::getProperty(const char* key)
184 return this->pimpl_->getProperty(key);
187 void Host::setProperty(std::string key, std::string value)
189 simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
192 /** Get the processes attached to the host */
193 void Host::getProcesses(std::vector<ActorPtr>* list)
195 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
196 list->push_back(actor.iface());
200 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
201 double Host::getPstateSpeed(int pstate_index)
203 return simgrid::simix::kernelImmediate([this, pstate_index] {
204 return this->pimpl_cpu->getPstateSpeed(pstate_index);
208 /** @brief Get the peak processor speed (in flops/s), at the current pstate */
209 double Host::getSpeed()
211 return pimpl_cpu->getSpeed(1.0);
214 /** @brief Returns the number of core of the processor. */
215 int Host::getCoreCount()
217 return pimpl_cpu->coreCount();
220 /** @brief Set the pstate at which the host should run */
221 void Host::setPstate(int pstate_index)
223 simgrid::simix::kernelImmediate([this, pstate_index] {
224 this->pimpl_cpu->setPState(pstate_index);
227 /** @brief Retrieve the pstate at which the host is currently running */
228 int Host::getPstate()
230 return this->pimpl_cpu->getPState();
234 * \ingroup simix_storage_management
235 * \brief Returns the list of storages attached to an host.
236 * \return a vector containing all storages attached to the host
238 void Host::getAttachedStorages(std::vector<const char*>* storages)
240 simgrid::simix::kernelImmediate([this, storages] {
241 this->pimpl_->getAttachedStorageList(storages);
245 std::unordered_map<std::string, Storage*> const& Host::getMountedStorages()
247 if (mounts == nullptr) {
248 mounts = new std::unordered_map<std::string, Storage*>();
249 for (auto const& m : this->pimpl_->storage_) {
250 mounts->insert({m.first, &m.second->piface_});
256 void Host::execute(double flops)
258 Host* host_list[1] = {this};
259 double flops_list[1] = {flops};
260 smx_activity_t s = simcall_execution_parallel_start(nullptr /*name*/, 1, host_list, flops_list,
261 nullptr /*comm_sizes */, -1.0, -1 /*timeout*/);
262 simcall_execution_wait(s);
265 double Host::getLoad()
267 return this->pimpl_cpu->getLoad();
270 } // namespace simgrid