Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change StateChanged callback signatures
[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, e_surf_resource_state_t, e_surf_resource_state_t) workstationStateChangedCallbacks;
26 surf_callback(void, WorkstationActionPtr, e_surf_action_state_t, e_surf_action_state_t) 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   e_surf_resource_state_t old = Resource::getState();
116   Resource::setState(state);
117   surf_callback_emit(workstationStateChangedCallbacks, this, old, state);
118 }
119
120 int Workstation::getCore(){
121   return p_cpu->getCore();
122 }
123
124 double Workstation::getSpeed(double load){
125   return p_cpu->getSpeed(load);
126 }
127
128 double Workstation::getAvailableSpeed(){
129   return p_cpu->getAvailableSpeed();
130 }
131
132 double Workstation::getCurrentPowerPeak()
133 {
134   return p_cpu->getCurrentPowerPeak();
135 }
136
137 double Workstation::getPowerPeakAt(int pstate_index)
138 {
139   return p_cpu->getPowerPeakAt(pstate_index);
140 }
141
142 int Workstation::getNbPstates()
143 {
144   return p_cpu->getNbPstates();
145 }
146
147 void Workstation::setPowerPeakAt(int pstate_index)
148 {
149         p_cpu->setPowerPeakAt(pstate_index);
150 }
151
152 xbt_dict_t Workstation::getProperties()
153 {
154   return p_cpu->getProperties();
155 }
156
157 StoragePtr Workstation::findStorageOnMountList(const char* mount)
158 {
159   StoragePtr st = NULL;
160   s_mount_t mnt;
161   unsigned int cursor;
162
163   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
164   xbt_dynar_foreach(p_storage,cursor,mnt)
165   {
166     XBT_DEBUG("See '%s'",mnt.name);
167     if(!strcmp(mount,mnt.name)){
168       st = static_cast<StoragePtr>(mnt.storage);
169       break;
170     }
171   }
172   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
173   return st;
174 }
175
176 xbt_dict_t Workstation::getMountedStorageList()
177 {
178   s_mount_t mnt;
179   unsigned int i;
180   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
181   char *storage_name = NULL;
182
183   xbt_dynar_foreach(p_storage,i,mnt){
184     storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
185     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
186   }
187   return storage_list;
188 }
189
190 xbt_dynar_t Workstation::getAttachedStorageList()
191 {
192   xbt_lib_cursor_t cursor;
193   char *key;
194   void **data;
195   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
196   xbt_lib_foreach(storage_lib, cursor, key, data) {
197     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
198           StoragePtr storage = static_cast<StoragePtr>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
199           if(!strcmp((const char*)storage->p_attach,this->getName())){
200             xbt_dynar_push_as(result, void *,(void *)static_cast<ResourcePtr>(storage)->getName());
201           }
202         }
203   }
204   return result;
205 }
206
207 ActionPtr Workstation::open(const char* fullpath) {
208
209   StoragePtr st = NULL;
210   s_mount_t mnt;
211   unsigned int cursor;
212   size_t longest_prefix_length = 0;
213   char *path = NULL;
214   char *file_mount_name = NULL;
215   char *mount_name = NULL;
216
217   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
218   xbt_dynar_foreach(p_storage,cursor,mnt)
219   {
220     XBT_DEBUG("See '%s'",mnt.name);
221     file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
222     strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
223     file_mount_name[strlen(mnt.name)] = '\0';
224
225     if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
226     {/* The current mount name is found in the full path and is bigger than the previous*/
227       longest_prefix_length = strlen(mnt.name);
228       st = static_cast<StoragePtr>(mnt.storage);
229     }
230     free(file_mount_name);
231   }
232   if(longest_prefix_length>0)
233   { /* Mount point found, split fullpath into mount_name and path+filename*/
234         path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
235         mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
236         strncpy(mount_name, fullpath, longest_prefix_length+1);
237         strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
238         path[strlen(fullpath)-longest_prefix_length] = '\0';
239         mount_name[longest_prefix_length] = '\0';
240   }
241   else
242     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
243
244   ActionPtr action = st->open((const char*)mount_name, (const char*)path);
245   free((char*)path);
246   free((char*)mount_name);
247   return action;
248 }
249
250 ActionPtr Workstation::close(surf_file_t fd) {
251   StoragePtr st = findStorageOnMountList(fd->mount);
252   XBT_DEBUG("CLOSE on disk '%s'",st->getName());
253   return st->close(fd);
254 }
255
256 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
257   StoragePtr st = findStorageOnMountList(fd->mount);
258   XBT_DEBUG("READ on disk '%s'",st->getName());
259   return st->read(fd, size);
260 }
261
262 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
263   StoragePtr st = findStorageOnMountList(fd->mount);
264   XBT_DEBUG("WRITE on disk '%s'",st->getName());
265   return st->write(fd, size);
266 }
267
268 int Workstation::unlink(surf_file_t fd) {
269   if (!fd){
270     XBT_WARN("No such file descriptor. Impossible to unlink");
271     return 0;
272   } else {
273
274     StoragePtr st = findStorageOnMountList(fd->mount);
275     /* Check if the file is on this storage */
276     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
277       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
278           st->getName());
279       return 0;
280     } else {
281       XBT_DEBUG("UNLINK on disk '%s'",st->getName());
282       st->m_usedSize -= fd->size;
283
284       // Remove the file from storage
285       xbt_dict_remove(st->p_content, fd->name);
286
287       free(fd->name);
288       free(fd->mount);
289       xbt_free(fd);
290       return 1;
291     }
292   }
293 }
294
295 ActionPtr Workstation::ls(const char* mount, const char *path){
296   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
297   StoragePtr st = findStorageOnMountList(mount);
298   return st->ls(path);
299 }
300
301 sg_size_t Workstation::getSize(surf_file_t fd){
302   return fd->size;
303 }
304
305 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
306 {
307   StoragePtr st = findStorageOnMountList(fd->mount);
308   sg_size_t *psize = xbt_new(sg_size_t, 1);
309   *psize = fd->size;
310   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
311   xbt_dynar_push_as(info, sg_size_t *, psize);
312   xbt_dynar_push_as(info, void *, fd->mount);
313   xbt_dynar_push_as(info, void *, (void *)st->getName());
314   xbt_dynar_push_as(info, void *, st->p_typeId);
315   xbt_dynar_push_as(info, void *, st->p_contentType);
316
317   return info;
318 }
319
320 sg_size_t Workstation::fileTell(surf_file_t fd){
321   return fd->current_position;
322 }
323
324 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
325
326   switch (origin) {
327   case SEEK_SET:
328     fd->current_position = 0;
329         return MSG_OK;
330   case SEEK_CUR:
331         if(offset > fd->size)
332           offset = fd->size;
333         fd->current_position = offset;
334         return MSG_OK;
335   case SEEK_END:
336         fd->current_position = fd->size;
337         return MSG_OK;
338   default:
339         return MSG_TASK_CANCELED;
340   }
341 }
342
343 int Workstation::fileMove(surf_file_t fd, const char* fullpath){
344
345   /* Check if the new full path is on the same mount point */
346   if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount)))
347   {
348     sg_size_t *psize, *new_psize;
349     psize = (sg_size_t*) xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,fd->name);
350     new_psize = xbt_new(sg_size_t, 1);
351     *new_psize = *psize;
352     if (psize){// src file exists
353           xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
354
355           char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));;
356           strncpy(path, fullpath+strlen(fd->mount), strlen(fullpath)-strlen(fd->mount)+1);
357           xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path, new_psize,NULL);
358           XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
359           free(path);
360           return MSG_OK;
361     }
362     else
363           XBT_WARN("File %s doesn't exist", fd->name);
364       return MSG_TASK_CANCELED;
365     }
366   else
367   {
368         XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount);
369         return MSG_TASK_CANCELED;
370   }
371 }
372
373 int Workstation::fileRcopy(surf_file_t fd, surf_resource_t host_dest, const char* fullpath){
374
375   XBT_DEBUG("Rcopy file %s on %s to %s",fd->name, host_dest->key, fullpath);
376
377   /* Find the host src where the file is located */
378   StoragePtr storage = findStorageOnMountList(fd->mount);
379   const char* host_name_src = (const char*)storage->p_attach;
380
381   /* Find the real host dest where the file will be stored */
382   s_mount_t mnt;
383   unsigned int cursor;
384   StoragePtr storage_dest = NULL;
385   const char* host_name_dest;
386   char *file_mount_name;
387   size_t longest_prefix_length = 0;
388   WorkstationPtr dest_ws, src_ws;
389
390   dest_ws = static_cast<WorkstationPtr>(surf_workstation_resource_priv(host_dest));
391
392   xbt_dynar_foreach(dest_ws->p_storage,cursor,mnt)
393   {
394         file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
395     strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
396     file_mount_name[strlen(mnt.name)] = '\0';
397
398         if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
399         {/* The current mount name is found in the full path and is bigger than the previous*/
400       longest_prefix_length = strlen(mnt.name);
401       storage_dest = static_cast<StoragePtr>(mnt.storage);
402         }
403         free(file_mount_name);
404   }
405   if(longest_prefix_length>0)
406   { /* Mount point found, retrieve the host the storage is attached to */
407     host_name_dest = storage_dest->p_attach;
408   }
409   else
410   {
411     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host_dest->key);
412     return MSG_TASK_CANCELED;
413   }
414
415   /* Check that there is a route between src and dest workstations */
416   xbt_dynar_t route = NULL;
417   dest_ws = static_cast<WorkstationPtr>(surf_workstation_resource_priv(xbt_lib_get_elm_or_null(host_lib, host_name_dest)));
418   src_ws = static_cast<WorkstationPtr>(surf_workstation_resource_priv(xbt_lib_get_elm_or_null(host_lib, host_name_src)));
419
420   routing_get_route_and_latency(src_ws->p_netElm, dest_ws->p_netElm, &route, NULL);
421   if(!xbt_dynar_length (route))
422   {
423         XBT_WARN("There is no route between %s and %s. Action has been canceled", src_ws->getName(), dest_ws->getName());
424         return MSG_TASK_CANCELED;
425   }
426   else
427   {/* There is a route between src and dest, let's copy the file */
428
429     /* Read the file on the src side */
430         src_ws->read(fd, fd->size);
431
432         /* Send a message from src to dest to simulate data transfer */
433         surf_network_model->communicate(src_ws->p_netElm, dest_ws->p_netElm, fd->size, .0);
434
435         /* Create the file on the dest side and write data into it*/
436         char *mount_name, *path;
437         path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
438         mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
439
440         /* deduce mount_name and path from fullpath */
441         strncpy(mount_name, fullpath, longest_prefix_length+1);
442         strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
443         path[strlen(fullpath)-longest_prefix_length] = '\0';
444         mount_name[longest_prefix_length] = '\0';
445
446     /* create the file */
447         StorageActionPtr open_action = storage_dest->open((const char*)mount_name, (const char*)path);
448
449         surf_file_t surf_file = xbt_new(s_surf_file_t, 1);
450         surf_file->current_position = 0;
451         surf_file->mount = mount_name;
452         surf_file->name = strdup(path);
453         surf_file->size = 0;
454
455         /* write data and close file*/
456         storage_dest->write(surf_file, fd->size);
457         storage_dest->close(open_action->p_file);
458
459         free(path);
460     free(mount_name);
461     XBT_DEBUG("File %s has been copied on %s to %s",fd->name, host_dest->key, fullpath);
462     return MSG_OK;
463   }
464 }
465
466 sg_size_t Workstation::getFreeSize(const char* name)
467 {
468   StoragePtr st = findStorageOnMountList(name);
469   return st->m_size - st->m_usedSize;
470 }
471
472 sg_size_t Workstation::getUsedSize(const char* name)
473 {
474   StoragePtr st = findStorageOnMountList(name);
475   return st->m_usedSize;
476 }
477
478 xbt_dynar_t Workstation::getVms()
479 {
480   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
481
482   /* iterate for all virtual machines */
483   for (WorkstationVMModel::vm_list_t::iterator iter =
484          WorkstationVMModel::ws_vms.begin();
485        iter !=  WorkstationVMModel::ws_vms.end(); ++iter) {
486
487     WorkstationVMPtr ws_vm = &*iter;
488     if (this == ws_vm-> p_subWs)
489       xbt_dynar_push(dyn, &ws_vm->p_subWs);
490   }
491
492   return dyn;
493 }
494
495 void Workstation::getParams(ws_params_t params)
496 {
497   *params = p_params;
498 }
499
500 void Workstation::setParams(ws_params_t params)
501 {
502   /* may check something here. */
503   p_params = *params;
504 }
505
506 /**********
507  * Action *
508  **********/
509
510 void WorkstationAction::setState(e_surf_action_state_t state){
511   e_surf_action_state_t old = getState();
512   Action::setState(state);
513   surf_callback_emit(workstationActionStateChangedCallbacks, this, old, state);
514 }