1 /* Copyright (c) 2006-2018. 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. */
6 #include "simgrid/kernel/routing/NetPoint.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "src/simix/smx_host_private.hpp"
9 #include "src/surf/HostImpl.hpp"
13 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
15 int USER_HOST_LEVEL = -1;
20 template class Extendable<simgrid::s4u::Host>;
25 simgrid::xbt::signal<void(Host&)> Host::onCreation;
26 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
27 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
28 simgrid::xbt::signal<void(Host&)> Host::onSpeedChange;
30 Host::Host(const char* name)
33 xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
34 Engine::getInstance()->addHost(std::string(name_), this);
35 new simgrid::surf::HostImpl(this);
40 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
43 if (pimpl_netpoint != nullptr) // not removed yet by a children class
44 simgrid::s4u::Engine::getInstance()->netpointUnregister(pimpl_netpoint);
49 /** @brief Fire the required callbacks and destroy the object
51 * Don't delete directly an Host, call h->destroy() instead.
53 * This is cumbersome but this is the simplest solution to ensure that the
54 * onDestruction() callback receives a valid object (because of the destructor
55 * order in a class hierarchy).
59 if (not currentlyDestroying_) {
60 currentlyDestroying_ = true;
62 Engine::getInstance()->delHost(std::string(name_));
67 Host* Host::by_name(std::string name)
69 return Engine::getInstance()->hostByName(name);
71 Host* Host::by_name(const char* name)
73 return Engine::getInstance()->hostByName(std::string(name));
75 Host* Host::by_name_or_null(const char* name)
77 return Engine::getInstance()->hostByNameOrNull(std::string(name));
79 Host* Host::by_name_or_null(std::string name)
81 return Engine::getInstance()->hostByNameOrNull(name);
84 Host *Host::current(){
85 smx_actor_t smx_proc = SIMIX_process_self();
86 if (smx_proc == nullptr)
87 xbt_die("Cannot call Host::current() from the maestro context");
88 return smx_proc->host;
93 simgrid::simix::kernelImmediate([this] {
94 this->extension<simgrid::simix::Host>()->turnOn();
95 this->pimpl_cpu->turn_on();
101 void Host::turnOff() {
103 smx_actor_t self = SIMIX_process_self();
104 simgrid::simix::kernelImmediate([this, self] {
105 SIMIX_host_off(this, self);
106 onStateChange(*this);
112 return this->pimpl_cpu->is_on();
115 int Host::getPstatesCount() const
117 return this->pimpl_cpu->getNbPStates();
121 * \brief Return the list of actors attached to an host.
123 * \param whereto a vector in which we should push actors living on that host
125 void Host::actorList(std::vector<ActorPtr>* whereto)
127 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
128 whereto->push_back(actor.ciface());
133 * \brief Find a route toward another host
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
141 * walk through the routing components tree and find a route between hosts
142 * by calling each "get_route" function in each routing component.
144 void Host::routeTo(Host* dest, std::vector<Link*>& links, double* latency)
146 std::vector<kernel::resource::LinkImpl*> linkImpls;
147 this->routeTo(dest, linkImpls, latency);
148 for (kernel::resource::LinkImpl* const& l : linkImpls)
149 links.push_back(&l->piface_);
152 /** @brief Just like Host::routeTo, but filling an array of link implementations */
153 void Host::routeTo(Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency)
155 simgrid::kernel::routing::NetZoneImpl::get_global_route(pimpl_netpoint, dest->pimpl_netpoint, links, latency);
156 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
157 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", get_cname(), dest->get_cname(),
158 (latency == nullptr ? -1 : *latency));
159 for (auto const& link : links)
160 XBT_CDEBUG(surf_route, "Link %s", link->get_cname());
164 /** Get the properties assigned to a host */
165 std::map<std::string, std::string>* Host::getProperties()
167 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
170 /** Retrieve the property value (or nullptr if not set) */
171 const char* Host::getProperty(const char* key)
173 return this->pimpl_->getProperty(key);
176 void Host::setProperty(std::string key, std::string value)
178 simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
181 /** Get the processes attached to the host */
182 void Host::getProcesses(std::vector<ActorPtr>* list)
184 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
185 list->push_back(actor.iface());
189 /** @brief Returns how many actors have been launched on this host */
190 // FIXME: Specify whether the user should expect only non-daemon actors here!
191 int Host::get_actor_count()
193 return this->extension<simgrid::simix::Host>()->process_list.size();
196 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
197 double Host::getPstateSpeed(int pstate_index)
199 return simgrid::simix::kernelImmediate([this, pstate_index] {
200 return this->pimpl_cpu->getPstateSpeed(pstate_index);
204 /** @brief Get the peak processor speed (under full load (=1.0), in flops/s), at the current pstate */
205 double Host::getSpeed()
207 return pimpl_cpu->getSpeed(1.0);
210 /** @brief Returns the number of core of the processor. */
211 int Host::getCoreCount()
213 return pimpl_cpu->coreCount();
216 /** @brief Set the pstate at which the host should run */
217 void Host::setPstate(int pstate_index)
219 simgrid::simix::kernelImmediate([this, pstate_index] {
220 this->pimpl_cpu->setPState(pstate_index);
223 /** @brief Retrieve the pstate at which the host is currently running */
224 int Host::getPstate()
226 return this->pimpl_cpu->getPState();
230 * \ingroup simix_storage_management
231 * \brief Returns the list of storages attached to an host.
232 * \return a vector containing all storages attached to the host
234 std::vector<const char*> Host::get_attached_storages()
236 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->get_attached_storages(); });
239 void Host::getAttachedStorages(std::vector<const char*>* storages)
241 std::vector<const char*> local_storages =
242 simgrid::simix::kernelImmediate([this] { return this->pimpl_->get_attached_storages(); });
243 for (auto elm : local_storages)
244 storages->push_back(elm);
247 std::unordered_map<std::string, Storage*> const& Host::getMountedStorages()
249 if (mounts == nullptr) {
250 mounts = new std::unordered_map<std::string, Storage*>();
251 for (auto const& m : this->pimpl_->storage_) {
252 mounts->insert({m.first, &m.second->piface_});
258 void Host::execute(double flops)
260 Host* host_list[1] = {this};
261 double flops_list[1] = {flops};
262 smx_activity_t s = simcall_execution_parallel_start(nullptr /*name*/, 1, host_list, flops_list,
263 nullptr /*comm_sizes */, -1.0, -1 /*timeout*/);
264 simcall_execution_wait(s);
267 double Host::getLoad()
269 return this->pimpl_cpu->get_load();
272 } // namespace simgrid