Logo AND Algorithmique Numérique Distribuée

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