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 boost::unordered_map<std::string, Host*> *Host::hosts
35 = new boost::unordered_map<std::string, 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::byName(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());
59 Host *Host::current(){
60 smx_process_t smx_proc = SIMIX_process_self();
62 xbt_die("Cannot call Host::current() from the maestro context");
63 return SIMIX_process_get_host(smx_proc);
67 simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
70 void Host::turnOff() {
71 simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
75 return this->pimpl_cpu->isOn();
78 int Host::getNbPStates() const {
79 return this->pimpl_cpu->getNbPStates();
82 boost::unordered_map<std::string, Storage&> &Host::mountedStorages() {
84 mounts = new boost::unordered_map<std::string, Storage&> ();
86 xbt_dict_t dict = this->getMountedStorageList();
88 xbt_dict_cursor_t cursor;
91 xbt_dict_foreach(dict, cursor, mountname, storagename) {
92 mounts->insert({mountname, Storage::byName(storagename)});
100 /** Get the properties assigned to a host */
101 xbt_dict_t Host::getProperties() {
102 return simgrid::simix::kernel([&] {
103 simgrid::surf::Host* surf_host = this->extension<simgrid::surf::Host>();
104 return surf_host->getProperties();
108 /** Get the processes attached to the host */
109 xbt_swag_t Host::getProcessList()
111 return simgrid::simix::kernel([&]() {
112 return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
116 /** Get the peak power of a host */
117 double Host::getCurrentPowerPeak()
119 return simgrid::simix::kernel([&] {
120 return this->pimpl_cpu->getCurrentPowerPeak();
124 /** Get one power peak (in flops/s) of a host at a given pstate */
125 double Host::getPowerPeakAt(int pstate_index)
127 return simgrid::simix::kernel([&] {
128 return this->pimpl_cpu->getPowerPeakAt(pstate_index);
132 /** @brief Get the speed of the cpu associated to a host */
133 double Host::getSpeed() {
134 return pimpl_cpu->getSpeed(1.0);
136 /** @brief Returns the number of core of the processor. */
137 int Host::getCoreAmount() {
138 return pimpl_cpu->getCore();
141 Host* Host::by_name_or_null(const char* name)
143 return (Host*) xbt_dict_get_or_null(host_list, name);
146 Host* Host::by_name_or_create(const char* name)
148 Host* host = by_name_or_null(name);
149 if (host == nullptr) {
150 host = new Host(name);
151 xbt_dict_set(host_list, name, host, NULL);
156 /** @brief Set the pstate at which the host should run */
157 void Host::setPState(int pstate_index)
159 simgrid::simix::kernel(std::bind(
160 &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
163 /** @brief Retrieve the pstate at which the host is currently running */
164 int Host::getPState()
166 return pimpl_cpu->getPState();
169 void Host::getParams(vm_params_t params)
171 simgrid::simix::kernel([&]() {
172 this->extension<simgrid::surf::Host>()->getParams(params);
176 void Host::setParams(vm_params_t params)
178 simgrid::simix::kernel([&]() {
179 this->extension<simgrid::surf::Host>()->setParams(params);
184 * \ingroup simix_storage_management
185 * \brief Returns the list of storages mounted on an host.
186 * \return a dict containing all storages mounted on the host
188 xbt_dict_t Host::getMountedStorageList()
190 return simgrid::simix::kernel([&] {
191 return this->extension<simgrid::surf::Host>()->getMountedStorageList();
196 * \ingroup simix_storage_management
197 * \brief Returns the list of storages attached to an host.
198 * \return a dict containing all storages attached to the host
200 xbt_dynar_t Host::getAttachedStorageList()
202 return simgrid::simix::kernel([&] {
203 return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
207 } // namespace simgrid