Logo AND Algorithmique Numérique Distribuée

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