Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please codacy
[simgrid.git] / src / msg / msg_io.cpp
1 /* Copyright (c) 2004-2017. 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/File.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "src/msg/msg_private.h"
10 #include <numeric>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
13
14 SG_BEGIN_DECL()
15
16 /** @addtogroup msg_file
17  * (#msg_file_t) and the functions for managing it.
18  *
19  *  \see #msg_file_t
20  */
21
22 static int MSG_host_get_file_descriptor_id(msg_host_t host)
23 {
24   simgrid::MsgHostExt* priv = host->extension<simgrid::MsgHostExt>();
25   if (priv->file_descriptor_table == nullptr) {
26     priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
27     std::iota(priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
28   }
29   xbt_assert(not priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
30   int desc = priv->file_descriptor_table->back();
31   priv->file_descriptor_table->pop_back();
32   return desc;
33 }
34
35 static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
36 {
37   host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
38 }
39
40 /** \ingroup msg_file
41  *
42  * \brief Set the user data of a #msg_file_t.
43  *
44  * This functions attach \a data to \a file.
45  */
46 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
47 {
48   fd->setUserdata(data);
49   return MSG_OK;
50 }
51
52 /** \ingroup msg_file
53  *
54  * \brief Return the user data of a #msg_file_t.
55  *
56  * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
57  */
58 void* MSG_file_get_data(msg_file_t fd)
59 {
60   return fd->userdata();
61 }
62
63 /** \ingroup msg_file
64  * \brief Display information related to a file descriptor
65  *
66  * \param fd is a the file descriptor
67  */
68 void MSG_file_dump (msg_file_t fd){
69   XBT_INFO("File Descriptor information:\n"
70            "\t\tFull path: '%s'\n"
71            "\t\tSize: %llu\n"
72            "\t\tMount point: '%s'\n"
73            "\t\tStorage Id: '%s'\n"
74            "\t\tStorage Type: '%s'\n"
75            "\t\tFile Descriptor Id: %d",
76            fd->path(), fd->size(), fd->mount_point.c_str(), fd->storageId, fd->storage_type, fd->desc_id);
77 }
78
79 /** \ingroup msg_file
80  * \brief Read a file (local or remote)
81  *
82  * \param size of the file to read
83  * \param fd is a the file descriptor
84  * \return the number of bytes successfully read or -1 if an error occurred
85  */
86 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
87 {
88   sg_size_t read_size;
89
90   if (fd->size() == 0) /* Nothing to read, return */
91     return 0;
92
93   /* Find the host where the file is physically located and read it */
94   msg_storage_t storage_src           = simgrid::s4u::Storage::byName(fd->storageId);
95   msg_host_t attached_host            = storage_src->host();
96   read_size                           = fd->read(size); // TODO re-add attached_host;
97
98   if (strcmp(attached_host->cname(), MSG_host_self()->cname())) {
99     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
100     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->cname(), read_size);
101     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
102     double flops_amount[]    = {0, 0};
103     double bytes_amount[]    = {0, 0, static_cast<double>(read_size), 0};
104
105     msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
106                       nullptr);
107     msg_error_t transfer = MSG_parallel_task_execute(task);
108     MSG_task_destroy(task);
109
110     if(transfer != MSG_OK){
111       if (transfer == MSG_HOST_FAILURE)
112         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
113       if (transfer == MSG_TASK_CANCELED)
114         XBT_WARN("Transfer error, task has been canceled!");
115
116       return -1;
117     }
118   }
119   return read_size;
120 }
121
122 /** \ingroup msg_file
123  * \brief Write into a file (local or remote)
124  *
125  * \param size of the file to write
126  * \param fd is a the file descriptor
127  * \return the number of bytes successfully write or -1 if an error occurred
128  */
129 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
130 {
131   if (size == 0) /* Nothing to write, return */
132     return 0;
133
134   /* Find the host where the file is physically located (remote or local)*/
135   msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
136   msg_host_t attached_host  = storage_src->host();
137
138   if (strcmp(attached_host->cname(), MSG_host_self()->cname())) {
139     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
140     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->cname(), size);
141     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
142     double flops_amount[]    = {0, 0};
143     double bytes_amount[]    = {0, static_cast<double>(size), 0, 0};
144
145     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
146                                                nullptr);
147     msg_error_t transfer = MSG_parallel_task_execute(task);
148     MSG_task_destroy(task);
149
150     if(transfer != MSG_OK){
151       if (transfer == MSG_HOST_FAILURE)
152         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
153       if (transfer == MSG_TASK_CANCELED)
154         XBT_WARN("Transfer error, task has been canceled!");
155
156       return -1;
157     }
158   }
159   /* Write file on local or remote host */
160   // sg_size_t offset     = fd->tell();
161   sg_size_t write_size = fd->write(size); // TODO readd attached_host;
162
163   return write_size;
164 }
165
166 /** \ingroup msg_file
167  * \brief Opens the file whose name is the string pointed to by path
168  *
169  * \param fullpath is the file location on the storage
170  * \param data user data to attach to the file
171  *
172  * \return An #msg_file_t associated to the file
173  */
174 msg_file_t MSG_file_open(const char* fullpath, void* data)
175 {
176   msg_file_t fd         = new simgrid::s4u::File(fullpath, MSG_host_self());
177   fd->desc_id           = MSG_host_get_file_descriptor_id(MSG_host_self());
178   return fd;
179 }
180
181 /** \ingroup msg_file
182  * \brief Close the file
183  *
184  * \param fd is the file to close
185  * \return 0 on success or 1 on error
186  */
187 int MSG_file_close(msg_file_t fd)
188 {
189   MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
190   delete fd;
191
192   return MSG_OK;
193 }
194
195 /** \ingroup msg_file
196  * \brief Unlink the file pointed by fd
197  *
198  * \param fd is the file descriptor (#msg_file_t)
199  * \return 0 on success or 1 on error
200  */
201 msg_error_t MSG_file_unlink(msg_file_t fd)
202 {
203   /* Find the host where the file is physically located (remote or local)*/
204   msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
205   msg_host_t attached_host  = storage_src->host();
206   fd->unlink(attached_host);
207   return MSG_OK;
208 }
209
210 /** \ingroup msg_file
211  * \brief Return the size of a file
212  *
213  * \param fd is the file descriptor (#msg_file_t)
214  * \return the size of the file (as a #sg_size_t)
215  */
216 sg_size_t MSG_file_get_size(msg_file_t fd)
217 {
218   return fd->size();
219 }
220
221 /**
222  * \ingroup msg_file
223  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
224  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
225  *
226  * \param fd : file object that identifies the stream
227  * \param offset : number of bytes to offset from origin
228  * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
229  *                 in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
230  *                 SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
231  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
232  */
233 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
234 {
235   fd->seek(offset); // TODO re-add origin
236   return MSG_OK;
237 }
238
239 /**
240  * \ingroup msg_file
241  * \brief Returns the current value of the position indicator of the file
242  *
243  * \param fd : file object that identifies the stream
244  * \return On success, the current value of the position indicator is returned.
245  *
246  */
247 sg_size_t MSG_file_tell(msg_file_t fd)
248 {
249   return fd->tell();
250 }
251
252 const char *MSG_file_get_name(msg_file_t fd) {
253   xbt_assert((fd != nullptr), "Invalid parameters");
254   return fd->path();
255 }
256
257 /**
258  * \ingroup msg_file
259  * \brief Move a file to another location on the *same mount point*.
260  *
261  */
262 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
263 {
264   fd->move(fullpath);
265   return MSG_OK;
266 }
267
268 /**
269  * \ingroup msg_file
270  * \brief Copy a file to another location on a remote host.
271  * \param file : the file to move
272  * \param host : the remote host where the file has to be copied
273  * \param fullpath : the complete path destination on the remote host
274  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
275  */
276 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
277 {
278   /* Find the host where the file is physically located and read it */
279   msg_storage_t storage_src = simgrid::s4u::Storage::byName(file->storageId);
280   msg_host_t src_host       = storage_src->host();
281   MSG_file_seek(file, 0, SEEK_SET);
282   sg_size_t read_size = file->read(file->size());
283
284   /* Find the host that owns the storage where the file has to be copied */
285   msg_storage_t storage_dest = nullptr;
286   msg_host_t dst_host;
287   size_t longest_prefix_length = 0;
288
289   for (auto elm : host->mountedStorages()) {
290     std::string mount_point = std::string(fullpath).substr(0, elm.first.size());
291     if (mount_point == elm.first && elm.first.length() > longest_prefix_length) {
292       /* The current mount name is found in the full path and is bigger than the previous*/
293       longest_prefix_length = elm.first.length();
294       storage_dest          = elm.second;
295     }
296   }
297
298   if (storage_dest != nullptr) {
299     /* Mount point found, retrieve the host the storage is attached to */
300     dst_host = storage_dest->host();
301   }else{
302     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
303     return MSG_TASK_CANCELED;
304   }
305
306   XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, src_host->cname(),
307             storage_dest->host()->cname());
308   msg_host_t m_host_list[] = {src_host, dst_host};
309   double flops_amount[]    = {0, 0};
310   double bytes_amount[]    = {0, static_cast<double>(read_size), 0, 0};
311
312   msg_task_t task =
313       MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
314   msg_error_t err = MSG_parallel_task_execute(task);
315   MSG_task_destroy(task);
316
317   if (err != MSG_OK) {
318     if (err == MSG_HOST_FAILURE)
319       XBT_WARN("Transfer error, %s remote host just turned off!", storage_dest->host()->cname());
320     if (err == MSG_TASK_CANCELED)
321       XBT_WARN("Transfer error, task has been canceled!");
322
323     return err;
324   }
325
326   /* Create file on remote host, write it and close it */
327   msg_file_t fd = new simgrid::s4u::File(fullpath, dst_host, nullptr);
328   fd->write(read_size, dst_host);
329   delete fd;
330   return MSG_OK;
331 }
332
333 /**
334  * \ingroup msg_file
335  * \brief Move a file to another location on a remote host.
336  * \param file : the file to move
337  * \param host : the remote host where the file has to be moved
338  * \param fullpath : the complete path destination on the remote host
339  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
340  */
341 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
342 {
343   msg_error_t res = MSG_file_rcopy(file, host, fullpath);
344   MSG_file_unlink(file);
345   return res;
346 }
347
348 /********************************* Storage **************************************/
349 /** @addtogroup msg_storage_management
350  * (#msg_storage_t) and the functions for managing it.
351  */
352
353 /** \ingroup msg_storage_management
354  *
355  * \brief Returns the name of the #msg_storage_t.
356  *
357  * This functions checks whether a storage is a valid pointer or not and return its name.
358  */
359 const char* MSG_storage_get_name(msg_storage_t storage)
360 {
361   xbt_assert((storage != nullptr), "Invalid parameters");
362   return storage->name();
363 }
364
365 /** \ingroup msg_storage_management
366  * \brief Returns the free space size of a storage element
367  * \param storage a storage
368  * \return the free space size of the storage element (as a #sg_size_t)
369  */
370 sg_size_t MSG_storage_get_free_size(msg_storage_t storage)
371 {
372   return storage->sizeFree();
373 }
374
375 /** \ingroup msg_storage_management
376  * \brief Returns the used space size of a storage element
377  * \param storage a storage
378  * \return the used space size of the storage element (as a #sg_size_t)
379  */
380 sg_size_t MSG_storage_get_used_size(msg_storage_t storage)
381 {
382   return storage->sizeUsed();
383 }
384
385 /** \ingroup msg_storage_management
386  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
387  * \param storage a storage
388  * \return a dict containing the properties
389  */
390 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
391 {
392   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
393   return storage->properties();
394 }
395
396 /** \ingroup msg_storage_management
397  * \brief Change the value of a given storage property
398  *
399  * \param storage a storage
400  * \param name a property name
401  * \param value what to change the property to
402  */
403 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
404 {
405   storage->setProperty(name, value);
406 }
407
408 /** \ingroup m_storage_management
409  * \brief Returns the value of a given storage property
410  *
411  * \param storage a storage
412  * \param name a property name
413  * \return value of a property (or nullptr if property not set)
414  */
415 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
416 {
417   return storage->property(name);
418 }
419
420 /** \ingroup msg_storage_management
421  * \brief Finds a msg_storage_t using its name.
422  * \param name the name of a storage
423  * \return the corresponding storage
424  */
425 msg_storage_t MSG_storage_get_by_name(const char *name)
426 {
427   return simgrid::s4u::Storage::byName(name);
428 }
429
430 /** \ingroup msg_storage_management
431  * \brief Returns a dynar containing all the storage elements declared at a given point of time
432  */
433 xbt_dynar_t MSG_storages_as_dynar()
434 {
435   std::map<std::string, simgrid::s4u::Storage*>* storage_map = simgrid::s4u::allStorages();
436   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
437   for (auto s : *storage_map)
438     xbt_dynar_push(res, &(s.second));
439   delete storage_map;
440   return res;
441 }
442
443 /** \ingroup msg_storage_management
444  *
445  * \brief Set the user data of a #msg_storage_t.
446  * This functions attach \a data to \a storage if possible.
447  */
448 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
449 {
450   storage->setUserdata(data);
451   return MSG_OK;
452 }
453
454 /** \ingroup m_host_management
455  *
456  * \brief Returns the user data of a #msg_storage_t.
457  *
458  * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
459  */
460 void *MSG_storage_get_data(msg_storage_t storage)
461 {
462   xbt_assert((storage != nullptr), "Invalid parameters");
463   return storage->userdata();
464 }
465
466 /** \ingroup msg_storage_management
467  *
468  * \brief Returns the content (file list) of a #msg_storage_t.
469  * \param storage a storage
470  * \return The content of this storage element as a dict (full path file => size)
471  */
472 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
473 {
474   std::map<std::string, sg_size_t>* content = storage->content();
475   xbt_dict_t content_dict = xbt_dict_new_homogeneous(&free);
476
477   for (auto entry : *content) {
478     sg_size_t* psize = static_cast<sg_size_t*>(malloc(sizeof(sg_size_t)));
479     *psize           = entry.second;
480     xbt_dict_set(content_dict, entry.first.c_str(), psize, nullptr);
481   }
482   return content_dict;
483 }
484
485 /** \ingroup msg_storage_management
486  *
487  * \brief Returns the size of a #msg_storage_t.
488  * \param storage a storage
489  * \return The size of the storage
490  */
491 sg_size_t MSG_storage_get_size(msg_storage_t storage)
492 {
493   return storage->size();
494 }
495
496 /** \ingroup msg_storage_management
497  *
498  * \brief Returns the host name the storage is attached to
499  *
500  * This functions checks whether a storage is a valid pointer or not and return its name.
501  */
502 const char* MSG_storage_get_host(msg_storage_t storage)
503 {
504   xbt_assert((storage != nullptr), "Invalid parameters");
505   return storage->host()->cname();
506 }
507
508 SG_END_DECL()