Logo AND Algorithmique Numérique Distribuée

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