Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Untabify.
[simgrid.git] / src / surf / workstation_interface.cpp
1 #include "workstation_interface.hpp"
2 #include "vm_workstation_interface.hpp"
3 #include "cpu_cas01.hpp"
4 #include "simgrid/sg_config.h"
5
6 extern "C" {
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
8                                 "Logging specific to the SURF workstation module");
9 }
10
11 WorkstationModelPtr surf_workstation_model = NULL;
12
13 /*********
14  * Model *
15  *********/
16 WorkstationModel::WorkstationModel(string name)
17  : Model(name)
18 {
19   p_cpuModel = surf_cpu_model_pm;
20 }
21
22 WorkstationModel::WorkstationModel()
23 : Model("Workstation") {
24   p_cpuModel = surf_cpu_model_pm;
25 }
26
27 WorkstationModel::~WorkstationModel() {
28 }
29
30
31
32 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
33  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
34  * active task, the dummy CPU action must be deactivated, so that the VM does
35  * not get any CPU share in the PM layer. */
36 void WorkstationModel::adjustWeightOfDummyCpuActions()
37 {
38   /* iterate for all hosts including virtual machines */
39   xbt_lib_cursor_t cursor;
40   char *key;
41   void **ind_host;
42
43   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
44     WorkstationPtr ws = dynamic_cast<WorkstationPtr>(
45                                        static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
46     CpuCas01LmmPtr cpu_cas01 = dynamic_cast<CpuCas01LmmPtr>(
47                                static_cast<ResourcePtr>(ind_host[SURF_CPU_LEVEL]));
48
49     if (!ws)
50       continue;
51     /* skip if it is not a virtual machine */
52     if (ws->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
53       continue;
54     xbt_assert(cpu_cas01, "cpu-less workstation");
55
56     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
57     WorkstationVMLmmPtr ws_vm = dynamic_cast<WorkstationVMLmmPtr>(ws);
58
59     int is_active = lmm_constraint_used(cpu_cas01->p_model->p_maxminSystem, cpu_cas01->p_constraint);
60     // int is_active_old = constraint_is_active(cpu_cas01);
61
62     // {
63     //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
64     // }
65
66     if (is_active) {
67       /* some tasks exist on this VM */
68       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
69
70       /* FIXME: we shoud use lmm_update_variable_weight() ? */
71       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
72       ws_vm->p_action->setPriority(1);
73
74     } else {
75       /* no task exits on this VM */
76       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
77
78       ws_vm->p_action->setPriority(0);
79     }
80   }
81 }
82
83 /************
84  * Resource *
85  ************/
86 Workstation::Workstation(xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
87  : p_storage(storage), p_netElm(netElm), p_cpu(cpu)
88 {}
89
90 int Workstation::getCore(){
91   return p_cpu->getCore();
92 }
93
94 double Workstation::getSpeed(double load){
95   return p_cpu->getSpeed(load);
96 }
97
98 double Workstation::getAvailableSpeed(){
99   return p_cpu->getAvailableSpeed();
100 }
101
102 double Workstation::getCurrentPowerPeak()
103 {
104   return p_cpu->getCurrentPowerPeak();
105 }
106
107 double Workstation::getPowerPeakAt(int pstate_index)
108 {
109   return p_cpu->getPowerPeakAt(pstate_index);
110 }
111
112 int Workstation::getNbPstates()
113 {
114   return p_cpu->getNbPstates();
115 }
116
117 void Workstation::setPowerPeakAt(int pstate_index)
118 {
119         p_cpu->setPowerPeakAt(pstate_index);
120 }
121
122 double Workstation::getConsumedEnergy()
123 {
124   return p_cpu->getConsumedEnergy();
125 }
126
127 xbt_dict_t Workstation::getProperties()
128 {
129   return p_cpu->m_properties;
130 }
131
132
133 StoragePtr Workstation::findStorageOnMountList(const char* mount)
134 {
135   StoragePtr st = NULL;
136   s_mount_t mnt;
137   unsigned int cursor;
138
139   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, m_name);
140   xbt_dynar_foreach(p_storage,cursor,mnt)
141   {
142     XBT_DEBUG("See '%s'",mnt.name);
143     if(!strcmp(mount,mnt.name)){
144       st = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage));
145       break;
146     }
147   }
148   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, m_name);
149   return st;
150 }
151
152 xbt_dict_t Workstation::getStorageList()
153 {
154   s_mount_t mnt;
155   unsigned int i;
156   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
157   char *storage_name = NULL;
158
159   xbt_dynar_foreach(p_storage,i,mnt){
160     storage_name = (char *)dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage))->m_name;
161     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
162   }
163   return storage_list;
164 }
165
166 ActionPtr Workstation::open(const char* mount, const char* path) {
167   StoragePtr st = findStorageOnMountList(mount);
168   XBT_DEBUG("OPEN on disk '%s'", st->m_name);
169   return st->open(mount, path);
170 }
171
172 ActionPtr Workstation::close(surf_file_t fd) {
173   StoragePtr st = findStorageOnMountList(fd->mount);
174   XBT_DEBUG("CLOSE on disk '%s'",st->m_name);
175   return st->close(fd);
176 }
177
178 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
179   StoragePtr st = findStorageOnMountList(fd->mount);
180   XBT_DEBUG("READ on disk '%s'",st->m_name);
181   return st->read(fd, size);
182 }
183
184 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
185   StoragePtr st = findStorageOnMountList(fd->mount);
186   XBT_DEBUG("WRITE on disk '%s'",st->m_name);
187   return st->write(fd, size);
188 }
189
190 int Workstation::unlink(surf_file_t fd) {
191   if (!fd){
192     XBT_WARN("No such file descriptor. Impossible to unlink");
193     return 0;
194   } else {
195 //    XBT_INFO("%s %zu", fd->storage, fd->size);
196     StoragePtr st = findStorageOnMountList(fd->mount);
197     /* Check if the file is on this storage */
198     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
199       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
200           st->m_name);
201       return 0;
202     } else {
203       XBT_DEBUG("UNLINK on disk '%s'",st->m_name);
204       st->m_usedSize -= fd->size;
205
206       // Remove the file from storage
207       xbt_dict_remove(st->p_content, fd->name);
208
209       free(fd->name);
210       free(fd->mount);
211       xbt_free(fd);
212       return 1;
213     }
214   }
215 }
216
217 ActionPtr Workstation::ls(const char* mount, const char *path){
218   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
219   StoragePtr st = findStorageOnMountList(mount);
220   return st->ls(path);
221 }
222
223 sg_size_t Workstation::getSize(surf_file_t fd){
224   return fd->size;
225 }
226
227 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
228 {
229   StoragePtr st = findStorageOnMountList(fd->mount);
230   sg_size_t *psize = xbt_new(sg_size_t, 1);
231   *psize = fd->size;
232   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
233   xbt_dynar_push_as(info, sg_size_t *, psize);
234   xbt_dynar_push_as(info, void *, fd->mount);
235   xbt_dynar_push_as(info, void *, (void *)st->m_name);
236   xbt_dynar_push_as(info, void *, st->p_typeId);
237   xbt_dynar_push_as(info, void *, st->p_contentType);
238
239   return info;
240 }
241
242 sg_size_t Workstation::fileTell(surf_file_t fd){
243   return fd->current_position;
244 }
245
246 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
247
248   switch (origin) {
249   case SEEK_SET:
250     fd->current_position = 0;
251         return MSG_OK;
252   case SEEK_CUR:
253         if(offset > fd->size)
254           offset = fd->size;
255         fd->current_position = offset;
256         return MSG_OK;
257   case SEEK_END:
258         fd->current_position = fd->size;
259         return MSG_OK;
260   default:
261         return MSG_TASK_CANCELED;
262   }
263 }
264
265 sg_size_t Workstation::getFreeSize(const char* name)
266 {
267   StoragePtr st = findStorageOnMountList(name);
268   return st->m_size - st->m_usedSize;
269 }
270
271 sg_size_t Workstation::getUsedSize(const char* name)
272 {
273   StoragePtr st = findStorageOnMountList(name);
274   return st->m_usedSize;
275 }
276
277 xbt_dynar_t Workstation::getVms()
278 {
279   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
280
281   /* iterate for all hosts including virtual machines */
282   xbt_lib_cursor_t cursor;
283   char *key;
284   void **ind_host;
285   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
286     WorkstationPtr ws = dynamic_cast<WorkstationPtr>(static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
287     if (!ws)
288       continue;
289     /* skip if it is not a virtual machine */
290     if (ws->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
291       continue;
292
293     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
294     WorkstationVMPtr ws_vm = dynamic_cast<WorkstationVMPtr>(ws);
295     if (this == ws_vm-> p_subWs)
296       xbt_dynar_push(dyn, &ws_vm->p_subWs);
297   }
298
299   return dyn;
300 }
301
302 void Workstation::getParams(ws_params_t params)
303 {
304   memcpy(params, &p_params, sizeof(s_ws_params_t));
305 }
306
307 void Workstation::setParams(ws_params_t params)
308 {
309   /* may check something here. */
310   memcpy(&p_params, params, sizeof(s_ws_params_t));
311 }
312
313 /**********
314  * Action *
315  **********/