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.h"
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)
50 Host *Host::by_name(std::string name) {
51 Host* host = Host::by_name_or_null(name.c_str());
52 // TODO, raise an exception instead?
54 xbt_die("No such host: %s", name.c_str());
57 Host* Host::by_name_or_null(const char* name)
59 return (Host*) xbt_dict_get_or_null(host_list, name);
61 Host* Host::by_name_or_create(const char* name)
63 Host* host = by_name_or_null(name);
64 if (host == nullptr) {
65 host = new Host(name);
66 xbt_dict_set(host_list, name, host, NULL);
71 Host *Host::current(){
72 smx_process_t smx_proc = SIMIX_process_self();
74 xbt_die("Cannot call Host::current() from the maestro context");
75 return SIMIX_process_get_host(smx_proc);
79 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
82 void Host::turnOff() {
83 simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
87 return this->pimpl_cpu->isOn();
90 int Host::pstatesCount() const {
91 return this->pimpl_cpu->getNbPStates();
94 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
96 mounts = new boost::unordered_map<std::string, Storage*> ();
98 xbt_dict_t dict = this->mountedStoragesAsDict();
100 xbt_dict_cursor_t cursor;
103 xbt_dict_foreach(dict, cursor, mountname, storagename) {
104 mounts->insert({mountname, &Storage::byName(storagename)});
106 xbt_dict_free(&dict);
112 /** Get the properties assigned to a host */
113 xbt_dict_t Host::properties() {
114 return simgrid::simix::kernel([&] {
115 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
116 return surf_host->getProperties();
120 /** Retrieve the property value (or NULL if not set) */
121 const char*Host::property(const char*key) {
122 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
123 return surf_host->getProperty(key);
125 void Host::setProperty(const char*key, const char *value){
126 simgrid::simix::kernel([&] {
127 simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
128 surf_host->setProperty(key,value);
132 /** Get the processes attached to the host */
133 xbt_swag_t Host::processes()
135 return simgrid::simix::kernel([&]() {
136 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
140 /** Get the peak power of a host */
141 double Host::currentPowerPeak()
143 return simgrid::simix::kernel([&] {
144 return this->pimpl_cpu->getCurrentPowerPeak();
148 /** Get one power peak (in flops/s) of a host at a given pstate */
149 double Host::powerPeakAt(int pstate_index)
151 return simgrid::simix::kernel([&] {
152 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
156 /** @brief Get the speed of the cpu associated to a host */
157 double Host::speed() {
158 return pimpl_cpu->getSpeed(1.0);
160 /** @brief Returns the number of core of the processor. */
161 int Host::core_count() {
162 return pimpl_cpu->getCore();
165 /** @brief Set the pstate at which the host should run */
166 void Host::setPstate(int pstate_index)
168 simgrid::simix::kernel(std::bind(
169 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
172 /** @brief Retrieve the pstate at which the host is currently running */
175 return pimpl_cpu->getPState();
178 void Host::parameters(vm_params_t params)
180 simgrid::simix::kernel([&]() {
181 this->extension<simgrid::surf::HostImpl>()->getParams(params);
185 void Host::setParameters(vm_params_t params)
187 simgrid::simix::kernel([&]() {
188 this->extension<simgrid::surf::HostImpl>()->setParams(params);
193 * \ingroup simix_storage_management
194 * \brief Returns the list of storages mounted on an host.
195 * \return a dict containing all storages mounted on the host
197 xbt_dict_t Host::mountedStoragesAsDict()
199 return simgrid::simix::kernel([&] {
200 return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
205 * \ingroup simix_storage_management
206 * \brief Returns the list of storages attached to an host.
207 * \return a dict containing all storages attached to the host
209 xbt_dynar_t Host::attachedStorages()
211 return simgrid::simix::kernel([&] {
212 return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
216 } // namespace simgrid