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: make it a static field of Host
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)
51 Host *Host::by_name(std::string name) {
52 Host* host = Host::by_name_or_null(name.c_str());
53 // TODO, raise an exception instead?
55 xbt_die("No such host: %s", name.c_str());
58 Host* Host::by_name_or_null(const char* name)
60 if (host_list == nullptr)
61 host_list = xbt_dict_new_homogeneous([](void*p) {
62 simgrid::s4u::Host* host = static_cast<simgrid::s4u::Host*>(p);
63 simgrid::s4u::Host::onDestruction(*host);
66 return (Host*) xbt_dict_get_or_null(host_list, name);
68 Host* Host::by_name_or_create(const char* name)
70 Host* host = by_name_or_null(name);
71 if (host == nullptr) {
72 host = new Host(name);
73 xbt_dict_set(host_list, name, host, nullptr);
78 Host *Host::current(){
79 smx_actor_t smx_proc = SIMIX_process_self();
80 if (smx_proc == nullptr)
81 xbt_die("Cannot call Host::current() from the maestro context");
82 return smx_proc->host;
87 simgrid::simix::kernelImmediate([&]{
88 this->extension<simgrid::simix::Host>()->turnOn();
89 this->extension<simgrid::surf::HostImpl>()->turnOn();
94 void Host::turnOff() {
96 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
101 return this->pimpl_cpu->isOn();
104 int Host::pstatesCount() const {
105 return this->pimpl_cpu->getNbPStates();
108 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
109 if (mounts == nullptr) {
110 mounts = new boost::unordered_map<std::string, Storage*> ();
112 xbt_dict_t dict = this->mountedStoragesAsDict();
114 xbt_dict_cursor_t cursor;
117 xbt_dict_foreach(dict, cursor, mountname, storagename) {
118 mounts->insert({mountname, &Storage::byName(storagename)});
120 xbt_dict_free(&dict);
126 /** Get the properties assigned to a host */
127 xbt_dict_t Host::properties() {
128 return simgrid::simix::kernelImmediate([&] {
129 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
130 return surf_host->getProperties();
134 /** Retrieve the property value (or nullptr if not set) */
135 const char*Host::property(const char*key) {
136 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
137 return surf_host->getProperty(key);
139 void Host::setProperty(const char*key, const char *value){
140 simgrid::simix::kernelImmediate([&] {
141 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
142 surf_host->setProperty(key,value);
146 /** Get the processes attached to the host */
147 xbt_swag_t Host::processes()
149 return simgrid::simix::kernelImmediate([&]() {
150 return this->extension<simgrid::simix::Host>()->process_list;
154 /** Get the peak power of a host */
155 double Host::getPstateSpeedCurrent()
157 return simgrid::simix::kernelImmediate([&] {
158 return this->pimpl_cpu->getPstateSpeedCurrent();
162 /** Get one power peak (in flops/s) of a host at a given pstate */
163 double Host::getPstateSpeed(int pstate_index)
165 return simgrid::simix::kernelImmediate([&] {
166 return this->pimpl_cpu->getPstateSpeed(pstate_index);
170 /** @brief Get the speed of the cpu associated to a host */
171 double Host::speed() {
172 return pimpl_cpu->getSpeed(1.0);
174 /** @brief Returns the number of core of the processor. */
175 int Host::coreCount() {
176 return pimpl_cpu->coreCount();
179 /** @brief Set the pstate at which the host should run */
180 void Host::setPstate(int pstate_index)
182 simgrid::simix::kernelImmediate(std::bind(
183 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
186 /** @brief Retrieve the pstate at which the host is currently running */
189 return pimpl_cpu->getPState();
192 void Host::parameters(vm_params_t params)
194 simgrid::simix::kernelImmediate([&]() {
195 this->extension<simgrid::surf::HostImpl>()->getParams(params);
199 void Host::setParameters(vm_params_t params)
201 simgrid::simix::kernelImmediate([&]() {
202 this->extension<simgrid::surf::HostImpl>()->setParams(params);
207 * \ingroup simix_storage_management
208 * \brief Returns the list of storages mounted on an host.
209 * \return a dict containing all storages mounted on the host
211 xbt_dict_t Host::mountedStoragesAsDict()
213 return simgrid::simix::kernelImmediate([&] {
214 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
219 * \ingroup simix_storage_management
220 * \brief Returns the list of storages attached to an host.
221 * \return a dict containing all storages attached to the host
223 xbt_dynar_t Host::attachedStorages()
225 return simgrid::simix::kernelImmediate([&] {
226 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
230 } // namespace simgrid