Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a first draft for automatic privatization of global variables for smpi.
[simgrid.git] / src / surf / workstation_interface.cpp
1 /* Copyright (c) 2013-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 "workstation_interface.hpp"
8 #include "vm_workstation_interface.hpp"
9 #include "cpu_cas01.hpp"
10 #include "simgrid/sg_config.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
13                                 "Logging specific to the SURF workstation module");
14
15 WorkstationModelPtr surf_workstation_model = NULL;
16
17 /*************
18  * Callbacks *
19  *************/
20
21 surf_callback(void, WorkstationPtr) workstationCreatedCallbacks;
22 surf_callback(void, WorkstationPtr) workstationDestructedCallbacks;
23 surf_callback(void, WorkstationPtr) workstationStateChangedCallbacks;
24 surf_callback(void, WorkstationActionPtr) workstationActionStateChangedCallbacks;
25
26 /*********
27  * Model *
28  *********/
29 WorkstationModel::WorkstationModel(const char *name)
30  : Model(name)
31 {
32   p_cpuModel = surf_cpu_model_pm;
33 }
34
35 WorkstationModel::WorkstationModel()
36 : Model("Workstation") {
37   p_cpuModel = surf_cpu_model_pm;
38 }
39
40 WorkstationModel::~WorkstationModel() {
41 }
42
43 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
44  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
45  * active task, the dummy CPU action must be deactivated, so that the VM does
46  * not get any CPU share in the PM layer. */
47 void WorkstationModel::adjustWeightOfDummyCpuActions()
48 {
49   /* iterate for all virtual machines */
50   for (WorkstationVMModel::vm_list_t::iterator iter =
51          WorkstationVMModel::ws_vms.begin();
52        iter !=  WorkstationVMModel::ws_vms.end(); ++iter) {
53
54     WorkstationVMPtr ws_vm = &*iter;
55     CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ws_vm->p_cpu);
56     xbt_assert(cpu_cas01, "cpu-less workstation");
57
58     int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
59     // int is_active_old = constraint_is_active(cpu_cas01);
60
61     // {
62     //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
63     // }
64
65     if (is_active) {
66       /* some tasks exist on this VM */
67       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
68
69       /* FIXME: we shoud use lmm_update_variable_weight() ? */
70       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
71       ws_vm->p_action->setPriority(1);
72
73     } else {
74       /* no task exits on this VM */
75       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
76
77       ws_vm->p_action->setPriority(0);
78     }
79   }
80 }
81
82 /************
83  * Resource *
84  ************/
85 Workstation::Workstation()
86 {
87   surf_callback_emit(workstationCreatedCallbacks, this);
88 }
89
90 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
91                                  xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
92  : Resource(model, name, props)
93  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
94 {
95   p_params.ramsize = 0;
96   surf_callback_emit(workstationCreatedCallbacks, this);
97 }
98
99 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
100                                          xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
101  : Resource(model, name, props, constraint)
102  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
103 {
104   p_params.ramsize = 0;
105   surf_callback_emit(workstationCreatedCallbacks, this);
106 }
107
108 Workstation::~Workstation(){
109   surf_callback_emit(workstationDestructedCallbacks, this);
110 }
111
112 void Workstation::setState(e_surf_resource_state_t state){
113   Resource::setState(state);
114   surf_callback_emit(workstationStateChangedCallbacks, this);
115 }
116
117 int Workstation::getCore(){
118   return p_cpu->getCore();
119 }
120
121 double Workstation::getSpeed(double load){
122   return p_cpu->getSpeed(load);
123 }
124
125 double Workstation::getAvailableSpeed(){
126   return p_cpu->getAvailableSpeed();
127 }
128
129 double Workstation::getCurrentPowerPeak()
130 {
131   return p_cpu->getCurrentPowerPeak();
132 }
133
134 double Workstation::getPowerPeakAt(int pstate_index)
135 {
136   return p_cpu->getPowerPeakAt(pstate_index);
137 }
138
139 int Workstation::getNbPstates()
140 {
141   return p_cpu->getNbPstates();
142 }
143
144 void Workstation::setPowerPeakAt(int pstate_index)
145 {
146         p_cpu->setPowerPeakAt(pstate_index);
147 }
148
149 xbt_dict_t Workstation::getProperties()
150 {
151   return p_cpu->getProperties();
152 }
153
154 StoragePtr Workstation::findStorageOnMountList(const char* mount)
155 {
156   StoragePtr st = NULL;
157   s_mount_t mnt;
158   unsigned int cursor;
159
160   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
161   xbt_dynar_foreach(p_storage,cursor,mnt)
162   {
163     XBT_DEBUG("See '%s'",mnt.name);
164     if(!strcmp(mount,mnt.name)){
165       st = static_cast<StoragePtr>(mnt.storage);
166       break;
167     }
168   }
169   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
170   return st;
171 }
172
173 xbt_dict_t Workstation::getMountedStorageList()
174 {
175   s_mount_t mnt;
176   unsigned int i;
177   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
178   char *storage_name = NULL;
179
180   xbt_dynar_foreach(p_storage,i,mnt){
181     storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
182     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
183   }
184   return storage_list;
185 }
186
187 xbt_dynar_t Workstation::getAttachedStorageList()
188 {
189   xbt_lib_cursor_t cursor;
190   char *key;
191   void **data;
192   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
193   xbt_lib_foreach(storage_lib, cursor, key, data) {
194     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
195           StoragePtr storage = static_cast<StoragePtr>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
196           if(!strcmp((const char*)storage->p_attach,this->getName())){
197             xbt_dynar_push_as(result, void *,(void *)static_cast<ResourcePtr>(storage)->getName());
198           }
199         }
200   }
201   return result;
202 }
203
204 ActionPtr Workstation::open(const char* fullpath) {
205
206   StoragePtr st = NULL;
207   s_mount_t mnt;
208   unsigned int cursor;
209   size_t longest_prefix_length = 0;
210   char *path = NULL;
211   char *file_mount_name = NULL;
212   char *mount_name = NULL;
213
214   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
215   xbt_dynar_foreach(p_storage,cursor,mnt)
216   {
217     XBT_DEBUG("See '%s'",mnt.name);
218     file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
219     strncpy(file_mount_name,fullpath,strlen(mnt.name));
220     file_mount_name[strlen(mnt.name)] = '\0';
221
222     if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
223     {/* The current mount name is found in the full path and is bigger than the previous*/
224       longest_prefix_length = strlen(mnt.name);
225       st = static_cast<StoragePtr>(mnt.storage);
226     }
227     free(file_mount_name);
228   }
229   if(longest_prefix_length>0)
230   { /* Mount point found, split fullpath into mount_name and path+filename*/
231         path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
232         mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
233         strncpy(mount_name, fullpath, longest_prefix_length);
234         path[strlen(fullpath)-longest_prefix_length] = '\0';
235         mount_name[longest_prefix_length] = '\0';
236   }
237   else
238     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
239
240   return st->open(mount_name, path);
241 }
242
243 ActionPtr Workstation::close(surf_file_t fd) {
244   StoragePtr st = findStorageOnMountList(fd->mount);
245   XBT_DEBUG("CLOSE on disk '%s'",st->getName());
246   return st->close(fd);
247 }
248
249 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
250   StoragePtr st = findStorageOnMountList(fd->mount);
251   XBT_DEBUG("READ on disk '%s'",st->getName());
252   return st->read(fd, size);
253 }
254
255 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
256   StoragePtr st = findStorageOnMountList(fd->mount);
257   XBT_DEBUG("WRITE on disk '%s'",st->getName());
258   return st->write(fd, size);
259 }
260
261 int Workstation::unlink(surf_file_t fd) {
262   if (!fd){
263     XBT_WARN("No such file descriptor. Impossible to unlink");
264     return 0;
265   } else {
266 //    XBT_INFO("%s %zu", fd->storage, fd->size);
267     StoragePtr st = findStorageOnMountList(fd->mount);
268     /* Check if the file is on this storage */
269     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
270       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
271           st->getName());
272       return 0;
273     } else {
274       XBT_DEBUG("UNLINK on disk '%s'",st->getName());
275       st->m_usedSize -= fd->size;
276
277       // Remove the file from storage
278       xbt_dict_remove(st->p_content, fd->name);
279
280       free(fd->name);
281       free(fd->mount);
282       xbt_free(fd);
283       return 1;
284     }
285   }
286 }
287
288 ActionPtr Workstation::ls(const char* mount, const char *path){
289   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
290   StoragePtr st = findStorageOnMountList(mount);
291   return st->ls(path);
292 }
293
294 sg_size_t Workstation::getSize(surf_file_t fd){
295   return fd->size;
296 }
297
298 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
299 {
300   StoragePtr st = findStorageOnMountList(fd->mount);
301   sg_size_t *psize = xbt_new(sg_size_t, 1);
302   *psize = fd->size;
303   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
304   xbt_dynar_push_as(info, sg_size_t *, psize);
305   xbt_dynar_push_as(info, void *, fd->mount);
306   xbt_dynar_push_as(info, void *, (void *)st->getName());
307   xbt_dynar_push_as(info, void *, st->p_typeId);
308   xbt_dynar_push_as(info, void *, st->p_contentType);
309
310   return info;
311 }
312
313 sg_size_t Workstation::fileTell(surf_file_t fd){
314   return fd->current_position;
315 }
316
317 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
318
319   switch (origin) {
320   case SEEK_SET:
321     fd->current_position = 0;
322         return MSG_OK;
323   case SEEK_CUR:
324         if(offset > fd->size)
325           offset = fd->size;
326         fd->current_position = offset;
327         return MSG_OK;
328   case SEEK_END:
329         fd->current_position = fd->size;
330         return MSG_OK;
331   default:
332         return MSG_TASK_CANCELED;
333   }
334 }
335
336 sg_size_t Workstation::getFreeSize(const char* name)
337 {
338   StoragePtr st = findStorageOnMountList(name);
339   return st->m_size - st->m_usedSize;
340 }
341
342 sg_size_t Workstation::getUsedSize(const char* name)
343 {
344   StoragePtr st = findStorageOnMountList(name);
345   return st->m_usedSize;
346 }
347
348 xbt_dynar_t Workstation::getVms()
349 {
350   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
351
352   /* iterate for all virtual machines */
353   for (WorkstationVMModel::vm_list_t::iterator iter =
354          WorkstationVMModel::ws_vms.begin();
355        iter !=  WorkstationVMModel::ws_vms.end(); ++iter) {
356
357     WorkstationVMPtr ws_vm = &*iter;
358     if (this == ws_vm-> p_subWs)
359       xbt_dynar_push(dyn, &ws_vm->p_subWs);
360   }
361
362   return dyn;
363 }
364
365 void Workstation::getParams(ws_params_t params)
366 {
367   *params = p_params;
368 }
369
370 void Workstation::setParams(ws_params_t params)
371 {
372   /* may check something here. */
373   p_params = *params;
374 }
375
376 /**********
377  * Action *
378  **********/
379
380 void WorkstationAction::setState(e_surf_action_state_t state){
381   Action::setState(state);
382   surf_callback_emit(workstationActionStateChangedCallbacks, this);
383 }