Logo AND Algorithmique Numérique Distribuée

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