Logo AND Algorithmique Numérique Distribuée

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