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 template class Extendable<simgrid::s4u::Host>;
39 simgrid::xbt::signal<void(Host&)> Host::onCreation;
40 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
41 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
43 Host::Host(const char* name)
53 Host *Host::by_name(std::string name) {
54 Host* host = Host::by_name_or_null(name.c_str());
55 // TODO, raise an exception instead?
57 xbt_die("No such host: %s", name.c_str());
61 Host *Host::current(){
62 smx_process_t smx_proc = SIMIX_process_self();
64 xbt_die("Cannot call Host::current() from the maestro context");
65 return SIMIX_process_get_host(smx_proc);
68 void Host::turn_on() {
69 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
72 void Host::turn_off() {
73 simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
77 return this->pimpl_cpu->isOn();
80 int Host::pstates_count() const {
81 return this->pimpl_cpu->getNbPStates();
84 boost::unordered_map<std::string, Storage*> const& Host::mounted_storages() {
86 mounts = new boost::unordered_map<std::string, Storage*> ();
88 xbt_dict_t dict = this->mounted_storages_as_dict();
90 xbt_dict_cursor_t cursor;
93 xbt_dict_foreach(dict, cursor, mountname, storagename) {
94 mounts->insert({mountname, &Storage::byName(storagename)});
102 /** Get the properties assigned to a host */
103 xbt_dict_t Host::properties() {
104 return simgrid::simix::kernel([&] {
105 simgrid::surf::Host* surf_host = this->extension<simgrid::surf::Host>();
106 return surf_host->getProperties();
110 /** Get the processes attached to the host */
111 xbt_swag_t Host::processes()
113 return simgrid::simix::kernel([&]() {
114 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
118 /** Get the peak power of a host */
119 double Host::current_power_peak()
121 return simgrid::simix::kernel([&] {
122 return this->pimpl_cpu->getCurrentPowerPeak();
126 /** Get one power peak (in flops/s) of a host at a given pstate */
127 double Host::power_peak_at(int pstate_index)
129 return simgrid::simix::kernel([&] {
130 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
134 /** @brief Get the speed of the cpu associated to a host */
135 double Host::speed() {
136 return pimpl_cpu->getSpeed(1.0);
138 /** @brief Returns the number of core of the processor. */
139 int Host::core_count() {
140 return pimpl_cpu->getCore();
143 Host* Host::by_name_or_null(const char* name)
145 return (Host*) xbt_dict_get_or_null(host_list, name);
148 Host* Host::by_name_or_create(const char* name)
150 Host* host = by_name_or_null(name);
151 if (host == nullptr) {
152 host = new Host(name);
153 xbt_dict_set(host_list, name, host, NULL);
158 /** @brief Set the pstate at which the host should run */
159 void Host::set_pstate(int pstate_index)
161 simgrid::simix::kernel(std::bind(
162 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
165 /** @brief Retrieve the pstate at which the host is currently running */
168 return pimpl_cpu->getPState();
171 void Host::get_parameters(vm_params_t params)
173 simgrid::simix::kernel([&]() {
174 this->extension<simgrid::surf::Host>()->getParams(params);
178 void Host::set_parameters(vm_params_t params)
180 simgrid::simix::kernel([&]() {
181 this->extension<simgrid::surf::Host>()->setParams(params);
186 * \ingroup simix_storage_management
187 * \brief Returns the list of storages mounted on an host.
188 * \return a dict containing all storages mounted on the host
190 xbt_dict_t Host::mounted_storages_as_dict()
192 return simgrid::simix::kernel([&] {
193 return this->extension<simgrid::surf::Host>()->getMountedStorageList();
198 * \ingroup simix_storage_management
199 * \brief Returns the list of storages attached to an host.
200 * \return a dict containing all storages attached to the host
202 xbt_dynar_t Host::attached_storages()
204 return simgrid::simix::kernel([&] {
205 return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
209 } // namespace simgrid