Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
72c3dfcb379e61968ce0bdb3f173925273f82551
[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/s4u/engine.hpp"
14 #include "simgrid/s4u/host.hpp"
15 #include "simgrid/s4u/storage.hpp"
16 #include "simgrid/simix.hpp"
17 #include "src/kernel/routing/NetCard.hpp"
18 #include "src/msg/msg_private.h"
19 #include "src/simix/ActorImpl.hpp"
20 #include "src/simix/smx_private.h"
21 #include "src/surf/HostImpl.hpp"
22 #include "src/surf/cpu_interface.hpp"
23 #include "xbt/log.h"
24
25 XBT_LOG_EXTERNAL_CATEGORY(surf_route);
26
27 std::unordered_map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
28
29 int MSG_HOST_LEVEL = -1;
30 int USER_HOST_LEVEL = -1;
31
32 namespace simgrid {
33
34 namespace xbt {
35 template class Extendable<simgrid::s4u::Host>;
36 }
37
38 namespace s4u {
39
40 simgrid::xbt::signal<void(Host&)> Host::onCreation;
41 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
42 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
43
44 Host::Host(const char* name)
45   : name_(name)
46 {
47   xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name);
48   host_list[name_] = 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_netcard != nullptr) // not removed yet by a children class
57     simgrid::s4u::Engine::instance()->netcardUnregister(pimpl_netcard);
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 (!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   if (host_list.find(name) == host_list.end())
91     return nullptr;
92   return host_list.at(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([&]{
105       this->extension<simgrid::simix::Host>()->turnOn();
106       this->pimpl_cpu->turnOn();
107     });
108   }
109 }
110
111 void Host::turnOff() {
112   if (isOn()) {
113     simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
114   }
115 }
116
117 bool Host::isOn() {
118   return this->pimpl_cpu->isOn();
119 }
120
121 int Host::pstatesCount() const {
122   return this->pimpl_cpu->getNbPStates();
123 }
124
125 /**
126  * \brief Find a route toward another host
127  *
128  * \param dest [IN] where to
129  * \param route [OUT] where to store the list of links (must exist, cannot be nullptr).
130  * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
131  *                It is the caller responsibility to initialize latency to 0 (we add to provided route)
132  * \pre route!=nullptr
133  *
134  * walk through the routing components tree and find a route between hosts
135  * by calling each "get_route" function in each routing component.
136  */
137 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
138 {
139   simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
140   if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
141     XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
142                (latency == nullptr ? -1 : *latency));
143     for (auto link : *links)
144       XBT_CDEBUG(surf_route, "Link %s", link->getName());
145   }
146 }
147
148 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
149   if (mounts == nullptr) {
150     mounts = new boost::unordered_map<std::string, Storage*> ();
151
152     xbt_dict_t dict = this->mountedStoragesAsDict();
153
154     xbt_dict_cursor_t cursor;
155     char *mountname;
156     char *storagename;
157     xbt_dict_foreach(dict, cursor, mountname, storagename) {
158       mounts->insert({mountname, &Storage::byName(storagename)});
159     }
160     xbt_dict_free(&dict);
161   }
162
163   return *mounts;
164 }
165
166 /** Get the properties assigned to a host */
167 xbt_dict_t Host::properties() {
168   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
169 }
170
171 /** Retrieve the property value (or nullptr if not set) */
172 const char*Host::property(const char*key) {
173   return this->pimpl_->getProperty(key);
174 }
175 void Host::setProperty(const char*key, const char *value){
176   simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
177 }
178
179 /** Get the processes attached to the host */
180 xbt_swag_t Host::processes()
181 {
182   return simgrid::simix::kernelImmediate([&]() {
183     return this->extension<simgrid::simix::Host>()->process_list;
184   });
185 }
186
187 /** Get the peak power of a host */
188 double Host::getPstateSpeedCurrent()
189 {
190   return simgrid::simix::kernelImmediate([&] {
191     return this->pimpl_cpu->getPstateSpeedCurrent();
192   });
193 }
194
195 /** Get one power peak (in flops/s) of a host at a given pstate */
196 double Host::getPstateSpeed(int pstate_index)
197 {
198   return simgrid::simix::kernelImmediate([&] {
199     return this->pimpl_cpu->getPstateSpeed(pstate_index);
200   });
201 }
202
203 /** @brief Get the speed of the cpu associated to a host */
204 double Host::speed() {
205   return pimpl_cpu->getSpeed(1.0);
206 }
207 /** @brief Returns the number of core of the processor. */
208 int Host::coreCount() {
209   return pimpl_cpu->coreCount();
210 }
211
212 /** @brief Set the pstate at which the host should run */
213 void Host::setPstate(int pstate_index)
214 {
215   simgrid::simix::kernelImmediate(std::bind(
216       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
217   ));
218 }
219 /** @brief Retrieve the pstate at which the host is currently running */
220 int Host::pstate()
221 {
222   return pimpl_cpu->getPState();
223 }
224
225 /**
226  * \ingroup simix_storage_management
227  * \brief Returns the list of storages mounted on an host.
228  * \return a dict containing all storages mounted on the host
229  */
230 xbt_dict_t Host::mountedStoragesAsDict()
231 {
232   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
233 }
234
235 /**
236  * \ingroup simix_storage_management
237  * \brief Returns the list of storages attached to an host.
238  * \return a dict containing all storages attached to the host
239  */
240 xbt_dynar_t Host::attachedStorages()
241 {
242   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
243 }
244
245 } // namespace simgrid
246 } // namespace s4u