Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 #include "network_interface.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
15                                 "Logging specific to the SURF workstation module");
16
17 WorkstationModelPtr surf_workstation_model = NULL;
18
19 /*************
20  * Callbacks *
21  *************/
22
23 surf_callback(void, WorkstationPtr) workstationCreatedCallbacks;
24 surf_callback(void, WorkstationPtr) workstationDestructedCallbacks;
25 surf_callback(void, WorkstationPtr) workstationStateChangedCallbacks;
26 surf_callback(void, WorkstationActionPtr) workstationActionStateChangedCallbacks;
27
28 /*********
29  * Model *
30  *********/
31 WorkstationModel::WorkstationModel(const char *name)
32  : Model(name)
33 {
34   p_cpuModel = surf_cpu_model_pm;
35 }
36
37 WorkstationModel::WorkstationModel()
38 : Model("Workstation") {
39   p_cpuModel = surf_cpu_model_pm;
40 }
41
42 WorkstationModel::~WorkstationModel() {
43 }
44
45 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
46  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
47  * active task, the dummy CPU action must be deactivated, so that the VM does
48  * not get any CPU share in the PM layer. */
49 void WorkstationModel::adjustWeightOfDummyCpuActions()
50 {
51   /* iterate for all virtual machines */
52   for (WorkstationVMModel::vm_list_t::iterator iter =
53          WorkstationVMModel::ws_vms.begin();
54        iter !=  WorkstationVMModel::ws_vms.end(); ++iter) {
55
56     WorkstationVMPtr ws_vm = &*iter;
57     CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ws_vm->p_cpu);
58     xbt_assert(cpu_cas01, "cpu-less workstation");
59
60     int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
61     // int is_active_old = constraint_is_active(cpu_cas01);
62
63     // {
64     //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
65     // }
66
67     if (is_active) {
68       /* some tasks exist on this VM */
69       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
70
71       /* FIXME: we shoud use lmm_update_variable_weight() ? */
72       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
73       ws_vm->p_action->setPriority(1);
74
75     } else {
76       /* no task exits on this VM */
77       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
78
79       ws_vm->p_action->setPriority(0);
80     }
81   }
82 }
83
84 /************
85  * Resource *
86  ************/
87 Workstation::Workstation()
88 {
89   surf_callback_emit(workstationCreatedCallbacks, this);
90 }
91
92 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
93                                  xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
94  : Resource(model, name, props)
95  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
96 {
97   p_params.ramsize = 0;
98   surf_callback_emit(workstationCreatedCallbacks, this);
99 }
100
101 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
102                                          xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
103  : Resource(model, name, props, constraint)
104  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
105 {
106   p_params.ramsize = 0;
107   surf_callback_emit(workstationCreatedCallbacks, this);
108 }
109
110 Workstation::~Workstation(){
111   surf_callback_emit(workstationDestructedCallbacks, this);
112 }
113
114 void Workstation::setState(e_surf_resource_state_t state){
115   Resource::setState(state);
116   surf_callback_emit(workstationStateChangedCallbacks, this);
117 }
118
119 int Workstation::getCore(){
120   return p_cpu->getCore();
121 }
122
123 double Workstation::getSpeed(double load){
124   return p_cpu->getSpeed(load);
125 }
126
127 double Workstation::getAvailableSpeed(){
128   return p_cpu->getAvailableSpeed();
129 }
130
131 double Workstation::getCurrentPowerPeak()
132 {
133   return p_cpu->getCurrentPowerPeak();
134 }
135
136 double Workstation::getPowerPeakAt(int pstate_index)
137 {
138   return p_cpu->getPowerPeakAt(pstate_index);
139 }
140
141 int Workstation::getNbPstates()
142 {
143   return p_cpu->getNbPstates();
144 }
145
146 void Workstation::setPowerPeakAt(int pstate_index)
147 {
148         p_cpu->setPowerPeakAt(pstate_index);
149 }
150
151 xbt_dict_t Workstation::getProperties()
152 {
153   return p_cpu->getProperties();
154 }
155
156 StoragePtr Workstation::findStorageOnMountList(const char* mount)
157 {
158   StoragePtr st = NULL;
159   s_mount_t mnt;
160   unsigned int cursor;
161
162   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
163   xbt_dynar_foreach(p_storage,cursor,mnt)
164   {
165     XBT_DEBUG("See '%s'",mnt.name);
166     if(!strcmp(mount,mnt.name)){
167       st = static_cast<StoragePtr>(mnt.storage);
168       break;
169     }
170   }
171   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
172   return st;
173 }
174
175 xbt_dict_t Workstation::getMountedStorageList()
176 {
177   s_mount_t mnt;
178   unsigned int i;
179   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
180   char *storage_name = NULL;
181
182   xbt_dynar_foreach(p_storage,i,mnt){
183     storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
184     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
185   }
186   return storage_list;
187 }
188
189 xbt_dynar_t Workstation::getAttachedStorageList()
190 {
191   xbt_lib_cursor_t cursor;
192   char *key;
193   void **data;
194   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
195   xbt_lib_foreach(storage_lib, cursor, key, data) {
196     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
197           StoragePtr storage = static_cast<StoragePtr>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
198           if(!strcmp((const char*)storage->p_attach,this->getName())){
199             xbt_dynar_push_as(result, void *,(void *)static_cast<ResourcePtr>(storage)->getName());
200           }
201         }
202   }
203   return result;
204 }
205
206 ActionPtr Workstation::open(const char* fullpath) {
207
208   StoragePtr st = NULL;
209   s_mount_t mnt;
210   unsigned int cursor;
211   size_t longest_prefix_length = 0;
212   char *path = NULL;
213   char *file_mount_name = NULL;
214   char *mount_name = NULL;
215
216   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
217   xbt_dynar_foreach(p_storage,cursor,mnt)
218   {
219     XBT_DEBUG("See '%s'",mnt.name);
220     file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
221     strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
222     file_mount_name[strlen(mnt.name)] = '\0';
223
224     if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
225     {/* The current mount name is found in the full path and is bigger than the previous*/
226       longest_prefix_length = strlen(mnt.name);
227       st = static_cast<StoragePtr>(mnt.storage);
228     }
229     free(file_mount_name);
230   }
231   if(longest_prefix_length>0)
232   { /* Mount point found, split fullpath into mount_name and path+filename*/
233         path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
234         mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
235         strncpy(mount_name, fullpath, longest_prefix_length+1);
236         strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
237         path[strlen(fullpath)-longest_prefix_length] = '\0';
238         mount_name[longest_prefix_length] = '\0';
239   }
240   else
241     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
242
243   ActionPtr action = st->open((const char*)mount_name, (const char*)path);
244   free((char*)path);
245   free((char*)mount_name);
246   return action;
247 }
248
249 ActionPtr Workstation::close(surf_file_t fd) {
250   StoragePtr st = findStorageOnMountList(fd->mount);
251   XBT_DEBUG("CLOSE on disk '%s'",st->getName());
252   return st->close(fd);
253 }
254
255 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
256   StoragePtr st = findStorageOnMountList(fd->mount);
257   XBT_DEBUG("READ on disk '%s'",st->getName());
258   return st->read(fd, size);
259 }
260
261 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
262   StoragePtr st = findStorageOnMountList(fd->mount);
263   XBT_DEBUG("WRITE on disk '%s'",st->getName());
264   return st->write(fd, size);
265 }
266
267 int Workstation::unlink(surf_file_t fd) {
268   if (!fd){
269     XBT_WARN("No such file descriptor. Impossible to unlink");
270     return 0;
271   } else {
272
273     StoragePtr st = findStorageOnMountList(fd->mount);
274     /* Check if the file is on this storage */
275     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
276       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
277           st->getName());
278       return 0;
279     } else {
280       XBT_DEBUG("UNLINK on disk '%s'",st->getName());
281       st->m_usedSize -= fd->size;
282
283       // Remove the file from storage
284       xbt_dict_remove(st->p_content, fd->name);
285
286       free(fd->name);
287       free(fd->mount);
288       xbt_free(fd);
289       return 1;
290     }
291   }
292 }
293
294 ActionPtr Workstation::ls(const char* mount, const char *path){
295   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
296   StoragePtr st = findStorageOnMountList(mount);
297   return st->ls(path);
298 }
299
300 sg_size_t Workstation::getSize(surf_file_t fd){
301   return fd->size;
302 }
303
304 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
305 {
306   StoragePtr st = findStorageOnMountList(fd->mount);
307   sg_size_t *psize = xbt_new(sg_size_t, 1);
308   *psize = fd->size;
309   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
310   xbt_dynar_push_as(info, sg_size_t *, psize);
311   xbt_dynar_push_as(info, void *, fd->mount);
312   xbt_dynar_push_as(info, void *, (void *)st->getName());
313   xbt_dynar_push_as(info, void *, st->p_typeId);
314   xbt_dynar_push_as(info, void *, st->p_contentType);
315
316   return info;
317 }
318
319 sg_size_t Workstation::fileTell(surf_file_t fd){
320   return fd->current_position;
321 }
322
323 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
324
325   switch (origin) {
326   case SEEK_SET:
327     fd->current_position = 0;
328         return MSG_OK;
329   case SEEK_CUR:
330         if(offset > fd->size)
331           offset = fd->size;
332         fd->current_position = offset;
333         return MSG_OK;
334   case SEEK_END:
335         fd->current_position = fd->size;
336         return MSG_OK;
337   default:
338         return MSG_TASK_CANCELED;
339   }
340 }
341
342 int Workstation::fileMove(surf_file_t fd, const char* fullpath){
343
344   /* Check if the new full path is on the same mount point */
345   if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount)))
346   {
347     sg_size_t *psize, *new_psize;
348     psize = (sg_size_t*) xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,fd->name);
349     new_psize = xbt_new(sg_size_t, 1);
350     *new_psize = *psize;
351     if (psize){// src file exists
352           xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
353
354           char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));;
355           strncpy(path, fullpath+strlen(fd->mount), strlen(fullpath)-strlen(fd->mount)+1);
356           xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path, new_psize,NULL);
357           XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
358           free(path);
359           return MSG_OK;
360     }
361     else
362           XBT_WARN("File %s doesn't exist", fd->name);
363       return MSG_TASK_CANCELED;
364     }
365   else
366   {
367         XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount);
368         return MSG_TASK_CANCELED;
369   }
370 }
371
372 int Workstation::fileRcopy(surf_file_t fd, surf_resource_t host_dest, const char* fullpath){
373
374   XBT_DEBUG("Rcopy file %s on %s to %s",fd->name, host_dest->key, fullpath);
375
376   /* Find the host src where the file is located */
377   StoragePtr storage = findStorageOnMountList(fd->mount);
378   const char* host_name_src = (const char*)storage->p_attach;
379
380   /* Find the real host dest where the file will be stored */
381   s_mount_t mnt;
382   unsigned int cursor;
383   StoragePtr storage_dest = NULL;
384   const char* host_name_dest;
385   char *file_mount_name;
386   size_t longest_prefix_length = 0;
387   WorkstationPtr dest_ws, src_ws;
388
389   dest_ws = static_cast<WorkstationPtr>(surf_workstation_resource_priv(host_dest));
390
391   xbt_dynar_foreach(dest_ws->p_storage,cursor,mnt)
392   {
393         file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
394     strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
395     file_mount_name[strlen(mnt.name)] = '\0';
396
397         if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
398         {/* The current mount name is found in the full path and is bigger than the previous*/
399       longest_prefix_length = strlen(mnt.name);
400       storage_dest = static_cast<StoragePtr>(mnt.storage);
401         }
402         free(file_mount_name);
403   }
404   if(longest_prefix_length>0)
405   { /* Mount point found, retrieve the host the storage is attached to */
406     host_name_dest = storage_dest->p_attach;
407   }
408   else
409   {
410     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host_dest->key);
411     return MSG_TASK_CANCELED;
412   }
413
414   /* Check that there is a route between src and dest workstations */
415   xbt_dynar_t route = NULL;
416   dest_ws = static_cast<WorkstationPtr>(surf_workstation_resource_priv(xbt_lib_get_elm_or_null(host_lib, host_name_dest)));
417   src_ws = static_cast<WorkstationPtr>(surf_workstation_resource_priv(xbt_lib_get_elm_or_null(host_lib, host_name_src)));
418
419   routing_get_route_and_latency(src_ws->p_netElm, dest_ws->p_netElm, &route, NULL);
420   if(!xbt_dynar_length (route))
421   {
422         XBT_WARN("There is no route between %s and %s. Action has been canceled", src_ws->getName(), dest_ws->getName());
423         return MSG_TASK_CANCELED;
424   }
425   else
426   {/* There is a route between src and dest, let's copy the file */
427
428     /* Read the file on the src side */
429         src_ws->read(fd, fd->size);
430
431         /* Send a message from src to dest to simulate data transfer */
432         surf_network_model->communicate(src_ws->p_netElm, dest_ws->p_netElm, fd->size, -1.0);
433
434         /* Create the file on the dest side and write data into it*/
435         char *mount_name, *path;
436         path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
437         mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
438         /* deduce mount_name and path from fullpath */
439         strncpy(mount_name, fullpath, longest_prefix_length+1);
440         strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
441         path[strlen(fullpath)-longest_prefix_length] = '\0';
442         mount_name[longest_prefix_length] = '\0';
443     /* create the file */
444         ActionPtr open_action = storage_dest->open((const char*)mount_name, (const char*)path);
445     /* write data */
446         dest_ws->write(static_cast<StorageActionPtr>(open_action)->p_file, fd->size);
447     dest_ws->close(static_cast<StorageActionPtr>(open_action)->p_file);
448     free(path);
449     free(mount_name);
450     XBT_DEBUG("File %s has been copied on %s to %s",fd->name, host_dest->key, fullpath);
451     return MSG_OK;
452   }
453
454
455   XBT_INFO("SRC %s DEST %s", host_name_src, host_name_dest);
456
457
458
459 //  /* Check that file to copy is local to the src workstation (storage is attached to src workstation) */
460 //  StoragePtr storage = findStorageOnMountList(fd->mount);
461 //  if(!strcmp((const char*)storage->p_attach, this->getName()))
462 //  {
463 //    /* Check that there is a route between src and dest workstations */
464 //    xbt_dynar_t route = NULL;
465 //    routing_get_route_and_latency(this->p_netElm, ((WorkstationPtr)host_dest)->p_netElm, &route, NULL);
466 //    if(route){
467 //
468 //      ATTENTION DISCUSSION AVEC FRED !
469 //      return MSG_OK;
470 //    }
471 //    else
472 //    {
473 //      XBT_WARN("There is no route between %s and %s. Action has been canceled", this->getName(), host_dest->key);
474 //      return MSG_TASK_CANCELED;
475 //    }
476 //  }
477 //  else
478 //  {
479 //    XBT_WARN("File %s is not local to %s but to %s. Action has been canceled", fd->name,this->getName(), storage->p_attach);
480 //    return MSG_TASK_CANCELED;
481 //  }
482 }
483
484 sg_size_t Workstation::getFreeSize(const char* name)
485 {
486   StoragePtr st = findStorageOnMountList(name);
487   return st->m_size - st->m_usedSize;
488 }
489
490 sg_size_t Workstation::getUsedSize(const char* name)
491 {
492   StoragePtr st = findStorageOnMountList(name);
493   return st->m_usedSize;
494 }
495
496 xbt_dynar_t Workstation::getVms()
497 {
498   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
499
500   /* iterate for all virtual machines */
501   for (WorkstationVMModel::vm_list_t::iterator iter =
502          WorkstationVMModel::ws_vms.begin();
503        iter !=  WorkstationVMModel::ws_vms.end(); ++iter) {
504
505     WorkstationVMPtr ws_vm = &*iter;
506     if (this == ws_vm-> p_subWs)
507       xbt_dynar_push(dyn, &ws_vm->p_subWs);
508   }
509
510   return dyn;
511 }
512
513 void Workstation::getParams(ws_params_t params)
514 {
515   *params = p_params;
516 }
517
518 void Workstation::setParams(ws_params_t params)
519 {
520   /* may check something here. */
521   p_params = *params;
522 }
523
524 /**********
525  * Action *
526  **********/
527
528 void WorkstationAction::setState(e_surf_action_state_t state){
529   Action::setState(state);
530   surf_callback_emit(workstationActionStateChangedCallbacks, this);
531 }