Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
56229fed3333fb0766b5eb1803f7d49a3876cfcc
[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 {
49   xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
50
51   delete pimpl_;
52   delete pimpl_cpu;
53   delete pimpl_netcard;
54   delete mounts;
55 }
56
57 /** @brief Fire the required callbacks and destroy the object
58  *
59  * Don't delete directly an Host, call h->destroy() instead.
60  *
61  * This is cumbersome but this is the simplest solution to ensure that the
62  * onDestruction() callback receives a valid object (because of the destructor
63  * order in a class hierarchy).
64  */
65 void Host::destroy()
66 {
67   if (!currentlyDestroying_) {
68     currentlyDestroying_ = true;
69     xbt_dict_remove(host_list, name().c_str());
70     onDestruction(*this);
71     delete this;
72   }
73 }
74
75 Host* Host::by_name(std::string name)
76 {
77   Host* host = Host::by_name_or_null(name.c_str());
78   // TODO, raise an exception instead?
79   if (host == nullptr)
80     xbt_die("No such host: '%s'", name.c_str());
81   return host;
82 }
83 Host* Host::by_name_or_null(const char* name)
84 {
85   if (host_list == nullptr)
86     host_list = xbt_dict_new_homogeneous(nullptr);
87   return (Host*) xbt_dict_get_or_null(host_list, name);
88 }
89
90 Host *Host::current(){
91   smx_actor_t smx_proc = SIMIX_process_self();
92   if (smx_proc == nullptr)
93     xbt_die("Cannot call Host::current() from the maestro context");
94   return smx_proc->host;
95 }
96
97 void Host::turnOn() {
98   if (isOff()) {
99     simgrid::simix::kernelImmediate([&]{
100       this->extension<simgrid::simix::Host>()->turnOn();
101       this->pimpl_->turnOn();
102     });
103   }
104 }
105
106 void Host::turnOff() {
107   if (isOn()) {
108     simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
109   }
110 }
111
112 bool Host::isOn() {
113   return this->pimpl_cpu->isOn();
114 }
115
116 int Host::pstatesCount() const {
117   return this->pimpl_cpu->getNbPStates();
118 }
119
120 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
121   if (mounts == nullptr) {
122     mounts = new boost::unordered_map<std::string, Storage*> ();
123
124     xbt_dict_t dict = this->mountedStoragesAsDict();
125
126     xbt_dict_cursor_t cursor;
127     char *mountname;
128     char *storagename;
129     xbt_dict_foreach(dict, cursor, mountname, storagename) {
130       mounts->insert({mountname, &Storage::byName(storagename)});
131     }
132     xbt_dict_free(&dict);
133   }
134
135   return *mounts;
136 }
137
138 /** Get the properties assigned to a host */
139 xbt_dict_t Host::properties() {
140   return simgrid::simix::kernelImmediate([&] {
141     return this->pimpl_->getProperties();
142   });
143 }
144
145 /** Retrieve the property value (or nullptr if not set) */
146 const char*Host::property(const char*key) {
147   return this->pimpl_->getProperty(key);
148 }
149 void Host::setProperty(const char*key, const char *value){
150   simgrid::simix::kernelImmediate([&] {
151     this->pimpl_->setProperty(key,value);
152   });
153 }
154
155 /** Get the processes attached to the host */
156 xbt_swag_t Host::processes()
157 {
158   return simgrid::simix::kernelImmediate([&]() {
159     return this->extension<simgrid::simix::Host>()->process_list;
160   });
161 }
162
163 /** Get the peak power of a host */
164 double Host::getPstateSpeedCurrent()
165 {
166   return simgrid::simix::kernelImmediate([&] {
167     return this->pimpl_cpu->getPstateSpeedCurrent();
168   });
169 }
170
171 /** Get one power peak (in flops/s) of a host at a given pstate */
172 double Host::getPstateSpeed(int pstate_index)
173 {
174   return simgrid::simix::kernelImmediate([&] {
175     return this->pimpl_cpu->getPstateSpeed(pstate_index);
176   });
177 }
178
179 /** @brief Get the speed of the cpu associated to a host */
180 double Host::speed() {
181   return pimpl_cpu->getSpeed(1.0);
182 }
183 /** @brief Returns the number of core of the processor. */
184 int Host::coreCount() {
185   return pimpl_cpu->coreCount();
186 }
187
188 /** @brief Set the pstate at which the host should run */
189 void Host::setPstate(int pstate_index)
190 {
191   simgrid::simix::kernelImmediate(std::bind(
192       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
193   ));
194 }
195 /** @brief Retrieve the pstate at which the host is currently running */
196 int Host::pstate()
197 {
198   return pimpl_cpu->getPState();
199 }
200
201 void Host::parameters(vm_params_t params)
202 {
203   simgrid::simix::kernelImmediate([&]() {
204     this->pimpl_->getParams(params);
205   });
206 }
207
208 void Host::setParameters(vm_params_t params)
209 {
210   simgrid::simix::kernelImmediate([&]() {
211     this->pimpl_->setParams(params);
212   });
213 }
214
215 /**
216  * \ingroup simix_storage_management
217  * \brief Returns the list of storages mounted on an host.
218  * \return a dict containing all storages mounted on the host
219  */
220 xbt_dict_t Host::mountedStoragesAsDict()
221 {
222   return simgrid::simix::kernelImmediate([&] {
223     return this->pimpl_->getMountedStorageList();
224   });
225 }
226
227 /**
228  * \ingroup simix_storage_management
229  * \brief Returns the list of storages attached to an host.
230  * \return a dict containing all storages attached to the host
231  */
232 xbt_dynar_t Host::attachedStorages()
233 {
234   return simgrid::simix::kernelImmediate([&] {
235     return this->pimpl_->getAttachedStorageList();
236   });
237 }
238
239 } // namespace simgrid
240 } // namespace s4u