Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7c18d226d8d3f564697153daf3d74a60fa4bb63b
[simgrid.git] / src / s4u / s4u_host.cpp
1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/kernel/routing/NetPoint.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "src/simix/smx_host_private.hpp"
9 #include "src/surf/HostImpl.hpp"
10
11 #include <string>
12
13 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
14
15 int USER_HOST_LEVEL = -1;
16
17 namespace simgrid {
18
19 namespace xbt {
20 template class Extendable<simgrid::s4u::Host>;
21 }
22
23 namespace s4u {
24
25 simgrid::xbt::signal<void(Host&)> Host::onCreation;
26 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
27 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
28 simgrid::xbt::signal<void(Host&)> Host::onSpeedChange;
29
30 Host::Host(const char* name)
31   : name_(name)
32 {
33   xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
34   Engine::getInstance()->addHost(std::string(name_), this);
35   new simgrid::surf::HostImpl(this);
36 }
37
38 Host::~Host()
39 {
40   xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
41
42   delete pimpl_;
43   if (pimpl_netpoint != nullptr) // not removed yet by a children class
44     simgrid::s4u::Engine::getInstance()->netpointUnregister(pimpl_netpoint);
45   delete pimpl_cpu;
46   delete mounts;
47 }
48
49 /** @brief Fire the required callbacks and destroy the object
50  *
51  * Don't delete directly an Host, call h->destroy() instead.
52  *
53  * This is cumbersome but this is the simplest solution to ensure that the
54  * onDestruction() callback receives a valid object (because of the destructor
55  * order in a class hierarchy).
56  */
57 void Host::destroy()
58 {
59   if (not currentlyDestroying_) {
60     currentlyDestroying_ = true;
61     onDestruction(*this);
62     Engine::getInstance()->delHost(std::string(name_));
63     delete this;
64   }
65 }
66
67 Host* Host::by_name(std::string name)
68 {
69   return Engine::getInstance()->hostByName(name);
70 }
71 Host* Host::by_name(const char* name)
72 {
73   return Engine::getInstance()->hostByName(std::string(name));
74 }
75 Host* Host::by_name_or_null(const char* name)
76 {
77   return Engine::getInstance()->hostByNameOrNull(std::string(name));
78 }
79 Host* Host::by_name_or_null(std::string name)
80 {
81   return Engine::getInstance()->hostByNameOrNull(name);
82 }
83
84 Host *Host::current(){
85   smx_actor_t smx_proc = SIMIX_process_self();
86   if (smx_proc == nullptr)
87     xbt_die("Cannot call Host::current() from the maestro context");
88   return smx_proc->host;
89 }
90
91 void Host::turnOn() {
92   if (isOff()) {
93     simgrid::simix::kernelImmediate([this] {
94       this->extension<simgrid::simix::Host>()->turnOn();
95       this->pimpl_cpu->turn_on();
96       onStateChange(*this);
97     });
98   }
99 }
100
101 void Host::turnOff() {
102   if (isOn()) {
103     smx_actor_t self = SIMIX_process_self();
104     simgrid::simix::kernelImmediate([this, self] {
105       SIMIX_host_off(this, self);
106       onStateChange(*this);
107     });
108   }
109 }
110
111 bool Host::isOn() {
112   return this->pimpl_cpu->is_on();
113 }
114
115 int Host::getPstatesCount() const
116 {
117   return this->pimpl_cpu->getNbPStates();
118 }
119
120 /**
121  * \brief Return the list of actors attached to an host.
122  *
123  * \param whereto a vector in which we should push actors living on that host
124  */
125 void Host::actorList(std::vector<ActorPtr>* whereto)
126 {
127   for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
128     whereto->push_back(actor.ciface());
129   }
130 }
131
132 /**
133  * \brief Find a route toward another host
134  *
135  * \param dest [IN] where to
136  * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
137  * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
138  *                It is the caller responsibility to initialize latency to 0 (we add to provided route)
139  * \pre links!=nullptr
140  *
141  * walk through the routing components tree and find a route between hosts
142  * by calling each "get_route" function in each routing component.
143  */
144 void Host::routeTo(Host* dest, std::vector<Link*>& links, double* latency)
145 {
146   std::vector<kernel::resource::LinkImpl*> linkImpls;
147   this->routeTo(dest, linkImpls, latency);
148   for (kernel::resource::LinkImpl* const& l : linkImpls)
149     links.push_back(&l->piface_);
150 }
151
152 /** @brief Just like Host::routeTo, but filling an array of link implementations */
153 void Host::routeTo(Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency)
154 {
155   simgrid::kernel::routing::NetZoneImpl::get_global_route(pimpl_netpoint, dest->pimpl_netpoint, links, latency);
156   if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
157     XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", get_cname(), dest->get_cname(),
158                (latency == nullptr ? -1 : *latency));
159     for (auto const& link : links)
160       XBT_CDEBUG(surf_route, "Link %s", link->get_cname());
161   }
162 }
163
164 /** Get the properties assigned to a host */
165 std::map<std::string, std::string>* Host::getProperties()
166 {
167   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
168 }
169
170 /** Retrieve the property value (or nullptr if not set) */
171 const char* Host::getProperty(const char* key)
172 {
173   return this->pimpl_->getProperty(key);
174 }
175
176 void Host::setProperty(std::string key, std::string value)
177 {
178   simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
179 }
180
181 /** Get the processes attached to the host */
182 void Host::getProcesses(std::vector<ActorPtr>* list)
183 {
184   for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
185     list->push_back(actor.iface());
186   }
187 }
188
189 /** @brief Returns how many actors have been launched on this host */
190 // FIXME: Specify whether the user should expect only non-daemon actors here!
191 int Host::get_actor_count() const
192 {
193   return this->extension<simgrid::simix::Host>()->process_list.size();
194 }
195
196 /** @brief Get the peak processor speed (in flops/s), at the specified pstate  */
197 double Host::getPstateSpeed(int pstate_index)
198 {
199   return simgrid::simix::kernelImmediate([this, pstate_index] {
200     return this->pimpl_cpu->getPstateSpeed(pstate_index);
201   });
202 }
203
204 /** @brief Get the peak processor speed (under full load (=1.0), in flops/s), at the current pstate */
205 double Host::getSpeed()
206 {
207   return pimpl_cpu->getSpeed(1.0);
208 }
209
210 /** @brief Returns the number of core of the processor. */
211 int Host::getCoreCount()
212 {
213   return pimpl_cpu->coreCount();
214 }
215
216 /** @brief Set the pstate at which the host should run */
217 void Host::setPstate(int pstate_index)
218 {
219   simgrid::simix::kernelImmediate([this, pstate_index] {
220       this->pimpl_cpu->setPState(pstate_index);
221   });
222 }
223 /** @brief Retrieve the pstate at which the host is currently running */
224 int Host::getPstate()
225 {
226   return this->pimpl_cpu->getPState();
227 }
228
229 /**
230  * \ingroup simix_storage_management
231  * \brief Returns the list of storages attached to an host.
232  * \return a vector containing all storages attached to the host
233  */
234 std::vector<const char*> Host::get_attached_storages()
235 {
236   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->get_attached_storages(); });
237 }
238
239 void Host::getAttachedStorages(std::vector<const char*>* storages)
240 {
241   std::vector<const char*> local_storages =
242       simgrid::simix::kernelImmediate([this] { return this->pimpl_->get_attached_storages(); });
243   for (auto elm : local_storages)
244     storages->push_back(elm);
245 }
246
247 std::unordered_map<std::string, Storage*> const& Host::getMountedStorages()
248 {
249   if (mounts == nullptr) {
250     mounts = new std::unordered_map<std::string, Storage*>();
251     for (auto const& m : this->pimpl_->storage_) {
252       mounts->insert({m.first, &m.second->piface_});
253     }
254   }
255   return *mounts;
256 }
257
258 void Host::execute(double flops)
259 {
260   Host* host_list[1]   = {this};
261   double flops_list[1] = {flops};
262   smx_activity_t s     = simcall_execution_parallel_start(nullptr /*name*/, 1, host_list, flops_list,
263                                                       nullptr /*comm_sizes */, -1.0, -1 /*timeout*/);
264   simcall_execution_wait(s);
265 }
266
267 double Host::getLoad()
268 {
269   return this->pimpl_cpu->get_load();
270 }
271
272 } // namespace simgrid
273 } // namespace s4u