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>
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 "src/surf/host_interface.hpp"
22 #include "simgrid/s4u/host.hpp"
23 #include "simgrid/s4u/storage.hpp"
28 int ROUTING_HOST_LEVEL;
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)
48 Host *Host::by_name(std::string name) {
49 Host* host = Host::by_name_or_null(name.c_str());
50 // TODO, raise an exception instead?
52 xbt_die("No such host: %s", name.c_str());
56 Host *Host::current(){
57 smx_process_t smx_proc = SIMIX_process_self();
59 xbt_die("Cannot call Host::current() from the maestro context");
60 return SIMIX_process_get_host(smx_proc);
63 void Host::turn_on() {
64 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
67 void Host::turn_off() {
68 simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
72 return this->pimpl_cpu->isOn();
75 int Host::pstates_count() const {
76 return this->pimpl_cpu->getNbPStates();
79 boost::unordered_map<std::string, Storage*> const& Host::mounted_storages() {
81 mounts = new boost::unordered_map<std::string, Storage*> ();
83 xbt_dict_t dict = this->mounted_storages_as_dict();
85 xbt_dict_cursor_t cursor;
88 xbt_dict_foreach(dict, cursor, mountname, storagename) {
89 mounts->insert({mountname, &Storage::byName(storagename)});
97 /** Get the properties assigned to a host */
98 xbt_dict_t Host::properties() {
99 return simgrid::simix::kernel([&] {
100 simgrid::surf::Host* surf_host = this->extension<simgrid::surf::Host>();
101 return surf_host->getProperties();
105 /** Get the processes attached to the host */
106 xbt_swag_t Host::processes()
108 return simgrid::simix::kernel([&]() {
109 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
113 /** Get the peak power of a host */
114 double Host::current_power_peak()
116 return simgrid::simix::kernel([&] {
117 return this->pimpl_cpu->getCurrentPowerPeak();
121 /** Get one power peak (in flops/s) of a host at a given pstate */
122 double Host::power_peak_at(int pstate_index)
124 return simgrid::simix::kernel([&] {
125 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
129 /** @brief Get the speed of the cpu associated to a host */
130 double Host::speed() {
131 return pimpl_cpu->getSpeed(1.0);
133 /** @brief Returns the number of core of the processor. */
134 int Host::core_count() {
135 return pimpl_cpu->getCore();
138 Host* Host::by_name_or_null(const char* name)
140 return (Host*) xbt_dict_get_or_null(host_list, name);
143 Host* Host::by_name_or_create(const char* name)
145 Host* host = by_name_or_null(name);
146 if (host == nullptr) {
147 host = new Host(name);
148 xbt_dict_set(host_list, name, host, NULL);
153 /** @brief Set the pstate at which the host should run */
154 void Host::set_pstate(int pstate_index)
156 simgrid::simix::kernel(std::bind(
157 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
160 /** @brief Retrieve the pstate at which the host is currently running */
163 return pimpl_cpu->getPState();
166 void Host::get_parameters(vm_params_t params)
168 simgrid::simix::kernel([&]() {
169 this->extension<simgrid::surf::Host>()->getParams(params);
173 void Host::set_parameters(vm_params_t params)
175 simgrid::simix::kernel([&]() {
176 this->extension<simgrid::surf::Host>()->setParams(params);
181 * \ingroup simix_storage_management
182 * \brief Returns the list of storages mounted on an host.
183 * \return a dict containing all storages mounted on the host
185 xbt_dict_t Host::mounted_storages_as_dict()
187 return simgrid::simix::kernel([&] {
188 return this->extension<simgrid::surf::Host>()->getMountedStorageList();
193 * \ingroup simix_storage_management
194 * \brief Returns the list of storages attached to an host.
195 * \return a dict containing all storages attached to the host
197 xbt_dynar_t Host::attached_storages()
199 return simgrid::simix::kernel([&] {
200 return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
204 } // namespace simgrid