Logo AND Algorithmique Numérique Distribuée

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