Logo AND Algorithmique Numérique Distribuée

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