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_dict_t host_list = nullptr; // 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 xbt_dict_set(host_list, name, this, nullptr);
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;
69 xbt_dict_remove(host_list, name().c_str());
75 Host* Host::by_name(std::string name)
77 Host* host = Host::by_name_or_null(name.c_str());
78 // TODO, raise an exception instead?
80 xbt_die("No such host: '%s'", name.c_str());
83 Host* Host::by_name_or_null(const char* name)
85 if (host_list == nullptr)
86 host_list = xbt_dict_new_homogeneous(nullptr);
87 return (Host*) xbt_dict_get_or_null(host_list, name);
90 Host *Host::current(){
91 smx_actor_t smx_proc = SIMIX_process_self();
92 if (smx_proc == nullptr)
93 xbt_die("Cannot call Host::current() from the maestro context");
94 return smx_proc->host;
99 simgrid::simix::kernelImmediate([&]{
100 this->extension<simgrid::simix::Host>()->turnOn();
101 this->pimpl_cpu->turnOn();
106 void Host::turnOff() {
108 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
113 return this->pimpl_cpu->isOn();
116 int Host::pstatesCount() const {
117 return this->pimpl_cpu->getNbPStates();
120 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
121 if (mounts == nullptr) {
122 mounts = new boost::unordered_map<std::string, Storage*> ();
124 xbt_dict_t dict = this->mountedStoragesAsDict();
126 xbt_dict_cursor_t cursor;
129 xbt_dict_foreach(dict, cursor, mountname, storagename) {
130 mounts->insert({mountname, &Storage::byName(storagename)});
132 xbt_dict_free(&dict);
138 /** Get the properties assigned to a host */
139 xbt_dict_t Host::properties() {
140 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
143 /** Retrieve the property value (or nullptr if not set) */
144 const char*Host::property(const char*key) {
145 return this->pimpl_->getProperty(key);
147 void Host::setProperty(const char*key, const char *value){
148 simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
151 /** Get the processes attached to the host */
152 xbt_swag_t Host::processes()
154 return simgrid::simix::kernelImmediate([&]() {
155 return this->extension<simgrid::simix::Host>()->process_list;
159 /** Get the peak power of a host */
160 double Host::getPstateSpeedCurrent()
162 return simgrid::simix::kernelImmediate([&] {
163 return this->pimpl_cpu->getPstateSpeedCurrent();
167 /** Get one power peak (in flops/s) of a host at a given pstate */
168 double Host::getPstateSpeed(int pstate_index)
170 return simgrid::simix::kernelImmediate([&] {
171 return this->pimpl_cpu->getPstateSpeed(pstate_index);
175 /** @brief Get the speed of the cpu associated to a host */
176 double Host::speed() {
177 return pimpl_cpu->getSpeed(1.0);
179 /** @brief Returns the number of core of the processor. */
180 int Host::coreCount() {
181 return pimpl_cpu->coreCount();
184 /** @brief Set the pstate at which the host should run */
185 void Host::setPstate(int pstate_index)
187 simgrid::simix::kernelImmediate(std::bind(
188 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
191 /** @brief Retrieve the pstate at which the host is currently running */
194 return pimpl_cpu->getPState();
197 void Host::parameters(vm_params_t params)
199 THROW_IMPOSSIBLE; // This should only be used on VMs
202 void Host::setParameters(vm_params_t params)
204 THROW_IMPOSSIBLE; // This should only be used on VMs
208 * \ingroup simix_storage_management
209 * \brief Returns the list of storages mounted on an host.
210 * \return a dict containing all storages mounted on the host
212 xbt_dict_t Host::mountedStoragesAsDict()
214 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
218 * \ingroup simix_storage_management
219 * \brief Returns the list of storages attached to an host.
220 * \return a dict containing all storages attached to the host
222 xbt_dynar_t Host::attachedStorages()
224 return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
227 } // namespace simgrid