Logo AND Algorithmique Numérique Distribuée

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