Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://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/smx_process_private.h"
18 #include "src/simix/smx_private.hpp"
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;
24 int SIMIX_HOST_LEVEL;
25 int ROUTING_HOST_LEVEL;
26 int USER_HOST_LEVEL;
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 }
44
45 Host::~Host() {
46   delete pimpl_cpu;
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, NULL);
67   }
68   return host;
69 }
70
71 Host *Host::current(){
72   smx_process_t smx_proc = SIMIX_process_self();
73   if (smx_proc == NULL)
74     xbt_die("Cannot call Host::current() from the maestro context");
75   return SIMIX_process_get_host(smx_proc);
76 }
77
78 void Host::turnOn() {
79   simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
80 }
81
82 void Host::turnOff() {
83   simgrid::simix::kernel(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 == NULL) {
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::kernel([&] {
115     simgrid::surf::HostImpl* surf_host = this->extension<simgrid::surf::HostImpl>();
116     return surf_host->getProperties();
117   });
118 }
119
120 /** Get the processes attached to the host */
121 xbt_swag_t Host::processes()
122 {
123   return simgrid::simix::kernel([&]() {
124     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
125   });
126 }
127
128 /** Get the peak power of a host */
129 double Host::currentPowerPeak()
130 {
131   return simgrid::simix::kernel([&] {
132     return this->pimpl_cpu->getCurrentPowerPeak();
133   });
134 }
135
136 /** Get one power peak (in flops/s) of a host at a given pstate */
137 double Host::powerPeakAt(int pstate_index)
138 {
139   return simgrid::simix::kernel([&] {
140     return this->pimpl_cpu->getPowerPeakAt(pstate_index);
141   });
142 }
143
144 /** @brief Get the speed of the cpu associated to a host */
145 double Host::speed() {
146   return pimpl_cpu->getSpeed(1.0);
147 }
148 /** @brief Returns the number of core of the processor. */
149 int Host::core_count() {
150   return pimpl_cpu->getCore();
151 }
152
153 /** @brief Set the pstate at which the host should run */
154 void Host::setPstate(int pstate_index)
155 {
156   simgrid::simix::kernel(std::bind(
157       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
158   ));
159 }
160 /** @brief Retrieve the pstate at which the host is currently running */
161 int Host::pstate()
162 {
163   return pimpl_cpu->getPState();
164 }
165
166 void Host::parameters(vm_params_t params)
167 {
168   simgrid::simix::kernel([&]() {
169     this->extension<simgrid::surf::HostImpl>()->getParams(params);
170   });
171 }
172
173 void Host::setParameters(vm_params_t params)
174 {
175   simgrid::simix::kernel([&]() {
176     this->extension<simgrid::surf::HostImpl>()->setParams(params);
177   });
178 }
179
180 /**
181  * \ingroup simix_storage_management
182  * \brief Returns the list of storages mounted on an host.
183  * \return a dict containing all storages mounted on the host
184  */
185 xbt_dict_t Host::mountedStoragesAsDict()
186 {
187   return simgrid::simix::kernel([&] {
188     return this->extension<simgrid::surf::HostImpl>()->getMountedStorageList();
189   });
190 }
191
192 /**
193  * \ingroup simix_storage_management
194  * \brief Returns the list of storages attached to an host.
195  * \return a dict containing all storages attached to the host
196  */
197 xbt_dynar_t Host::attachedStorages()
198 {
199   return simgrid::simix::kernel([&] {
200     return this->extension<simgrid::surf::HostImpl>()->getAttachedStorageList();
201   });
202 }
203
204 } // namespace simgrid
205 } // namespace s4u