Logo AND Algorithmique Numérique Distribuée

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