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 std::unordered_map<simgrid::xbt::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
25 int MSG_HOST_LEVEL = -1;
26 int USER_HOST_LEVEL = -1;
31 template class Extendable<simgrid::s4u::Host>;
36 simgrid::xbt::signal<void(Host&)> Host::onCreation;
37 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
38 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
40 Host::Host(const char* name)
43 xbt_assert(sg_host_by_name(name) == nullptr, "Refusing to create a second host named '%s'.", name);
44 host_list[name_] = this;
49 xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
57 /** @brief Fire the required callbacks and destroy the object
59 * Don't delete directly an Host, call h->destroy() instead.
61 * This is cumbersome but this is the simplest solution to ensure that the
62 * onDestruction() callback receives a valid object (because of the destructor
63 * order in a class hierarchy).
67 if (!currentlyDestroying_) {
68 currentlyDestroying_ = true;
70 host_list.erase(name_);
75 Host* Host::by_name(std::string name)
77 return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
79 Host* Host::by_name_or_null(const char* name)
82 return host_list.at(name);
83 } catch (std::out_of_range& e) {
88 Host *Host::current(){
89 smx_actor_t smx_proc = SIMIX_process_self();
90 if (smx_proc == nullptr)
91 xbt_die("Cannot call Host::current() from the maestro context");
92 return smx_proc->host;
97 simgrid::simix::kernelImmediate([&]{
98 this->extension<simgrid::simix::Host>()->turnOn();
99 this->pimpl_cpu->turnOn();
104 void Host::turnOff() {
106 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
111 return this->pimpl_cpu->isOn();
114 int Host::pstatesCount() const {
115 return this->pimpl_cpu->getNbPStates();
118 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
119 if (mounts == nullptr) {
120 mounts = new boost::unordered_map<std::string, Storage*> ();
122 xbt_dict_t dict = this->mountedStoragesAsDict();
124 xbt_dict_cursor_t cursor;
127 xbt_dict_foreach(dict, cursor, mountname, storagename) {
128 mounts->insert({mountname, &Storage::byName(storagename)});
130 xbt_dict_free(&dict);
136 /** Get the properties assigned to a host */
137 xbt_dict_t Host::properties() {
138 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
141 /** Retrieve the property value (or nullptr if not set) */
142 const char*Host::property(const char*key) {
143 return this->pimpl_->getProperty(key);
145 void Host::setProperty(const char*key, const char *value){
146 simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
149 /** Get the processes attached to the host */
150 xbt_swag_t Host::processes()
152 return simgrid::simix::kernelImmediate([&]() {
153 return this->extension<simgrid::simix::Host>()->process_list;
157 /** Get the peak power of a host */
158 double Host::getPstateSpeedCurrent()
160 return simgrid::simix::kernelImmediate([&] {
161 return this->pimpl_cpu->getPstateSpeedCurrent();
165 /** Get one power peak (in flops/s) of a host at a given pstate */
166 double Host::getPstateSpeed(int pstate_index)
168 return simgrid::simix::kernelImmediate([&] {
169 return this->pimpl_cpu->getPstateSpeed(pstate_index);
173 /** @brief Get the speed of the cpu associated to a host */
174 double Host::speed() {
175 return pimpl_cpu->getSpeed(1.0);
177 /** @brief Returns the number of core of the processor. */
178 int Host::coreCount() {
179 return pimpl_cpu->coreCount();
182 /** @brief Set the pstate at which the host should run */
183 void Host::setPstate(int pstate_index)
185 simgrid::simix::kernelImmediate(std::bind(
186 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
189 /** @brief Retrieve the pstate at which the host is currently running */
192 return pimpl_cpu->getPState();
196 * \ingroup simix_storage_management
197 * \brief Returns the list of storages mounted on an host.
198 * \return a dict containing all storages mounted on the host
200 xbt_dict_t Host::mountedStoragesAsDict()
202 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
206 * \ingroup simix_storage_management
207 * \brief Returns the list of storages attached to an host.
208 * \return a dict containing all storages attached to the host
210 xbt_dynar_t Host::attachedStorages()
212 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
215 } // namespace simgrid