Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start renaming AS to NetZone
[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/host.hpp"
14 #include "simgrid/s4u/storage.hpp"
15 #include "simgrid/simix.hpp"
16 #include "src/kernel/routing/NetCard.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 std::unordered_map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
27
28 int MSG_HOST_LEVEL = -1;
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
43 Host::Host(const char* name)
44   : name_(name)
45 {
46   xbt_assert(sg_host_by_name(name) == nullptr, "Refusing to create a second host named '%s'.", name);
47   host_list[name_] = this;
48 }
49
50 Host::~Host()
51 {
52   xbt_assert(currentlyDestroying_, "Please call h->destroy() instead of manually deleting it.");
53
54   delete pimpl_;
55   delete pimpl_cpu;
56   delete pimpl_netcard;
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 (!currentlyDestroying_) {
71     currentlyDestroying_ = true;
72     onDestruction(*this);
73     host_list.erase(name_);
74     delete this;
75   }
76 }
77
78 Host* Host::by_name(std::string name)
79 {
80   return host_list.at(name); // Will raise a std::out_of_range if the host does not exist
81 }
82 Host* Host::by_name_or_null(const char* name)
83 {
84   return by_name_or_null(std::string(name));
85 }
86 Host* Host::by_name_or_null(std::string name)
87 {
88   if (host_list.find(name) == host_list.end())
89     return nullptr;
90   return host_list.at(name);
91 }
92
93 Host *Host::current(){
94   smx_actor_t smx_proc = SIMIX_process_self();
95   if (smx_proc == nullptr)
96     xbt_die("Cannot call Host::current() from the maestro context");
97   return smx_proc->host;
98 }
99
100 void Host::turnOn() {
101   if (isOff()) {
102     simgrid::simix::kernelImmediate([&]{
103       this->extension<simgrid::simix::Host>()->turnOn();
104       this->pimpl_cpu->turnOn();
105     });
106   }
107 }
108
109 void Host::turnOff() {
110   if (isOn()) {
111     simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
112   }
113 }
114
115 bool Host::isOn() {
116   return this->pimpl_cpu->isOn();
117 }
118
119 int Host::pstatesCount() const {
120   return this->pimpl_cpu->getNbPStates();
121 }
122
123 /**
124  * \brief Find a route toward another host
125  *
126  * \param dest [IN] where to
127  * \param route [OUT] where to store the list of links (must exist, cannot be nullptr).
128  * \param latency [OUT] where to store the latency experienced on the path (or nullptr if not interested)
129  *                It is the caller responsibility to initialize latency to 0 (we add to provided route)
130  * \pre route!=nullptr
131  *
132  * walk through the routing components tree and find a route between hosts
133  * by calling each "get_route" function in each routing component.
134  */
135 void Host::routeTo(Host* dest, std::vector<Link*>* links, double* latency)
136 {
137   simgrid::kernel::routing::AsImpl::getGlobalRoute(pimpl_netcard, dest->pimpl_netcard, links, latency);
138   if (XBT_LOG_ISENABLED(surf_route, xbt_log_priority_debug)) {
139     XBT_CDEBUG(surf_route, "Route from '%s' to '%s' (latency: %f):", cname(), dest->cname(),
140                (latency == nullptr ? -1 : *latency));
141     for (auto link : *links)
142       XBT_CDEBUG(surf_route, "Link %s", link->getName());
143   }
144 }
145
146 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
147   if (mounts == nullptr) {
148     mounts = new boost::unordered_map<std::string, Storage*> ();
149
150     xbt_dict_t dict = this->mountedStoragesAsDict();
151
152     xbt_dict_cursor_t cursor;
153     char *mountname;
154     char *storagename;
155     xbt_dict_foreach(dict, cursor, mountname, storagename) {
156       mounts->insert({mountname, &Storage::byName(storagename)});
157     }
158     xbt_dict_free(&dict);
159   }
160
161   return *mounts;
162 }
163
164 /** Get the properties assigned to a host */
165 xbt_dict_t Host::properties() {
166   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getProperties(); });
167 }
168
169 /** Retrieve the property value (or nullptr if not set) */
170 const char*Host::property(const char*key) {
171   return this->pimpl_->getProperty(key);
172 }
173 void Host::setProperty(const char*key, const char *value){
174   simgrid::simix::kernelImmediate([&] { this->pimpl_->setProperty(key, value); });
175 }
176
177 /** Get the processes attached to the host */
178 xbt_swag_t Host::processes()
179 {
180   return simgrid::simix::kernelImmediate([&]() {
181     return this->extension<simgrid::simix::Host>()->process_list;
182   });
183 }
184
185 /** Get the peak power of a host */
186 double Host::getPstateSpeedCurrent()
187 {
188   return simgrid::simix::kernelImmediate([&] {
189     return this->pimpl_cpu->getPstateSpeedCurrent();
190   });
191 }
192
193 /** Get one power peak (in flops/s) of a host at a given pstate */
194 double Host::getPstateSpeed(int pstate_index)
195 {
196   return simgrid::simix::kernelImmediate([&] {
197     return this->pimpl_cpu->getPstateSpeed(pstate_index);
198   });
199 }
200
201 /** @brief Get the speed of the cpu associated to a host */
202 double Host::speed() {
203   return pimpl_cpu->getSpeed(1.0);
204 }
205 /** @brief Returns the number of core of the processor. */
206 int Host::coreCount() {
207   return pimpl_cpu->coreCount();
208 }
209
210 /** @brief Set the pstate at which the host should run */
211 void Host::setPstate(int pstate_index)
212 {
213   simgrid::simix::kernelImmediate(std::bind(
214       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
215   ));
216 }
217 /** @brief Retrieve the pstate at which the host is currently running */
218 int Host::pstate()
219 {
220   return pimpl_cpu->getPState();
221 }
222
223 /**
224  * \ingroup simix_storage_management
225  * \brief Returns the list of storages mounted on an host.
226  * \return a dict containing all storages mounted on the host
227  */
228 xbt_dict_t Host::mountedStoragesAsDict()
229 {
230   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getMountedStorageList(); });
231 }
232
233 /**
234  * \ingroup simix_storage_management
235  * \brief Returns the list of storages attached to an host.
236  * \return a dict containing all storages attached to the host
237  */
238 xbt_dynar_t Host::attachedStorages()
239 {
240   return simgrid::simix::kernelImmediate([&] { return this->pimpl_->getAttachedStorageList(); });
241 }
242
243 } // namespace simgrid
244 } // namespace s4u