Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9bab73463030c55d97c26eb55d3e30efc1d235a0
[simgrid.git] / src / s4u / s4u_host.cpp
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <string>
8 #include <functional>
9 #include <stdexcept>
10
11 #include <unordered_map>
12
13 #include <simgrid/simix.hpp>
14
15 #include "xbt/log.h"
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"
21
22 #include "simgrid/s4u/host.hpp"
23 #include "simgrid/s4u/storage.hpp"
24
25 int MSG_HOST_LEVEL;
26 int SD_HOST_LEVEL;
27 int SIMIX_HOST_LEVEL;
28 int ROUTING_HOST_LEVEL;
29 int USER_HOST_LEVEL;
30
31 namespace simgrid {
32 namespace s4u {
33
34 boost::unordered_map<std::string, Host*> *Host::hosts
35                 = new boost::unordered_map<std::string, Host*>();
36
37 simgrid::xbt::signal<void(Host&)> Host::onCreation;
38 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
39 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
40
41 Host::Host(const char* name)
42   : name_(name)
43 {
44 }
45
46 Host::~Host() {
47         if (mounts != NULL)
48                 delete mounts;
49 }
50
51 Host *Host::byName(std::string name) {
52         Host* host = Host::by_name_or_null(name.c_str());
53         // TODO, raise an exception instead?
54         if (host == nullptr)
55                 xbt_die("No such host: %s", name.c_str());
56         return host;
57 }
58
59 Host *Host::current(){
60         smx_process_t smx_proc = SIMIX_process_self();
61         if (smx_proc == NULL)
62                 xbt_die("Cannot call Host::current() from the maestro context");
63         return SIMIX_process_get_host(smx_proc);
64 }
65
66 void Host::turnOn() {
67         simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
68 }
69
70 void Host::turnOff() {
71         simgrid::simix::simcall<void>(SIMCALL_HOST_OFF, this);
72 }
73
74 bool Host::isOn() {
75         return this->pimpl_cpu->isOn();
76 }
77
78 int Host::getNbPStates() const {
79         return this->pimpl_cpu->getNbPStates();
80 }
81
82 boost::unordered_map<std::string, Storage&> &Host::mountedStorages() {
83         if (mounts == NULL) {
84                 mounts = new boost::unordered_map<std::string, Storage&> ();
85
86                 xbt_dict_t dict = this->getMountedStorageList();
87
88                 xbt_dict_cursor_t cursor;
89                 char *mountname;
90                 char *storagename;
91                 xbt_dict_foreach(dict, cursor, mountname, storagename) {
92                         mounts->insert({mountname, Storage::byName(storagename)});
93                 }
94                 xbt_dict_free(&dict);
95         }
96
97         return *mounts;
98 }
99
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();
105         });
106 }
107
108 /** Get the processes attached to the host */
109 xbt_swag_t Host::getProcessList()
110 {
111   return simgrid::simix::kernel([&]() {
112     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
113   });
114 }
115
116 /** Get the peak power of a host */
117 double Host::getCurrentPowerPeak()
118 {
119   return simgrid::simix::kernel([&] {
120     return this->pimpl_cpu->getCurrentPowerPeak();
121   });
122 }
123
124 /** Get one power peak (in flops/s) of a host at a given pstate */
125 double Host::getPowerPeakAt(int pstate_index)
126 {
127   return simgrid::simix::kernel([&] {
128     return this->pimpl_cpu->getPowerPeakAt(pstate_index);
129   });
130 }
131
132 /** @brief Get the speed of the cpu associated to a host */
133 double Host::getSpeed() {
134         return pimpl_cpu->getSpeed(1.0);
135 }
136 /** @brief Returns the number of core of the processor. */
137 int Host::getCoreAmount() {
138         return pimpl_cpu->getCore();
139 }
140
141 Host* Host::by_name_or_null(const char* name)
142 {
143   return (Host*) xbt_dict_get_or_null(host_list, name);
144 }
145
146 Host* Host::by_name_or_create(const char* name)
147 {
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);
152   }
153   return host;
154 }
155
156 /** @brief Set the pstate at which the host should run */
157 void Host::setPState(int pstate_index)
158 {
159   simgrid::simix::kernel(std::bind(
160       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
161   ));
162 }
163 /** @brief Retrieve the pstate at which the host is currently running */
164 int Host::getPState()
165 {
166   return pimpl_cpu->getPState();
167 }
168
169 void Host::getParams(vm_params_t params)
170 {
171   simgrid::simix::kernel([&]() {
172     this->extension<simgrid::surf::Host>()->getParams(params);
173   });
174 }
175
176 void Host::setParams(vm_params_t params)
177 {
178   simgrid::simix::kernel([&]() {
179     this->extension<simgrid::surf::Host>()->setParams(params);
180   });
181 }
182
183 /**
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
187  */
188 xbt_dict_t Host::getMountedStorageList()
189 {
190   return simgrid::simix::kernel([&] {
191     return this->extension<simgrid::surf::Host>()->getMountedStorageList();
192   });
193 }
194
195 /**
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
199  */
200 xbt_dynar_t Host::getAttachedStorageList()
201 {
202   return simgrid::simix::kernel([&] {
203     return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
204   });
205 }
206
207 } // namespace simgrid
208 } // namespace s4u