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/simix/ActorImpl.hpp"
17 #include "src/simix/smx_private.h"
18 #include "src/surf/cpu_interface.hpp"
19 #include "simgrid/s4u/host.hpp"
20 #include "simgrid/s4u/storage.hpp"
21 #include "../msg/msg_private.hpp"
23 int MSG_HOST_LEVEL = -1;
24 int USER_HOST_LEVEL = -1;
29 template class Extendable<simgrid::s4u::Host>;
34 simgrid::xbt::signal<void(Host&)> Host::onCreation;
35 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
36 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
38 Host::Host(const char* name)
49 Host *Host::by_name(std::string name) {
50 Host* host = Host::by_name_or_null(name.c_str());
51 // TODO, raise an exception instead?
53 xbt_die("No such host: %s", name.c_str());
56 Host* Host::by_name_or_null(const char* name)
58 return (Host*) xbt_dict_get_or_null(host_list, name);
60 Host* Host::by_name_or_create(const char* name)
62 Host* host = by_name_or_null(name);
63 if (host == nullptr) {
64 host = new Host(name);
65 xbt_dict_set(host_list, name, host, nullptr);
70 Host *Host::current(){
71 smx_actor_t smx_proc = SIMIX_process_self();
72 if (smx_proc == nullptr)
73 xbt_die("Cannot call Host::current() from the maestro context");
74 return smx_proc->host;
79 simgrid::simix::kernelImmediate([&]{
80 this->extension<simgrid::simix::Host>()->turnOn();
81 this->extension<simgrid::surf::HostImpl>()->turnOn();
86 void Host::turnOff() {
88 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
93 return this->pimpl_cpu->isOn();
96 int Host::pstatesCount() const {
97 return this->pimpl_cpu->getNbPStates();
100 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
101 if (mounts == nullptr) {
102 mounts = new boost::unordered_map<std::string, Storage*> ();
104 xbt_dict_t dict = this->mountedStoragesAsDict();
106 xbt_dict_cursor_t cursor;
109 xbt_dict_foreach(dict, cursor, mountname, storagename) {
110 mounts->insert({mountname, &Storage::byName(storagename)});
112 xbt_dict_free(&dict);
118 /** Get the properties assigned to a host */
119 xbt_dict_t Host::properties() {
120 return simgrid::simix::kernelImmediate([&] {
121 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
122 return surf_host->getProperties();
126 /** Retrieve the property value (or nullptr if not set) */
127 const char*Host::property(const char*key) {
128 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
129 return surf_host->getProperty(key);
131 void Host::setProperty(const char*key, const char *value){
132 simgrid::simix::kernelImmediate([&] {
133 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
134 surf_host->setProperty(key,value);
138 /** Get the processes attached to the host */
139 xbt_swag_t Host::processes()
141 return simgrid::simix::kernelImmediate([&]() {
142 return this->extension<simgrid::simix::Host>()->process_list;
146 /** Get the peak power of a host */
147 double Host::getPstateSpeedCurrent()
149 return simgrid::simix::kernelImmediate([&] {
150 return this->pimpl_cpu->getPstateSpeedCurrent();
154 /** Get one power peak (in flops/s) of a host at a given pstate */
155 double Host::getPstateSpeed(int pstate_index)
157 return simgrid::simix::kernelImmediate([&] {
158 return this->pimpl_cpu->getPstateSpeed(pstate_index);
162 /** @brief Get the speed of the cpu associated to a host */
163 double Host::speed() {
164 return pimpl_cpu->getSpeed(1.0);
166 /** @brief Returns the number of core of the processor. */
167 int Host::coresCount() {
168 return pimpl_cpu->getCore();
171 /** @brief Set the pstate at which the host should run */
172 void Host::setPstate(int pstate_index)
174 simgrid::simix::kernelImmediate(std::bind(
175 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
178 /** @brief Retrieve the pstate at which the host is currently running */
181 return pimpl_cpu->getPState();
184 void Host::parameters(vm_params_t params)
186 simgrid::simix::kernelImmediate([&]() {
187 this->extension<simgrid::surf::HostImpl>()->getParams(params);
191 void Host::setParameters(vm_params_t params)
193 simgrid::simix::kernelImmediate([&]() {
194 this->extension<simgrid::surf::HostImpl>()->setParams(params);
199 * \ingroup simix_storage_management
200 * \brief Returns the list of storages mounted on an host.
201 * \return a dict containing all storages mounted on the host
203 xbt_dict_t Host::mountedStoragesAsDict()
205 return simgrid::simix::kernelImmediate([&] {
206 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
211 * \ingroup simix_storage_management
212 * \brief Returns the list of storages attached to an host.
213 * \return a dict containing all storages attached to the host
215 xbt_dynar_t Host::attachedStorages()
217 return simgrid::simix::kernelImmediate([&] {
218 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
222 } // namespace simgrid