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)
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());
57 Host *Host::current(){
58 smx_process_t smx_proc = SIMIX_process_self();
60 xbt_die("Cannot call Host::current() from the maestro context");
61 return SIMIX_process_get_host(smx_proc);
64 void Host::turn_on() {
65 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
68 void Host::turn_off() {
69 simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
73 return this->pimpl_cpu->isOn();
76 int Host::pstates_count() const {
77 return this->pimpl_cpu->getNbPStates();
80 boost::unordered_map<std::string, Storage*> const& Host::mounted_storages() {
82 mounts = new boost::unordered_map<std::string, Storage*> ();
84 xbt_dict_t dict = this->mounted_storages_as_dict();
86 xbt_dict_cursor_t cursor;
89 xbt_dict_foreach(dict, cursor, mountname, storagename) {
90 mounts->insert({mountname, &Storage::byName(storagename)});
98 /** Get the properties assigned to a host */
99 xbt_dict_t Host::properties() {
100 return simgrid::simix::kernel([&] {
101 simgrid::surf::Host* surf_host = this->extension<simgrid::surf::Host>();
102 return surf_host->getProperties();
106 /** Get the processes attached to the host */
107 xbt_swag_t Host::processes()
109 return simgrid::simix::kernel([&]() {
110 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
114 /** Get the peak power of a host */
115 double Host::current_power_peak()
117 return simgrid::simix::kernel([&] {
118 return this->pimpl_cpu->getCurrentPowerPeak();
122 /** Get one power peak (in flops/s) of a host at a given pstate */
123 double Host::power_peak_at(int pstate_index)
125 return simgrid::simix::kernel([&] {
126 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
130 /** @brief Get the speed of the cpu associated to a host */
131 double Host::speed() {
132 return pimpl_cpu->getSpeed(1.0);
134 /** @brief Returns the number of core of the processor. */
135 int Host::core_count() {
136 return pimpl_cpu->getCore();
139 Host* Host::by_name_or_null(const char* name)
141 return (Host*) xbt_dict_get_or_null(host_list, name);
144 Host* Host::by_name_or_create(const char* name)
146 Host* host = by_name_or_null(name);
147 if (host == nullptr) {
148 host = new Host(name);
149 xbt_dict_set(host_list, name, host, NULL);
154 /** @brief Set the pstate at which the host should run */
155 void Host::set_pstate(int pstate_index)
157 simgrid::simix::kernel(std::bind(
158 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
161 /** @brief Retrieve the pstate at which the host is currently running */
164 return pimpl_cpu->getPState();
167 void Host::get_parameters(vm_params_t params)
169 simgrid::simix::kernel([&]() {
170 this->extension<simgrid::surf::Host>()->getParams(params);
174 void Host::set_parameters(vm_params_t params)
176 simgrid::simix::kernel([&]() {
177 this->extension<simgrid::surf::Host>()->setParams(params);
182 * \ingroup simix_storage_management
183 * \brief Returns the list of storages mounted on an host.
184 * \return a dict containing all storages mounted on the host
186 xbt_dict_t Host::mounted_storages_as_dict()
188 return simgrid::simix::kernel([&] {
189 return this->extension<simgrid::surf::Host>()->getMountedStorageList();
194 * \ingroup simix_storage_management
195 * \brief Returns the list of storages attached to an host.
196 * \return a dict containing all storages attached to the host
198 xbt_dynar_t Host::attached_storages()
200 return simgrid::simix::kernel([&] {
201 return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
205 } // namespace simgrid