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/host.hpp"
14 #include "simgrid/s4u/storage.hpp"
15 #include "simgrid/simix.hpp"
16 #include "src/kernel/routing/NetCard.hpp"
17 #include "src/msg/msg_private.h"
18 #include "src/simix/ActorImpl.hpp"
19 #include "src/simix/smx_private.h"
20 #include "src/surf/HostImpl.hpp"
21 #include "src/surf/cpu_interface.hpp"
24 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
26 std::unordered_map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
28 int MSG_HOST_LEVEL = -1;
29 int USER_HOST_LEVEL = -1;
34 template class Extendable<simgrid::s4u::Host>;
39 simgrid::xbt::signal<void(Host&)> Host::onCreation;
40 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
41 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
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;
52 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
55 if (pimpl_netcard != nullptr) // not removed yet by a children class
56 xbt_dict_remove(netcards_dict, name_.c_str());
61 /** @brief Fire the required callbacks and destroy the object
63 * Don't delete directly an Host, call h->destroy() instead.
65 * This is cumbersome but this is the simplest solution to ensure that the
66 * onDestruction() callback receives a valid object (because of the destructor
67 * order in a class hierarchy).
71 if (!currentlyDestroying_) {
72 currentlyDestroying_ = true;
74 host_list.erase(name_);
79 Host* Host::by_name(std::string name)
81 return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
83 Host* Host::by_name_or_null(const char* name)
85 return by_name_or_null(std::string(name));
87 Host* Host::by_name_or_null(std::string name)
89 if (host_list.find(name) == host_list.end())
91 return host_list.at(name);
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([&]{
104 this->extension<simgrid::simix::Host>()->turnOn();
105 this->pimpl_cpu->turnOn();
110 void Host::turnOff() {
112 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
117 return this->pimpl_cpu->isOn();
120 int Host::pstatesCount() const {
121 return this->pimpl_cpu->getNbPStates();
125 * \brief Find a route toward another host
127 * \param dest [IN] where to
128 * \param route [OUT] where to store the list of links (must exist, cannot be nullptr).
129 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
130 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
131 * \pre route!=nullptr
133 * walk through the routing components tree and find a route between hosts
134 * by calling each "get_route" function in each routing component.
136 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
138 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
139 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
140 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
141 (latency == nullptr ? -1 : *latency));
142 for (auto link : *links)
143 XBT_CDEBUG(surf_route, "Link %s", link->getName());
147 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
148 if (mounts == nullptr) {
149 mounts = new boost::unordered_map<std::string, Storage*> ();
151 xbt_dict_t dict = this->mountedStoragesAsDict();
153 xbt_dict_cursor_t cursor;
156 xbt_dict_foreach(dict, cursor, mountname, storagename) {
157 mounts->insert({mountname, &Storage::byName(storagename)});
159 xbt_dict_free(&dict);
165 /** Get the properties assigned to a host */
166 xbt_dict_t Host::properties() {
167 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
170 /** Retrieve the property value (or nullptr if not set) */
171 const char*Host::property(const char*key) {
172 return this->pimpl_->getProperty(key);
174 void Host::setProperty(const char*key, const char *value){
175 simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
178 /** Get the processes attached to the host */
179 xbt_swag_t Host::processes()
181 return simgrid::simix::kernelImmediate([&]() {
182 return this->extension<simgrid::simix::Host>()->process_list;
186 /** Get the peak power of a host */
187 double Host::getPstateSpeedCurrent()
189 return simgrid::simix::kernelImmediate([&] {
190 return this->pimpl_cpu->getPstateSpeedCurrent();
194 /** Get one power peak (in flops/s) of a host at a given pstate */
195 double Host::getPstateSpeed(int pstate_index)
197 return simgrid::simix::kernelImmediate([&] {
198 return this->pimpl_cpu->getPstateSpeed(pstate_index);
202 /** @brief Get the speed of the cpu associated to a host */
203 double Host::speed() {
204 return pimpl_cpu->getSpeed(1.0);
206 /** @brief Returns the number of core of the processor. */
207 int Host::coreCount() {
208 return pimpl_cpu->coreCount();
211 /** @brief Set the pstate at which the host should run */
212 void Host::setPstate(int pstate_index)
214 simgrid::simix::kernelImmediate(std::bind(
215 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
218 /** @brief Retrieve the pstate at which the host is currently running */
221 return pimpl_cpu->getPState();
225 * \ingroup simix_storage_management
226 * \brief Returns the list of storages mounted on an host.
227 * \return a dict containing all storages mounted on the host
229 xbt_dict_t Host::mountedStoragesAsDict()
231 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
235 * \ingroup simix_storage_management
236 * \brief Returns the list of storages attached to an host.
237 * \return a dict containing all storages attached to the host
239 xbt_dynar_t Host::attachedStorages()
241 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
244 } // namespace simgrid