Logo AND Algorithmique Numérique Distribuée

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