Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
85dc8b73fc9ea2b4d04d9f8407c3b58383f01a41
[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 void Host::setState(e_surf_resource_state_t state){
140   e_surf_resource_state_t old = Resource::getState();
141   Resource::setState(state);
142   onStateChange(this, old, state);
143   p_cpu->setState(state);
144 }
145
146 simgrid::surf::Storage *Host::findStorageOnMountList(const char* mount)
147 {
148   simgrid::surf::Storage *st = NULL;
149   s_mount_t mnt;
150   unsigned int cursor;
151
152   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
153   xbt_dynar_foreach(p_storage,cursor,mnt)
154   {
155     XBT_DEBUG("See '%s'",mnt.name);
156     if(!strcmp(mount,mnt.name)){
157       st = static_cast<simgrid::surf::Storage*>(mnt.storage);
158       break;
159     }
160   }
161   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
162   return st;
163 }
164
165 xbt_dict_t Host::getMountedStorageList()
166 {
167   s_mount_t mnt;
168   unsigned int i;
169   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
170   char *storage_name = NULL;
171
172   xbt_dynar_foreach(p_storage,i,mnt){
173     storage_name = (char *)static_cast<simgrid::surf::Storage*>(mnt.storage)->getName();
174     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
175   }
176   return storage_list;
177 }
178
179 xbt_dynar_t Host::getAttachedStorageList()
180 {
181   xbt_lib_cursor_t cursor;
182   char *key;
183   void **data;
184   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
185   xbt_lib_foreach(storage_lib, cursor, key, data) {
186     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
187           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));
188           if(!strcmp((const char*)storage->p_attach,this->getName())){
189             xbt_dynar_push_as(result, void *, (void*)storage->getName());
190           }
191         }
192   }
193   return result;
194 }
195
196 Action *Host::open(const char* fullpath) {
197
198   simgrid::surf::Storage *st = NULL;
199   s_mount_t mnt;
200   unsigned int cursor;
201   size_t longest_prefix_length = 0;
202   char *path = NULL;
203   char *file_mount_name = NULL;
204   char *mount_name = NULL;
205
206   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
207   xbt_dynar_foreach(p_storage,cursor,mnt)
208   {
209     XBT_DEBUG("See '%s'",mnt.name);
210     file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
211     strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
212     file_mount_name[strlen(mnt.name)] = '\0';
213
214     if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
215     {/* The current mount name is found in the full path and is bigger than the previous*/
216       longest_prefix_length = strlen(mnt.name);
217       st = static_cast<simgrid::surf::Storage*>(mnt.storage);
218     }
219     free(file_mount_name);
220   }
221   if(longest_prefix_length>0)
222   { /* Mount point found, split fullpath into mount_name and path+filename*/
223         path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
224         mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
225         strncpy(mount_name, fullpath, longest_prefix_length+1);
226         strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
227         path[strlen(fullpath)-longest_prefix_length] = '\0';
228         mount_name[longest_prefix_length] = '\0';
229   }
230   else
231     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
232
233   XBT_DEBUG("OPEN %s on disk '%s'",path, st->getName());
234   Action *action = st->open((const char*)mount_name, (const char*)path);
235   free((char*)path);
236   free((char*)mount_name);
237   return action;
238 }
239
240 Action *Host::close(surf_file_t fd) {
241   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
242   XBT_DEBUG("CLOSE %s on disk '%s'",fd->name, st->getName());
243   return st->close(fd);
244 }
245
246 Action *Host::read(surf_file_t fd, sg_size_t size) {
247   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
248   XBT_DEBUG("READ %s on disk '%s'",fd->name, st->getName());
249   return st->read(fd, size);
250 }
251
252 Action *Host::write(surf_file_t fd, sg_size_t size) {
253   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
254   XBT_DEBUG("WRITE %s on disk '%s'",fd->name, st->getName());
255   return st->write(fd, size);
256 }
257
258 int Host::unlink(surf_file_t fd) {
259   if (!fd){
260     XBT_WARN("No such file descriptor. Impossible to unlink");
261     return -1;
262   } else {
263
264     simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
265     /* Check if the file is on this storage */
266     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
267       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
268           st->getName());
269       return -1;
270     } else {
271       XBT_DEBUG("UNLINK %s on disk '%s'",fd->name, st->getName());
272       st->m_usedSize -= fd->size;
273
274       // Remove the file from storage
275       xbt_dict_remove(st->p_content, fd->name);
276
277       xbt_free(fd->name);
278       xbt_free(fd->mount);
279       xbt_free(fd);
280       return 0;
281     }
282   }
283 }
284
285 sg_size_t Host::getSize(surf_file_t fd){
286   return fd->size;
287 }
288
289 xbt_dynar_t Host::getInfo( surf_file_t fd)
290 {
291   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
292   sg_size_t *psize = xbt_new(sg_size_t, 1);
293   *psize = fd->size;
294   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
295   xbt_dynar_push_as(info, sg_size_t *, psize);
296   xbt_dynar_push_as(info, void *, fd->mount);
297   xbt_dynar_push_as(info, void *, (void *)st->getName());
298   xbt_dynar_push_as(info, void *, st->p_typeId);
299   xbt_dynar_push_as(info, void *, st->p_contentType);
300
301   return info;
302 }
303
304 sg_size_t Host::fileTell(surf_file_t fd){
305   return fd->current_position;
306 }
307
308 int Host::fileSeek(surf_file_t fd, sg_offset_t offset, int origin){
309
310   switch (origin) {
311   case SEEK_SET:
312     fd->current_position = offset;
313     return 0;
314   case SEEK_CUR:
315     fd->current_position += offset;
316     return 0;
317   case SEEK_END:
318     fd->current_position = fd->size + offset;
319     return 0;
320   default:
321     return -1;
322   }
323 }
324
325 int Host::fileMove(surf_file_t fd, const char* fullpath){
326   /* Check if the new full path is on the same mount point */
327   if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
328     sg_size_t *psize, *new_psize;
329     psize = (sg_size_t*)
330         xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,
331                              fd->name);
332     new_psize = xbt_new(sg_size_t, 1);
333     *new_psize = *psize;
334     if (psize){// src file exists
335       xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
336       char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));
337       strncpy(path, fullpath+strlen(fd->mount),
338               strlen(fullpath)-strlen(fd->mount)+1);
339       xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path,
340                    new_psize,NULL);
341       XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
342       free(path);
343       return 0;
344     } else {
345       XBT_WARN("File %s doesn't exist", fd->name);
346       return -1;
347     }
348   } else {
349     XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.",
350              fullpath, fd->mount);
351     return -1;
352   }
353 }
354
355 xbt_dynar_t Host::getVms()
356 {
357   xbt_dynar_t dyn = xbt_dynar_new(sizeof(simgrid::surf::VirtualMachine*), NULL);
358
359   /* iterate for all virtual machines */
360   for (simgrid::surf::VMModel::vm_list_t::iterator iter =
361          simgrid::surf::VMModel::ws_vms.begin();
362        iter !=  simgrid::surf::VMModel::ws_vms.end(); ++iter) {
363
364     simgrid::surf::VirtualMachine *ws_vm = &*iter;
365     if (this == ws_vm->p_subWs)
366       xbt_dynar_push(dyn, &ws_vm);
367   }
368
369   return dyn;
370 }
371
372 void Host::getParams(vm_params_t params)
373 {
374   *params = p_params;
375 }
376
377 void Host::setParams(vm_params_t params)
378 {
379   /* may check something here. */
380   p_params = *params;
381 }
382
383 /**********
384  * Action *
385  **********/
386 simgrid::surf::signal<void(simgrid::surf::HostAction*, e_surf_action_state_t, e_surf_action_state_t)> HostAction::onStateChange;
387
388 void HostAction::setState(e_surf_action_state_t state){
389   e_surf_action_state_t old = getState();
390   Action::setState(state);
391   onStateChange(this, old, state);
392 }
393
394 }
395 }