Logo AND Algorithmique Numérique Distribuée

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