Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a stupid function. Let's do regular C++
[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 #include "src/surf/HostImpl.hpp"
15 #include "xbt/log.h"
16 #include "src/msg/msg_private.h"
17 #include "src/simix/ActorImpl.hpp"
18 #include "src/simix/smx_private.h"
19 #include "src/surf/cpu_interface.hpp"
20 #include "simgrid/s4u/host.hpp"
21 #include "simgrid/s4u/storage.hpp"
22
23 xbt_dict_t host_list = nullptr; // FIXME: move it to Engine
24
25 int MSG_HOST_LEVEL = -1;
26 int USER_HOST_LEVEL = -1;
27
28 namespace simgrid {
29
30 namespace xbt {
31 template class Extendable<simgrid::s4u::Host>;
32 }
33
34 namespace s4u {
35
36 simgrid::xbt::signal<void(Host&)> Host::onCreation;
37 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
38 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
39
40 Host::Host(const char* name)
41   : name_(name)
42 {
43   xbt_assert(sg_host_by_name(name) == nullptr, "Refusing to create a second host named '%s'.", name);
44   xbt_dict_set(host_list, name, this, nullptr);
45 }
46
47 Host::~Host() {
48   delete pimpl_cpu;
49   delete pimpl_netcard;
50   delete mounts;
51 }
52
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?
56   if (host == nullptr)
57     xbt_die("No such host: %s", name.c_str());
58   return host;
59 }
60 Host* Host::by_name_or_null(const char* name)
61 {
62   if (host_list == nullptr)
63     host_list = xbt_dict_new_homogeneous([](void*p) {
64       simgrid::s4u::Host* host = static_cast<simgrid::s4u::Host*>(p);
65       simgrid::s4u::Host::onDestruction(*host);
66       delete host;
67     });
68   return (Host*) xbt_dict_get_or_null(host_list, name);
69 }
70
71 Host *Host::current(){
72   smx_actor_t smx_proc = SIMIX_process_self();
73   if (smx_proc == nullptr)
74     xbt_die("Cannot call Host::current() from the maestro context");
75   return smx_proc->host;
76 }
77
78 void Host::turnOn() {
79   if (isOff()) {
80     simgrid::simix::kernelImmediate([&]{
81       this->extension<simgrid::simix::Host>()->turnOn();
82       this->extension<simgrid::surf::HostImpl>()->turnOn();
83     });
84   }
85 }
86
87 void Host::turnOff() {
88   if (isOn()) {
89     simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
90   }
91 }
92
93 bool Host::isOn() {
94   return this->pimpl_cpu->isOn();
95 }
96
97 int Host::pstatesCount() const {
98   return this->pimpl_cpu->getNbPStates();
99 }
100
101 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
102   if (mounts == nullptr) {
103     mounts = new boost::unordered_map<std::string, Storage*> ();
104
105     xbt_dict_t dict = this->mountedStoragesAsDict();
106
107     xbt_dict_cursor_t cursor;
108     char *mountname;
109     char *storagename;
110     xbt_dict_foreach(dict, cursor, mountname, storagename) {
111       mounts->insert({mountname, &Storage::byName(storagename)});
112     }
113     xbt_dict_free(&dict);
114   }
115
116   return *mounts;
117 }
118
119 /** Get the properties assigned to a host */
120 xbt_dict_t Host::properties() {
121   return simgrid::simix::kernelImmediate([&] {
122     simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
123     return surf_host->getProperties();
124   });
125 }
126
127 /** Retrieve the property value (or nullptr if not set) */
128 const char*Host::property(const char*key) {
129   simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
130   return surf_host->getProperty(key);
131 }
132 void Host::setProperty(const char*key, const char *value){
133   simgrid::simix::kernelImmediate([&] {
134     simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
135     surf_host->setProperty(key,value);
136   });
137 }
138
139 /** Get the processes attached to the host */
140 xbt_swag_t Host::processes()
141 {
142   return simgrid::simix::kernelImmediate([&]() {
143     return this->extension<simgrid::simix::Host>()->process_list;
144   });
145 }
146
147 /** Get the peak power of a host */
148 double Host::getPstateSpeedCurrent()
149 {
150   return simgrid::simix::kernelImmediate([&] {
151     return this->pimpl_cpu->getPstateSpeedCurrent();
152   });
153 }
154
155 /** Get one power peak (in flops/s) of a host at a given pstate */
156 double Host::getPstateSpeed(int pstate_index)
157 {
158   return simgrid::simix::kernelImmediate([&] {
159     return this->pimpl_cpu->getPstateSpeed(pstate_index);
160   });
161 }
162
163 /** @brief Get the speed of the cpu associated to a host */
164 double Host::speed() {
165   return pimpl_cpu->getSpeed(1.0);
166 }
167 /** @brief Returns the number of core of the processor. */
168 int Host::coreCount() {
169   return pimpl_cpu->coreCount();
170 }
171
172 /** @brief Set the pstate at which the host should run */
173 void Host::setPstate(int pstate_index)
174 {
175   simgrid::simix::kernelImmediate(std::bind(
176       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
177   ));
178 }
179 /** @brief Retrieve the pstate at which the host is currently running */
180 int Host::pstate()
181 {
182   return pimpl_cpu->getPState();
183 }
184
185 void Host::parameters(vm_params_t params)
186 {
187   simgrid::simix::kernelImmediate([&]() {
188     this->extension<simgrid::surf::HostImpl>()->getParams(params);
189   });
190 }
191
192 void Host::setParameters(vm_params_t params)
193 {
194   simgrid::simix::kernelImmediate([&]() {
195     this->extension<simgrid::surf::HostImpl>()->setParams(params);
196   });
197 }
198
199 /**
200  * \ingroup simix_storage_management
201  * \brief Returns the list of storages mounted on an host.
202  * \return a dict containing all storages mounted on the host
203  */
204 xbt_dict_t Host::mountedStoragesAsDict()
205 {
206   return simgrid::simix::kernelImmediate([&] {
207     return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
208   });
209 }
210
211 /**
212  * \ingroup simix_storage_management
213  * \brief Returns the list of storages attached to an host.
214  * \return a dict containing all storages attached to the host
215  */
216 xbt_dynar_t Host::attachedStorages()
217 {
218   return simgrid::simix::kernelImmediate([&] {
219     return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
220   });
221 }
222
223 } // namespace simgrid
224 } // namespace s4u