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/HostImplem.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"
26 int ROUTING_HOST_LEVEL;
32 template class Extendable<simgrid::s4u::Host>;
37 simgrid::xbt::signal<void(Host&)> Host::onCreation;
38 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
39 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
41 Host::Host(const char* name)
51 Host *Host::by_name(std::string name) {
52 Host* host = Host::by_name_or_null(name.c_str());
53 // TODO, raise an exception instead?
55 xbt_die("No such host: %s", name.c_str());
58 Host* Host::by_name_or_null(const char* name)
60 return (Host*) xbt_dict_get_or_null(host_list, name);
62 Host* Host::by_name_or_create(const char* name)
64 Host* host = by_name_or_null(name);
65 if (host == nullptr) {
66 host = new Host(name);
67 xbt_dict_set(host_list, name, host, NULL);
72 Host *Host::current(){
73 smx_process_t smx_proc = SIMIX_process_self();
75 xbt_die("Cannot call Host::current() from the maestro context");
76 return SIMIX_process_get_host(smx_proc);
80 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
83 void Host::turnOff() {
84 simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
88 return this->pimpl_cpu->isOn();
91 int Host::pstatesCount() const {
92 return this->pimpl_cpu->getNbPStates();
95 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
97 mounts = new boost::unordered_map<std::string, Storage*> ();
99 xbt_dict_t dict = this->mountedStoragesAsDict();
101 xbt_dict_cursor_t cursor;
104 xbt_dict_foreach(dict, cursor, mountname, storagename) {
105 mounts->insert({mountname, &Storage::byName(storagename)});
107 xbt_dict_free(&dict);
113 /** Get the properties assigned to a host */
114 xbt_dict_t Host::properties() {
115 return simgrid::simix::kernel([&] {
116 simgrid::surf::HostImplem* surf_host = this->extension<simgrid::surf::HostImplem>();
117 return surf_host->getProperties();
121 /** Get the processes attached to the host */
122 xbt_swag_t Host::processes()
124 return simgrid::simix::kernel([&]() {
125 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
129 /** Get the peak power of a host */
130 double Host::currentPowerPeak()
132 return simgrid::simix::kernel([&] {
133 return this->pimpl_cpu->getCurrentPowerPeak();
137 /** Get one power peak (in flops/s) of a host at a given pstate */
138 double Host::powerPeakAt(int pstate_index)
140 return simgrid::simix::kernel([&] {
141 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
145 /** @brief Get the speed of the cpu associated to a host */
146 double Host::speed() {
147 return pimpl_cpu->getSpeed(1.0);
149 /** @brief Returns the number of core of the processor. */
150 int Host::core_count() {
151 return pimpl_cpu->getCore();
154 /** @brief Set the pstate at which the host should run */
155 void Host::setPstate(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::parameters(vm_params_t params)
169 simgrid::simix::kernel([&]() {
170 this->extension<simgrid::surf::HostImplem>()->getParams(params);
174 void Host::setParameters(vm_params_t params)
176 simgrid::simix::kernel([&]() {
177 this->extension<simgrid::surf::HostImplem>()->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::mountedStoragesAsDict()
188 return simgrid::simix::kernel([&] {
189 return this->extension<simgrid::surf::HostImplem>()->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::attachedStorages()
200 return simgrid::simix::kernel([&] {
201 return this->extension<simgrid::surf::HostImplem>()->getAttachedStorageList();
205 } // namespace simgrid