Logo AND Algorithmique Numérique Distribuée

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