Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a21b4d01ec4a5cb30788150c07ec94e570310da0
[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
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 "src/surf/host_interface.hpp"
21
22 #include "simgrid/s4u/host.hpp"
23 #include "simgrid/s4u/storage.hpp"
24
25 int MSG_HOST_LEVEL;
26 int SD_HOST_LEVEL;
27 int SIMIX_HOST_LEVEL;
28 int ROUTING_HOST_LEVEL;
29 int USER_HOST_LEVEL;
30
31 namespace simgrid {
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         if (mounts != NULL)
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
57 Host *Host::current(){
58         smx_process_t smx_proc = SIMIX_process_self();
59         if (smx_proc == NULL)
60                 xbt_die("Cannot call Host::current() from the maestro context");
61         return SIMIX_process_get_host(smx_proc);
62 }
63
64 void Host::turn_on() {
65         simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
66 }
67
68 void Host::turn_off() {
69   simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
70 }
71
72 bool Host::is_on() {
73         return this->pimpl_cpu->isOn();
74 }
75
76 int Host::pstates_count() const {
77         return this->pimpl_cpu->getNbPStates();
78 }
79
80 boost::unordered_map<std::string, Storage*> const& Host::mounted_storages() {
81         if (mounts == NULL) {
82                 mounts = new boost::unordered_map<std::string, Storage*> ();
83
84                 xbt_dict_t dict = this->mounted_storages_as_dict();
85
86                 xbt_dict_cursor_t cursor;
87                 char *mountname;
88                 char *storagename;
89                 xbt_dict_foreach(dict, cursor, mountname, storagename) {
90                         mounts->insert({mountname, &Storage::byName(storagename)});
91                 }
92                 xbt_dict_free(&dict);
93         }
94
95         return *mounts;
96 }
97
98 /** Get the properties assigned to a host */
99 xbt_dict_t Host::properties() {
100   return simgrid::simix::kernel([&] {
101                 simgrid::surf::Host* surf_host = this->extension<simgrid::surf::Host>();
102                 return surf_host->getProperties();
103         });
104 }
105
106 /** Get the processes attached to the host */
107 xbt_swag_t Host::processes()
108 {
109   return simgrid::simix::kernel([&]() {
110     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
111   });
112 }
113
114 /** Get the peak power of a host */
115 double Host::current_power_peak()
116 {
117   return simgrid::simix::kernel([&] {
118     return this->pimpl_cpu->getCurrentPowerPeak();
119   });
120 }
121
122 /** Get one power peak (in flops/s) of a host at a given pstate */
123 double Host::power_peak_at(int pstate_index)
124 {
125   return simgrid::simix::kernel([&] {
126     return this->pimpl_cpu->getPowerPeakAt(pstate_index);
127   });
128 }
129
130 /** @brief Get the speed of the cpu associated to a host */
131 double Host::speed() {
132         return pimpl_cpu->getSpeed(1.0);
133 }
134 /** @brief Returns the number of core of the processor. */
135 int Host::core_count() {
136         return pimpl_cpu->getCore();
137 }
138
139 Host* Host::by_name_or_null(const char* name)
140 {
141   return (Host*) xbt_dict_get_or_null(host_list, name);
142 }
143
144 Host* Host::by_name_or_create(const char* name)
145 {
146   Host* host = by_name_or_null(name);
147   if (host == nullptr) {
148     host = new Host(name);
149     xbt_dict_set(host_list, name, host, NULL);
150   }
151   return host;
152 }
153
154 /** @brief Set the pstate at which the host should run */
155 void Host::set_pstate(int pstate_index)
156 {
157   simgrid::simix::kernel(std::bind(
158       &simgrid::surf::Cpu::setPState, pimpl_cpu, pstate_index
159   ));
160 }
161 /** @brief Retrieve the pstate at which the host is currently running */
162 int Host::pstate()
163 {
164   return pimpl_cpu->getPState();
165 }
166
167 void Host::get_parameters(vm_params_t params)
168 {
169   simgrid::simix::kernel([&]() {
170     this->extension<simgrid::surf::Host>()->getParams(params);
171   });
172 }
173
174 void Host::set_parameters(vm_params_t params)
175 {
176   simgrid::simix::kernel([&]() {
177     this->extension<simgrid::surf::Host>()->setParams(params);
178   });
179 }
180
181 /**
182  * \ingroup simix_storage_management
183  * \brief Returns the list of storages mounted on an host.
184  * \return a dict containing all storages mounted on the host
185  */
186 xbt_dict_t Host::mounted_storages_as_dict()
187 {
188   return simgrid::simix::kernel([&] {
189     return this->extension<simgrid::surf::Host>()->getMountedStorageList();
190   });
191 }
192
193 /**
194  * \ingroup simix_storage_management
195  * \brief Returns the list of storages attached to an host.
196  * \return a dict containing all storages attached to the host
197  */
198 xbt_dynar_t Host::attached_storages()
199 {
200   return simgrid::simix::kernel([&] {
201     return this->extension<simgrid::surf::Host>()->getAttachedStorageList();
202   });
203 }
204
205 } // namespace simgrid
206 } // namespace s4u