Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case and cleanup some more methods of s4u::Engine
[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) : name_(name)
31 {
32   xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
33   Engine::get_instance()->host_register(std::string(name_), this);
34   new simgrid::surf::HostImpl(this);
35 }
36
37 Host::~Host()
38 {
39   xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
40
41   delete pimpl_;
42   if (pimpl_netpoint != nullptr) // not removed yet by a children class
43     simgrid::s4u::Engine::get_instance()->netpoint_unregister(pimpl_netpoint);
44   delete pimpl_cpu;
45   delete mounts;
46 }
47
48 /** @brief Fire the required callbacks and destroy the object
49  *
50  * Don't delete directly an Host, call h->destroy() instead.
51  *
52  * This is cumbersome but this is the simplest solution to ensure that the
53  * onDestruction() callback receives a valid object (because of the destructor
54  * order in a class hierarchy).
55  */
56 void Host::destroy()
57 {
58   if (not currentlyDestroying_) {
59     currentlyDestroying_ = true;
60     onDestruction(*this);
61     Engine::get_instance()->host_unregister(std::string(name_));
62     delete this;
63   }
64 }
65
66 Host* Host::by_name(std::string name)
67 {
68   return Engine::get_instance()->host_by_name(name);
69 }
70 Host* Host::by_name(const char* name)
71 {
72   return Engine::get_instance()->host_by_name(std::string(name));
73 }
74 Host* Host::by_name_or_null(const char* name)
75 {
76   return Engine::get_instance()->host_by_name_or_null(std::string(name));
77 }
78 Host* Host::by_name_or_null(std::string name)
79 {
80   return Engine::get_instance()->host_by_name_or_null(name);
81 }
82
83 Host* Host::current()
84 {
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 {
93   if (isOff()) {
94     simgrid::simix::kernelImmediate([this] {
95       this->extension<simgrid::simix::Host>()->turnOn();
96       this->pimpl_cpu->turn_on();
97       onStateChange(*this);
98     });
99   }
100 }
101
102 void Host::turnOff()
103 {
104   if (isOn()) {
105     smx_actor_t self = SIMIX_process_self();
106     simgrid::simix::kernelImmediate([this, self] {
107       SIMIX_host_off(this, self);
108       onStateChange(*this);
109     });
110   }
111 }
112
113 bool Host::isOn()
114 {
115   return this->pimpl_cpu->is_on();
116 }
117
118 int Host::getPstatesCount() const
119 {
120   return this->pimpl_cpu->getNbPStates();
121 }
122
123 /**
124  * \brief Return the list of actors attached to an host.
125  *
126  * \param whereto a vector in which we should push actors living on that host
127  */
128 void Host::actorList(std::vector<ActorPtr>* whereto)
129 {
130   for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
131     whereto->push_back(actor.ciface());
132   }
133 }
134
135 /**
136  * \brief Find a route toward another host
137  *
138  * \param dest [IN] where to
139  * \param links [OUT] where to store the list of links (must exist, cannot be nullptr).
140  * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
141  *                It is the caller responsibility to initialize latency to 0 (we add to provided route)
142  * \pre links!=nullptr
143  *
144  * walk through the routing components tree and find a route between hosts
145  * by calling each "get_route" function in each routing component.
146  */
147 void Host::routeTo(Host* dest, std::vector<Link*>& links, double* latency)
148 {
149   std::vector<kernel::resource::LinkImpl*> linkImpls;
150   this->routeTo(dest, linkImpls, latency);
151   for (kernel::resource::LinkImpl* const& l : linkImpls)
152     links.push_back(&l->piface_);
153 }
154
155 /** @brief Just like Host::routeTo, but filling an array of link implementations */
156 void Host::routeTo(Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency)
157 {
158   simgrid::kernel::routing::NetZoneImpl::get_global_route(pimpl_netpoint, dest->pimpl_netpoint, links, latency);
159   if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
160     XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", get_cname(), dest->get_cname(),
161                (latency == nullptr ? -1 : *latency));
162     for (auto const& link : links)
163       XBT_CDEBUG(surf_route, "Link %s", link->get_cname());
164   }
165 }
166
167 /** Get the properties assigned to a host */
168 std::map<std::string, std::string>* Host::getProperties()
169 {
170   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
171 }
172
173 /** Retrieve the property value (or nullptr if not set) */
174 const char* Host::getProperty(const char* key)
175 {
176   return this->pimpl_->getProperty(key);
177 }
178
179 void Host::setProperty(std::string key, std::string value)
180 {
181   simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
182 }
183
184 /** Get the processes attached to the host */
185 void Host::getProcesses(std::vector<ActorPtr>* list)
186 {
187   for (auto& actor : this->extension<simgrid::simix::Host>()->process_list) {
188     list->push_back(actor.iface());
189   }
190 }
191
192 /** @brief Returns how many actors have been launched on this host */
193 // FIXME: Specify whether the user should expect only non-daemon actors here!
194 int Host::get_actor_count()
195 {
196   return this->extension<simgrid::simix::Host>()->process_list.size();
197 }
198
199 /** @brief Get the peak processor speed (in flops/s), at the specified pstate  */
200 double Host::getPstateSpeed(int pstate_index)
201 {
202   return simgrid::simix::kernelImmediate(
203       [this, pstate_index] { return this->pimpl_cpu->getPstateSpeed(pstate_index); });
204 }
205
206 /** @brief Get the peak processor speed (under full load (=1.0), in flops/s), at the current pstate */
207 double Host::getSpeed()
208 {
209   return pimpl_cpu->getSpeed(1.0);
210 }
211
212 /** @brief Returns the number of core of the processor. */
213 int Host::getCoreCount()
214 {
215   return pimpl_cpu->coreCount();
216 }
217
218 /** @brief Set the pstate at which the host should run */
219 void Host::setPstate(int pstate_index)
220 {
221   simgrid::simix::kernelImmediate([this, pstate_index] { this->pimpl_cpu->setPState(pstate_index); });
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 s4u
273 } // namespace simgrid