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);
53 Host *Host::by_name(std::string name) {
54 Host* host = Host::by_name_or_null(name.c_str());
55 // TODO, raise an exception instead?
57 xbt_die("No such host: %s", name.c_str());
60 Host* Host::by_name_or_null(const char* name)
62 if (host_list == nullptr)
63 host_list = xbt_dict_new_homogeneous([](void*p) {
64 simgrid::s4u::Host* host = static_cast<simgrid::s4u::Host*>(p);
65 simgrid::s4u::Host::onDestruction(*host);
68 return (Host*) xbt_dict_get_or_null(host_list, name);
71 Host *Host::current(){
72 smx_actor_t smx_proc = SIMIX_process_self();
73 if (smx_proc == nullptr)
74 xbt_die("Cannot call Host::current() from the maestro context");
75 return smx_proc->host;
80 simgrid::simix::kernelImmediate([&]{
81 this->extension<simgrid::simix::Host>()->turnOn();
82 this->extension<simgrid::surf::HostImpl>()->turnOn();
87 void Host::turnOff() {
89 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
94 return this->pimpl_cpu->isOn();
97 int Host::pstatesCount() const {
98 return this->pimpl_cpu->getNbPStates();
101 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
102 if (mounts == nullptr) {
103 mounts = new boost::unordered_map<std::string, Storage*> ();
105 xbt_dict_t dict = this->mountedStoragesAsDict();
107 xbt_dict_cursor_t cursor;
110 xbt_dict_foreach(dict, cursor, mountname, storagename) {
111 mounts->insert({mountname, &Storage::byName(storagename)});
113 xbt_dict_free(&dict);
119 /** Get the properties assigned to a host */
120 xbt_dict_t Host::properties() {
121 return simgrid::simix::kernelImmediate([&] {
122 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
123 return surf_host->getProperties();
127 /** Retrieve the property value (or nullptr if not set) */
128 const char*Host::property(const char*key) {
129 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
130 return surf_host->getProperty(key);
132 void Host::setProperty(const char*key, const char *value){
133 simgrid::simix::kernelImmediate([&] {
134 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
135 surf_host->setProperty(key,value);
139 /** Get the processes attached to the host */
140 xbt_swag_t Host::processes()
142 return simgrid::simix::kernelImmediate([&]() {
143 return this->extension<simgrid::simix::Host>()->process_list;
147 /** Get the peak power of a host */
148 double Host::getPstateSpeedCurrent()
150 return simgrid::simix::kernelImmediate([&] {
151 return this->pimpl_cpu->getPstateSpeedCurrent();
155 /** Get one power peak (in flops/s) of a host at a given pstate */
156 double Host::getPstateSpeed(int pstate_index)
158 return simgrid::simix::kernelImmediate([&] {
159 return this->pimpl_cpu->getPstateSpeed(pstate_index);
163 /** @brief Get the speed of the cpu associated to a host */
164 double Host::speed() {
165 return pimpl_cpu->getSpeed(1.0);
167 /** @brief Returns the number of core of the processor. */
168 int Host::coreCount() {
169 return pimpl_cpu->coreCount();
172 /** @brief Set the pstate at which the host should run */
173 void Host::setPstate(int pstate_index)
175 simgrid::simix::kernelImmediate(std::bind(
176 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
179 /** @brief Retrieve the pstate at which the host is currently running */
182 return pimpl_cpu->getPState();
185 void Host::parameters(vm_params_t params)
187 simgrid::simix::kernelImmediate([&]() {
188 this->extension<simgrid::surf::HostImpl>()->getParams(params);
192 void Host::setParameters(vm_params_t params)
194 simgrid::simix::kernelImmediate([&]() {
195 this->extension<simgrid::surf::HostImpl>()->setParams(params);
200 * \ingroup simix_storage_management
201 * \brief Returns the list of storages mounted on an host.
202 * \return a dict containing all storages mounted on the host
204 xbt_dict_t Host::mountedStoragesAsDict()
206 return simgrid::simix::kernelImmediate([&] {
207 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
212 * \ingroup simix_storage_management
213 * \brief Returns the list of storages attached to an host.
214 * \return a dict containing all storages attached to the host
216 xbt_dynar_t Host::attachedStorages()
218 return simgrid::simix::kernelImmediate([&] {
219 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
223 } // namespace simgrid