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/smx_process_private.h"
18 #include "src/simix/smx_private.hpp"
19 #include "src/surf/cpu_interface.hpp"
20 #include "simgrid/s4u/host.hpp"
21 #include "simgrid/s4u/storage.hpp"
30 template class Extendable<simgrid::s4u::Host>;
35 simgrid::xbt::signal<void(Host&)> Host::onCreation;
36 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
37 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
39 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, NULL);
70 Host *Host::current(){
71 smx_process_t smx_proc = SIMIX_process_self();
73 xbt_die("Cannot call Host::current() from the maestro context");
74 return SIMIX_process_get_host(smx_proc);
78 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
81 void Host::turnOff() {
82 simgrid::simix::kernel(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() {
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::kernel([&] {
114 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
115 return surf_host->getProperties();
119 /** Get the processes attached to the host */
120 xbt_swag_t Host::processes()
122 return simgrid::simix::kernel([&]() {
123 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
127 /** Get the peak power of a host */
128 double Host::currentPowerPeak()
130 return simgrid::simix::kernel([&] {
131 return this->pimpl_cpu->getCurrentPowerPeak();
135 /** Get one power peak (in flops/s) of a host at a given pstate */
136 double Host::powerPeakAt(int pstate_index)
138 return simgrid::simix::kernel([&] {
139 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
143 /** @brief Get the speed of the cpu associated to a host */
144 double Host::speed() {
145 return pimpl_cpu->getSpeed(1.0);
147 /** @brief Returns the number of core of the processor. */
148 int Host::core_count() {
149 return pimpl_cpu->getCore();
152 /** @brief Set the pstate at which the host should run */
153 void Host::setPstate(int pstate_index)
155 simgrid::simix::kernel(std::bind(
156 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
159 /** @brief Retrieve the pstate at which the host is currently running */
162 return pimpl_cpu->getPState();
165 void Host::parameters(vm_params_t params)
167 simgrid::simix::kernel([&]() {
168 this->extension<simgrid::surf::HostImpl>()->getParams(params);
172 void Host::setParameters(vm_params_t params)
174 simgrid::simix::kernel([&]() {
175 this->extension<simgrid::surf::HostImpl>()->setParams(params);
180 * \ingroup simix_storage_management
181 * \brief Returns the list of storages mounted on an host.
182 * \return a dict containing all storages mounted on the host
184 xbt_dict_t Host::mountedStoragesAsDict()
186 return simgrid::simix::kernel([&] {
187 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
192 * \ingroup simix_storage_management
193 * \brief Returns the list of storages attached to an host.
194 * \return a dict containing all storages attached to the host
196 xbt_dynar_t Host::attachedStorages()
198 return simgrid::simix::kernel([&] {
199 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
203 } // namespace simgrid