Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Memleaks fixes on VM
[simgrid.git] / src / surf / workstation_interface.cpp
1 #include "workstation_interface.hpp"
2 #include "vm_workstation_interface.hpp"
3 #include "cpu_cas01.hpp"
4 #include "simgrid/sg_config.h"
5
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
7                                 "Logging specific to the SURF workstation module");
8
9 WorkstationModelPtr surf_workstation_model = NULL;
10
11 /*********
12  * Model *
13  *********/
14 WorkstationModel::WorkstationModel(const char *name)
15  : Model(name)
16 {
17   p_cpuModel = surf_cpu_model_pm;
18 }
19
20 WorkstationModel::WorkstationModel()
21 : Model("Workstation") {
22   p_cpuModel = surf_cpu_model_pm;
23 }
24
25 WorkstationModel::~WorkstationModel() {
26 }
27
28
29
30 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
31  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
32  * active task, the dummy CPU action must be deactivated, so that the VM does
33  * not get any CPU share in the PM layer. */
34 void WorkstationModel::adjustWeightOfDummyCpuActions()
35 {
36   /* iterate for all hosts including virtual machines */
37   xbt_lib_cursor_t cursor;
38   char *key;
39   void **ind_host;
40
41   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
42     WorkstationPtr ws = static_cast<WorkstationPtr>(ind_host[SURF_WKS_LEVEL]);
43     CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ind_host[SURF_CPU_LEVEL]);
44
45     if (!ws)
46       continue;
47     /* skip if it is not a virtual machine */
48     if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
49       continue;
50     xbt_assert(cpu_cas01, "cpu-less workstation");
51
52     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
53     WorkstationVMPtr ws_vm = static_cast<WorkstationVMPtr>(ws);
54
55     int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
56     // int is_active_old = constraint_is_active(cpu_cas01);
57
58     // {
59     //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
60     // }
61
62     if (is_active) {
63       /* some tasks exist on this VM */
64       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
65
66       /* FIXME: we shoud use lmm_update_variable_weight() ? */
67       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
68       ws_vm->p_action->setPriority(1);
69
70     } else {
71       /* no task exits on this VM */
72       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
73
74       ws_vm->p_action->setPriority(0);
75     }
76   }
77 }
78
79 /************
80  * Resource *
81  ************/
82 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
83                                  xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
84  : Resource(model, name, props)
85  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
86 {}
87
88 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
89                                          xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
90  : Resource(model, name, props, constraint)
91  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
92 {}
93
94 int Workstation::getCore(){
95   return p_cpu->getCore();
96 }
97
98 double Workstation::getSpeed(double load){
99   return p_cpu->getSpeed(load);
100 }
101
102 double Workstation::getAvailableSpeed(){
103   return p_cpu->getAvailableSpeed();
104 }
105
106 double Workstation::getCurrentPowerPeak()
107 {
108   return p_cpu->getCurrentPowerPeak();
109 }
110
111 double Workstation::getPowerPeakAt(int pstate_index)
112 {
113   return p_cpu->getPowerPeakAt(pstate_index);
114 }
115
116 int Workstation::getNbPstates()
117 {
118   return p_cpu->getNbPstates();
119 }
120
121 void Workstation::setPowerPeakAt(int pstate_index)
122 {
123         p_cpu->setPowerPeakAt(pstate_index);
124 }
125
126 double Workstation::getConsumedEnergy()
127 {
128   return p_cpu->getConsumedEnergy();
129 }
130
131 xbt_dict_t Workstation::getProperties()
132 {
133   return p_cpu->getProperties();
134 }
135
136
137 StoragePtr Workstation::findStorageOnMountList(const char* mount)
138 {
139   StoragePtr st = NULL;
140   s_mount_t mnt;
141   unsigned int cursor;
142
143   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
144   xbt_dynar_foreach(p_storage,cursor,mnt)
145   {
146     XBT_DEBUG("See '%s'",mnt.name);
147     if(!strcmp(mount,mnt.name)){
148       st = static_cast<StoragePtr>(mnt.storage);
149       break;
150     }
151   }
152   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
153   return st;
154 }
155
156 xbt_dict_t Workstation::getStorageList()
157 {
158   s_mount_t mnt;
159   unsigned int i;
160   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
161   char *storage_name = NULL;
162
163   xbt_dynar_foreach(p_storage,i,mnt){
164     storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
165     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
166   }
167   return storage_list;
168 }
169
170 ActionPtr Workstation::open(const char* mount, const char* path) {
171   StoragePtr st = findStorageOnMountList(mount);
172   XBT_DEBUG("OPEN on disk '%s'", st->getName());
173   return st->open(mount, path);
174 }
175
176 ActionPtr Workstation::close(surf_file_t fd) {
177   StoragePtr st = findStorageOnMountList(fd->mount);
178   XBT_DEBUG("CLOSE on disk '%s'",st->getName());
179   return st->close(fd);
180 }
181
182 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
183   StoragePtr st = findStorageOnMountList(fd->mount);
184   XBT_DEBUG("READ on disk '%s'",st->getName());
185   return st->read(fd, size);
186 }
187
188 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
189   StoragePtr st = findStorageOnMountList(fd->mount);
190   XBT_DEBUG("WRITE on disk '%s'",st->getName());
191   return st->write(fd, size);
192 }
193
194 int Workstation::unlink(surf_file_t fd) {
195   if (!fd){
196     XBT_WARN("No such file descriptor. Impossible to unlink");
197     return 0;
198   } else {
199 //    XBT_INFO("%s %zu", fd->storage, fd->size);
200     StoragePtr st = findStorageOnMountList(fd->mount);
201     /* Check if the file is on this storage */
202     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
203       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
204           st->getName());
205       return 0;
206     } else {
207       XBT_DEBUG("UNLINK on disk '%s'",st->getName());
208       st->m_usedSize -= fd->size;
209
210       // Remove the file from storage
211       xbt_dict_remove(st->p_content, fd->name);
212
213       free(fd->name);
214       free(fd->mount);
215       xbt_free(fd);
216       return 1;
217     }
218   }
219 }
220
221 ActionPtr Workstation::ls(const char* mount, const char *path){
222   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
223   StoragePtr st = findStorageOnMountList(mount);
224   return st->ls(path);
225 }
226
227 sg_size_t Workstation::getSize(surf_file_t fd){
228   return fd->size;
229 }
230
231 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
232 {
233   StoragePtr st = findStorageOnMountList(fd->mount);
234   sg_size_t *psize = xbt_new(sg_size_t, 1);
235   *psize = fd->size;
236   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
237   xbt_dynar_push_as(info, sg_size_t *, psize);
238   xbt_dynar_push_as(info, void *, fd->mount);
239   xbt_dynar_push_as(info, void *, (void *)st->getName());
240   xbt_dynar_push_as(info, void *, st->p_typeId);
241   xbt_dynar_push_as(info, void *, st->p_contentType);
242
243   return info;
244 }
245
246 sg_size_t Workstation::fileTell(surf_file_t fd){
247   return fd->current_position;
248 }
249
250 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
251
252   switch (origin) {
253   case SEEK_SET:
254     fd->current_position = 0;
255         return MSG_OK;
256   case SEEK_CUR:
257         if(offset > fd->size)
258           offset = fd->size;
259         fd->current_position = offset;
260         return MSG_OK;
261   case SEEK_END:
262         fd->current_position = fd->size;
263         return MSG_OK;
264   default:
265         return MSG_TASK_CANCELED;
266   }
267 }
268
269 sg_size_t Workstation::getFreeSize(const char* name)
270 {
271   StoragePtr st = findStorageOnMountList(name);
272   return st->m_size - st->m_usedSize;
273 }
274
275 sg_size_t Workstation::getUsedSize(const char* name)
276 {
277   StoragePtr st = findStorageOnMountList(name);
278   return st->m_usedSize;
279 }
280
281 xbt_dynar_t Workstation::getVms()
282 {
283   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
284
285   /* iterate for all hosts including virtual machines */
286   xbt_lib_cursor_t cursor;
287   char *key;
288   void **ind_host;
289   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
290     WorkstationPtr ws = static_cast<WorkstationPtr>(ind_host[SURF_WKS_LEVEL]);
291     if (!ws)
292       continue;
293     /* skip if it is not a virtual machine */
294     if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
295       continue;
296
297     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
298     WorkstationVMPtr ws_vm = static_cast<WorkstationVMPtr>(ws);
299     if (this == ws_vm-> p_subWs)
300       xbt_dynar_push(dyn, &ws_vm->p_subWs);
301   }
302
303   return dyn;
304 }
305
306 void Workstation::getParams(ws_params_t params)
307 {
308   memcpy(params, &p_params, sizeof(s_ws_params_t));
309 }
310
311 void Workstation::setParams(ws_params_t params)
312 {
313   /* may check something here. */
314   memcpy(&p_params, params, sizeof(s_ws_params_t));
315 }
316
317 /**********
318  * Action *
319  **********/