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.");
56 /** @brief Fire the required callbacks and destroy the object
58 * Don't delete directly an Host, call h->destroy() instead.
60 * This is cumbersome but there is the simplest solution to ensure that the
61 * onDestruction() callback receives a valid object (because of the destructor
62 * order in a class hierarchy).
66 if (!currentlyDestroying_) {
67 currentlyDestroying_ = true;
68 xbt_dict_remove(host_list, name().c_str());
74 Host* Host::by_name(std::string name)
76 Host* host = Host::by_name_or_null(name.c_str());
77 // TODO, raise an exception instead?
79 xbt_die("No such host: '%s'", name.c_str());
82 Host* Host::by_name_or_null(const char* name)
84 if (host_list == nullptr)
85 host_list = xbt_dict_new_homogeneous(nullptr);
86 return (Host*) xbt_dict_get_or_null(host_list, name);
89 Host *Host::current(){
90 smx_actor_t smx_proc = SIMIX_process_self();
91 if (smx_proc == nullptr)
92 xbt_die("Cannot call Host::current() from the maestro context");
93 return smx_proc->host;
98 simgrid::simix::kernelImmediate([&]{
99 this->extension<simgrid::simix::Host>()->turnOn();
100 this->extension<simgrid::surf::HostImpl>()->turnOn();
105 void Host::turnOff() {
107 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
112 return this->pimpl_cpu->isOn();
115 int Host::pstatesCount() const {
116 return this->pimpl_cpu->getNbPStates();
119 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
120 if (mounts == nullptr) {
121 mounts = new boost::unordered_map<std::string, Storage*> ();
123 xbt_dict_t dict = this->mountedStoragesAsDict();
125 xbt_dict_cursor_t cursor;
128 xbt_dict_foreach(dict, cursor, mountname, storagename) {
129 mounts->insert({mountname, &Storage::byName(storagename)});
131 xbt_dict_free(&dict);
137 /** Get the properties assigned to a host */
138 xbt_dict_t Host::properties() {
139 return simgrid::simix::kernelImmediate([&] {
140 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
141 return surf_host->getProperties();
145 /** Retrieve the property value (or nullptr if not set) */
146 const char*Host::property(const char*key) {
147 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
148 return surf_host->getProperty(key);
150 void Host::setProperty(const char*key, const char *value){
151 simgrid::simix::kernelImmediate([&] {
152 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
153 surf_host->setProperty(key,value);
157 /** Get the processes attached to the host */
158 xbt_swag_t Host::processes()
160 return simgrid::simix::kernelImmediate([&]() {
161 return this->extension<simgrid::simix::Host>()->process_list;
165 /** Get the peak power of a host */
166 double Host::getPstateSpeedCurrent()
168 return simgrid::simix::kernelImmediate([&] {
169 return this->pimpl_cpu->getPstateSpeedCurrent();
173 /** Get one power peak (in flops/s) of a host at a given pstate */
174 double Host::getPstateSpeed(int pstate_index)
176 return simgrid::simix::kernelImmediate([&] {
177 return this->pimpl_cpu->getPstateSpeed(pstate_index);
181 /** @brief Get the speed of the cpu associated to a host */
182 double Host::speed() {
183 return pimpl_cpu->getSpeed(1.0);
185 /** @brief Returns the number of core of the processor. */
186 int Host::coreCount() {
187 return pimpl_cpu->coreCount();
190 /** @brief Set the pstate at which the host should run */
191 void Host::setPstate(int pstate_index)
193 simgrid::simix::kernelImmediate(std::bind(
194 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
197 /** @brief Retrieve the pstate at which the host is currently running */
200 return pimpl_cpu->getPState();
203 void Host::parameters(vm_params_t params)
205 simgrid::simix::kernelImmediate([&]() {
206 this->extension<simgrid::surf::HostImpl>()->getParams(params);
210 void Host::setParameters(vm_params_t params)
212 simgrid::simix::kernelImmediate([&]() {
213 this->extension<simgrid::surf::HostImpl>()->setParams(params);
218 * \ingroup simix_storage_management
219 * \brief Returns the list of storages mounted on an host.
220 * \return a dict containing all storages mounted on the host
222 xbt_dict_t Host::mountedStoragesAsDict()
224 return simgrid::simix::kernelImmediate([&] {
225 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
230 * \ingroup simix_storage_management
231 * \brief Returns the list of storages attached to an host.
232 * \return a dict containing all storages attached to the host
234 xbt_dynar_t Host::attachedStorages()
236 return simgrid::simix::kernelImmediate([&] {
237 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
241 } // namespace simgrid