Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
642671a0c036b56430f5c0a47018d39409f1c92e
[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/HostImplem.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 SD_HOST_LEVEL;
25 int SIMIX_HOST_LEVEL;
26 int ROUTING_HOST_LEVEL;
27 int USER_HOST_LEVEL;
28
29 namespace simgrid {
30
31 namespace xbt {
32 template class Extendable<simgrid::s4u::Host>;
33 }
34
35 namespace s4u {
36
37 simgrid::xbt::signal<void(Host&)> Host::onCreation;
38 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
39 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
40
41 Host::Host(const char* name)
42   : name_(name)
43 {
44 }
45
46 Host::~Host() {
47   delete pimpl_cpu;
48   delete mounts;
49 }
50
51 Host *Host::by_name(std::string name) {
52   Host* host = Host::by_name_or_null(name.c_str());
53   // TODO, raise an exception instead?
54   if (host == nullptr)
55     xbt_die("No such host: %s", name.c_str());
56   return host;
57 }
58 Host* Host::by_name_or_null(const char* name)
59 {
60   return (Host*) xbt_dict_get_or_null(host_list, name);
61 }
62 Host* Host::by_name_or_create(const char* name)
63 {
64   Host* host = by_name_or_null(name);
65   if (host == nullptr) {
66     host = new Host(name);
67     xbt_dict_set(host_list, name, host, NULL);
68   }
69   return host;
70 }
71
72 Host *Host::current(){
73   smx_process_t smx_proc = SIMIX_process_self();
74   if (smx_proc == NULL)
75     xbt_die("Cannot call Host::current() from the maestro context");
76   return SIMIX_process_get_host(smx_proc);
77 }
78
79 void Host::turnOn() {
80   simgrid::simix::kernel(std::bind(SIMIX_host_on, this));
81 }
82
83 void Host::turnOff() {
84   simgrid::simix::kernel(std::bind(SIMIX_host_off, this, SIMIX_process_self()));
85 }
86
87 bool Host::isOn() {
88   return this->pimpl_cpu->isOn();
89 }
90
91 int Host::pstatesCount() const {
92   return this->pimpl_cpu->getNbPStates();
93 }
94
95 boost::unordered_map<std::string, Storage*> const& Host::mountedStorages() {
96   if (mounts == NULL) {
97     mounts = new boost::unordered_map<std::string, Storage*> ();
98
99     xbt_dict_t dict = this->mountedStoragesAsDict();
100
101     xbt_dict_cursor_t cursor;
102     char *mountname;
103     char *storagename;
104     xbt_dict_foreach(dict, cursor, mountname, storagename) {
105       mounts->insert({mountname, &Storage::byName(storagename)});
106     }
107     xbt_dict_free(&dict);
108   }
109
110   return *mounts;
111 }
112
113 /** Get the properties assigned to a host */
114 xbt_dict_t Host::properties() {
115   return simgrid::simix::kernel([&] {
116     simgrid::surf::HostImplem* surf_host = this->extension<simgrid::surf::HostImplem>();
117     return surf_host->getProperties();
118   });
119 }
120
121 /** Get the processes attached to the host */
122 xbt_swag_t Host::processes()
123 {
124   return simgrid::simix::kernel([&]() {
125     return ((smx_host_priv_t)this->extension(SIMIX_HOST_LEVEL))->process_list;
126   });
127 }
128
129 /** Get the peak power of a host */
130 double Host::currentPowerPeak()
131 {
132   return simgrid::simix::kernel([&] {
133     return this->pimpl_cpu->getCurrentPowerPeak();
134   });
135 }
136
137 /** Get one power peak (in flops/s) of a host at a given pstate */
138 double Host::powerPeakAt(int pstate_index)
139 {
140   return simgrid::simix::kernel([&] {
141     return this->pimpl_cpu->getPowerPeakAt(pstate_index);
142   });
143 }
144
145 /** @brief Get the speed of the cpu associated to a host */
146 double Host::speed() {
147   return pimpl_cpu->getSpeed(1.0);
148 }
149 /** @brief Returns the number of core of the processor. */
150 int Host::core_count() {
151   return pimpl_cpu->getCore();
152 }
153
154 /** @brief Set the pstate at which the host should run */
155 void Host::setPstate(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::parameters(vm_params_t params)
168 {
169   simgrid::simix::kernel([&]() {
170     this->extension<simgrid::surf::HostImplem>()->getParams(params);
171   });
172 }
173
174 void Host::setParameters(vm_params_t params)
175 {
176   simgrid::simix::kernel([&]() {
177     this->extension<simgrid::surf::HostImplem>()->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::mountedStoragesAsDict()
187 {
188   return simgrid::simix::kernel([&] {
189     return this->extension<simgrid::surf::HostImplem>()->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::attachedStorages()
199 {
200   return simgrid::simix::kernel([&] {
201     return this->extension<simgrid::surf::HostImplem>()->getAttachedStorageList();
202   });
203 }
204
205 } // namespace simgrid
206 } // namespace s4u