Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a6022e6e0fe7f10ea4c246a829439aee38dbc8d8
[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/simix.hpp"
14 #include "src/surf/HostImpl.hpp"
15 #include "xbt/log.h"
16 #include "src/msg/msg_private.h"
17 #include "src/simix/ActorImpl.hpp"
18 #include "src/simix/smx_private.h"
19 #include "src/surf/cpu_interface.hpp"
20 #include "simgrid/s4u/host.hpp"
21 #include "simgrid/s4u/storage.hpp"
22
23 xbt_dict_t host_list = nullptr; // FIXME: make it a static field of Host
24
25 int MSG_HOST_LEVEL = -1;
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
40 Host::Host(const char* name)
41   : name_(name)
42 {
43   xbt_dict_set(host_list, name, this, nullptr);
44 }
45
46 Host::~Host() {
47   delete pimpl_cpu;
48   delete pimpl_netcard;
49   delete mounts;
50 }
51
52 Host *Host::by_name(std::string name) {
53   Host* host = Host::by_name_or_null(name.c_str());
54   // TODO, raise an exception instead?
55   if (host == nullptr)
56     xbt_die("No such host: %s", name.c_str());
57   return host;
58 }
59 Host* Host::by_name_or_null(const char* name)
60 {
61   if (host_list == nullptr)
62     host_list = xbt_dict_new_homogeneous([](void*p) {
63       simgrid::s4u::Host* host = static_cast<simgrid::s4u::Host*>(p);
64       simgrid::s4u::Host::onDestruction(*host);
65       delete host;
66     });
67   return (Host*) xbt_dict_get_or_null(host_list, name);
68 }
69 Host* Host::by_name_or_create(const char* name)
70 {
71   Host* host = by_name_or_null(name);
72   if (host == nullptr)
73     host = new Host(name);
74
75   return host;
76 }
77
78 Host *Host::current(){
79   smx_actor_t smx_proc = SIMIX_process_self();
80   if (smx_proc == nullptr)
81     xbt_die("Cannot call Host::current() from the maestro context");
82   return smx_proc->host;
83 }
84
85 void Host::turnOn() {
86   if (isOff()) {
87     simgrid::simix::kernelImmediate([&]{
88       this->extension<simgrid::simix::Host>()->turnOn();
89       this->extension<simgrid::surf::HostImpl>()->turnOn();
90     });
91   }
92 }
93
94 void Host::turnOff() {
95   if (isOn()) {
96     simgrid::simix::kernelImmediate(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
97   }
98 }
99
100 bool Host::isOn() {
101   return this->pimpl_cpu->isOn();
102 }
103
104 int Host::pstatesCount() const {
105   return this->pimpl_cpu->getNbPStates();
106 }
107
108 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
109   if (mounts == nullptr) {
110     mounts = new boost::unordered_map<std::string, Storage*> ();
111
112     xbt_dict_t dict = this->mountedStoragesAsDict();
113
114     xbt_dict_cursor_t cursor;
115     char *mountname;
116     char *storagename;
117     xbt_dict_foreach(dict, cursor, mountname, storagename) {
118       mounts->insert({mountname, &Storage::byName(storagename)});
119     }
120     xbt_dict_free(&dict);
121   }
122
123   return *mounts;
124 }
125
126 /** Get the properties assigned to a host */
127 xbt_dict_t Host::properties() {
128   return simgrid::simix::kernelImmediate([&] {
129     simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
130     return surf_host->getProperties();
131   });
132 }
133
134 /** Retrieve the property value (or nullptr if not set) */
135 const char*Host::property(const char*key) {
136   simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
137   return surf_host->getProperty(key);
138 }
139 void Host::setProperty(const char*key, const char *value){
140   simgrid::simix::kernelImmediate([&] {
141     simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
142     surf_host->setProperty(key,value);
143   });
144 }
145
146 /** Get the processes attached to the host */
147 xbt_swag_t Host::processes()
148 {
149   return simgrid::simix::kernelImmediate([&]() {
150     return this->extension<simgrid::simix::Host>()->process_list;
151   });
152 }
153
154 /** Get the peak power of a host */
155 double Host::getPstateSpeedCurrent()
156 {
157   return simgrid::simix::kernelImmediate([&] {
158     return this->pimpl_cpu->getPstateSpeedCurrent();
159   });
160 }
161
162 /** Get one power peak (in flops/s) of a host at a given pstate */
163 double Host::getPstateSpeed(int pstate_index)
164 {
165   return simgrid::simix::kernelImmediate([&] {
166     return this->pimpl_cpu->getPstateSpeed(pstate_index);
167   });
168 }
169
170 /** @brief Get the speed of the cpu associated to a host */
171 double Host::speed() {
172   return pimpl_cpu->getSpeed(1.0);
173 }
174 /** @brief Returns the number of core of the processor. */
175 int Host::coreCount() {
176   return pimpl_cpu->coreCount();
177 }
178
179 /** @brief Set the pstate at which the host should run */
180 void Host::setPstate(int pstate_index)
181 {
182   simgrid::simix::kernelImmediate(std::bind(
183       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
184   ));
185 }
186 /** @brief Retrieve the pstate at which the host is currently running */
187 int Host::pstate()
188 {
189   return pimpl_cpu->getPState();
190 }
191
192 void Host::parameters(vm_params_t params)
193 {
194   simgrid::simix::kernelImmediate([&]() {
195     this->extension<simgrid::surf::HostImpl>()->getParams(params);
196   });
197 }
198
199 void Host::setParameters(vm_params_t params)
200 {
201   simgrid::simix::kernelImmediate([&]() {
202     this->extension<simgrid::surf::HostImpl>()->setParams(params);
203   });
204 }
205
206 /**
207  * \ingroup simix_storage_management
208  * \brief Returns the list of storages mounted on an host.
209  * \return a dict containing all storages mounted on the host
210  */
211 xbt_dict_t Host::mountedStoragesAsDict()
212 {
213   return simgrid::simix::kernelImmediate([&] {
214     return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
215   });
216 }
217
218 /**
219  * \ingroup simix_storage_management
220  * \brief Returns the list of storages attached to an host.
221  * \return a dict containing all storages attached to the host
222  */
223 xbt_dynar_t Host::attachedStorages()
224 {
225   return simgrid::simix::kernelImmediate([&] {
226     return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
227   });
228 }
229
230 } // namespace simgrid
231 } // namespace s4u