Logo AND Algorithmique Numérique Distribuée

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