Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sanitize the network_model->communicate() API
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/s4u/host.hpp>
7
8 #include "src/simix/smx_private.h"
9 #include "cpu_cas01.hpp"
10 #include "src/surf/HostImpl.hpp"
11 #include "simgrid/sg_config.h"
12
13 #include "network_interface.hpp"
14 #include "virtual_machine.hpp"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
17
18 simgrid::surf::HostModel *surf_host_model = nullptr;
19
20 /*************
21  * Callbacks *
22  *************/
23
24 namespace simgrid {
25 namespace surf {
26
27 simgrid::xbt::Extension<simgrid::s4u::Host, HostImpl> HostImpl::EXTENSION_ID;
28
29 /*********
30  * Model *
31  *********/
32 HostImpl *HostModel::createHost(const char *name, kernel::routing::NetCard *netElm, Cpu *cpu){
33   xbt_dynar_t storageList = (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name, ROUTING_STORAGE_HOST_LEVEL);
34
35   HostImpl *host = new simgrid::surf::HostImpl(surf_host_model, name, storageList, cpu);
36   XBT_DEBUG("Create host %s with %ld mounted disks", name, xbt_dynar_length(host->storage_));
37   return host;
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 (VirtualMachine *ws_vm : VirtualMachine::allVms_) {
48
49     Cpu *cpu = ws_vm->cpu_;
50
51     int is_active = lmm_constraint_used(cpu->getModel()->getMaxminSystem(), cpu->getConstraint());
52
53     if (is_active) {
54       /* some tasks exist on this VM */
55       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
56
57       /* FIXME: we should use lmm_update_variable_weight() ? */
58       /* FIXME: If we assign 1.05 and 0.05, the system makes apparently wrong values. */
59       ws_vm->action_->setPriority(1);
60
61     } else {
62       /* no task exits on this VM */
63       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
64
65       ws_vm->action_->setPriority(0);
66     }
67   }
68 }
69
70 Action *HostModel::executeParallelTask(int host_nb,
71     sg_host_t *host_list,
72     double *flops_amount,
73     double *bytes_amount,
74     double rate)
75 {
76 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
77   Action *action =nullptr;
78   if ((host_nb == 1)
79       && (cost_or_zero(bytes_amount, 0) == 0.0)){
80     action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
81   } else if ((host_nb == 1)
82            && (cost_or_zero(flops_amount, 0) == 0.0)) {
83     action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
84   } else if ((host_nb == 2)
85              && (cost_or_zero(flops_amount, 0) == 0.0)
86              && (cost_or_zero(flops_amount, 1) == 0.0)) {
87     int i,nb = 0;
88     double value = 0.0;
89
90     for (i = 0; i < host_nb * host_nb; i++) {
91       if (cost_or_zero(bytes_amount, i) > 0.0) {
92         nb++;
93         value = cost_or_zero(bytes_amount, i);
94       }
95     }
96     if (nb == 1) {
97       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
98     } else if (nb == 0) {
99        xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the ptask model");
100     } else {       
101        xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider using the ptask model");
102     }
103   } else 
104     xbt_die("This model only accepts one of the following. You should consider using the ptask model for the other cases.\n - execution with one host only and no communication\n - Self-comms with one host only\n - Communications with two hosts and no computation");
105 #undef cost_or_zero
106   xbt_free(host_list);
107   return action;
108 }
109
110 /************
111  * Resource *
112  ************/
113 HostImpl::HostImpl(simgrid::surf::HostModel *model, const char *name, xbt_dynar_t storage, Cpu *cpu)
114  : Resource(model, name)
115  , PropertyHolder(nullptr)
116  , storage_(storage), cpu_(cpu)
117 {
118   if (!EXTENSION_ID.valid())
119     EXTENSION_ID = simgrid::s4u::Host::extension_create<simgrid::surf::HostImpl>();
120   params_.ramsize = 0;
121 }
122
123 HostImpl::HostImpl(simgrid::surf::HostModel *model, const char *name, lmm_constraint_t constraint,
124                  xbt_dynar_t storage, Cpu *cpu)
125  : Resource(model, name, constraint)
126  , PropertyHolder(nullptr)
127  , storage_(storage), cpu_(cpu)
128 {
129   params_.ramsize = 0;
130 }
131
132 /** @brief use destroy() instead of this destructor */
133 HostImpl::~HostImpl()
134 {
135 }
136
137 void HostImpl::attach(simgrid::s4u::Host* host)
138 {
139   if (piface_ != nullptr)
140     xbt_die("Already attached to host %s", host->name().c_str());
141   host->extension_set(this);
142   piface_ = host;
143 }
144
145 bool HostImpl::isOn() const {
146   return cpu_->isOn();
147 }
148 bool HostImpl::isOff() const {
149   return cpu_->isOff();
150 }
151 void HostImpl::turnOn(){
152   if (isOff()) {
153     cpu_->turnOn();
154     simgrid::s4u::Host::onStateChange(*this->piface_);
155   }
156 }
157 void HostImpl::turnOff(){
158   if (isOn()) {
159     cpu_->turnOff();
160     simgrid::s4u::Host::onStateChange(*this->piface_);
161   }
162 }
163
164 simgrid::surf::Storage *HostImpl::findStorageOnMountList(const char* mount)
165 {
166   simgrid::surf::Storage *st = nullptr;
167   s_mount_t mnt;
168   unsigned int cursor;
169
170   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
171   xbt_dynar_foreach(storage_,cursor,mnt){
172     XBT_DEBUG("See '%s'",mnt.name);
173     if(!strcmp(mount,mnt.name)){
174       st = static_cast<simgrid::surf::Storage*>(mnt.storage);
175       break;
176     }
177   }
178   if(!st)
179     xbt_die("Can't find mount '%s' for '%s'", mount, getName());
180   return st;
181 }
182
183 xbt_dict_t HostImpl::getMountedStorageList()
184 {
185   s_mount_t mnt;
186   unsigned int i;
187   xbt_dict_t storage_list = xbt_dict_new_homogeneous(nullptr);
188   char *storage_name = nullptr;
189
190   xbt_dynar_foreach(storage_,i,mnt){
191     storage_name = (char *)static_cast<simgrid::surf::Storage*>(mnt.storage)->getName();
192     xbt_dict_set(storage_list,mnt.name,storage_name,nullptr);
193   }
194   return storage_list;
195 }
196
197 xbt_dynar_t HostImpl::getAttachedStorageList()
198 {
199   xbt_lib_cursor_t cursor;
200   char *key;
201   void **data;
202   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), nullptr);
203   xbt_lib_foreach(storage_lib, cursor, key, data) {
204     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != nullptr) {
205     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));
206     if(!strcmp((const char*)storage->attach_,this->getName())){
207       xbt_dynar_push_as(result, void *, (void*)storage->getName());
208     }
209   }
210   }
211   return result;
212 }
213
214 Action *HostImpl::open(const char* fullpath) {
215
216   simgrid::surf::Storage *st = nullptr;
217   s_mount_t mnt;
218   unsigned int cursor;
219   size_t longest_prefix_length = 0;
220   char *path = nullptr;
221   char *file_mount_name = nullptr;
222   char *mount_name = nullptr;
223
224   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
225   xbt_dynar_foreach(storage_,cursor,mnt)
226   {
227     XBT_DEBUG("See '%s'",mnt.name);
228     file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
229     strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
230     file_mount_name[strlen(mnt.name)] = '\0';
231
232     if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
233     {/* The current mount name is found in the full path and is bigger than the previous*/
234       longest_prefix_length = strlen(mnt.name);
235       st = static_cast<simgrid::surf::Storage*>(mnt.storage);
236     }
237     free(file_mount_name);
238   }
239   if(longest_prefix_length>0)
240   { /* Mount point found, split fullpath into mount_name and path+filename*/
241   path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
242   mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
243   strncpy(mount_name, fullpath, longest_prefix_length+1);
244   strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
245   path[strlen(fullpath)-longest_prefix_length] = '\0';
246   mount_name[longest_prefix_length] = '\0';
247   }
248   else
249     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
250
251   XBT_DEBUG("OPEN %s on disk '%s'",path, st->getName());
252   Action *action = st->open((const char*)mount_name, (const char*)path);
253   free((char*)path);
254   free((char*)mount_name);
255   return action;
256 }
257
258 Action *HostImpl::close(surf_file_t fd) {
259   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
260   XBT_DEBUG("CLOSE %s on disk '%s'",fd->name, st->getName());
261   return st->close(fd);
262 }
263
264 Action *HostImpl::read(surf_file_t fd, sg_size_t size) {
265   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
266   XBT_DEBUG("READ %s on disk '%s'",fd->name, st->getName());
267   return st->read(fd, size);
268 }
269
270 Action *HostImpl::write(surf_file_t fd, sg_size_t size) {
271   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
272   XBT_DEBUG("WRITE %s on disk '%s'",fd->name, st->getName());
273   return st->write(fd, size);
274 }
275
276 int HostImpl::unlink(surf_file_t fd) {
277   if (!fd){
278     XBT_WARN("No such file descriptor. Impossible to unlink");
279     return -1;
280   } else {
281
282     simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
283     /* Check if the file is on this storage */
284     if (!xbt_dict_get_or_null(st->content_, fd->name)){
285       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
286           st->getName());
287       return -1;
288     } else {
289       XBT_DEBUG("UNLINK %s on disk '%s'",fd->name, st->getName());
290       st->usedSize_ -= fd->size;
291
292       // Remove the file from storage
293       xbt_dict_remove(st->content_, fd->name);
294
295       xbt_free(fd->name);
296       xbt_free(fd->mount);
297       xbt_free(fd);
298       return 0;
299     }
300   }
301 }
302
303 sg_size_t HostImpl::getSize(surf_file_t fd){
304   return fd->size;
305 }
306
307 xbt_dynar_t HostImpl::getInfo( surf_file_t fd)
308 {
309   simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
310   sg_size_t *psize = xbt_new(sg_size_t, 1);
311   *psize = fd->size;
312   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), nullptr);
313   xbt_dynar_push_as(info, sg_size_t *, psize);
314   xbt_dynar_push_as(info, void *, fd->mount);
315   xbt_dynar_push_as(info, void *, (void *)st->getName());
316   xbt_dynar_push_as(info, void *, st->typeId_);
317   xbt_dynar_push_as(info, void *, st->contentType_);
318
319   return info;
320 }
321
322 sg_size_t HostImpl::fileTell(surf_file_t fd){
323   return fd->current_position;
324 }
325
326 int HostImpl::fileSeek(surf_file_t fd, sg_offset_t offset, int origin){
327
328   switch (origin) {
329   case SEEK_SET:
330     fd->current_position = offset;
331     return 0;
332   case SEEK_CUR:
333     fd->current_position += offset;
334     return 0;
335   case SEEK_END:
336     fd->current_position = fd->size + offset;
337     return 0;
338   default:
339     return -1;
340   }
341 }
342
343 int HostImpl::fileMove(surf_file_t fd, const char* fullpath){
344   /* Check if the new full path is on the same mount point */
345   if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
346     sg_size_t *psize, *new_psize;
347     psize = (sg_size_t*)
348         xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->content_,
349                              fd->name);
350     new_psize = xbt_new(sg_size_t, 1);
351     *new_psize = *psize;
352     if (psize){// src file exists
353       xbt_dict_remove(findStorageOnMountList(fd->mount)->content_, fd->name);
354       char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));
355       strncpy(path, fullpath+strlen(fd->mount),
356               strlen(fullpath)-strlen(fd->mount)+1);
357       xbt_dict_set(findStorageOnMountList(fd->mount)->content_, path,
358                    new_psize,nullptr);
359       XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
360       free(path);
361       return 0;
362     } else {
363       XBT_WARN("File %s doesn't exist", fd->name);
364       return -1;
365     }
366   } else {
367     XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.",
368              fullpath, fd->mount);
369     return -1;
370   }
371 }
372
373 xbt_dynar_t HostImpl::getVms()
374 {
375   xbt_dynar_t dyn = xbt_dynar_new(sizeof(simgrid::surf::VirtualMachine*), nullptr);
376
377   for (VirtualMachine *ws_vm : VirtualMachine::allVms_) {
378     if (this == ws_vm->getPm()->extension<simgrid::surf::HostImpl>())
379       xbt_dynar_push(dyn, &ws_vm);
380   }
381
382   return dyn;
383 }
384
385 void HostImpl::getParams(vm_params_t params)
386 {
387   *params = params_;
388 }
389
390 void HostImpl::setParams(vm_params_t params)
391 {
392   /* may check something here. */
393   params_ = *params;
394 }
395
396 }}