Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more stringification
[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 "src/surf/HostImpl.hpp"
7 #include "src/plugins/vm/VirtualMachineImpl.hpp"
8 #include <string>
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
11
12 simgrid::surf::HostModel *surf_host_model = nullptr;
13
14 /*************
15  * Callbacks *
16  *************/
17
18 namespace simgrid {
19 namespace surf {
20
21 /*********
22  * Model *
23  *********/
24
25 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
26  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
27  * active task, the dummy CPU action must be deactivated, so that the VM does
28  * not get any CPU share in the PM layer. */
29 void HostModel::adjustWeightOfDummyCpuActions()
30 {
31   /* iterate for all virtual machines */
32   for (s4u::VirtualMachine* ws_vm : vm::VirtualMachineImpl::allVms_) {
33
34     Cpu* cpu = ws_vm->pimpl_cpu;
35
36     int is_active = lmm_constraint_used(cpu->model()->getMaxminSystem(), cpu->constraint());
37
38     if (is_active) {
39       /* some tasks exist on this VM */
40       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
41
42       /* FIXME: we should use lmm_update_variable_weight() ? */
43       /* FIXME: If we assign 1.05 and 0.05, the system makes apparently wrong values. */
44       ws_vm->pimpl_vm_->action_->setPriority(1);
45
46     } else {
47       /* no task exits on this VM */
48       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
49
50       ws_vm->pimpl_vm_->action_->setPriority(0);
51     }
52   }
53 }
54
55 /* Helper function for executeParallelTask */
56 static inline double has_cost(double* array, int pos)
57 {
58   if (array)
59     return array[pos];
60   else
61     return -1.0;
62 }
63 Action* HostModel::executeParallelTask(int host_nb, simgrid::s4u::Host** host_list, double* flops_amount,
64     double* bytes_amount, double rate)
65 {
66   Action* action = nullptr;
67   if ((host_nb == 1) && (has_cost(bytes_amount, 0) <= 0)) {
68     action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
69   } else if ((host_nb == 1) && (has_cost(flops_amount, 0) <= 0)) {
70     action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
71   } else if ((host_nb == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
72     int nb = 0;
73     double value = 0.0;
74
75     for (int i = 0; i < host_nb * host_nb; i++) {
76       if (has_cost(bytes_amount, i) > 0.0) {
77         nb++;
78         value = has_cost(bytes_amount, i);
79       }
80     }
81     if (nb == 1) {
82       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
83     } else if (nb == 0) {
84       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
85           "ptask model");
86     } else {
87       xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
88           "using the ptask model");
89     }
90   } else
91     xbt_die(
92         "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
93         " - execution with one host only and no communication\n"
94         " - Self-comms with one host only\n"
95         " - Communications with two hosts and no computation");
96   xbt_free(host_list);
97   return action;
98 }
99
100 /************
101  * Resource *
102  ************/
103 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
104 {
105   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
106   delete piface_->pimpl_;
107   piface_->pimpl_ = this;
108 }
109
110 /** @brief use destroy() instead of this destructor */
111 HostImpl::~HostImpl() = default;
112
113 simgrid::surf::Storage* HostImpl::findStorageOnMountList(const char* mount)
114 {
115   simgrid::surf::Storage* st = nullptr;
116   s_mount_t mnt;
117   unsigned int cursor;
118
119   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, piface_->cname());
120   xbt_dynar_foreach (storage_, cursor, mnt) {
121     XBT_DEBUG("See '%s'", mnt.name);
122     if (!strcmp(mount, mnt.name)) {
123       st = static_cast<simgrid::surf::Storage*>(mnt.storage);
124       break;
125     }
126   }
127   if (!st)
128     xbt_die("Can't find mount '%s' for '%s'", mount, piface_->cname());
129   return st;
130 }
131
132 xbt_dict_t HostImpl::getMountedStorageList()
133 {
134   s_mount_t mnt;
135   unsigned int i;
136   xbt_dict_t storage_list = xbt_dict_new_homogeneous(nullptr);
137   char* storage_name      = nullptr;
138
139   xbt_dynar_foreach (storage_, i, mnt) {
140     storage_name = (char*)static_cast<simgrid::surf::Storage*>(mnt.storage)->cname();
141     xbt_dict_set(storage_list, mnt.name, storage_name, nullptr);
142   }
143   return storage_list;
144 }
145
146 xbt_dynar_t HostImpl::getAttachedStorageList()
147 {
148   xbt_lib_cursor_t cursor;
149   char* key;
150   void** data;
151   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), nullptr);
152   xbt_lib_foreach(storage_lib, cursor, key, data)
153   {
154     if (xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != nullptr) {
155       simgrid::surf::Storage* storage = static_cast<simgrid::surf::Storage*>(
156           xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
157       if (!strcmp(static_cast<const char*>(storage->attach_), piface_->cname())) {
158         xbt_dynar_push_as(result, void*, (void*)storage->cname());
159       }
160     }
161   }
162   return result;
163 }
164
165 Action* HostImpl::open(const char* fullpath)
166 {
167
168   simgrid::surf::Storage* st = nullptr;
169   s_mount_t mnt;
170   unsigned int cursor;
171   size_t longest_prefix_length = 0;
172   std::string path;
173   std::string mount_name;
174
175   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, piface_->cname());
176   xbt_dynar_foreach (storage_, cursor, mnt) {
177     XBT_DEBUG("See '%s'", mnt.name);
178     std::string file_mount_name = std::string(fullpath).substr(0, strlen(mnt.name));
179
180     if (!strcmp(file_mount_name.c_str(), mnt.name) &&
181         strlen(mnt.name) > longest_prefix_length) { /* The current mount name is found in the full path and is
182                                                            bigger than the previous*/
183       longest_prefix_length = strlen(mnt.name);
184       st                    = static_cast<simgrid::surf::Storage*>(mnt.storage);
185     }
186   }
187   if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/
188     mount_name = std::string(fullpath).substr(0, longest_prefix_length);
189     path       = std::string(fullpath).substr(longest_prefix_length, strlen(fullpath));
190   } else
191     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, piface_->cname());
192
193   XBT_DEBUG("OPEN %s on disk '%s'", path.c_str(), st->cname());
194   Action* action = st->open(mount_name.c_str(), path.c_str());
195   return action;
196 }
197
198 Action* HostImpl::close(surf_file_t fd)
199 {
200   simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
201   XBT_DEBUG("CLOSE %s on disk '%s'", fd->name, st->cname());
202   return st->close(fd);
203 }
204
205 Action* HostImpl::read(surf_file_t fd, sg_size_t size)
206 {
207   simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
208   XBT_DEBUG("READ %s on disk '%s'", fd->name, st->cname());
209   return st->read(fd, size);
210 }
211
212 Action* HostImpl::write(surf_file_t fd, sg_size_t size)
213 {
214   simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
215   XBT_DEBUG("WRITE %s on disk '%s'", fd->name, st->cname());
216   return st->write(fd, size);
217 }
218
219 int HostImpl::unlink(surf_file_t fd)
220 {
221   if (!fd) {
222     XBT_WARN("No such file descriptor. Impossible to unlink");
223     return -1;
224   } else {
225
226     simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
227     /* Check if the file is on this storage */
228     if (!xbt_dict_get_or_null(st->content_, fd->name)) {
229       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name, st->cname());
230       return -1;
231     } else {
232       XBT_DEBUG("UNLINK %s on disk '%s'", fd->name, st->cname());
233       st->usedSize_ -= fd->size;
234
235       // Remove the file from storage
236       xbt_dict_remove(st->content_, fd->name);
237
238       xbt_free(fd->name);
239       xbt_free(fd->mount);
240       xbt_free(fd);
241       return 0;
242     }
243   }
244 }
245
246 sg_size_t HostImpl::getSize(surf_file_t fd)
247 {
248   return fd->size;
249 }
250
251 xbt_dynar_t HostImpl::getInfo(surf_file_t fd)
252 {
253   simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
254   sg_size_t* psize           = xbt_new(sg_size_t, 1);
255   *psize                     = fd->size;
256   xbt_dynar_t info           = xbt_dynar_new(sizeof(void*), nullptr);
257   xbt_dynar_push_as(info, sg_size_t*, psize);
258   xbt_dynar_push_as(info, void*, fd->mount);
259   xbt_dynar_push_as(info, void*, (void*)st->cname());
260   xbt_dynar_push_as(info, void*, st->typeId_);
261   xbt_dynar_push_as(info, void*, st->contentType_);
262
263   return info;
264 }
265
266 sg_size_t HostImpl::fileTell(surf_file_t fd)
267 {
268   return fd->current_position;
269 }
270
271 int HostImpl::fileSeek(surf_file_t fd, sg_offset_t offset, int origin)
272 {
273
274   switch (origin) {
275   case SEEK_SET:
276     fd->current_position = offset;
277     return 0;
278   case SEEK_CUR:
279     fd->current_position += offset;
280     return 0;
281   case SEEK_END:
282     fd->current_position = fd->size + offset;
283     return 0;
284   default:
285     return -1;
286   }
287 }
288
289 int HostImpl::fileMove(surf_file_t fd, const char* fullpath)
290 {
291   /* Check if the new full path is on the same mount point */
292   if (!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
293     sg_size_t* psize = (sg_size_t*)xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->content_, fd->name);
294     if (psize) { // src file exists
295       sg_size_t* new_psize = xbt_new(sg_size_t, 1);
296       *new_psize           = *psize;
297       xbt_dict_remove(findStorageOnMountList(fd->mount)->content_, fd->name);
298       std::string path = std::string(fullpath).substr(strlen(fd->mount), strlen(fullpath));
299       xbt_dict_set(findStorageOnMountList(fd->mount)->content_, path.c_str(), new_psize, nullptr);
300       XBT_DEBUG("Move file from %s to %s, size '%llu'", fd->name, fullpath, *psize);
301       return 0;
302     } else {
303       XBT_WARN("File %s doesn't exist", fd->name);
304       return -1;
305     }
306   } else {
307     XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount);
308     return -1;
309   }
310 }
311
312 }
313 }