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/simix.hpp"
14 #include "src/surf/HostImpl.hpp"
16 #include "src/msg/msg_private.h"
17 #include "src/simix/ActorImpl.hpp"
18 #include "src/simix/smx_private.h"
19 #include "src/surf/cpu_interface.hpp"
20 #include "simgrid/s4u/host.hpp"
21 #include "simgrid/s4u/storage.hpp"
23 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
25 std::unordered_map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
27 int MSG_HOST_LEVEL = -1;
28 int USER_HOST_LEVEL = -1;
33 template class Extendable<simgrid::s4u::Host>;
38 simgrid::xbt::signal<void(Host&)> Host::onCreation;
39 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
40 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
42 Host::Host(const char* name)
45 xbt_assert(sg_host_by_name(name) == nullptr, "Refusing to create a second host named '%s'.", name);
46 host_list[name_] = this;
51 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
59 /** @brief Fire the required callbacks and destroy the object
61 * Don't delete directly an Host, call h->destroy() instead.
63 * This is cumbersome but this is the simplest solution to ensure that the
64 * onDestruction() callback receives a valid object (because of the destructor
65 * order in a class hierarchy).
69 if (!currentlyDestroying_) {
70 currentlyDestroying_ = true;
72 host_list.erase(name_);
77 Host* Host::by_name(std::string name)
79 return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
81 Host* Host::by_name_or_null(const char* name)
83 return by_name_or_null(std::string(name));
85 Host* Host::by_name_or_null(std::string name)
87 if (host_list.find(name) == host_list.end())
89 return host_list.at(name);
92 Host *Host::current(){
93 smx_actor_t smx_proc = SIMIX_process_self();
94 if (smx_proc == nullptr)
95 xbt_die("Cannot call Host::current() from the maestro context");
96 return smx_proc->host;
101 simgrid::simix::kernelImmediate([&]{
102 this->extension<simgrid::simix::Host>()->turnOn();
103 this->pimpl_cpu->turnOn();
108 void Host::turnOff() {
110 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
115 return this->pimpl_cpu->isOn();
118 int Host::pstatesCount() const {
119 return this->pimpl_cpu->getNbPStates();
123 * \brief Find a route toward another host
125 * \param dest [IN] where to
126 * \param route [OUT] where to store the list of links (must exist, cannot be nullptr).
127 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
128 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
129 * \pre route!=nullptr
131 * walk through the routing components tree and find a route between hosts
132 * by calling each "get_route" function in each routing component.
134 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
136 simgrid::kernel::routing::AsImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
137 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
138 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
139 (latency == nullptr ? -1 : *latency));
140 for (auto link : *links)
141 XBT_CDEBUG(surf_route, "Link %s", link->getName());
145 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
146 if (mounts == nullptr) {
147 mounts = new boost::unordered_map<std::string, Storage*> ();
149 xbt_dict_t dict = this->mountedStoragesAsDict();
151 xbt_dict_cursor_t cursor;
154 xbt_dict_foreach(dict, cursor, mountname, storagename) {
155 mounts->insert({mountname, &Storage::byName(storagename)});
157 xbt_dict_free(&dict);
163 /** Get the properties assigned to a host */
164 xbt_dict_t Host::properties() {
165 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
168 /** Retrieve the property value (or nullptr if not set) */
169 const char*Host::property(const char*key) {
170 return this->pimpl_->getProperty(key);
172 void Host::setProperty(const char*key, const char *value){
173 simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
176 /** Get the processes attached to the host */
177 xbt_swag_t Host::processes()
179 return simgrid::simix::kernelImmediate([&]() {
180 return this->extension<simgrid::simix::Host>()->process_list;
184 /** Get the peak power of a host */
185 double Host::getPstateSpeedCurrent()
187 return simgrid::simix::kernelImmediate([&] {
188 return this->pimpl_cpu->getPstateSpeedCurrent();
192 /** Get one power peak (in flops/s) of a host at a given pstate */
193 double Host::getPstateSpeed(int pstate_index)
195 return simgrid::simix::kernelImmediate([&] {
196 return this->pimpl_cpu->getPstateSpeed(pstate_index);
200 /** @brief Get the speed of the cpu associated to a host */
201 double Host::speed() {
202 return pimpl_cpu->getSpeed(1.0);
204 /** @brief Returns the number of core of the processor. */
205 int Host::coreCount() {
206 return pimpl_cpu->coreCount();
209 /** @brief Set the pstate at which the host should run */
210 void Host::setPstate(int pstate_index)
212 simgrid::simix::kernelImmediate(std::bind(
213 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
216 /** @brief Retrieve the pstate at which the host is currently running */
219 return pimpl_cpu->getPState();
223 * \ingroup simix_storage_management
224 * \brief Returns the list of storages mounted on an host.
225 * \return a dict containing all storages mounted on the host
227 xbt_dict_t Host::mountedStoragesAsDict()
229 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
233 * \ingroup simix_storage_management
234 * \brief Returns the list of storages attached to an host.
235 * \return a dict containing all storages attached to the host
237 xbt_dynar_t Host::attachedStorages()
239 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
242 } // namespace simgrid