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(sg_host_by_name(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.");
60 /** @brief Fire the required callbacks and destroy the object
62 * Don't delete directly an Host, call h->destroy() instead.
64 * This is cumbersome but this is the simplest solution to ensure that the
65 * onDestruction() callback receives a valid object (because of the destructor
66 * order in a class hierarchy).
70 if (!currentlyDestroying_) {
71 currentlyDestroying_ = true;
73 host_list.erase(name_);
78 Host* Host::by_name(std::string name)
80 return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
82 Host* Host::by_name_or_null(const char* name)
84 return by_name_or_null(std::string(name));
86 Host* Host::by_name_or_null(std::string name)
88 if (host_list.find(name) == host_list.end())
90 return host_list.at(name);
93 Host *Host::current(){
94 smx_actor_t smx_proc = SIMIX_process_self();
95 if (smx_proc == nullptr)
96 xbt_die("Cannot call Host::current() from the maestro context");
97 return smx_proc->host;
100 void Host::turnOn() {
102 simgrid::simix::kernelImmediate([&]{
103 this->extension<simgrid::simix::Host>()->turnOn();
104 this->pimpl_cpu->turnOn();
109 void Host::turnOff() {
111 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
116 return this->pimpl_cpu->isOn();
119 int Host::pstatesCount() const {
120 return this->pimpl_cpu->getNbPStates();
124 * \brief Find a route toward another host
126 * \param dest [IN] where to
127 * \param route [OUT] where to store the list of links (must exist, cannot be nullptr).
128 * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
129 * It is the caller responsibility to initialize latency to 0 (we add to provided route)
130 * \pre route!=nullptr
132 * walk through the routing components tree and find a route between hosts
133 * by calling each "get_route" function in each routing component.
135 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
137 simgrid::kernel::routing::AsImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
138 if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
139 XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
140 (latency == nullptr ? -1 : *latency));
141 for (auto link : *links)
142 XBT_CDEBUG(surf_route, "Link %s", link->getName());
146 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
147 if (mounts == nullptr) {
148 mounts = new boost::unordered_map<std::string, Storage*> ();
150 xbt_dict_t dict = this->mountedStoragesAsDict();
152 xbt_dict_cursor_t cursor;
155 xbt_dict_foreach(dict, cursor, mountname, storagename) {
156 mounts->insert({mountname, &Storage::byName(storagename)});
158 xbt_dict_free(&dict);
164 /** Get the properties assigned to a host */
165 xbt_dict_t Host::properties() {
166 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
169 /** Retrieve the property value (or nullptr if not set) */
170 const char*Host::property(const char*key) {
171 return this->pimpl_->getProperty(key);
173 void Host::setProperty(const char*key, const char *value){
174 simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
177 /** Get the processes attached to the host */
178 xbt_swag_t Host::processes()
180 return simgrid::simix::kernelImmediate([&]() {
181 return this->extension<simgrid::simix::Host>()->process_list;
185 /** Get the peak power of a host */
186 double Host::getPstateSpeedCurrent()
188 return simgrid::simix::kernelImmediate([&] {
189 return this->pimpl_cpu->getPstateSpeedCurrent();
193 /** Get one power peak (in flops/s) of a host at a given pstate */
194 double Host::getPstateSpeed(int pstate_index)
196 return simgrid::simix::kernelImmediate([&] {
197 return this->pimpl_cpu->getPstateSpeed(pstate_index);
201 /** @brief Get the speed of the cpu associated to a host */
202 double Host::speed() {
203 return pimpl_cpu->getSpeed(1.0);
205 /** @brief Returns the number of core of the processor. */
206 int Host::coreCount() {
207 return pimpl_cpu->coreCount();
210 /** @brief Set the pstate at which the host should run */
211 void Host::setPstate(int pstate_index)
213 simgrid::simix::kernelImmediate(std::bind(
214 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
217 /** @brief Retrieve the pstate at which the host is currently running */
220 return pimpl_cpu->getPState();
224 * \ingroup simix_storage_management
225 * \brief Returns the list of storages mounted on an host.
226 * \return a dict containing all storages mounted on the host
228 xbt_dict_t Host::mountedStoragesAsDict()
230 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
234 * \ingroup simix_storage_management
235 * \brief Returns the list of storages attached to an host.
236 * \return a dict containing all storages attached to the host
238 xbt_dynar_t Host::attachedStorages()
240 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
243 } // namespace simgrid