Logo AND Algorithmique Numérique Distribuée

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