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 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::getInstance()->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 (not 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 auto host = host_list.find(name);
91 return host == host_list.end() ? nullptr : host->second;
94 Host *Host::current(){
95 smx_actor_t smx_proc = SIMIX_process_self();
96 if (smx_proc == nullptr)
97 xbt_die("Cannot call Host::current() from the maestro context");
98 return smx_proc->host;
101 void Host::turnOn() {
103 simgrid::simix::kernelImmediate([this] {
104 this->extension<simgrid::simix::Host>()->turnOn();
105 this->pimpl_cpu->turnOn();
106 onStateChange(*this);
111 void Host::turnOff() {
113 smx_actor_t self = SIMIX_process_self();
114 simgrid::simix::kernelImmediate([this, self] {
115 SIMIX_host_off(this, self);
116 onStateChange(*this);
122 return this->pimpl_cpu->isOn();
125 int Host::getPstatesCount() 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 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
138 whereto->push_back(actor.ciface());
143 * \brief Find a route toward another host
145 * \param dest [IN] where to
146 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
147 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
148 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
149 * \pre links!=nullptr
151 * walk through the routing components tree and find a route between hosts
152 * by calling each "get_route" function in each routing component.
154 void Host::routeTo(Host* dest, std::vector<Link*>& links, double* latency)
156 std::vector<surf::LinkImpl*> linkImpls;
157 this->routeTo(dest, linkImpls, latency);
158 for (surf::LinkImpl* const& l : linkImpls)
159 links.push_back(&l->piface_);
162 /** @brief Just like Host::routeTo, but filling an array of link implementations */
163 void Host::routeTo(Host* dest, std::vector<surf::LinkImpl*>& links, double* latency)
165 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(pimpl_netpoint, dest->pimpl_netpoint, links, latency);
166 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
167 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", getCname(), dest->getCname(),
168 (latency == nullptr ? -1 : *latency));
169 for (auto const& link : links)
170 XBT_CDEBUG(surf_route, "Link %s", link->getCname());
174 /** Get the properties assigned to a host */
175 std::map<std::string, std::string>* Host::getProperties()
177 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
180 /** Retrieve the property value (or nullptr if not set) */
181 const char* Host::getProperty(const char* key)
183 return this->pimpl_->getProperty(key);
186 void Host::setProperty(std::string key, std::string value)
188 simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
191 /** Get the processes attached to the host */
192 void Host::getProcesses(std::vector<ActorPtr>* list)
194 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
195 list->push_back(actor.iface());
199 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
200 double Host::getPstateSpeed(int pstate_index)
202 return simgrid::simix::kernelImmediate([this, pstate_index] {
203 return this->pimpl_cpu->getPstateSpeed(pstate_index);
207 /** @brief Get the peak processor speed (in flops/s), at the current pstate */
208 double Host::getSpeed()
210 return pimpl_cpu->getSpeed(1.0);
213 /** @brief Returns the number of core of the processor. */
214 int Host::getCoreCount()
216 return pimpl_cpu->coreCount();
219 /** @brief Set the pstate at which the host should run */
220 void Host::setPstate(int pstate_index)
222 simgrid::simix::kernelImmediate([this, pstate_index] {
223 this->pimpl_cpu->setPState(pstate_index);
226 /** @brief Retrieve the pstate at which the host is currently running */
227 int Host::getPstate()
229 return this->pimpl_cpu->getPState();
233 * \ingroup simix_storage_management
234 * \brief Returns the list of storages attached to an host.
235 * \return a vector containing all storages attached to the host
237 void Host::getAttachedStorages(std::vector<const char*>* storages)
239 simgrid::simix::kernelImmediate([this, storages] {
240 this->pimpl_->getAttachedStorageList(storages);
244 std::unordered_map<std::string, Storage*> const& Host::getMountedStorages()
246 if (mounts == nullptr) {
247 mounts = new std::unordered_map<std::string, Storage*>();
248 for (auto const& m : this->pimpl_->storage_) {
249 mounts->insert({m.first, &m.second->piface_});
255 void Host::execute(double flops)
257 Host* host_list[1] = {this};
258 double flops_list[1] = {flops};
259 smx_activity_t s = simcall_execution_parallel_start(nullptr /*name*/, 1, host_list, flops_list,
260 nullptr /*comm_sizes */, -1.0, -1 /*timeout*/);
261 simcall_execution_wait(s);
264 } // namespace simgrid