1 /* Copyright (c) 2006-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <unordered_map>
13 #include "simgrid/s4u/engine.hpp"
14 #include "simgrid/s4u/host.hpp"
15 #include "simgrid/s4u/storage.hpp"
16 #include "simgrid/simix.hpp"
17 #include "src/kernel/routing/NetCard.hpp"
18 #include "src/msg/msg_private.h"
19 #include "src/simix/ActorImpl.hpp"
20 #include "src/simix/smx_private.h"
21 #include "src/surf/HostImpl.hpp"
22 #include "src/surf/cpu_interface.hpp"
25 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
27 std::unordered_map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
29 int MSG_HOST_LEVEL = -1;
30 int USER_HOST_LEVEL = -1;
35 template class Extendable<simgrid::s4u::Host>;
40 simgrid::xbt::signal<void(Host&)> Host::onCreation;
41 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
42 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
44 Host::Host(const char* name)
47 xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
48 host_list[name_] = this;
49 new simgrid::surf::HostImpl(this);
54 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
57 if (pimpl_netcard != nullptr) // not removed yet by a children class
58 simgrid::s4u::Engine::instance()->netcardUnregister(pimpl_netcard);
63 /** @brief Fire the required callbacks and destroy the object
65 * Don't delete directly an Host, call h->destroy() instead.
67 * This is cumbersome but this is the simplest solution to ensure that the
68 * onDestruction() callback receives a valid object (because of the destructor
69 * order in a class hierarchy).
73 if (!currentlyDestroying_) {
74 currentlyDestroying_ = true;
76 host_list.erase(name_);
81 Host* Host::by_name(std::string name)
83 return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
85 Host* Host::by_name_or_null(const char* name)
87 return by_name_or_null(std::string(name));
89 Host* Host::by_name_or_null(std::string name)
91 if (host_list.find(name) == host_list.end())
93 return host_list.at(name);
96 Host *Host::current(){
97 smx_actor_t smx_proc = SIMIX_process_self();
98 if (smx_proc == nullptr)
99 xbt_die("Cannot call Host::current() from the maestro context");
100 return smx_proc->host;
103 void Host::turnOn() {
105 simgrid::simix::kernelImmediate([&]{
106 this->extension<simgrid::simix::Host>()->turnOn();
107 this->pimpl_cpu->turnOn();
108 onStateChange(*this);
113 void Host::turnOff() {
115 smx_actor_t self = SIMIX_process_self();
116 simgrid::simix::kernelImmediate([&] {
117 SIMIX_host_off(this, self);
118 onStateChange(*this);
124 return this->pimpl_cpu->isOn();
127 int Host::pstatesCount() const {
128 return this->pimpl_cpu->getNbPStates();
132 * \brief Find a route toward another host
134 * \param dest [IN] where to
135 * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
136 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
137 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
138 * \pre links!=nullptr
140 * walk through the routing components tree and find a route between hosts
141 * by calling each "get_route" function in each routing component.
143 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
145 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
146 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
147 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
148 (latency == nullptr ? -1 : *latency));
149 for (auto link : *links)
150 XBT_CDEBUG(surf_route, "Link %s", link->getName());
154 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
155 if (mounts == nullptr) {
156 mounts = new boost::unordered_map<std::string, Storage*> ();
158 xbt_dict_t dict = this->mountedStoragesAsDict();
160 xbt_dict_cursor_t cursor;
163 xbt_dict_foreach(dict, cursor, mountname, storagename) {
164 mounts->insert({mountname, &Storage::byName(storagename)});
166 xbt_dict_free(&dict);
172 /** Get the properties assigned to a host */
173 xbt_dict_t Host::properties() {
174 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
177 /** Retrieve the property value (or nullptr if not set) */
178 const char*Host::property(const char*key) {
179 return this->pimpl_->getProperty(key);
181 void Host::setProperty(const char*key, const char *value){
182 simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
185 /** Get the processes attached to the host */
186 xbt_swag_t Host::processes()
188 return simgrid::simix::kernelImmediate([&]() {
189 return this->extension<simgrid::simix::Host>()->process_list;
193 /** Get the peak power of a host */
194 double Host::getPstateSpeedCurrent()
196 return simgrid::simix::kernelImmediate([&] {
197 return this->pimpl_cpu->getPstateSpeedCurrent();
201 /** Get one power peak (in flops/s) of a host at a given pstate */
202 double Host::getPstateSpeed(int pstate_index)
204 return simgrid::simix::kernelImmediate([&] {
205 return this->pimpl_cpu->getPstateSpeed(pstate_index);
209 /** @brief Get the speed of the cpu associated to a host */
210 double Host::speed() {
211 return pimpl_cpu->getSpeed(1.0);
213 /** @brief Returns the number of core of the processor. */
214 int Host::coreCount() {
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(std::bind(
222 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
225 /** @brief Retrieve the pstate at which the host is currently running */
228 return pimpl_cpu->getPState();
232 * \ingroup simix_storage_management
233 * \brief Returns the list of storages mounted on an host.
234 * \return a dict containing all storages mounted on the host
236 xbt_dict_t Host::mountedStoragesAsDict()
238 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
242 * \ingroup simix_storage_management
243 * \brief Returns the list of storages attached to an host.
244 * \return a dict containing all storages attached to the host
246 xbt_dynar_t Host::attachedStorages()
248 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
251 } // namespace simgrid