Logo AND Algorithmique Numérique Distribuée

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