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) : name_(name)
32 xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
33 Engine::getInstance()->host_register(std::string(name_), this);
34 new simgrid::surf::HostImpl(this);
39 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
42 if (pimpl_netpoint != nullptr) // not removed yet by a children class
43 simgrid::s4u::Engine::getInstance()->netpoint_unregister(pimpl_netpoint);
48 /** @brief Fire the required callbacks and destroy the object
50 * Don't delete directly an Host, call h->destroy() instead.
52 * This is cumbersome but this is the simplest solution to ensure that the
53 * onDestruction() callback receives a valid object (because of the destructor
54 * order in a class hierarchy).
58 if (not currentlyDestroying_) {
59 currentlyDestroying_ = true;
61 Engine::getInstance()->host_unregister(std::string(name_));
66 Host* Host::by_name(std::string name)
68 return Engine::getInstance()->host_by_name(name);
70 Host* Host::by_name(const char* name)
72 return Engine::getInstance()->host_by_name(std::string(name));
74 Host* Host::by_name_or_null(const char* name)
76 return Engine::getInstance()->host_by_name_or_null(std::string(name));
78 Host* Host::by_name_or_null(std::string name)
80 return Engine::getInstance()->host_by_name_or_null(name);
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;
94 simgrid::simix::kernelImmediate([this] {
95 this->extension<simgrid::simix::Host>()->turnOn();
96 this->pimpl_cpu->turn_on();
105 smx_actor_t self = SIMIX_process_self();
106 simgrid::simix::kernelImmediate([this, self] {
107 SIMIX_host_off(this, self);
108 onStateChange(*this);
115 return this->pimpl_cpu->is_on();
118 int Host::getPstatesCount() const
120 return this->pimpl_cpu->getNbPStates();
124 * \brief Return the list of actors attached to an host.
126 * \param whereto a vector in which we should push actors living on that host
128 void Host::actorList(std::vector<ActorPtr>* whereto)
130 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
131 whereto->push_back(actor.ciface());
136 * \brief Find a route toward another host
138 * \param dest [IN] where to
139 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
140 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
141 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
142 * \pre links!=nullptr
144 * walk through the routing components tree and find a route between hosts
145 * by calling each "get_route" function in each routing component.
147 void Host::routeTo(Host* dest, std::vector<Link*>& links, double* latency)
149 std::vector<kernel::resource::LinkImpl*> linkImpls;
150 this->routeTo(dest, linkImpls, latency);
151 for (kernel::resource::LinkImpl* const& l : linkImpls)
152 links.push_back(&l->piface_);
155 /** @brief Just like Host::routeTo, but filling an array of link implementations */
156 void Host::routeTo(Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency)
158 simgrid::kernel::routing::NetZoneImpl::get_global_route(pimpl_netpoint, dest->pimpl_netpoint, links, latency);
159 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
160 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", get_cname(), dest->get_cname(),
161 (latency == nullptr ? -1 : *latency));
162 for (auto const& link : links)
163 XBT_CDEBUG(surf_route, "Link %s", link->get_cname());
167 /** Get the properties assigned to a host */
168 std::map<std::string, std::string>* Host::getProperties()
170 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
173 /** Retrieve the property value (or nullptr if not set) */
174 const char* Host::getProperty(const char* key)
176 return this->pimpl_->getProperty(key);
179 void Host::setProperty(std::string key, std::string value)
181 simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
184 /** Get the processes attached to the host */
185 void Host::getProcesses(std::vector<ActorPtr>* list)
187 for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
188 list->push_back(actor.iface());
192 /** @brief Returns how many actors have been launched on this host */
193 // FIXME: Specify whether the user should expect only non-daemon actors here!
194 int Host::get_actor_count()
196 return this->extension<simgrid::simix::Host>()->process_list.size();
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(
203 [this, pstate_index] { return this->pimpl_cpu->getPstateSpeed(pstate_index); });
206 /** @brief Get the peak processor speed (under full load (=1.0), in flops/s), at the current pstate */
207 double Host::getSpeed()
209 return pimpl_cpu->getSpeed(1.0);
212 /** @brief Returns the number of core of the processor. */
213 int Host::getCoreCount()
215 return pimpl_cpu->coreCount();
218 /** @brief Set the pstate at which the host should run */
219 void Host::setPstate(int pstate_index)
221 simgrid::simix::kernelImmediate([this, pstate_index] { 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();
273 } // namespace simgrid