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 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;
78 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_on, this));
81 void Host::turnOff() {
82 simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
86 return this->pimpl_cpu->isOn();
89 int Host::pstatesCount() const {
90 return this->pimpl_cpu->getNbPStates();
93 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
94 if (mounts == nullptr) {
95 mounts = new boost::unordered_map<std::string, Storage*> ();
97 xbt_dict_t dict = this->mountedStoragesAsDict();
99 xbt_dict_cursor_t cursor;
102 xbt_dict_foreach(dict, cursor, mountname, storagename) {
103 mounts->insert({mountname, &Storage::byName(storagename)});
105 xbt_dict_free(&dict);
111 /** Get the properties assigned to a host */
112 xbt_dict_t Host::properties() {
113 return simgrid::simix::kernelImmediate([&] {
114 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
115 return surf_host->getProperties();
119 /** Retrieve the property value (or nullptr if not set) */
120 const char*Host::property(const char*key) {
121 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
122 return surf_host->getProperty(key);
124 void Host::setProperty(const char*key, const char *value){
125 simgrid::simix::kernelImmediate([&] {
126 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
127 surf_host->setProperty(key,value);
131 /** Get the processes attached to the host */
132 xbt_swag_t Host::processes()
134 return simgrid::simix::kernelImmediate([&]() {
135 return this->extension<simgrid::simix::Host>()->process_list;
139 /** Get the peak power of a host */
140 double Host::getPstateSpeedCurrent()
142 return simgrid::simix::kernelImmediate([&] {
143 return this->pimpl_cpu->getPstateSpeedCurrent();
147 /** Get one power peak (in flops/s) of a host at a given pstate */
148 double Host::getPstateSpeed(int pstate_index)
150 return simgrid::simix::kernelImmediate([&] {
151 return this->pimpl_cpu->getPstateSpeed(pstate_index);
155 /** @brief Get the speed of the cpu associated to a host */
156 double Host::speed() {
157 return pimpl_cpu->getSpeed(1.0);
159 /** @brief Returns the number of core of the processor. */
160 int Host::coresCount() {
161 return pimpl_cpu->getCore();
164 /** @brief Set the pstate at which the host should run */
165 void Host::setPstate(int pstate_index)
167 simgrid::simix::kernelImmediate(std::bind(
168 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
171 /** @brief Retrieve the pstate at which the host is currently running */
174 return pimpl_cpu->getPState();
177 void Host::parameters(vm_params_t params)
179 simgrid::simix::kernelImmediate([&]() {
180 this->extension<simgrid::surf::HostImpl>()->getParams(params);
184 void Host::setParameters(vm_params_t params)
186 simgrid::simix::kernelImmediate([&]() {
187 this->extension<simgrid::surf::HostImpl>()->setParams(params);
192 * \ingroup simix_storage_management
193 * \brief Returns the list of storages mounted on an host.
194 * \return a dict containing all storages mounted on the host
196 xbt_dict_t Host::mountedStoragesAsDict()
198 return simgrid::simix::kernelImmediate([&] {
199 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
204 * \ingroup simix_storage_management
205 * \brief Returns the list of storages attached to an host.
206 * \return a dict containing all storages attached to the host
208 xbt_dynar_t Host::attachedStorages()
210 return simgrid::simix::kernelImmediate([&] {
211 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
215 } // namespace simgrid